2
0

UserRootFolder.cs 4.2 KB

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