Skip to content

Commit

Permalink
improve(Suwayomi#733): fixes SChapter to ChapterDataClass,
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre JOURNET committed Oct 30, 2023
1 parent 923d212 commit 04cc440
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 58 deletions.
52 changes: 21 additions & 31 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Chapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ object Chapter {
suspend fun fetchChapterList(mangaId: Int): List<SChapter> {
val manga = getManga(mangaId)
val source = getCatalogueSourceOrStub(manga.sourceId.toLong())
val isHttpSource = source is HttpSource

val sManga =
SManga.create().apply {
title = manga.title
url = manga.url
}

val numberOfCurrentChapters = getCountOfMangaChapters(mangaId)
val chapterList = source.getChapterList(sManga)
val now = Instant.now().epochSecond
val chaptersInDb =
Expand All @@ -135,24 +135,14 @@ object Chapter {
val chapterEntry = chaptersInDb.find { it.url == fetchedChapter.url }

val chapterData =
if (isHttpSource) {
ChapterDataClass.fromSChapter(
fetchedChapter,
chapterEntry?.id ?: 0,
index + 1,
now,
mangaId,
(source as? HttpSource)?.getChapterUrl(fetchedChapter),
)
} else {
ChapterDataClass.fromSChapter(
fetchedChapter,
chapterEntry?.id ?: 0,
index + 1,
now,
mangaId,
)
}
ChapterDataClass.fromSChapter(
fetchedChapter,
chapterEntry?.id ?: 0,
index + 1,
now,
mangaId,
(source as? HttpSource)?.getChapterUrl(fetchedChapter),
)

if (chapterEntry == null) {
chaptersToInsert.add(chapterData)
Expand Down Expand Up @@ -211,19 +201,19 @@ object Chapter {
}

val chapterUrls = chapterList.map { it.url }.toSet()
val chaptersToDelete = mutableListOf<ResultRow>()

dbChapterList.forEachIndexed { index, dbChapter ->
val isOrphaned = !chapterUrls.contains(dbChapter[ChapterTable.url])
val isDuplicate =
index < dbChapterList.lastIndex && dbChapter[ChapterTable.url] == dbChapterList[index + 1][ChapterTable.url]

if (isOrphaned || isDuplicate) {
chaptersToDelete.add(dbChapter)
val chaptersIdsToDelete =
dbChapterList.mapIndexedNotNull { index, dbChapter ->
val isOrphaned = !chapterUrls.contains(dbChapter[ChapterTable.url])
val isDuplicate =
index < dbChapterList.lastIndex && dbChapter[ChapterTable.url] == dbChapterList[index + 1][ChapterTable.url]
val deleteChapter = isOrphaned || isDuplicate
if (deleteChapter) {
dbChapter[ChapterTable.id].value
} else {
null
}
}
}

val chaptersIdsToDelete = chaptersToDelete.map { it[ChapterTable.id] }

transaction {
PageTable.deleteWhere { PageTable.chapter inList chaptersIdsToDelete }
Expand All @@ -232,7 +222,7 @@ object Chapter {
}

if (manga.inLibrary) {
downloadNewChapters(mangaId, chapterList.size, newChapters)
downloadNewChapters(mangaId, numberOfCurrentChapters, newChapters)
}

return chapterList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,6 @@ data class ChapterDataClass(
val meta: Map<String, String> = emptyMap(),
) {
companion object {
fun fromSChapter(
sChapter: SChapter,
id: Int,
index: Int,
fetchedAt: Long,
mangaId: Int,
): ChapterDataClass {
return ChapterDataClass(
id = 0,
url = sChapter.url,
name = sChapter.name,
uploadDate = sChapter.date_upload,
chapterNumber = sChapter.chapter_number,
scanlator = sChapter.scanlator ?: "",
index = index,
fetchedAt = fetchedAt,
realUrl = null,
mangaId = mangaId,
read = false,
bookmarked = false,
lastPageRead = 0,
lastReadAt = 0,
downloaded = false,
)
}

fun fromSChapter(
sChapter: SChapter,
id: Int,
Expand All @@ -77,7 +51,7 @@ data class ChapterDataClass(
realUrl: String?,
): ChapterDataClass {
return ChapterDataClass(
id = 0,
id = id,
url = sChapter.url,
name = sChapter.name,
uploadDate = sChapter.date_upload,
Expand Down

0 comments on commit 04cc440

Please sign in to comment.