Skip to content
Permalink
0fb7bf6686
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Latest commit 0fb7bf6 Mar 21, 2024 History
1 contributor

Users who have contributed to this file

99 lines (82 sloc) 2.86 KB
import { getBanList, setBanList } from "../../../roblox-open-cloud.js";
import { jsonError } from "../../../common.js";
import sendEmail from "../../../email.js";
import { sendPushNotification } from "../../../gcloud.js";
export async function onRequestPost(context: RequestContext) {
const reportId = context.params.id as string;
const reportData: (ReportCardProps & { fcm_token?: string }) | null =
await context.env.DATA.get(`report_${reportId}`, { type: "json" });
if (!reportData) return jsonError("Report does not exist", 404);
const actionMap = context.data.body;
const newActions: { [k: string]: { BanType: number } } = {};
const logMap: { [k: string]: number } = {};
const { user } = reportData as ReportCardProps & { user?: { email: string } };
for (const [user, action] of Object.entries(actionMap)) {
if (
isNaN(parseInt(user)) ||
typeof action !== "number" ||
action < 0 ||
action > 2
)
return jsonError("Invalid action map", 400);
if (action === 0) continue;
newActions[user] = { BanType: action };
logMap[user] = action;
}
if (Object.values(logMap).length) {
const batchedQueries = [];
const statement = context.env.D1.prepare(
"INSERT INTO game_mod_logs (action, evidence, executed_at, executor, id, target) VALUES (?, ?, ?, ?, ?, ?);",
);
const actionMap: { [k: number]: string } = {
1: "blacklist",
2: "ban",
};
for (const [k, v] of Object.entries(logMap)) {
if (v === 0) continue;
batchedQueries.push(
statement.bind(
actionMap[v],
`https://carcrushers.cc/mod-queue?type=report&id=${context.params.id}`,
Date.now(),
context.data.current_user.id,
crypto.randomUUID(),
parseInt(k),
),
);
}
await context.env.D1.batch(batchedQueries);
const banList = (await getBanList(context)) as {
[k: string]: { BanType: number };
};
await setBanList(context, Object.assign(banList, newActions));
}
reportData.open = false;
if (user?.email && !reportData.fcm_token)
await sendEmail(
user.email,
context.env.MAILGUN_API_KEY,
"Report Processed",
"report_processed",
{
username: reportData.user?.username as string,
},
);
else if (reportData.fcm_token)
await sendPushNotification(
context.env,
"Report Processed",
`Your report for ${reportData.target_usernames.toString()} has been reviewed.`,
reportData.fcm_token,
);
delete reportData.fcm_token;
delete (reportData.user as { email?: string; id: string; username: string })
?.email;
await context.env.DATA.put(`report_${reportId}`, JSON.stringify(reportData));
await context.env.D1.prepare("UPDATE reports SET open = 0 WHERE id = ?;")
.bind(reportId)
.run();
return new Response(null, {
status: 204,
});
}