jellyfin/Emby.Server.Implementations/TV/TVSeriesManager.cs

328 lines
13 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
2020-05-20 19:07:53 +02:00
using Jellyfin.Data.Entities;
2020-05-13 04:10:35 +02:00
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.TV;
using MediaBrowser.Model.Querying;
2020-05-20 19:07:53 +02:00
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using Series = MediaBrowser.Controller.Entities.TV.Series;
2016-11-03 08:14:14 +01:00
namespace Emby.Server.Implementations.TV
{
public class TVSeriesManager : ITVSeriesManager
{
private readonly IUserManager _userManager;
private readonly IUserDataManager _userDataManager;
private readonly ILibraryManager _libraryManager;
private readonly IServerConfigurationManager _configurationManager;
public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
{
_userManager = userManager;
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_configurationManager = configurationManager;
}
2021-09-03 18:46:34 +02:00
public QueryResult<BaseItem> GetNextUp(NextUpQuery query, DtoOptions options)
{
2021-09-03 18:46:34 +02:00
var user = _userManager.GetUserById(query.UserId);
2022-12-05 15:00:20 +01:00
if (user is null)
{
throw new ArgumentException("User not found");
}
2016-05-02 07:32:04 +02:00
string presentationUniqueKey = null;
2022-08-14 13:03:48 +02:00
if (query.SeriesId.HasValue && !query.SeriesId.Value.Equals(default))
2016-05-02 07:32:04 +02:00
{
2022-08-14 13:03:48 +02:00
if (_libraryManager.GetItemById(query.SeriesId.Value) is Series series)
2016-05-02 07:32:04 +02:00
{
2016-07-01 17:51:35 +02:00
presentationUniqueKey = GetUniqueSeriesKey(series);
2016-05-02 07:32:04 +02:00
}
}
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(presentationUniqueKey))
2017-08-01 18:45:57 +02:00
{
2021-09-03 18:46:34 +02:00
return GetResult(GetNextUpEpisodes(query, user, new[] { presentationUniqueKey }, options), query);
2017-08-01 18:45:57 +02:00
}
2018-09-12 19:26:21 +02:00
BaseItem[] parents;
2017-10-02 22:18:27 +02:00
2021-09-03 18:46:34 +02:00
if (query.ParentId.HasValue)
2017-10-02 22:18:27 +02:00
{
2021-09-03 18:46:34 +02:00
var parent = _libraryManager.GetItemById(query.ParentId.Value);
2018-09-12 19:26:21 +02:00
2022-12-05 15:01:13 +01:00
if (parent is not null)
2017-10-02 22:18:27 +02:00
{
2018-09-12 19:26:21 +02:00
parents = new[] { parent };
}
else
{
parents = Array.Empty<BaseItem>();
2017-10-02 22:18:27 +02:00
}
}
else
{
2018-09-12 19:26:21 +02:00
parents = _libraryManager.GetUserRootFolder().GetChildren(user, true)
2017-10-02 22:18:27 +02:00
.Where(i => i is Folder)
2020-12-13 16:15:26 +01:00
.Where(i => !user.GetPreferenceValues<Guid>(PreferenceKind.LatestItemExcludes).Contains(i.Id))
2018-09-12 19:26:21 +02:00
.ToArray();
2017-10-02 22:18:27 +02:00
}
2016-07-05 08:09:11 +02:00
2021-09-03 18:46:34 +02:00
return GetNextUp(query, parents, options);
}
2021-09-03 18:46:34 +02:00
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, BaseItem[] parentsFolders, DtoOptions options)
{
2014-09-14 17:10:51 +02:00
var user = _userManager.GetUserById(request.UserId);
2022-12-05 15:00:20 +01:00
if (user is null)
{
throw new ArgumentException("User not found");
}
2016-05-02 07:32:04 +02:00
string presentationUniqueKey = null;
int? limit = null;
2022-08-14 13:03:48 +02:00
if (request.SeriesId.HasValue && !request.SeriesId.Value.Equals(default))
2016-05-02 07:32:04 +02:00
{
2022-08-14 13:03:48 +02:00
if (_libraryManager.GetItemById(request.SeriesId.Value) is Series series)
2016-05-02 07:32:04 +02:00
{
2016-07-01 17:51:35 +02:00
presentationUniqueKey = GetUniqueSeriesKey(series);
2016-05-02 07:32:04 +02:00
limit = 1;
}
}
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(presentationUniqueKey))
2017-08-01 18:45:57 +02:00
{
2021-09-03 18:46:34 +02:00
return GetResult(GetNextUpEpisodes(request, user, new[] { presentationUniqueKey }, options), request);
2017-08-01 18:45:57 +02:00
}
if (limit.HasValue)
2016-07-05 08:09:11 +02:00
{
limit = limit.Value + 10;
}
var items = _libraryManager
2020-07-15 19:04:36 +02:00
.GetItemList(
new InternalItemsQuery(user)
{
2021-12-12 03:31:30 +01:00
IncludeItemTypes = new[] { BaseItemKind.Episode },
2021-12-24 22:18:24 +01:00
OrderBy = new[] { (ItemSortBy.DatePlayed, SortOrder.Descending) },
2020-07-15 19:04:36 +02:00
SeriesPresentationUniqueKey = presentationUniqueKey,
Limit = limit,
DtoOptions = new DtoOptions { Fields = new[] { ItemFields.SeriesPresentationUniqueKey }, EnableImages = false },
GroupBySeriesPresentationUniqueKey = true
2021-12-24 18:28:27 +01:00
},
parentsFolders.ToList())
.Cast<Episode>()
.Where(episode => !string.IsNullOrEmpty(episode.SeriesPresentationUniqueKey))
2022-03-02 16:17:24 +01:00
.Select(GetUniqueSeriesKey)
.ToList();
// Avoid implicitly captured closure
2021-09-03 18:46:34 +02:00
var episodes = GetNextUpEpisodes(request, user, items, options);
2017-03-21 18:31:40 +01:00
return GetResult(episodes, request);
}
private IEnumerable<Episode> GetNextUpEpisodes(NextUpQuery request, User user, IReadOnlyList<string> seriesKeys, DtoOptions dtoOptions)
{
var allNextUp = seriesKeys.Select(i => GetNextUp(i, user, dtoOptions, false));
if (request.EnableRewatching)
{
allNextUp = allNextUp.Concat(
seriesKeys.Select(i => GetNextUp(i, user, dtoOptions, true)))
.OrderByDescending(i => i.LastWatchedDate);
}
2016-12-12 09:53:25 +01:00
// If viewing all next up for all series, remove first episodes
// But if that returns empty, keep those first episodes (avoid completely empty view)
2022-08-14 13:03:48 +02:00
var alwaysEnableFirstEpisode = request.SeriesId.HasValue && !request.SeriesId.Value.Equals(default);
var anyFound = false;
2016-07-23 22:27:22 +02:00
return allNextUp
2016-12-12 06:49:19 +01:00
.Where(i =>
{
if (request.DisableFirstEpisode)
{
return i.LastWatchedDate != DateTime.MinValue;
}
if (alwaysEnableFirstEpisode || (i.LastWatchedDate != DateTime.MinValue && i.LastWatchedDate.Date >= request.NextUpDateCutoff))
{
anyFound = true;
return true;
}
return !anyFound && i.LastWatchedDate == DateTime.MinValue;
2016-12-12 06:49:19 +01:00
})
.Select(i => i.GetEpisodeFunction())
2022-12-05 15:01:13 +01:00
.Where(i => i is not null);
}
private static string GetUniqueSeriesKey(Episode episode)
2017-08-01 18:45:57 +02:00
{
return episode.SeriesPresentationUniqueKey;
}
private static string GetUniqueSeriesKey(Series series)
2016-07-01 17:51:35 +02:00
{
return series.GetPresentationUniqueKey();
2016-07-01 17:51:35 +02:00
}
/// <summary>
/// Gets the next up.
/// </summary>
/// <returns>Task{Episode}.</returns>
private (DateTime LastWatchedDate, Func<Episode> GetEpisodeFunction) GetNextUp(string seriesKey, User user, DtoOptions dtoOptions, bool rewatching)
{
2022-02-20 18:05:57 +01:00
var lastQuery = new InternalItemsQuery(user)
{
2017-05-24 21:12:55 +02:00
AncestorWithPresentationUniqueKey = null,
SeriesPresentationUniqueKey = seriesKey,
2021-12-12 03:31:30 +01:00
IncludeItemTypes = new[] { BaseItemKind.Episode },
2016-06-29 18:31:01 +02:00
IsPlayed = true,
2016-06-14 21:21:26 +02:00
Limit = 1,
2016-12-12 06:49:19 +01:00
ParentIndexNumberNotEquals = 0,
2019-01-13 21:37:13 +01:00
DtoOptions = new DtoOptions
2016-12-12 06:49:19 +01:00
{
Fields = new[] { ItemFields.SortName },
2016-12-12 06:49:19 +01:00
EnableImages = false
}
2022-02-20 18:05:57 +01:00
};
// If rewatching is enabled, sort first by date played and then by season and episode numbers
lastQuery.OrderBy = rewatching
? new[] { (ItemSortBy.DatePlayed, SortOrder.Descending), (ItemSortBy.ParentIndexNumber, SortOrder.Descending), (ItemSortBy.IndexNumber, SortOrder.Descending) }
: new[] { (ItemSortBy.ParentIndexNumber, SortOrder.Descending), (ItemSortBy.IndexNumber, SortOrder.Descending) };
2022-02-20 18:05:57 +01:00
var lastWatchedEpisode = _libraryManager.GetItemList(lastQuery).Cast<Episode>().FirstOrDefault();
Episode GetEpisode()
2016-06-04 02:15:14 +02:00
{
2022-02-20 18:05:57 +01:00
var nextQuery = new InternalItemsQuery(user)
2016-11-21 18:17:26 +01:00
{
2017-05-24 21:12:55 +02:00
AncestorWithPresentationUniqueKey = null,
SeriesPresentationUniqueKey = seriesKey,
2021-12-12 03:31:30 +01:00
IncludeItemTypes = new[] { BaseItemKind.Episode },
OrderBy = new[] { (ItemSortBy.ParentIndexNumber, SortOrder.Ascending), (ItemSortBy.IndexNumber, SortOrder.Ascending) },
2016-11-21 18:17:26 +01:00
Limit = 1,
2022-02-20 18:05:57 +01:00
IsPlayed = rewatching,
2016-11-21 18:17:26 +01:00
IsVirtualItem = false,
ParentIndexNumberNotEquals = 0,
DtoOptions = dtoOptions
2022-02-20 18:05:57 +01:00
};
// Locate the next up episode based on the last watched episode's season and episode number
var lastWatchedParentIndexNumber = lastWatchedEpisode?.ParentIndexNumber;
var lastWatchedIndexNumber = lastWatchedEpisode?.IndexNumberEnd ?? lastWatchedEpisode?.IndexNumber;
if (lastWatchedParentIndexNumber.HasValue && lastWatchedIndexNumber.HasValue)
2022-02-20 18:05:57 +01:00
{
nextQuery.MinParentAndIndexNumber = (lastWatchedParentIndexNumber.Value, lastWatchedIndexNumber.Value + 1);
2022-02-20 18:05:57 +01:00
}
var nextEpisode = _libraryManager.GetItemList(nextQuery).Cast<Episode>().FirstOrDefault();
if (_configurationManager.Configuration.DisplaySpecialsWithinSeasons)
{
var consideredEpisodes = _libraryManager.GetItemList(new InternalItemsQuery(user)
{
AncestorWithPresentationUniqueKey = null,
SeriesPresentationUniqueKey = seriesKey,
ParentIndexNumber = 0,
2021-12-12 03:31:30 +01:00
IncludeItemTypes = new[] { BaseItemKind.Episode },
2022-02-20 18:05:57 +01:00
IsPlayed = rewatching,
IsVirtualItem = false,
DtoOptions = dtoOptions
})
.Cast<Episode>()
2022-12-05 15:01:13 +01:00
.Where(episode => episode.AirsBeforeSeasonNumber is not null || episode.AirsAfterSeasonNumber is not null)
.ToList();
2022-12-05 15:01:13 +01:00
if (lastWatchedEpisode is not null)
{
// Last watched episode is added, because there could be specials that aired before the last watched episode
consideredEpisodes.Add(lastWatchedEpisode);
}
2022-12-05 15:01:13 +01:00
if (nextEpisode is not null)
{
consideredEpisodes.Add(nextEpisode);
}
var sortedConsideredEpisodes = _libraryManager.Sort(consideredEpisodes, user, new[] { (ItemSortBy.AiredEpisodeOrder, SortOrder.Ascending) })
.Cast<Episode>();
2022-12-05 15:01:13 +01:00
if (lastWatchedEpisode is not null)
{
sortedConsideredEpisodes = sortedConsideredEpisodes.SkipWhile(episode => !episode.Id.Equals(lastWatchedEpisode.Id)).Skip(1);
}
nextEpisode = sortedConsideredEpisodes.FirstOrDefault();
}
2022-12-05 15:01:13 +01:00
if (nextEpisode is not null)
{
2020-12-01 19:33:18 +01:00
var userData = _userDataManager.GetUserData(user, nextEpisode);
2020-12-01 19:33:18 +01:00
if (userData.PlaybackPositionTicks > 0)
{
return null;
}
}
2020-12-01 19:26:51 +01:00
return nextEpisode;
}
2016-06-14 21:21:26 +02:00
2022-12-05 15:01:13 +01:00
if (lastWatchedEpisode is not null)
2016-06-14 21:21:26 +02:00
{
var userData = _userDataManager.GetUserData(user, lastWatchedEpisode);
2016-06-04 02:15:14 +02:00
2016-07-01 17:51:35 +02:00
var lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
return (lastWatchedDate, GetEpisode);
2016-06-04 02:15:14 +02:00
}
2015-01-23 07:15:15 +01:00
// Return the first episode
return (DateTime.MinValue, GetEpisode);
}
private static QueryResult<BaseItem> GetResult(IEnumerable<BaseItem> items, NextUpQuery query)
{
2017-03-21 18:31:40 +01:00
int totalCount = 0;
2017-03-21 18:31:40 +01:00
if (query.EnableTotalRecordCount)
{
2017-03-21 18:31:40 +01:00
var list = items.ToList();
totalCount = list.Count;
items = list;
}
2017-03-21 18:31:40 +01:00
if (query.StartIndex.HasValue)
{
items = items.Skip(query.StartIndex.Value);
}
2020-06-15 23:43:52 +02:00
2017-03-21 18:31:40 +01:00
if (query.Limit.HasValue)
{
2017-03-21 18:31:40 +01:00
items = items.Take(query.Limit.Value);
}
2022-01-20 16:46:17 +01:00
return new QueryResult<BaseItem>(
query.StartIndex,
totalCount,
items.ToArray());
}
}
}