Permalink
Newer
100644
123 lines (111 sloc)
2.9 KB
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,
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(
46
)
47
)
48
return new Response('{"error":"Appeal already submitted"}', {
49
headers: {
50
"content-type": "application/json",
51
},
52
status: 403,
53
});
54
56
await context.env.D1.prepare("SELECT * FROM appeal_bans WHERE user = ?;")
57
.bind(currentUser.id)
58
.first()
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({
81
user: {
82
email: currentUser.email,
83
id: currentUser.id,
84
username: currentUser.username,
85
},
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
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",