2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Linq;
|
2020-05-20 17:07:53 +00:00
|
|
|
using Jellyfin.Data.Entities;
|
2020-07-01 01:44:41 +00:00
|
|
|
using Jellyfin.Data.Enums;
|
2019-11-17 22:05:39 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.Dto;
|
2017-04-15 19:45:33 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2017-04-15 19:45:33 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Model.Dto;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2017-04-15 19:45:33 +00:00
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using MediaBrowser.Model.Services;
|
2019-11-17 22:05:39 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2017-04-15 19:45:33 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
{
|
|
|
|
[Route("/Users/{UserId}/Suggestions", "GET", Summary = "Gets items based on a query.")]
|
2018-09-12 17:26:21 +00:00
|
|
|
public class GetSuggestedItems : IReturn<QueryResult<BaseItemDto>>
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
|
|
|
public string MediaType { get; set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-04-15 19:45:33 +00:00
|
|
|
public string Type { get; set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Guid UserId { get; set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-04-15 19:45:33 +00:00
|
|
|
public bool EnableTotalRecordCount { get; set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-04-15 19:45:33 +00:00
|
|
|
public int? StartIndex { get; set; }
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-04-15 19:45:33 +00:00
|
|
|
public int? Limit { get; set; }
|
|
|
|
|
|
|
|
public string[] GetMediaTypes()
|
|
|
|
{
|
|
|
|
return (MediaType ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string[] GetIncludeItemTypes()
|
|
|
|
{
|
|
|
|
return (Type ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SuggestionsService : BaseApiService
|
|
|
|
{
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
public SuggestionsService(
|
|
|
|
ILogger<SuggestionsService> logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
|
|
|
IDtoService dtoService,
|
|
|
|
IAuthorizationContext authContext,
|
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager)
|
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
|
|
|
_dtoService = dtoService;
|
|
|
|
_authContext = authContext;
|
|
|
|
_userManager = userManager;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
}
|
|
|
|
|
2017-08-28 00:33:05 +00:00
|
|
|
public object Get(GetSuggestedItems request)
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetResultItems(request);
|
2017-04-15 19:45:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 00:33:05 +00:00
|
|
|
private QueryResult<BaseItemDto> GetResultItems(GetSuggestedItems request)
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var user = !request.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(request.UserId) : null;
|
2017-04-15 19:45:33 +00:00
|
|
|
|
|
|
|
var dtoOptions = GetDtoOptions(_authContext, request);
|
|
|
|
var result = GetItems(request, user, dtoOptions);
|
|
|
|
|
2017-08-28 00:33:05 +00:00
|
|
|
var dtoList = _dtoService.GetBaseItemDtos(result.Items, dtoOptions, user);
|
2017-04-15 19:45:33 +00:00
|
|
|
|
|
|
|
return new QueryResult<BaseItemDto>
|
|
|
|
{
|
|
|
|
TotalRecordCount = result.TotalRecordCount,
|
2017-08-19 19:43:35 +00:00
|
|
|
Items = dtoList
|
2017-04-15 19:45:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:07:53 +00:00
|
|
|
private QueryResult<BaseItem> GetItems(GetSuggestedItems request, User user, DtoOptions dtoOptions)
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
2017-04-17 01:45:44 +00:00
|
|
|
return _libraryManager.GetItemsResult(new InternalItemsQuery(user)
|
2017-04-15 19:45:33 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
OrderBy = new[] { ItemSortBy.Random }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Descending)).ToArray(),
|
2017-04-15 19:45:33 +00:00
|
|
|
MediaTypes = request.GetMediaTypes(),
|
|
|
|
IncludeItemTypes = request.GetIncludeItemTypes(),
|
|
|
|
IsVirtualItem = false,
|
|
|
|
StartIndex = request.StartIndex,
|
|
|
|
Limit = request.Limit,
|
2017-04-17 01:45:44 +00:00
|
|
|
DtoOptions = dtoOptions,
|
|
|
|
EnableTotalRecordCount = request.EnableTotalRecordCount,
|
2017-04-17 19:01:16 +00:00
|
|
|
Recursive = true
|
2017-04-17 01:45:44 +00:00
|
|
|
});
|
2017-04-15 19:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|