From 5eb58a73ee9b99f4f629e1726ca63d66f230d20f Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Thu, 5 Aug 2021 06:39:09 +0430 Subject: [PATCH] check app update api closes #72 --- .../suwayomi/tachidesk/global/GlobalAPI.kt | 9 ++- .../tachidesk/global/impl/AppUpdate.kt | 57 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/global/impl/AppUpdate.kt diff --git a/server/src/main/kotlin/suwayomi/tachidesk/global/GlobalAPI.kt b/server/src/main/kotlin/suwayomi/tachidesk/global/GlobalAPI.kt index dbab69ca..8f87da39 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/global/GlobalAPI.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/global/GlobalAPI.kt @@ -2,6 +2,8 @@ package suwayomi.tachidesk.global import io.javalin.Javalin import suwayomi.tachidesk.global.impl.About +import suwayomi.tachidesk.global.impl.AppUpdate +import suwayomi.tachidesk.server.JavalinSetup /* * Copyright (C) Contributors to the Suwayomi project @@ -17,9 +19,12 @@ object GlobalAPI { ctx.json(About.getAbout()) } - // TODO: app update check api + // check for app updates app.get("/api/v1/settings/check-update/") { ctx -> - ctx.json(About.getAbout()) + + ctx.json( + JavalinSetup.future { AppUpdate.checkUpdate() } + ) } } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/global/impl/AppUpdate.kt b/server/src/main/kotlin/suwayomi/tachidesk/global/impl/AppUpdate.kt new file mode 100644 index 00000000..0579a3ef --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/global/impl/AppUpdate.kt @@ -0,0 +1,57 @@ +package suwayomi.tachidesk.global.impl + +import eu.kanade.tachiyomi.network.GET +import eu.kanade.tachiyomi.network.NetworkHelper +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive +import suwayomi.tachidesk.manga.impl.util.network.await +import uy.kohesive.injekt.injectLazy + +/* + * 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/. */ + +data class UpdateDataClass( + val channel: String, + val tag: String, + val url: String +) + +object AppUpdate { + const val LATEST_RELEASE_URL = "https://api.github.com/repos/Suwayomi/Tachidesk/releases/latest" + const val LATEST_PREVIEW_URL = "https://api.github.com/repos/Suwayomi/Tachidesk-preview/releases/latest" + + private val json: Json by injectLazy() + private val network: NetworkHelper by injectLazy() + + suspend fun checkUpdate(): List { + val releaseJson = json.parseToJsonElement( + network.client.newCall( + GET(LATEST_RELEASE_URL) + ).await().body!!.string() + ).jsonObject + + val previewJson = json.parseToJsonElement( + network.client.newCall( + GET(LATEST_PREVIEW_URL) + ).await().body!!.string() + ).jsonObject + + return listOf( + UpdateDataClass( + "Release", + releaseJson["tag_name"]!!.jsonPrimitive.content, + releaseJson["html_url"]!!.jsonPrimitive.content, + ), + UpdateDataClass( + "Preview", + previewJson["tag_name"]!!.jsonPrimitive.content, + previewJson["html_url"]!!.jsonPrimitive.content, + ), + ) + } +}