Skip to content
Permalink
Newer
Older
100644 123 lines (111 sloc) 2.9 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,
October 19, 2023 16:50
22
},
October 19, 2023 16:49
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(
October 19, 2023 16:50
39
`blockedappeal_${currentUser.id}`,
October 19, 2023 16:49
40
);
41
42
if (
43
existingBlockedAppeal ||
44
existingAppeals.keys.find(
October 19, 2023 16:50
45
(appeal) => (appeal.metadata as { [k: string]: any })?.open,
October 19, 2023 16:49
46
)
47
)
48
return new Response('{"error":"Appeal already submitted"}', {
49
headers: {
50
"content-type": "application/json",
51
},
52
status: 403,
53
});
54
October 19, 2023 16:50
55
if (
56
await context.env.D1.prepare("SELECT * FROM appeal_bans WHERE user = ?;")
57
.bind(currentUser.id)
58
.first()
October 19, 2023 16:50
59
) {
October 19, 2023 16:49
60
await context.env.DATA.put(`blockedappeal_${currentUser.id}`, "1", {
61
metadata: { email: currentUser.email },
62
});
63
64
return new Response(null, {
65
status: 204,
66
});
67
}
68
69
const appealId = `${currentUser.id}${Date.now()}${crypto
70
.randomUUID()
71
.replaceAll("-", "")}`;
72
73
await context.env.DATA.put(
74
`appeal_${appealId}`,
75
JSON.stringify({
76
ban_reason: whyBanned,
October 19, 2023 16:50
77
created_at: Date.now(),
October 19, 2023 16:49
78
learned,
October 19, 2023 16:49
79
id: appealId,
80
reason_for_unban: whyUnban,
81
user: {
82
email: currentUser.email,
83
id: currentUser.id,
84
username: currentUser.username,
85
},
October 19, 2023 16:49
86
}),
87
{
88
expirationTtl: 94608000,
October 19, 2023 16:50
89
},
October 19, 2023 16:49
90
);
91
October 19, 2023 16:50
92
await context.env.D1.prepare(
93
"INSERT INTO appeals (created_at, id, open, user) VALUES (?, ?, ?, ?)",
94
)
95
.bind(Date.now(), appealId, 1, currentUser.id)
96
.run();
97
October 19, 2023 16:49
98
await fetch(context.env.APPEALS_WEBHOOK, {
99
body: JSON.stringify({
100
embeds: [
101
{
102
title: "Appeal Submitted",
103
color: 3756250,
104
description: `View this appeal at https://carcrushers.cc/mod-queue?id=${appealId}&type=appeal`,
105
fields: [
106
{
107
name: "Submitter",
108
value: `${currentUser.username} (${currentUser.id})`,
October 19, 2023 16:49
109
},
110
],
111
},
112
],
113
}),
114
headers: {
115
"content-type": "application/json",
116
},
117
method: "POST",
118
});
119
120
return new Response(null, {
121
status: 204,
122
});
123
}