Skip to content
Permalink
Newer
Older
100644 113 lines (102 sloc) 2.66 KB
October 19, 2023 16:49
1
export async function onRequestPost(context: RequestContext) {
2
const { learned, whyBanned, whyUnban } = context.data.body;
3
4
if (
5
typeof learned !== "string" ||
6
typeof whyBanned !== "string" ||
7
typeof whyUnban !== "string" ||
8
!learned.length ||
9
learned.length > 2000 ||
10
!whyBanned.length ||
11
whyBanned.length > 500 ||
12
!whyUnban.length ||
13
whyUnban.length > 2000
14
)
15
return new Response(
16
'{"error":"One or more fields are missing or invalid"}',
17
{
18
headers: {
19
"content-type": "application/json",
20
},
21
status: 400,
22
}
23
);
24
25
const { current_user: currentUser } = context.data;
26
27
if (!currentUser.email)
28
return new Response('{"error":"No email for this session"}', {
29
headers: {
30
"content-type": "application/json",
31
},
32
status: 403,
33
});
34
35
const existingAppeals = await context.env.DATA.list({
36
prefix: `appeal_${currentUser.id}`,
37
});
38
const existingBlockedAppeal = await context.env.DATA.get(
39
`blockedappeal_${currentUser.id}`
40
);
41
42
if (
43
existingBlockedAppeal ||
44
existingAppeals.keys.find(
45
(appeal) => (appeal.metadata as { [k: string]: any })?.open
46
)
47
)
48
return new Response('{"error":"Appeal already submitted"}', {
49
headers: {
50
"content-type": "application/json",
51
},
52
status: 403,
53
});
54
55
if (await context.env.DATA.get(`appealban_${currentUser.id}`)) {
56
await context.env.DATA.put(`blockedappeal_${currentUser.id}`, "1", {
57
metadata: { email: currentUser.email },
58
});
59
60
return new Response(null, {
61
status: 204,
62
});
63
}
64
65
const appealId = `${currentUser.id}${Date.now()}${crypto
66
.randomUUID()
67
.replaceAll("-", "")}`;
68
69
await context.env.DATA.put(
70
`appeal_${appealId}`,
71
JSON.stringify({
72
ban_reason: whyBanned,
73
createdAt: Date.now(),
October 19, 2023 16:49
74
learned,
October 19, 2023 16:49
75
id: appealId,
76
reason_for_unban: whyUnban,
77
user: {
78
email: currentUser.email,
79
id: currentUser.id,
80
username: currentUser.username,
81
},
October 19, 2023 16:49
82
}),
83
{
84
expirationTtl: 94608000,
85
}
86
);
87
88
await fetch(context.env.APPEALS_WEBHOOK, {
89
body: JSON.stringify({
90
embeds: [
91
{
92
title: "Appeal Submitted",
93
color: 3756250,
94
description: `View this appeal at https://carcrushers.cc/mod-queue?id=${appealId}&type=appeal`,
95
fields: [
96
{
97
name: "Submitter",
98
value: `${currentUser.username} (${currentUser.id})`,
October 19, 2023 16:49
99
},
100
],
101
},
102
],
103
}),
104
headers: {
105
"content-type": "application/json",
106
},
107
method: "POST",
108
});
109
110
return new Response(null, {
111
status: 204,
112
});
113
}