UserRootFolder.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. var itemsArray = result;
  56. var totalCount = itemsArray.Length;
  57. return new QueryResult<BaseItem>
  58. {
  59. TotalRecordCount = totalCount,
  60. Items = itemsArray //TODO Fix The co-variant conversion between Folder[] and BaseItem[], this can generate runtime issues.
  61. };
  62. }
  63. public override int GetChildCount(User user)
  64. {
  65. return GetChildren(user, true).Count;
  66. }
  67. [JsonIgnore]
  68. protected override bool SupportsShortcutChildren => true;
  69. [JsonIgnore]
  70. public override bool IsPreSorted => true;
  71. protected override IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  72. {
  73. var list = base.GetEligibleChildrenForRecursiveChildren(user).ToList();
  74. list.AddRange(LibraryManager.RootFolder.VirtualChildren);
  75. return list;
  76. }
  77. public override bool BeforeMetadataRefresh(bool replaceAllMetdata)
  78. {
  79. ClearCache();
  80. var hasChanges = base.BeforeMetadataRefresh(replaceAllMetdata);
  81. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  82. {
  83. Name = "Media Folders";
  84. hasChanges = true;
  85. }
  86. return hasChanges;
  87. }
  88. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  89. {
  90. ClearCache();
  91. return base.GetNonCachedChildren(directoryService);
  92. }
  93. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  94. {
  95. ClearCache();
  96. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  97. .ConfigureAwait(false);
  98. ClearCache();
  99. // Not the best way to handle this, but it solves an issue
  100. // CollectionFolders aren't always getting saved after changes
  101. // This means that grabbing the item by Id may end up returning the old one
  102. // Fix is in two places - make sure the folder gets saved
  103. // And here to remedy it for affected users.
  104. // In theory this can be removed eventually.
  105. foreach (var item in Children)
  106. {
  107. LibraryManager.RegisterItem(item);
  108. }
  109. }
  110. }
  111. }