Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / BannedNote.kt
@Taehui Taehui on 29 Mar 1 KB 2024-03-29 오후 1:09
package net.taehui.twilight.system

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger
import java.io.IOException
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap

object BannedNote : Logger {
    private var bannedNoteStore = mutableMapOf<String, String>()

    fun loadBannedNote() {
        try {
            bannedNoteStore.putAll(
                ObjectMapper().readValue(
                    Paths.get("Banned Note.json").toFile().absoluteFile,
                    object : TypeReference<ConcurrentHashMap<String, String>>() {})
            )
            logInfo("Loaded Banned Note")
            saveBannedNote()
        } catch (e: IOException) {
            logFault(e)
        }
    }

    fun saveBannedNote() {
        ObjectMapper().writerWithDefaultPrettyPrinter()
            .writeValue(Paths.get("Banned Note.json").toFile().absoluteFile, bannedNoteStore)
        logInfo("Saved Banned Note")
    }

    fun putBannedNoteFile(noteID: String, commentary: String) {
        bannedNoteStore[noteID] = commentary
    }

    fun wipeBannedNoteFile(noteID: String) {
        bannedNoteStore.remove(noteID)
    }

    fun isBanned(noteID: String): Boolean {
        return bannedNoteStore.contains(noteID)
    }
}