jellyfin-server/Jellyfin.Api/Controllers/InstantMixController.cs

362 lines
20 KiB
C#
Raw Normal View History

2020-11-21 13:26:03 +00:00
using System;
2020-06-26 11:42:21 +00:00
using System.Collections.Generic;
2020-08-06 14:17:45 +00:00
using System.ComponentModel.DataAnnotations;
2020-06-26 11:42:21 +00:00
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
2020-11-09 21:59:04 +00:00
using Jellyfin.Api.ModelBinders;
2020-06-26 11:42:21 +00:00
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Playlists;
using MediaBrowser.Model.Dto;
2020-10-09 23:52:39 +00:00
using MediaBrowser.Model.Entities;
2020-06-26 11:42:21 +00:00
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
2020-06-26 15:45:07 +00:00
using Microsoft.AspNetCore.Http;
2020-06-26 11:42:21 +00:00
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The instant mix controller.
/// </summary>
2020-08-04 18:48:53 +00:00
[Route("")]
2020-06-26 11:42:21 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
public class InstantMixController : BaseJellyfinApiController
{
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
private readonly ILibraryManager _libraryManager;
private readonly IMusicManager _musicManager;
/// <summary>
/// Initializes a new instance of the <see cref="InstantMixController"/> class.
/// </summary>
/// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param>
/// <param name="musicManager">Instance of the <see cref="IMusicManager"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public InstantMixController(
IUserManager userManager,
IDtoService dtoService,
IMusicManager musicManager,
ILibraryManager libraryManager)
{
_userManager = userManager;
_dtoService = dtoService;
_musicManager = musicManager;
_libraryManager = libraryManager;
}
/// <summary>
/// Creates an instant playlist based on a given song.
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-08-04 18:48:53 +00:00
[HttpGet("Songs/{id}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-26 11:42:21 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromSong(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid id,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var item = _libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
/// <summary>
2021-03-13 16:09:22 +00:00
/// Creates an instant playlist based on a given album.
2020-06-26 11:42:21 +00:00
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-08-04 18:48:53 +00:00
[HttpGet("Albums/{id}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-26 11:42:21 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromAlbum(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid id,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var album = _libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromItem(album, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
/// <summary>
2021-03-13 16:09:22 +00:00
/// Creates an instant playlist based on a given playlist.
2020-06-26 11:42:21 +00:00
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-08-04 18:48:53 +00:00
[HttpGet("Playlists/{id}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-26 11:42:21 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromPlaylist(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid id,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var playlist = (Playlist)_libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromItem(playlist, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
/// <summary>
2021-03-13 16:09:22 +00:00
/// Creates an instant playlist based on a given genre.
2020-06-26 11:42:21 +00:00
/// </summary>
/// <param name="name">The genre name.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-08-04 18:48:53 +00:00
[HttpGet("MusicGenres/{name}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2021-03-13 16:09:22 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromMusicGenreByName(
2020-09-08 00:45:06 +00:00
[FromRoute, Required] string name,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromGenres(new[] { name }, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
/// <summary>
2021-03-13 16:09:22 +00:00
/// Creates an instant playlist based on a given artist.
2020-06-26 11:42:21 +00:00
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-11-15 17:58:39 +00:00
[HttpGet("Artists/{id}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-26 11:42:21 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromArtists(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid id,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var item = _libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
/// <summary>
2021-03-13 16:09:22 +00:00
/// Creates an instant playlist based on a given item.
2020-06-26 11:42:21 +00:00
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
2020-10-09 23:35:08 +00:00
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
2020-06-26 11:42:21 +00:00
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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-06-26 15:45:07 +00:00
/// <response code="200">Instant playlist returned.</response>
2020-06-26 11:42:21 +00:00
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
2020-08-04 18:48:53 +00:00
[HttpGet("Items/{id}/InstantMix")]
2020-06-26 15:45:07 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-26 11:42:21 +00:00
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromItem(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid id,
[FromQuery] Guid? userId,
2020-06-26 11:42:21 +00:00
[FromQuery] int? limit,
2020-11-09 21:59:04 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
2020-06-26 11:42:21 +00:00
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
2020-11-09 21:53:23 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
2020-06-26 11:42:21 +00:00
{
var item = _libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2020-10-29 17:36:45 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
2020-06-26 11:42:21 +00:00
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2020-06-26 11:42:21 +00:00
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
}
2021-03-13 16:09:22 +00:00
/// <summary>
/// Creates an instant playlist based on a given artist.
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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>
/// <response code="200">Instant playlist returned.</response>
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
[HttpGet("Artists/InstantMix")]
[ProducesResponseType(StatusCodes.Status200OK)]
[Obsolete("Use GetInstantMixFromArtists")]
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromArtists2(
[FromQuery, Required] Guid id,
[FromQuery] Guid? userId,
[FromQuery] int? limit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
{
return GetInstantMixFromArtists(
id,
userId,
limit,
fields,
enableImages,
enableUserData,
imageTypeLimit,
enableImageTypes);
}
/// <summary>
/// Creates an instant playlist based on a given genre.
/// </summary>
/// <param name="id">The item id.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
/// <param name="enableImages">Optional. Include image information in output.</param>
/// <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>
/// <response code="200">Instant playlist returned.</response>
/// <returns>A <see cref="QueryResult{BaseItemDto}"/> with the playlist items.</returns>
[HttpGet("MusicGenres/InstantMix")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<QueryResult<BaseItemDto>> GetInstantMixFromMusicGenreById(
2021-03-13 16:09:22 +00:00
[FromQuery, Required] Guid id,
[FromQuery] Guid? userId,
[FromQuery] int? limit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
[FromQuery] bool? enableImages,
[FromQuery] bool? enableUserData,
[FromQuery] int? imageTypeLimit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes)
{
2021-07-12 23:20:07 +00:00
var item = _libraryManager.GetItemById(id);
var user = userId is null || userId.Value.Equals(default)
? null
: _userManager.GetUserById(userId.Value);
2021-07-12 23:20:07 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
.AddClientFields(Request)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
2021-07-12 23:20:07 +00:00
var items = _musicManager.GetInstantMixFromItem(item, user, dtoOptions);
return GetResult(items, user, limit, dtoOptions);
2021-03-13 16:09:22 +00:00
}
private QueryResult<BaseItemDto> GetResult(List<BaseItem> items, User? user, int? limit, DtoOptions dtoOptions)
2020-06-26 11:42:21 +00:00
{
var list = items;
2022-01-20 15:46:17 +00:00
var totalCount = list.Count;
2020-06-26 11:42:21 +00:00
2020-11-06 15:39:41 +00:00
if (limit.HasValue && limit < list.Count)
2020-06-26 11:42:21 +00:00
{
2020-11-06 15:15:30 +00:00
list = list.GetRange(0, limit.Value);
2020-06-26 11:42:21 +00:00
}
var returnList = _dtoService.GetBaseItemDtos(list, dtoOptions, user);
2022-01-20 15:46:17 +00:00
var result = new QueryResult<BaseItemDto>(
0,
totalCount,
returnList);
2020-06-26 11:42:21 +00:00
return result;
}
}
}