2015-01-24 14:03:55 -05:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Library;
|
2014-07-13 17:03:57 -04:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-21 03:25:49 -04:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2015-11-02 12:25:01 -05:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-10-23 22:45:23 -04:00
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2017-08-09 15:56:38 -04:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
|
namespace Emby.Server.Implementations.Library
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
public class UserViewManager : IUserViewManager
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
private readonly IChannelManager _channelManager;
|
|
|
|
|
private readonly ILiveTvManager _liveTvManager;
|
2015-01-24 14:03:55 -05:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2015-10-04 15:09:13 -04:00
|
|
|
|
public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerConfigurationManager config)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_localizationManager = localizationManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_channelManager = channelManager;
|
|
|
|
|
_liveTvManager = liveTvManager;
|
2015-01-24 14:03:55 -05:00
|
|
|
|
_config = config;
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 15:43:35 -04:00
|
|
|
|
public async Task<Folder[]> GetUserViews(UserViewQuery query, CancellationToken cancellationToken)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2014-09-14 11:10:51 -04:00
|
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
|
|
|
|
var folders = user.RootFolder
|
|
|
|
|
.GetChildren(user, true)
|
|
|
|
|
.OfType<Folder>()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2015-11-11 09:56:31 -05:00
|
|
|
|
if (!query.IncludeHidden)
|
|
|
|
|
{
|
|
|
|
|
folders = folders.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
var hidden = i as IHiddenFromDisplay;
|
|
|
|
|
return hidden == null || !hidden.IsHiddenFromUser(user);
|
|
|
|
|
}).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 00:49:20 -05:00
|
|
|
|
var groupedFolders = new List<ICollectionFolder>();
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2015-03-15 15:22:04 -04:00
|
|
|
|
var list = new List<Folder>();
|
|
|
|
|
|
2015-11-18 00:49:20 -05:00
|
|
|
|
foreach (var folder in folders)
|
2015-03-15 15:22:04 -04:00
|
|
|
|
{
|
2015-11-14 11:58:01 -05:00
|
|
|
|
var collectionFolder = folder as ICollectionFolder;
|
|
|
|
|
var folderViewType = collectionFolder == null ? null : collectionFolder.CollectionType;
|
2015-09-15 14:09:44 -04:00
|
|
|
|
|
2015-11-14 11:58:01 -05:00
|
|
|
|
if (UserView.IsUserSpecific(folder))
|
|
|
|
|
{
|
2017-08-26 20:32:33 -04:00
|
|
|
|
list.Add(_libraryManager.GetNamedView(user, folder.Name, folder.Id.ToString("N"), folderViewType, null, cancellationToken));
|
2015-11-14 11:58:01 -05:00
|
|
|
|
continue;
|
2015-11-17 13:17:52 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 00:49:20 -05:00
|
|
|
|
if (collectionFolder != null && UserView.IsEligibleForGrouping(folder) && user.IsFolderGrouped(folder.Id))
|
|
|
|
|
{
|
|
|
|
|
groupedFolders.Add(collectionFolder);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.PresetViews.Contains(folderViewType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2017-08-26 20:32:33 -04:00
|
|
|
|
list.Add(GetUserView(folder, folderViewType, string.Empty, cancellationToken));
|
2015-09-14 13:28:42 -04:00
|
|
|
|
}
|
2015-11-14 11:58:01 -05:00
|
|
|
|
else
|
2015-09-14 13:28:42 -04:00
|
|
|
|
{
|
2015-11-18 00:49:20 -05:00
|
|
|
|
list.Add(folder);
|
2015-03-15 15:22:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-11 16:38:13 -04:00
|
|
|
|
|
2015-11-18 00:49:20 -05:00
|
|
|
|
foreach (var viewType in new[] { CollectionType.Movies, CollectionType.TvShows })
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2015-11-18 00:49:20 -05:00
|
|
|
|
var parents = groupedFolders.Where(i => string.Equals(i.CollectionType, viewType, StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(i.CollectionType))
|
|
|
|
|
.ToList();
|
2015-04-15 17:59:20 -04:00
|
|
|
|
|
2015-11-18 00:49:20 -05:00
|
|
|
|
if (parents.Count > 0)
|
|
|
|
|
{
|
2017-10-16 02:10:55 -04:00
|
|
|
|
var localizationKey = string.Equals(viewType, CollectionType.TvShows, StringComparison.OrdinalIgnoreCase) ?
|
2017-10-17 15:49:39 -04:00
|
|
|
|
"TvShows" :
|
2017-10-16 02:10:55 -04:00
|
|
|
|
"Movies";
|
2017-10-13 01:43:11 -04:00
|
|
|
|
|
|
|
|
|
list.Add(GetUserView(parents, viewType, localizationKey, string.Empty, user, query.PresetViews, cancellationToken));
|
2015-11-18 00:49:20 -05:00
|
|
|
|
}
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 01:51:33 -04:00
|
|
|
|
if (_config.Configuration.EnableFolderView)
|
|
|
|
|
{
|
2017-10-13 01:43:11 -04:00
|
|
|
|
var name = _localizationManager.GetLocalizedString("Folders");
|
2017-10-03 14:39:37 -04:00
|
|
|
|
list.Add(_libraryManager.GetNamedView(name, CollectionType.Folders, string.Empty, cancellationToken));
|
2016-06-04 01:51:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 15:46:24 -04:00
|
|
|
|
if (query.IncludeExternalContent)
|
|
|
|
|
{
|
2015-08-14 14:30:08 -04:00
|
|
|
|
var channelResult = await _channelManager.GetChannelsInternal(new ChannelQuery
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
|
|
|
|
UserId = query.UserId
|
|
|
|
|
|
2014-06-10 13:36:06 -04:00
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2014-06-30 15:24:35 -04:00
|
|
|
|
var channels = channelResult.Items;
|
2017-08-26 20:32:33 -04:00
|
|
|
|
|
2016-08-31 16:46:09 -04:00
|
|
|
|
if (_config.Configuration.EnableChannelView && channels.Length > 0)
|
2016-01-27 14:28:12 -05:00
|
|
|
|
{
|
2017-10-03 14:39:37 -04:00
|
|
|
|
list.Add(_channelManager.GetInternalChannelFolder(cancellationToken));
|
2016-01-27 14:28:12 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
list.AddRange(channels);
|
|
|
|
|
}
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2014-06-10 13:36:06 -04:00
|
|
|
|
if (_liveTvManager.GetEnabledUsers().Select(i => i.Id.ToString("N")).Contains(query.UserId))
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2017-10-03 14:39:37 -04:00
|
|
|
|
list.Add(_liveTvManager.GetInternalLiveTvFolder(CancellationToken.None));
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 21:42:53 -04: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"));
|
|
|
|
|
|
2015-11-17 13:17:52 -05:00
|
|
|
|
if (index == -1)
|
|
|
|
|
{
|
|
|
|
|
var view = i as UserView;
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
if (view.DisplayParentId != Guid.Empty)
|
|
|
|
|
{
|
|
|
|
|
index = orders.IndexOf(view.DisplayParentId.ToString("N"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 21:42:53 -04:00
|
|
|
|
return index == -1 ? int.MaxValue : index;
|
|
|
|
|
})
|
|
|
|
|
.ThenBy(sorted.IndexOf)
|
2017-08-19 15:43:35 -04:00
|
|
|
|
.ThenBy(i => i.SortName)
|
|
|
|
|
.ToArray();
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 01:43:11 -04:00
|
|
|
|
public UserView GetUserSubViewWithName(string name, string parentId, string type, string sortName, CancellationToken cancellationToken)
|
2014-11-10 22:41:55 -05:00
|
|
|
|
{
|
2015-04-20 14:04:02 -04:00
|
|
|
|
var uniqueId = parentId + "subview" + type;
|
|
|
|
|
|
2015-08-14 14:00:26 -04:00
|
|
|
|
return _libraryManager.GetNamedView(name, parentId, type, sortName, uniqueId, cancellationToken);
|
2014-11-10 22:41:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 01:43:11 -04:00
|
|
|
|
public UserView GetUserSubView(string parentId, string type, string localizationKey, string sortName, CancellationToken cancellationToken)
|
2014-06-07 15:46:24 -04:00
|
|
|
|
{
|
2017-10-13 01:43:11 -04:00
|
|
|
|
var name = _localizationManager.GetLocalizedString(localizationKey);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
|
2017-10-13 01:43:11 -04:00
|
|
|
|
return GetUserSubViewWithName(name, parentId, type, sortName, cancellationToken);
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
2014-07-29 23:31:35 -04:00
|
|
|
|
|
2017-10-13 01:43:11 -04:00
|
|
|
|
private Folder GetUserView(List<ICollectionFolder> parents, string viewType, string localizationKey, string sortName, User user, string[] presetViews, CancellationToken cancellationToken)
|
2014-07-29 23:31:35 -04:00
|
|
|
|
{
|
2015-11-18 00:49:20 -05:00
|
|
|
|
if (parents.Count == 1 && parents.All(i => string.Equals(i.CollectionType, viewType, StringComparison.OrdinalIgnoreCase)))
|
2015-04-15 17:59:20 -04:00
|
|
|
|
{
|
2015-11-17 13:17:52 -05:00
|
|
|
|
if (!presetViews.Contains(viewType, StringComparer.OrdinalIgnoreCase))
|
2015-11-14 11:58:01 -05:00
|
|
|
|
{
|
|
|
|
|
return (Folder)parents[0];
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-26 20:32:33 -04:00
|
|
|
|
return GetUserView((Folder)parents[0], viewType, string.Empty, cancellationToken);
|
2015-04-15 17:59:20 -04:00
|
|
|
|
}
|
2015-04-20 14:04:02 -04:00
|
|
|
|
|
2017-10-13 01:43:11 -04:00
|
|
|
|
var name = _localizationManager.GetLocalizedString(localizationKey);
|
2017-08-26 20:32:33 -04:00
|
|
|
|
return _libraryManager.GetNamedView(user, name, viewType, sortName, cancellationToken);
|
2014-07-29 23:31:35 -04:00
|
|
|
|
}
|
2015-01-26 11:47:15 -05:00
|
|
|
|
|
2017-08-26 20:32:33 -04:00
|
|
|
|
public UserView GetUserView(Folder parent, string viewType, string sortName, CancellationToken cancellationToken)
|
2015-03-15 15:22:04 -04:00
|
|
|
|
{
|
2015-11-18 00:49:20 -05:00
|
|
|
|
return _libraryManager.GetShadowView(parent, viewType, sortName, cancellationToken);
|
2015-09-15 14:09:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 03:25:49 -04:00
|
|
|
|
public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request, DtoOptions options)
|
2015-01-26 11:47:15 -05:00
|
|
|
|
{
|
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
|
|
|
|
|
2017-05-21 03:25:49 -04:00
|
|
|
|
var libraryItems = GetItemsForLatestItems(user, request, options);
|
2015-01-26 11:47:15 -05:00
|
|
|
|
|
|
|
|
|
var list = new List<Tuple<BaseItem, List<BaseItem>>>();
|
|
|
|
|
|
2015-10-29 15:01:04 -04:00
|
|
|
|
foreach (var item in libraryItems)
|
2015-01-26 11:47:15 -05:00
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 15:56:38 -04:00
|
|
|
|
private List<BaseItem> GetItemsForLatestItems(User user, LatestItemsQuery request, DtoOptions options)
|
2015-01-26 11:47:15 -05:00
|
|
|
|
{
|
2016-06-12 01:03:52 -04:00
|
|
|
|
var parentId = request.ParentId;
|
|
|
|
|
|
|
|
|
|
var includeItemTypes = request.IncludeItemTypes;
|
|
|
|
|
var limit = request.Limit ?? 10;
|
|
|
|
|
|
2016-11-21 03:54:53 -05:00
|
|
|
|
var parents = new List<BaseItem>();
|
2015-01-26 11:47:15 -05:00
|
|
|
|
|
2016-11-21 03:54:53 -05:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(parentId))
|
2015-11-02 12:25:01 -05:00
|
|
|
|
{
|
2016-11-21 03:54:53 -05:00
|
|
|
|
var parent = _libraryManager.GetItemById(parentId) as Folder;
|
|
|
|
|
if (parent != null)
|
|
|
|
|
{
|
|
|
|
|
parents.Add(parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 16:11:13 -05:00
|
|
|
|
var isPlayed = request.IsPlayed;
|
|
|
|
|
|
|
|
|
|
if (parents.OfType<ICollectionFolder>().Any(i => string.Equals(i.CollectionType, CollectionType.Music, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
isPlayed = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 03:54:53 -05:00
|
|
|
|
if (parents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
parents = user.RootFolder.GetChildren(user, true)
|
|
|
|
|
.Where(i => i is Folder)
|
|
|
|
|
.Where(i => !user.Configuration.LatestItemsExcludes.Contains(i.Id.ToString("N")))
|
|
|
|
|
.ToList();
|
2015-11-02 12:25:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 03:54:53 -05:00
|
|
|
|
if (parents.Count == 0)
|
2016-03-26 12:58:03 -04:00
|
|
|
|
{
|
|
|
|
|
return new List<BaseItem>();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-01 12:45:57 -04:00
|
|
|
|
var mediaTypes = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (includeItemTypes.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var parent in parents.OfType<ICollectionFolder>())
|
|
|
|
|
{
|
|
|
|
|
switch (parent.CollectionType)
|
|
|
|
|
{
|
|
|
|
|
case CollectionType.Books:
|
|
|
|
|
mediaTypes.Add(MediaType.Book);
|
|
|
|
|
break;
|
|
|
|
|
case CollectionType.Games:
|
|
|
|
|
mediaTypes.Add(MediaType.Game);
|
|
|
|
|
break;
|
|
|
|
|
case CollectionType.Music:
|
|
|
|
|
mediaTypes.Add(MediaType.Audio);
|
|
|
|
|
break;
|
|
|
|
|
case CollectionType.Photos:
|
|
|
|
|
mediaTypes.Add(MediaType.Photo);
|
|
|
|
|
mediaTypes.Add(MediaType.Video);
|
|
|
|
|
break;
|
|
|
|
|
case CollectionType.HomeVideos:
|
|
|
|
|
mediaTypes.Add(MediaType.Photo);
|
|
|
|
|
mediaTypes.Add(MediaType.Video);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
mediaTypes.Add(MediaType.Video);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mediaTypes = mediaTypes.Distinct().ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var excludeItemTypes = includeItemTypes.Length == 0 && mediaTypes.Count == 0 ? new[]
|
2016-03-19 01:04:38 -04:00
|
|
|
|
{
|
2016-06-12 01:03:52 -04:00
|
|
|
|
typeof(Person).Name,
|
|
|
|
|
typeof(Studio).Name,
|
|
|
|
|
typeof(Year).Name,
|
|
|
|
|
typeof(GameGenre).Name,
|
|
|
|
|
typeof(MusicGenre).Name,
|
|
|
|
|
typeof(Genre).Name
|
2016-03-19 01:04:38 -04:00
|
|
|
|
|
|
|
|
|
} : new string[] { };
|
2015-11-02 12:25:01 -05:00
|
|
|
|
|
2017-06-03 03:36:32 -04:00
|
|
|
|
var query = new InternalItemsQuery(user)
|
2015-01-26 11:47:15 -05:00
|
|
|
|
{
|
2015-10-29 15:01:04 -04:00
|
|
|
|
IncludeItemTypes = includeItemTypes,
|
2017-09-04 15:28:22 -04:00
|
|
|
|
OrderBy = new[] { new Tuple<string, SortOrder>(ItemSortBy.DateCreated, SortOrder.Descending) },
|
2015-11-02 12:25:01 -05:00
|
|
|
|
IsFolder = includeItemTypes.Length == 0 ? false : (bool?)null,
|
|
|
|
|
ExcludeItemTypes = excludeItemTypes,
|
2017-01-09 12:05:34 -05:00
|
|
|
|
IsVirtualItem = false,
|
2016-06-12 01:03:52 -04:00
|
|
|
|
Limit = limit * 5,
|
2017-05-21 03:25:49 -04:00
|
|
|
|
IsPlayed = isPlayed,
|
2017-08-01 12:45:57 -04:00
|
|
|
|
DtoOptions = options,
|
2017-08-09 15:56:38 -04:00
|
|
|
|
MediaTypes = mediaTypes.ToArray(mediaTypes.Count)
|
2017-06-03 03:36:32 -04:00
|
|
|
|
};
|
2015-01-26 11:47:15 -05:00
|
|
|
|
|
2017-06-03 03:36:32 -04:00
|
|
|
|
if (parents.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return _libraryManager.GetItemList(query, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _libraryManager.GetItemList(query, parents);
|
2015-01-26 11:47:15 -05:00
|
|
|
|
}
|
2014-06-07 15:46:24 -04:00
|
|
|
|
}
|
|
|
|
|
}
|