From b5a92db9542cd0b77b9a9169b4884fc43dbccddc Mon Sep 17 00:00:00 2001 From: regalijan Date: Thu, 19 Oct 2023 16:51:00 -0400 Subject: [PATCH] Move most email sending to common email function --- functions/api/appeals/[id]/accept.ts | 31 ++++++++++++++-------------- functions/api/appeals/[id]/deny.ts | 24 ++++++++------------- functions/api/reports/[id]/action.ts | 13 ++++++++++++ 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/functions/api/appeals/[id]/accept.ts b/functions/api/appeals/[id]/accept.ts index b97f90d..6b1df9e 100644 --- a/functions/api/appeals/[id]/accept.ts +++ b/functions/api/appeals/[id]/accept.ts @@ -1,27 +1,20 @@ import { jsonError } from "../../../common.js"; +import sendEmail from "../../../email.js"; export async function onRequestPost(context: RequestContext) { const { appeal } = context.data; - const body = new FormData(); - body.append("from", "noreply@mail.carcrushers.cc"); - body.append("to", appeal.user.email); - body.append("subject", "Appeal Accepted"); - body.append("template", "appeal_accepted"); - body.append("v:note", context.data.body.feedback || "No note provided."); - - const emailReq = await fetch( - "https://api.mailgun.net/v3/mail.carcrushers.cc/messages", + const emailResponse = await sendEmail( + appeal.user.email, + context.env.MAILGUN_API_KEY, + "Appeal Accepted", + "appeal_accepted", { - body, - headers: { - authorization: `Basic ${btoa("api:" + context.env.MAILGUN_API_KEY)}`, - }, - method: "POST", + note: context.data.body.feedback || "No note provided.", }, ); - if (!emailReq.ok) { - console.log(await emailReq.json()); + if (!emailResponse.ok) { + console.log(await emailResponse.json()); return jsonError("Failed to accept appeal", 500); } @@ -31,6 +24,12 @@ export async function onRequestPost(context: RequestContext) { .bind(context.params.id) .run(); + delete appeal.user.email; + + await context.env.DATA.put(`appeal_${appeal.id}`, JSON.stringify(appeal), { + expirationTtl: 94608000, + }); + await fetch( `https://discord.com/api/v10/guilds/242263977986359297/bans/${appeal.id}`, { diff --git a/functions/api/appeals/[id]/deny.ts b/functions/api/appeals/[id]/deny.ts index e104158..b7f93c1 100644 --- a/functions/api/appeals/[id]/deny.ts +++ b/functions/api/appeals/[id]/deny.ts @@ -1,27 +1,21 @@ import { jsonError } from "../../../common.js"; +import sendEmail from "../../../email.js"; export async function onRequestPost(context: RequestContext) { const { appeal } = context.data; - const body = new FormData(); - body.append("from", "noreply@mail.carcrushers.cc"); - body.append("to", appeal.user.email); - body.append("subject", "Appeal Denied"); - body.append("template", "appeal_denied"); - body.append("v:note", context.data.body.feedback || "No note provided."); - const emailReq = await fetch( - "https://api.mailgun.net/v3/mail.carcrushers.cc/messages", + const emailResponse = await sendEmail( + appeal.user.email, + context.env.MAILGUN_API_KEY, + "Appeal Denied", + "appeal_denied", { - body, - headers: { - authorization: `Basic ${btoa("api:" + context.env.MAILGUN_API_KEY)}`, - }, - method: "POST", + note: context.data.body.feedback || "No note provided.", }, ); - if (!emailReq.ok) { - console.log(await emailReq.json()); + if (!emailResponse.ok) { + console.log(await emailResponse.json()); return jsonError("Failed to deny appeal", 500); } diff --git a/functions/api/reports/[id]/action.ts b/functions/api/reports/[id]/action.ts index cbdec69..8d0df0b 100644 --- a/functions/api/reports/[id]/action.ts +++ b/functions/api/reports/[id]/action.ts @@ -1,6 +1,7 @@ import { getBanList, setBanList } from "../../../roblox-open-cloud.js"; import { GetAccessToken, insertLogs } from "../../../gcloud.js"; import { jsonError } from "../../../common.js"; +import sendEmail from "../../../email.js"; export async function onRequestPost(context: RequestContext) { const reportId = context.params.id as string; @@ -14,6 +15,18 @@ export async function onRequestPost(context: RequestContext) { const actionMap = context.data.body; const newActions: { [k: string]: { BanType: number } } = {}; const logMap: { [k: string]: number } = {}; + const { user } = reportData as ReportCardProps & { user?: { email: string } }; + + if (user?.email) + await sendEmail( + user.email, + context.env.MAILGUN_API_KEY, + "Report Processed", + "report_processed", + { + username: reportData.user?.username as string, + }, + ); if (!Object.values(actionMap).find((action) => action !== 0)) { await context.env.DATA.delete(`report_${reportId}`);