| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using Jellyfin.Api.Helpers; |
| | | 4 | | using Jellyfin.Api.ModelBinders; |
| | | 5 | | using Jellyfin.Data.Enums; |
| | | 6 | | using Jellyfin.Extensions; |
| | | 7 | | using MediaBrowser.Controller.Dto; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using MediaBrowser.Model.Dto; |
| | | 11 | | using MediaBrowser.Model.Querying; |
| | | 12 | | using Microsoft.AspNetCore.Authorization; |
| | | 13 | | using Microsoft.AspNetCore.Http; |
| | | 14 | | using Microsoft.AspNetCore.Mvc; |
| | | 15 | | |
| | | 16 | | namespace Jellyfin.Api.Controllers; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Filters controller. |
| | | 20 | | /// </summary> |
| | | 21 | | [Route("")] |
| | | 22 | | [Authorize] |
| | | 23 | | public class FilterController : BaseJellyfinApiController |
| | | 24 | | { |
| | | 25 | | private readonly ILibraryManager _libraryManager; |
| | | 26 | | private readonly IUserManager _userManager; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="FilterController"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 32 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | 0 | 33 | | public FilterController(ILibraryManager libraryManager, IUserManager userManager) |
| | | 34 | | { |
| | 0 | 35 | | _libraryManager = libraryManager; |
| | 0 | 36 | | _userManager = userManager; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets legacy query filters. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="userId">Optional. User id.</param> |
| | | 43 | | /// <param name="parentId">Optional. Parent id.</param> |
| | | 44 | | /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows |
| | | 45 | | /// <param name="mediaTypes">Optional. Filter by MediaType. Allows multiple, comma delimited.</param> |
| | | 46 | | /// <response code="200">Legacy filters retrieved.</response> |
| | | 47 | | /// <returns>Legacy query filters.</returns> |
| | | 48 | | [HttpGet("Items/Filters")] |
| | | 49 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 50 | | public ActionResult<QueryFiltersLegacy> GetQueryFiltersLegacy( |
| | | 51 | | [FromQuery] Guid? userId, |
| | | 52 | | [FromQuery] Guid? parentId, |
| | | 53 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes, |
| | | 54 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] MediaType[] mediaTypes) |
| | | 55 | | { |
| | 0 | 56 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 57 | | var user = userId.IsNullOrEmpty() |
| | 0 | 58 | | ? null |
| | 0 | 59 | | : _userManager.GetUserById(userId.Value); |
| | | 60 | | |
| | 0 | 61 | | BaseItem? item = null; |
| | 0 | 62 | | if (includeItemTypes.Length != 1 |
| | 0 | 63 | | || !(includeItemTypes[0] == BaseItemKind.Trailer |
| | 0 | 64 | | || includeItemTypes[0] == BaseItemKind.Program)) |
| | | 65 | | { |
| | 0 | 66 | | item = _libraryManager.GetParentItem(parentId, user?.Id); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | if (item is not Folder folder) |
| | | 70 | | { |
| | 0 | 71 | | return new QueryFiltersLegacy(); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | var query = new InternalItemsQuery(user) |
| | 0 | 75 | | { |
| | 0 | 76 | | MediaTypes = mediaTypes, |
| | 0 | 77 | | IncludeItemTypes = includeItemTypes, |
| | 0 | 78 | | Recursive = true, |
| | 0 | 79 | | EnableTotalRecordCount = false, |
| | 0 | 80 | | AncestorIds = [folder.Id], |
| | 0 | 81 | | DtoOptions = new DtoOptions |
| | 0 | 82 | | { |
| | 0 | 83 | | Fields = [], |
| | 0 | 84 | | EnableImages = false, |
| | 0 | 85 | | EnableUserData = false |
| | 0 | 86 | | } |
| | 0 | 87 | | }; |
| | | 88 | | |
| | 0 | 89 | | return _libraryManager.GetQueryFiltersLegacy(query); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets query filters. |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="userId">Optional. User id.</param> |
| | | 96 | | /// <param name="parentId">Optional. Specify this to localize the search to a specific item or folder. Omit to use t |
| | | 97 | | /// <param name="includeItemTypes">Optional. If specified, results will be filtered based on item type. This allows |
| | | 98 | | /// <param name="isAiring">Optional. Is item airing.</param> |
| | | 99 | | /// <param name="isMovie">Optional. Is item movie.</param> |
| | | 100 | | /// <param name="isSports">Optional. Is item sports.</param> |
| | | 101 | | /// <param name="isKids">Optional. Is item kids.</param> |
| | | 102 | | /// <param name="isNews">Optional. Is item news.</param> |
| | | 103 | | /// <param name="isSeries">Optional. Is item series.</param> |
| | | 104 | | /// <param name="recursive">Optional. Search recursive.</param> |
| | | 105 | | /// <response code="200">Filters retrieved.</response> |
| | | 106 | | /// <returns>Query filters.</returns> |
| | | 107 | | [HttpGet("Items/Filters2")] |
| | | 108 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 109 | | public ActionResult<QueryFilters> GetQueryFilters( |
| | | 110 | | [FromQuery] Guid? userId, |
| | | 111 | | [FromQuery] Guid? parentId, |
| | | 112 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] BaseItemKind[] includeItemTypes, |
| | | 113 | | [FromQuery] bool? isAiring, |
| | | 114 | | [FromQuery] bool? isMovie, |
| | | 115 | | [FromQuery] bool? isSports, |
| | | 116 | | [FromQuery] bool? isKids, |
| | | 117 | | [FromQuery] bool? isNews, |
| | | 118 | | [FromQuery] bool? isSeries, |
| | | 119 | | [FromQuery] bool? recursive) |
| | | 120 | | { |
| | 0 | 121 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 122 | | var user = userId.IsNullOrEmpty() |
| | 0 | 123 | | ? null |
| | 0 | 124 | | : _userManager.GetUserById(userId.Value); |
| | | 125 | | |
| | 0 | 126 | | BaseItem? parentItem = null; |
| | 0 | 127 | | if (includeItemTypes.Length == 1 |
| | 0 | 128 | | && (includeItemTypes[0] == BaseItemKind.Trailer |
| | 0 | 129 | | || includeItemTypes[0] == BaseItemKind.Program)) |
| | | 130 | | { |
| | 0 | 131 | | parentItem = null; |
| | | 132 | | } |
| | 0 | 133 | | else if (parentId.HasValue) |
| | | 134 | | { |
| | 0 | 135 | | parentItem = _libraryManager.GetItemById<BaseItem>(parentId.Value); |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | var filters = new QueryFilters(); |
| | 0 | 139 | | var genreQuery = new InternalItemsQuery(user) |
| | 0 | 140 | | { |
| | 0 | 141 | | IncludeItemTypes = includeItemTypes, |
| | 0 | 142 | | DtoOptions = new DtoOptions |
| | 0 | 143 | | { |
| | 0 | 144 | | Fields = Array.Empty<ItemFields>(), |
| | 0 | 145 | | EnableImages = false, |
| | 0 | 146 | | EnableUserData = false |
| | 0 | 147 | | }, |
| | 0 | 148 | | IsAiring = isAiring, |
| | 0 | 149 | | IsMovie = isMovie, |
| | 0 | 150 | | IsSports = isSports, |
| | 0 | 151 | | IsKids = isKids, |
| | 0 | 152 | | IsNews = isNews, |
| | 0 | 153 | | IsSeries = isSeries |
| | 0 | 154 | | }; |
| | | 155 | | |
| | 0 | 156 | | if ((recursive ?? true) || parentItem is UserView || parentItem is ICollectionFolder) |
| | | 157 | | { |
| | 0 | 158 | | genreQuery.AncestorIds = parentItem is null ? Array.Empty<Guid>() : new[] { parentItem.Id }; |
| | | 159 | | } |
| | | 160 | | else |
| | | 161 | | { |
| | 0 | 162 | | genreQuery.Parent = parentItem; |
| | | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | if (includeItemTypes.Length == 1 |
| | 0 | 166 | | && (includeItemTypes[0] == BaseItemKind.MusicAlbum |
| | 0 | 167 | | || includeItemTypes[0] == BaseItemKind.MusicVideo |
| | 0 | 168 | | || includeItemTypes[0] == BaseItemKind.MusicArtist |
| | 0 | 169 | | || includeItemTypes[0] == BaseItemKind.Audio)) |
| | | 170 | | { |
| | 0 | 171 | | filters.Genres = _libraryManager.GetMusicGenres(genreQuery).Items.Select(i => new NameGuidPair |
| | 0 | 172 | | { |
| | 0 | 173 | | Name = i.Item.Name, |
| | 0 | 174 | | Id = i.Item.Id |
| | 0 | 175 | | }).ToArray(); |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | 0 | 179 | | filters.Genres = _libraryManager.GetGenres(genreQuery).Items.Select(i => new NameGuidPair |
| | 0 | 180 | | { |
| | 0 | 181 | | Name = i.Item.Name, |
| | 0 | 182 | | Id = i.Item.Id |
| | 0 | 183 | | }).ToArray(); |
| | | 184 | | } |
| | | 185 | | |
| | 0 | 186 | | return filters; |
| | | 187 | | } |
| | | 188 | | } |