UserViewManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Movies;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Controller.Localization;
  9. using MediaBrowser.Controller.Playlists;
  10. using MediaBrowser.Model.Channels;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Library;
  13. using MediaBrowser.Model.Querying;
  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 IFileSystem _fileSystem;
  26. private readonly IUserManager _userManager;
  27. private readonly IChannelManager _channelManager;
  28. private readonly ILiveTvManager _liveTvManager;
  29. private readonly IServerApplicationPaths _appPaths;
  30. private readonly IPlaylistManager _playlists;
  31. public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerApplicationPaths appPaths, IPlaylistManager playlists)
  32. {
  33. _libraryManager = libraryManager;
  34. _localizationManager = localizationManager;
  35. _fileSystem = fileSystem;
  36. _userManager = userManager;
  37. _channelManager = channelManager;
  38. _liveTvManager = liveTvManager;
  39. _appPaths = appPaths;
  40. _playlists = playlists;
  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 list = new List<Folder>();
  50. var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
  51. var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList();
  52. var foldersWithViewTypes = folders
  53. .Except(standaloneFolders)
  54. .OfType<ICollectionFolder>()
  55. .ToList();
  56. list.AddRange(standaloneFolders);
  57. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase)) ||
  58. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  59. {
  60. list.Add(await GetUserView(CollectionType.TvShows, string.Empty, cancellationToken).ConfigureAwait(false));
  61. }
  62. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)) ||
  63. foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.MusicVideos, StringComparison.OrdinalIgnoreCase)))
  64. {
  65. list.Add(await GetUserView(CollectionType.Music, string.Empty, cancellationToken).ConfigureAwait(false));
  66. }
  67. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Movies, StringComparison.OrdinalIgnoreCase)) ||
  68. foldersWithViewTypes.Any(i => string.IsNullOrWhiteSpace(i.CollectionType)))
  69. {
  70. list.Add(await GetUserView(CollectionType.Movies, string.Empty, cancellationToken).ConfigureAwait(false));
  71. }
  72. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Games, StringComparison.OrdinalIgnoreCase)))
  73. {
  74. list.Add(await GetUserView(CollectionType.Games, string.Empty, cancellationToken).ConfigureAwait(false));
  75. }
  76. if (user.Configuration.DisplayCollectionsView &&
  77. folders
  78. .Except(standaloneFolders)
  79. .SelectMany(i => i.GetRecursiveChildren(user, false)).OfType<BoxSet>().Any())
  80. {
  81. list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, cancellationToken).ConfigureAwait(false));
  82. }
  83. if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase)))
  84. {
  85. list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
  86. }
  87. if (user.Configuration.DisplayFoldersView)
  88. {
  89. list.Add(await GetUserView(CollectionType.Folders, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
  90. }
  91. if (query.IncludeExternalContent)
  92. {
  93. var channelResult = await _channelManager.GetChannels(new ChannelQuery
  94. {
  95. UserId = query.UserId
  96. }, cancellationToken).ConfigureAwait(false);
  97. var channels = channelResult.Items;
  98. var embeddedChannels = channels
  99. .Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
  100. .ToList();
  101. list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
  102. if (channels.Length > embeddedChannels.Count)
  103. {
  104. list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  105. }
  106. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
  107. {
  108. list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  109. }
  110. }
  111. var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  112. var orders = user.Configuration.OrderedViews.ToList();
  113. return list
  114. .OrderBy(i =>
  115. {
  116. var index = orders.IndexOf(i.Id.ToString("N"));
  117. return index == -1 ? int.MaxValue : index;
  118. })
  119. .ThenBy(sorted.IndexOf)
  120. .ThenBy(i => i.SortName);
  121. }
  122. public Task<UserView> GetUserView(string parentId, string type, User user, string sortName, CancellationToken cancellationToken)
  123. {
  124. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  125. return _libraryManager.GetSpecialFolder(user, name, parentId, type, sortName, cancellationToken);
  126. }
  127. public Task<UserView> GetUserView(string type, string sortName, CancellationToken cancellationToken)
  128. {
  129. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  130. return _libraryManager.GetNamedView(name, type, sortName, cancellationToken);
  131. }
  132. }
  133. }