TVSeriesManager.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using Jellyfin.Data.Entities;
  7. using Jellyfin.Data.Enums;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Dto;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.TV;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Querying;
  15. using Episode = MediaBrowser.Controller.Entities.TV.Episode;
  16. using Series = MediaBrowser.Controller.Entities.TV.Series;
  17. namespace Emby.Server.Implementations.TV
  18. {
  19. public class TVSeriesManager : ITVSeriesManager
  20. {
  21. private readonly IUserManager _userManager;
  22. private readonly IUserDataManager _userDataManager;
  23. private readonly ILibraryManager _libraryManager;
  24. public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager)
  25. {
  26. _userManager = userManager;
  27. _userDataManager = userDataManager;
  28. _libraryManager = libraryManager;
  29. }
  30. public QueryResult<BaseItem> GetNextUp(NextUpQuery request, DtoOptions dtoOptions)
  31. {
  32. var user = _userManager.GetUserById(request.UserId);
  33. if (user == null)
  34. {
  35. throw new ArgumentException("User not found");
  36. }
  37. string presentationUniqueKey = null;
  38. if (!string.IsNullOrEmpty(request.SeriesId))
  39. {
  40. var series = _libraryManager.GetItemById(request.SeriesId) as Series;
  41. if (series != null)
  42. {
  43. presentationUniqueKey = GetUniqueSeriesKey(series);
  44. }
  45. }
  46. if (!string.IsNullOrEmpty(presentationUniqueKey))
  47. {
  48. return GetResult(GetNextUpEpisodes(request, user, new[] { presentationUniqueKey }, dtoOptions), request);
  49. }
  50. BaseItem[] parents;
  51. if (request.ParentId.HasValue)
  52. {
  53. var parent = _libraryManager.GetItemById(request.ParentId.Value);
  54. if (parent != null)
  55. {
  56. parents = new[] { parent };
  57. }
  58. else
  59. {
  60. parents = Array.Empty<BaseItem>();
  61. }
  62. }
  63. else
  64. {
  65. parents = _libraryManager.GetUserRootFolder().GetChildren(user, true)
  66. .Where(i => i is Folder)
  67. .Where(i => !user.GetPreferenceValues<Guid>(PreferenceKind.LatestItemExcludes).Contains(i.Id))
  68. .ToArray();
  69. }
  70. return GetNextUp(request, parents, dtoOptions);
  71. }
  72. public QueryResult<BaseItem> GetNextUp(NextUpQuery request, BaseItem[] parentsFolders, DtoOptions dtoOptions)
  73. {
  74. var user = _userManager.GetUserById(request.UserId);
  75. if (user == null)
  76. {
  77. throw new ArgumentException("User not found");
  78. }
  79. string presentationUniqueKey = null;
  80. int? limit = null;
  81. if (!string.IsNullOrEmpty(request.SeriesId))
  82. {
  83. var series = _libraryManager.GetItemById(request.SeriesId) as Series;
  84. if (series != null)
  85. {
  86. presentationUniqueKey = GetUniqueSeriesKey(series);
  87. limit = 1;
  88. }
  89. }
  90. if (!string.IsNullOrEmpty(presentationUniqueKey))
  91. {
  92. return GetResult(GetNextUpEpisodes(request, user, new[] { presentationUniqueKey }, dtoOptions), request);
  93. }
  94. if (limit.HasValue)
  95. {
  96. limit = limit.Value + 10;
  97. }
  98. var items = _libraryManager
  99. .GetItemList(
  100. new InternalItemsQuery(user)
  101. {
  102. IncludeItemTypes = new[] { nameof(Episode) },
  103. OrderBy = new[] { new ValueTuple<string, SortOrder>(ItemSortBy.DatePlayed, SortOrder.Descending) },
  104. SeriesPresentationUniqueKey = presentationUniqueKey,
  105. Limit = limit,
  106. DtoOptions = new DtoOptions { Fields = new[] { ItemFields.SeriesPresentationUniqueKey }, EnableImages = false },
  107. GroupBySeriesPresentationUniqueKey = true
  108. }, parentsFolders.ToList())
  109. .Cast<Episode>()
  110. .Where(episode => !string.IsNullOrEmpty(episode.SeriesPresentationUniqueKey))
  111. .Select(GetUniqueSeriesKey);
  112. // Avoid implicitly captured closure
  113. var episodes = GetNextUpEpisodes(request, user, items, dtoOptions);
  114. return GetResult(episodes, request);
  115. }
  116. public IEnumerable<Episode> GetNextUpEpisodes(NextUpQuery request, User user, IEnumerable<string> seriesKeys, DtoOptions dtoOptions)
  117. {
  118. // Avoid implicitly captured closure
  119. var currentUser = user;
  120. var allNextUp = seriesKeys
  121. .Select(i => GetNextUp(i, currentUser, dtoOptions));
  122. return allNextUp
  123. .Where(i =>
  124. {
  125. return i.Item1 != DateTime.MinValue;
  126. })
  127. .Select(i => i.Item2())
  128. .Where(i => i != null);
  129. }
  130. private static string GetUniqueSeriesKey(Episode episode)
  131. {
  132. return episode.SeriesPresentationUniqueKey;
  133. }
  134. private static string GetUniqueSeriesKey(Series series)
  135. {
  136. return series.GetPresentationUniqueKey();
  137. }
  138. /// <summary>
  139. /// Gets the next up.
  140. /// </summary>
  141. /// <returns>Task{Episode}.</returns>
  142. private Tuple<DateTime, Func<Episode>> GetNextUp(string seriesKey, User user, DtoOptions dtoOptions)
  143. {
  144. var lastWatchedEpisode = _libraryManager.GetItemList(new InternalItemsQuery(user)
  145. {
  146. AncestorWithPresentationUniqueKey = null,
  147. SeriesPresentationUniqueKey = seriesKey,
  148. IncludeItemTypes = new[] { nameof(Episode) },
  149. OrderBy = new[] { new ValueTuple<string, SortOrder>(ItemSortBy.SortName, SortOrder.Descending) },
  150. IsPlayed = true,
  151. Limit = 1,
  152. ParentIndexNumberNotEquals = 0,
  153. DtoOptions = new DtoOptions
  154. {
  155. Fields = new ItemFields[]
  156. {
  157. ItemFields.SortName
  158. },
  159. EnableImages = false
  160. }
  161. }).FirstOrDefault();
  162. Func<Episode> getEpisode = () =>
  163. {
  164. var nextEpisode = _libraryManager.GetItemList(new InternalItemsQuery(user)
  165. {
  166. AncestorWithPresentationUniqueKey = null,
  167. SeriesPresentationUniqueKey = seriesKey,
  168. IncludeItemTypes = new[] { nameof(Episode) },
  169. OrderBy = new[] { new ValueTuple<string, SortOrder>(ItemSortBy.SortName, SortOrder.Ascending) },
  170. Limit = 1,
  171. IsPlayed = false,
  172. IsVirtualItem = false,
  173. ParentIndexNumberNotEquals = 0,
  174. MinSortName = lastWatchedEpisode?.SortName,
  175. DtoOptions = dtoOptions
  176. }).Cast<Episode>().FirstOrDefault();
  177. if (nextEpisode != null)
  178. {
  179. var userData = _userDataManager.GetUserData(user, nextEpisode);
  180. if (userData.PlaybackPositionTicks > 0)
  181. {
  182. return null;
  183. }
  184. }
  185. return nextEpisode;
  186. };
  187. if (lastWatchedEpisode != null)
  188. {
  189. var userData = _userDataManager.GetUserData(user, lastWatchedEpisode);
  190. var lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
  191. return new Tuple<DateTime, Func<Episode>>(lastWatchedDate, getEpisode);
  192. }
  193. // Return the first episode
  194. return new Tuple<DateTime, Func<Episode>>(DateTime.MinValue, getEpisode);
  195. }
  196. private static QueryResult<BaseItem> GetResult(IEnumerable<BaseItem> items, NextUpQuery query)
  197. {
  198. int totalCount = 0;
  199. if (query.EnableTotalRecordCount)
  200. {
  201. var list = items.ToList();
  202. totalCount = list.Count;
  203. items = list;
  204. }
  205. if (query.StartIndex.HasValue)
  206. {
  207. items = items.Skip(query.StartIndex.Value);
  208. }
  209. if (query.Limit.HasValue)
  210. {
  211. items = items.Take(query.Limit.Value);
  212. }
  213. return new QueryResult<BaseItem>
  214. {
  215. TotalRecordCount = totalCount,
  216. Items = items.ToArray()
  217. };
  218. }
  219. }
  220. }