UserRootFolder.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Data.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 List<Guid> _childrenIds = null;
  22. private readonly object _childIdsLock = new object();
  23. protected override List<BaseItem> LoadChildren()
  24. {
  25. lock (_childIdsLock)
  26. {
  27. if (_childrenIds == null)
  28. {
  29. var list = base.LoadChildren();
  30. _childrenIds = list.Select(i => i.Id).ToList();
  31. return list;
  32. }
  33. return _childrenIds.Select(LibraryManager.GetItemById).Where(i => i != null).ToList();
  34. }
  35. }
  36. [JsonIgnore]
  37. public override bool SupportsInheritedParentImages => false;
  38. [JsonIgnore]
  39. public override bool SupportsPlayedStatus => false;
  40. private void ClearCache()
  41. {
  42. lock (_childIdsLock)
  43. {
  44. _childrenIds = null;
  45. }
  46. }
  47. protected override QueryResult<BaseItem> GetItemsInternal(InternalItemsQuery query)
  48. {
  49. if (query.Recursive)
  50. {
  51. return QueryRecursive(query);
  52. }
  53. var result = UserViewManager.GetUserViews(new UserViewQuery
  54. {
  55. UserId = query.User.Id,
  56. PresetViews = query.PresetViews
  57. });
  58. return UserViewBuilder.SortAndPage(result, null, query, LibraryManager, true);
  59. }
  60. public override int GetChildCount(User user)
  61. {
  62. return GetChildren(user, true).Count;
  63. }
  64. [JsonIgnore]
  65. protected override bool SupportsShortcutChildren => true;
  66. [JsonIgnore]
  67. public override bool IsPreSorted => true;
  68. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  69. {
  70. var list = base.GetEligibleChildrenForRecursiveChildren(user).ToList();
  71. list.AddRange(LibraryManager.RootFolder.VirtualChildren);
  72. return list;
  73. }
  74. public override bool BeforeMetadataRefresh(bool replaceAllMetadata)
  75. {
  76. ClearCache();
  77. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetadata);
  78. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  79. {
  80. Name = "Media Folders";
  81. hasChanges = true;
  82. }
  83. return hasChanges;
  84. }
  85. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  86. {
  87. ClearCache();
  88. return base.GetNonCachedChildren(directoryService);
  89. }
  90. protected override async Task ValidateChildrenInternal(IProgress<double> progress, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService, CancellationToken cancellationToken)
  91. {
  92. ClearCache();
  93. await base.ValidateChildrenInternal(progress, recursive, refreshChildMetadata, refreshOptions, directoryService, cancellationToken)
  94. .ConfigureAwait(false);
  95. ClearCache();
  96. // Not the best way to handle this, but it solves an issue
  97. // CollectionFolders aren't always getting saved after changes
  98. // This means that grabbing the item by Id may end up returning the old one
  99. // Fix is in two places - make sure the folder gets saved
  100. // And here to remedy it for affected users.
  101. // In theory this can be removed eventually.
  102. foreach (var item in Children)
  103. {
  104. LibraryManager.RegisterItem(item);
  105. }
  106. }
  107. }
  108. }