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