From e974f0f9d685d2780d3de0589a3aa5da28a84b68 Mon Sep 17 00:00:00 2001
From: Regalijan <r@regalijan.com>
Date: Mon, 30 Oct 2023 12:21:35 -0400
Subject: [PATCH] Make appeal ban deletion endpoint

---
 functions/api/appeals/[id]/ban.ts | 39 +++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/functions/api/appeals/[id]/ban.ts b/functions/api/appeals/[id]/ban.ts
index a04b996..ecbba34 100644
--- a/functions/api/appeals/[id]/ban.ts
+++ b/functions/api/appeals/[id]/ban.ts
@@ -1,5 +1,44 @@
 import { jsonError } from "../../../common.js";
 
+export async function onRequestDelete(context: RequestContext) {
+  const targetId = context.params.id as string;
+
+  if (targetId.search(/^\d{16.19}$/) === -1)
+    return jsonError("Invalid target id", 400);
+
+  await context.env.D1.prepare("DELETE FROM appeal_bans WHERE user = ?;")
+    .bind(targetId)
+    .run();
+
+  const { current_user: currentUser } = context.data;
+
+  await fetch(context.env.APPEALS_WEBHOOK, {
+    body: JSON.stringify({
+      embeds: [
+        {
+          title: "User Unblocked",
+          color: 3756250,
+          description: `User ${targetId} is no longer blocked from the appeal form.`,
+          fields: [
+            {
+              name: "Moderator",
+              value: `${currentUser.username} (${currentUser.id})`,
+            },
+          ],
+        },
+      ],
+    }),
+    headers: {
+      "content-type": "application/json",
+    },
+    method: "POST",
+  });
+
+  return new Response(null, {
+    status: 204,
+  });
+}
+
 export async function onRequestPost(context: RequestContext) {
   const { current_user: currentUser } = context.data;
   const targetId = context.params.id as string;