diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/CategoryMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/CategoryMutation.kt index d852fb46..50c15a27 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/CategoryMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/CategoryMutation.kt @@ -19,7 +19,7 @@ import suwayomi.tachidesk.manga.impl.Category import suwayomi.tachidesk.manga.impl.Category.DEFAULT_CATEGORY_ID import suwayomi.tachidesk.manga.impl.util.lang.isEmpty import suwayomi.tachidesk.manga.impl.util.lang.isNotEmpty -import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude import suwayomi.tachidesk.manga.model.table.CategoryMangaTable import suwayomi.tachidesk.manga.model.table.CategoryMetaTable import suwayomi.tachidesk.manga.model.table.CategoryTable @@ -85,7 +85,8 @@ class CategoryMutation { data class UpdateCategoryPatch( val name: String? = null, val default: Boolean? = null, - val includeInUpdate: IncludeInUpdate? = null, + val includeInUpdate: IncludeOrExclude? = null, + val includeInDownload: IncludeOrExclude? = null, ) data class UpdateCategoryPayload( @@ -136,6 +137,13 @@ class CategoryMutation { } } } + if (patch.includeInDownload != null) { + CategoryTable.update({ CategoryTable.id inList ids }) { update -> + patch.includeInDownload.also { + update[includeInDownload] = it.value + } + } + } } } @@ -229,7 +237,8 @@ class CategoryMutation { val name: String, val order: Int? = null, val default: Boolean? = null, - val includeInUpdate: IncludeInUpdate? = null, + val includeInUpdate: IncludeOrExclude? = null, + val includeInDownload: IncludeOrExclude? = null, ) data class CreateCategoryPayload( @@ -238,7 +247,7 @@ class CategoryMutation { ) fun createCategory(input: CreateCategoryInput): CreateCategoryPayload { - val (clientMutationId, name, order, default, includeInUpdate) = input + val (clientMutationId, name, order, default, includeInUpdate, includeInDownload) = input transaction { require(CategoryTable.select { CategoryTable.name eq input.name }.isEmpty()) { "'name' must be unique" @@ -271,6 +280,9 @@ class CategoryMutation { if (includeInUpdate != null) { it[CategoryTable.includeInUpdate] = includeInUpdate.value } + if (includeInDownload != null) { + it[CategoryTable.includeInDownload] = includeInDownload.value + } } Category.normalizeCategories() diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/CategoryType.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/CategoryType.kt index 2c9e99fd..dcc52b74 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/CategoryType.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/types/CategoryType.kt @@ -15,7 +15,7 @@ import suwayomi.tachidesk.graphql.server.primitives.Edge import suwayomi.tachidesk.graphql.server.primitives.Node import suwayomi.tachidesk.graphql.server.primitives.NodeList import suwayomi.tachidesk.graphql.server.primitives.PageInfo -import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude import suwayomi.tachidesk.manga.model.table.CategoryTable import java.util.concurrent.CompletableFuture @@ -24,14 +24,16 @@ class CategoryType( val order: Int, val name: String, val default: Boolean, - val includeInUpdate: IncludeInUpdate, + val includeInUpdate: IncludeOrExclude, + val includeInDownload: IncludeOrExclude, ) : Node { constructor(row: ResultRow) : this( row[CategoryTable.id].value, row[CategoryTable.order], row[CategoryTable.name], row[CategoryTable.isDefault], - IncludeInUpdate.fromValue(row[CategoryTable.includeInUpdate]), + IncludeOrExclude.fromValue(row[CategoryTable.includeInUpdate]), + IncludeOrExclude.fromValue(row[CategoryTable.includeInDownload]), ) fun mangas(dataFetchingEnvironment: DataFetchingEnvironment): CompletableFuture { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/controller/CategoryController.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/controller/CategoryController.kt index d96a6f5b..4aed4491 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/controller/CategoryController.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/controller/CategoryController.kt @@ -65,14 +65,15 @@ object CategoryController { formParam("name"), formParam("default"), formParam("includeInUpdate"), + formParam("includeInDownload"), documentWith = { withOperation { summary("Category modify") description("Modify a category") } }, - behaviorOf = { ctx, categoryId, name, isDefault, includeInUpdate -> - Category.updateCategory(categoryId, name, isDefault, includeInUpdate) + behaviorOf = { ctx, categoryId, name, isDefault, includeInUpdate, includeInDownload -> + Category.updateCategory(categoryId, name, isDefault, includeInUpdate, includeInDownload) ctx.status(200) }, withResults = { diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Category.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Category.kt index 0e04a2d1..9556a0b1 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Category.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Category.kt @@ -55,6 +55,7 @@ object Category { name: String?, isDefault: Boolean?, includeInUpdate: Int?, + includeInDownload: Int?, ) { transaction { CategoryTable.update({ CategoryTable.id eq categoryId }) { @@ -66,6 +67,7 @@ object Category { } if (categoryId != DEFAULT_CATEGORY_ID && isDefault != null) it[CategoryTable.isDefault] = isDefault if (includeInUpdate != null) it[CategoryTable.includeInUpdate] = includeInUpdate + if (includeInDownload != null) it[CategoryTable.includeInDownload] = includeInDownload } } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt index bdc613e7..d22c5ae4 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt @@ -37,6 +37,7 @@ import suwayomi.tachidesk.manga.impl.download.DownloadManager.EnqueueInput import suwayomi.tachidesk.manga.impl.track.Track import suwayomi.tachidesk.manga.impl.util.source.GetCatalogueSource.getCatalogueSourceOrStub import suwayomi.tachidesk.manga.model.dataclass.ChapterDataClass +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude import suwayomi.tachidesk.manga.model.dataclass.MangaChapterDataClass import suwayomi.tachidesk.manga.model.dataclass.PaginatedList import suwayomi.tachidesk.manga.model.dataclass.paginatedFrom @@ -49,6 +50,7 @@ import suwayomi.tachidesk.server.serverConfig import java.time.Instant import java.util.TreeSet import java.util.concurrent.TimeUnit +import kotlin.collections.listOf import kotlin.math.max object Chapter { @@ -309,20 +311,65 @@ object Chapter { ")", ) + if (!serverConfig.autoDownloadNewChapters.value) { + log.debug { "automatic download is not configured" } + return + } + + // Only download if there are new chapters, or if this is the first fetch val newNumberOfChapters = updatedChapterList.size val numberOfNewChapters = newNumberOfChapters - prevNumberOfChapters val areNewChaptersAvailable = numberOfNewChapters > 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" } + if (!areNewChaptersAvailable) { + log.debug { "no new chapters available" } return } + if (wasInitialFetch) { + log.debug { "skipping download on initial fetch" } + return + } + + // Verify the manga is configured to be downloaded based on it's categories. + var mangaCategories = CategoryManga.getMangaCategories(mangaId).toSet() + // if the manga has no categories, then it's implicitly in the default category + if (mangaCategories.isEmpty()) { + val defaultCategory = Category.getCategoryById(Category.DEFAULT_CATEGORY_ID) + if (defaultCategory != null) { + mangaCategories = setOf(defaultCategory) + } else { + log.warn { "missing default category" } + } + } + + if (mangaCategories.isNotEmpty()) { + var downloadCategoriesMap = Category.getCategoryList().groupBy { it.includeInDownload } + val unsetCategories = downloadCategoriesMap[IncludeOrExclude.UNSET].orEmpty() + // We only download if it's in the include list, and not in the exclude list. + // Use the unset categories as the included categories if the included categories is + // empty + val includedCategories = downloadCategoriesMap[IncludeOrExclude.INCLUDE].orEmpty().ifEmpty { unsetCategories } + val excludedCategories = downloadCategoriesMap[IncludeOrExclude.EXCLUDE].orEmpty() + // Only download manga that aren't in any excluded categories + val mangaExcludeCategories = mangaCategories.intersect(excludedCategories) + if (mangaExcludeCategories.isNotEmpty()) { + log.debug { "download excluded by categories: '${mangaExcludeCategories.joinToString("', '") { it.name }}'" } + return + } + val mangaDownloadCategories = mangaCategories.intersect(includedCategories) + if (mangaDownloadCategories.isNotEmpty()) { + log.debug { "download inluded by categories: '${mangaDownloadCategories.joinToString("', '") { it.name }}'" } + } else { + log.debug { "skipping download due to download categories configuration" } + return + } + } else { + log.debug { "no categories configured, skipping check for category download include/excludes" } + } + val newChapters = updatedChapterList.subList(0, numberOfNewChapters) // make sure to only consider the latest chapters. e.g. old unread chapters should be ignored diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt index 350e24ce..aa0eaec3 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/update/Updater.kt @@ -26,7 +26,7 @@ import suwayomi.tachidesk.manga.impl.CategoryManga import suwayomi.tachidesk.manga.impl.Chapter import suwayomi.tachidesk.manga.impl.Manga import suwayomi.tachidesk.manga.model.dataclass.CategoryDataClass -import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude import suwayomi.tachidesk.manga.model.dataclass.MangaDataClass import suwayomi.tachidesk.manga.model.table.MangaStatus import suwayomi.tachidesk.server.serverConfig @@ -222,9 +222,9 @@ class Updater : IUpdater { } val includeInUpdateStatusToCategoryMap = categories.groupBy { it.includeInUpdate } - val excludedCategories = includeInUpdateStatusToCategoryMap[IncludeInUpdate.EXCLUDE].orEmpty() - val includedCategories = includeInUpdateStatusToCategoryMap[IncludeInUpdate.INCLUDE].orEmpty() - val unsetCategories = includeInUpdateStatusToCategoryMap[IncludeInUpdate.UNSET].orEmpty() + val excludedCategories = includeInUpdateStatusToCategoryMap[IncludeOrExclude.EXCLUDE].orEmpty() + val includedCategories = includeInUpdateStatusToCategoryMap[IncludeOrExclude.INCLUDE].orEmpty() + val unsetCategories = includeInUpdateStatusToCategoryMap[IncludeOrExclude.UNSET].orEmpty() val categoriesToUpdate = if (forceAll) { categories @@ -277,6 +277,8 @@ class Updater : IUpdater { // In case no manga gets updated and no update job was running before, the client would never receive an info about its update request updateStatus(emptyList(), mangasToUpdate.isNotEmpty(), updateStatusCategories, skippedMangas) + logger.debug { "mangasToUpdate $mangasToUpdate" } + if (mangasToUpdate.isEmpty()) { return } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/model/dataclass/CategoryDataClass.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/model/dataclass/CategoryDataClass.kt index a4f3c799..0d6d131c 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/model/dataclass/CategoryDataClass.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/model/dataclass/CategoryDataClass.kt @@ -9,7 +9,7 @@ import com.fasterxml.jackson.annotation.JsonValue * 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/. */ -enum class IncludeInUpdate( +enum class IncludeOrExclude( @JsonValue val value: Int, ) { EXCLUDE(0), @@ -18,7 +18,7 @@ enum class IncludeInUpdate( ; companion object { - fun fromValue(value: Int) = IncludeInUpdate.values().find { it.value == value } ?: UNSET + fun fromValue(value: Int) = IncludeOrExclude.values().find { it.value == value } ?: UNSET } } @@ -28,6 +28,7 @@ data class CategoryDataClass( val name: String, val default: Boolean, val size: Int, - val includeInUpdate: IncludeInUpdate, + val includeInUpdate: IncludeOrExclude, + val includeInDownload: IncludeOrExclude, val meta: Map = emptyMap(), ) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/model/table/CategoryTable.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/model/table/CategoryTable.kt index 6ab21fe0..8bca5368 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/model/table/CategoryTable.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/model/table/CategoryTable.kt @@ -11,13 +11,14 @@ import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.ResultRow import suwayomi.tachidesk.manga.impl.Category import suwayomi.tachidesk.manga.model.dataclass.CategoryDataClass -import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude object CategoryTable : IntIdTable() { val name = varchar("name", 64) val order = integer("order").default(0) val isDefault = bool("is_default").default(false) - val includeInUpdate = integer("include_in_update").default(IncludeInUpdate.UNSET.value) + val includeInUpdate = integer("include_in_update").default(IncludeOrExclude.UNSET.value) + val includeInDownload = integer("include_in_download").default(IncludeOrExclude.UNSET.value) } fun CategoryTable.toDataClass(categoryEntry: ResultRow) = @@ -27,6 +28,7 @@ fun CategoryTable.toDataClass(categoryEntry: ResultRow) = categoryEntry[name], categoryEntry[isDefault], Category.getCategorySize(categoryEntry[id].value), - IncludeInUpdate.fromValue(categoryEntry[includeInUpdate]), + IncludeOrExclude.fromValue(categoryEntry[includeInUpdate]), + IncludeOrExclude.fromValue(categoryEntry[includeInDownload]), Category.getCategoryMetaMap(categoryEntry[id].value), ) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0026_CategoryIncludeInUpdate.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0026_CategoryIncludeInUpdate.kt index 02e2c15b..95996bc5 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0026_CategoryIncludeInUpdate.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0026_CategoryIncludeInUpdate.kt @@ -8,12 +8,12 @@ package suwayomi.tachidesk.server.database.migration * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import de.neonew.exposed.migrations.helpers.AddColumnMigration -import suwayomi.tachidesk.manga.model.dataclass.IncludeInUpdate +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude @Suppress("ClassName", "unused") class M0026_CategoryIncludeInUpdate : AddColumnMigration( "Category", "include_in_update", "INT", - IncludeInUpdate.UNSET.value.toString(), + IncludeOrExclude.UNSET.value.toString(), ) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0034_CategoryIncludeInDownload.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0034_CategoryIncludeInDownload.kt new file mode 100644 index 00000000..a83e0fee --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0034_CategoryIncludeInDownload.kt @@ -0,0 +1,19 @@ +package suwayomi.tachidesk.server.database.migration + +/* + * 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/. */ + +import de.neonew.exposed.migrations.helpers.AddColumnMigration +import suwayomi.tachidesk.manga.model.dataclass.IncludeOrExclude + +@Suppress("ClassName", "unused") +class M0034_CategoryIncludeInDownload : AddColumnMigration( + "Category", + "include_in_download", + "INT", + IncludeOrExclude.UNSET.value.toString(), +)