UserViewManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Entities.Movies;
  6. using MediaBrowser.Controller.Entities.TV;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.LiveTv;
  9. using MediaBrowser.Controller.Localization;
  10. using MediaBrowser.Model.Channels;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Library;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Server.Implementations.Library
  19. {
  20. public class UserViewManager : IUserViewManager
  21. {
  22. private readonly ILibraryManager _libraryManager;
  23. private readonly ILocalizationManager _localizationManager;
  24. private readonly IFileSystem _fileSystem;
  25. private readonly IUserManager _userManager;
  26. private readonly IChannelManager _channelManager;
  27. private readonly ILiveTvManager _liveTvManager;
  28. public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager)
  29. {
  30. _libraryManager = libraryManager;
  31. _localizationManager = localizationManager;
  32. _fileSystem = fileSystem;
  33. _userManager = userManager;
  34. _channelManager = channelManager;
  35. _liveTvManager = liveTvManager;
  36. }
  37. public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
  38. {
  39. var user = _userManager.GetUserById(new Guid(query.UserId));
  40. var folders = user.RootFolder
  41. .GetChildren(user, true)
  42. .OfType<Folder>()
  43. .ToList();
  44. var list = new List<Folder>();
  45. var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
  46. var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList();
  47. list.AddRange(standaloneFolders);
  48. var recursiveChildren = folders
  49. .Except(standaloneFolders)
  50. .SelectMany(i => i.GetRecursiveChildren(user, false))
  51. .ToList();
  52. if (recursiveChildren.OfType<Series>().Any())
  53. {
  54. list.Add(await GetUserView(CollectionType.TvShows, user, cancellationToken).ConfigureAwait(false));
  55. }
  56. if (recursiveChildren.OfType<MusicAlbum>().Any() ||
  57. recursiveChildren.OfType<MusicVideo>().Any())
  58. {
  59. list.Add(await GetUserView(CollectionType.Music, user, cancellationToken).ConfigureAwait(false));
  60. }
  61. if (recursiveChildren.OfType<Movie>().Any())
  62. {
  63. list.Add(await GetUserView(CollectionType.Movies, user, cancellationToken).ConfigureAwait(false));
  64. }
  65. if (recursiveChildren.OfType<Game>().Any())
  66. {
  67. list.Add(await GetUserView(CollectionType.Games, user, cancellationToken).ConfigureAwait(false));
  68. }
  69. if (recursiveChildren.OfType<BoxSet>().Any())
  70. {
  71. list.Add(await GetUserView(CollectionType.BoxSets, user, cancellationToken).ConfigureAwait(false));
  72. }
  73. if (query.IncludeExternalContent)
  74. {
  75. var channelResult = await _channelManager.GetChannels(new ChannelQuery
  76. {
  77. Limit = 0,
  78. UserId = query.UserId
  79. }, cancellationToken).ConfigureAwait(false);
  80. if (channelResult.TotalRecordCount > 0)
  81. {
  82. list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  83. }
  84. if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
  85. {
  86. list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
  87. }
  88. }
  89. return list.OrderBy(i => i.SortName);
  90. }
  91. private Task<UserView> GetUserView(string type, User user, CancellationToken cancellationToken)
  92. {
  93. var name = _localizationManager.GetLocalizedString("ViewType" + type);
  94. return _libraryManager.GetNamedView(name, type, string.Empty, cancellationToken);
  95. }
  96. }
  97. }