2020-11-21 13:26:03 +00:00
using System ;
2020-09-05 23:07:25 +00:00
using System.ComponentModel.DataAnnotations ;
2020-07-01 00:26:53 +00:00
using System.Linq ;
using Jellyfin.Api.Extensions ;
2023-02-17 22:16:08 +00:00
using Jellyfin.Api.Helpers ;
2020-10-14 19:03:36 +00:00
using Jellyfin.Api.ModelBinders ;
2020-07-01 00:26:53 +00:00
using Jellyfin.Data.Entities ;
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Model.Dto ;
2020-10-09 23:52:39 +00:00
using MediaBrowser.Model.Entities ;
2020-07-01 00:26:53 +00:00
using MediaBrowser.Model.Querying ;
2020-08-06 14:17:45 +00:00
using Microsoft.AspNetCore.Authorization ;
2020-07-01 00:26:53 +00:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers ;
/// <summary>
/// Persons controller.
/// </summary>
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
public class PersonsController : BaseJellyfinApiController
2020-07-01 00:26:53 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly ILibraryManager _libraryManager ;
private readonly IDtoService _dtoService ;
private readonly IUserManager _userManager ;
2020-07-01 00:26:53 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="PersonsController"/> class.
2020-07-01 00:26:53 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
public PersonsController (
ILibraryManager libraryManager ,
IDtoService dtoService ,
IUserManager userManager )
2020-07-01 00:26:53 +00:00
{
2023-01-31 11:18:10 +00:00
_libraryManager = libraryManager ;
_dtoService = dtoService ;
_userManager = userManager ;
}
2020-07-01 00:26:53 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets all persons.
/// </summary>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="searchTerm">The search term.</param>
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
/// <param name="filters">Optional. Specify additional filters to apply.</param>
/// <param name="isFavorite">Optional filter by items that are marked as favorite, or not. userId is required.</param>
/// <param name="enableUserData">Optional, include user data.</param>
/// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
/// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
/// <param name="excludePersonTypes">Optional. If specified results will be filtered to exclude those containing the specified PersonType. Allows multiple, comma-delimited.</param>
/// <param name="personTypes">Optional. If specified results will be filtered to include only those containing the specified PersonType. Allows multiple, comma-delimited.</param>
/// <param name="appearsInItemId">Optional. If specified, person results will be filtered on items related to said persons.</param>
/// <param name="userId">User id.</param>
/// <param name="enableImages">Optional, include image information in output.</param>
/// <response code="200">Persons returned.</response>
/// <returns>An <see cref="OkResult"/> containing the queryresult of persons.</returns>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult < QueryResult < BaseItemDto > > GetPersons (
[FromQuery] int? limit ,
[FromQuery] string? searchTerm ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields [ ] fields ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter [ ] filters ,
[FromQuery] bool? isFavorite ,
[FromQuery] bool? enableUserData ,
[FromQuery] int? imageTypeLimit ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType [ ] enableImageTypes ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string [ ] excludePersonTypes ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string [ ] personTypes ,
[FromQuery] Guid ? appearsInItemId ,
[FromQuery] Guid ? userId ,
[FromQuery] bool? enableImages = true )
{
2023-02-17 22:16:08 +00:00
userId = RequestHelpers . GetUserId ( User , userId ) ;
2023-01-31 11:18:10 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
. AddClientFields ( User )
. AddAdditionalDtoOptions ( enableImages , enableUserData , imageTypeLimit , enableImageTypes ) ;
2023-02-17 22:16:08 +00:00
User ? user = userId . Value . Equals ( default )
2023-01-31 11:18:10 +00:00
? null
: _userManager . GetUserById ( userId . Value ) ;
2020-07-01 00:26:53 +00:00
2023-01-31 11:18:10 +00:00
var isFavoriteInFilters = filters . Any ( f = > f = = ItemFilter . IsFavorite ) ;
var peopleItems = _libraryManager . GetPeopleItems ( new InternalPeopleQuery (
personTypes ,
excludePersonTypes )
2020-07-01 00:26:53 +00:00
{
2023-01-31 11:18:10 +00:00
NameContains = searchTerm ,
User = user ,
IsFavorite = ! isFavorite . HasValue & & isFavoriteInFilters ? true : isFavorite ,
AppearsInItemId = appearsInItemId ? ? Guid . Empty ,
Limit = limit ? ? 0
} ) ;
2020-07-01 00:26:53 +00:00
2023-01-31 11:18:10 +00:00
return new QueryResult < BaseItemDto > (
peopleItems
. Select ( person = > _dtoService . GetItemByNameDto ( person , dtoOptions , null , user ) )
. ToArray ( ) ) ;
}
2020-07-01 00:26:53 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Get person by name.
/// </summary>
/// <param name="name">Person name.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <response code="200">Person returned.</response>
/// <response code="404">Person not found.</response>
/// <returns>An <see cref="OkResult"/> containing the person on success,
/// or a <see cref="NotFoundResult"/> if person not found.</returns>
[HttpGet("{name}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult < BaseItemDto > GetPerson ( [ FromRoute , Required ] string name , [ FromQuery ] Guid ? userId )
{
2023-02-17 22:16:08 +00:00
userId = RequestHelpers . GetUserId ( User , userId ) ;
2023-01-31 11:18:10 +00:00
var dtoOptions = new DtoOptions ( )
. AddClientFields ( User ) ;
2020-07-01 00:26:53 +00:00
2023-01-31 11:18:10 +00:00
var item = _libraryManager . GetPerson ( name ) ;
if ( item is null )
{
return NotFound ( ) ;
2020-07-01 00:26:53 +00:00
}
2023-02-17 22:16:08 +00:00
if ( ! userId . Value . Equals ( default ) )
2020-07-01 00:26:53 +00:00
{
2023-01-31 11:18:10 +00:00
var user = _userManager . GetUserById ( userId . Value ) ;
return _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
2020-07-01 00:26:53 +00:00
}
2023-01-31 11:18:10 +00:00
return _dtoService . GetBaseItemDto ( item , dtoOptions ) ;
2020-07-01 00:26:53 +00:00
}
}