Skip to content

Commit

Permalink
Merge pull request #72 from flytegg/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Dawsson committed Jul 1, 2024
2 parents 22c628f + 87307d2 commit c4a15f5
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .carbon/cli.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ id = "5218ae25"
file = "./plugin/build/libs/plugin-all.jar"

[watch_plugin]
file_name = "plugin-all.jar"
file_name = "PluginPortal-2.0.4-SNAPSHOT.jar"
directory = "./plugin/build/libs"
post_deploy_command = "reload confirm"
1 change: 0 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- master
- dev

jobs:
publish:
Expand Down
70 changes: 33 additions & 37 deletions common/src/main/kotlin/gg/flyte/pluginportal/common/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import gg.flyte.pluginportal.common.types.Plugin
import gg.flyte.pluginportal.common.util.GSON
import gg.flyte.pluginportal.common.util.Http.BASE_URL
import okhttp3.OkHttpClient
import okhttp3.Request
import java.net.URLEncoder

object API {
Expand All @@ -16,15 +17,25 @@ object API {
client.cache?.close()
}

private fun get(url: String, params: HashMap<String, String>): String {
val request = okhttp3.Request.Builder()
.url("$BASE_URL$url?" + params.map { "${it.key}=${it.value}" }.joinToString("&"))
.build()
private fun get(url: String, params: Map<String, String>): Pair<String, Int> {
val fullUrl = buildString {
append(BASE_URL)
append(url)
if (params.isNotEmpty()) {
append("?")
append(params.map { (key, value) ->
"${URLEncoder.encode(key, "UTF-8")}=${URLEncoder.encode(value, "UTF-8")}"
}.joinToString("&"))
}
}

val response = client.newCall(request).execute()
return response.body?.string() ?: ""
val request = Request.Builder().url(fullUrl).build()
return client.newCall(request).execute().use { response ->
(response.body?.string() ?: "") to response.code
}
}


private class AuthorisationException(code: Int)
: RuntimeException("Server returned code $code: You do not have permission to access this resource")

Expand All @@ -33,41 +44,26 @@ object API {
"PLEASE REPORT THIS TO THE PLUGIN PORTAL AUTHORS")

fun getPlugin(platform: MarketplacePlatform, id: String): Plugin? {
val response = get("/plugins/${platform.toString().lowercase()}/$id", hashMapOf()).ifEmpty {
return PluginRequestFailedException(platform, id).printStackTrace().let { null }
}
val statusCode: Int = response.substringAfter("\"statusCode\":", "200").slice(0..2).toInt()
when (statusCode) {
200 -> return GSON.fromJson(response, Plugin::class.java)
404 -> return null // not found
401,403 -> { // not authorised
AuthorisationException(statusCode).printStackTrace()
return null
}
400 -> { // bad request
IllegalArgumentException("Server returned code 400: The request for $id on $platform PLEASE REPORT THIS TO THE PLUGIN PORTAL AUTHORS")
.printStackTrace()
return null
}
else -> {
PluginRequestFailedException(platform, id).printStackTrace()
return null
}
}
val (response, code) = get("/plugins/${platform.toString().lowercase()}/$id", emptyMap())
return when (code) {
200 -> GSON.fromJson(response, Plugin::class.java)
404 -> null // not found
401, 403 -> null.also { AuthorisationException(code).printStackTrace() } // not authorised
400 -> null.also {
IllegalArgumentException("Server returned code 400: The request for $id on $platform PLEASE REPORT THIS TO THE PLUGIN PORTAL AUTHORS").printStackTrace()
} // bad request
else -> null.also { PluginRequestFailedException(platform, id).printStackTrace() }
}.takeIf { response.isNotEmpty() }
}

fun getPlugins(prefix: String? = null, limit: Int? = 50): List<Plugin> {
val params = hashMapOf<String, String>()

if (prefix != null) params["prefix"] = URLEncoder.encode(prefix, "UTF-8")
if (limit != null) params["limit"] = limit.toString()
val params = buildMap {
prefix?.let { put("prefix", it) }
limit?.let { put("limit", it.toString()) }
}

return GSON.fromJson(
get(
"/plugins",
params
), Array<Plugin>::class.java
).toList()
val (response, _) = get("/plugins", params)
return GSON.fromJson(response, Array<Plugin>::class.java).toList()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,12 @@ object SearchPlugins {
.build<String, List<Plugin>>()

fun search(query: String): List<Plugin> {
if (searchCache.asMap().containsKey(query)) {
return searchCache.getIfPresent(query)!!
}

val response = API.getPlugins(query).also {
searchCache.put(query, it)
}

return response
return searchCache.get(query) { API.getPlugins(query) }
}

fun getCachedSearch(query: String): List<Plugin>? {
searchCache.asMap().forEach { (key, value) ->
if (query.contains(key, ignoreCase = true)) {
return value
}
}

return null
return searchCache.asMap().entries
.firstOrNull { (key, _) -> query.contains(key, ignoreCase = true) }
?.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ data class Plugin(
@SerializedName("_id")
val id: String,
val name: String,
val platforms: MutableMap<MarketplacePlatform, PlatformPlugin>,
val platforms: Map<MarketplacePlatform, PlatformPlugin>,
) {

val highestPriorityPlatform get() = MarketplacePlatform.entries.find(platforms::containsKey) ?: platforms.keys.first()
val downloadableName get() = name.replace("/", "")
.replace("\\", "")

val downloadableName = name.replace(Regex("[/\\\\]"), "")


fun getFirstPlatform(): PlatformPlugin? = platforms.values.firstOrNull()

Expand All @@ -31,7 +33,9 @@ data class Plugin(
}
}

val totalDownloads: Int get() = platforms.values.sumOf { platform -> platform.downloads }
val totalDownloads by lazy {
platforms.values.sumOf { it.downloads }
}
}

data class PlatformPlugin(
Expand Down
84 changes: 42 additions & 42 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,50 @@ tasks {
val branch = rootProject.branchName()
val baseVersion = project.version as String
val isRelease = !baseVersion.contains('-')
val isMainBranch = branch == "master"
if (!isRelease || isMainBranch) { // Only publish releases from the main branch
val suffixedVersion = if (isRelease) baseVersion else baseVersion + "+" + System.getenv("GITHUB_RUN_NUMBER")
val changelogContent = if (isRelease) {
"See [GitHub](https://github.com/flytegg/plugin-portal) for release notes."
} else {
val commitHash = rootProject.latestCommitHash()
"[$commitHash](https://github.com/flytegg/plugin-portal/commit/$commitHash) ${rootProject.latestCommitMessage()}"
}
//val isMainBranch = branch == "master"
//if (!isRelease || isMainBranch) { // Only publish releases from the main branch
val suffixedVersion = if (isRelease) baseVersion else baseVersion + "+" + System.getenv("GITHUB_RUN_NUMBER")
val changelogContent = if (isRelease) {
"See [GitHub](https://github.com/flytegg/plugin-portal) for release notes."
} else {
val commitHash = rootProject.latestCommitHash()
"[$commitHash](https://github.com/flytegg/plugin-portal/commit/$commitHash) ${rootProject.latestCommitMessage()}"
}

modrinth {
val mcVersions: List<String> = (property("mcVersions") as String)
.split(",")
.map { it.trim() }
token.set(System.getenv("MODRINTH_TOKEN"))
projectId.set("pluginportal")
versionType.set(if (isRelease) "release" else if (isMainBranch) "beta" else "alpha")
versionNumber.set(suffixedVersion)
versionName.set(suffixedVersion)
changelog.set(changelogContent)
uploadFile.set(tasks.shadowJar.flatMap { it.archiveFile })
gameVersions.set(mcVersions)
loaders.add("bukkit")
loaders.add("spigot")
loaders.add("paper")
loaders.add("folia")
loaders.add("purpur")
autoAddDependsOn.set(false)
detectLoaders.set(false)
}
modrinth {
val mcVersions: List<String> = (property("mcVersions") as String)
.split(",")
.map { it.trim() }
token.set(System.getenv("MODRINTH_TOKEN"))
projectId.set("pluginportal")
versionType.set(if (isRelease) "release" else "beta")
versionNumber.set(suffixedVersion)
versionName.set(suffixedVersion)
changelog.set(changelogContent)
uploadFile.set(tasks.shadowJar.flatMap { it.archiveFile })
gameVersions.set(mcVersions)
loaders.add("bukkit")
loaders.add("spigot")
loaders.add("paper")
loaders.add("folia")
loaders.add("purpur")
autoAddDependsOn.set(false)
detectLoaders.set(false)
}

hangarPublish {
publications.register("plugin") {
version.set(suffixedVersion)
id.set("PluginPortal")
channel.set(if (isRelease) "Release" else if (isMainBranch) "Snapshot" else "Alpha")
changelog.set(changelogContent)
apiKey.set(System.getenv("HANGAR_API_KEY"))
platforms {
paper {
jar.set(tasks.shadowJar.flatMap { it.archiveFile })
platformVersions.set(listOf(property("mcVersionRange") as String))
}
hangarPublish {
publications.register("plugin") {
version.set(suffixedVersion)
id.set("PluginPortal")
channel.set(if (isRelease) "Release" else "Snapshot")
changelog.set(changelogContent)
apiKey.set(System.getenv("HANGAR_API_KEY"))
platforms {
paper {
jar.set(tasks.shadowJar.flatMap { it.archiveFile })
platformVersions.set(listOf(property("mcVersionRange") as String))
}
}
}
}
}
//}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gg.flyte.pluginportal.plugin.chat

import DefaultFontInfo
import gg.flyte.pluginportal.common.types.LocalPlugin
import gg.flyte.pluginportal.common.types.Plugin
import gg.flyte.pluginportal.plugin.util.format
Expand Down Expand Up @@ -92,7 +91,6 @@ fun sendPluginListMessage(audience: Audience, message: String, plugins: List<Plu

val platform = plugin.highestPriorityPlatform
val name = plugin.name.shortenToLine(23 + plugin.totalDownloads.format().pixelLength() + plugin.platformString.pixelLength())

audience.sendMessage(
textSecondary(" - ").appendPrimary("$name - ${plugin.totalDownloads.format()}")
.append(text("", AQUA, TextDecoration.UNDERLINED))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package gg.flyte.pluginportal.plugin.chat

enum class DefaultFontInfo(val character: Char, val length: Int) {

A('A', 5),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DumpSubCommand {
textSecondary(" Join our ")
.append(
text("Discord")
.clickEvent(ClickEvent.openUrl("https://discord.gg/flytegg"))
.clickEvent(ClickEvent.openUrl("https://discord.gg/flyte"))
.color(TextColor.fromHexString("#5865F2"))
.bold()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ import net.kyori.adventure.identity.Identity
import org.bukkit.command.CommandSender
import java.io.File
import java.util.logging.Level
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream

// TODO: After 1k entries move this to a zipped archive folder and create a new file
// TODO: Load & Read & make queryable
object PortalLogger {
private val file = File(PluginPortal.instance.dataFolder, "history.log").createIfNotExists()
private const val MAX_ENTRIES = 1000
private const val ARCHIVE_FOLDER = "archive"
private val dataFolder = PluginPortal.instance.dataFolder
private var currentFile = File(dataFolder, "history.log").apply { createNewFile() }
private var entryCount = 0

init {
loadExistingEntries()
}

fun info(action: Action, message: String) = log(Record(System.currentTimeMillis(), "SYSTEM", action, message))

Expand All @@ -31,7 +40,73 @@ object PortalLogger {
writeToFile(record)
}

private fun writeToFile(record: Record) = async { file.appendLine(record.description) }
private fun writeToFile(record: Record) {
currentFile.appendLine(record.description)
entryCount++

if (entryCount >= MAX_ENTRIES) {
archiveCurrentFile()
createNewFile()
}
}

private fun archiveCurrentFile() {
val archiveFolder = File(dataFolder, ARCHIVE_FOLDER).apply { mkdirs() }
val archiveFile = File(archiveFolder, "history_${System.currentTimeMillis()}.zip")

ZipOutputStream(archiveFile.outputStream()).use { zipOut ->
zipOut.putNextEntry(ZipEntry(currentFile.name))
currentFile.inputStream().use { input ->
input.copyTo(zipOut)
}
}

currentFile.delete()
}

private fun createNewFile() {
currentFile = File(dataFolder, "history.log").apply { createNewFile() }
entryCount = 0
}

private fun loadExistingEntries() {
entryCount = currentFile.readLines().size
}

fun query(filter: (Record) -> Boolean): List<Record> {
val records = mutableListOf<Record>()

// read current
currentFile.readLines().mapNotNull { parseRecord(it) }.filter(filter).let { records.addAll(it) }

// read from archive
File(dataFolder, ARCHIVE_FOLDER).listFiles { file -> file.extension == "zip" }?.forEach { zipFile ->
ZipFile(zipFile).use { zip ->
zip.entries().asSequence().forEach { entry ->
zip.getInputStream(entry).bufferedReader().useLines { lines ->
lines.mapNotNull { parseRecord(it) }.filter(filter).let { records.addAll(it) }
}
}
}
}

return records
}

private fun parseRecord(line: String): Record? {
val parts = line.split("|")
if (parts.size != 4) return null
return try {
Record(
parts[0].toLong(),
parts[1],
Action.valueOf(parts[2]),
parts[3]
)
} catch (e: Exception) {
null
}
}

enum class Action {
// Actions are queried linearly thus AUTO_UPDATE must precede UPDATE etc.
Expand Down
Loading

0 comments on commit c4a15f5

Please sign in to comment.