UserViewManager.cs 14 KB

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