mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Scrobbling Stability (#3863)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
This commit is contained in:
parent
45e24aa311
commit
14a8f5c1e5
@ -67,7 +67,7 @@ public class QueryableExtensionsTests
|
|||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(true, 2)]
|
[InlineData(true, 2)]
|
||||||
[InlineData(false, 1)]
|
[InlineData(false, 2)]
|
||||||
public void RestrictAgainstAgeRestriction_Genre_ShouldRestrictEverythingAboveTeen(bool includeUnknowns, int expectedCount)
|
public void RestrictAgainstAgeRestriction_Genre_ShouldRestrictEverythingAboveTeen(bool includeUnknowns, int expectedCount)
|
||||||
{
|
{
|
||||||
var items = new List<Genre>()
|
var items = new List<Genre>()
|
||||||
@ -94,7 +94,7 @@ public class QueryableExtensionsTests
|
|||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(true, 2)]
|
[InlineData(true, 2)]
|
||||||
[InlineData(false, 1)]
|
[InlineData(false, 2)]
|
||||||
public void RestrictAgainstAgeRestriction_Tag_ShouldRestrictEverythingAboveTeen(bool includeUnknowns, int expectedCount)
|
public void RestrictAgainstAgeRestriction_Tag_ShouldRestrictEverythingAboveTeen(bool includeUnknowns, int expectedCount)
|
||||||
{
|
{
|
||||||
var items = new List<Tag>()
|
var items = new List<Tag>()
|
||||||
|
@ -2935,6 +2935,8 @@ public class ExternalMetadataServiceTests : AbstractDbTest
|
|||||||
metadataSettings.EnableTags = false;
|
metadataSettings.EnableTags = false;
|
||||||
metadataSettings.EnablePublicationStatus = false;
|
metadataSettings.EnablePublicationStatus = false;
|
||||||
metadataSettings.EnableStartDate = false;
|
metadataSettings.EnableStartDate = false;
|
||||||
|
metadataSettings.FieldMappings = [];
|
||||||
|
metadataSettings.AgeRatingMappings = new Dictionary<string, AgeRating>();
|
||||||
Context.MetadataSettings.Update(metadataSettings);
|
Context.MetadataSettings.Update(metadataSettings);
|
||||||
|
|
||||||
await Context.SaveChangesAsync();
|
await Context.SaveChangesAsync();
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
using System.Linq;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using API.Data.Repositories;
|
||||||
using API.DTOs.Scrobbling;
|
using API.DTOs.Scrobbling;
|
||||||
|
using API.Entities;
|
||||||
using API.Entities.Enums;
|
using API.Entities.Enums;
|
||||||
|
using API.Entities.Scrobble;
|
||||||
using API.Helpers.Builders;
|
using API.Helpers.Builders;
|
||||||
using API.Services;
|
using API.Services;
|
||||||
using API.Services.Plus;
|
using API.Services.Plus;
|
||||||
using API.SignalR;
|
using API.SignalR;
|
||||||
|
using Kavita.Common;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@ -15,11 +21,33 @@ namespace API.Tests.Services;
|
|||||||
|
|
||||||
public class ScrobblingServiceTests : AbstractDbTest
|
public class ScrobblingServiceTests : AbstractDbTest
|
||||||
{
|
{
|
||||||
|
private const int ChapterPages = 100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// {
|
||||||
|
/// "Issuer": "Issuer",
|
||||||
|
/// "Issued At": "2025-06-15T21:01:57.615Z",
|
||||||
|
/// "Expiration": "2200-06-15T21:01:57.615Z"
|
||||||
|
/// }
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Our UnitTests will fail in 2200 :(</remarks>
|
||||||
|
private const string ValidJwtToken =
|
||||||
|
"eyJhbGciOiJIUzI1NiJ9.eyJJc3N1ZXIiOiJJc3N1ZXIiLCJleHAiOjcyNzI0NTAxMTcsImlhdCI6MTc1MDAyMTMxN30.zADmcGq_BfxbcV8vy4xw5Cbzn4COkmVINxgqpuL17Ng";
|
||||||
|
|
||||||
private readonly ScrobblingService _service;
|
private readonly ScrobblingService _service;
|
||||||
private readonly ILicenseService _licenseService;
|
private readonly ILicenseService _licenseService;
|
||||||
private readonly ILocalizationService _localizationService;
|
private readonly ILocalizationService _localizationService;
|
||||||
private readonly ILogger<ScrobblingService> _logger;
|
private readonly ILogger<ScrobblingService> _logger;
|
||||||
private readonly IEmailService _emailService;
|
private readonly IEmailService _emailService;
|
||||||
|
private readonly IKavitaPlusApiService _kavitaPlusApiService;
|
||||||
|
/// <summary>
|
||||||
|
/// IReaderService, without the ScrobblingService injected
|
||||||
|
/// </summary>
|
||||||
|
private readonly IReaderService _readerService;
|
||||||
|
/// <summary>
|
||||||
|
/// IReaderService, with the _service injected
|
||||||
|
/// </summary>
|
||||||
|
private readonly IReaderService _hookedUpReaderService;
|
||||||
|
|
||||||
public ScrobblingServiceTests()
|
public ScrobblingServiceTests()
|
||||||
{
|
{
|
||||||
@ -27,8 +55,24 @@ public class ScrobblingServiceTests : AbstractDbTest
|
|||||||
_localizationService = Substitute.For<ILocalizationService>();
|
_localizationService = Substitute.For<ILocalizationService>();
|
||||||
_logger = Substitute.For<ILogger<ScrobblingService>>();
|
_logger = Substitute.For<ILogger<ScrobblingService>>();
|
||||||
_emailService = Substitute.For<IEmailService>();
|
_emailService = Substitute.For<IEmailService>();
|
||||||
|
_kavitaPlusApiService = Substitute.For<IKavitaPlusApiService>();
|
||||||
|
|
||||||
_service = new ScrobblingService(UnitOfWork, Substitute.For<IEventHub>(), _logger, _licenseService, _localizationService, _emailService);
|
_service = new ScrobblingService(UnitOfWork, Substitute.For<IEventHub>(), _logger, _licenseService,
|
||||||
|
_localizationService, _emailService, _kavitaPlusApiService);
|
||||||
|
|
||||||
|
_readerService = new ReaderService(UnitOfWork,
|
||||||
|
Substitute.For<ILogger<ReaderService>>(),
|
||||||
|
Substitute.For<IEventHub>(),
|
||||||
|
Substitute.For<IImageService>(),
|
||||||
|
Substitute.For<IDirectoryService>(),
|
||||||
|
Substitute.For<IScrobblingService>()); // Do not use the actual one
|
||||||
|
|
||||||
|
_hookedUpReaderService = new ReaderService(UnitOfWork,
|
||||||
|
Substitute.For<ILogger<ReaderService>>(),
|
||||||
|
Substitute.For<IEventHub>(),
|
||||||
|
Substitute.For<IImageService>(),
|
||||||
|
Substitute.For<IDirectoryService>(),
|
||||||
|
_service);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ResetDb()
|
protected override async Task ResetDb()
|
||||||
@ -46,6 +90,30 @@ public class ScrobblingServiceTests : AbstractDbTest
|
|||||||
var series = new SeriesBuilder("Test Series")
|
var series = new SeriesBuilder("Test Series")
|
||||||
.WithFormat(MangaFormat.Archive)
|
.WithFormat(MangaFormat.Archive)
|
||||||
.WithMetadata(new SeriesMetadataBuilder().Build())
|
.WithMetadata(new SeriesMetadataBuilder().Build())
|
||||||
|
.WithVolume(new VolumeBuilder("Volume 1")
|
||||||
|
.WithChapters([
|
||||||
|
new ChapterBuilder("1")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build(),
|
||||||
|
new ChapterBuilder("2")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build(),
|
||||||
|
new ChapterBuilder("3")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build()])
|
||||||
|
.Build())
|
||||||
|
.WithVolume(new VolumeBuilder("Volume 2")
|
||||||
|
.WithChapters([
|
||||||
|
new ChapterBuilder("4")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build(),
|
||||||
|
new ChapterBuilder("5")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build(),
|
||||||
|
new ChapterBuilder("6")
|
||||||
|
.WithPages(ChapterPages)
|
||||||
|
.Build()])
|
||||||
|
.Build())
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var library = new LibraryBuilder("Test Library", LibraryType.Manga)
|
var library = new LibraryBuilder("Test Library", LibraryType.Manga)
|
||||||
@ -67,6 +135,296 @@ public class ScrobblingServiceTests : AbstractDbTest
|
|||||||
await UnitOfWork.CommitAsync();
|
await UnitOfWork.CommitAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<ScrobbleEvent> CreateScrobbleEvent(int? seriesId = null)
|
||||||
|
{
|
||||||
|
var evt = new ScrobbleEvent
|
||||||
|
{
|
||||||
|
ScrobbleEventType = ScrobbleEventType.ChapterRead,
|
||||||
|
Format = PlusMediaFormat.Manga,
|
||||||
|
SeriesId = seriesId ?? 0,
|
||||||
|
LibraryId = 0,
|
||||||
|
AppUserId = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (seriesId != null)
|
||||||
|
{
|
||||||
|
var series = await UnitOfWork.SeriesRepository.GetSeriesByIdAsync(seriesId.Value);
|
||||||
|
if (series != null) evt.Series = series;
|
||||||
|
}
|
||||||
|
|
||||||
|
return evt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#region K+ API Request Tests
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PostScrobbleUpdate_AuthErrors()
|
||||||
|
{
|
||||||
|
_kavitaPlusApiService.PostScrobbleUpdate(null!, "")
|
||||||
|
.ReturnsForAnyArgs(new ScrobbleResponseDto()
|
||||||
|
{
|
||||||
|
ErrorMessage = "Unauthorized"
|
||||||
|
});
|
||||||
|
|
||||||
|
var evt = await CreateScrobbleEvent();
|
||||||
|
await Assert.ThrowsAsync<KavitaException>(async () =>
|
||||||
|
{
|
||||||
|
await _service.PostScrobbleUpdate(new ScrobbleDto(), "", evt);
|
||||||
|
});
|
||||||
|
Assert.True(evt.IsErrored);
|
||||||
|
Assert.Equal("Kavita+ subscription no longer active", evt.ErrorDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PostScrobbleUpdate_UnknownSeriesLoggedAsError()
|
||||||
|
{
|
||||||
|
_kavitaPlusApiService.PostScrobbleUpdate(null!, "")
|
||||||
|
.ReturnsForAnyArgs(new ScrobbleResponseDto()
|
||||||
|
{
|
||||||
|
ErrorMessage = "Unknown Series"
|
||||||
|
});
|
||||||
|
|
||||||
|
await SeedData();
|
||||||
|
var evt = await CreateScrobbleEvent(1);
|
||||||
|
|
||||||
|
await _service.PostScrobbleUpdate(new ScrobbleDto(), "", evt);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
Assert.True(evt.IsErrored);
|
||||||
|
|
||||||
|
var series = await UnitOfWork.SeriesRepository.GetSeriesByIdAsync(1);
|
||||||
|
Assert.NotNull(series);
|
||||||
|
Assert.True(series.IsBlacklisted);
|
||||||
|
|
||||||
|
var errors = await UnitOfWork.ScrobbleRepository.GetAllScrobbleErrorsForSeries(1);
|
||||||
|
Assert.Single(errors);
|
||||||
|
Assert.Equal("Series cannot be matched for Scrobbling", errors.First().Comment);
|
||||||
|
Assert.Equal(series.Id, errors.First().SeriesId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PostScrobbleUpdate_InvalidAccessToken()
|
||||||
|
{
|
||||||
|
_kavitaPlusApiService.PostScrobbleUpdate(null!, "")
|
||||||
|
.ReturnsForAnyArgs(new ScrobbleResponseDto()
|
||||||
|
{
|
||||||
|
ErrorMessage = "Access token is invalid"
|
||||||
|
});
|
||||||
|
|
||||||
|
var evt = await CreateScrobbleEvent();
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<KavitaException>(async () =>
|
||||||
|
{
|
||||||
|
await _service.PostScrobbleUpdate(new ScrobbleDto(), "", evt);
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.True(evt.IsErrored);
|
||||||
|
Assert.Equal("Access Token needs to be rotated to continue scrobbling", evt.ErrorDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region K+ API Request data tests
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ProcessReadEvents_CreatesNoEventsWhenNoProgress()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
// Set Returns
|
||||||
|
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
|
||||||
|
_kavitaPlusApiService.GetRateLimit(Arg.Any<string>(), Arg.Any<string>())
|
||||||
|
.Returns(100);
|
||||||
|
|
||||||
|
var user = await UnitOfWork.UserRepository.GetUserByIdAsync(1);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
// Ensure CanProcessScrobbleEvent returns true
|
||||||
|
user.AniListAccessToken = ValidJwtToken;
|
||||||
|
UnitOfWork.UserRepository.Update(user);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
var chapter = await UnitOfWork.ChapterRepository.GetChapterAsync(4);
|
||||||
|
Assert.NotNull(chapter);
|
||||||
|
|
||||||
|
var volume = await UnitOfWork.VolumeRepository.GetVolumeAsync(1, VolumeIncludes.Chapters);
|
||||||
|
Assert.NotNull(volume);
|
||||||
|
|
||||||
|
// Call Scrobble without having any progress
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ProcessReadEvents_UpdateVolumeAndChapterData()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
// Set Returns
|
||||||
|
_licenseService.HasActiveLicense().Returns(Task.FromResult(true));
|
||||||
|
_kavitaPlusApiService.GetRateLimit(Arg.Any<string>(), Arg.Any<string>())
|
||||||
|
.Returns(100);
|
||||||
|
|
||||||
|
var user = await UnitOfWork.UserRepository.GetUserByIdAsync(1);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
// Ensure CanProcessScrobbleEvent returns true
|
||||||
|
user.AniListAccessToken = ValidJwtToken;
|
||||||
|
UnitOfWork.UserRepository.Update(user);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
var chapter = await UnitOfWork.ChapterRepository.GetChapterAsync(4);
|
||||||
|
Assert.NotNull(chapter);
|
||||||
|
|
||||||
|
var volume = await UnitOfWork.VolumeRepository.GetVolumeAsync(1, VolumeIncludes.Chapters);
|
||||||
|
Assert.NotNull(volume);
|
||||||
|
|
||||||
|
// Mark something as read to trigger event creation
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, new List<Chapter>() {volume.Chapters[0]});
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// Call Scrobble while having some progress
|
||||||
|
await _service.ScrobbleReadingUpdate(user.Id, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Single(events);
|
||||||
|
|
||||||
|
// Give it some (more) read progress
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, volume.Chapters);
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, [chapter]);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
await _service.ProcessUpdatesSinceLastSync();
|
||||||
|
|
||||||
|
await _kavitaPlusApiService.Received(1).PostScrobbleUpdate(
|
||||||
|
Arg.Is<ScrobbleDto>(data =>
|
||||||
|
data.ChapterNumber == (int)chapter.MaxNumber &&
|
||||||
|
data.VolumeNumber == (int)volume.MaxNumber
|
||||||
|
),
|
||||||
|
Arg.Any<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Scrobble Reading Update Tests
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ScrobbleReadingUpdate_IgnoreNoLicense()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
_licenseService.HasActiveLicense().Returns(false);
|
||||||
|
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ScrobbleReadingUpdate_RemoveWhenNoProgress()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
_licenseService.HasActiveLicense().Returns(true);
|
||||||
|
|
||||||
|
var user = await UnitOfWork.UserRepository.GetUserByIdAsync(1);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
var volume = await UnitOfWork.VolumeRepository.GetVolumeAsync(1, VolumeIncludes.Chapters);
|
||||||
|
Assert.NotNull(volume);
|
||||||
|
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, new List<Chapter>() {volume.Chapters[0]});
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Single(events);
|
||||||
|
|
||||||
|
var readEvent = events.First();
|
||||||
|
Assert.False(readEvent.IsProcessed);
|
||||||
|
|
||||||
|
await _hookedUpReaderService.MarkSeriesAsUnread(user, 1);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// Existing event is deleted
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
|
||||||
|
await _hookedUpReaderService.MarkSeriesAsUnread(user, 1);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// No new events are added
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ScrobbleReadingUpdate_UpdateExistingNotIsProcessed()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
var user = await UnitOfWork.UserRepository.GetUserByIdAsync(1);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
var chapter1 = await UnitOfWork.ChapterRepository.GetChapterAsync(1);
|
||||||
|
var chapter2 = await UnitOfWork.ChapterRepository.GetChapterAsync(2);
|
||||||
|
var chapter3 = await UnitOfWork.ChapterRepository.GetChapterAsync(3);
|
||||||
|
Assert.NotNull(chapter1);
|
||||||
|
Assert.NotNull(chapter2);
|
||||||
|
Assert.NotNull(chapter3);
|
||||||
|
|
||||||
|
_licenseService.HasActiveLicense().Returns(true);
|
||||||
|
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
|
||||||
|
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, [chapter1]);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// Scrobble update
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Single(events);
|
||||||
|
|
||||||
|
var readEvent = events[0];
|
||||||
|
Assert.False(readEvent.IsProcessed);
|
||||||
|
Assert.Equal(1, readEvent.ChapterNumber);
|
||||||
|
|
||||||
|
// Mark as processed
|
||||||
|
readEvent.IsProcessed = true;
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, [chapter2]);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// Scrobble update
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Equal(2, events.Count);
|
||||||
|
Assert.Single(events.Where(e => e.IsProcessed).ToList());
|
||||||
|
Assert.Single(events.Where(e => !e.IsProcessed).ToList());
|
||||||
|
|
||||||
|
// Should update the existing non processed event
|
||||||
|
await _readerService.MarkChaptersAsRead(user, 1, [chapter3]);
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
// Scrobble update
|
||||||
|
await _service.ScrobbleReadingUpdate(1, 1);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Equal(2, events.Count);
|
||||||
|
Assert.Single(events.Where(e => e.IsProcessed).ToList());
|
||||||
|
Assert.Single(events.Where(e => !e.IsProcessed).ToList());
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region ScrobbleWantToReadUpdate Tests
|
#region ScrobbleWantToReadUpdate Tests
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -203,6 +561,59 @@ public class ScrobblingServiceTests : AbstractDbTest
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Scrobble Rating Update Test
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ScrobbleRatingUpdate_IgnoreNoLicense()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
_licenseService.HasActiveLicense().Returns(false);
|
||||||
|
|
||||||
|
await _service.ScrobbleRatingUpdate(1, 1, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Empty(events);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ScrobbleRatingUpdate_UpdateExistingNotIsProcessed()
|
||||||
|
{
|
||||||
|
await ResetDb();
|
||||||
|
await SeedData();
|
||||||
|
|
||||||
|
_licenseService.HasActiveLicense().Returns(true);
|
||||||
|
|
||||||
|
var user = await UnitOfWork.UserRepository.GetUserByIdAsync(1);
|
||||||
|
Assert.NotNull(user);
|
||||||
|
|
||||||
|
var series = await UnitOfWork.SeriesRepository.GetSeriesByIdAsync(1);
|
||||||
|
Assert.NotNull(series);
|
||||||
|
|
||||||
|
await _service.ScrobbleRatingUpdate(user.Id, series.Id, 1);
|
||||||
|
var events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Single(events);
|
||||||
|
Assert.Equal(1, events.First().Rating);
|
||||||
|
|
||||||
|
// Mark as processed
|
||||||
|
events.First().IsProcessed = true;
|
||||||
|
await UnitOfWork.CommitAsync();
|
||||||
|
|
||||||
|
await _service.ScrobbleRatingUpdate(user.Id, series.Id, 5);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Equal(2, events.Count);
|
||||||
|
Assert.Single(events, evt => evt.IsProcessed);
|
||||||
|
Assert.Single(events, evt => !evt.IsProcessed);
|
||||||
|
|
||||||
|
await _service.ScrobbleRatingUpdate(user.Id, series.Id, 5);
|
||||||
|
events = await UnitOfWork.ScrobbleRepository.GetAllEventsForSeries(1);
|
||||||
|
Assert.Single(events, evt => !evt.IsProcessed);
|
||||||
|
Assert.Equal(5, events.First(evt => !evt.IsProcessed).Rating);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("https://anilist.co/manga/35851/Byeontaega-Doeja/", 35851)]
|
[InlineData("https://anilist.co/manga/35851/Byeontaega-Doeja/", 35851)]
|
||||||
[InlineData("https://anilist.co/manga/30105", 30105)]
|
[InlineData("https://anilist.co/manga/30105", 30105)]
|
||||||
|
@ -254,7 +254,7 @@ public class ScrobblingController : BaseApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a hold against the Series for user's scrobbling
|
/// Remove a hold against the Series for user's scrobbling
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="seriesId"></param>
|
/// <param name="seriesId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
@ -281,4 +281,18 @@ public class ScrobblingController : BaseApiController
|
|||||||
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());
|
var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());
|
||||||
return Ok(user is {HasRunScrobbleEventGeneration: true});
|
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 sealed record ScrobbleEventDto
|
||||||
{
|
{
|
||||||
|
public long Id { get; init; }
|
||||||
public string SeriesName { get; set; }
|
public string SeriesName { get; set; }
|
||||||
public int SeriesId { get; set; }
|
public int SeriesId { get; set; }
|
||||||
public int LibraryId { get; set; }
|
public int LibraryId { get; set; }
|
||||||
|
@ -8,5 +8,6 @@ public sealed record ScrobbleResponseDto
|
|||||||
{
|
{
|
||||||
public bool Successful { get; set; }
|
public bool Successful { get; set; }
|
||||||
public string? ErrorMessage { get; set; }
|
public string? ErrorMessage { get; set; }
|
||||||
|
public string? ExtraInformation {get; set;}
|
||||||
public int RateLeft { get; set; }
|
public int RateLeft { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,23 @@ public interface IScrobbleRepository
|
|||||||
Task<IList<ScrobbleError>> GetAllScrobbleErrorsForSeries(int seriesId);
|
Task<IList<ScrobbleError>> GetAllScrobbleErrorsForSeries(int seriesId);
|
||||||
Task ClearScrobbleErrors();
|
Task ClearScrobbleErrors();
|
||||||
Task<bool> HasErrorForSeries(int seriesId);
|
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);
|
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<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination);
|
||||||
Task<IList<ScrobbleEvent>> GetAllEventsForSeries(int seriesId);
|
Task<IList<ScrobbleEvent>> GetAllEventsForSeries(int seriesId);
|
||||||
Task<IList<ScrobbleEvent>> GetAllEventsWithSeriesIds(IEnumerable<int> seriesIds);
|
Task<IList<ScrobbleEvent>> GetAllEventsWithSeriesIds(IEnumerable<int> seriesIds);
|
||||||
@ -146,22 +161,32 @@ public class ScrobbleRepository : IScrobbleRepository
|
|||||||
return await _context.ScrobbleError.AnyAsync(n => n.SeriesId == seriesId);
|
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 =>
|
return await _context.ScrobbleEvent
|
||||||
e.AppUserId == userId && e.SeriesId == seriesId && e.ScrobbleEventType == eventType);
|
.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)
|
public async Task<IEnumerable<ScrobbleEvent>> GetUserEventsForSeries(int userId, int seriesId)
|
||||||
{
|
{
|
||||||
return await _context.ScrobbleEvent
|
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)
|
.Include(e => e.Series)
|
||||||
.OrderBy(e => e.LastModifiedUtc)
|
.OrderBy(e => e.LastModifiedUtc)
|
||||||
.AsSplitQuery()
|
.AsSplitQuery()
|
||||||
.ToListAsync();
|
.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)
|
public async Task<PagedList<ScrobbleEventDto>> GetUserEvents(int userId, ScrobbleEventFilter filter, UserParams pagination)
|
||||||
{
|
{
|
||||||
var query = _context.ScrobbleEvent
|
var query = _context.ScrobbleEvent
|
||||||
|
@ -68,4 +68,14 @@ public class ScrobbleEvent : IEntityDate
|
|||||||
public DateTime LastModified { get; set; }
|
public DateTime LastModified { get; set; }
|
||||||
public DateTime CreatedUtc { get; set; }
|
public DateTime CreatedUtc { get; set; }
|
||||||
public DateTime LastModifiedUtc { 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<ISettingsService, SettingsService>();
|
||||||
|
|
||||||
|
|
||||||
|
services.AddScoped<IKavitaPlusApiService, KavitaPlusApiService>();
|
||||||
services.AddScoped<IScrobblingService, ScrobblingService>();
|
services.AddScoped<IScrobblingService, ScrobblingService>();
|
||||||
services.AddScoped<ILicenseService, LicenseService>();
|
services.AddScoped<ILicenseService, LicenseService>();
|
||||||
services.AddScoped<IExternalMetadataService, ExternalMetadataService>();
|
services.AddScoped<IExternalMetadataService, ExternalMetadataService>();
|
||||||
|
@ -56,6 +56,12 @@ public static class RestrictByAgeExtensions
|
|||||||
sm.Metadata.AgeRating <= restriction.AgeRating && sm.Metadata.AgeRating > AgeRating.Unknown));
|
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)
|
public static IQueryable<Genre> RestrictAgainstAgeRestriction(this IQueryable<Genre> queryable, AgeRestriction restriction)
|
||||||
{
|
{
|
||||||
if (restriction.AgeRating == AgeRating.NotApplicable) return queryable;
|
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
@ -7,6 +7,7 @@ export enum ScrobbleEventType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ScrobbleEvent {
|
export interface ScrobbleEvent {
|
||||||
|
id: number;
|
||||||
seriesName: string;
|
seriesName: string;
|
||||||
seriesId: number;
|
seriesId: number;
|
||||||
libraryId: number;
|
libraryId: number;
|
||||||
|
@ -104,6 +104,10 @@ export class ScrobblingService {
|
|||||||
|
|
||||||
triggerScrobbleEventGeneration() {
|
triggerScrobbleEventGeneration() {
|
||||||
return this.httpClient.post(this.baseUrl + 'scrobbling/generate-scrobble-events', TextResonse);
|
return this.httpClient.post(this.baseUrl + 'scrobbling/generate-scrobble-events', TextResonse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bulkRemoveEvents(eventIds: number[]) {
|
||||||
|
return this.httpClient.post(this.baseUrl + "scrobbling/bulk-remove-events", eventIds)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,10 @@
|
|||||||
<form [formGroup]="formGroup">
|
<form [formGroup]="formGroup">
|
||||||
<div class="form-group pe-1">
|
<div class="form-group pe-1">
|
||||||
<label for="filter">{{t('filter-label')}}</label>
|
<label for="filter">{{t('filter-label')}}</label>
|
||||||
<input id="filter" type="text" class="form-control" formControlName="filter" autocomplete="off"/>
|
<div class="input-group">
|
||||||
|
<input id="filter" type="text" class="form-control" formControlName="filter" autocomplete="off" [placeholder]="t('filter-label')"/>
|
||||||
|
<button class="btn btn-primary" type="button" [disabled]="!selections.hasAnySelected()" (click)="bulkDelete()">{{t('delete-selected-label')}}</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -40,6 +43,20 @@
|
|||||||
[sorts]="[{prop: 'createdUtc', dir: 'desc'}]"
|
[sorts]="[{prop: 'createdUtc', dir: 'desc'}]"
|
||||||
>
|
>
|
||||||
|
|
||||||
|
<ngx-datatable-column prop="select" [sortable]="false" [draggable]="false" [resizeable]="false" [width]="50">
|
||||||
|
<ng-template let-column="column" ngx-datatable-header-template>
|
||||||
|
<div class="form-check">
|
||||||
|
<input id="select-all" type="checkbox" class="form-check-input"
|
||||||
|
[ngModel]="selectAll" (change)="toggleAll()" [indeterminate]="selections.hasSomeSelected()">
|
||||||
|
<label for="select-all" class="form-check-label d-md-block d-none">{{t('select-all-label')}}</label>
|
||||||
|
</div>
|
||||||
|
</ng-template>
|
||||||
|
<ng-template let-event="row" let-idx="index" ngx-datatable-cell-template>
|
||||||
|
<input id="select-event-{{idx}}" type="checkbox" class="form-check-input"
|
||||||
|
[ngModel]="selections.isSelected(event)" (change)="handleSelection(event, idx)">
|
||||||
|
</ng-template>
|
||||||
|
</ngx-datatable-column>
|
||||||
|
|
||||||
<ngx-datatable-column prop="createdUtc" [sortable]="true" [draggable]="false" [resizeable]="false">
|
<ngx-datatable-column prop="createdUtc" [sortable]="true" [draggable]="false" [resizeable]="false">
|
||||||
<ng-template let-column="column" ngx-datatable-header-template>
|
<ng-template let-column="column" ngx-datatable-header-template>
|
||||||
{{t('created-header')}}
|
{{t('created-header')}}
|
||||||
@ -101,7 +118,7 @@
|
|||||||
</ng-template>
|
</ng-template>
|
||||||
</ngx-datatable-column>
|
</ngx-datatable-column>
|
||||||
|
|
||||||
<ngx-datatable-column prop="isPorcessed" [sortable]="true" [draggable]="false" [resizeable]="false">
|
<ngx-datatable-column prop="isProcessed" [sortable]="true" [draggable]="false" [resizeable]="false">
|
||||||
<ng-template let-column="column" ngx-datatable-header-template>
|
<ng-template let-column="column" ngx-datatable-header-template>
|
||||||
{{t('is-processed-header')}}
|
{{t('is-processed-header')}}
|
||||||
</ng-template>
|
</ng-template>
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
import {ChangeDetectionStrategy, ChangeDetectorRef, Component, DestroyRef, inject, OnInit} from '@angular/core';
|
import {
|
||||||
|
ChangeDetectionStrategy,
|
||||||
|
ChangeDetectorRef,
|
||||||
|
Component,
|
||||||
|
DestroyRef,
|
||||||
|
HostListener,
|
||||||
|
inject,
|
||||||
|
OnInit
|
||||||
|
} from '@angular/core';
|
||||||
|
|
||||||
import {ScrobbleProvider, ScrobblingService} from "../../_services/scrobbling.service";
|
import {ScrobbleProvider, ScrobblingService} from "../../_services/scrobbling.service";
|
||||||
import {takeUntilDestroyed} from "@angular/core/rxjs-interop";
|
import {takeUntilDestroyed} from "@angular/core/rxjs-interop";
|
||||||
@ -9,7 +17,7 @@ import {ScrobbleEventSortField} from "../../_models/scrobbling/scrobble-event-fi
|
|||||||
import {debounceTime, take} from "rxjs/operators";
|
import {debounceTime, take} from "rxjs/operators";
|
||||||
import {PaginatedResult} from "../../_models/pagination";
|
import {PaginatedResult} from "../../_models/pagination";
|
||||||
import {SortEvent} from "../table/_directives/sortable-header.directive";
|
import {SortEvent} from "../table/_directives/sortable-header.directive";
|
||||||
import {FormControl, FormGroup, ReactiveFormsModule} from "@angular/forms";
|
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from "@angular/forms";
|
||||||
import {translate, TranslocoModule} from "@jsverse/transloco";
|
import {translate, TranslocoModule} from "@jsverse/transloco";
|
||||||
import {DefaultValuePipe} from "../../_pipes/default-value.pipe";
|
import {DefaultValuePipe} from "../../_pipes/default-value.pipe";
|
||||||
import {TranslocoLocaleModule} from "@jsverse/transloco-locale";
|
import {TranslocoLocaleModule} from "@jsverse/transloco-locale";
|
||||||
@ -19,6 +27,7 @@ import {ColumnMode, NgxDatatableModule} from "@siemens/ngx-datatable";
|
|||||||
import {AsyncPipe} from "@angular/common";
|
import {AsyncPipe} from "@angular/common";
|
||||||
import {AccountService} from "../../_services/account.service";
|
import {AccountService} from "../../_services/account.service";
|
||||||
import {ToastrService} from "ngx-toastr";
|
import {ToastrService} from "ngx-toastr";
|
||||||
|
import {SelectionModel} from "../../typeahead/_models/selection-model";
|
||||||
|
|
||||||
export interface DataTablePage {
|
export interface DataTablePage {
|
||||||
pageNumber: number,
|
pageNumber: number,
|
||||||
@ -30,7 +39,7 @@ export interface DataTablePage {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'app-user-scrobble-history',
|
selector: 'app-user-scrobble-history',
|
||||||
imports: [ScrobbleEventTypePipe, ReactiveFormsModule, TranslocoModule,
|
imports: [ScrobbleEventTypePipe, ReactiveFormsModule, TranslocoModule,
|
||||||
DefaultValuePipe, TranslocoLocaleModule, UtcToLocalTimePipe, NgbTooltip, NgxDatatableModule, AsyncPipe],
|
DefaultValuePipe, TranslocoLocaleModule, UtcToLocalTimePipe, NgbTooltip, NgxDatatableModule, AsyncPipe, FormsModule],
|
||||||
templateUrl: './user-scrobble-history.component.html',
|
templateUrl: './user-scrobble-history.component.html',
|
||||||
styleUrls: ['./user-scrobble-history.component.scss'],
|
styleUrls: ['./user-scrobble-history.component.scss'],
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush
|
changeDetection: ChangeDetectionStrategy.OnPush
|
||||||
@ -48,8 +57,6 @@ export class UserScrobbleHistoryComponent implements OnInit {
|
|||||||
private readonly toastr = inject(ToastrService);
|
private readonly toastr = inject(ToastrService);
|
||||||
protected readonly accountService = inject(AccountService);
|
protected readonly accountService = inject(AccountService);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tokenExpired = false;
|
tokenExpired = false;
|
||||||
formGroup: FormGroup = new FormGroup({
|
formGroup: FormGroup = new FormGroup({
|
||||||
'filter': new FormControl('', [])
|
'filter': new FormControl('', [])
|
||||||
@ -68,6 +75,21 @@ export class UserScrobbleHistoryComponent implements OnInit {
|
|||||||
};
|
};
|
||||||
hasRunScrobbleGen: boolean = false;
|
hasRunScrobbleGen: boolean = false;
|
||||||
|
|
||||||
|
selections: SelectionModel<ScrobbleEvent> = new SelectionModel();
|
||||||
|
selectAll: boolean = false;
|
||||||
|
isShiftDown: boolean = false;
|
||||||
|
lastSelectedIndex: number | null = null;
|
||||||
|
|
||||||
|
@HostListener('document:keydown.shift', ['$event'])
|
||||||
|
handleKeypress(_: KeyboardEvent) {
|
||||||
|
this.isShiftDown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@HostListener('document:keyup.shift', ['$event'])
|
||||||
|
handleKeyUp(_: KeyboardEvent) {
|
||||||
|
this.isShiftDown = false;
|
||||||
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
|
||||||
this.pageInfo.pageNumber = 0;
|
this.pageInfo.pageNumber = 0;
|
||||||
@ -118,6 +140,7 @@ export class UserScrobbleHistoryComponent implements OnInit {
|
|||||||
.pipe(take(1))
|
.pipe(take(1))
|
||||||
.subscribe((result: PaginatedResult<ScrobbleEvent[]>) => {
|
.subscribe((result: PaginatedResult<ScrobbleEvent[]>) => {
|
||||||
this.events = result.result;
|
this.events = result.result;
|
||||||
|
this.selections = new SelectionModel(false, this.events);
|
||||||
|
|
||||||
this.pageInfo.totalPages = result.pagination.totalPages - 1; // ngx-datatable is 0 based, Kavita is 1 based
|
this.pageInfo.totalPages = result.pagination.totalPages - 1; // ngx-datatable is 0 based, Kavita is 1 based
|
||||||
this.pageInfo.size = result.pagination.itemsPerPage;
|
this.pageInfo.size = result.pagination.itemsPerPage;
|
||||||
@ -143,4 +166,55 @@ export class UserScrobbleHistoryComponent implements OnInit {
|
|||||||
this.toastr.info(translate('toasts.scrobble-gen-init'))
|
this.toastr.info(translate('toasts.scrobble-gen-init'))
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bulkDelete() {
|
||||||
|
if (!this.selections.hasAnySelected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const eventIds = this.selections.selected().map(e => e.id);
|
||||||
|
|
||||||
|
this.scrobblingService.bulkRemoveEvents(eventIds).subscribe({
|
||||||
|
next: () => {
|
||||||
|
this.events = this.events.filter(e => !eventIds.includes(e.id));
|
||||||
|
this.selectAll = false;
|
||||||
|
this.selections.clearSelected();
|
||||||
|
this.pageInfo.totalElements -= eventIds.length;
|
||||||
|
this.cdRef.markForCheck();
|
||||||
|
},
|
||||||
|
error: err => {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
toggleAll() {
|
||||||
|
this.selectAll = !this.selectAll;
|
||||||
|
this.events.forEach(e => this.selections.toggle(e, this.selectAll));
|
||||||
|
this.cdRef.markForCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSelection(item: ScrobbleEvent, index: number) {
|
||||||
|
if (this.isShiftDown && this.lastSelectedIndex !== null) {
|
||||||
|
// Bulk select items between the last selected item and the current one
|
||||||
|
const start = Math.min(this.lastSelectedIndex, index);
|
||||||
|
const end = Math.max(this.lastSelectedIndex, index);
|
||||||
|
|
||||||
|
for (let i = start; i <= end; i++) {
|
||||||
|
const event = this.events[i];
|
||||||
|
if (!this.selections.isSelected(event, (e1, e2) => e1.id == e2.id)) {
|
||||||
|
this.selections.toggle(event, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.selections.toggle(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastSelectedIndex = index;
|
||||||
|
|
||||||
|
|
||||||
|
const numberOfSelected = this.selections.selected().length;
|
||||||
|
this.selectAll = numberOfSelected === this.events.length;
|
||||||
|
this.cdRef.markForCheck();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
<label for="filter" class="visually-hidden">{{t('filter-label')}}</label>
|
<label for="filter" class="visually-hidden">{{t('filter-label')}}</label>
|
||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<input id="filter" type="text" class="form-control" [placeholder]="t('filter-label')" formControlName="filter" />
|
<input id="filter" type="text" class="form-control" [placeholder]="t('filter-label')" formControlName="filter" />
|
||||||
<button class="btn btn-primary" type="button" (click)="clear()">{{t('clear-errors')}}</button>
|
<button class="btn btn-primary" type="button" [disabled]="data.length === 0" (click)="clear()">{{t('clear-errors')}}</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -70,6 +70,28 @@ export class SelectionModel<T> {
|
|||||||
return (selectedCount !== this._data.length && selectedCount !== 0)
|
return (selectedCount !== this._data.length && selectedCount !== 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return If at least one item is selected
|
||||||
|
*/
|
||||||
|
hasAnySelected(): boolean {
|
||||||
|
for (const d of this._data) {
|
||||||
|
if (d.selected) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks every data entry has not selected
|
||||||
|
*/
|
||||||
|
clearSelected() {
|
||||||
|
this._data = this._data.map(d => {
|
||||||
|
d.selected = false;
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @returns All Selected items
|
* @returns All Selected items
|
||||||
|
@ -42,6 +42,8 @@
|
|||||||
"series-header": "Series",
|
"series-header": "Series",
|
||||||
"data-header": "Data",
|
"data-header": "Data",
|
||||||
"is-processed-header": "Is Processed",
|
"is-processed-header": "Is Processed",
|
||||||
|
"select-all-label": "Select all",
|
||||||
|
"delete-selected-label": "Delete selected",
|
||||||
"no-data": "{{common.no-data}}",
|
"no-data": "{{common.no-data}}",
|
||||||
"volume-and-chapter-num": "Volume {{v}} Chapter {{n}}",
|
"volume-and-chapter-num": "Volume {{v}} Chapter {{n}}",
|
||||||
"volume-num": "Volume {{num}}",
|
"volume-num": "Volume {{num}}",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user