diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/SourceDataLoader.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/SourceDataLoader.kt new file mode 100644 index 00000000..13f476db --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/dataLoaders/SourceDataLoader.kt @@ -0,0 +1,62 @@ +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +package suwayomi.tachidesk.graphql.dataLoaders + +import com.expediagroup.graphql.dataloader.KotlinDataLoader +import org.dataloader.DataLoader +import org.dataloader.DataLoaderFactory +import org.jetbrains.exposed.sql.StdOutSqlLogger +import org.jetbrains.exposed.sql.addLogger +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.transactions.transaction +import suwayomi.tachidesk.graphql.types.SourceType +import suwayomi.tachidesk.manga.impl.Source +import suwayomi.tachidesk.manga.model.table.MangaTable +import suwayomi.tachidesk.server.JavalinSetup.future + +class SourceDataLoader : KotlinDataLoader { + override val dataLoaderName = "SourceDataLoader" + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { ids -> + future { + Source.getSourceList().filter { it.id in ids } + .map { SourceType(it) } + } + } +} + +class SourceForMangaDataLoader : KotlinDataLoader { + override val dataLoaderName = "SourceForMangaDataLoader" + override fun getDataLoader(): DataLoader = DataLoaderFactory.newDataLoader { ids -> + future { + transaction { + addLogger(StdOutSqlLogger) + val mangaSourceMap = MangaTable + .select { MangaTable.id inList ids } + .associate { it[MangaTable.id].value to it[MangaTable.sourceReference] } + + val sourceIds = mangaSourceMap + .values + .distinct() + .map { it.toString() } + + val sources = Source.getSourceList() + .filter { it.id in sourceIds } + .map { SourceType(it) } + .associateBy { it.id } + + val mangaSourceTypeMap = mangaSourceMap.mapValues { + sources[it.value] + } + + ids.map { + mangaSourceTypeMap[it] + } + } + } + } +} diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/SourceQuery.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/SourceQuery.kt new file mode 100644 index 00000000..edfb44d2 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/SourceQuery.kt @@ -0,0 +1,24 @@ +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +package suwayomi.tachidesk.graphql.queries + +import com.expediagroup.graphql.server.extensions.getValueFromDataLoader +import graphql.schema.DataFetchingEnvironment +import suwayomi.tachidesk.graphql.types.SourceType +import suwayomi.tachidesk.manga.impl.Source +import java.util.concurrent.CompletableFuture + +class SourceQuery { + fun source(dataFetchingEnvironment: DataFetchingEnvironment, id: Long): CompletableFuture { + return dataFetchingEnvironment.getValueFromDataLoader("SourceDataLoader", id) + } + + fun sources(): List { + return Source.getSourceList().map { SourceType(it) } + } +} diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskDataLoaderRegistryFactory.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskDataLoaderRegistryFactory.kt index 1186a1ce..639f6829 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskDataLoaderRegistryFactory.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskDataLoaderRegistryFactory.kt @@ -21,7 +21,9 @@ class TachideskDataLoaderRegistryFactory { MangaMetaDataLoader(), MangaForCategoryDataLoader(), CategoryMetaDataLoader(), - CategoriesForMangaDataLoader() + CategoriesForMangaDataLoader(), + SourceDataLoader(), + SourceForMangaDataLoader() ) } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt index c30a1af0..113afb4c 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/server/TachideskGraphQLSchema.kt @@ -17,6 +17,7 @@ import suwayomi.tachidesk.graphql.mutations.ChapterMutation import suwayomi.tachidesk.graphql.queries.CategoryQuery import suwayomi.tachidesk.graphql.queries.ChapterQuery import suwayomi.tachidesk.graphql.queries.MangaQuery +import suwayomi.tachidesk.graphql.queries.SourceQuery import suwayomi.tachidesk.graphql.subscriptions.DownloadSubscription import kotlin.reflect.KClass import kotlin.reflect.KType @@ -37,7 +38,8 @@ val schema = toSchema( queries = listOf( TopLevelObject(MangaQuery()), TopLevelObject(ChapterQuery()), - TopLevelObject(CategoryQuery()) + TopLevelObject(CategoryQuery()), + TopLevelObject(SourceQuery()) ), mutations = listOf( TopLevelObject(ChapterMutation()) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt index 1c1030ce..01e21816 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/MangaType.kt @@ -95,4 +95,8 @@ class MangaType( fun categories(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture> { return dataFetchingEnvironment.getValueFromDataLoader>("CategoriesForMangaDataLoader", id) } + + fun source(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture { + return dataFetchingEnvironment.getValueFromDataLoader("SourceForMangaDataLoader", id) + } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SourceType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SourceType.kt new file mode 100644 index 00000000..c157747d --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/SourceType.kt @@ -0,0 +1,39 @@ +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +package suwayomi.tachidesk.graphql.types + +import com.expediagroup.graphql.server.extensions.getValueFromDataLoader +import graphql.schema.DataFetchingEnvironment +import suwayomi.tachidesk.manga.model.dataclass.SourceDataClass +import java.util.concurrent.CompletableFuture + +class SourceType( + val id: Long, + val name: String, + val lang: String, + val iconUrl: String, + val supportsLatest: Boolean, + val isConfigurable: Boolean, + val isNsfw: Boolean, + val displayName: String +) { + constructor(source: SourceDataClass) : this( + id = source.id.toLong(), + name = source.name, + lang = source.lang, + iconUrl = source.iconUrl, + supportsLatest = source.supportsLatest, + isConfigurable = source.isConfigurable, + isNsfw = source.isNsfw, + displayName = source.displayName + ) + + fun manga(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture> { + return dataFetchingEnvironment.getValueFromDataLoader>("MangaForSourceDataLoader", id) + } +}