jellyfin/Jellyfin.Api/Controllers/FilterController.cs

215 lines
7.8 KiB
C#
Raw Normal View History

2020-11-21 13:26:03 +00:00
using System;
2020-06-08 04:04:59 +00:00
using System.Linq;
2020-11-17 03:37:43 +00:00
using Jellyfin.Api.ModelBinders;
2021-02-11 00:09:23 +00:00
using Jellyfin.Data.Enums;
2020-06-08 04:04:59 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Filters controller.
/// </summary>
[Route("")]
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
public class FilterController : BaseJellyfinApiController
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
/// <summary>
/// Initializes a new instance of the <see cref="FilterController"/> class.
/// </summary>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
public FilterController(ILibraryManager libraryManager, IUserManager userManager)
{
_libraryManager = libraryManager;
_userManager = userManager;
}
2020-06-08 04:04:59 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Gets legacy query filters.
2020-06-08 04:04:59 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="userId">Optional. User id.</param>
/// <param name="parentId">Optional. Parent id.</param>
/// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
/// <param name="mediaTypes">Optional. Filter by MediaType. Allows multiple, comma delimited.</param>
/// <response code="200">Legacy filters retrieved.</response>
/// <returns>Legacy query filters.</returns>
[HttpGet("Items/Filters")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QueryFiltersLegacy> GetQueryFiltersLegacy(
[FromQuery] Guid? userId,
[FromQuery] Guid? parentId,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] string[] mediaTypes)
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
BaseItem? item = null;
if (includeItemTypes.Length != 1
|| !(includeItemTypes[0] == BaseItemKind.BoxSet
|| includeItemTypes[0] == BaseItemKind.Playlist
|| includeItemTypes[0] == BaseItemKind.Trailer
|| includeItemTypes[0] == BaseItemKind.Program))
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
item = _libraryManager.GetParentItem(parentId, user?.Id);
2020-06-08 04:04:59 +00:00
}
2023-01-31 11:18:10 +00:00
var query = new InternalItemsQuery
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
User = user,
MediaTypes = mediaTypes,
IncludeItemTypes = includeItemTypes,
Recursive = true,
EnableTotalRecordCount = false,
DtoOptions = new DtoOptions
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
Fields = new[] { ItemFields.Genres, ItemFields.Tags },
EnableImages = false,
EnableUserData = false
2020-12-01 19:10:11 +00:00
}
2023-01-31 11:18:10 +00:00
};
2020-06-08 04:04:59 +00:00
2023-01-31 11:18:10 +00:00
if (item is not Folder folder)
{
return new QueryFiltersLegacy();
2020-06-08 04:04:59 +00:00
}
2023-01-31 11:18:10 +00:00
var itemList = folder.GetItemList(query);
return new QueryFiltersLegacy
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
Years = itemList.Select(i => i.ProductionYear ?? -1)
.Where(i => i > 0)
.Distinct()
.Order()
.ToArray(),
Genres = itemList.SelectMany(i => i.Genres)
.DistinctNames()
.Order()
.ToArray(),
Tags = itemList
.SelectMany(i => i.Tags)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Order()
.ToArray(),
OfficialRatings = itemList
.Select(i => i.OfficialRating)
.Where(i => !string.IsNullOrWhiteSpace(i))
.Distinct(StringComparer.OrdinalIgnoreCase)
.Order()
.ToArray()
};
}
2020-06-08 04:04:59 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets query filters.
/// </summary>
/// <param name="userId">Optional. User id.</param>
/// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
/// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimited.</param>
/// <param name="isAiring">Optional. Is item airing.</param>
/// <param name="isMovie">Optional. Is item movie.</param>
/// <param name="isSports">Optional. Is item sports.</param>
/// <param name="isKids">Optional. Is item kids.</param>
/// <param name="isNews">Optional. Is item news.</param>
/// <param name="isSeries">Optional. Is item series.</param>
/// <param name="recursive">Optional. Search recursive.</param>
/// <response code="200">Filters retrieved.</response>
/// <returns>Query filters.</returns>
[HttpGet("Items/Filters2")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QueryFilters> GetQueryFilters(
[FromQuery] Guid? userId,
[FromQuery] Guid? parentId,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
[FromQuery] bool? isAiring,
[FromQuery] bool? isMovie,
[FromQuery] bool? isSports,
[FromQuery] bool? isKids,
[FromQuery] bool? isNews,
[FromQuery] bool? isSeries,
[FromQuery] bool? recursive)
{
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
BaseItem? parentItem = null;
if (includeItemTypes.Length == 1
&& (includeItemTypes[0] == BaseItemKind.BoxSet
|| includeItemTypes[0] == BaseItemKind.Playlist
|| includeItemTypes[0] == BaseItemKind.Trailer
|| includeItemTypes[0] == BaseItemKind.Program))
{
parentItem = null;
}
else if (parentId.HasValue)
{
parentItem = _libraryManager.GetItemById(parentId.Value);
}
var filters = new QueryFilters();
var genreQuery = new InternalItemsQuery(user)
{
IncludeItemTypes = includeItemTypes,
DtoOptions = new DtoOptions
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
Fields = Array.Empty<ItemFields>(),
EnableImages = false,
EnableUserData = false
},
IsAiring = isAiring,
IsMovie = isMovie,
IsSports = isSports,
IsKids = isKids,
IsNews = isNews,
IsSeries = isSeries
};
if ((recursive ?? true) || parentItem is UserView || parentItem is ICollectionFolder)
{
genreQuery.AncestorIds = parentItem is null ? Array.Empty<Guid>() : new[] { parentItem.Id };
}
else
{
genreQuery.Parent = parentItem;
}
2020-06-08 04:04:59 +00:00
2023-01-31 11:18:10 +00:00
if (includeItemTypes.Length == 1
&& (includeItemTypes[0] == BaseItemKind.MusicAlbum
|| includeItemTypes[0] == BaseItemKind.MusicVideo
|| includeItemTypes[0] == BaseItemKind.MusicArtist
|| includeItemTypes[0] == BaseItemKind.Audio))
{
filters.Genres = _libraryManager.GetMusicGenres(genreQuery).Items.Select(i => new NameGuidPair
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
Name = i.Item.Name,
Id = i.Item.Id
}).ToArray();
}
else
{
filters.Genres = _libraryManager.GetGenres(genreQuery).Items.Select(i => new NameGuidPair
2020-06-08 04:04:59 +00:00
{
2023-01-31 11:18:10 +00:00
Name = i.Item.Name,
Id = i.Item.Id
}).ToArray();
2020-06-08 04:04:59 +00:00
}
2023-01-31 11:18:10 +00:00
return filters;
2020-06-08 04:04:59 +00:00
}
}