UserViewManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. if (string.IsNullOrWhiteSpace(folderViewType))
  65. {
  66. list.Add(folder);
  67. }
  68. else
  69. {
  70. list.Add(await GetUserView(folder.Id, folder.Name, folderViewType, string.Empty, user, cancellationToken).ConfigureAwait(false));
  71. }
  72. }
  73. }
  74. else
  75. {
  76. list.AddRange(standaloneFolders);
  77. }
  78. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) ||
  79. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  80. {
  81. list.Add(await GetUserView(CollectionType.TvShows, string.Empty, user, cancellationToken).ConfigureAwait(false));
  82. }
  83. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) ||
  84. foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)))
  85. {
  86. list.Add(await GetUserView(CollectionType.Music, string.Empty, user, cancellationToken).ConfigureAwait(false));
  87. }
  88. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) ||
  89. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  90. {
  91. list.Add(await GetUserView(CollectionType.Movies, string.Empty, user, cancellationToken).ConfigureAwait(false));
  92. }
  93. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)))
  94. {
  95. list.Add(await GetUserView(CollectionType.Games, string.Empty, user, cancellationToken).ConfigureAwait(false));
  96. }
  97. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase)))
  98. {
  99. //list.Add(_collectionManager.GetCollectionsFolder(user.Id.ToString("N")));
  100. list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, user, cancellationToken).ConfigureAwait(false));
  101. }
  102. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)))
  103. {
  104. //list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
  105. list.Add(await GetUserView(CollectionType.Playlists, string.Empty, user, cancellationToken).ConfigureAwait(false));
  106. }
  107. if (user.Configuration.DisplayFoldersView)
  108. {
  109. list.Add(await GetUserView(CollectionType.Folders, "zz_" + CollectionType.Folders, user, cancellationToken).ConfigureAwait(false));
  110. }
  111. if (query.IncludeExternalContent)
  112. {
  113. var channelResult = await _channelManager.GetChannels(new ChannelQuery
  114. {
  115. UserId = query.UserId
  116. }, cancellationToken).ConfigureAwait(false);
  117. var channels = channelResult.Items;
  118. var embeddedChannels = channels
  119. .Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
  120. .ToList();
  121. list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
  122. if (channels.Length > embeddedChannels.Count)
  123. {
  124. list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  125. }
  126. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
  127. {
  128. //list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  129. list.Add(await GetUserView(CollectionType.LiveTv, string.Empty, user, cancellationToken).ConfigureAwait(false));
  130. }
  131. }
  132. var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  133. var orders = user.Configuration.OrderedViews.ToList();
  134. return list
  135. .OrderBy(i =>
  136. {
  137. var index = orders.IndexOf(i.Id.ToString("N"));
  138. return index == -1 ? int.MaxValue : index;
  139. })
  140. .ThenBy(sorted.IndexOf)
  141. .ThenBy(i => i.SortName);
  142. }
  143. public Task<UserView> GetUserSubView(string name, string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
  144. {
  145. return _libraryManager.GetNamedView(user, name, parentId, type, sortName, cancellationToken);
  146. }
  147. public Task<UserView> GetUserSubView(string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
  148. {
  149. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  150. return GetUserSubView(name, parentId, type, user, sortName, cancellationToken);
  151. }
  152. public Task<UserView> GetUserView(string type, string sortName, User user, CancellationToken cancellationToken)
  153. {
  154. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  155. return _libraryManager.GetNamedView(user, name, type, sortName, cancellationToken);
  156. }
  157. public Task<UserView> GetUserView(Guid parentId, string name, string type, string sortName, User user, CancellationToken cancellationToken)
  158. {
  159. return _libraryManager.GetNamedView(user, name, parentId.ToString("N"), type, sortName, cancellationToken);
  160. }
  161. public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request)
  162. {
  163. var user = _userManager.GetUserById(request.UserId);
  164. var includeTypes = request.IncludeItemTypes;
  165. var currentUser = user;
  166. Func<BaseItem, bool> filter = i =>
  167. {
  168. if (includeTypes.Length > 0)
  169. {
  170. if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase))
  171. {
  172. return false;
  173. }
  174. }
  175. if (request.IsPlayed.HasValue)
  176. {
  177. var val = request.IsPlayed.Value;
  178. if (i is Video && i.IsPlayed(currentUser) != val)
  179. {
  180. return false;
  181. }
  182. }
  183. return i.LocationType != LocationType.Virtual && !i.IsFolder;
  184. };
  185. // Avoid implicitly captured closure
  186. var libraryItems = string.IsNullOrEmpty(request.ParentId) && user != null ?
  187. GetItemsConfiguredForLatest(user, filter) :
  188. GetAllLibraryItems(request.UserId, _userManager, _libraryManager, request.ParentId, filter);
  189. libraryItems = libraryItems.OrderByDescending(i => i.DateCreated);
  190. if (request.IsPlayed.HasValue)
  191. {
  192. var takeLimit = (request.Limit ?? 20) * 20;
  193. libraryItems = libraryItems.Take(takeLimit);
  194. }
  195. // Avoid implicitly captured closure
  196. var items = libraryItems
  197. .ToList();
  198. var list = new List<Tuple<BaseItem, List<BaseItem>>>();
  199. foreach (var item in items)
  200. {
  201. // Only grab the index container for media
  202. var container = item.IsFolder || !request.GroupItems ? null : item.LatestItemsIndexContainer;
  203. if (container == null)
  204. {
  205. list.Add(new Tuple<BaseItem, List<BaseItem>>(null, new List<BaseItem> { item }));
  206. }
  207. else
  208. {
  209. var current = list.FirstOrDefault(i => i.Item1 != null && i.Item1.Id == container.Id);
  210. if (current != null)
  211. {
  212. current.Item2.Add(item);
  213. }
  214. else
  215. {
  216. list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item }));
  217. }
  218. }
  219. if (list.Count >= request.Limit)
  220. {
  221. break;
  222. }
  223. }
  224. return list;
  225. }
  226. protected IList<BaseItem> GetAllLibraryItems(string userId, IUserManager userManager, ILibraryManager libraryManager, string parentId, Func<BaseItem, bool> filter)
  227. {
  228. if (!string.IsNullOrEmpty(parentId))
  229. {
  230. var folder = (Folder)libraryManager.GetItemById(new Guid(parentId));
  231. if (!string.IsNullOrWhiteSpace(userId))
  232. {
  233. var user = userManager.GetUserById(userId);
  234. if (user == null)
  235. {
  236. throw new ArgumentException("User not found");
  237. }
  238. return folder
  239. .GetRecursiveChildren(user, filter)
  240. .ToList();
  241. }
  242. return folder
  243. .GetRecursiveChildren(filter);
  244. }
  245. if (!string.IsNullOrWhiteSpace(userId))
  246. {
  247. var user = userManager.GetUserById(userId);
  248. if (user == null)
  249. {
  250. throw new ArgumentException("User not found");
  251. }
  252. return user
  253. .RootFolder
  254. .GetRecursiveChildren(user, filter)
  255. .ToList();
  256. }
  257. return libraryManager
  258. .RootFolder
  259. .GetRecursiveChildren(filter);
  260. }
  261. private IEnumerable<BaseItem> GetItemsConfiguredForLatest(User user, Func<BaseItem, bool> filter)
  262. {
  263. // Avoid implicitly captured closure
  264. var currentUser = user;
  265. return user.RootFolder.GetChildren(user, true)
  266. .OfType<Folder>()
  267. .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N")))
  268. .SelectMany(i => i.GetRecursiveChildren(currentUser, filter))
  269. .DistinctBy(i => i.Id);
  270. }
  271. }
  272. }