From 86e21e3b8024ec10257be7690b8f1becb3f3125d Mon Sep 17 00:00:00 2001 From: limepotato Date: Sat, 21 Sep 2024 04:15:07 -0600 Subject: [PATCH] ntfy is now directed through the backend --- .../nelleObserverBackend/plugins/Client.kt | 18 ++- .../nelleObserverBackend/plugins/Routing.kt | 7 + frontend/public/scripts/ntfy.js | 129 ++---------------- 3 files changed, 39 insertions(+), 115 deletions(-) diff --git a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Client.kt b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Client.kt index 3777c07..cf2b400 100644 --- a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Client.kt +++ b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Client.kt @@ -3,6 +3,7 @@ package observer.nelle.nelleObserverBackend.plugins import io.ktor.client.* +import io.ktor.client.request.* import io.ktor.client.request.forms.* import io.ktor.client.statement.* import io.ktor.http.* @@ -32,5 +33,20 @@ suspend fun makePost( } // Send a ntfy message -suspend fun ntfyMsg() { +suspend fun ntfyMsg( + client: HttpClient, + title: String, + message: String, + attach: String, + click: String, +) { + val message: HttpResponse = + client.post("https://ntfy.ouroboros.group/pushy") { + headers { + append("Title", title) + append("Attach", title) + append("Click", title) + } + setBody(message) + } } 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 5c6cad4..70b511e 100644 --- a/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt +++ b/backend/src/main/kotlin/observer/nelle/nelleObserverBackend/plugins/Routing.kt @@ -134,6 +134,13 @@ fun Application.configureRouting(client: HttpClient) { } post { val formParameters = call.receiveParameters() + val title = formParameters["Title"].toString() + val message = formParameters["Message"].toString() + val attach = formParameters["Attach"].toString() + val click = formParameters["Click"].toString() + call.response.status(HttpStatusCode(201, "Received...")) + call.respondText("Received:: Title: $title, Message: $message, Attachment: $attach, Click: $click") + ntfyMsg(client, title, message, attach, click) } } } diff --git a/frontend/public/scripts/ntfy.js b/frontend/public/scripts/ntfy.js index 8660109..e8780c9 100644 --- a/frontend/public/scripts/ntfy.js +++ b/frontend/public/scripts/ntfy.js @@ -1,5 +1,5 @@ // i deeply apologize for the horrors within... -const ntfyEndpoint = "https://ntfy.ouroboros.group/pushy"; +const ntfyEndpoint = "https://nelle.observer/api/ntfy"; // text fields and buttons const ntfyMessage = document.getElementById("ntfy-message"); @@ -34,7 +34,7 @@ const clickPFX = "click-action: "; // Ctrl + Enter to send document.addEventListener("keydown", (event) => { - if (event.ctrlKey && event.key == "Enter") { + if (event.ctrlKey && event.key === "Enter") { ntfyClick() } }); @@ -173,118 +173,25 @@ async function ntfyClick() { } } -// only message -function sendNone(endpoint, message) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.send(message); -} // send all function sendAll(endpoint, title, message, attachment, click) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Title", title); - r.setRequestHeader("Attach", attachment); - r.setRequestHeader("Click", click); - r.send(message); -} -// send title only -function sendTitle(endpoint, title, message) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Title", title); - r.send(message); -} -// send attachment only -function sendAttach(endpoint, message, attachment) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Attach", attachment); - r.send(message); -} -// send click only -function sendClick(endpoint, message, click) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Click", click); - r.send(message); -} -// send Title & Attachment -function sendTitleAttach(endpoint, title, message, attachment) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Title", title); - r.setRequestHeader("Attach", attachment); - r.send(message); -} -// send Title & Click -function sendTitleClick(endpoint, title, message, click) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Title", title); - r.setRequestHeader("Click", click); - r.send(message); -} -// send Attach & Click -function sendAttachClick(endpoint, message, attachment, click) { - const r = new XMLHttpRequest(); - r.open("POST", endpoint, true); - r.setRequestHeader("Content-Type", "text/plain"); - r.setRequestHeader("Attach", attachment); - r.setRequestHeader("Click", click); - r.send(message); + const xhr = new XMLHttpRequest(); + const formData = new FormData(); + + formData.append("Title", title); + formData.append("Message", message); + formData.append("Attach", attachment); + formData.append("Click", click); + + // Submit the data via fetch() + fetch(endpoint, { + method: "POST", + body: formData, + }); } // send notification function sendNotification() { - // title only - if (titleEnabled && !attachmentEnabled && !clickEnabled) { - sendTitle(ntfyTitle.value, ntfyMessage.value); - ntfyTitle.value = ""; - ntfyMessage.value = ""; - } - // attachment only - if (!titleEnabled && attachmentEnabled && !clickEnabled) { - sendAttach(ntfyEndpoint, ntfyMessage.value, ntfyAttach.value); - ntfyMessage.value = ""; - ntfyAttach.value = ""; - } - // click only - if (!titleEnabled && !attachmentEnabled && clickEnabled) { - sendClick(ntfyEndpoint, ntfyMessage.value, ntfyClickAction.value); - ntfyMessage.value = ""; - ntfyClickAction.value = ""; - } - // title & attachment - if (titleEnabled && attachmentEnabled && !clickEnabled) { - sendTitleAttach(ntfyEndpoint, ntfyTitle.value, ntfyMessage.value, ntfyAttach.value); - ntfyTitle.value = ""; - ntfyMessage.value = ""; - ntfyAttach.value = ""; - } - // title & click - if (titleEnabled && !attachmentEnabled && clickEnabled) { - sendTitleClick(ntfyEndpoint, ntfyTitle.value, ntfyMessage.value, ntfyClickAction.value); - ntfyTitle.value = ""; - ntfyMessage.value = ""; - ntfyClickAction.value = ""; - } - // attachment & click - if (!titleEnabled && attachmentEnabled && clickEnabled) { - sendAttachClick(ntfyEndpoint, ntfyMessage.value, ntfyAttach.value, ntfyClickAction.value); - ntfyAttach.value = ""; - ntfyMessage.value = ""; - ntfyClickAction.value = ""; - } - // all three - if (titleEnabled && attachmentEnabled && clickEnabled) { sendAll( ntfyEndpoint, ntfyTitle.value, @@ -296,10 +203,4 @@ function sendNotification() { ntfyMessage.value = ""; ntfyAttach.value = ""; ntfyClickAction.value = ""; - } - // none - else { - sendNone(ntfyEndpoint, ntfyMessage.value); - ntfyMessage.value = ""; - } }