Initial commit
This commit is contained in:
113
functions/api/appeals/submit.ts
Normal file
113
functions/api/appeals/submit.ts
Normal file
@ -0,0 +1,113 @@
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { learned, whyBanned, whyUnban } = context.data.body;
|
||||
|
||||
if (
|
||||
typeof learned !== "string" ||
|
||||
typeof whyBanned !== "string" ||
|
||||
typeof whyUnban !== "string" ||
|
||||
!learned.length ||
|
||||
learned.length > 2000 ||
|
||||
!whyBanned.length ||
|
||||
whyBanned.length > 500 ||
|
||||
!whyUnban.length ||
|
||||
whyUnban.length > 2000
|
||||
)
|
||||
return new Response(
|
||||
'{"error":"One or more fields are missing or invalid"}',
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
}
|
||||
);
|
||||
|
||||
const { current_user: currentUser } = context.data;
|
||||
|
||||
if (!currentUser.email)
|
||||
return new Response('{"error":"No email for this session"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
|
||||
const existingAppeals = await context.env.DATA.list({
|
||||
prefix: `appeal_${currentUser.id}`,
|
||||
});
|
||||
const existingBlockedAppeal = await context.env.DATA.get(
|
||||
`blockedappeal_${currentUser.id}`
|
||||
);
|
||||
|
||||
if (
|
||||
existingBlockedAppeal ||
|
||||
existingAppeals.keys.find(
|
||||
(appeal) => (appeal.metadata as { [k: string]: any })?.open
|
||||
)
|
||||
)
|
||||
return new Response('{"error":"Appeal already submitted"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 403,
|
||||
});
|
||||
|
||||
if (await context.env.DATA.get(`appealban_${currentUser.id}`)) {
|
||||
await context.env.DATA.put(`blockedappeal_${currentUser.id}`, "1", {
|
||||
metadata: { email: currentUser.email },
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
||||
|
||||
const appealId = `${currentUser.id}${Date.now()}${crypto
|
||||
.randomUUID()
|
||||
.replaceAll("-", "")}`;
|
||||
|
||||
await context.env.DATA.put(
|
||||
`appeal_${appealId}`,
|
||||
JSON.stringify({
|
||||
learned,
|
||||
user: currentUser,
|
||||
whyBanned,
|
||||
whyUnban,
|
||||
}),
|
||||
{
|
||||
expirationTtl: 94608000,
|
||||
metadata: {
|
||||
created_at: Date.now(),
|
||||
id: currentUser.id,
|
||||
open: true,
|
||||
tag: `${currentUser.id}#${currentUser.discriminator}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
await fetch(context.env.APPEALS_WEBHOOK, {
|
||||
body: JSON.stringify({
|
||||
embeds: [
|
||||
{
|
||||
title: "Appeal Submitted",
|
||||
color: 3756250,
|
||||
description: `View this appeal at https://carcrushers.cc/mod-queue?id=${appealId}&type=appeal`,
|
||||
fields: [
|
||||
{
|
||||
name: "Submitter",
|
||||
value: `${currentUser.username}#${currentUser.discriminator} (${currentUser.id})`,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
21
functions/api/appeals/toggle.ts
Normal file
21
functions/api/appeals/toggle.ts
Normal file
@ -0,0 +1,21 @@
|
||||
export async function onRequestPost(context: RequestContext) {
|
||||
const { active } = context.data.body;
|
||||
|
||||
if (typeof active !== "boolean")
|
||||
return new Response('{"error":"Active property must be a boolean"}', {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
status: 400,
|
||||
});
|
||||
|
||||
if (active) {
|
||||
await context.env.DATA.delete("appeal_disabled");
|
||||
} else {
|
||||
await context.env.DATA.put("appeal_disabled", "1");
|
||||
}
|
||||
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user