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
This commit is contained in:
schroda
2023-11-06 03:16:40 +01:00
committed by GitHub
parent db36896f92
commit 05bf4f5525

View File

@@ -298,18 +298,28 @@ object Chapter {
prevNumberOfChapters: Int,
updatedChapterList: List<ResultRow>,
) {
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))
}