Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-remove duplicate chapters #294

Merged
merged 3 commits into from
Feb 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.chapter.ChapterRecognition
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.SortOrder.ASC
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
Expand Down Expand Up @@ -101,14 +102,21 @@ object Chapter {
}
}

// clear any orphaned chapters that are in the db but not in `chapterList`
// clear any orphaned/duplicate chapters that are in the db but not in `chapterList`
val dbChapterCount = transaction { ChapterTable.select { ChapterTable.manga eq mangaId }.count() }
if (dbChapterCount > chapterCount) { // we got some clean up due
val dbChapterList = transaction { ChapterTable.select { ChapterTable.manga eq mangaId }.toList() }
val dbChapterList = transaction {
ChapterTable.select { ChapterTable.manga eq mangaId }.orderBy(ChapterTable.url to ASC).toList()
}
val chapterUrls = chapterList.map { it.url }.toSet()

dbChapterList.forEach { dbChapter ->
if (!chapterUrls.contains(dbChapter[ChapterTable.url])) {
for (i in dbChapterList.indices) {
AriaMoradi marked this conversation as resolved.
Show resolved Hide resolved
val dbChapter = dbChapterList[i]
AriaMoradi marked this conversation as resolved.
Show resolved Hide resolved

if (
!chapterUrls.contains(dbChapter[ChapterTable.url]) || // is orphaned
(i + 1 < dbChapterList.size && dbChapter[ChapterTable.url] == dbChapterList[i + 1][ChapterTable.url]) // is duplicate
AriaMoradi marked this conversation as resolved.
Show resolved Hide resolved
) {
transaction {
PageTable.deleteWhere { PageTable.chapter eq dbChapter[ChapterTable.id] }
ChapterTable.deleteWhere { ChapterTable.id eq dbChapter[ChapterTable.id] }
Expand Down