From c441eed84773fdc295e6d004e4f4628453b54659 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 7 Apr 2024 05:07:55 +0200 Subject: [PATCH] Exclude duplicated chapters from auto download limit (#923) In case the new chapters include duplicates from different scanlators, they would be included in the limit causing the auto download to potentially only download duplicated chapters while there might be more non duplicated chapters to download. Instead, the limit should only consider unique chapters and then should include all duplicates of the chapters that should get downloaded --- .../suwayomi/tachidesk/manga/impl/Chapter.kt | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt index 5de631d6..22e75f31 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt @@ -50,6 +50,15 @@ import java.util.TreeSet import java.util.concurrent.TimeUnit import kotlin.math.max +private fun List.removeDuplicates(currentChapter: ChapterDataClass): List { + return groupBy { it.chapterNumber } + .map { (_, chapters) -> + chapters.find { it.id == currentChapter.id } + ?: chapters.find { it.scanlator == currentChapter.scanlator } + ?: chapters.first() + } +} + object Chapter { private val logger = KotlinLogging.logger { } @@ -358,12 +367,12 @@ object Chapter { prevLatestChapterNumber: Float, ): List { val reUploadedChapters = newChapters.filter { it.chapterNumber < prevLatestChapterNumber } - val actualNewChapters = newChapters.subtract(reUploadedChapters.toSet()) + val actualNewChapters = newChapters.subtract(reUploadedChapters.toSet()).toList() val chaptersToConsiderForDownloadLimit = if (serverConfig.autoDownloadIgnoreReUploads.value) { - actualNewChapters + actualNewChapters.removeDuplicates(actualNewChapters[0]) } else { - newChapters + newChapters.removeDuplicates(newChapters[0]) }.sortedBy { it.index } val latestChapterToDownloadIndex = @@ -372,8 +381,16 @@ object Chapter { } else { serverConfig.autoDownloadNewChaptersLimit.value.coerceAtMost(chaptersToConsiderForDownloadLimit.size) } + val limitedChaptersToDownload = chaptersToConsiderForDownloadLimit.subList(0, latestChapterToDownloadIndex) + val limitedChaptersToDownloadWithDuplicates = + ( + limitedChaptersToDownload + + newChapters.filter { newChapter -> + limitedChaptersToDownload.find { it.chapterNumber == newChapter.chapterNumber } != null + } + ).toSet() - return chaptersToConsiderForDownloadLimit.subList(0, latestChapterToDownloadIndex).map { it.id } + return limitedChaptersToDownloadWithDuplicates.map { it.id } } fun modifyChapter(