< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.PersonsController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/PersonsController.cs
Line coverage
35%
Covered lines: 14
Uncovered lines: 26
Coverable lines: 40
Total lines: 141
Line coverage: 35%
Branch coverage
10%
Covered branches: 1
Total branches: 10
Branch coverage: 10%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetPersons(...)0%4260%
GetPerson(...)25%5.02460%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/PersonsController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Linq;
 4using Jellyfin.Api.Extensions;
 5using Jellyfin.Api.Helpers;
 6using Jellyfin.Api.ModelBinders;
 7using Jellyfin.Data.Entities;
 8using Jellyfin.Extensions;
 9using MediaBrowser.Controller.Dto;
 10using MediaBrowser.Controller.Entities;
 11using MediaBrowser.Controller.Library;
 12using MediaBrowser.Model.Dto;
 13using MediaBrowser.Model.Entities;
 14using MediaBrowser.Model.Querying;
 15using Microsoft.AspNetCore.Authorization;
 16using Microsoft.AspNetCore.Http;
 17using Microsoft.AspNetCore.Mvc;
 18
 19namespace Jellyfin.Api.Controllers;
 20
 21/// <summary>
 22/// Persons controller.
 23/// </summary>
 24[Authorize]
 25public class PersonsController : BaseJellyfinApiController
 26{
 27    private readonly ILibraryManager _libraryManager;
 28    private readonly IDtoService _dtoService;
 29    private readonly IUserManager _userManager;
 30
 31    /// <summary>
 32    /// Initializes a new instance of the <see cref="PersonsController"/> class.
 33    /// </summary>
 34    /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
 35    /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
 36    /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 137    public PersonsController(
 138        ILibraryManager libraryManager,
 139        IDtoService dtoService,
 140        IUserManager userManager)
 41    {
 142        _libraryManager = libraryManager;
 143        _dtoService = dtoService;
 144        _userManager = userManager;
 145    }
 46
 47    /// <summary>
 48    /// Gets all persons.
 49    /// </summary>
 50    /// <param name="limit">Optional. The maximum number of records to return.</param>
 51    /// <param name="searchTerm">The search term.</param>
 52    /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
 53    /// <param name="filters">Optional. Specify additional filters to apply.</param>
 54    /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not. userId is required.</para
 55    /// <param name="enableUserData">Optional, include user data.</param>
 56    /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
 57    /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
 58    /// <param name="excludePersonTypes">Optional. If specified results will be filtered to exclude those containing the
 59    /// <param name="personTypes">Optional. If specified results will be filtered to include only those containing the s
 60    /// <param name="appearsInItemId">Optional. If specified, person results will be filtered on items related to said p
 61    /// <param name="userId">User id.</param>
 62    /// <param name="enableImages">Optional, include image information in output.</param>
 63    /// <response code="200">Persons returned.</response>
 64    /// <returns>An <see cref="OkResult"/> containing the queryresult of persons.</returns>
 65    [HttpGet]
 66    [ProducesResponseType(StatusCodes.Status200OK)]
 67    public ActionResult<QueryResult<BaseItemDto>> GetPersons(
 68        [FromQuery] int? limit,
 69        [FromQuery] string? searchTerm,
 70        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
 71        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFilter[] filters,
 72        [FromQuery] bool? isFavorite,
 73        [FromQuery] bool? enableUserData,
 74        [FromQuery] int? imageTypeLimit,
 75        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
 76        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] excludePersonTypes,
 77        [FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] personTypes,
 78        [FromQuery] Guid? appearsInItemId,
 79        [FromQuery] Guid? userId,
 80        [FromQuery] bool? enableImages = true)
 81    {
 082        userId = RequestHelpers.GetUserId(User, userId);
 083        var dtoOptions = new DtoOptions { Fields = fields }
 084            .AddClientFields(User)
 085            .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
 86
 087        User? user = userId.IsNullOrEmpty()
 088            ? null
 089            : _userManager.GetUserById(userId.Value);
 90
 091        var isFavoriteInFilters = filters.Any(f => f == ItemFilter.IsFavorite);
 092        var peopleItems = _libraryManager.GetPeopleItems(new InternalPeopleQuery(
 093            personTypes,
 094            excludePersonTypes)
 095        {
 096            NameContains = searchTerm,
 097            User = user,
 098            IsFavorite = !isFavorite.HasValue && isFavoriteInFilters ? true : isFavorite,
 099            AppearsInItemId = appearsInItemId ?? Guid.Empty,
 0100            Limit = limit ?? 0
 0101        });
 102
 0103        return new QueryResult<BaseItemDto>(
 0104            peopleItems
 0105            .Select(person => _dtoService.GetItemByNameDto(person, dtoOptions, null, user))
 0106            .ToArray());
 107    }
 108
 109    /// <summary>
 110    /// Get person by name.
 111    /// </summary>
 112    /// <param name="name">Person name.</param>
 113    /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
 114    /// <response code="200">Person returned.</response>
 115    /// <response code="404">Person not found.</response>
 116    /// <returns>An <see cref="OkResult"/> containing the person on success,
 117    /// or a <see cref="NotFoundResult"/> if person not found.</returns>
 118    [HttpGet("{name}")]
 119    [ProducesResponseType(StatusCodes.Status200OK)]
 120    [ProducesResponseType(StatusCodes.Status404NotFound)]
 121    public ActionResult<BaseItemDto> GetPerson([FromRoute, Required] string name, [FromQuery] Guid? userId)
 122    {
 1123        userId = RequestHelpers.GetUserId(User, userId);
 1124        var dtoOptions = new DtoOptions()
 1125            .AddClientFields(User);
 126
 1127        var item = _libraryManager.GetPerson(name);
 1128        if (item is null)
 129        {
 1130            return NotFound();
 131        }
 132
 0133        if (!userId.IsNullOrEmpty())
 134        {
 0135            var user = _userManager.GetUserById(userId.Value);
 0136            return _dtoService.GetBaseItemDto(item, dtoOptions, user);
 137        }
 138
 0139        return _dtoService.GetBaseItemDto(item, dtoOptions);
 140    }
 141}