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, prevNumberOfChapters: Int,
updatedChapterList: List<ResultRow>, 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 // convert numbers to be index based
val currentNumberOfChapters = (prevNumberOfChapters - 1).coerceAtLeast(0) val newNumberOfChapters = updatedChapterList.size
val updatedNumberOfChapters = (updatedChapterList.size - 1).coerceAtLeast(0) val numberOfNewChapters = newNumberOfChapters - prevNumberOfChapters
val numberOfNewChapters = updatedNumberOfChapters - currentNumberOfChapters
val areNewChaptersAvailable = numberOfNewChapters > 0 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 // make sure to ignore initial fetch
val isDownloadPossible = val isDownloadPossible =
serverConfig.autoDownloadNewChapters.value && areNewChaptersAvailable && !wasInitialFetch serverConfig.autoDownloadNewChapters.value && areNewChaptersAvailable && !wasInitialFetch
if (!isDownloadPossible) { if (!isDownloadPossible) {
log.debug { "download is not allowed/possible" }
return return
} }
@@ -317,19 +327,20 @@ object Chapter {
// make sure to only consider the latest chapters. e.g. old unread chapters should be ignored // make sure to only consider the latest chapters. e.g. old unread chapters should be ignored
val latestReadChapterIndex = 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 = val unreadChapters =
updatedChapterList.subList(numberOfNewChapters, latestReadChapterIndex) updatedChapterList.subList(numberOfNewChapters, latestReadChapterIndex)
.filter { !it[ChapterTable.isRead] } .filter { !it[ChapterTable.isRead] }
val skipDueToUnreadChapters = serverConfig.excludeEntryWithUnreadChapters.value && unreadChapters.isNotEmpty() val skipDueToUnreadChapters = serverConfig.excludeEntryWithUnreadChapters.value && unreadChapters.isNotEmpty()
if (skipDueToUnreadChapters) { if (skipDueToUnreadChapters) {
log.debug { "ignore due to unread chapters" }
return return
} }
val firstChapterToDownloadIndex = val firstChapterToDownloadIndex =
if (serverConfig.autoDownloadAheadLimit.value > 0) { if (serverConfig.autoDownloadAheadLimit.value > 0) {
(numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value).coerceAtLeast(0) (numberOfNewChapters - serverConfig.autoDownloadAheadLimit.value - 1).coerceAtLeast(0)
} else { } else {
0 0
} }
@@ -340,10 +351,11 @@ object Chapter {
.map { it[ChapterTable.id].value } .map { it[ChapterTable.id].value }
if (chapterIdsToDownload.isEmpty()) { if (chapterIdsToDownload.isEmpty()) {
log.debug { "no chapters available for download" }
return return
} }
logger.info { "downloadNewChapters($mangaId): Downloading \"${chapterIdsToDownload.size}\" new chapter(s)..." } log.info { "download ${chapterIdsToDownload.size} new chapter(s)..." }
DownloadManager.enqueue(EnqueueInput(chapterIdsToDownload)) DownloadManager.enqueue(EnqueueInput(chapterIdsToDownload))
} }