UserViewManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Channels;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Entities.Movies;
  7. using MediaBrowser.Controller.Entities.TV;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.LiveTv;
  10. using MediaBrowser.Controller.Localization;
  11. using MediaBrowser.Controller.Playlists;
  12. using MediaBrowser.Model.Channels;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Library;
  15. using MediaBrowser.Model.Querying;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. namespace MediaBrowser.Server.Implementations.Library
  22. {
  23. public class UserViewManager : IUserViewManager
  24. {
  25. private readonly ILibraryManager _libraryManager;
  26. private readonly ILocalizationManager _localizationManager;
  27. private readonly IFileSystem _fileSystem;
  28. private readonly IUserManager _userManager;
  29. private readonly IChannelManager _channelManager;
  30. private readonly ILiveTvManager _liveTvManager;
  31. private readonly IServerApplicationPaths _appPaths;
  32. private readonly IPlaylistManager _playlists;
  33. public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerApplicationPaths appPaths, IPlaylistManager playlists)
  34. {
  35. _libraryManager = libraryManager;
  36. _localizationManager = localizationManager;
  37. _fileSystem = fileSystem;
  38. _userManager = userManager;
  39. _channelManager = channelManager;
  40. _liveTvManager = liveTvManager;
  41. _appPaths = appPaths;
  42. _playlists = playlists;
  43. }
  44. public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
  45. {
  46. var user = _userManager.GetUserById(query.UserId);
  47. var folders = user.RootFolder
  48. .GetChildren(user, true)
  49. .OfType<Folder>()
  50. .ToList();
  51. var list = new List<Folder>();
  52. var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
  53. var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList();
  54. list.AddRange(standaloneFolders);
  55. var recursiveChildren = folders
  56. .Except(standaloneFolders)
  57. .SelectMany(i => i.GetRecursiveChildren(user, false))
  58. .ToList();
  59. if (recursiveChildren.OfType<Series>().Any())
  60. {
  61. list.Add(await GetUserView(CollectionType.TvShows, user, string.Empty, cancellationToken).ConfigureAwait(false));
  62. }
  63. if (recursiveChildren.OfType<MusicAlbum>().Any() ||
  64. recursiveChildren.OfType<MusicVideo>().Any())
  65. {
  66. list.Add(await GetUserView(CollectionType.Music, user, string.Empty, cancellationToken).ConfigureAwait(false));
  67. }
  68. if (recursiveChildren.OfType<Movie>().Any())
  69. {
  70. list.Add(await GetUserView(CollectionType.Movies, user, string.Empty, cancellationToken).ConfigureAwait(false));
  71. }
  72. if (recursiveChildren.OfType<Game>().Any())
  73. {
  74. list.Add(await GetUserView(CollectionType.Games, user, string.Empty, cancellationToken).ConfigureAwait(false));
  75. }
  76. if (user.Configuration.DisplayCollectionsView &&
  77. recursiveChildren.OfType<BoxSet>().Any())
  78. {
  79. list.Add(await GetUserView(CollectionType.BoxSets, user, string.Empty, cancellationToken).ConfigureAwait(false));
  80. }
  81. if (recursiveChildren.OfType<Playlist>().Any())
  82. {
  83. list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
  84. }
  85. if (user.Configuration.DisplayFoldersView)
  86. {
  87. list.Add(await GetUserView(CollectionType.Folders, user, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
  88. }
  89. if (query.IncludeExternalContent)
  90. {
  91. var channelResult = await _channelManager.GetChannels(new ChannelQuery
  92. {
  93. UserId = query.UserId
  94. }, cancellationToken).ConfigureAwait(false);
  95. var channels = channelResult.Items;
  96. var embeddedChannels = channels
  97. .Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
  98. .ToList();
  99. list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
  100. if (channels.Length > embeddedChannels.Count)
  101. {
  102. list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  103. }
  104. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
  105. {
  106. list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  107. }
  108. }
  109. var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
  110. var orders = user.Configuration.OrderedViews.ToList();
  111. return list
  112. .OrderBy(i =>
  113. {
  114. var index = orders.IndexOf(i.Id.ToString("N"));
  115. return index == -1 ? int.MaxValue : index;
  116. })
  117. .ThenBy(sorted.IndexOf)
  118. .ThenBy(i => i.SortName);
  119. }
  120. public Task<UserView> GetUserView(string category, string type, User user, string sortName, CancellationToken cancellationToken)
  121. {
  122. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  123. return _libraryManager.GetNamedView(name, category, type, sortName, cancellationToken);
  124. }
  125. public Task<UserView> GetUserView(string type, User user, string sortName, CancellationToken cancellationToken)
  126. {
  127. return GetUserView(null, type, user, sortName, cancellationToken);
  128. }
  129. }
  130. }