Skip to content
Permalink
Newer
Older
100644 83 lines (73 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) {
6
const { appeal, fcm_token } = context.data;
October 19, 2023 16:51
9
await sendPushNotification(
10
context.env,
11
"Appeal Accepted",
12
context.data.body.feedback || "No additional details to display",
October 19, 2023 16:51
14
);
15
16
await context.env.D1.prepare(
17
"DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'appeal';",
18
)
19
.bind(appeal.id)
20
.run();
October 19, 2023 16:51
21
} else {
22
const emailResponse = await sendEmail(
23
appeal.user.email,
24
context.env.MAILGUN_API_KEY,
25
"Appeal Accepted",
26
"appeal_accepted",
27
{
28
note: context.data.body.feedback || "No note provided.",
29
},
October 19, 2023 16:51
30
);
31
32
if (!emailResponse.ok) {
33
console.log(await emailResponse.json());
34
return jsonError("Failed to accept appeal", 500);
35
}
36
}
37
38
const { current_user: currentUser } = context.data;
39
October 19, 2023 16:51
40
await context.env.D1.prepare(
41
"UPDATE appeals SET approved = 1, user = json_remove(user, '$.email') WHERE id = ?;",
October 19, 2023 16:51
42
)
43
.bind(context.params.id)
44
.run();
45
47
await fetch(
October 21, 2023 00:26
48
`https://discord.com/api/v10/guilds/242263977986359297/bans/${appeal.user.id}`,
49
{
50
headers: {
51
authorization: `Bot ${context.env.BOT_TOKEN}`,
52
"x-audit-log-reason": `Appeal accepted by ${currentUser.username} (${currentUser.id})`,
53
},
54
method: "DELETE",
55
},
56
);
57
58
await fetch(context.env.APPEALS_WEBHOOK, {
59
body: JSON.stringify({
60
embeds: [
61
{
62
title: "Appeal Accepted",
63
color: 0x00ff00,
October 19, 2023 16:50
64
description: `Appeal from user ${appeal.user.username} (${appeal.user.id}) was accepted.`,
65
fields: [
66
{
67
name: "Moderator",
68
value: `${currentUser.username} (${currentUser.id})`,
69
},
70
],
71
},
72
],
73
}),
74
headers: {
75
"content-type": "application/json",
76
},
78
});
79
80
return new Response(null, {
82
});
83
}