using System;
using System.Linq;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
///
/// The suggestions controller.
///
[Route("")]
public class SuggestionsController : BaseJellyfinApiController
{
private readonly IDtoService _dtoService;
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
/// Instance of the interface.
/// Instance of the interface.
public SuggestionsController(
IDtoService dtoService,
IUserManager userManager,
ILibraryManager libraryManager)
{
_dtoService = dtoService;
_userManager = userManager;
_libraryManager = libraryManager;
}
///
/// Gets suggestions.
///
/// The user id.
/// The media types.
/// The type.
/// Optional. The start index.
/// Optional. The limit.
/// Whether to enable the total record count.
/// Suggestions returned.
/// A with the suggestions.
[HttpGet("Users/{userId}/Suggestions")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult> GetSuggestions(
[FromRoute][Required] Guid userId,
[FromQuery] string? mediaType,
[FromQuery] string? type,
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery] bool enableTotalRecordCount = false)
{
var user = !userId.Equals(Guid.Empty) ? _userManager.GetUserById(userId) : null;
var dtoOptions = new DtoOptions().AddClientFields(Request);
var result = _libraryManager.GetItemsResult(new InternalItemsQuery(user)
{
OrderBy = new[] { ItemSortBy.Random }.Select(i => new ValueTuple(i, SortOrder.Descending)).ToArray(),
MediaTypes = RequestHelpers.Split(mediaType!, ',', true),
IncludeItemTypes = RequestHelpers.Split(type!, ',', true),
IsVirtualItem = false,
StartIndex = startIndex,
Limit = limit,
DtoOptions = dtoOptions,
EnableTotalRecordCount = enableTotalRecordCount,
Recursive = true
});
var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
return new QueryResult
{
TotalRecordCount = result.TotalRecordCount,
Items = dtoList
};
}
}
}