UserRootFolder.cs 4.2 KB

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