diff --git a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs index dec35100d..922260c07 100644 --- a/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs +++ b/MediaBrowser.Api/UserLibrary/BaseItemsByNameService.cs @@ -137,24 +137,14 @@ namespace MediaBrowser.Api.UserLibrary { var libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList(); - items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => - { - var userdata = UserDataRepository.GetUserData(user.Id, l.GetUserDataKey()); - - return userdata != null && userdata.Played; - })); + items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => l.IsPlayed(user))); } if (filters.Contains(ItemFilter.IsUnplayed)) { var libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList(); - items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => - { - var userdata = UserDataRepository.GetUserData(user.Id, l.GetUserDataKey()); - - return userdata == null || !userdata.Played; - })); + items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => !l.IsPlayed(user))); } return items; diff --git a/MediaBrowser.Api/UserLibrary/ItemsService.cs b/MediaBrowser.Api/UserLibrary/ItemsService.cs index 202ac293f..22149df86 100644 --- a/MediaBrowser.Api/UserLibrary/ItemsService.cs +++ b/MediaBrowser.Api/UserLibrary/ItemsService.cs @@ -386,6 +386,9 @@ namespace MediaBrowser.Api.UserLibrary /// IEnumerable{BaseItem}. internal static IEnumerable ApplyFilter(IEnumerable items, ItemFilter filter, User user, IUserDataManager repository) { + // Avoid implicitly captured closure + var currentUser = user; + switch (filter) { case ItemFilter.IsFavoriteOrLikes: @@ -440,20 +443,10 @@ namespace MediaBrowser.Api.UserLibrary }); case ItemFilter.IsPlayed: - return items.Where(item => - { - var userdata = repository.GetUserData(user.Id, item.GetUserDataKey()); - - return userdata != null && userdata.Played; - }); + return items.Where(item => item.IsPlayed(currentUser)); case ItemFilter.IsUnplayed: - return items.Where(item => - { - var userdata = repository.GetUserData(user.Id, item.GetUserDataKey()); - - return userdata == null || !userdata.Played; - }); + return items.Where(item => !item.IsPlayed(currentUser)); case ItemFilter.IsFolder: return items.Where(item => item.IsFolder); diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index c887f6977..0fc4f10f1 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -167,6 +167,7 @@ namespace MediaBrowser.Controller.Entities public static ILocalizationManager LocalizationManager { get; set; } public static IItemRepository ItemRepository { get; set; } public static IFileSystem FileSystem { get; set; } + public static IUserDataManager UserDataManager { get; set; } /// /// Returns a that represents this instance. @@ -1705,5 +1706,12 @@ namespace MediaBrowser.Controller.Entities // Directory watchers should repeat this, but do a quick refresh first return RefreshMetadata(CancellationToken.None, forceSave: true, allowSlowProviders: false); } + + public virtual bool IsPlayed(User user) + { + var userdata = UserDataManager.GetUserData(user.Id, GetUserDataKey()); + + return userdata != null && userdata.Played; + } } } diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index 6fb99f547..119b4dd7e 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -1084,5 +1084,10 @@ namespace MediaBrowser.Controller.Entities } }); } + + public override bool IsPlayed(User user) + { + return GetRecursiveChildren(user).Where(i => !i.IsFolder).All(i => i.IsPlayed(user)); + } } } diff --git a/MediaBrowser.Server.Implementations/Sorting/IsUnplayedComparer.cs b/MediaBrowser.Server.Implementations/Sorting/IsUnplayedComparer.cs index 596b344de..6c757a8a3 100644 --- a/MediaBrowser.Server.Implementations/Sorting/IsUnplayedComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/IsUnplayedComparer.cs @@ -32,14 +32,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// DateTime. private int GetValue(BaseItem x) { - var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey()); - - if (userdata == null) - { - return 0; - } - - return userdata.Played ? 1 : 0; + return x.IsPlayed(User) ? 1 : 0; } /// @@ -90,14 +83,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// DateTime. private int GetValue(BaseItem x) { - var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey()); - - if (userdata == null) - { - return 1; - } - - return userdata.Played ? 0 : 1; + return x.IsPlayed(User) ? 0 : 1; } ///