2020-11-21 06:26:03 -07:00
using System ;
2020-09-05 16:07:25 -07:00
using System.ComponentModel.DataAnnotations ;
2020-06-30 17:26:53 -07:00
using System.Linq ;
2020-08-06 07:17:45 -07:00
using Jellyfin.Api.Constants ;
2020-06-30 17:26:53 -07:00
using Jellyfin.Api.Extensions ;
2020-10-14 12:03:36 -07:00
using Jellyfin.Api.ModelBinders ;
2020-06-30 17:26:53 -07:00
using Jellyfin.Data.Entities ;
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Model.Dto ;
2020-10-09 16:52:39 -07:00
using MediaBrowser.Model.Entities ;
2020-06-30 17:26:53 -07:00
using MediaBrowser.Model.Querying ;
2020-08-06 07:17:45 -07:00
using Microsoft.AspNetCore.Authorization ;
2020-06-30 17:26:53 -07:00
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Persons controller.
/// </summary>
2020-08-06 07:17:45 -07:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-06-30 17:26:53 -07:00
public class PersonsController : BaseJellyfinApiController
{
private readonly ILibraryManager _libraryManager ;
private readonly IDtoService _dtoService ;
private readonly IUserManager _userManager ;
/// <summary>
/// Initializes a new instance of the <see cref="PersonsController"/> class.
/// </summary>
/// <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 ,
2021-11-16 04:24:17 -07:00
IUserManager userManager )
2020-06-30 17:26:53 -07:00
{
_libraryManager = libraryManager ;
_dtoService = dtoService ;
_userManager = userManager ;
}
/// <summary>
2020-11-05 04:27:22 -07:00
/// Gets all persons.
2020-06-30 17:26:53 -07:00
/// </summary>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="searchTerm">The search term.</param>
2020-10-09 16:35:08 -07:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-11-10 11:48:21 -07:00
/// <param name="filters">Optional. Specify additional filters to apply.</param>
2020-11-05 04:27:22 -07:00
/// <param name="isFavorite">Optional filter by items that are marked as favorite, or not. userId is required.</param>
2020-06-30 17:26:53 -07:00
/// <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>
2020-11-05 04:27:22 -07:00
/// <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>
2020-06-30 17:26:53 -07:00
/// <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 ,
2020-07-07 08:10:51 -07:00
[FromQuery] string? searchTerm ,
2020-11-09 14:59:04 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields [ ] fields ,
2020-10-14 12:03:36 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter [ ] filters ,
2020-06-30 17:26:53 -07:00
[FromQuery] bool? isFavorite ,
[FromQuery] bool? enableUserData ,
[FromQuery] int? imageTypeLimit ,
2020-11-09 14:53:23 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType [ ] enableImageTypes ,
2020-11-16 20:29:46 -07:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string [ ] excludePersonTypes ,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string [ ] personTypes ,
2020-12-01 11:07:41 -07:00
[FromQuery] Guid ? appearsInItemId ,
2020-07-07 08:10:51 -07:00
[FromQuery] Guid ? userId ,
2020-11-05 04:27:22 -07:00
[FromQuery] bool? enableImages = true )
2020-06-30 17:26:53 -07:00
{
2020-10-29 10:36:45 -07:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-30 17:26:53 -07:00
. AddClientFields ( Request )
. AddAdditionalDtoOptions ( enableImages , enableUserData , imageTypeLimit , enableImageTypes ) ;
User ? user = null ;
2020-07-07 08:10:51 -07:00
if ( userId . HasValue & & ! userId . Equals ( Guid . Empty ) )
2020-06-30 17:26:53 -07:00
{
2020-07-07 08:10:51 -07:00
user = _userManager . GetUserById ( userId . Value ) ;
2020-06-30 17:26:53 -07:00
}
2020-11-05 04:27:22 -07:00
var isFavoriteInFilters = filters . Any ( f = > f = = ItemFilter . IsFavorite ) ;
2021-05-11 04:55:46 -07:00
var peopleItems = _libraryManager . GetPeopleItems ( new InternalPeopleQuery (
personTypes ,
excludePersonTypes )
2020-06-30 17:26:53 -07:00
{
2020-11-05 04:27:22 -07:00
NameContains = searchTerm ,
User = user ,
IsFavorite = ! isFavorite . HasValue & & isFavoriteInFilters ? true : isFavorite ,
2020-12-01 11:07:41 -07:00
AppearsInItemId = appearsInItemId ? ? Guid . Empty ,
2020-11-05 04:27:22 -07:00
Limit = limit ? ? 0
2020-06-30 17:26:53 -07:00
} ) ;
2022-01-20 08:46:17 -07:00
return new QueryResult < BaseItemDto > ( peopleItems . Select ( person = > _dtoService . GetItemByNameDto ( person , dtoOptions , null , user ) ) . ToArray ( ) ) ;
2020-06-30 17:26:53 -07: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)]
2020-09-06 08:07:27 -07:00
public ActionResult < BaseItemDto > GetPerson ( [ FromRoute , Required ] string name , [ FromQuery ] Guid ? userId )
2020-06-30 17:26:53 -07:00
{
var dtoOptions = new DtoOptions ( )
. AddClientFields ( Request ) ;
var item = _libraryManager . GetPerson ( name ) ;
if ( item = = null )
{
return NotFound ( ) ;
}
2020-07-07 08:10:51 -07:00
if ( userId . HasValue & & ! userId . Equals ( Guid . Empty ) )
2020-06-30 17:26:53 -07:00
{
2020-07-07 08:10:51 -07:00
var user = _userManager . GetUserById ( userId . Value ) ;
2020-06-30 17:26:53 -07:00
return _dtoService . GetBaseItemDto ( item , dtoOptions , user ) ;
}
return _dtoService . GetBaseItemDto ( item , dtoOptions ) ;
}
}
}