diff --git a/MediaBrowser.Api/GamesService.cs b/MediaBrowser.Api/GamesService.cs index 159a25004..040872fcc 100644 --- a/MediaBrowser.Api/GamesService.cs +++ b/MediaBrowser.Api/GamesService.cs @@ -192,7 +192,7 @@ namespace MediaBrowser.Api _userDataRepository, _dtoService, Logger, - request, item => item is Game, + request, new[] { typeof(Game) }, SimilarItemsHelper.GetSimiliarityScore); return ToOptimizedSerializedResultUsingCache(result); diff --git a/MediaBrowser.Api/Library/LibraryService.cs b/MediaBrowser.Api/Library/LibraryService.cs index 871f5e8a8..097546af6 100644 --- a/MediaBrowser.Api/Library/LibraryService.cs +++ b/MediaBrowser.Api/Library/LibraryService.cs @@ -397,7 +397,7 @@ namespace MediaBrowser.Api.Library }); } - if (item is Series || (program != null && program.IsSeries) ) + if (item is Series || (program != null && program.IsSeries)) { return new TvShowsService(_userManager, _userDataManager, _libraryManager, _itemRepo, _dtoService, _tvManager) { @@ -659,87 +659,38 @@ namespace MediaBrowser.Api.Library /// System.Object. public object Get(GetItemCounts request) { - var filteredItems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager, null, i => i.LocationType != LocationType.Virtual && FilterItem(i, request, request.UserId)); + var user = string.IsNullOrWhiteSpace(request.UserId) ? null : _userManager.GetUserById(request.UserId); var counts = new ItemCounts { - AlbumCount = filteredItems.Count(i => i is MusicAlbum), - EpisodeCount = filteredItems.Count(i => i is Episode), - GameCount = filteredItems.Count(i => i is Game), - GameSystemCount = filteredItems.Count(i => i is GameSystem), - MovieCount = filteredItems.Count(i => i is Movie), - SeriesCount = filteredItems.Count(i => i is Series), - SongCount = filteredItems.Count(i => i is Audio), - MusicVideoCount = filteredItems.Count(i => i is MusicVideo), - BoxSetCount = filteredItems.Count(i => i is BoxSet), - BookCount = filteredItems.Count(i => i is Book), - - UniqueTypes = filteredItems.Select(i => i.GetClientTypeName()).Distinct().ToList() + AlbumCount = GetCount(typeof(MusicAlbum), user, request), + EpisodeCount = GetCount(typeof(Episode), user, request), + GameCount = GetCount(typeof(Game), user, request), + GameSystemCount = GetCount(typeof(GameSystem), user, request), + MovieCount = GetCount(typeof(Movie), user, request), + SeriesCount = GetCount(typeof(Series), user, request), + SongCount = GetCount(typeof(Audio), user, request), + MusicVideoCount = GetCount(typeof(MusicVideo), user, request), + BoxSetCount = GetCount(typeof(BoxSet), user, request), + BookCount = GetCount(typeof(Book), user, request) }; return ToOptimizedSerializedResultUsingCache(counts); } - private IList GetAllLibraryItems(string userId, IUserManager userManager, ILibraryManager libraryManager, string parentId, Func filter) + private int GetCount(Type type, User user, GetItemCounts request) { - if (!string.IsNullOrEmpty(parentId)) + var query = new InternalItemsQuery(user) { - var folder = (Folder)libraryManager.GetItemById(new Guid(parentId)); + IncludeItemTypes = new[] { type.Name }, + Limit = 0, + Recursive = true, + ExcludeLocationTypes = new[] { LocationType.Virtual }, + SourceTypes = new[] { SourceType.Library }, + IsFavorite = request.IsFavorite + }; - if (!string.IsNullOrWhiteSpace(userId)) - { - var user = userManager.GetUserById(userId); - - if (user == null) - { - throw new ArgumentException("User not found"); - } - - return folder - .GetRecursiveChildren(user, filter) - .ToList(); - } - - return folder - .GetRecursiveChildren(filter); - } - if (!string.IsNullOrWhiteSpace(userId)) - { - var user = userManager.GetUserById(userId); - - if (user == null) - { - throw new ArgumentException("User not found"); - } - - return userManager - .GetUserById(userId) - .RootFolder - .GetRecursiveChildren(user, filter) - .ToList(); - } - - return libraryManager - .RootFolder - .GetRecursiveChildren(filter); - } - - private bool FilterItem(BaseItem item, GetItemCounts request, string userId) - { - if (!string.IsNullOrWhiteSpace(userId)) - { - if (request.IsFavorite.HasValue) - { - var val = request.IsFavorite.Value; - - if (_userDataManager.GetUserData(userId, item.GetUserDataKey()).IsFavorite != val) - { - return false; - } - } - } - - return true; + return _libraryManager.GetItemsResult(query).TotalRecordCount; } /// @@ -980,20 +931,15 @@ namespace MediaBrowser.Api.Library ? new string[] { } : request.IncludeItemTypes.Split(','); - Func filter = i => + var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null; + + var query = new InternalItemsQuery(user) { - if (includeTypes.Length > 0) - { - if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase)) - { - return false; - } - } - - return true; + IncludeItemTypes = includeTypes, + Recursive = true }; - IEnumerable items = GetAllLibraryItems(request.UserId, _userManager, _libraryManager, null, filter); + var items = _libraryManager.GetItemList(query); var lookup = items .ToLookup(i => i.ProductionYear ?? -1) diff --git a/MediaBrowser.Api/Music/AlbumsService.cs b/MediaBrowser.Api/Music/AlbumsService.cs index 548598d42..9628ab231 100644 --- a/MediaBrowser.Api/Music/AlbumsService.cs +++ b/MediaBrowser.Api/Music/AlbumsService.cs @@ -52,10 +52,15 @@ namespace MediaBrowser.Api.Music public object Get(GetSimilarArtists request) { - var result = GetSimilarItemsResult( - - request, + var dtoOptions = GetDtoOptions(request); + var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager, + _itemRepo, + _libraryManager, + _userDataRepository, + _dtoService, + Logger, + request, new[] { typeof(MusicArtist) }, SimilarItemsHelper.GetSimiliarityScore); return ToOptimizedSerializedResultUsingCache(result); @@ -76,44 +81,11 @@ namespace MediaBrowser.Api.Music _userDataRepository, _dtoService, Logger, - request, item => item is MusicAlbum, + request, new[] { typeof(MusicAlbum) }, GetAlbumSimilarityScore); return ToOptimizedSerializedResultUsingCache(result); } - - private ItemsResult GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func, List, BaseItem, int> getSimilarityScore) - { - var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null; - - var item = string.IsNullOrEmpty(request.Id) ? - (!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder : - _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id); - - var inputItems = _libraryManager.GetArtists(user.RootFolder.GetRecursiveChildren(user, i => i is IHasArtist).OfType()); - - var list = inputItems.ToList(); - - var items = SimilarItemsHelper.GetSimilaritems(item, _libraryManager, list, getSimilarityScore).ToList(); - - IEnumerable returnItems = items; - - if (request.Limit.HasValue) - { - returnItems = returnItems.Take(request.Limit.Value); - } - - var dtoOptions = GetDtoOptions(request); - - var result = new ItemsResult - { - Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(), - - TotalRecordCount = items.Count - }; - - return result; - } /// /// Gets the album similarity score. diff --git a/MediaBrowser.Api/SimilarItemsHelper.cs b/MediaBrowser.Api/SimilarItemsHelper.cs index d114446ee..277bba1dd 100644 --- a/MediaBrowser.Api/SimilarItemsHelper.cs +++ b/MediaBrowser.Api/SimilarItemsHelper.cs @@ -54,21 +54,7 @@ namespace MediaBrowser.Api /// public static class SimilarItemsHelper { - /// - /// Gets the similar items. - /// - /// The dto options. - /// The user manager. - /// The item repository. - /// The library manager. - /// The user data repository. - /// The dto service. - /// The logger. - /// The request. - /// The include in search. - /// The get similarity score. - /// ItemsResult. - internal static ItemsResult GetSimilarItemsResult(DtoOptions dtoOptions, IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataManager userDataRepository, IDtoService dtoService, ILogger logger, BaseGetSimilarItemsFromItem request, Func includeInSearch, Func, List, BaseItem, int> getSimilarityScore) + internal static ItemsResult GetSimilarItemsResult(DtoOptions dtoOptions, IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataManager userDataRepository, IDtoService dtoService, ILogger logger, BaseGetSimilarItemsFromItem request, Type[] includeTypes, Func, List, BaseItem, int> getSimilarityScore) { var user = !string.IsNullOrWhiteSpace(request.UserId) ? userManager.GetUserById(request.UserId) : null; @@ -76,11 +62,13 @@ namespace MediaBrowser.Api (!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder : libraryManager.RootFolder) : libraryManager.GetItemById(request.Id); - Func filter = i => i.Id != item.Id && includeInSearch(i); + var query = new InternalItemsQuery(user) + { + IncludeItemTypes = includeTypes.Select(i => i.Name).ToArray(), + Recursive = true + }; - var inputItems = user == null - ? libraryManager.RootFolder.GetRecursiveChildren(filter) - : user.RootFolder.GetRecursiveChildren(user, filter); + var inputItems = libraryManager.GetItemList(query); var items = GetSimilaritems(item, libraryManager, inputItems, getSimilarityScore) .ToList(); diff --git a/MediaBrowser.Api/TvShowsService.cs b/MediaBrowser.Api/TvShowsService.cs index 7893ff5bd..5b5b0a902 100644 --- a/MediaBrowser.Api/TvShowsService.cs +++ b/MediaBrowser.Api/TvShowsService.cs @@ -263,7 +263,7 @@ namespace MediaBrowser.Api _userDataManager, _dtoService, Logger, - request, item => item is Series, + request, new[] { typeof(Series) }, SimilarItemsHelper.GetSimiliarityScore); return ToOptimizedSerializedResultUsingCache(result); diff --git a/MediaBrowser.Model/Dto/ItemCounts.cs b/MediaBrowser.Model/Dto/ItemCounts.cs index c8745ebe3..e80de26b1 100644 --- a/MediaBrowser.Model/Dto/ItemCounts.cs +++ b/MediaBrowser.Model/Dto/ItemCounts.cs @@ -62,15 +62,5 @@ namespace MediaBrowser.Model.Dto /// /// The book count. public int BookCount { get; set; } - /// - /// Gets or sets the unique types. - /// - /// The unique types. - public List UniqueTypes { get; set; } - - public ItemCounts() - { - UniqueTypes = new List(); - } } }