From 05bf4f552542053689ea491951c0afb1686e40b5 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 6 Nov 2023 03:16:40 +0100 Subject: [PATCH] Fix/auto download new chapters initial fetch (#761) * Fix automatic chapter download for initial chapter list fetch The initial fetch wasn't correctly detected which caused chapters to get downloaded. Using index based numbers also caused the first chapter to not get downloaded due to it being omitted in the "subList" call which excludes the "toIndex". * [Logging] Update logs --- .../suwayomi/tachidesk/manga/impl/Chapter.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 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 d67138f124..727a6596c0 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt @@ -298,18 +298,28 @@ object Chapter { prevNumberOfChapters: Int, updatedChapterList: List, ) { + val log = + KotlinLogging.logger( + "${logger.name}::downloadNewChapters(" + + "mangaId= $mangaId, " + + "prevNumberOfChapters= $prevNumberOfChapters, " + + "updatedChapterList= ${updatedChapterList.size}, " + + "downloadAheadLimit= ${serverConfig.autoDownloadAheadLimit.value}" + + ")", + ) + // convert numbers to be index based - val currentNumberOfChapters = (prevNumberOfChapters - 1).coerceAtLeast(0) - val updatedNumberOfChapters = (updatedChapterList.size - 1).coerceAtLeast(0) - val numberOfNewChapters = updatedNumberOfChapters - currentNumberOfChapters + val newNumberOfChapters = updatedChapterList.size + val numberOfNewChapters = newNumberOfChapters - prevNumberOfChapters val areNewChaptersAvailable = numberOfNewChapters > 0 - val wasInitialFetch = currentNumberOfChapters == -1 // has to be -1 - due to converting to index based 1 chapter will be 0 + val wasInitialFetch = prevNumberOfChapters == 0 // make sure to ignore initial fetch val isDownloadPossible = serverConfig.autoDownloadNewChapters.value && areNewChaptersAvailable && !wasInitialFetch if (!isDownloadPossible) { + log.debug { "download is not allowed/possible" } return } @@ -317,19 +327,20 @@ object Chapter { // make sure to only consider the latest chapters. e.g. old unread chapters should be ignored val latestReadChapterIndex = - updatedChapterList.indexOfFirst { it[ChapterTable.isRead] }.takeIf { it > -1 } ?: (updatedChapterList.size - 1) + updatedChapterList.indexOfFirst { it[ChapterTable.isRead] }.takeIf { it > -1 } ?: (updatedChapterList.size) val unreadChapters = updatedChapterList.subList(numberOfNewChapters, latestReadChapterIndex) .filter { !it[ChapterTable.isRead] } val skipDueToUnreadChapters = serverConfig.excludeEntryWithUnreadChapters.value && unreadChapters.isNotEmpty() if (skipDueToUnreadChapters) { + log.debug { "ignore due to unread chapters" } return } val firstChapterToDownloadIndex = if (serverConfig.autoDownloadAheadLimit.value > 0) { - (numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value).coerceAtLeast(0) + (numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value - 1).coerceAtLeast(0) } else { 0 } @@ -340,10 +351,11 @@ object Chapter { .map { it[ChapterTable.id].value } if (chapterIdsToDownload.isEmpty()) { + log.debug { "no chapters available for download" } return } - logger.info { "downloadNewChapters($mangaId): Downloading \"${chapterIdsToDownload.size}\" new chapter(s)..." } + log.info { "download ${chapterIdsToDownload.size} new chapter(s)..." } DownloadManager.enqueue(EnqueueInput(chapterIdsToDownload)) }