Major changes

This commit is contained in:
Enstrayed
2024-08-21 17:26:23 -07:00
parent 8c2e39e88c
commit 05c7261c46
10 changed files with 90 additions and 176 deletions

View File

@@ -1,38 +1,22 @@
const { app, globalConfig } = require("../index.js") // Get globals from index
const { checkToken } = require("../liberals/auth.js")
const { logRequest } = require("../liberals/logging.js")
app.post("/sendemail", (rreq,rres) => {
app.post("/api/sendemail", (rreq,rres) => {
checkToken(rreq.get("Authorization"),"mailjet").then(authRes => {
if (authRes === false) { // If the supplied authorization is invalid or an error occured
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) // Log the request
if (authRes === false) {
rres.sendStatus(401)
} else if (authRes === true) { // If the authorization was valid, continue function
// 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever
if (rreq.body == undefined || rreq.body.recipient == undefined) {
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 400 KEY:${rreq.get("Authorization").split("_")[0]}`)
} else if (authRes === true) {
if (rreq.body == undefined || rreq.body.recipient == undefined) { // 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever
rres.sendStatus(400)
} else {
let message = {
"Messages": [
{
"From": {
"Email": globalConfig.mailjet.senderAddress
},
"To": [
{
"Email": rreq.body.recipient,
}
],
"Messages": [{
"From": { "Email": globalConfig.mailjet.senderAddress },
"To": [{ "Email": rreq.body.recipient, }],
"Subject": rreq.body.subject || "Request did not include a subject.",
"TextPart": rreq.body.message || "Request did not include a message.",
}
]
}]
}
fetch("https://api.mailjet.com/v3.1/send", {
@@ -44,17 +28,16 @@ app.post("/sendemail", (rreq,rres) => {
body: JSON.stringify(message)
}).then(fetchRes => {
if (fetchRes.status == 200) {
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 200 KEY:${rreq.get("Authorization").split("_")[1]}`)
logRequest(rres,rreq,200)
rres.sendStatus(200)
} else {
console.log(`Mailjet Fetch returned result other than OK: ${fetchRes.status} ${fetchRes.statusText}`)
logRequest(rres,rreq,500,`Mailjet fetch did not return OK: ${fetchRes.status} ${fetchRes.statusText}`)
rres.sendStatus(500)
}
})
}
}
})
})
module.exports = {app} // export routes to be imported by index for execution
module.exports = {app}