Customized Scheduler + Saved Kavita+ Details (#2644)

This commit is contained in:
Joe Milazzo 2024-01-22 12:10:57 -06:00 committed by GitHub
parent 2092e120c3
commit ad74871623
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
76 changed files with 6076 additions and 3370 deletions

View file

@ -1,19 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using System.Threading.Tasks;
using API.Data;
using API.DTOs;
using API.DTOs.Recommendation;
using API.Extensions;
using API.Helpers;
using API.Services;
using API.Services.Plus;
using EasyCaching.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
namespace API.Controllers;
@ -22,56 +12,14 @@ namespace API.Controllers;
public class RecommendedController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IRecommendationService _recommendationService;
private readonly ILicenseService _licenseService;
private readonly ILocalizationService _localizationService;
private readonly IEasyCachingProvider _cacheProvider;
public const string CacheKey = "recommendation_";
public RecommendedController(IUnitOfWork unitOfWork, IRecommendationService recommendationService,
ILicenseService licenseService, IEasyCachingProviderFactory cachingProviderFactory,
ILocalizationService localizationService)
public RecommendedController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_recommendationService = recommendationService;
_licenseService = licenseService;
_localizationService = localizationService;
_cacheProvider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.KavitaPlusRecommendations);
}
/// <summary>
/// For Kavita+ users, this will return recommendations on the server.
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpGet("recommendations")]
[ResponseCache(CacheProfileName = ResponseCacheProfiles.KavitaPlus, VaryByQueryKeys = new []{"seriesId"})]
public async Task<ActionResult<RecommendationDto>> GetRecommendations(int seriesId)
{
var userId = User.GetUserId();
if (!await _licenseService.HasActiveLicense())
{
return Ok(new RecommendationDto());
}
if (!await _unitOfWork.UserRepository.HasAccessToSeries(userId, seriesId))
{
return BadRequest(await _localizationService.Translate(User.GetUserId(), "series-restricted"));
}
var cacheKey = $"{CacheKey}-{seriesId}-{userId}";
var results = await _cacheProvider.GetAsync<RecommendationDto>(cacheKey);
if (results.HasValue)
{
return Ok(results.Value);
}
var ret = await _recommendationService.GetRecommendationsForSeries(userId, seriesId);
await _cacheProvider.SetAsync(cacheKey, ret, TimeSpan.FromHours(10));
return Ok(ret);
}
/// <summary>
/// Quick Reads are series that should be readable in less than 10 in total and are not Ongoing in release.
/// </summary>
@ -79,7 +27,7 @@ public class RecommendedController : BaseApiController
/// <param name="userParams">Pagination</param>
/// <returns></returns>
[HttpGet("quick-reads")]
public async Task<ActionResult<PagedList<SeriesDto>>> GetQuickReads(int libraryId, [FromQuery] UserParams userParams)
public async Task<ActionResult<PagedList<SeriesDto>>> GetQuickReads(int libraryId, [FromQuery] UserParams? userParams)
{
userParams ??= UserParams.Default;
var series = await _unitOfWork.SeriesRepository.GetQuickReads(User.GetUserId(), libraryId, userParams);
@ -95,7 +43,7 @@ public class RecommendedController : BaseApiController
/// <param name="userParams"></param>
/// <returns></returns>
[HttpGet("quick-catchup-reads")]
public async Task<ActionResult<PagedList<SeriesDto>>> GetQuickCatchupReads(int libraryId, [FromQuery] UserParams userParams)
public async Task<ActionResult<PagedList<SeriesDto>>> GetQuickCatchupReads(int libraryId, [FromQuery] UserParams? userParams)
{
userParams ??= UserParams.Default;
var series = await _unitOfWork.SeriesRepository.GetQuickCatchupReads(User.GetUserId(), libraryId, userParams);
@ -111,7 +59,7 @@ public class RecommendedController : BaseApiController
/// <param name="userParams">Pagination</param>
/// <returns></returns>
[HttpGet("highly-rated")]
public async Task<ActionResult<PagedList<SeriesDto>>> GetHighlyRated(int libraryId, [FromQuery] UserParams userParams)
public async Task<ActionResult<PagedList<SeriesDto>>> GetHighlyRated(int libraryId, [FromQuery] UserParams? userParams)
{
var userId = User.GetUserId();
userParams ??= UserParams.Default;
@ -129,7 +77,7 @@ public class RecommendedController : BaseApiController
/// <param name="userParams">Pagination</param>
/// <returns></returns>
[HttpGet("more-in")]
public async Task<ActionResult<PagedList<SeriesDto>>> GetMoreIn(int libraryId, int genreId, [FromQuery] UserParams userParams)
public async Task<ActionResult<PagedList<SeriesDto>>> GetMoreIn(int libraryId, int genreId, [FromQuery] UserParams? userParams)
{
var userId = User.GetUserId();
@ -148,7 +96,7 @@ public class RecommendedController : BaseApiController
/// <param name="userParams">Pagination</param>
/// <returns></returns>
[HttpGet("rediscover")]
public async Task<ActionResult<PagedList<SeriesDto>>> GetRediscover(int libraryId, [FromQuery] UserParams userParams)
public async Task<ActionResult<PagedList<SeriesDto>>> GetRediscover(int libraryId, [FromQuery] UserParams? userParams)
{
userParams ??= UserParams.Default;
var series = await _unitOfWork.SeriesRepository.GetRediscover(User.GetUserId(), libraryId, userParams);