Skip to content

Commit

Permalink
Exclude duplicated chapters from auto download limit
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schroda committed Apr 1, 2024
1 parent a470e71 commit 3b0e179
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ import java.util.TreeSet
import java.util.concurrent.TimeUnit
import kotlin.math.max

private fun List<ChapterDataClass>.removeDuplicates(currentChapter: ChapterDataClass): List<ChapterDataClass> {
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 { }

Expand Down Expand Up @@ -359,12 +368,12 @@ object Chapter {
prevLatestChapterNumber: Float,
): List<Int> {
val reUploadedChapters = newChapters.filter { it.chapterNumber < prevLatestChapterNumber }
val actualNewChapters = newChapters.subtract(reUploadedChapters.toSet())
val actualNewChapters = newChapters.subtract(reUploadedChapters.toSet()).toList()
val chaptersToConsiderForDownloadLimit =
if (serverConfig.autoDownloadExcludeReUploadsFromLimit.value || serverConfig.autoDownloadIgnoreReUploads.value) {
actualNewChapters
actualNewChapters.removeDuplicates(actualNewChapters[0])
} else {
newChapters
newChapters.removeDuplicates(newChapters[0])
}.sortedBy { it.index }

val latestChapterToDownloadIndex =
Expand All @@ -374,11 +383,18 @@ object Chapter {
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 if (serverConfig.autoDownloadExcludeReUploadsFromLimit.value && !serverConfig.autoDownloadIgnoreReUploads.value) {
reUploadedChapters + limitedChaptersToDownload.subtract(reUploadedChapters.toSet())
reUploadedChapters + limitedChaptersToDownloadWithDuplicates.subtract(reUploadedChapters.toSet())
} else {
limitedChaptersToDownload
limitedChaptersToDownloadWithDuplicates
}.map { it.id }
}

Expand Down

0 comments on commit 3b0e179

Please sign in to comment.