UserRootFolder.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using MediaBrowser.Controller.Providers;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Library;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  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. public override async Task<QueryResult<BaseItem>> GetUserItems(UserItemsQuery query)
  19. {
  20. var result = await UserViewManager.GetUserViews(new UserViewQuery
  21. {
  22. UserId = query.User.Id.ToString("N")
  23. }, CancellationToken.None).ConfigureAwait(false);
  24. return SortAndFilter(result, query);
  25. }
  26. /// <summary>
  27. /// Get the children of this folder from the actual file system
  28. /// </summary>
  29. /// <returns>IEnumerable{BaseItem}.</returns>
  30. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  31. {
  32. return base.GetNonCachedChildren(directoryService).Concat(LibraryManager.RootFolder.VirtualChildren);
  33. }
  34. public override bool BeforeMetadataRefresh()
  35. {
  36. var hasChanges = base.BeforeMetadataRefresh();
  37. if (string.Equals("default", Name, StringComparison.OrdinalIgnoreCase))
  38. {
  39. Name = "Media Folders";
  40. hasChanges = true;
  41. }
  42. return hasChanges;
  43. }
  44. protected override async Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  45. {
  46. await base.ValidateChildrenInternal(progress, cancellationToken, recursive, refreshChildMetadata, refreshOptions, directoryService)
  47. .ConfigureAwait(false);
  48. // Not the best way to handle this, but it solves an issue
  49. // CollectionFolders aren't always getting saved after changes
  50. // This means that grabbing the item by Id may end up returning the old one
  51. // Fix is in two places - make sure the folder gets saved
  52. // And here to remedy it for affected users.
  53. // In theory this can be removed eventually.
  54. foreach (var item in Children)
  55. {
  56. LibraryManager.RegisterItem(item);
  57. }
  58. }
  59. public override void FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, User user)
  60. {
  61. // Nothing meaninful here and will only waste resources
  62. }
  63. }
  64. }