UserRootFolder.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json.Serialization;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Jellyfin.Database.Implementations.Entities;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Library;
  12. using MediaBrowser.Model.Querying;
  13. namespace MediaBrowser.Controller.Entities
  14. {
  15. /// <summary>
  16. /// Special class used for User Roots. Children contain actual ones defined for this user
  17. /// PLUS the virtual folders from the physical root (added by plug-ins).
  18. /// </summary>
  19. public class UserRootFolder : Folder
  20. {
  21. private readonly Lock _childIdsLock = new();
  22. private List<Guid> _childrenIds = null;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="UserRootFolder"/> class.
  25. /// </summary>
  26. public UserRootFolder()
  27. {
  28. IsRoot = true;
  29. }
  30. [JsonIgnore]
  31. public override bool SupportsInheritedParentImages => false;
  32. [JsonIgnore]
  33. public override bool SupportsPlayedStatus => false;
  34. [JsonIgnore]
  35. protected override bool SupportsShortcutChildren => true;
  36. [JsonIgnore]
  37. public override bool IsPreSorted => true;
  38. private void ClearCache()
  39. {
  40. lock (_childIdsLock)
  41. {
  42. _childrenIds = null;
  43. }
  44. }
  45. protected override IReadOnlyList<BaseItem> LoadChildren()
  46. {
  47. lock (_childIdsLock)
  48. {
  49. if (_childrenIds is null)
  50. {
  51. var list = base.LoadChildren();
  52. _childrenIds = list.Select(i => i.Id).ToList();
  53. return list;
  54. }
  55. return _childrenIds.Select(LibraryManager.GetItemById).Where(i => i is not null).ToList();
  56. }
  57. }
  58. protected override QueryResult<BaseItem> GetItemsInternal(InternalItemsQuery query)
  59. {
  60. if (query.Recursive)
  61. {
  62. return QueryRecursive(query);
  63. }
  64. var result = UserViewManager.GetUserViews(new UserViewQuery
  65. {
  66. User = query.User,
  67. PresetViews = query.PresetViews
  68. });
  69. return UserViewBuilder.SortAndPage(result, null, query, LibraryManager, true);
  70. }
  71. public override int GetChildCount(User user)
  72. {
  73. return GetChildren(user, true).Count;
  74. }
  75. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  76. {
  77. var list = base.GetEligibleChildrenForRecursiveChildren(user).ToList();
  78. list.AddRange(LibraryManager.RootFolder.VirtualChildren);
  79. return list;
  80. }
  81. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  82. {
  83. ClearCache();
  84. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  85. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  86. {
  87. Name = "Media Folders";
  88. hasChanges = true;
  89. }
  90. return hasChanges;
  91. }
  92. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  93. {
  94. ClearCache();
  95. return base.GetNonCachedChildren(directoryService);
  96. }
  97. protected override async Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, bool allowRemoveRoot, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  98. {
  99. ClearCache();
  100. await base.ValidateChildrenInternal(progress, recursive, refreshChildMetadata, allowRemoveRoot, refreshOptions, directoryService, cancellationToken)
  101. .ConfigureAwait(false);
  102. ClearCache();
  103. // Not the best way to handle this, but it solves an issue
  104. // CollectionFolders aren't always getting saved after changes
  105. // This means that grabbing the item by Id may end up returning the old one
  106. // Fix is in two places - make sure the folder gets saved
  107. // And here to remedy it for affected users.
  108. // In theory this can be removed eventually.
  109. foreach (var item in Children)
  110. {
  111. LibraryManager.RegisterItem(item);
  112. }
  113. }
  114. }
  115. }