jellyfin-server/MediaBrowser.Api/UserLibrary/ItemsService.cs

446 lines
17 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Library;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
2013-03-10 05:36:39 +00:00
using MediaBrowser.Model.Querying;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
2013-12-28 23:09:24 +00:00
using System.Globalization;
2013-02-21 01:33:05 +00:00
using System.Linq;
2014-10-06 23:58:46 +00:00
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities.Audio;
2017-08-19 19:43:35 +00:00
using MediaBrowser.Model.Dto;
2016-10-24 02:45:23 +00:00
using MediaBrowser.Model.Globalization;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
using MediaBrowser.Model.Extensions;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class GetItems
/// </summary>
2014-03-28 19:58:18 +00:00
[Route("/Items", "GET", Summary = "Gets items based on a query.")]
[Route("/Users/{UserId}/Items", "GET", Summary = "Gets items based on a query.")]
2017-08-19 19:43:35 +00:00
public class GetItems : BaseItemsRequest, IReturn<QueryResult<BaseItemDto>>
2013-02-21 01:33:05 +00:00
{
}
/// <summary>
/// Class ItemsService
/// </summary>
2014-07-02 18:34:08 +00:00
[Authenticated]
2013-03-16 05:52:33 +00:00
public class ItemsService : BaseApiService
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization;
2013-03-09 05:15:51 +00:00
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2016-11-10 14:41:24 +00:00
private readonly IAuthorizationContext _authContext;
2013-09-04 17:02:19 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ItemsService" /> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
2013-03-28 22:00:58 +00:00
/// <param name="libraryManager">The library manager.</param>
2014-04-22 17:25:54 +00:00
/// <param name="localization">The localization.</param>
/// <param name="dtoService">The dto service.</param>
2016-11-10 14:41:24 +00:00
public ItemsService(IUserManager userManager, ILibraryManager libraryManager, ILocalizationManager localization, IDtoService dtoService, IAuthorizationContext authContext)
{
2016-06-26 20:35:03 +00:00
if (userManager == null)
{
throw new ArgumentNullException("userManager");
}
if (libraryManager == null)
{
throw new ArgumentNullException("libraryManager");
}
if (localization == null)
{
throw new ArgumentNullException("localization");
}
if (dtoService == null)
{
throw new ArgumentNullException("dtoService");
}
_userManager = userManager;
_libraryManager = libraryManager;
2013-06-13 18:17:59 +00:00
_localization = localization;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
2016-11-10 14:41:24 +00:00
_authContext = authContext;
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2017-08-28 00:33:05 +00:00
public object Get(GetItems request)
2013-02-21 01:33:05 +00:00
{
2016-06-26 20:35:03 +00:00
if (request == null)
{
throw new ArgumentNullException("request");
}
2017-08-28 00:33:05 +00:00
var result = GetItems(request);
2013-02-21 01:33:05 +00:00
return ToOptimizedSerializedResultUsingCache(result);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Gets the items.
/// </summary>
/// <param name="request">The request.</param>
2017-08-28 00:33:05 +00:00
private QueryResult<BaseItemDto> GetItems(GetItems request)
2013-02-21 01:33:05 +00:00
{
2015-05-29 23:51:33 +00:00
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
2016-10-08 05:57:38 +00:00
2016-11-10 14:41:24 +00:00
var dtoOptions = GetDtoOptions(_authContext, request);
2016-10-08 05:57:38 +00:00
2017-05-26 06:48:54 +00:00
var result = GetQueryResult(request, dtoOptions, user);
2013-02-21 01:33:05 +00:00
2016-06-26 20:35:03 +00:00
if (result == null)
{
throw new InvalidOperationException("GetItemsToSerialize returned null");
}
2016-06-26 20:38:50 +00:00
if (result.Items == null)
{
throw new InvalidOperationException("GetItemsToSerialize result.Items returned null");
}
2017-08-28 00:33:05 +00:00
var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
2016-06-26 20:35:03 +00:00
if (dtoList == null)
{
throw new InvalidOperationException("GetBaseItemDtos returned null");
}
2017-08-19 19:43:35 +00:00
return new QueryResult<BaseItemDto>
2013-02-21 01:33:05 +00:00
{
2016-03-20 06:46:51 +00:00
TotalRecordCount = result.TotalRecordCount,
2017-08-19 19:43:35 +00:00
Items = dtoList
2013-02-21 01:33:05 +00:00
};
}
/// <summary>
/// Gets the items to serialize.
/// </summary>
2017-05-26 06:48:54 +00:00
private QueryResult<BaseItem> GetQueryResult(GetItems request, DtoOptions dtoOptions, User user)
2013-02-21 01:33:05 +00:00
{
2014-01-22 17:05:23 +00:00
var item = string.IsNullOrEmpty(request.ParentId) ?
2016-10-12 18:23:09 +00:00
null :
2016-03-20 06:46:51 +00:00
_libraryManager.GetItemById(request.ParentId);
2013-02-21 01:33:05 +00:00
2015-11-15 22:30:47 +00:00
if (string.Equals(request.IncludeItemTypes, "Playlist", StringComparison.OrdinalIgnoreCase))
{
2016-10-12 18:23:09 +00:00
if (item == null || user != null)
{
item = _libraryManager.RootFolder.Children.OfType<Folder>().FirstOrDefault(i => string.Equals(i.GetType().Name, "PlaylistsFolder", StringComparison.OrdinalIgnoreCase));
}
2015-11-15 22:30:47 +00:00
}
2016-02-05 17:04:38 +00:00
else if (string.Equals(request.IncludeItemTypes, "BoxSet", StringComparison.OrdinalIgnoreCase))
{
item = user == null ? _libraryManager.RootFolder : user.RootFolder;
}
2015-11-15 22:30:47 +00:00
2016-10-12 18:23:09 +00:00
if (item == null)
{
item = string.IsNullOrEmpty(request.ParentId) ?
user == null ? _libraryManager.RootFolder : user.RootFolder :
_libraryManager.GetItemById(request.ParentId);
}
2013-02-21 01:33:05 +00:00
// Default list type = children
2016-06-23 17:04:18 +00:00
var folder = item as Folder;
if (folder == null)
{
folder = user == null ? _libraryManager.RootFolder : _libraryManager.GetUserRootFolder();
}
2016-08-18 16:45:36 +00:00
if (request.Recursive || !string.IsNullOrEmpty(request.Ids) || user == null)
2013-10-15 22:16:39 +00:00
{
2017-05-26 06:48:54 +00:00
return folder.GetItems(GetItemsQuery(request, dtoOptions, user));
2013-10-15 22:16:39 +00:00
}
2015-07-14 19:04:16 +00:00
var userRoot = item as UserRootFolder;
2014-10-06 23:58:46 +00:00
2015-07-14 19:04:16 +00:00
if (userRoot == null)
{
2017-05-26 06:48:54 +00:00
return folder.GetItems(GetItemsQuery(request, dtoOptions, user));
2013-02-21 01:33:05 +00:00
}
2017-08-19 19:43:35 +00:00
var itemsArray = folder.GetChildren(user, true).ToArray();
2015-07-14 19:04:16 +00:00
2016-03-20 06:46:51 +00:00
return new QueryResult<BaseItem>
2014-10-06 23:58:46 +00:00
{
2015-07-14 19:04:16 +00:00
Items = itemsArray,
TotalRecordCount = itemsArray.Length
2016-03-20 06:46:51 +00:00
};
2013-02-21 01:33:05 +00:00
}
2016-10-08 05:57:38 +00:00
private InternalItemsQuery GetItemsQuery(GetItems request, DtoOptions dtoOptions, User user)
2013-02-21 01:33:05 +00:00
{
2016-03-20 06:46:51 +00:00
var query = new InternalItemsQuery(user)
2014-10-06 23:58:46 +00:00
{
IsPlayed = request.IsPlayed,
MediaTypes = request.GetMediaTypes(),
IncludeItemTypes = request.GetIncludeItemTypes(),
ExcludeItemTypes = request.GetExcludeItemTypes(),
Recursive = request.Recursive,
2017-09-04 19:28:22 +00:00
OrderBy = request.GetOrderBy(),
2014-10-06 23:58:46 +00:00
IsFavorite = request.IsFavorite,
2014-10-06 23:58:46 +00:00
Limit = request.Limit,
2014-10-08 01:37:45 +00:00
StartIndex = request.StartIndex,
IsMissing = request.IsMissing,
IsUnaired = request.IsUnaired,
CollapseBoxSetItems = request.CollapseBoxSetItems,
NameLessThan = request.NameLessThan,
NameStartsWith = request.NameStartsWith,
NameStartsWithOrGreater = request.NameStartsWithOrGreater,
HasImdbId = request.HasImdbId,
IsPlaceHolder = request.IsPlaceHolder,
IsLocked = request.IsLocked,
IsInBoxSet = request.IsInBoxSet,
IsHD = request.IsHD,
Is3D = request.Is3D,
HasTvdbId = request.HasTvdbId,
HasTmdbId = request.HasTmdbId,
HasOverview = request.HasOverview,
HasOfficialRating = request.HasOfficialRating,
HasParentalRating = request.HasParentalRating,
HasSpecialFeature = request.HasSpecialFeature,
HasSubtitles = request.HasSubtitles,
HasThemeSong = request.HasThemeSong,
HasThemeVideo = request.HasThemeVideo,
HasTrailer = request.HasTrailer,
Tags = request.GetTags(),
OfficialRatings = request.GetOfficialRatings(),
2014-10-08 01:37:45 +00:00
Genres = request.GetGenres(),
2016-12-20 19:59:25 +00:00
ArtistIds = request.GetArtistIds(),
2015-11-06 15:02:22 +00:00
GenreIds = request.GetGenreIds(),
2015-03-13 18:45:03 +00:00
StudioIds = request.GetStudioIds(),
2014-10-08 01:37:45 +00:00
Person = request.Person,
2015-03-18 16:40:16 +00:00
PersonIds = request.GetPersonIds(),
2014-10-08 01:37:45 +00:00
PersonTypes = request.GetPersonTypes(),
Years = request.GetYears(),
ImageTypes = request.GetImageTypes(),
VideoTypes = request.GetVideoTypes(),
2015-07-14 19:04:16 +00:00
AdjacentTo = request.AdjacentTo,
ItemIds = request.GetItemIds(),
MinPlayers = request.MinPlayers,
MaxPlayers = request.MaxPlayers,
MinCommunityRating = request.MinCommunityRating,
2016-03-20 06:46:51 +00:00
MinCriticRating = request.MinCriticRating,
2016-03-20 19:53:22 +00:00
ParentId = string.IsNullOrWhiteSpace(request.ParentId) ? (Guid?)null : new Guid(request.ParentId),
2016-03-20 20:04:27 +00:00
ParentIndexNumber = request.ParentIndexNumber,
2016-03-21 00:15:56 +00:00
AiredDuringSeason = request.AiredDuringSeason,
2016-07-22 05:38:01 +00:00
EnableTotalRecordCount = request.EnableTotalRecordCount,
2016-10-08 05:57:38 +00:00
ExcludeItemIds = request.GetExcludeItemIds(),
2016-10-10 18:18:28 +00:00
DtoOptions = dtoOptions
2014-10-06 23:58:46 +00:00
};
2014-10-08 01:37:45 +00:00
if (!string.IsNullOrWhiteSpace(request.Ids))
{
query.CollapseBoxSetItems = false;
}
2014-10-06 23:58:46 +00:00
foreach (var filter in request.GetFilters())
{
switch (filter)
{
case ItemFilter.Dislikes:
query.IsLiked = false;
break;
case ItemFilter.IsFavorite:
query.IsFavorite = true;
break;
case ItemFilter.IsFavoriteOrLikes:
query.IsFavoriteOrLiked = true;
break;
case ItemFilter.IsFolder:
query.IsFolder = true;
break;
case ItemFilter.IsNotFolder:
query.IsFolder = false;
break;
case ItemFilter.IsPlayed:
query.IsPlayed = true;
break;
case ItemFilter.IsResumable:
query.IsResumable = true;
break;
case ItemFilter.IsUnplayed:
query.IsPlayed = false;
break;
case ItemFilter.Likes:
query.IsLiked = true;
break;
}
}
2013-02-21 01:33:05 +00:00
2017-05-23 16:43:24 +00:00
if (!string.IsNullOrEmpty(request.MinDateLastSaved))
{
query.MinDateLastSaved = DateTime.Parse(request.MinDateLastSaved, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
}
if (!string.IsNullOrEmpty(request.MinDateLastSavedForUser))
{
query.MinDateLastSavedForUser = DateTime.Parse(request.MinDateLastSavedForUser, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
}
2016-03-20 06:46:51 +00:00
if (!string.IsNullOrEmpty(request.MinPremiereDate))
{
query.MinPremiereDate = DateTime.Parse(request.MinPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
}
if (!string.IsNullOrEmpty(request.MaxPremiereDate))
{
query.MaxPremiereDate = DateTime.Parse(request.MaxPremiereDate, null, DateTimeStyles.RoundtripKind).ToUniversalTime();
}
2016-03-20 19:53:22 +00:00
// Filter by Series Status
if (!string.IsNullOrEmpty(request.SeriesStatus))
{
query.SeriesStatuses = request.SeriesStatus.Split(',').Select(d => (SeriesStatus)Enum.Parse(typeof(SeriesStatus), d, true)).ToArray();
}
// ExcludeLocationTypes
if (!string.IsNullOrEmpty(request.ExcludeLocationTypes))
{
2017-01-09 17:05:34 +00:00
var excludeLocationTypes = request.ExcludeLocationTypes.Split(',').Select(d => (LocationType)Enum.Parse(typeof(LocationType), d, true)).ToArray();
if (excludeLocationTypes.Contains(LocationType.Virtual))
{
query.IsVirtualItem = false;
}
2016-03-20 19:53:22 +00:00
}
if (!string.IsNullOrEmpty(request.LocationTypes))
{
2017-05-06 05:18:54 +00:00
var requestedLocationTypes =
2017-08-19 19:43:35 +00:00
request.LocationTypes.Split(',');
2017-05-06 05:18:54 +00:00
2017-08-19 19:43:35 +00:00
if (requestedLocationTypes.Length > 0 && requestedLocationTypes.Length < 4)
2017-05-06 05:18:54 +00:00
{
2017-08-19 19:43:35 +00:00
query.IsVirtualItem = requestedLocationTypes.Contains(LocationType.Virtual.ToString());
2017-05-06 05:18:54 +00:00
}
2016-03-20 19:53:22 +00:00
}
2016-06-25 00:29:39 +00:00
2016-03-21 03:10:37 +00:00
// Min official rating
2016-04-27 17:50:32 +00:00
if (!string.IsNullOrWhiteSpace(request.MinOfficialRating))
2016-03-21 03:10:37 +00:00
{
query.MinParentalRating = _localization.GetRatingLevel(request.MinOfficialRating);
}
// Max official rating
2016-04-27 17:50:32 +00:00
if (!string.IsNullOrWhiteSpace(request.MaxOfficialRating))
2016-03-21 03:10:37 +00:00
{
2016-04-27 17:50:32 +00:00
query.MaxParentalRating = _localization.GetRatingLevel(request.MaxOfficialRating);
2016-03-21 03:10:37 +00:00
}
2016-03-20 19:53:22 +00:00
2016-03-21 16:50:50 +00:00
// Artists
if (!string.IsNullOrEmpty(request.Artists))
{
2016-12-20 19:59:25 +00:00
query.ArtistIds = request.Artists.Split('|').Select(i =>
{
try
{
2017-05-22 04:54:02 +00:00
return _libraryManager.GetArtist(i, new DtoOptions(false));
2016-12-20 19:59:25 +00:00
}
catch
{
return null;
}
}).Where(i => i != null).Select(i => i.Id.ToString("N")).ToArray();
2016-03-21 16:50:50 +00:00
}
2016-07-22 22:10:39 +00:00
// ExcludeArtistIds
if (!string.IsNullOrWhiteSpace(request.ExcludeArtistIds))
2016-07-22 17:54:43 +00:00
{
2016-07-22 22:10:39 +00:00
query.ExcludeArtistIds = request.ExcludeArtistIds.Split('|');
2016-07-22 17:54:43 +00:00
}
if (!string.IsNullOrWhiteSpace(request.AlbumIds))
{
query.AlbumIds = request.AlbumIds.Split('|');
}
2016-03-21 16:50:50 +00:00
// Albums
if (!string.IsNullOrEmpty(request.Albums))
{
2017-05-21 07:25:49 +00:00
query.AlbumIds = request.Albums.Split('|').SelectMany(i =>
{
2017-05-21 07:25:49 +00:00
return _libraryManager.GetItemIds(new InternalItemsQuery
{
IncludeItemTypes = new[] { typeof(MusicAlbum).Name },
Name = i,
Limit = 1
2017-05-21 07:25:49 +00:00
}).Select(albumId => albumId.ToString("N"));
}).ToArray();
2016-03-21 16:50:50 +00:00
}
2016-12-20 19:59:25 +00:00
// Studios
if (!string.IsNullOrEmpty(request.Studios))
{
query.StudioIds = request.Studios.Split('|').Select(i =>
{
try
{
return _libraryManager.GetStudio(i);
}
catch
{
return null;
}
}).Where(i => i != null).Select(i => i.Id.ToString("N")).ToArray();
}
2017-09-07 18:17:18 +00:00
// Apply default sorting if none requested
if (query.OrderBy.Length == 0)
{
// Albums by artist
if (query.ArtistIds.Length > 0 && query.IncludeItemTypes.Length == 1 && string.Equals(query.IncludeItemTypes[0], "MusicAlbum", StringComparison.OrdinalIgnoreCase))
{
query.OrderBy = new Tuple<string, SortOrder>[]
{
new Tuple<string, SortOrder>(ItemSortBy.ProductionYear, SortOrder.Descending),
new Tuple<string, SortOrder>(ItemSortBy.SortName, SortOrder.Ascending)
};
}
}
2014-10-06 23:58:46 +00:00
return query;
2013-02-21 01:33:05 +00:00
}
}
/// <summary>
/// Class DateCreatedComparer
/// </summary>
public class DateCreatedComparer : IComparer<BaseItem>
{
/// <summary>
/// Compares the specified x.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>System.Int32.</returns>
public int Compare(BaseItem x, BaseItem y)
{
return x.DateCreated.CompareTo(y.DateCreated);
}
}
}