Refactor some code
authorLanius Trolling <lanius@laniustrolling.dev>
Sun, 3 Mar 2024 22:17:26 +0000 (17:17 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Sun, 3 Mar 2024 22:17:26 +0000 (17:17 -0500)
fontparser/build.gradle.kts
src/jvmMain/kotlin/info/mechyrdia/auth/session_storage.kt
src/jvmMain/kotlin/info/mechyrdia/data/comments.kt
src/jvmMain/kotlin/info/mechyrdia/data/data.kt
src/jvmMain/kotlin/info/mechyrdia/data/nations.kt
src/jvmMain/kotlin/info/mechyrdia/data/views_comment.kt
src/jvmMain/kotlin/info/mechyrdia/data/views_user.kt
src/jvmMain/kotlin/info/mechyrdia/data/visits.kt
src/jvmMain/kotlin/info/mechyrdia/lore/parser_toc.kt

index ac45f8c008668c749094de6a3f25ebfa0cb18bf2..db7da6f2ea3376ef5a1f9c44148e051e25c5eb54 100644 (file)
@@ -2,13 +2,6 @@ plugins {
        java
 }
 
-repositories {
-       mavenCentral()
-}
-
-dependencies {
-}
-
 java {
        toolchain {
                languageVersion.set(JavaLanguageVersion.of(17))
index ad09fb2c51d79c211d0ae9c14a520fa84a21bf8d..c8645d1c1577fe3d94b739ea4fde6f5acb1e38bd 100644 (file)
@@ -1,9 +1,6 @@
 package info.mechyrdia.auth
 
-import info.mechyrdia.data.DataDocument
-import info.mechyrdia.data.DocumentTable
-import info.mechyrdia.data.Id
-import info.mechyrdia.data.TableHolder
+import info.mechyrdia.data.*
 import io.ktor.server.sessions.*
 import kotlinx.serialization.SerialName
 import kotlinx.serialization.Serializable
@@ -24,7 +21,7 @@ object SessionStorageMongoDB : SessionStorage {
 
 @Serializable
 data class SessionStorageDoc(
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        override val id: Id<SessionStorageDoc>,
        val session: String
 ) : DataDocument<SessionStorageDoc> {
index 5ed70da1de709e875aba74435482a94f1ce24a07..f97817a81ca6b30c09b8e59768f34d6daf4f0a56 100644 (file)
@@ -10,7 +10,7 @@ import java.time.Instant
 
 @Serializable
 data class Comment(
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        override val id: Id<Comment>,
        
        val submittedBy: Id<NationData>,
@@ -42,7 +42,7 @@ data class Comment(
 
 @Serializable
 data class CommentReplyLink(
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        override val id: Id<CommentReplyLink> = Id(),
        
        val originalPost: Id<Comment>,
index 481e09c6544c33cf72ca429c0fee6a6d611736b2..7aefc798f0d67b8e70bdf09e0e2a7804d5d8650e 100644 (file)
@@ -88,7 +88,7 @@ object ConnectionHolder {
 }
 
 interface DataDocument<T : DataDocument<T>> {
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        val id: Id<T>
 }
 
index 4bb5f219e6cea12b693c99523467cf896a026b62..15c12fed7e9a7920d0341b8df4a13427f5ec30b5 100644 (file)
@@ -15,7 +15,7 @@ import java.util.concurrent.ConcurrentHashMap
 
 @Serializable
 data class NationData(
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        override val id: Id<NationData>,
        val name: String,
        val flag: String,
index 9cf308ce6bcaca3731f11d6efafca8e378e4bacf..35cedfc7ae9f3f372f0c8313c579b1316ecf72a2 100644 (file)
@@ -98,7 +98,7 @@ suspend fun ApplicationCall.newCommentRoute(): Nothing {
 }
 
 suspend fun ApplicationCall.viewCommentRoute(): Nothing {
-       val commentId = Id<Comment>(parameters["id"]!!)
+       val commentId = Id<Comment>(parameters.getOrFail("id"))
        
        val comment = Comment.Table.get(commentId)!!
        
@@ -116,7 +116,7 @@ suspend fun ApplicationCall.viewCommentRoute(): Nothing {
 }
 
 suspend fun ApplicationCall.editCommentRoute(): Nothing {
-       val commentId = Id<Comment>(parameters["id"]!!)
+       val commentId = Id<Comment>(parameters.getOrFail("id"))
        
        val oldComment = Comment.Table.get(commentId)!!
        
@@ -150,7 +150,7 @@ suspend fun ApplicationCall.editCommentRoute(): Nothing {
 private suspend fun ApplicationCall.getCommentForDeletion(): Pair<NationData, Comment> {
        val currNation = currentNation() ?: redirectWithError("/auth/login", "You must be logged in to delete comments")
        
-       val commentId = Id<Comment>(parameters["id"]!!)
+       val commentId = Id<Comment>(parameters.getOrFail("id"))
        val comment = Comment.Table.get(commentId)!!
        
        if (currNation.id != comment.submittedBy && currNation.id != OwnerNationId)
index 3d79c29060a0b6bb0f95a0b9c4f237b13187593d..eefd542d80ee28e7d287d48ff7145caa45ed39a9 100644 (file)
@@ -9,12 +9,13 @@ import info.mechyrdia.lore.page
 import info.mechyrdia.lore.redirect
 import info.mechyrdia.lore.standardNavBar
 import io.ktor.server.application.*
+import io.ktor.server.util.*
 import kotlinx.coroutines.flow.toList
 import kotlinx.html.*
 
 suspend fun ApplicationCall.userPage(): HTML.() -> Unit {
        val currNation = currentNation()
-       val viewingNation = nationCache.getNation(Id(parameters["id"]!!))
+       val viewingNation = nationCache.getNation(Id(parameters.getOrFail("id")))
        
        val comments = CommentRenderData(
                Comment.getCommentsBy(viewingNation.id).toList(),
@@ -53,7 +54,7 @@ suspend fun ApplicationCall.adminBanUserRoute(): Nothing {
        ownerNationOnly()
        verifyCsrfToken()
        
-       val bannedNation = nationCache.getNation(Id(parameters["id"]!!))
+       val bannedNation = nationCache.getNation(Id(parameters.getOrFail("id")))
        
        if (!bannedNation.isBanned)
                NationData.Table.set(bannedNation.id, Updates.set(NationData::isBanned.serialName, true))
@@ -65,7 +66,7 @@ suspend fun ApplicationCall.adminUnbanUserRoute(): Nothing {
        ownerNationOnly()
        verifyCsrfToken()
        
-       val bannedNation = nationCache.getNation(Id(parameters["id"]!!))
+       val bannedNation = nationCache.getNation(Id(parameters.getOrFail("id")))
        
        if (bannedNation.isBanned)
                NationData.Table.set(bannedNation.id, Updates.set(NationData::isBanned.serialName, false))
index 4ab2a91b7a71a73a18a681f7b443968e63386459..7c1de939222e2a77a0a5fcbbe7e4ed2424a9b6fd 100644 (file)
@@ -28,7 +28,7 @@ data class PageVisitTotals(
 
 @Serializable
 data class PageVisitData(
-       @SerialName("_id")
+       @SerialName(MONGODB_ID_KEY)
        override val id: Id<PageVisitData> = Id(),
        
        val path: String,
index bc582916550f08792c57b07c753c13559ecb8dc2..fda0e8630544c89a0d454c8f565fc1a7a36e5a2a 100644 (file)
@@ -96,7 +96,7 @@ enum class TextParserToCBuilderTag(val type: TextParserTagType<TableOfContentsBu
                }
        ),
        IMAGE(
-               TextParserTagType.Indirect(false) { param, content, builder ->
+               TextParserTagType.Indirect(false) { _, content, builder ->
                        builder.addImage(imagePathToOpenGraphValue(content))
                        ""
                }