From 8c91b245fdfd93cfc6a5a05a3b79413c8e4ac8a4 Mon Sep 17 00:00:00 2001 From: kahverengi Date: Sat, 1 Jun 2024 18:15:22 +0300 Subject: [PATCH] feat: add addon hash table and return addon hash status from panel get plugins endpoint --- .../com/panomc/platform/db/DatabaseManager.kt | 3 +- .../panomc/platform/db/dao/AddonHashDao.kt | 17 ++++ .../db/implementation/AddonHashDaoImpl.kt | 81 +++++++++++++++++++ .../com/panomc/platform/db/migration/.gitkeep | 0 .../db/migration/DatabaseMigration1to2.kt | 36 +++++++++ .../com/panomc/platform/db/model/AddonHash.kt | 10 +++ .../route/api/panel/PanelGetPluginsAPI.kt | 14 +++- .../panomc/platform/util/AddonHashStatus.kt | 7 ++ 8 files changed, 165 insertions(+), 3 deletions(-) create mode 100755 Pano/src/main/kotlin/com/panomc/platform/db/dao/AddonHashDao.kt create mode 100755 Pano/src/main/kotlin/com/panomc/platform/db/implementation/AddonHashDaoImpl.kt delete mode 100644 Pano/src/main/kotlin/com/panomc/platform/db/migration/.gitkeep create mode 100644 Pano/src/main/kotlin/com/panomc/platform/db/migration/DatabaseMigration1to2.kt create mode 100755 Pano/src/main/kotlin/com/panomc/platform/db/model/AddonHash.kt create mode 100644 Pano/src/main/kotlin/com/panomc/platform/util/AddonHashStatus.kt diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/DatabaseManager.kt b/Pano/src/main/kotlin/com/panomc/platform/db/DatabaseManager.kt index 6bad6017..2409f9e6 100755 --- a/Pano/src/main/kotlin/com/panomc/platform/db/DatabaseManager.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/db/DatabaseManager.kt @@ -36,7 +36,8 @@ class DatabaseManager( @Lazy val websiteViewDao: WebsiteViewDao, @Lazy val tokenDao: TokenDao, @Lazy val notificationDao: NotificationDao, - @Lazy val serverPlayerDao: ServerPlayerDao + @Lazy val serverPlayerDao: ServerPlayerDao, + @Lazy val addonHashDao: AddonHashDao ) { @Autowired diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/dao/AddonHashDao.kt b/Pano/src/main/kotlin/com/panomc/platform/db/dao/AddonHashDao.kt new file mode 100755 index 00000000..a055bb5e --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/db/dao/AddonHashDao.kt @@ -0,0 +1,17 @@ +package com.panomc.platform.db.dao + +import com.panomc.platform.db.Dao +import com.panomc.platform.db.model.AddonHash +import io.vertx.sqlclient.SqlClient + +abstract class AddonHashDao : Dao(AddonHash::class.java) { + abstract suspend fun add( + addonHash: AddonHash, + sqlClient: SqlClient + ): Long + + abstract suspend fun byListOfHash( + hashList: List, + sqlClient: SqlClient + ): Map +} \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/implementation/AddonHashDaoImpl.kt b/Pano/src/main/kotlin/com/panomc/platform/db/implementation/AddonHashDaoImpl.kt new file mode 100755 index 00000000..564b4c95 --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/db/implementation/AddonHashDaoImpl.kt @@ -0,0 +1,81 @@ +package com.panomc.platform.db.implementation + +import com.panomc.platform.annotation.Dao +import com.panomc.platform.db.dao.AddonHashDao +import com.panomc.platform.db.model.AddonHash +import io.vertx.kotlin.coroutines.await +import io.vertx.mysqlclient.MySQLClient +import io.vertx.sqlclient.Row +import io.vertx.sqlclient.RowSet +import io.vertx.sqlclient.SqlClient +import io.vertx.sqlclient.Tuple + +@Dao +class AddonHashDaoImpl : AddonHashDao() { + + override suspend fun init(sqlClient: SqlClient) { + sqlClient + .query( + """ + CREATE TABLE IF NOT EXISTS `${getTablePrefix() + tableName}` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `hash` text NOT NULL, + `status` varchar(255) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Addon hash table.'; + """ + ) + .execute() + .await() + } + + override suspend fun add( + addonHash: AddonHash, + sqlClient: SqlClient + ): Long { + val query = + "INSERT INTO `${getTablePrefix() + tableName}` (`hash`, `status`) " + + "VALUES (?, ?)" + + val rows: RowSet = sqlClient + .preparedQuery(query) + .execute( + Tuple.of( + addonHash.hash, + addonHash.status + ) + ).await() + + return rows.property(MySQLClient.LAST_INSERTED_ID) + } + + override suspend fun byListOfHash( + hashList: List, + sqlClient: SqlClient + ): Map { + var listText = "" + + hashList.forEach { hash -> + if (listText == "") + listText = "'$hash'" + else + listText += ", '$hash'" + } + + val query = + "SELECT `id`, `hash`, `status` FROM `${getTablePrefix() + tableName}` where `hash` IN ($listText)" + + val rows: RowSet = sqlClient + .preparedQuery(query) + .execute() + .await() + + val listOfAddonHash = mutableMapOf() + + rows.forEach { row -> + listOfAddonHash[row.getString(1)] = row.toEntity() + } + + return listOfAddonHash + } +} \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/migration/.gitkeep b/Pano/src/main/kotlin/com/panomc/platform/db/migration/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/migration/DatabaseMigration1to2.kt b/Pano/src/main/kotlin/com/panomc/platform/db/migration/DatabaseMigration1to2.kt new file mode 100644 index 00000000..a5423f34 --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/db/migration/DatabaseMigration1to2.kt @@ -0,0 +1,36 @@ +package com.panomc.platform.db.migration + +import com.panomc.platform.annotation.Migration +import com.panomc.platform.db.DatabaseManager +import com.panomc.platform.db.DatabaseMigration +import io.vertx.kotlin.coroutines.await +import io.vertx.sqlclient.SqlClient + +@Migration +class DatabaseMigration1to2(databaseManager: DatabaseManager) : DatabaseMigration(databaseManager) { + override val FROM_SCHEME_VERSION = 1 + override val SCHEME_VERSION = 2 + override val SCHEME_VERSION_INFO = "Add addon_hash table" + + override val handlers: List Unit> = listOf( + deleteSecretKeyColumn() + ) + + private fun deleteSecretKeyColumn(): suspend (sqlClient: SqlClient) -> Unit = + { sqlClient: SqlClient -> + sqlClient + .query( + """ + CREATE TABLE IF NOT EXISTS `${getTablePrefix()}addon_hash` ( + `id` bigint NOT NULL AUTO_INCREMENT, + `hash` text NOT NULL, + `status` varchar(255) NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Addon hash table.'; + """.trimIndent() + ) + .execute() + .await() + } + +} \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/db/model/AddonHash.kt b/Pano/src/main/kotlin/com/panomc/platform/db/model/AddonHash.kt new file mode 100755 index 00000000..69b81b5a --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/db/model/AddonHash.kt @@ -0,0 +1,10 @@ +package com.panomc.platform.db.model + +import com.panomc.platform.db.DBEntity +import com.panomc.platform.util.AddonHashStatus + +data class AddonHash( + val id: Long = -1, + val hash: String, + val status: AddonHashStatus, +) : DBEntity() \ No newline at end of file diff --git a/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelGetPluginsAPI.kt b/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelGetPluginsAPI.kt index 4663168b..4792362d 100755 --- a/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelGetPluginsAPI.kt +++ b/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelGetPluginsAPI.kt @@ -4,7 +4,9 @@ import com.panomc.platform.PanoPluginDescriptor import com.panomc.platform.PanoPluginWrapper import com.panomc.platform.PluginManager import com.panomc.platform.annotation.Endpoint +import com.panomc.platform.db.DatabaseManager import com.panomc.platform.model.* +import com.panomc.platform.util.AddonHashStatus import com.panomc.platform.util.AddonStatusType import io.vertx.ext.web.RoutingContext import io.vertx.ext.web.validation.ValidationHandler @@ -19,6 +21,7 @@ import java.io.StringWriter @Endpoint class PanelGetPluginsAPI( + private val databaseManager: DatabaseManager, private val pluginManager: PluginManager ) : PanelApi() { override val paths = listOf(Path("/api/panel/plugins", RouteType.GET)) @@ -43,10 +46,16 @@ class PanelGetPluginsAPI( AddonStatusType.ACTIVE -> pluginManager.plugins.filter { it.pluginState == PluginState.STARTED } AddonStatusType.DISABLED -> pluginManager.plugins.filter { it.pluginState != PluginState.STARTED } else -> pluginManager.plugins - } + }.map { it as PanoPluginWrapper } + + val hashList = plugins.map { it.hash } + + val sqlClient = getSqlClient() + + val addonHashes = databaseManager.addonHashDao.byListOfHash(hashList, sqlClient) val result = mutableMapOf( - "plugins" to plugins.map { it as PanoPluginWrapper }.map { + "plugins" to plugins.map { val panoPluginDescriptor = it.descriptor as PanoPluginDescriptor mapOf( @@ -59,6 +68,7 @@ class PanelGetPluginsAPI( "license" to panoPluginDescriptor.license, "error" to if (it.failedException == null) null else getStackTraceAsString(it.failedException), "hash" to it.hash, + "verifyStatus" to if (addonHashes[it.hash] == null) AddonHashStatus.UNKNOWN else addonHashes[it.hash]!!.status, "sourceUrl" to panoPluginDescriptor.sourceUrl ) } diff --git a/Pano/src/main/kotlin/com/panomc/platform/util/AddonHashStatus.kt b/Pano/src/main/kotlin/com/panomc/platform/util/AddonHashStatus.kt new file mode 100644 index 00000000..444a9467 --- /dev/null +++ b/Pano/src/main/kotlin/com/panomc/platform/util/AddonHashStatus.kt @@ -0,0 +1,7 @@ +package com.panomc.platform.util + +enum class AddonHashStatus { + VERIFIED, + NOT_VERIFIED, + UNKNOWN +} \ No newline at end of file