fix: handle Komga tracks during manga migration (#6463)

* fix: handle Komga tracks during manga migration

closes #6354

* refactor: remove Komga direct reference
This commit is contained in:
Gauthier 2022-01-23 03:25:05 +08:00 committed by GitHub
parent 66a180bc36
commit cd7cf3583e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.data.track package eu.kanade.tachiyomi.data.track
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
@ -25,4 +26,14 @@ interface EnhancedTrackService {
* match is similar to TrackService.search, but only return zero or one match. * match is similar to TrackService.search, but only return zero or one match.
*/ */
suspend fun match(manga: Manga): TrackSearch? suspend fun match(manga: Manga): TrackSearch?
/**
* Checks whether the provided source/track/manga triplet is from this TrackService
*/
fun isTrackFrom(track: Track, manga: Manga, source: Source): Boolean
/**
* Migrates the given track for the manga to the newSource, if possible
*/
fun migrateTrack(track: Track, manga: Manga, newSource: Source): Track?
} }

View file

@ -10,6 +10,7 @@ import eu.kanade.tachiyomi.data.track.EnhancedTrackService
import eu.kanade.tachiyomi.data.track.NoLoginTrackService import eu.kanade.tachiyomi.data.track.NoLoginTrackService
import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.data.track.TrackService
import eu.kanade.tachiyomi.data.track.model.TrackSearch import eu.kanade.tachiyomi.data.track.model.TrackSearch
import eu.kanade.tachiyomi.source.Source
import okhttp3.Dns import okhttp3.Dns
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@ -103,4 +104,14 @@ class Komga(private val context: Context, id: Int) : TrackService(id), EnhancedT
} catch (e: Exception) { } catch (e: Exception) {
null null
} }
override fun isTrackFrom(track: Track, manga: Manga, source: Source): Boolean =
accept(source) && track.tracking_url == manga.url
override fun migrateTrack(track: Track, manga: Manga, newSource: Source): Track? =
if (accept(newSource)) {
track.also { track.tracking_url = manga.url }
} else {
null
}
} }

View file

@ -5,6 +5,8 @@ import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.MangaCategory
import eu.kanade.tachiyomi.data.database.models.toMangaInfo import eu.kanade.tachiyomi.data.database.models.toMangaInfo
import eu.kanade.tachiyomi.data.track.EnhancedTrackService
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
@ -19,6 +21,8 @@ import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchUI import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Date import java.util.Date
class SearchPresenter( class SearchPresenter(
@ -28,6 +32,8 @@ class SearchPresenter(
private val replacingMangaRelay = BehaviorRelay.create<Pair<Boolean, Manga?>>() private val replacingMangaRelay = BehaviorRelay.create<Pair<Boolean, Manga?>>()
private val enhancedServices by lazy { Injekt.get<TrackManager>().services.filterIsInstance<EnhancedTrackService>() }
override fun onCreate(savedState: Bundle?) { override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState) super.onCreate(savedState)
@ -57,6 +63,7 @@ class SearchPresenter(
} }
fun migrateManga(prevManga: Manga, manga: Manga, replace: Boolean) { fun migrateManga(prevManga: Manga, manga: Manga, replace: Boolean) {
val prevSource = sourceManager.get(prevManga.source) ?: return
val source = sourceManager.get(manga.source) ?: return val source = sourceManager.get(manga.source) ?: return
replacingMangaRelay.call(Pair(true, null)) replacingMangaRelay.call(Pair(true, null))
@ -66,7 +73,7 @@ class SearchPresenter(
val chapters = source.getChapterList(manga.toMangaInfo()) val chapters = source.getChapterList(manga.toMangaInfo())
.map { it.toSChapter() } .map { it.toSChapter() }
migrateMangaInternal(source, chapters, prevManga, manga, replace) migrateMangaInternal(prevSource, source, chapters, prevManga, manga, replace)
} catch (e: Throwable) { } catch (e: Throwable) {
withUIContext { view?.applicationContext?.toast(e.message) } withUIContext { view?.applicationContext?.toast(e.message) }
} }
@ -76,6 +83,7 @@ class SearchPresenter(
} }
private fun migrateMangaInternal( private fun migrateMangaInternal(
prevSource: Source,
source: Source, source: Source,
sourceChapters: List<SChapter>, sourceChapters: List<SChapter>,
prevManga: Manga, prevManga: Manga,
@ -135,12 +143,16 @@ class SearchPresenter(
// Update track // Update track
if (migrateTracks) { if (migrateTracks) {
val tracks = db.getTracks(prevManga).executeAsBlocking() val tracksToUpdate = db.getTracks(prevManga).executeAsBlocking().mapNotNull { track ->
for (track in tracks) {
track.id = null track.id = null
track.manga_id = manga.id!! track.manga_id = manga.id!!
val service = enhancedServices
.firstOrNull { it.isTrackFrom(track, prevManga, prevSource) }
if (service != null) service.migrateTrack(track, manga, source)
else track
} }
db.insertTracks(tracks).executeAsBlocking() db.insertTracks(tracksToUpdate).executeAsBlocking()
} }
// Update favorite status // Update favorite status