diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt index 83947ab8..bbdb6216 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt @@ -1,7 +1,9 @@ package suwayomi.tachidesk.graphql.mutations +import eu.kanade.tachiyomi.source.model.SChapter import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.batchInsert import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.transaction @@ -9,8 +11,12 @@ import org.jetbrains.exposed.sql.update import suwayomi.tachidesk.graphql.types.ChapterMetaType import suwayomi.tachidesk.graphql.types.ChapterType import suwayomi.tachidesk.manga.impl.Chapter +import suwayomi.tachidesk.manga.impl.util.lang.awaitSingle +import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource.getCatalogueSourceOrNull import suwayomi.tachidesk.manga.model.table.ChapterMetaTable import suwayomi.tachidesk.manga.model.table.ChapterTable +import suwayomi.tachidesk.manga.model.table.MangaTable +import suwayomi.tachidesk.manga.model.table.PageTable import suwayomi.tachidesk.server.JavalinSetup.future import java.time.Instant import java.util.concurrent.CompletableFuture @@ -183,4 +189,57 @@ class ChapterMutation { return DeleteChapterMetaPayload(clientMutationId, meta, chapter) } + + data class FetchChapterPagesInput( + val clientMutationId: String? = null, + val chapterId: Int + ) + data class FetchChapterPagesPayload( + val clientMutationId: String?, + val pages: List, + val chapter: ChapterType + ) + fun fetchChapterPages( + input: FetchChapterPagesInput + ): CompletableFuture { + val (clientMutationId, chapterId) = input + + val chapter = transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() } + val manga = transaction { MangaTable.select { MangaTable.id eq chapter[ChapterTable.manga] }.first() } + val source = getCatalogueSourceOrNull(manga[MangaTable.sourceReference])!! + + return future { + source.fetchPageList( + SChapter.create().apply { + url = chapter[ChapterTable.url] + name = chapter[ChapterTable.name] + } + ).awaitSingle() + }.thenApply { pageList -> + transaction { + PageTable.deleteWhere { PageTable.chapter eq chapterId } + PageTable.batchInsert(pageList) { page -> + this[PageTable.index] = page.index + this[PageTable.url] = page.url + this[PageTable.imageUrl] = page.imageUrl + this[PageTable.chapter] = chapterId + } + ChapterTable.update({ ChapterTable.id eq chapterId }) { + it[ChapterTable.pageCount] = pageList.size + } + } + + val mangaId = manga[MangaTable.id].value + val chapterIndex = chapter[ChapterTable.sourceOrder] + FetchChapterPagesPayload( + clientMutationId = clientMutationId, + pages = List(pageList.size) { index -> + "/api/v1/manga/$mangaId/chapter/$chapterIndex/page/$index" + }, + chapter = ChapterType( + transaction { ChapterTable.select { ChapterTable.id eq chapterId }.first() } + ) + ) + } + } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt index 9e0cc7f8..a8881bc2 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt @@ -83,8 +83,6 @@ private class ChapterForDownload( private fun updateDatabasePages(pageList: List) { val chapterId = chapterEntry[ChapterTable.id].value - val chapterIndex = chapterEntry[ChapterTable.sourceOrder] - val mangaId = chapterEntry[ChapterTable.manga].value transaction { pageList.forEach { page -> @@ -108,7 +106,7 @@ private class ChapterForDownload( } } - updatePageCount(pageList, mangaId, chapterIndex) + updatePageCount(pageList, chapterId) // chapter was updated chapterEntry = freshChapterEntry() @@ -116,13 +114,12 @@ private class ChapterForDownload( private fun updatePageCount( pageList: List, - mangaId: Int, - chapterIndex: Int + chapterId: Int ) { val pageCount = pageList.count() transaction { - ChapterTable.update({ (ChapterTable.manga eq mangaId) and (ChapterTable.sourceOrder eq chapterIndex) }) { + ChapterTable.update({ ChapterTable.id eq chapterId }) { it[ChapterTable.pageCount] = pageCount } }