Kavita/API/Controllers/ScrobblingController.cs
Joe Milazzo 979508047c
v0.7.8 - New Filtering System (#2260)
Co-authored-by: JeanPaulDOT <jp.houssier@gmail.com>
Co-authored-by: Francois Wilhelmy <ice_mouton@hotmail.com>
Co-authored-by: Gazy Mahomar <gmahomarf@gmail.com>
Co-authored-by: Stijn <stijn.biemans@gmail.com>
Co-authored-by: 無情天 <kofzhanganguo@126.com>
Co-authored-by: Havokdan <havokdan@yahoo.com.br>
Co-authored-by: Andre <andruecha32@gmail.com>
Co-authored-by: Mateusz <mateuszvx8.96@gmail.com>
Co-authored-by: Antonio Sanchez Castellón <angelfx19@gmail.com>
Co-authored-by: Duarte Silva <smallflake@protonmail.com>
Co-authored-by: LeeWan1210 <dldhks456@live.com>
Co-authored-by: aleixcox <18121624@qq.com>
Co-authored-by: Tomas Battistini <tomas.battistini@gmail.com>
Co-authored-by: mareczek82 <marek.posiadala@gmail.com>
Co-authored-by: Hans Kalisvaart <hans.kalisvaart@gmail.com>
Co-authored-by: majora2007 <kavitareader@gmail.com>
Co-authored-by: afermar <adrian.fm@protonmail.com>
Co-authored-by: oxygen44k <iiccpp@outlook.com>
Co-authored-by: Weblate (bot) <hosted@weblate.org>
Co-authored-by: Hadrien b <hadrien.1997@gmail.com>
Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
Co-authored-by: Andre Smith <Hobogrammer@users.noreply.github.com>
Co-authored-by: Safu Wan <safu@yahoo.com>
Co-authored-by: sibeck <sibeck.clown@gmail.com>
Co-authored-by: Florestano Pepe <florestano.pepe@gmail.com>
Co-authored-by: 书签 <shuqian.emu@gmail.com>
Co-authored-by: Stéphane Dupont <aleistor@gmail.com>
Co-authored-by: gallegonovato <fran-carro@hotmail.es>
Co-authored-by: AlienHack <the4got10@windowslive.com>
Co-authored-by: 周書丞 <tmrsm_chan@hotmail.com>
Co-authored-by: Andre Smith <andrepsmithjr@gmail.com>
Co-authored-by: xe1st <dnzkckali@gmail.com>
Co-authored-by: Jiří Heger <jiri.heger@gmail.com>
Co-authored-by: DR <weblate-kavita.snowflake668@slmail.me>
Co-authored-by: Mathieu Ares <matguitarist@gmail.com>
Co-authored-by: Stavros Kois <47820033+stavros-k@users.noreply.github.com>
Co-authored-by: Gazy Mahomar <gmahomarf@users.noreply.github.com>
Co-authored-by: Elias Jakob <elias.jakob100@gmail.com>
Co-authored-by: Christian Zanon <chri8431@libero.it>
Co-authored-by: Eryk Michalak <gnu.ewm@protonmail.com>
Co-authored-by: Hoshino0881118 <hoshino0881118@gmail.com>
2023-09-03 12:31:50 -07:00

210 lines
7.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Data.Repositories;
using API.DTOs.Account;
using API.DTOs.Scrobbling;
using API.Entities.Scrobble;
using API.Extensions;
using API.Helpers;
using API.Helpers.Builders;
using API.Services;
using API.Services.Plus;
using Hangfire;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace API.Controllers;
public class ScrobblingController : BaseApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly IScrobblingService _scrobblingService;
private readonly ILogger<ScrobblingController> _logger;
private readonly ILocalizationService _localizationService;
public ScrobblingController(IUnitOfWork unitOfWork, IScrobblingService scrobblingService,
ILogger<ScrobblingController> logger, ILocalizationService localizationService)
{
_unitOfWork = unitOfWork;
_scrobblingService = scrobblingService;
_logger = logger;
_localizationService = localizationService;
}
[HttpGet("anilist-token")]
public async Task<ActionResult> GetAniListToken()
{
// Validate the license
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
if (user == null) return Unauthorized();
return Ok(user.AniListAccessToken);
}
[HttpPost("update-anilist-token")]
public async Task<ActionResult> UpdateAniListToken(AniListUpdateDto dto)
{
// Validate the license
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
if (user == null) return Unauthorized();
var isNewToken = string.IsNullOrEmpty(user.AniListAccessToken);
user.AniListAccessToken = dto.Token;
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
if (isNewToken)
{
BackgroundJob.Enqueue(() => _scrobblingService.CreateEventsFromExistingHistory(user.Id));
}
return Ok();
}
[HttpGet("token-expired")]
public async Task<ActionResult<bool>> HasTokenExpired(ScrobbleProvider provider)
{
return Ok(await _scrobblingService.HasTokenExpired(User.GetUserId(), provider));
}
/// <summary>
/// Returns all scrobbling errors for the instance
/// </summary>
/// <remarks>Requires admin</remarks>
/// <returns></returns>
[Authorize(Policy = "RequireAdminRole")]
[HttpGet("scrobble-errors")]
public async Task<ActionResult<IEnumerable<ScrobbleErrorDto>>> GetScrobbleErrors()
{
return Ok(await _unitOfWork.ScrobbleRepository.GetScrobbleErrors());
}
/// <summary>
/// Clears the scrobbling errors table
/// </summary>
/// <returns></returns>
[Authorize(Policy = "RequireAdminRole")]
[HttpPost("clear-errors")]
public async Task<ActionResult> ClearScrobbleErrors()
{
await _unitOfWork.ScrobbleRepository.ClearScrobbleErrors();
return Ok();
}
/// <summary>
/// Returns the scrobbling history for the user
/// </summary>
/// <remarks>User must have a valid license</remarks>
/// <returns></returns>
[HttpPost("scrobble-events")]
public async Task<ActionResult<PagedList<ScrobbleEventDto>>> GetScrobblingEvents([FromQuery] UserParams pagination, [FromBody] ScrobbleEventFilter filter)
{
pagination ??= UserParams.Default;
var events = await _unitOfWork.ScrobbleRepository.GetUserEvents(User.GetUserId(), filter, pagination);
Response.AddPaginationHeader(events.CurrentPage, events.PageSize, events.TotalCount, events.TotalPages);
return Ok(events);
}
/// <summary>
/// Returns all scrobble holds for the current user
/// </summary>
/// <returns></returns>
[HttpGet("holds")]
public async Task<ActionResult<IEnumerable<ScrobbleHoldDto>>> GetScrobbleHolds()
{
return Ok(await _unitOfWork.UserRepository.GetHolds(User.GetUserId()));
}
/// <summary>
/// If there is an active hold on the series
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpGet("has-hold")]
public async Task<ActionResult<bool>> HasHold(int seriesId)
{
return Ok(await _unitOfWork.UserRepository.HasHoldOnSeries(User.GetUserId(), seriesId));
}
/// <summary>
/// Does the library the series is in allow scrobbling?
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpGet("library-allows-scrobbling")]
public async Task<ActionResult<bool>> LibraryAllowsScrobbling(int seriesId)
{
return Ok(await _unitOfWork.LibraryRepository.GetAllowsScrobblingBySeriesId(seriesId));
}
/// <summary>
/// Adds a hold against the Series for user's scrobbling
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpPost("add-hold")]
public async Task<ActionResult> AddHold(int seriesId)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ScrobbleHolds);
if (user == null) return Unauthorized();
if (user.ScrobbleHolds.Any(s => s.SeriesId == seriesId))
return Ok(await _localizationService.Translate(User.GetUserId(), "nothing-to-do"));
var seriesHold = new ScrobbleHoldBuilder().WithSeriesId(seriesId).Build();
user.ScrobbleHolds.Add(seriesHold);
_unitOfWork.UserRepository.Update(user);
try
{
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok();
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
// Reload the entity from the database
await entry.ReloadAsync();
}
// Retry the update
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok();
}
catch (Exception ex)
{
// Handle other exceptions or log the error
_logger.LogError(ex, "An error occurred while adding the hold");
return StatusCode(StatusCodes.Status500InternalServerError,
await _localizationService.Translate(User.GetUserId(), "nothing-to-do"));
}
}
/// <summary>
/// Adds a hold against the Series for user's scrobbling
/// </summary>
/// <param name="seriesId"></param>
/// <returns></returns>
[HttpDelete("remove-hold")]
public async Task<ActionResult> RemoveHold(int seriesId)
{
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId(), AppUserIncludes.ScrobbleHolds);
if (user == null) return Unauthorized();
user.ScrobbleHolds = user.ScrobbleHolds.Where(h => h.SeriesId != seriesId).ToList();
_unitOfWork.UserRepository.Update(user);
await _unitOfWork.CommitAsync();
return Ok();
}
}