jellyfin-server/MediaBrowser.Server.Implementations/Library/UserViewManager.cs

191 lines
7.4 KiB
C#
Raw Normal View History

2014-08-02 02:34:45 +00:00
using MediaBrowser.Common.Extensions;
2014-07-30 03:31:35 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller;
2014-06-07 19:46:24 +00:00
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Localization;
2014-08-02 02:34:45 +00:00
using MediaBrowser.Controller.Playlists;
2014-06-07 19:46:24 +00:00
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Library;
2014-07-13 21:03:57 +00:00
using MediaBrowser.Model.Querying;
2014-06-07 19:46:24 +00:00
using System;
using System.Collections.Generic;
2014-08-02 02:34:45 +00:00
using System.IO;
2014-06-07 19:46:24 +00:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Library
{
public class UserViewManager : IUserViewManager
{
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localizationManager;
private readonly IFileSystem _fileSystem;
private readonly IUserManager _userManager;
private readonly IChannelManager _channelManager;
private readonly ILiveTvManager _liveTvManager;
2014-07-30 03:31:35 +00:00
private readonly IServerApplicationPaths _appPaths;
2014-08-02 02:34:45 +00:00
private readonly IPlaylistManager _playlists;
2014-06-07 19:46:24 +00:00
2014-08-02 02:34:45 +00:00
public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IFileSystem fileSystem, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerApplicationPaths appPaths, IPlaylistManager playlists)
2014-06-07 19:46:24 +00:00
{
_libraryManager = libraryManager;
_localizationManager = localizationManager;
_fileSystem = fileSystem;
_userManager = userManager;
_channelManager = channelManager;
_liveTvManager = liveTvManager;
2014-07-30 03:31:35 +00:00
_appPaths = appPaths;
2014-08-02 02:34:45 +00:00
_playlists = playlists;
2014-06-07 19:46:24 +00:00
}
public async Task<IEnumerable<Folder>> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
{
var user = _userManager.GetUserById(new Guid(query.UserId));
var folders = user.RootFolder
.GetChildren(user, true)
.OfType<Folder>()
.ToList();
var list = new List<Folder>();
var excludeFolderIds = user.Configuration.ExcludeFoldersFromGrouping.Select(i => new Guid(i)).ToList();
var standaloneFolders = folders.Where(i => UserView.IsExcludedFromGrouping(i) || excludeFolderIds.Contains(i.Id)).ToList();
list.AddRange(standaloneFolders);
var recursiveChildren = folders
.Except(standaloneFolders)
.SelectMany(i => i.GetRecursiveChildren(user, false))
.ToList();
if (recursiveChildren.OfType<Series>().Any())
{
2014-07-01 04:06:28 +00:00
list.Add(await GetUserView(CollectionType.TvShows, user, string.Empty, cancellationToken).ConfigureAwait(false));
2014-06-07 19:46:24 +00:00
}
if (recursiveChildren.OfType<MusicAlbum>().Any() ||
recursiveChildren.OfType<MusicVideo>().Any())
{
2014-07-01 04:06:28 +00:00
list.Add(await GetUserView(CollectionType.Music, user, string.Empty, cancellationToken).ConfigureAwait(false));
2014-06-07 19:46:24 +00:00
}
if (recursiveChildren.OfType<Movie>().Any())
{
2014-07-01 04:06:28 +00:00
list.Add(await GetUserView(CollectionType.Movies, user, string.Empty, cancellationToken).ConfigureAwait(false));
2014-06-07 19:46:24 +00:00
}
if (recursiveChildren.OfType<Game>().Any())
{
2014-07-01 04:06:28 +00:00
list.Add(await GetUserView(CollectionType.Games, user, string.Empty, cancellationToken).ConfigureAwait(false));
2014-06-07 19:46:24 +00:00
}
2014-07-13 21:03:57 +00:00
if (user.Configuration.DisplayCollectionsView &&
2014-07-01 04:06:28 +00:00
recursiveChildren.OfType<BoxSet>().Any())
2014-06-07 19:46:24 +00:00
{
2014-08-14 13:24:30 +00:00
list.Add(await GetUserView(CollectionType.BoxSets, user, string.Empty, cancellationToken).ConfigureAwait(false));
2014-06-07 19:46:24 +00:00
}
2014-08-02 02:34:45 +00:00
if (recursiveChildren.OfType<Playlist>().Any())
{
list.Add(_playlists.GetPlaylistsFolder(user.Id.ToString("N")));
}
2014-08-14 13:24:30 +00:00
if (user.Configuration.DisplayFoldersView)
{
list.Add(await GetUserView(CollectionType.Folders, user, "zz_" + CollectionType.Folders, cancellationToken).ConfigureAwait(false));
}
2014-06-07 19:46:24 +00:00
if (query.IncludeExternalContent)
{
2014-06-10 17:36:06 +00:00
var channelResult = await _channelManager.GetChannels(new ChannelQuery
2014-06-07 19:46:24 +00:00
{
UserId = query.UserId
2014-06-10 17:36:06 +00:00
}, cancellationToken).ConfigureAwait(false);
2014-06-07 19:46:24 +00:00
var channels = channelResult.Items;
var embeddedChannels = channels
.Where(i => user.Configuration.DisplayChannelsWithinViews.Contains(i.Id))
.ToList();
list.AddRange(embeddedChannels.Select(i => _channelManager.GetChannel(i.Id)));
if (channels.Length > embeddedChannels.Count)
2014-06-07 19:46:24 +00:00
{
list.Add(await _channelManager.GetInternalChannelFolder(query.UserId, cancellationToken).ConfigureAwait(false));
}
2014-06-10 17:36:06 +00:00
if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
2014-06-07 19:46:24 +00:00
{
list.Add(await _liveTvManager.GetInternalLiveTvFolder(query.UserId, cancellationToken).ConfigureAwait(false));
}
}
2014-08-19 01:42:53 +00:00
var sorted = _libraryManager.Sort(list, user, new[] { ItemSortBy.SortName }, SortOrder.Ascending).ToList();
var orders = user.Configuration.OrderedViews.ToList();
return list
.OrderBy(i =>
{
var index = orders.IndexOf(i.Id.ToString("N"));
return index == -1 ? int.MaxValue : index;
})
.ThenBy(sorted.IndexOf)
.ThenBy(i => i.SortName);
2014-06-07 19:46:24 +00:00
}
2014-08-14 13:24:30 +00:00
public Task<UserView> GetUserView(string type, User user, string sortName, CancellationToken cancellationToken)
2014-06-07 19:46:24 +00:00
{
var name = _localizationManager.GetLocalizedString("ViewType" + type);
2014-07-01 04:06:28 +00:00
return _libraryManager.GetNamedView(name, type, sortName, cancellationToken);
2014-06-07 19:46:24 +00:00
}
2014-07-30 03:31:35 +00:00
public async Task<SpecialFolder> GetSpecialFolder(string name, SpecialFolderType type, string itemType, CancellationToken cancellationToken)
{
var path = Path.Combine(_appPaths.ItemsByNamePath,
"specialfolders",
_fileSystem.GetValidFilename(name));
var id = (path + "_specialfolder_" + name).GetMBId(typeof(SpecialFolder));
var item = _libraryManager.GetItemById(id) as SpecialFolder;
if (item == null)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
item = new SpecialFolder
{
Path = path,
Id = id,
DateCreated = DateTime.UtcNow,
Name = name,
SpecialFolderType = type,
ItemTypeName = itemType
};
await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
}
return item;
}
2014-06-07 19:46:24 +00:00
}
}