| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Jellyfin.Data; |
| | | 7 | | using Jellyfin.Data.Enums; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using Jellyfin.Database.Implementations.Enums; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using MediaBrowser.Controller.Configuration; |
| | | 12 | | using MediaBrowser.Controller.Dto; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Controller.TV; |
| | | 16 | | using MediaBrowser.Model.Querying; |
| | | 17 | | using Episode = MediaBrowser.Controller.Entities.TV.Episode; |
| | | 18 | | using Series = MediaBrowser.Controller.Entities.TV.Series; |
| | | 19 | | |
| | | 20 | | namespace Emby.Server.Implementations.TV |
| | | 21 | | { |
| | | 22 | | public class TVSeriesManager : ITVSeriesManager |
| | | 23 | | { |
| | | 24 | | private readonly IUserDataManager _userDataManager; |
| | | 25 | | private readonly ILibraryManager _libraryManager; |
| | | 26 | | private readonly IServerConfigurationManager _configurationManager; |
| | | 27 | | |
| | | 28 | | public TVSeriesManager(IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationMan |
| | | 29 | | { |
| | 21 | 30 | | _userDataManager = userDataManager; |
| | 21 | 31 | | _libraryManager = libraryManager; |
| | 21 | 32 | | _configurationManager = configurationManager; |
| | 21 | 33 | | } |
| | | 34 | | |
| | | 35 | | public QueryResult<BaseItem> GetNextUp(NextUpQuery query, DtoOptions options) |
| | | 36 | | { |
| | 0 | 37 | | var user = query.User; |
| | | 38 | | |
| | 0 | 39 | | string? presentationUniqueKey = null; |
| | 0 | 40 | | if (!query.SeriesId.IsNullOrEmpty()) |
| | | 41 | | { |
| | 0 | 42 | | if (_libraryManager.GetItemById(query.SeriesId.Value) is Series series) |
| | | 43 | | { |
| | 0 | 44 | | presentationUniqueKey = GetUniqueSeriesKey(series); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | if (!string.IsNullOrEmpty(presentationUniqueKey)) |
| | | 49 | | { |
| | 0 | 50 | | return GetResult(GetNextUpEpisodes(query, user, new[] { presentationUniqueKey }, options), query); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | BaseItem[] parents; |
| | | 54 | | |
| | 0 | 55 | | if (query.ParentId.HasValue) |
| | | 56 | | { |
| | 0 | 57 | | var parent = _libraryManager.GetItemById(query.ParentId.Value); |
| | | 58 | | |
| | 0 | 59 | | if (parent is not null) |
| | | 60 | | { |
| | 0 | 61 | | parents = new[] { parent }; |
| | | 62 | | } |
| | | 63 | | else |
| | | 64 | | { |
| | 0 | 65 | | parents = Array.Empty<BaseItem>(); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | else |
| | | 69 | | { |
| | 0 | 70 | | parents = _libraryManager.GetUserRootFolder().GetChildren(user, true) |
| | 0 | 71 | | .Where(i => i is Folder) |
| | 0 | 72 | | .Where(i => !user.GetPreferenceValues<Guid>(PreferenceKind.LatestItemExcludes).Contains(i.Id)) |
| | 0 | 73 | | .ToArray(); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return GetNextUp(query, parents, options); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public QueryResult<BaseItem> GetNextUp(NextUpQuery request, BaseItem[] parentsFolders, DtoOptions options) |
| | | 80 | | { |
| | 0 | 81 | | var user = request.User; |
| | | 82 | | |
| | 0 | 83 | | string? presentationUniqueKey = null; |
| | 0 | 84 | | int? limit = null; |
| | 0 | 85 | | if (!request.SeriesId.IsNullOrEmpty()) |
| | | 86 | | { |
| | 0 | 87 | | if (_libraryManager.GetItemById(request.SeriesId.Value) is Series series) |
| | | 88 | | { |
| | 0 | 89 | | presentationUniqueKey = GetUniqueSeriesKey(series); |
| | 0 | 90 | | limit = 1; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (!string.IsNullOrEmpty(presentationUniqueKey)) |
| | | 95 | | { |
| | 0 | 96 | | return GetResult(GetNextUpEpisodes(request, user, [presentationUniqueKey], options), request); |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | if (limit.HasValue) |
| | | 100 | | { |
| | 0 | 101 | | limit = limit.Value + 10; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | var nextUpSeriesKeys = _libraryManager.GetNextUpSeriesKeys(new InternalItemsQuery(user) { Limit = limit }, p |
| | | 105 | | |
| | 0 | 106 | | var episodes = GetNextUpEpisodes(request, user, nextUpSeriesKeys, options); |
| | | 107 | | |
| | 0 | 108 | | return GetResult(episodes, request); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private IEnumerable<Episode> GetNextUpEpisodes(NextUpQuery request, User user, IReadOnlyList<string> seriesKeys, |
| | | 112 | | { |
| | 0 | 113 | | var allNextUp = seriesKeys.Select(i => GetNextUp(i, user, dtoOptions, request.EnableResumable, false)); |
| | | 114 | | |
| | 0 | 115 | | if (request.EnableRewatching) |
| | | 116 | | { |
| | 0 | 117 | | allNextUp = allNextUp |
| | 0 | 118 | | .Concat(seriesKeys.Select(i => GetNextUp(i, user, dtoOptions, false, true))) |
| | 0 | 119 | | .OrderByDescending(i => i.LastWatchedDate); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | return allNextUp |
| | 0 | 123 | | .Select(i => i.GetEpisodeFunction()) |
| | 0 | 124 | | .Where(i => i is not null)!; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static string GetUniqueSeriesKey(Series series) |
| | | 128 | | { |
| | 0 | 129 | | return series.GetPresentationUniqueKey(); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets the next up. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <returns>Task{Episode}.</returns> |
| | | 136 | | private (DateTime LastWatchedDate, Func<Episode?> GetEpisodeFunction) GetNextUp(string seriesKey, User user, Dto |
| | | 137 | | { |
| | 0 | 138 | | var lastQuery = new InternalItemsQuery(user) |
| | 0 | 139 | | { |
| | 0 | 140 | | AncestorWithPresentationUniqueKey = null, |
| | 0 | 141 | | SeriesPresentationUniqueKey = seriesKey, |
| | 0 | 142 | | IncludeItemTypes = [BaseItemKind.Episode], |
| | 0 | 143 | | IsPlayed = true, |
| | 0 | 144 | | Limit = 1, |
| | 0 | 145 | | ParentIndexNumberNotEquals = 0, |
| | 0 | 146 | | DtoOptions = new DtoOptions |
| | 0 | 147 | | { |
| | 0 | 148 | | Fields = [ItemFields.SortName], |
| | 0 | 149 | | EnableImages = false |
| | 0 | 150 | | } |
| | 0 | 151 | | }; |
| | | 152 | | |
| | | 153 | | // If including played results, sort first by date played and then by season and episode numbers |
| | 0 | 154 | | lastQuery.OrderBy = includePlayed |
| | 0 | 155 | | ? new[] { (ItemSortBy.DatePlayed, SortOrder.Descending), (ItemSortBy.ParentIndexNumber, SortOrder.Descen |
| | 0 | 156 | | : new[] { (ItemSortBy.ParentIndexNumber, SortOrder.Descending), (ItemSortBy.IndexNumber, SortOrder.Desce |
| | | 157 | | |
| | 0 | 158 | | var lastWatchedEpisode = _libraryManager.GetItemList(lastQuery).Cast<Episode>().FirstOrDefault(); |
| | | 159 | | |
| | | 160 | | Episode? GetEpisode() |
| | | 161 | | { |
| | | 162 | | var nextQuery = new InternalItemsQuery(user) |
| | | 163 | | { |
| | | 164 | | AncestorWithPresentationUniqueKey = null, |
| | | 165 | | SeriesPresentationUniqueKey = seriesKey, |
| | | 166 | | IncludeItemTypes = [BaseItemKind.Episode], |
| | | 167 | | OrderBy = [(ItemSortBy.ParentIndexNumber, SortOrder.Ascending), (ItemSortBy.IndexNumber, SortOrder.A |
| | | 168 | | Limit = 1, |
| | | 169 | | IsPlayed = includePlayed, |
| | | 170 | | IsVirtualItem = false, |
| | | 171 | | ParentIndexNumberNotEquals = 0, |
| | | 172 | | DtoOptions = dtoOptions |
| | | 173 | | }; |
| | | 174 | | |
| | | 175 | | // Locate the next up episode based on the last watched episode's season and episode number |
| | | 176 | | var lastWatchedParentIndexNumber = lastWatchedEpisode?.ParentIndexNumber; |
| | | 177 | | var lastWatchedIndexNumber = lastWatchedEpisode?.IndexNumberEnd ?? lastWatchedEpisode?.IndexNumber; |
| | | 178 | | if (lastWatchedParentIndexNumber.HasValue && lastWatchedIndexNumber.HasValue) |
| | | 179 | | { |
| | | 180 | | nextQuery.MinParentAndIndexNumber = (lastWatchedParentIndexNumber.Value, lastWatchedIndexNumber.Valu |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | var nextEpisode = _libraryManager.GetItemList(nextQuery).Cast<Episode>().FirstOrDefault(); |
| | | 184 | | |
| | | 185 | | if (_configurationManager.Configuration.DisplaySpecialsWithinSeasons) |
| | | 186 | | { |
| | | 187 | | var consideredEpisodes = _libraryManager.GetItemList(new InternalItemsQuery(user) |
| | | 188 | | { |
| | | 189 | | AncestorWithPresentationUniqueKey = null, |
| | | 190 | | SeriesPresentationUniqueKey = seriesKey, |
| | | 191 | | ParentIndexNumber = 0, |
| | | 192 | | IncludeItemTypes = [BaseItemKind.Episode], |
| | | 193 | | IsPlayed = includePlayed, |
| | | 194 | | IsVirtualItem = false, |
| | | 195 | | DtoOptions = dtoOptions |
| | | 196 | | }) |
| | | 197 | | .Cast<Episode>() |
| | | 198 | | .Where(episode => episode.AirsBeforeSeasonNumber is not null || episode.AirsAfterSeasonNumber is not |
| | | 199 | | .ToList(); |
| | | 200 | | |
| | | 201 | | if (lastWatchedEpisode is not null) |
| | | 202 | | { |
| | | 203 | | // Last watched episode is added, because there could be specials that aired before the last wat |
| | | 204 | | consideredEpisodes.Add(lastWatchedEpisode); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | if (nextEpisode is not null) |
| | | 208 | | { |
| | | 209 | | consideredEpisodes.Add(nextEpisode); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | var sortedConsideredEpisodes = _libraryManager.Sort(consideredEpisodes, user, [(ItemSortBy.AiredEpis |
| | | 213 | | .Cast<Episode>(); |
| | | 214 | | if (lastWatchedEpisode is not null) |
| | | 215 | | { |
| | | 216 | | sortedConsideredEpisodes = sortedConsideredEpisodes.SkipWhile(episode => !episode.Id.Equals(last |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | nextEpisode = sortedConsideredEpisodes.FirstOrDefault(); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | if (nextEpisode is not null && !includeResumable) |
| | | 223 | | { |
| | | 224 | | var userData = _userDataManager.GetUserData(user, nextEpisode); |
| | | 225 | | |
| | | 226 | | if (userData?.PlaybackPositionTicks > 0) |
| | | 227 | | { |
| | | 228 | | return null; |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | return nextEpisode; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | if (lastWatchedEpisode is not null) |
| | | 236 | | { |
| | 0 | 237 | | var userData = _userDataManager.GetUserData(user, lastWatchedEpisode); |
| | | 238 | | |
| | 0 | 239 | | if (userData is null) |
| | | 240 | | { |
| | 0 | 241 | | return (DateTime.MinValue, GetEpisode); |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | var lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue.AddDays(1); |
| | | 245 | | |
| | 0 | 246 | | return (lastWatchedDate, GetEpisode); |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | // Return the first episode |
| | 0 | 250 | | return (DateTime.MinValue, GetEpisode); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | private static QueryResult<BaseItem> GetResult(IEnumerable<BaseItem> items, NextUpQuery query) |
| | | 254 | | { |
| | 0 | 255 | | int totalCount = 0; |
| | | 256 | | |
| | 0 | 257 | | if (query.EnableTotalRecordCount) |
| | | 258 | | { |
| | 0 | 259 | | var list = items.ToList(); |
| | 0 | 260 | | totalCount = list.Count; |
| | 0 | 261 | | items = list; |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | if (query.StartIndex.HasValue) |
| | | 265 | | { |
| | 0 | 266 | | items = items.Skip(query.StartIndex.Value); |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | if (query.Limit.HasValue) |
| | | 270 | | { |
| | 0 | 271 | | items = items.Take(query.Limit.Value); |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | return new QueryResult<BaseItem>( |
| | 0 | 275 | | query.StartIndex, |
| | 0 | 276 | | totalCount, |
| | 0 | 277 | | items.ToArray()); |
| | | 278 | | } |
| | | 279 | | } |
| | | 280 | | } |