2
0

UserViewManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using MediaBrowser.Controller.Channels;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Audio;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.LiveTv;
  13. using MediaBrowser.Model.Channels;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.Globalization;
  16. using MediaBrowser.Model.Library;
  17. using MediaBrowser.Model.Querying;
  18. namespace Emby.Server.Implementations.Library
  19. {
  20. public class UserViewManager : IUserViewManager
  21. {
  22. private readonly ILibraryManager _libraryManager;
  23. private readonly ILocalizationManager _localizationManager;
  24. private readonly IUserManager _userManager;
  25. private readonly IChannelManager _channelManager;
  26. private readonly ILiveTvManager _liveTvManager;
  27. private readonly IServerConfigurationManager _config;
  28. public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerConfigurationManager config)
  29. {
  30. _libraryManager = libraryManager;
  31. _localizationManager = localizationManager;
  32. _userManager = userManager;
  33. _channelManager = channelManager;
  34. _liveTvManager = liveTvManager;
  35. _config = config;
  36. }
  37. public Folder[] GetUserViews(UserViewQuery query)
  38. {
  39. var user = _userManager.GetUserById(query.UserId);
  40. var folders = _libraryManager.GetUserRootFolder()
  41. .GetChildren(user, true)
  42. .OfType<Folder>()
  43. .ToList();
  44. var groupedFolders = new List<ICollectionFolder>();
  45. var list = new List<Folder>();
  46. foreach (var folder in folders)
  47. {
  48. var collectionFolder = folder as ICollectionFolder;
  49. var folderViewType = collectionFolder == null ? null : collectionFolder.CollectionType;
  50. if (UserView.IsUserSpecific(folder))
  51. {
  52. list.Add(_libraryManager.GetNamedView(user, folder.Name, folder.Id, folderViewType, null));
  53. continue;
  54. }
  55. if (collectionFolder != null && UserView.IsEligibleForGrouping(folder) && user.IsFolderGrouped(folder.Id))
  56. {
  57. groupedFolders.Add(collectionFolder);
  58. continue;
  59. }
  60. if (query.PresetViews.Contains(folderViewType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  61. {
  62. list.Add(GetUserView(folder, folderViewType, string.Empty));
  63. }
  64. else
  65. {
  66. list.Add(folder);
  67. }
  68. }
  69. foreach (var viewType in new[] { CollectionType.Movies, CollectionType.TvShows })
  70. {
  71. var parents = groupedFolders.Where(i => string.Equals(i.CollectionType, viewType, StringComparison.OrdinalIgnoreCase) || string.IsNullOrEmpty(i.CollectionType))
  72. .ToList();
  73. if (parents.Count > 0)
  74. {
  75. var localizationKey = string.Equals(viewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) ?
  76. "TvShows" :
  77. "Movies";
  78. list.Add(GetUserView(parents, viewType, localizationKey, string.Empty, user, query.PresetViews));
  79. }
  80. }
  81. if (_config.Configuration.EnableFolderView)
  82. {
  83. var name = _localizationManager.GetLocalizedString("Folders");
  84. list.Add(_libraryManager.GetNamedView(name, CollectionType.Folders, string.Empty));
  85. }
  86. if (query.IncludeExternalContent)
  87. {
  88. var channelResult = _channelManager.GetChannelsInternal(new ChannelQuery
  89. {
  90. UserId = query.UserId
  91. });
  92. var channels = channelResult.Items;
  93. list.AddRange(channels);
  94. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id).Contains(query.UserId))
  95. {
  96. list.Add(_liveTvManager.GetInternalLiveTvFolder(CancellationToken.None));
  97. }
  98. }
  99. if (!query.IncludeHidden)
  100. {
  101. list = list.Where(i => !user.Configuration.MyMediaExcludes.Contains(i.Id.ToString("N", CultureInfo.InvariantCulture))).ToList();
  102. }
  103. var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  104. var orders = user.Configuration.OrderedViews.ToList();
  105. return list
  106. .OrderBy(i =>
  107. {
  108. var index = orders.IndexOf(i.Id.ToString("N", CultureInfo.InvariantCulture));
  109. if (index == -1)
  110. {
  111. var view = i as UserView;
  112. if (view != null)
  113. {
  114. if (!view.DisplayParentId.Equals(Guid.Empty))
  115. {
  116. index = orders.IndexOf(view.DisplayParentId.ToString("N", CultureInfo.InvariantCulture));
  117. }
  118. }
  119. }
  120. return index == -1 ? int.MaxValue : index;
  121. })
  122. .ThenBy(sorted.IndexOf)
  123. .ThenBy(i => i.SortName)
  124. .ToArray();
  125. }
  126. public UserView GetUserSubViewWithName(string name, Guid parentId, string type, string sortName)
  127. {
  128. var uniqueId = parentId + "subview" + type;
  129. return _libraryManager.GetNamedView(name, parentId, type, sortName, uniqueId);
  130. }
  131. public UserView GetUserSubView(Guid parentId, string type, string localizationKey, string sortName)
  132. {
  133. var name = _localizationManager.GetLocalizedString(localizationKey);
  134. return GetUserSubViewWithName(name, parentId, type, sortName);
  135. }
  136. private Folder GetUserView(List<ICollectionFolder> parents, string viewType, string localizationKey, string sortName, User user, string[] presetViews)
  137. {
  138. if (parents.Count == 1 && parents.All(i => string.Equals(i.CollectionType, viewType, StringComparison.OrdinalIgnoreCase)))
  139. {
  140. if (!presetViews.Contains(viewType, StringComparer.OrdinalIgnoreCase))
  141. {
  142. return (Folder)parents[0];
  143. }
  144. return GetUserView((Folder)parents[0], viewType, string.Empty);
  145. }
  146. var name = _localizationManager.GetLocalizedString(localizationKey);
  147. return _libraryManager.GetNamedView(user, name, viewType, sortName);
  148. }
  149. public UserView GetUserView(Folder parent, string viewType, string sortName)
  150. {
  151. return _libraryManager.GetShadowView(parent, viewType, sortName);
  152. }
  153. public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request, DtoOptions options)
  154. {
  155. var user = _userManager.GetUserById(request.UserId);
  156. var libraryItems = GetItemsForLatestItems(user, request, options);
  157. var list = new List<Tuple<BaseItem, List<BaseItem>>>();
  158. foreach (var item in libraryItems)
  159. {
  160. // Only grab the index container for media
  161. var container = item.IsFolder || !request.GroupItems ? null : item.LatestItemsIndexContainer;
  162. if (container == null)
  163. {
  164. list.Add(new Tuple<BaseItem, List<BaseItem>>(null, new List<BaseItem> { item }));
  165. }
  166. else
  167. {
  168. var current = list.FirstOrDefault(i => i.Item1 != null && i.Item1.Id == container.Id);
  169. if (current != null)
  170. {
  171. current.Item2.Add(item);
  172. }
  173. else
  174. {
  175. list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item }));
  176. }
  177. }
  178. if (list.Count >= request.Limit)
  179. {
  180. break;
  181. }
  182. }
  183. return list;
  184. }
  185. private IReadOnlyList<BaseItem> GetItemsForLatestItems(User user, LatestItemsQuery request, DtoOptions options)
  186. {
  187. var parentId = request.ParentId;
  188. var includeItemTypes = request.IncludeItemTypes;
  189. var limit = request.Limit ?? 10;
  190. var parents = new List<BaseItem>();
  191. if (!parentId.Equals(Guid.Empty))
  192. {
  193. var parentItem = _libraryManager.GetItemById(parentId);
  194. if (parentItem is Channel parentItemChannel)
  195. {
  196. return _channelManager.GetLatestChannelItemsInternal(
  197. new InternalItemsQuery(user)
  198. {
  199. ChannelIds = new[] { parentId },
  200. IsPlayed = request.IsPlayed,
  201. StartIndex = request.StartIndex,
  202. Limit = request.Limit,
  203. IncludeItemTypes = request.IncludeItemTypes,
  204. EnableTotalRecordCount = false
  205. },
  206. CancellationToken.None).Result.Items;
  207. }
  208. if (parentItem is Folder parent)
  209. {
  210. parents.Add(parent);
  211. }
  212. }
  213. var isPlayed = request.IsPlayed;
  214. if (parents.OfType<ICollectionFolder>().Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)))
  215. {
  216. isPlayed = null;
  217. }
  218. if (parents.Count == 0)
  219. {
  220. parents = _libraryManager.GetUserRootFolder().GetChildren(user, true)
  221. .Where(i => i is Folder)
  222. .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N", CultureInfo.InvariantCulture)))
  223. .ToList();
  224. }
  225. if (parents.Count == 0)
  226. {
  227. return new List<BaseItem>();
  228. }
  229. if (includeItemTypes.Length == 0)
  230. {
  231. // Handle situations with the grouping setting, e.g. movies showing up in tv, etc.
  232. // Thanks to mixed content libraries included in the UserView
  233. var hasCollectionType = parents.OfType<UserView>().ToArray();
  234. if (hasCollectionType.Length > 0)
  235. {
  236. if (hasCollectionType.All(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)))
  237. {
  238. includeItemTypes = new string[] { "Movie" };
  239. }
  240. else if (hasCollectionType.All(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)))
  241. {
  242. includeItemTypes = new string[] { "Episode" };
  243. }
  244. }
  245. }
  246. var mediaTypes = new List<string>();
  247. if (includeItemTypes.Length == 0)
  248. {
  249. foreach (var parent in parents.OfType<ICollectionFolder>())
  250. {
  251. switch (parent.CollectionType)
  252. {
  253. case CollectionType.Books:
  254. mediaTypes.Add(MediaType.Book);
  255. mediaTypes.Add(MediaType.Audio);
  256. break;
  257. case CollectionType.Music:
  258. mediaTypes.Add(MediaType.Audio);
  259. break;
  260. case CollectionType.Photos:
  261. mediaTypes.Add(MediaType.Photo);
  262. mediaTypes.Add(MediaType.Video);
  263. break;
  264. case CollectionType.HomeVideos:
  265. mediaTypes.Add(MediaType.Photo);
  266. mediaTypes.Add(MediaType.Video);
  267. break;
  268. default:
  269. mediaTypes.Add(MediaType.Video);
  270. break;
  271. }
  272. }
  273. mediaTypes = mediaTypes.Distinct().ToList();
  274. }
  275. var excludeItemTypes = includeItemTypes.Length == 0 && mediaTypes.Count == 0 ? new[]
  276. {
  277. typeof(Person).Name,
  278. typeof(Studio).Name,
  279. typeof(Year).Name,
  280. typeof(MusicGenre).Name,
  281. typeof(Genre).Name
  282. } : Array.Empty<string>();
  283. var query = new InternalItemsQuery(user)
  284. {
  285. IncludeItemTypes = includeItemTypes,
  286. OrderBy = new[] { new ValueTuple<string, SortOrder>(ItemSortBy.DateCreated, SortOrder.Descending) },
  287. IsFolder = includeItemTypes.Length == 0 ? false : (bool?)null,
  288. ExcludeItemTypes = excludeItemTypes,
  289. IsVirtualItem = false,
  290. Limit = limit * 5,
  291. IsPlayed = isPlayed,
  292. DtoOptions = options,
  293. MediaTypes = mediaTypes.ToArray()
  294. };
  295. if (parents.Count == 0)
  296. {
  297. return _libraryManager.GetItemList(query, false);
  298. }
  299. return _libraryManager.GetItemList(query, parents);
  300. }
  301. }
  302. }