Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
KV to D1 migration (this is totally gonna break something)
  • Loading branch information
regalijan committed May 12, 2024
1 parent a2b3391 commit e00b7e8
Show file tree
Hide file tree
Showing 24 changed files with 3,045 additions and 1,879 deletions.
2 changes: 1 addition & 1 deletion app/routes/mod-queue.tsx
Expand Up @@ -196,7 +196,7 @@ export default function () {

try {
queueReq = await fetch(
`/api/mod-queue/list?before=${before}&showClosed=${show_closed}&type=${queueType}`,
`/api/mod-queue/${queueType}/list?before=${before}&showClosed=${show_closed}`,
);
} catch {
alert("Failed to load mod queue");
Expand Down
20 changes: 17 additions & 3 deletions functions/api/appeals/[id]/_middleware.ts
Expand Up @@ -20,11 +20,25 @@ export async function onRequestPost(context: RequestContext) {
context.data.targetId = id;

if (!pathname.endsWith("/ban")) {
const key = await context.env.DATA.get(`appeal_${id}`);
const appeal: Record<string, any> | null = await context.env.D1.prepare(
"SELECT * FROM appeals WHERE id = ?;",
)
.bind(id)
.first();

if (!key) return jsonError("No appeal with that ID exists", 404);
if (!appeal) return jsonError("No appeal with that ID exists", 404);

context.data.appeal = JSON.parse(key);
appeal.user = JSON.parse(appeal.user);
context.data.appeal = appeal;

const pushNotificationData = await context.env.D1.prepare(
"SELECT token FROM push_notifications WHERE event_id = ? AND event_type = 'appeal';",
)
.bind(id)
.first();

if (pushNotificationData)
context.data.fcm_token = pushNotificationData.token;
}

if (
Expand Down
48 changes: 23 additions & 25 deletions functions/api/appeals/[id]/accept.ts
Expand Up @@ -3,24 +3,30 @@ import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
const { appeal, fcm_token } = context.data;

if (appeal.fcm_token) {
if (fcm_token) {
await sendPushNotification(
context.env,
"Appeal Accepted",
context.data.body.feedback || "No additional details to display",
appeal.fcm_token
fcm_token,
);

await context.env.D1.prepare(
"DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'appeal';",
)
.bind(appeal.id)
.run();
} else {
const emailResponse = await sendEmail(
appeal.user.email,
context.env.MAILGUN_API_KEY,
"Appeal Accepted",
"appeal_accepted",
{
note: context.data.body.feedback || "No note provided."
}
note: context.data.body.feedback || "No note provided.",
},
);

if (!emailResponse.ok) {
Expand All @@ -32,29 +38,21 @@ export async function onRequestPost(context: RequestContext) {
const { current_user: currentUser } = context.data;

await context.env.D1.prepare(
"UPDATE appeals SET approved = 1, open = 0 WHERE id = ?;"
"UPDATE appeals SET approved = 1, user = json_remove(user, '$.email') WHERE id = ?;",
)
.bind(context.params.id)
.run();

delete appeal.fcm_token;
delete appeal.user.email;

appeal.open = false;

await context.env.DATA.put(`appeal_${appeal.id}`, JSON.stringify(appeal), {
expirationTtl: 94608000
});

await fetch(
`https://discord.com/api/v10/guilds/242263977986359297/bans/${appeal.user.id}`,
{
headers: {
authorization: `Bot ${context.env.BOT_TOKEN}`,
"x-audit-log-reason": `Appeal accepted by ${currentUser.username} (${currentUser.id})`
"x-audit-log-reason": `Appeal accepted by ${currentUser.username} (${currentUser.id})`,
},
method: "DELETE"
}
method: "DELETE",
},
);

await fetch(context.env.APPEALS_WEBHOOK, {
Expand All @@ -67,19 +65,19 @@ export async function onRequestPost(context: RequestContext) {
fields: [
{
name: "Moderator",
value: `${currentUser.username} (${currentUser.id})`
}
]
}
]
value: `${currentUser.username} (${currentUser.id})`,
},
],
},
],
}),
headers: {
"content-type": "application/json"
"content-type": "application/json",
},
method: "POST"
method: "POST",
});

return new Response(null, {
status: 204
status: 204,
});
}
23 changes: 10 additions & 13 deletions functions/api/appeals/[id]/deny.ts
Expand Up @@ -3,16 +3,22 @@ import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";

export async function onRequestPost(context: RequestContext) {
const { appeal } = context.data;
if (appeal.fcm_token) {
const { appeal, fcm_token } = context.data;
if (fcm_token) {
await sendPushNotification(
context.env,
"Appeal Denied",
`Unfortunately, we have decided to deny your appeal for the following reason: ${
context.data.body.feedback || "No additional details"
}`,
appeal.fcm_token,
fcm_token,
);

await context.env.D1.prepare(
"DELETE FROM push_notifications WHERE event_id = ? AND event_type = 'appeal';",
)
.bind(appeal.id)
.run();
} else {
const emailResponse = await sendEmail(
appeal.user.email,
Expand All @@ -31,22 +37,13 @@ export async function onRequestPost(context: RequestContext) {
}

await context.env.D1.prepare(
"UPDATE appeals SET approved = 0, open = 0 WHERE id = ?;",
"UPDATE appeals SET approved = 0, user = json_remove(user, '$.email') WHERE id = ?;",
)
.bind(context.params.id)
.run();

const { current_user: currentUser } = context.data;

delete appeal.user.email;
delete appeal.fcm_token;

appeal.open = false;

await context.env.DATA.put(`appeal_${appeal.id}`, JSON.stringify(appeal), {
expirationTtl: 94608000,
});

await fetch(context.env.APPEALS_WEBHOOK, {
body: JSON.stringify({
embeds: [
Expand Down
49 changes: 23 additions & 26 deletions functions/api/appeals/submit.ts
Expand Up @@ -41,18 +41,17 @@ export async function onRequestPost(context: RequestContext) {

if (!currentUser.email) return jsonError("No email for this session", 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,
(await context.env.D1.prepare(
"SELECT approved FROM appeals WHERE approved IS NULL AND json_extract(user, '$.id') = ?;",
)
.bind(currentUser.id)
.first())
)
return jsonError("Appeal already submitted", 403);

Expand All @@ -74,33 +73,31 @@ export async function onRequestPost(context: RequestContext) {
.randomUUID()
.replaceAll("-", "")}`;

await context.env.DATA.put(
`appeal_${appealId}`,
JSON.stringify({
ban_reason: whyBanned,
created_at: Date.now(),
fcm_token: typeof senderTokenId === "string" ? senderTokenId : undefined,
await context.env.D1.prepare(
"INSERT INTO appeals (ban_reason, created_at, id, learned, reason_for_unban, user) VALUES (?, ?, ?, ?, ?, ?);",
)
.bind(
whyBanned,
Date.now(),
appealId,
learned,
id: appealId,
open: true,
reason_for_unban: whyUnban,
user: {
whyUnban,
JSON.stringify({
email: currentUser.email,
id: currentUser.id,
username: currentUser.username,
},
}),
{
expirationTtl: 94608000,
},
);

await context.env.D1.prepare(
"INSERT INTO appeals (created_at, id, open, user) VALUES (?, ?, ?, ?)",
)
.bind(Date.now(), appealId, 1, currentUser.id)
}),
)
.run();

if (typeof senderTokenId === "string") {
await context.env.D1.prepare(
"INSERT INTO push_notifications (created_at, event_id, event_type, token) VALUES (?, ?, 'appeal', ?)",
)
.bind(Date.now(), appealId, senderTokenId)
.run();
}

await fetch(context.env.APPEALS_WEBHOOK, {
body: JSON.stringify({
embeds: [
Expand Down
15 changes: 8 additions & 7 deletions functions/api/game-appeals/[id]/accept.ts
Expand Up @@ -7,13 +7,14 @@ export async function onRequestPost(context: RequestContext) {
if (statsReduction && typeof statsReduction !== "number")
return jsonError("Invalid stat reduction", 400);

const appeal = await context.env.DATA.get(
`gameappeal_${context.params.id as string}`,
);
const appeal: Record<string, any> | null = await context.env.D1.prepare(
"SELECT * FROM game_appeals WHERE id = ?;",
)
.bind(context.params.id)
.first();

if (!appeal) return jsonError("Appeal not found", 400);

const data = JSON.parse(appeal);
const banList = (await getBanList(context)) as {
[k: string]: { BanType: number; Unbanned?: boolean; UnbanReduct?: number };
};
Expand All @@ -23,12 +24,12 @@ export async function onRequestPost(context: RequestContext) {
.bind(context.params.id)
.run();

if (!banList[data.roblox_id])
if (!banList[appeal.roblox_id])
return new Response(null, {
status: 204,
});

banList[data.roblox_id] = {
banList[appeal.roblox_id] = {
BanType: 0,
Unbanned: true,
UnbanReduct: statsReduction,
Expand All @@ -43,7 +44,7 @@ export async function onRequestPost(context: RequestContext) {
Date.now(),
context.data.current_user.id,
crypto.randomUUID(),
data.roblox_id,
appeal.roblox_id,
)
.run();

Expand Down
10 changes: 6 additions & 4 deletions functions/api/game-appeals/[id]/deny.ts
Expand Up @@ -3,18 +3,20 @@ import { jsonError } from "../../../common.js";
export async function onRequestPost(context: RequestContext) {
const appealId = context.params.id as string;

const appeal = await context.env.DATA.get(`gameappeal_${appealId}`);
const appeal = await context.env.D1.prepare(
"SELECT * FROM game_appeals WHERE id = ?;",
)
.bind(appealId)
.first();

if (!appeal) return jsonError("Appeal not found", 404);

const appealData = JSON.parse(appeal);

await context.env.DATA.delete(`gameappeal_${appealId}`);
await context.env.D1.prepare("DELETE FROM game_appeals WHERE id = ?;")
.bind(appealId)
.run();
await context.env.DATA.put(
`gameappealblock_${appealData.roblox_id}`,
`gameappealblock_${appeal.roblox_id}`,
`${Date.now() + 2592000000}`,
{ expirationTtl: 2592000 },
);
Expand Down
4 changes: 1 addition & 3 deletions functions/api/game-appeals/precheck.ts
Expand Up @@ -5,9 +5,7 @@ export default async function (
user: number,
): Promise<{ can_appeal?: boolean; error?: string; reason?: string }> {
if (
await context.env.D1.prepare(
"SELECT * FROM game_appeals WHERE open = 1 AND user = ?;",
)
await context.env.D1.prepare("SELECT * FROM game_appeals WHERE user = ?;")
.bind(user)
.first()
)
Expand Down
21 changes: 5 additions & 16 deletions functions/api/game-appeals/submit.ts
Expand Up @@ -43,25 +43,14 @@ export async function onRequestPost(context: RequestContext) {
if (!precheckData.can_appeal)
return jsonError(precheckData.reason as string, 400);

const appealId = `${id}${context.request.headers
.get("cf-ray")
?.split("-")[0]}${Date.now()}`;

await context.env.DATA.put(
`gameappeal_${appealId}`,
JSON.stringify({
id: appealId,
reasonForUnban,
roblox_id: id,
roblox_username: username,
whatHappened,
}),
);
const appealId = `${id}${
context.request.headers.get("cf-ray")?.split("-")[0]
}${Date.now()}`;

await context.env.D1.prepare(
"INSERT INTO game_appeals (created_at, id, open, user) VALUES (?, ?, ?, ?);",
"INSERT INTO game_appeals (created_at, id, reason_for_unban, roblox_id, roblox_username) VALUES (?, ?, ?, ?, ?);",
)
.bind(Date.now(), appealId, 1, id)
.bind(Date.now(), appealId, reasonForUnban, id, username)
.run();

await fetch(context.env.REPORTS_WEBHOOK, {
Expand Down

0 comments on commit e00b7e8

Please sign in to comment.