2019-01-13 13:01:16 -07:00
using System ;
2019-01-13 12:24:58 -07:00
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
2019-09-01 23:19:29 -07:00
using MediaBrowser.Common.Extensions ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Controller.Dto ;
2013-04-13 11:02:30 -07:00
using MediaBrowser.Controller.Entities ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Controller.Entities.Audio ;
2013-02-20 18:33:05 -07:00
using MediaBrowser.Controller.Library ;
2014-07-02 11:34:08 -07:00
using MediaBrowser.Controller.Net ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Controller.Providers ;
2013-02-21 10:50:46 -07:00
using MediaBrowser.Model.Dto ;
2013-10-02 10:23:10 -07:00
using MediaBrowser.Model.Entities ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.IO ;
2019-01-13 12:24:58 -07:00
using MediaBrowser.Model.Querying ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services ;
2013-02-20 18:33:05 -07:00
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class GetItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}", "GET", Summary = "Gets an item from a user's library")]
2013-02-21 10:50:46 -07:00
public class GetItem : IReturn < BaseItemDto >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-20 18:33:05 -07:00
public string Id { get ; set ; }
}
2013-03-08 19:34:54 -07:00
/// <summary>
/// Class GetItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/Root", "GET", Summary = "Gets the root folder from a user's library")]
2013-03-08 19:34:54 -07:00
public class GetRootFolder : IReturn < BaseItemDto >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-03-08 19:34:54 -07:00
}
2013-02-20 18:33:05 -07:00
/// <summary>
/// Class GetIntros
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}/Intros", "GET", Summary = "Gets intros to play before the main media item plays")]
2017-08-19 12:43:35 -07:00
public class GetIntros : IReturn < QueryResult < BaseItemDto > >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the item id.
/// </summary>
/// <value>The item id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-20 18:33:05 -07:00
public string Id { get ; set ; }
}
/// <summary>
/// Class MarkFavoriteItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/FavoriteItems/{Id}", "POST", Summary = "Marks an item as a favorite")]
2013-08-22 14:33:39 -07:00
public class MarkFavoriteItem : IReturn < UserItemDataDto >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid Id { get ; set ; }
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Class UnmarkFavoriteItem
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/FavoriteItems/{Id}", "DELETE", Summary = "Unmarks an item as a favorite")]
2013-08-22 14:33:39 -07:00
public class UnmarkFavoriteItem : IReturn < UserItemDataDto >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2018-09-12 10:26:21 -07:00
public Guid Id { get ; set ; }
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Class ClearUserItemRating
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}/Rating", "DELETE", Summary = "Deletes a user's saved personal rating for an item")]
2013-08-22 14:33:39 -07:00
public class DeleteUserItemRating : IReturn < UserItemDataDto >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
2018-09-12 10:26:21 -07:00
public Guid Id { get ; set ; }
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Class UpdateUserItemRating
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}/Rating", "POST", Summary = "Updates a user's rating for an item")]
2013-08-22 14:33:39 -07:00
public class UpdateUserItemRating : IReturn < UserItemDataDto >
2013-02-20 18:33:05 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2013-03-08 15:41:38 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2018-09-12 10:26:21 -07:00
public Guid Id { get ; set ; }
2013-02-20 18:33:05 -07:00
/// <summary>
2013-04-24 09:37:12 -07:00
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
2013-02-20 18:33:05 -07:00
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
2013-03-08 16:10:37 -07:00
[ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]
2013-02-20 18:33:05 -07:00
public bool Likes { get ; set ; }
}
/// <summary>
2014-07-04 22:21:13 -07:00
/// Class GetLocalTrailers
2013-04-24 09:37:12 -07:00
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}/LocalTrailers", "GET", Summary = "Gets local trailers for an item")]
2017-08-19 12:43:35 -07:00
public class GetLocalTrailers : IReturn < BaseItemDto [ ] >
2013-02-24 19:41:51 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 19:41:51 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-24 19:41:51 -07:00
public string Id { get ; set ; }
}
2013-04-24 09:37:12 -07:00
/// <summary>
2014-07-04 22:21:13 -07:00
/// Class GetSpecialFeatures
2013-04-24 09:37:12 -07:00
/// </summary>
2014-11-14 19:31:03 -07:00
[Route("/Users/{UserId}/Items/{Id}/SpecialFeatures", "GET", Summary = "Gets special features for an item")]
2017-08-19 12:43:35 -07:00
public class GetSpecialFeatures : IReturn < BaseItemDto [ ] >
2013-02-24 19:41:51 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 19:41:51 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "Id", Description = "Movie Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-02-24 19:41:51 -07:00
public string Id { get ; set ; }
}
2014-07-04 22:21:13 -07:00
[Route("/Users/{UserId}/Items/Latest", "GET", Summary = "Gets latest media")]
2017-08-19 12:43:35 -07:00
public class GetLatestMedia : IReturn < BaseItemDto [ ] > , IHasDtoOptions
2013-02-24 19:41:51 -07:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid UserId { get ; set ; }
2013-02-24 19:41:51 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "Limit", Description = "Limit", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int Limit { get ; set ; }
2013-02-24 19:41:51 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2018-09-12 10:26:21 -07:00
public Guid ParentId { get ; set ; }
2014-04-06 10:53:23 -07:00
2017-07-05 11:30:12 -07:00
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, SortName, Studios, Taglines", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2014-07-04 22:21:13 -07:00
public string Fields { get ; set ; }
2013-04-24 09:37:12 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
2013-02-20 18:33:05 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "IsFolder", Description = "Filter by items that are folders, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsFolder { get ; set ; }
2013-02-20 18:33:05 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "IsPlayed", Description = "Filter by items that are played, or not.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsPlayed { get ; set ; }
2013-02-20 18:33:05 -07:00
2014-07-04 22:21:13 -07:00
[ApiMember(Name = "GroupItems", Description = "Whether or not to group items into a parent container.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool GroupItems { get ; set ; }
2014-10-30 21:57:24 -07:00
2014-12-01 05:43:34 -07:00
[ApiMember(Name = "EnableImages", Description = "Optional, include image information in output", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? EnableImages { get ; set ; }
[ApiMember(Name = "ImageTypeLimit", Description = "Optional, the max number of images to return, per image type", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? ImageTypeLimit { get ; set ; }
[ApiMember(Name = "EnableImageTypes", Description = "Optional. The image types to include in the output.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string EnableImageTypes { get ; set ; }
2015-01-24 23:34:50 -07:00
2016-08-17 12:28:43 -07:00
[ApiMember(Name = "EnableUserData", Description = "Optional, include user data", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? EnableUserData { get ; set ; }
2014-07-04 22:21:13 -07:00
public GetLatestMedia ( )
{
Limit = 20 ;
GroupItems = true ;
}
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Class UserLibraryService
/// </summary>
2014-07-02 11:34:08 -07:00
[Authenticated]
2013-03-15 22:52:33 -07:00
public class UserLibraryService : BaseApiService
2013-02-20 18:33:05 -07:00
{
2013-02-27 13:25:45 -07:00
private readonly IUserManager _userManager ;
2013-10-02 09:08:58 -07:00
private readonly IUserDataManager _userDataRepository ;
2013-02-28 12:32:41 -07:00
private readonly ILibraryManager _libraryManager ;
2013-09-04 10:02:19 -07:00
private readonly IDtoService _dtoService ;
2014-06-07 12:46:24 -07:00
private readonly IUserViewManager _userViewManager ;
2016-08-14 14:29:35 -07:00
private readonly IFileSystem _fileSystem ;
2016-11-10 07:41:24 -07:00
private readonly IAuthorizationContext _authContext ;
2014-06-07 12:46:24 -07:00
2016-11-10 07:41:24 -07:00
public UserLibraryService ( IUserManager userManager , ILibraryManager libraryManager , IUserDataManager userDataRepository , IDtoService dtoService , IUserViewManager userViewManager , IFileSystem fileSystem , IAuthorizationContext authContext )
2013-02-24 14:53:54 -07:00
{
2013-02-27 13:25:45 -07:00
_userManager = userManager ;
2013-02-28 12:32:41 -07:00
_libraryManager = libraryManager ;
2013-04-13 11:02:30 -07:00
_userDataRepository = userDataRepository ;
2013-09-04 10:02:19 -07:00
_dtoService = dtoService ;
2014-06-07 12:46:24 -07:00
_userViewManager = userViewManager ;
2016-08-14 14:29:35 -07:00
_fileSystem = fileSystem ;
2016-11-10 07:41:24 -07:00
_authContext = authContext ;
2013-02-24 14:53:54 -07:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-02-20 18:33:05 -07:00
public object Get ( GetSpecialFeatures request )
2013-08-22 14:33:39 -07:00
{
2013-09-16 19:44:06 -07:00
var result = GetAsync ( request ) ;
2013-08-22 14:33:39 -07:00
2018-09-12 10:26:21 -07:00
return ToOptimizedResult ( result ) ;
2013-08-22 14:33:39 -07:00
}
2014-07-04 22:21:13 -07:00
public object Get ( GetLatestMedia request )
{
var user = _userManager . GetUserById ( request . UserId ) ;
2015-03-31 09:24:16 -07:00
if ( ! request . IsPlayed . HasValue )
{
if ( user . Configuration . HidePlayedInLatest )
{
request . IsPlayed = false ;
}
}
2017-05-21 00:25:49 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2015-01-26 09:47:15 -07:00
var list = _userViewManager . GetLatestItems ( new LatestItemsQuery
2014-07-04 22:21:13 -07:00
{
2015-01-26 09:47:15 -07:00
GroupItems = request . GroupItems ,
2017-08-09 12:56:38 -07:00
IncludeItemTypes = ApiEntryPoint . Split ( request . IncludeItemTypes , ',' , true ) ,
2015-01-26 09:47:15 -07:00
IsPlayed = request . IsPlayed ,
Limit = request . Limit ,
ParentId = request . ParentId ,
2017-05-21 00:25:49 -07:00
UserId = request . UserId ,
} , dtoOptions ) ;
2014-07-04 22:21:13 -07:00
var dtos = list . Select ( i = >
{
var item = i . Item2 [ 0 ] ;
var childCount = 0 ;
2016-12-11 22:49:19 -07:00
if ( i . Item1 ! = null & & ( i . Item2 . Count > 1 | | i . Item1 is MusicAlbum ) )
2014-07-04 22:21:13 -07:00
{
item = i . Item1 ;
childCount = i . Item2 . Count ;
}
2014-12-01 05:43:34 -07:00
2016-11-10 07:41:24 -07:00
var dto = _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2014-07-04 22:21:13 -07:00
dto . ChildCount = childCount ;
return dto ;
} ) ;
2017-08-19 12:43:35 -07:00
return ToOptimizedResult ( dtos . ToArray ( ) ) ;
2014-07-04 22:21:13 -07:00
}
2017-08-19 12:43:35 -07:00
private BaseItemDto [ ] GetAsync ( GetSpecialFeatures request )
2013-02-20 18:33:05 -07:00
{
2013-02-27 13:25:45 -07:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-20 18:33:05 -07:00
2014-06-04 19:32:40 -07:00
var item = string . IsNullOrEmpty ( request . Id ) ?
2018-09-12 10:26:21 -07:00
_libraryManager . GetUserRootFolder ( ) :
2014-04-25 13:15:50 -07:00
_libraryManager . GetItemById ( request . Id ) ;
2013-02-24 14:53:54 -07:00
2018-09-12 10:26:21 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2014-09-20 08:48:23 -07:00
2018-09-12 10:26:21 -07:00
var dtos = item . GetDisplayExtras ( )
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) ) ;
2014-09-20 08:48:23 -07:00
2018-09-12 10:26:21 -07:00
return dtos . ToArray ( ) ;
2013-02-20 18:33:05 -07:00
}
2013-02-24 14:53:54 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-02-20 18:33:05 -07:00
public object Get ( GetLocalTrailers request )
{
2013-02-27 13:25:45 -07:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
var item = string . IsNullOrEmpty ( request . Id ) ? _libraryManager . GetUserRootFolder ( ) : _libraryManager . GetItemById ( request . Id ) ;
2013-12-02 09:46:25 -07:00
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2014-12-26 22:08:39 -07:00
2019-08-19 09:40:39 -07:00
var dtosExtras = item . GetExtras ( new [ ] { ExtraType . Trailer } )
2019-09-01 23:19:29 -07:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) )
. ToArray ( ) ;
2019-08-19 09:40:39 -07:00
2019-09-01 23:19:29 -07:00
if ( item is IHasTrailers hasTrailers )
{
var trailers = hasTrailers . GetTrailers ( ) ;
var dtosTrailers = _dtoService . GetBaseItemDtos ( trailers , dtoOptions , user , item ) ;
var allTrailers = new BaseItemDto [ dtosExtras . Length + dtosTrailers . Count ] ;
dtosExtras . CopyTo ( allTrailers , 0 ) ;
dtosTrailers . CopyTo ( allTrailers , dtosExtras . Length ) ;
return ToOptimizedResult ( allTrailers ) ;
}
2013-02-20 18:33:05 -07:00
2019-09-01 23:19:29 -07:00
return ToOptimizedResult ( dtosExtras ) ;
2013-02-20 18:33:05 -07:00
}
2013-02-24 14:53:54 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-08-14 14:29:35 -07:00
public async Task < object > Get ( GetItem request )
2013-02-20 18:33:05 -07:00
{
2013-02-27 13:25:45 -07:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
var item = string . IsNullOrEmpty ( request . Id ) ? _libraryManager . GetUserRootFolder ( ) : _libraryManager . GetItemById ( request . Id ) ;
2013-02-24 14:53:54 -07:00
2016-08-14 14:29:35 -07:00
await RefreshItemOnDemandIfNeeded ( item ) . ConfigureAwait ( false ) ;
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2013-02-20 18:33:05 -07:00
2014-12-26 22:08:39 -07:00
var result = _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
return ToOptimizedResult ( result ) ;
2013-02-20 18:33:05 -07:00
}
2016-08-14 14:29:35 -07:00
private async Task RefreshItemOnDemandIfNeeded ( BaseItem item )
{
if ( item is Person )
{
var hasMetdata = ! string . IsNullOrWhiteSpace ( item . Overview ) & & item . HasImage ( ImageType . Primary ) ;
2016-08-14 14:45:36 -07:00
var performFullRefresh = ! hasMetdata & & ( DateTime . UtcNow - item . DateLastRefreshed ) . TotalDays > = 3 ;
2016-08-14 14:29:35 -07:00
if ( ! hasMetdata )
{
2018-12-14 12:17:29 -07:00
var options = new MetadataRefreshOptions ( new DirectoryService ( Logger , _fileSystem ) )
2016-08-14 14:29:35 -07:00
{
2016-08-14 14:45:36 -07:00
MetadataRefreshMode = MetadataRefreshMode . FullRefresh ,
2018-09-12 10:26:21 -07:00
ImageRefreshMode = MetadataRefreshMode . FullRefresh ,
2016-08-14 14:29:35 -07:00
ForceSave = performFullRefresh
} ;
await item . RefreshMetadata ( options , CancellationToken . None ) . ConfigureAwait ( false ) ;
}
}
}
2013-04-24 09:37:12 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2013-03-08 19:34:54 -07:00
public object Get ( GetRootFolder request )
{
var user = _userManager . GetUserById ( request . UserId ) ;
2018-09-12 10:26:21 -07:00
var item = _libraryManager . GetUserRootFolder ( ) ;
2013-03-08 19:34:54 -07:00
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2013-03-08 19:34:54 -07:00
2014-12-26 22:08:39 -07:00
var result = _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2013-03-08 19:34:54 -07:00
2018-09-12 10:26:21 -07:00
return ToOptimizedResult ( result ) ;
2013-03-08 19:34:54 -07:00
}
2013-04-24 09:37:12 -07:00
2013-02-20 18:33:05 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2014-09-22 14:56:54 -07:00
public async Task < object > Get ( GetIntros request )
2013-02-20 18:33:05 -07:00
{
2013-02-27 13:25:45 -07:00
var user = _userManager . GetUserById ( request . UserId ) ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
var item = string . IsNullOrEmpty ( request . Id ) ? _libraryManager . GetUserRootFolder ( ) : _libraryManager . GetItemById ( request . Id ) ;
2013-02-20 18:33:05 -07:00
2014-09-22 14:56:54 -07:00
var items = await _libraryManager . GetIntros ( item , user ) . ConfigureAwait ( false ) ;
2013-10-04 10:04:18 -07:00
2016-11-10 07:41:24 -07:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2013-10-04 10:04:18 -07:00
2017-08-09 12:56:38 -07:00
var dtos = items . Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user ) ) . ToArray ( ) ;
2013-10-04 10:04:18 -07:00
2017-08-19 12:43:35 -07:00
var result = new QueryResult < BaseItemDto >
2013-10-04 10:04:18 -07:00
{
Items = dtos ,
TotalRecordCount = dtos . Length
} ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
return ToOptimizedResult ( result ) ;
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2017-08-26 17:32:33 -07:00
public object Post ( MarkFavoriteItem request )
2013-02-20 18:33:05 -07:00
{
2018-09-12 10:26:21 -07:00
var dto = MarkFavorite ( request . UserId , request . Id , true ) ;
2013-08-22 14:56:19 -07:00
return ToOptimizedResult ( dto ) ;
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 14:56:19 -07:00
public object Delete ( UnmarkFavoriteItem request )
2013-02-20 18:33:05 -07:00
{
2017-08-26 17:32:33 -07:00
var dto = MarkFavorite ( request . UserId , request . Id , false ) ;
2013-02-20 18:33:05 -07:00
2013-08-22 18:36:34 -07:00
return ToOptimizedResult ( dto ) ;
}
2013-10-04 10:04:18 -07:00
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
2018-09-12 10:26:21 -07:00
private UserItemDataDto MarkFavorite ( Guid userId , Guid itemId , bool isFavorite )
2013-08-22 18:36:34 -07:00
{
var user = _userManager . GetUserById ( userId ) ;
2018-09-12 10:26:21 -07:00
var item = itemId . Equals ( Guid . Empty ) ? _libraryManager . GetUserRootFolder ( ) : _libraryManager . GetItemById ( itemId ) ;
2013-02-20 18:33:05 -07:00
// Get the user data for this item
2016-04-30 15:05:13 -07:00
var data = _userDataRepository . GetUserData ( user , item ) ;
2013-02-20 18:33:05 -07:00
// Set favorite status
2013-08-22 18:36:34 -07:00
data . IsFavorite = isFavorite ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
_userDataRepository . SaveUserData ( user , item , data , UserDataSaveReason . UpdateUserRating , CancellationToken . None ) ;
2013-08-22 14:56:19 -07:00
2017-05-25 23:48:54 -07:00
return _userDataRepository . GetUserDataDto ( item , user ) ;
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2013-08-22 14:56:19 -07:00
public object Delete ( DeleteUserItemRating request )
2013-02-20 18:33:05 -07:00
{
2017-08-26 17:32:33 -07:00
var dto = UpdateUserItemRating ( request . UserId , request . Id , null ) ;
2013-08-22 14:56:19 -07:00
return ToOptimizedResult ( dto ) ;
2013-02-20 18:33:05 -07:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2017-08-26 17:32:33 -07:00
public object Post ( UpdateUserItemRating request )
2013-02-20 18:33:05 -07:00
{
2018-09-12 10:26:21 -07:00
var dto = UpdateUserItemRating ( request . UserId , request . Id , request . Likes ) ;
2013-02-20 18:33:05 -07:00
2013-08-22 18:36:34 -07:00
return ToOptimizedResult ( dto ) ;
}
2013-10-04 10:04:18 -07:00
/// <summary>
/// Updates the user item rating.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="itemId">The item id.</param>
/// <param name="likes">if set to <c>true</c> [likes].</param>
2018-09-12 10:26:21 -07:00
private UserItemDataDto UpdateUserItemRating ( Guid userId , Guid itemId , bool? likes )
2013-08-22 18:36:34 -07:00
{
var user = _userManager . GetUserById ( userId ) ;
2018-09-12 10:26:21 -07:00
var item = itemId . Equals ( Guid . Empty ) ? _libraryManager . GetUserRootFolder ( ) : _libraryManager . GetItemById ( itemId ) ;
2013-02-20 18:33:05 -07:00
// Get the user data for this item
2016-04-30 15:05:13 -07:00
var data = _userDataRepository . GetUserData ( user , item ) ;
2013-02-20 18:33:05 -07:00
2013-08-22 18:36:34 -07:00
data . Likes = likes ;
2013-02-20 18:33:05 -07:00
2018-09-12 10:26:21 -07:00
_userDataRepository . SaveUserData ( user , item , data , UserDataSaveReason . UpdateUserRating , CancellationToken . None ) ;
2013-08-22 14:56:19 -07:00
2017-05-25 23:48:54 -07:00
return _userDataRepository . GetUserDataDto ( item , user ) ;
2013-02-20 18:33:05 -07:00
}
}
}