2
0

UserViewManager.cs 14 KB

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