Scrobbling Stability (#3863)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
This commit is contained in:
parent
45e24aa311
commit
14a8f5c1e5
19 changed files with 1622 additions and 806 deletions
|
|
@ -254,7 +254,7 @@ public class ScrobblingController : BaseApiController
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a hold against the Series for user's scrobbling
|
||||
/// Remove a hold against the Series for user's scrobbling
|
||||
/// </summary>
|
||||
/// <param name="seriesId"></param>
|
||||
/// <returns></returns>
|
||||
|
|
@ -281,4 +281,18 @@ public class ScrobblingController : BaseApiController
|
|||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());
|
||||
return Ok(user is {HasRunScrobbleEventGeneration: true});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete the given scrobble events if they belong to that user
|
||||
/// </summary>
|
||||
/// <param name="eventIds"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("bulk-remove-events")]
|
||||
public async Task<ActionResult> BulkRemoveScrobbleEvents(IList<long> eventIds)
|
||||
{
|
||||
var events = await _unitOfWork.ScrobbleRepository.GetUserEvents(User.GetUserId(), eventIds);
|
||||
_unitOfWork.ScrobbleRepository.Remove(events);
|
||||
await _unitOfWork.CommitAsync();
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace API.DTOs.Scrobbling;
|
|||
|
||||
public sealed record ScrobbleEventDto
|
||||
{
|
||||
public long Id { get; init; }
|
||||
public string SeriesName { get; set; }
|
||||
public int SeriesId { get; set; }
|
||||
public int LibraryId { get; set; }
|
||||
|
|
|
|||
|
|
@ -8,5 +8,6 @@ public sealed record ScrobbleResponseDto
|
|||
{
|
||||
public bool Successful { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
public string? ExtraInformation {get; set;}
|
||||
public int RateLeft { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,8 +29,23 @@ public interface IScrobbleRepository
|
|||
Task<IList<ScrobbleError>> GetAllScrobbleErrorsForSeries(int seriesId);
|
||||
Task ClearScrobbleErrors();
|
||||
Task<bool> HasErrorForSeries(int seriesId);
|
||||
Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType);
|
||||
/// <summary>
|
||||
/// Get all events for a specific user and type
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="seriesId"></param>
|
||||
/// <param name="eventType"></param>
|
||||
/// <param name="isNotProcessed">If true, only returned not processed events</param>
|
||||
/// <returns></returns>
|
||||
Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType, bool isNotProcessed = false);
|
||||
Task<IEnumerable<ScrobbleEvent>> GetUserEventsForSeries(int userId, int seriesId);
|
||||
/// <summary>
|
||||
/// Return the events with given ids, when belonging to the passed user
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <param name="scrobbleEventIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<IList<ScrobbleEvent>> GetUserEvents(int userId, IList<long> scrobbleEventIds);
|
||||
Task<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination);
|
||||
Task<IList<ScrobbleEvent>> GetAllEventsForSeries(int seriesId);
|
||||
Task<IList<ScrobbleEvent>> GetAllEventsWithSeriesIds(IEnumerable<int> seriesIds);
|
||||
|
|
@ -146,22 +161,32 @@ public class ScrobbleRepository : IScrobbleRepository
|
|||
return await _context.ScrobbleError.AnyAsync(n => n.SeriesId == seriesId);
|
||||
}
|
||||
|
||||
public async Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType)
|
||||
public async Task<ScrobbleEvent?> GetEvent(int userId, int seriesId, ScrobbleEventType eventType, bool isNotProcessed = false)
|
||||
{
|
||||
return await _context.ScrobbleEvent.FirstOrDefaultAsync(e =>
|
||||
e.AppUserId == userId && e.SeriesId == seriesId && e.ScrobbleEventType == eventType);
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && e.SeriesId == seriesId && e.ScrobbleEventType == eventType)
|
||||
.WhereIf(isNotProcessed, e => !e.IsProcessed)
|
||||
.OrderBy(e => e.LastModifiedUtc)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ScrobbleEvent>> GetUserEventsForSeries(int userId, int seriesId)
|
||||
{
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && !e.IsProcessed)
|
||||
.Where(e => e.AppUserId == userId && !e.IsProcessed && e.SeriesId == seriesId)
|
||||
.Include(e => e.Series)
|
||||
.OrderBy(e => e.LastModifiedUtc)
|
||||
.AsSplitQuery()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<ScrobbleEvent>> GetUserEvents(int userId, IList<long> scrobbleEventIds)
|
||||
{
|
||||
return await _context.ScrobbleEvent
|
||||
.Where(e => e.AppUserId == userId && scrobbleEventIds.Contains(e.Id))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination)
|
||||
{
|
||||
var query = _context.ScrobbleEvent
|
||||
|
|
|
|||
|
|
@ -68,4 +68,14 @@ public class ScrobbleEvent : IEntityDate
|
|||
public DateTime LastModified { get; set; }
|
||||
public DateTime CreatedUtc { get; set; }
|
||||
public DateTime LastModifiedUtc { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the ErrorDetail and marks the event as <see cref="IsErrored"/>
|
||||
/// </summary>
|
||||
/// <param name="errorMessage"></param>
|
||||
public void SetErrorMessage(string errorMessage)
|
||||
{
|
||||
ErrorDetails = errorMessage;
|
||||
IsErrored = true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ public static class ApplicationServiceExtensions
|
|||
services.AddScoped<ISettingsService, SettingsService>();
|
||||
|
||||
|
||||
services.AddScoped<IKavitaPlusApiService, KavitaPlusApiService>();
|
||||
services.AddScoped<IScrobblingService, ScrobblingService>();
|
||||
services.AddScoped<ILicenseService, LicenseService>();
|
||||
services.AddScoped<IExternalMetadataService, ExternalMetadataService>();
|
||||
|
|
|
|||
|
|
@ -56,6 +56,12 @@ public static class RestrictByAgeExtensions
|
|||
sm.Metadata.AgeRating <= restriction.AgeRating && sm.Metadata.AgeRating > AgeRating.Unknown));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all Genres where any of the linked Series/Chapters are less than or equal to restriction age rating
|
||||
/// </summary>
|
||||
/// <param name="queryable"></param>
|
||||
/// <param name="restriction"></param>
|
||||
/// <returns></returns>
|
||||
public static IQueryable<Genre> RestrictAgainstAgeRestriction(this IQueryable<Genre> queryable, AgeRestriction restriction)
|
||||
{
|
||||
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
||||
|
|
|
|||
75
API/Services/Plus/KavitaPlusApiService.cs
Normal file
75
API/Services/Plus/KavitaPlusApiService.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#nullable enable
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs.Scrobbling;
|
||||
using API.Extensions;
|
||||
using Flurl.Http;
|
||||
using Kavita.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace API.Services.Plus;
|
||||
|
||||
/// <summary>
|
||||
/// All Http requests to K+ should be contained in this service, the service will not handle any errors.
|
||||
/// This is expected from the caller.
|
||||
/// </summary>
|
||||
public interface IKavitaPlusApiService
|
||||
{
|
||||
Task<bool> HasTokenExpired(string license, string token, ScrobbleProvider provider);
|
||||
Task<int> GetRateLimit(string license, string token);
|
||||
Task<ScrobbleResponseDto> PostScrobbleUpdate(ScrobbleDto data, string license);
|
||||
}
|
||||
|
||||
public class KavitaPlusApiService(ILogger<KavitaPlusApiService> logger): IKavitaPlusApiService
|
||||
{
|
||||
private const string ScrobblingPath = "/api/scrobbling/";
|
||||
|
||||
public async Task<bool> HasTokenExpired(string license, string token, ScrobbleProvider provider)
|
||||
{
|
||||
var res = await Get(ScrobblingPath + "valid-key?provider=" + provider + "&key=" + token, license, token);
|
||||
var str = await res.GetStringAsync();
|
||||
return bool.Parse(str);
|
||||
}
|
||||
|
||||
public async Task<int> GetRateLimit(string license, string token)
|
||||
{
|
||||
var res = await Get(ScrobblingPath + "rate-limit?accessToken=" + token, license, token);
|
||||
var str = await res.GetStringAsync();
|
||||
return int.Parse(str);
|
||||
}
|
||||
|
||||
public async Task<ScrobbleResponseDto> PostScrobbleUpdate(ScrobbleDto data, string license)
|
||||
{
|
||||
return await PostAndReceive<ScrobbleResponseDto>(ScrobblingPath + "update", data, license);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a GET request to K+
|
||||
/// </summary>
|
||||
/// <param name="url">only path of the uri, the host is added</param>
|
||||
/// <param name="license"></param>
|
||||
/// <param name="aniListToken"></param>
|
||||
/// <returns></returns>
|
||||
private static async Task<IFlurlResponse> Get(string url, string license, string? aniListToken = null)
|
||||
{
|
||||
return await (Configuration.KavitaPlusApiUrl + url)
|
||||
.WithKavitaPlusHeaders(license, aniListToken)
|
||||
.GetAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a POST request to K+
|
||||
/// </summary>
|
||||
/// <param name="url">only path of the uri, the host is added</param>
|
||||
/// <param name="body"></param>
|
||||
/// <param name="license"></param>
|
||||
/// <param name="aniListToken"></param>
|
||||
/// <typeparam name="T">Return type</typeparam>
|
||||
/// <returns></returns>
|
||||
private static async Task<T> PostAndReceive<T>(string url, object body, string license, string? aniListToken = null)
|
||||
{
|
||||
return await (Configuration.KavitaPlusApiUrl + url)
|
||||
.WithKavitaPlusHeaders(license, aniListToken)
|
||||
.PostJsonAsync(body)
|
||||
.ReceiveJson<T>();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue