UserRootFolder.cs 4.2 KB

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