Prevent greater last page read than page count (#938)

In case multiple chapters are getting updated, the last page read might be higher than the available pages of a chapter
This commit is contained in:
schroda
2024-04-28 06:34:40 +02:00
committed by GitHub
parent 668d5cf8f0
commit 729385588a

View File

@@ -1,11 +1,12 @@
package suwayomi.tachidesk.graphql.mutations package suwayomi.tachidesk.graphql.mutations
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.statements.BatchUpdateStatement
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import suwayomi.tachidesk.graphql.types.ChapterMetaType import suwayomi.tachidesk.graphql.types.ChapterMetaType
import suwayomi.tachidesk.graphql.types.ChapterType import suwayomi.tachidesk.graphql.types.ChapterType
import suwayomi.tachidesk.manga.impl.Chapter import suwayomi.tachidesk.manga.impl.Chapter
@@ -55,19 +56,34 @@ class ChapterMutation {
patch: UpdateChapterPatch, patch: UpdateChapterPatch,
) { ) {
transaction { transaction {
val chapterIdToPageCount =
if (patch.lastPageRead != null) {
ChapterTable
.slice(ChapterTable.id, ChapterTable.pageCount)
.select { ChapterTable.id inList ids }
.groupBy { it[ChapterTable.id].value }
.mapValues { it.value.firstOrNull()?.let { it[ChapterTable.pageCount] } }
} else {
emptyMap()
}
if (patch.isRead != null || patch.isBookmarked != null || patch.lastPageRead != null) { if (patch.isRead != null || patch.isBookmarked != null || patch.lastPageRead != null) {
val now = Instant.now().epochSecond val now = Instant.now().epochSecond
ChapterTable.update({ ChapterTable.id inList ids }) { update ->
patch.isRead?.also { BatchUpdateStatement(ChapterTable).apply {
update[isRead] = it ids.forEach { chapterId ->
} addBatch(EntityID(chapterId, ChapterTable))
patch.isBookmarked?.also { patch.isRead?.also {
update[isBookmarked] = it this[ChapterTable.isRead] = it
} }
patch.lastPageRead?.also { patch.isBookmarked?.also {
update[lastPageRead] = it this[ChapterTable.isBookmarked] = it
update[lastReadAt] = now }
patch.lastPageRead?.also {
this[ChapterTable.lastPageRead] = it.coerceAtMost(chapterIdToPageCount[it] ?: 0).coerceAtLeast(0)
this[ChapterTable.lastReadAt] = now
}
} }
execute(this@transaction)
} }
} }
} }