Skip to content
Permalink
Newer
Older
100644 85 lines (74 sloc) 2.1 KB
October 19, 2023 16:50
1
import { jsonError } from "../../../common.js";
2
import sendEmail from "../../../email.js";
October 19, 2023 16:51
3
import { sendPushNotification } from "../../../gcloud.js";
October 19, 2023 16:50
4
5
export async function onRequestPost(context: RequestContext) {
October 19, 2023 16:49
6
const { appeal } = context.data;
October 19, 2023 16:51
8
if (appeal.fcm_token) {
9
await sendPushNotification(
10
context.env,
11
"Appeal Accepted",
12
context.data.body.feedback || "No additional details to display",
13
appeal.fcm_token,
14
);
15
} else {
16
const emailResponse = await sendEmail(
17
appeal.user.email,
18
context.env.MAILGUN_API_KEY,
19
"Appeal Accepted",
20
"appeal_accepted",
21
{
22
note: context.data.body.feedback || "No note provided.",
23
},
24
);
25
26
if (!emailResponse.ok) {
27
console.log(await emailResponse.json());
28
return jsonError("Failed to accept appeal", 500);
29
}
30
}
31
32
const { current_user: currentUser } = context.data;
33
October 19, 2023 16:51
34
await context.env.D1.prepare(
35
"UPDATE appeals SET approved = 1, open = 0 WHERE id = ?;",
36
)
37
.bind(context.params.id)
38
.run();
39
40
delete appeal.fcm_token;
41
delete appeal.user.email;
42
October 20, 2023 12:35
43
appeal.open = false;
44
45
await context.env.DATA.put(`appeal_${appeal.id}`, JSON.stringify(appeal), {
46
expirationTtl: 94608000,
47
});
48
49
await fetch(
October 19, 2023 16:49
50
`https://discord.com/api/v10/guilds/242263977986359297/bans/${appeal.id}`,
51
{
52
headers: {
53
authorization: `Bot ${context.env.BOT_TOKEN}`,
54
"x-audit-log-reason": `Appeal accepted by ${currentUser.username} (${currentUser.id})`,
55
},
56
method: "DELETE",
October 19, 2023 16:50
57
},
58
);
59
60
await fetch(context.env.APPEALS_WEBHOOK, {
61
body: JSON.stringify({
62
embeds: [
63
{
64
title: "Appeal Accepted",
65
color: 0x00ff00,
October 19, 2023 16:50
66
description: `Appeal from user ${appeal.user.username} (${appeal.user.id}) was accepted.`,
67
fields: [
68
{
69
name: "Moderator",
70
value: `${currentUser.username} (${currentUser.id})`,
71
},
72
],
73
},
74
],
75
}),
76
headers: {
77
"content-type": "application/json",
78
},
79
method: "POST",
80
});
81
82
return new Response(null, {
83
status: 204,
84
});
85
}