diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Library/UserViewManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Library/UserViewManager.cs | 163 |
1 files changed, 150 insertions, 13 deletions
diff --git a/MediaBrowser.Server.Implementations/Library/UserViewManager.cs b/MediaBrowser.Server.Implementations/Library/UserViewManager.cs index 46c32cc56..2ec9e8a4f 100644 --- a/MediaBrowser.Server.Implementations/Library/UserViewManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserViewManager.cs @@ -1,6 +1,6 @@ -using MediaBrowser.Common.IO; -using MediaBrowser.Controller; -using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Channels; +using MediaBrowser.Controller.Collections; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Library; @@ -16,6 +16,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MoreLinq; namespace MediaBrowser.Server.Implementations.Library { @@ -23,24 +24,24 @@ namespace MediaBrowser.Server.Implementations.Library { private readonly ILibraryManager _libraryManager; private readonly ILocalizationManager _localizationManager; - private readonly IFileSystem _fileSystem; private readonly IUserManager _userManager; private readonly IChannelManager _channelManager; private readonly ILiveTvManager _liveTvManager; - private readonly IServerApplicationPaths _appPaths; private readonly IPlaylistManager _playlists; + private readonly ICollectionManager _collectionManager; + private readonly IServerConfigurationManager _config; - public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerApplicationPaths appPaths, IPlaylistManager playlists) + public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IPlaylistManager playlists, ICollectionManager collectionManager, IServerConfigurationManager config) { _libraryManager = libraryManager; _localizationManager = localizationManager; - _fileSystem = fileSystem; _userManager = userManager; _channelManager = channelManager; _liveTvManager = liveTvManager; - _appPaths = appPaths; _playlists = playlists; + _collectionManager = collectionManager; + _config = config; } public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken) @@ -56,7 +57,9 @@ namespace MediaBrowser.Server.Implementations.Library var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList(); - var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList(); + var standaloneFolders = folders + .Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)) + .ToList(); var foldersWithViewTypes = folders .Except(standaloneFolders) @@ -88,11 +91,9 @@ namespace MediaBrowser.Server.Implementations.Library list.Add(await GetUserView(CollectionType.Games, string.Empty, cancellationToken).ConfigureAwait(false)); } - if (user.Configuration.DisplayCollectionsView && - folders - .Except(standaloneFolders) - .SelectMany(i => i.GetRecursiveChildren(user, false)).OfType<BoxSet>().Any()) + if (foldersWithViewTypes.Any(i => string.Equals(i.CollectionType, CollectionType.BoxSets, StringComparison.OrdinalIgnoreCase))) { + //list.Add(_collectionManager.GetCollectionsFolder(user.Id.ToString("N"))); list.Add(await GetUserView(CollectionType.BoxSets, string.Empty, cancellationToken).ConfigureAwait(false)); } @@ -166,5 +167,141 @@ namespace MediaBrowser.Server.Implementations.Library return _libraryManager.GetNamedView(name, type, sortName, cancellationToken); } + + public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request) + { + var user = _userManager.GetUserById(request.UserId); + + var includeTypes = request.IncludeItemTypes; + + var currentUser = user; + + Func<BaseItem, bool> filter = i => + { + if (includeTypes.Length > 0) + { + if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase)) + { + return false; + } + } + + if (request.IsPlayed.HasValue) + { + var val = request.IsPlayed.Value; + if (i.IsPlayed(currentUser) != val) + { + return false; + } + } + + return i.LocationType != LocationType.Virtual && !i.IsFolder; + }; + + // Avoid implicitly captured closure + var libraryItems = string.IsNullOrEmpty(request.ParentId) && user != null ? + GetItemsConfiguredForLatest(user, filter) : + GetAllLibraryItems(request.UserId, _userManager, _libraryManager, request.ParentId, filter); + + libraryItems = libraryItems.OrderByDescending(i => i.DateCreated); + + if (request.IsPlayed.HasValue) + { + var takeLimit = (request.Limit ?? 20) * 20; + libraryItems = libraryItems.Take(takeLimit); + } + + // Avoid implicitly captured closure + var items = libraryItems + .ToList(); + + var list = new List<Tuple<BaseItem, List<BaseItem>>>(); + + foreach (var item in items) + { + // Only grab the index container for media + var container = item.IsFolder || !request.GroupItems ? null : item.LatestItemsIndexContainer; + + if (container == null) + { + list.Add(new Tuple<BaseItem, List<BaseItem>>(null, new List<BaseItem> { item })); + } + else + { + var current = list.FirstOrDefault(i => i.Item1 != null && i.Item1.Id == container.Id); + + if (current != null) + { + current.Item2.Add(item); + } + else + { + list.Add(new Tuple<BaseItem, List<BaseItem>>(container, new List<BaseItem> { item })); + } + } + + if (list.Count >= request.Limit) + { + break; + } + } + + return list; + } + + protected IList<BaseItem> GetAllLibraryItems(string userId, IUserManager userManager, ILibraryManager libraryManager, string parentId, Func<BaseItem, bool> filter) + { + if (!string.IsNullOrEmpty(parentId)) + { + var folder = (Folder)libraryManager.GetItemById(new Guid(parentId)); + + 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 user + .RootFolder + .GetRecursiveChildren(user, filter) + .ToList(); + } + + return libraryManager + .RootFolder + .GetRecursiveChildren(filter); + } + + private IEnumerable<BaseItem> GetItemsConfiguredForLatest(User user, Func<BaseItem, bool> filter) + { + // Avoid implicitly captured closure + var currentUser = user; + + return user.RootFolder.GetChildren(user, true) + .OfType<Folder>() + .Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N"))) + .SelectMany(i => i.GetRecursiveChildren(currentUser, filter)) + .DistinctBy(i => i.Id); + } } } |
