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