ntfy is now directed through the backend

This commit is contained in:
nelle 2024-09-21 04:15:07 -06:00
parent 5626ff054c
commit 86e21e3b80
3 changed files with 39 additions and 115 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}
}

View file

@ -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,
@ -297,9 +204,3 @@ function sendNotification() {
ntfyAttach.value = "";
ntfyClickAction.value = "";
}
// none
else {
sendNone(ntfyEndpoint, ntfyMessage.value);
ntfyMessage.value = "";
}
}