From 303e4061c6f4c04bb61dbb28b8d92862728e01c7 Mon Sep 17 00:00:00 2001 From: limepotato Date: Sat, 21 Sep 2024 04:30:50 -0600 Subject: [PATCH] we have history babeeee! --- backend/.gitignore | 3 +- .../nelle/nelleObserverBackend/Application.kt | 3 ++ .../helpers/NtfyHistory.kt | 29 +++++++++++++++++++ .../nelleObserverBackend/plugins/Routing.kt | 2 ++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/kotlin/observer/nelle/nelleObserverBackend/helpers/NtfyHistory.kt diff --git a/backend/.gitignore b/backend/.gitignore index febebea..2699084 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -45,4 +45,5 @@ bin/ .DS_Store ### Other ### -bot.cfg \ No newline at end of file +bot.cfg +ntfyHistory.txt \ No newline at end of file diff --git a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/Application.kt b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/Application.kt index 3b033ed..5bf8ee1 100644 --- a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/Application.kt +++ b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/Application.kt @@ -14,6 +14,7 @@ import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.plugins.cors.routing.* import io.ktor.server.plugins.ratelimit.* import kotlinx.coroutines.runBlocking +import observer.nelle.nelleObserverBackend.helpers.checkCreate import observer.nelle.nelleObserverBackend.plugins.configureRouting import kotlin.time.Duration.Companion.minutes @@ -36,6 +37,8 @@ fun Application.module() { allowHeader(HttpHeaders.ContentType) anyHost() } + // check for ntfyhistory file, and if it doesnt exist, create it + checkCreate() runBlocking { val client = HttpClient(CIO) { diff --git a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/helpers/NtfyHistory.kt b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/helpers/NtfyHistory.kt new file mode 100644 index 0000000..c392169 --- /dev/null +++ b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/helpers/NtfyHistory.kt @@ -0,0 +1,29 @@ +package observer.nelle.nelleObserverBackend.helpers + +import observer.nelle.nelleObserverBackend.prop +import java.io.File +import java.io.FileInputStream +import java.nio.file.Files +import java.nio.file.Paths + +val historyPath = Paths.get("").toAbsolutePath().toString() +val historyFile = File("$historyPath/ntfyHistory.txt") + +fun checkCreate() { + if (historyFile.exists()) { + FileInputStream(historyFile).use { prop.load(it) } + } else { + Files.createFile(Paths.get("$historyPath/ntfyHistory.txt")) + } +} + +fun storeMessage( + title: String, + message: String, + attach: String, + click: String, +) { + val text = "Title: $title, Message: $message, Attach: $attach, Click: $click\n" + + historyFile.appendText(text, Charsets.UTF_8) +} diff --git a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt index 70b511e..975dfbf 100644 --- a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt +++ b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt @@ -12,6 +12,7 @@ import io.ktor.server.routing.* import observer.nelle.nelleObserverBackend.* import observer.nelle.nelleObserverBackend.helpers.getBeep import observer.nelle.nelleObserverBackend.helpers.getMeow +import observer.nelle.nelleObserverBackend.helpers.storeMessage import java.util.* import kotlin.concurrent.timerTask import kotlin.time.Duration.Companion.minutes @@ -140,6 +141,7 @@ fun Application.configureRouting(client: HttpClient) { val click = formParameters["Click"].toString() call.response.status(HttpStatusCode(201, "Received...")) call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click") + storeMessage(title, message, attach, click) ntfyMsg(client, title, message, attach, click) } }