mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-09 01:08:45 +01:00
Fix ContinueWatching and Nextup handling
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Jellyfin.Data;
|
||||
using Jellyfin.Data.Enums;
|
||||
@@ -136,11 +137,15 @@ namespace Emby.Server.Implementations.TV
|
||||
|
||||
if (nextEpisode is not null)
|
||||
{
|
||||
// The last played date and the version that was actually played live on the version item's user data
|
||||
// The played state propagated to the sibling versions carries no date
|
||||
var (playedVersion, lastPlayedDate) = GetMostRecentlyPlayedVersion(result.LastWatched, user);
|
||||
nextEpisode = GetPreferredVersion(nextEpisode, result.LastWatched, playedVersion);
|
||||
|
||||
DateTime lastWatchedDate = DateTime.MinValue;
|
||||
if (result.LastWatched is not null)
|
||||
{
|
||||
var userData = _userDataManager.GetUserData(user, result.LastWatched);
|
||||
lastWatchedDate = userData?.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
|
||||
lastWatchedDate = lastPlayedDate ?? DateTime.MinValue.AddDays(1);
|
||||
}
|
||||
|
||||
nextUpList.Add((lastWatchedDate, nextEpisode));
|
||||
@@ -152,11 +157,13 @@ namespace Emby.Server.Implementations.TV
|
||||
|
||||
if (nextPlayedEpisode is not null)
|
||||
{
|
||||
var (playedVersion, lastPlayedDate) = GetMostRecentlyPlayedVersion(result.LastWatchedForRewatching, user);
|
||||
nextPlayedEpisode = GetPreferredVersion(nextPlayedEpisode, result.LastWatchedForRewatching, playedVersion);
|
||||
|
||||
DateTime rewatchLastWatchedDate = DateTime.MinValue;
|
||||
if (result.LastWatchedForRewatching is not null)
|
||||
{
|
||||
var userData = _userDataManager.GetUserData(user, result.LastWatchedForRewatching);
|
||||
rewatchLastWatchedDate = userData?.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
|
||||
rewatchLastWatchedDate = lastPlayedDate ?? DateTime.MinValue.AddDays(1);
|
||||
}
|
||||
|
||||
nextUpList.Add((rewatchLastWatchedDate, nextPlayedEpisode));
|
||||
@@ -219,10 +226,13 @@ namespace Emby.Server.Implementations.TV
|
||||
|
||||
if (nextEpisode is not null && !includeResumable)
|
||||
{
|
||||
var userData = _userDataManager.GetUserData(user, nextEpisode);
|
||||
if (userData?.PlaybackPositionTicks > 0)
|
||||
// The resume progress may live on an alternate version
|
||||
foreach (var version in nextEpisode.GetAllVersions())
|
||||
{
|
||||
return null;
|
||||
if (_userDataManager.GetUserData(user, version)?.PlaybackPositionTicks > 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,6 +247,78 @@ namespace Emby.Server.Implementations.TV
|
||||
return DetermineNextEpisode(result, user, includeSpecials, includeResumable: false, includePlayed: true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the last watched episode that was actually played, together with its last played date.
|
||||
/// The version that was played carries the most recent LastPlayedDate.
|
||||
/// dates.
|
||||
/// </summary>
|
||||
/// <param name="lastWatched">The last watched episode (any version).</param>
|
||||
/// <param name="user">The user.</param>
|
||||
/// <returns>The played version and its last played date.</returns>
|
||||
private (Video? PlayedVersion, DateTime? LastPlayedDate) GetMostRecentlyPlayedVersion(BaseItem? lastWatched, User user)
|
||||
{
|
||||
if (lastWatched is not Video lastWatchedVideo)
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
Video? playedVersion = null;
|
||||
DateTime? lastPlayedDate = null;
|
||||
foreach (var version in lastWatchedVideo.GetAllVersions())
|
||||
{
|
||||
var data = _userDataManager.GetUserData(user, version);
|
||||
if (data?.LastPlayedDate is { } date && (lastPlayedDate is null || date > lastPlayedDate))
|
||||
{
|
||||
lastPlayedDate = date;
|
||||
playedVersion = version;
|
||||
}
|
||||
}
|
||||
|
||||
return (playedVersion, lastPlayedDate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When the last watched episode was played as an alternate version, prefer the next episode's version with the matching name,
|
||||
/// so Next Up continues in the version the user has been watching instead of falling back to the primary.
|
||||
/// </summary>
|
||||
/// <param name="nextEpisode">The determined next episode (a primary).</param>
|
||||
/// <param name="lastWatched">The last watched episode.</param>
|
||||
/// <param name="playedVersion">The version of the last watched episode that was played.</param>
|
||||
/// <returns>The matching version of the next episode, or the episode itself.</returns>
|
||||
private Episode GetPreferredVersion(Episode nextEpisode, BaseItem? lastWatched, Video? playedVersion)
|
||||
{
|
||||
// No version preference, or the primary was played
|
||||
if (lastWatched is not Video lastWatchedVideo
|
||||
|| playedVersion is null
|
||||
|| !playedVersion.PrimaryVersionId.HasValue)
|
||||
{
|
||||
return nextEpisode;
|
||||
}
|
||||
|
||||
// Match by version name
|
||||
var playedVersionId = playedVersion.Id.ToString("N", CultureInfo.InvariantCulture);
|
||||
var playedVersionName = lastWatchedVideo.GetMediaSources(false)
|
||||
.FirstOrDefault(source => string.Equals(source.Id, playedVersionId, StringComparison.OrdinalIgnoreCase))?.Name;
|
||||
|
||||
if (string.IsNullOrEmpty(playedVersionName))
|
||||
{
|
||||
return nextEpisode;
|
||||
}
|
||||
|
||||
var matchingSource = nextEpisode.GetMediaSources(false)
|
||||
.FirstOrDefault(source => string.Equals(source.Name, playedVersionName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (matchingSource is not null
|
||||
&& Guid.TryParse(matchingSource.Id, out var matchingId)
|
||||
&& !matchingId.Equals(nextEpisode.Id)
|
||||
&& _libraryManager.GetItemById<Episode>(matchingId) is { } matchingVersion)
|
||||
{
|
||||
return matchingVersion;
|
||||
}
|
||||
|
||||
return nextEpisode;
|
||||
}
|
||||
|
||||
private static string GetUniqueSeriesKey(Series series)
|
||||
{
|
||||
return series.GetPresentationUniqueKey();
|
||||
|
||||
Reference in New Issue
Block a user