Skip to content

Commit

Permalink
feat: add addon hash table and return addon hash status from panel ge…
Browse files Browse the repository at this point in the history
…t plugins endpoint
  • Loading branch information
duruer committed Jun 1, 2024
1 parent 58e066e commit 8c91b24
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions Pano/src/main/kotlin/com/panomc/platform/db/dao/AddonHashDao.kt
Original file line number Diff line number Diff line change
@@ -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>(AddonHash::class.java) {
abstract suspend fun add(
addonHash: AddonHash,
sqlClient: SqlClient
): Long

abstract suspend fun byListOfHash(
hashList: List<String>,
sqlClient: SqlClient
): Map<String, AddonHash>
}
Original file line number Diff line number Diff line change
@@ -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<Row> = sqlClient
.preparedQuery(query)
.execute(
Tuple.of(
addonHash.hash,
addonHash.status
)
).await()

return rows.property(MySQLClient.LAST_INSERTED_ID)
}

override suspend fun byListOfHash(
hashList: List<String>,
sqlClient: SqlClient
): Map<String, AddonHash> {
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<Row> = sqlClient
.preparedQuery(query)
.execute()
.await()

val listOfAddonHash = mutableMapOf<String, AddonHash>()

rows.forEach { row ->
listOfAddonHash[row.getString(1)] = row.toEntity()
}

return listOfAddonHash
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -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<suspend (SqlClient) -> 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()
}

}
10 changes: 10 additions & 0 deletions Pano/src/main/kotlin/com/panomc/platform/db/model/AddonHash.kt
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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(
Expand All @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.panomc.platform.util

enum class AddonHashStatus {
VERIFIED,
NOT_VERIFIED,
UNKNOWN
}

0 comments on commit 8c91b24

Please sign in to comment.