From 6af1d2f0f63823a61e2a2ae513e38cc781b01064 Mon Sep 17 00:00:00 2001 From: robertlordhood <204023072+robertlordhood@users.noreply.github.com> Date: Fri, 27 Nov 2026 14:48:03 +0000 Subject: [PATCH] Report owned books as complete on the Readarr API --- src/Chaptarr.Api.V1/Books/BookResource.cs | 5 +- .../Books/BookStatisticsResource.cs | 9 ++- .../BookControllerStatisticsBatchFixture.cs | 55 ++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Chaptarr.Api.V1/Books/BookResource.cs b/src/Chaptarr.Api.V1/Books/BookResource.cs index 404e4e702..adc89c3bf 100644 --- a/src/Chaptarr.Api.V1/Books/BookResource.cs +++ b/src/Chaptarr.Api.V1/Books/BookResource.cs @@ -230,6 +230,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption var hasFiles = options.Statistics != null ? options.Statistics.BookFileCount > 0 : model.HasFiles; + var mediaType = model.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook"; // Readarr facade compatibility: foreignBookId is bare only inside a path-declared // provider dialect. Native Chaptarr responses keep provider prefixes. @@ -290,7 +291,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption : (options.IncludeOverview ? (model.Overview ?? string.Empty) : null), Images = CloneImages(selectedEdition != null ? selectedEdition.Images : model.Images), Links = links, - Statistics = options.Statistics?.ToResource(), + Statistics = options.Statistics?.ToResource(reportFileAvailability: options.FacadeContext?.MediaType == mediaType), AddOptions = model.AddOptions, RemoteCover = string.Empty, Grabbed = false, @@ -299,7 +300,7 @@ public static BookResource ToResource(this Book model, BookResourceMappingOption NarratorNames = GetNarratorNamesForDisplay(model, selectedEdition), Duration = GetDuration(model, selectedEdition), DurationMinutes = GetDurationMinutes(model, selectedEdition), - MediaType = model.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook", + MediaType = mediaType, HasFiles = hasFiles, IsOmnibus = model.IsOmnibus }; diff --git a/src/Chaptarr.Api.V1/Books/BookStatisticsResource.cs b/src/Chaptarr.Api.V1/Books/BookStatisticsResource.cs index c83879ea4..d9621a7c9 100644 --- a/src/Chaptarr.Api.V1/Books/BookStatisticsResource.cs +++ b/src/Chaptarr.Api.V1/Books/BookStatisticsResource.cs @@ -9,11 +9,17 @@ public class BookStatisticsResource public int AvailableBookCount { get; set; } public int TotalBookCount { get; set; } public long SizeOnDisk { get; set; } + internal bool ReportFileAvailability { get; init; } public decimal PercentOfBooks { get { + if (ReportFileAvailability) + { + return BookFileCount > 0 ? 100 : 0; + } + if (BookCount == 0) { return 0; @@ -26,7 +32,7 @@ public decimal PercentOfBooks public static class BookStatisticsResourceMapper { - public static BookStatisticsResource ToResource(this BookStatistics model) + public static BookStatisticsResource ToResource(this BookStatistics model, bool reportFileAvailability = false) { if (model == null) { @@ -35,6 +41,7 @@ public static BookStatisticsResource ToResource(this BookStatistics model) return new BookStatisticsResource { + ReportFileAvailability = reportFileAvailability, BookFileCount = model.BookFileCount, BookCount = model.BookCount, AvailableBookCount = model.AvailableBookCount, diff --git a/src/Chaptarr.Core.Test/Api/BookControllerStatisticsBatchFixture.cs b/src/Chaptarr.Core.Test/Api/BookControllerStatisticsBatchFixture.cs index 7e8b4b598..dd2fd8bb2 100644 --- a/src/Chaptarr.Core.Test/Api/BookControllerStatisticsBatchFixture.cs +++ b/src/Chaptarr.Core.Test/Api/BookControllerStatisticsBatchFixture.cs @@ -144,9 +144,14 @@ INSERT INTO ""Editions"" VALUES { nameof(IBookService.FindAllByWorkProviderId) => Books.Where(book => book.MediaType == (BookMediaType)args[2]).ToList(), nameof(IBookService.GetBooks) => Books.Where(book => ((IEnumerable)args[0]).Contains(book.Id)).ToList(), + nameof(IBookService.GetBooksForDisplay) => Books.Where(book => + (string)args[1] == (book.MediaType == BookMediaType.Audiobook ? "audiobook" : "ebook")).ToList(), _ => throw new AssertionException(method.Name) }); - Controller = new BookController(null, bookService, null, null, null, + var authorService = Proxy((method, args) => method.Name == nameof(IAuthorService.GetAllAuthors) + ? Books.Select(book => book.Author).DistinctBy(author => author.Id).ToList() + : throw new AssertionException(method.Name)); + Controller = new BookController(authorService, bookService, null, null, null, Proxy((method, args) => new List()), Service, Proxy((method, args) => { @@ -220,6 +225,54 @@ public void title_slug_candidates_should_load_only_their_authors_once(bool postg Assert.That(harness.Results.Last().Select(stat => stat.AuthorId).Distinct(), Is.EqualTo(new[] { 1 })); } + [TestCase(false)] + [TestCase(true)] + public void facade_book_list_should_report_existing_files_independently_of_monitoring(bool postgres) + { + using var harness = new Harness(postgres); + var controller = harness.Controller; + var items = controller.HttpContext.Items; + var audio = new ReadarrFacadeContext("hc", "audiobook", "/audiobook"); + var ebook = new ReadarrFacadeContext("gr", "ebook", "/readarr/gr/ebook"); + harness.Books.Single(book => book.Id == 1).AudiobookMonitored = false; + harness.Connection.Execute("UPDATE \"Books\" SET \"AudiobookMonitored\" = FALSE WHERE \"Id\" = 1"); + + BookResource ReadBook(ReadarrFacadeContext facade, int id, string mediaType) + { + items[ReadarrFacadeContext.ItemKey] = facade; + return controller.GetBooks(null, null, null, mediaType: mediaType).Single(book => book.Id == id); + } + + var ownedAudio = ReadBook(audio, 1, "audiobook"); + var ownedEbook = ReadBook(ebook, 2, "ebook"); + items[ReadarrFacadeContext.ItemKey] = audio; + var wrongFormat = controller.GetBooks(null, new List { 2 }, null, mediaType: "audiobook").Single(); + Assert.Multiple(() => + { + Assert.That(ownedAudio.Statistics.BookFileCount, Is.EqualTo(2)); + Assert.That(ownedAudio.Statistics.BookCount, Is.Zero); + Assert.That(ownedAudio.Statistics.AvailableBookCount, Is.Zero); + Assert.That(ownedAudio.Statistics.TotalBookCount, Is.Zero); + Assert.That(ownedAudio.Statistics.PercentOfBooks, Is.EqualTo(100)); + Assert.That(ownedAudio.Monitored, Is.False); + Assert.That(ownedEbook.Statistics.PercentOfBooks, Is.EqualTo(100)); + Assert.That(ownedEbook.Monitored, Is.False); + Assert.That(wrongFormat.Statistics.PercentOfBooks, Is.Zero); + Assert.That(ReadBook(null, 1, "audiobook").Statistics.PercentOfBooks, Is.Zero); + }); + + harness.Connection.Execute("DELETE FROM \"BookFiles\" WHERE \"Id\" = 3"); + harness.Service.InvalidateAuthorCache(1); + Assert.That(ReadBook(ebook, 2, "ebook").Statistics.PercentOfBooks, Is.Zero); + Assert.That(ReadBook(audio, 1, "audiobook").Statistics.PercentOfBooks, Is.EqualTo(100)); + + harness.Connection.Execute("DELETE FROM \"BookFiles\" WHERE \"Id\" IN (1, 2)"); + harness.Service.InvalidateAuthorCache(1); + var missingAudio = ReadBook(audio, 1, "audiobook"); + Assert.That(missingAudio.Statistics.PercentOfBooks, Is.Zero); + Assert.That(missingAudio.Monitored, Is.False); + } + [TestCase(false, true)] [TestCase(true, true)] [TestCase(false, null)]