Skip to content

Commit

Permalink
Fix/auto download new chapters initial fetch (#761)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
schroda authored Nov 6, 2023
1 parent db36896 commit 05bf4f5
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -298,38 +298,49 @@ 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
}

val newChapters = updatedChapterList.subList(0, numberOfNewChapters)

// 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
}
Expand All @@ -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))
}
Expand Down

0 comments on commit 05bf4f5

Please sign in to comment.