PersonsController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using Jellyfin.Api.Constants;
  5. using Jellyfin.Api.Extensions;
  6. using Jellyfin.Api.Helpers;
  7. using Jellyfin.Data.Entities;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Querying;
  13. using Microsoft.AspNetCore.Authorization;
  14. using Microsoft.AspNetCore.Http;
  15. using Microsoft.AspNetCore.Mvc;
  16. namespace Jellyfin.Api.Controllers
  17. {
  18. /// <summary>
  19. /// Persons controller.
  20. /// </summary>
  21. [Authorize(Policy = Policies.DefaultAuthorization)]
  22. public class PersonsController : BaseJellyfinApiController
  23. {
  24. private readonly ILibraryManager _libraryManager;
  25. private readonly IDtoService _dtoService;
  26. private readonly IUserManager _userManager;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="PersonsController"/> class.
  29. /// </summary>
  30. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  31. /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
  32. /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
  33. public PersonsController(
  34. ILibraryManager libraryManager,
  35. IDtoService dtoService,
  36. IUserManager userManager)
  37. {
  38. _libraryManager = libraryManager;
  39. _dtoService = dtoService;
  40. _userManager = userManager;
  41. }
  42. /// <summary>
  43. /// Gets all persons from a given item, folder, or the entire library.
  44. /// </summary>
  45. /// <param name="minCommunityRating">Optional filter by minimum community rating.</param>
  46. /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
  47. /// <param name="limit">Optional. The maximum number of records to return.</param>
  48. /// <param name="searchTerm">The search term.</param>
  49. /// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
  50. /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimited. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines.</param>
  51. /// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
  52. /// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allows multiple, comma delimited.</param>
  53. /// <param name="filters">Optional. Specify additional filters to apply. This allows multiple, comma delimited. Options: IsFolder, IsNotFolder, IsUnplayed, IsPlayed, IsFavorite, IsResumable, Likes, Dislikes.</param>
  54. /// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
  55. /// <param name="mediaTypes">Optional filter by MediaType. Allows multiple, comma delimited.</param>
  56. /// <param name="genres">Optional. If specified, results will be filtered based on genre. This allows multiple, pipe delimited.</param>
  57. /// <param name="genreIds">Optional. If specified, results will be filtered based on genre id. This allows multiple, pipe delimited.</param>
  58. /// <param name="officialRatings">Optional. If specified, results will be filtered based on OfficialRating. This allows multiple, pipe delimited.</param>
  59. /// <param name="tags">Optional. If specified, results will be filtered based on tag. This allows multiple, pipe delimited.</param>
  60. /// <param name="years">Optional. If specified, results will be filtered based on production year. This allows multiple, comma delimited.</param>
  61. /// <param name="enableUserData">Optional, include user data.</param>
  62. /// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
  63. /// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
  64. /// <param name="person">Optional. If specified, results will be filtered to include only those containing the specified person.</param>
  65. /// <param name="personIds">Optional. If specified, results will be filtered to include only those containing the specified person id.</param>
  66. /// <param name="personTypes">Optional. If specified, along with Person, results will be filtered to include only those containing the specified person and PersonType. Allows multiple, comma-delimited.</param>
  67. /// <param name="studios">Optional. If specified, results will be filtered based on studio. This allows multiple, pipe delimited.</param>
  68. /// <param name="studioIds">Optional. If specified, results will be filtered based on studio id. This allows multiple, pipe delimited.</param>
  69. /// <param name="userId">User id.</param>
  70. /// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
  71. /// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
  72. /// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
  73. /// <param name="enableImages">Optional, include image information in output.</param>
  74. /// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
  75. /// <response code="200">Persons returned.</response>
  76. /// <returns>An <see cref="OkResult"/> containing the queryresult of persons.</returns>
  77. [HttpGet]
  78. [ProducesResponseType(StatusCodes.Status200OK)]
  79. public ActionResult<QueryResult<BaseItemDto>> GetPersons(
  80. [FromQuery] double? minCommunityRating,
  81. [FromQuery] int? startIndex,
  82. [FromQuery] int? limit,
  83. [FromQuery] string? searchTerm,
  84. [FromQuery] string? parentId,
  85. [FromQuery] string? fields,
  86. [FromQuery] string? excludeItemTypes,
  87. [FromQuery] string? includeItemTypes,
  88. [FromQuery] string? filters,
  89. [FromQuery] bool? isFavorite,
  90. [FromQuery] string? mediaTypes,
  91. [FromQuery] string? genres,
  92. [FromQuery] string? genreIds,
  93. [FromQuery] string? officialRatings,
  94. [FromQuery] string? tags,
  95. [FromQuery] string? years,
  96. [FromQuery] bool? enableUserData,
  97. [FromQuery] int? imageTypeLimit,
  98. [FromQuery] string? enableImageTypes,
  99. [FromQuery] string? person,
  100. [FromQuery] string? personIds,
  101. [FromQuery] string? personTypes,
  102. [FromQuery] string? studios,
  103. [FromQuery] string? studioIds,
  104. [FromQuery] Guid? userId,
  105. [FromQuery] string? nameStartsWithOrGreater,
  106. [FromQuery] string? nameStartsWith,
  107. [FromQuery] string? nameLessThan,
  108. [FromQuery] bool? enableImages = true,
  109. [FromQuery] bool enableTotalRecordCount = true)
  110. {
  111. var dtoOptions = new DtoOptions()
  112. .AddItemFields(fields)
  113. .AddClientFields(Request)
  114. .AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
  115. User? user = null;
  116. BaseItem parentItem;
  117. if (userId.HasValue && !userId.Equals(Guid.Empty))
  118. {
  119. user = _userManager.GetUserById(userId.Value);
  120. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.GetUserRootFolder() : _libraryManager.GetItemById(parentId);
  121. }
  122. else
  123. {
  124. parentItem = string.IsNullOrEmpty(parentId) ? _libraryManager.RootFolder : _libraryManager.GetItemById(parentId);
  125. }
  126. var query = new InternalItemsQuery(user)
  127. {
  128. ExcludeItemTypes = RequestHelpers.Split(excludeItemTypes, ',', true),
  129. IncludeItemTypes = RequestHelpers.Split(includeItemTypes, ',', true),
  130. MediaTypes = RequestHelpers.Split(mediaTypes, ',', true),
  131. StartIndex = startIndex,
  132. Limit = limit,
  133. IsFavorite = isFavorite,
  134. NameLessThan = nameLessThan,
  135. NameStartsWith = nameStartsWith,
  136. NameStartsWithOrGreater = nameStartsWithOrGreater,
  137. Tags = RequestHelpers.Split(tags, '|', true),
  138. OfficialRatings = RequestHelpers.Split(officialRatings, '|', true),
  139. Genres = RequestHelpers.Split(genres, '|', true),
  140. GenreIds = RequestHelpers.GetGuids(genreIds),
  141. StudioIds = RequestHelpers.GetGuids(studioIds),
  142. Person = person,
  143. PersonIds = RequestHelpers.GetGuids(personIds),
  144. PersonTypes = RequestHelpers.Split(personTypes, ',', true),
  145. Years = RequestHelpers.Split(years, ',', true).Select(y => Convert.ToInt32(y, CultureInfo.InvariantCulture)).ToArray(),
  146. MinCommunityRating = minCommunityRating,
  147. DtoOptions = dtoOptions,
  148. SearchTerm = searchTerm,
  149. EnableTotalRecordCount = enableTotalRecordCount
  150. };
  151. if (!string.IsNullOrWhiteSpace(parentId))
  152. {
  153. if (parentItem is Folder)
  154. {
  155. query.AncestorIds = new[] { new Guid(parentId) };
  156. }
  157. else
  158. {
  159. query.ItemIds = new[] { new Guid(parentId) };
  160. }
  161. }
  162. // Studios
  163. if (!string.IsNullOrEmpty(studios))
  164. {
  165. query.StudioIds = studios.Split('|')
  166. .Select(i =>
  167. {
  168. try
  169. {
  170. return _libraryManager.GetStudio(i);
  171. }
  172. catch
  173. {
  174. return null;
  175. }
  176. }).Where(i => i != null)
  177. .Select(i => i!.Id)
  178. .ToArray();
  179. }
  180. foreach (var filter in RequestHelpers.GetFilters(filters))
  181. {
  182. switch (filter)
  183. {
  184. case ItemFilter.Dislikes:
  185. query.IsLiked = false;
  186. break;
  187. case ItemFilter.IsFavorite:
  188. query.IsFavorite = true;
  189. break;
  190. case ItemFilter.IsFavoriteOrLikes:
  191. query.IsFavoriteOrLiked = true;
  192. break;
  193. case ItemFilter.IsFolder:
  194. query.IsFolder = true;
  195. break;
  196. case ItemFilter.IsNotFolder:
  197. query.IsFolder = false;
  198. break;
  199. case ItemFilter.IsPlayed:
  200. query.IsPlayed = true;
  201. break;
  202. case ItemFilter.IsResumable:
  203. query.IsResumable = true;
  204. break;
  205. case ItemFilter.IsUnplayed:
  206. query.IsPlayed = false;
  207. break;
  208. case ItemFilter.Likes:
  209. query.IsLiked = true;
  210. break;
  211. }
  212. }
  213. var result = new QueryResult<(BaseItem, ItemCounts)>();
  214. var dtos = result.Items.Select(i =>
  215. {
  216. var (baseItem, counts) = i;
  217. var dto = _dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  218. if (!string.IsNullOrWhiteSpace(includeItemTypes))
  219. {
  220. dto.ChildCount = counts.ItemCount;
  221. dto.ProgramCount = counts.ProgramCount;
  222. dto.SeriesCount = counts.SeriesCount;
  223. dto.EpisodeCount = counts.EpisodeCount;
  224. dto.MovieCount = counts.MovieCount;
  225. dto.TrailerCount = counts.TrailerCount;
  226. dto.AlbumCount = counts.AlbumCount;
  227. dto.SongCount = counts.SongCount;
  228. dto.ArtistCount = counts.ArtistCount;
  229. }
  230. return dto;
  231. });
  232. return new QueryResult<BaseItemDto>
  233. {
  234. Items = dtos.ToArray(),
  235. TotalRecordCount = result.TotalRecordCount
  236. };
  237. }
  238. /// <summary>
  239. /// Get person by name.
  240. /// </summary>
  241. /// <param name="name">Person name.</param>
  242. /// <param name="userId">Optional. Filter by user id, and attach user data.</param>
  243. /// <response code="200">Person returned.</response>
  244. /// <response code="404">Person not found.</response>
  245. /// <returns>An <see cref="OkResult"/> containing the person on success,
  246. /// or a <see cref="NotFoundResult"/> if person not found.</returns>
  247. [HttpGet("{name}")]
  248. [ProducesResponseType(StatusCodes.Status200OK)]
  249. [ProducesResponseType(StatusCodes.Status404NotFound)]
  250. public ActionResult<BaseItemDto> GetPerson([FromRoute] string name, [FromQuery] Guid? userId)
  251. {
  252. var dtoOptions = new DtoOptions()
  253. .AddClientFields(Request);
  254. var item = _libraryManager.GetPerson(name);
  255. if (item == null)
  256. {
  257. return NotFound();
  258. }
  259. if (userId.HasValue && !userId.Equals(Guid.Empty))
  260. {
  261. var user = _userManager.GetUserById(userId.Value);
  262. return _dtoService.GetBaseItemDto(item, dtoOptions, user);
  263. }
  264. return _dtoService.GetBaseItemDto(item, dtoOptions);
  265. }
  266. }
  267. }