UserViewManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using MediaBrowser.Controller.Channels;
  2. using MediaBrowser.Controller.Collections;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.LiveTv;
  7. using MediaBrowser.Controller.Localization;
  8. using MediaBrowser.Controller.Playlists;
  9. using MediaBrowser.Model.Channels;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.Library;
  12. using MediaBrowser.Model.Querying;
  13. using MoreLinq;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.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 IPlaylistManager _playlists;
  29. private readonly ICollectionManager _collectionManager;
  30. private readonly IServerConfigurationManager _config;
  31. public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IPlaylistManager playlists, ICollectionManager collectionManager, IServerConfigurationManager config)
  32. {
  33. _libraryManager = libraryManager;
  34. _localizationManager = localizationManager;
  35. _userManager = userManager;
  36. _channelManager = channelManager;
  37. _liveTvManager = liveTvManager;
  38. _playlists = playlists;
  39. _collectionManager = collectionManager;
  40. _config = config;
  41. }
  42. public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
  43. {
  44. var user = _userManager.GetUserById(query.UserId);
  45. var folders = user.RootFolder
  46. .GetChildren(user, true)
  47. .OfType<Folder>()
  48. .ToList();
  49. var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
  50. var standaloneFolders = folders
  51. .Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id))
  52. .ToList();
  53. var foldersWithViewTypes = folders
  54. .Except(standaloneFolders)
  55. .OfType<ICollectionFolder>()
  56. .ToList();
  57. var list = new List<Folder>();
  58. if (_config.Configuration.EnableUserSpecificUserViews)
  59. {
  60. foreach (var folder in standaloneFolders)
  61. {
  62. var collectionFolder = folder as ICollectionFolder;
  63. var folderViewType = collectionFolder == null ? null : collectionFolder.CollectionType;
  64. list.Add(await GetUserView(folder.Id, folder.Name, folderViewType, string.Empty, user, cancellationToken).ConfigureAwait(false));
  65. }
  66. }
  67. else
  68. {
  69. list.AddRange(standaloneFolders);
  70. }
  71. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) ||
  72. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  73. {
  74. list.Add(await GetUserView(CollectionType.TvShows, string.Empty, user, cancellationToken).ConfigureAwait(false));
  75. }
  76. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) ||
  77. foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)))
  78. {
  79. list.Add(await GetUserView(CollectionType.Music, string.Empty, user, cancellationToken).ConfigureAwait(false));
  80. }
  81. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) ||
  82. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  83. {
  84. list.Add(await GetUserView(CollectionType.Movies, string.Empty, user, cancellationToken).ConfigureAwait(false));
  85. }
  86. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)))
  87. {
  88. list.Add(await GetUserView(CollectionType.Games, string.Empty, user, cancellationToken).ConfigureAwait(false));
  89. }
  90. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase)))
  91. {
  92. //list.Add(_collectionManager.GetCollectionsFolder(user.Id.ToString("N")));
  93. list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, user, cancellationToken).ConfigureAwait(false));
  94. }
  95. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)))
  96. {
  97. //list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
  98. list.Add(await GetUserView(CollectionType.Playlists, string.Empty, user, cancellationToken).ConfigureAwait(false));
  99. }
  100. if (user.Configuration.DisplayFoldersView)
  101. {
  102. list.Add(await GetUserView(CollectionType.Folders, "zz_" + CollectionType.Folders, user, cancellationToken).ConfigureAwait(false));
  103. }
  104. if (query.IncludeExternalContent)
  105. {
  106. var channelResult = await _channelManager.GetChannels(new ChannelQuery
  107. {
  108. UserId = query.UserId
  109. }, cancellationToken).ConfigureAwait(false);
  110. var channels = channelResult.Items;
  111. var embeddedChannels = channels
  112. .Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
  113. .ToList();
  114. list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
  115. if (channels.Length > embeddedChannels.Count)
  116. {
  117. list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  118. }
  119. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
  120. {
  121. //list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  122. list.Add(await GetUserView(CollectionType.LiveTv, string.Empty, user, cancellationToken).ConfigureAwait(false));
  123. }
  124. }
  125. var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  126. var orders = user.Configuration.OrderedViews.ToList();
  127. return list
  128. .OrderBy(i =>
  129. {
  130. var index = orders.IndexOf(i.Id.ToString("N"));
  131. return index == -1 ? int.MaxValue : index;
  132. })
  133. .ThenBy(sorted.IndexOf)
  134. .ThenBy(i => i.SortName);
  135. }
  136. public Task<UserView> GetUserSubView(string name, string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
  137. {
  138. return _libraryManager.GetNamedView(user, name, parentId, type, sortName, cancellationToken);
  139. }
  140. public Task<UserView> GetUserSubView(string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
  141. {
  142. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  143. return GetUserSubView(name, parentId, type, user, sortName, cancellationToken);
  144. }
  145. public Task<UserView> GetUserView(string type, string sortName, User user, CancellationToken cancellationToken)
  146. {
  147. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  148. return _libraryManager.GetNamedView(user, name, type, sortName, cancellationToken);
  149. }
  150. public Task<UserView> GetUserView(Guid parentId, string name, string type, string sortName, User user, CancellationToken cancellationToken)
  151. {
  152. return _libraryManager.GetNamedView(user, name, parentId.ToString("N"), type, sortName, cancellationToken);
  153. }
  154. public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request)
  155. {
  156. var user = _userManager.GetUserById(request.UserId);
  157. var includeTypes = request.IncludeItemTypes;
  158. var currentUser = user;
  159. Func<BaseItem, bool> filter = i =>
  160. {
  161. if (includeTypes.Length > 0)
  162. {
  163. if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase))
  164. {
  165. return false;
  166. }
  167. }
  168. if (request.IsPlayed.HasValue)
  169. {
  170. var val = request.IsPlayed.Value;
  171. if (i.IsPlayed(currentUser) != val)
  172. {
  173. return false;
  174. }
  175. }
  176. return i.LocationType != LocationType.Virtual && !i.IsFolder;
  177. };
  178. // Avoid implicitly captured closure
  179. var libraryItems = string.IsNullOrEmpty(request.ParentId) && user != null ?
  180. GetItemsConfiguredForLatest(user, filter) :
  181. GetAllLibraryItems(request.UserId, _userManager, _libraryManager, request.ParentId, filter);
  182. libraryItems = libraryItems.OrderByDescending(i => i.DateCreated);
  183. if (request.IsPlayed.HasValue)
  184. {
  185. var takeLimit = (request.Limit ?? 20) * 20;
  186. libraryItems = libraryItems.Take(takeLimit);
  187. }
  188. // Avoid implicitly captured closure
  189. var items = libraryItems
  190. .ToList();
  191. var list = new List<Tuple<BaseItem, List<BaseItem>>>();
  192. foreach (var item in items)
  193. {
  194. // Only grab the index container for media
  195. var container = item.IsFolder || !request.GroupItems ? null : item.LatestItemsIndexContainer;
  196. if (container == null)
  197. {
  198. list.Add(new Tuple<BaseItem, List<BaseItem>>(null, new List<BaseItem> { item }));
  199. }
  200. else
  201. {
  202. var current = list.FirstOrDefault(i => i.Item1 != null && i.Item1.Id == container.Id);
  203. if (current != null)
  204. {
  205. current.Item2.Add(item);
  206. }
  207. else
  208. {
  209. list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item }));
  210. }
  211. }
  212. if (list.Count >= request.Limit)
  213. {
  214. break;
  215. }
  216. }
  217. return list;
  218. }
  219. protected IList<BaseItem> GetAllLibraryItems(string userId, IUserManager userManager, ILibraryManager libraryManager, string parentId, Func<BaseItem, bool> filter)
  220. {
  221. if (!string.IsNullOrEmpty(parentId))
  222. {
  223. var folder = (Folder)libraryManager.GetItemById(new Guid(parentId));
  224. if (!string.IsNullOrWhiteSpace(userId))
  225. {
  226. var user = userManager.GetUserById(userId);
  227. if (user == null)
  228. {
  229. throw new ArgumentException("User not found");
  230. }
  231. return folder
  232. .GetRecursiveChildren(user, filter)
  233. .ToList();
  234. }
  235. return folder
  236. .GetRecursiveChildren(filter);
  237. }
  238. if (!string.IsNullOrWhiteSpace(userId))
  239. {
  240. var user = userManager.GetUserById(userId);
  241. if (user == null)
  242. {
  243. throw new ArgumentException("User not found");
  244. }
  245. return user
  246. .RootFolder
  247. .GetRecursiveChildren(user, filter)
  248. .ToList();
  249. }
  250. return libraryManager
  251. .RootFolder
  252. .GetRecursiveChildren(filter);
  253. }
  254. private IEnumerable<BaseItem> GetItemsConfiguredForLatest(User user, Func<BaseItem, bool> filter)
  255. {
  256. // Avoid implicitly captured closure
  257. var currentUser = user;
  258. return user.RootFolder.GetChildren(user, true)
  259. .OfType<Folder>()
  260. .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N")))
  261. .SelectMany(i => i.GetRecursiveChildren(currentUser, filter))
  262. .DistinctBy(i => i.Id);
  263. }
  264. }
  265. }