diff --git a/functions/api/game-appeals/[id]/accept.ts b/functions/api/game-appeals/[id]/accept.ts index 41710ae..b06a092 100644 --- a/functions/api/game-appeals/[id]/accept.ts +++ b/functions/api/game-appeals/[id]/accept.ts @@ -1,4 +1,3 @@ -import { insertLogs } from "../../../gcloud.js"; import { getBanList, setBanList } from "../../../roblox-open-cloud.js"; import { jsonError } from "../../../common.js"; @@ -35,11 +34,19 @@ export async function onRequestPost(context: RequestContext) { UnbanReduct: statsReduction, }; - await insertLogs( - { [data.roblox_id]: 4 }, - context.params.id as string, - context, - ); + await context.env.D1.prepare( + "INSERT INTO game_mod_logs (action, evidence, executed_at, executor, id, target) VALUES (?, ?, ?, ?, ?, ?);", + ) + .bind( + "accept_appeal", + `https://carcrushers.cc/mod-queue?id=${context.params.id}&type=gma`, + Date.now(), + context.data.current_user.id, + crypto.randomUUID(), + data.roblox_id, + ) + .run(); + await setBanList(context, banList); return new Response(null, { diff --git a/functions/api/game-bans/[user]/history.ts b/functions/api/game-bans/[user]/history.ts index 0d4db89..19b15b0 100644 --- a/functions/api/game-bans/[user]/history.ts +++ b/functions/api/game-bans/[user]/history.ts @@ -1,6 +1,5 @@ import { getBanList } from "../../../roblox-open-cloud.js"; import { jsonError, jsonResponse } from "../../../common.js"; -import { queryLogs } from "../../../gcloud.js"; export async function onRequestGet(context: RequestContext) { const robloxUserReq = await fetch( @@ -52,12 +51,13 @@ export async function onRequestGet(context: RequestContext) { else if (banData.BanType === 2) current_status = "Banned"; const response = { - history: (await queryLogs(users[0].id, context)).sort((a, b) => - a.entity.properties.executed_at.integerValue < - b.entity.properties.executed_at.integerValue - ? 1 - : -1, - ), + history: ( + await context.env.D1.prepare( + "SELECT * FROM game_mod_logs WHERE target = ? ORDER BY executed_at DESC;", + ) + .bind(users[0].id) + .all() + ).results, user: { avatar: thumbnailRequest.ok ? ( diff --git a/functions/api/game-bans/[user]/revoke.ts b/functions/api/game-bans/[user]/revoke.ts index 4f8f37d..b0e9f5e 100644 --- a/functions/api/game-bans/[user]/revoke.ts +++ b/functions/api/game-bans/[user]/revoke.ts @@ -1,5 +1,4 @@ import { getBanList, setBanList } from "../../../roblox-open-cloud.js"; -import { insertLogs } from "../../../gcloud.js"; import { jsonError } from "../../../common.js"; export async function onRequestPost(context: RequestContext) { @@ -19,7 +18,18 @@ export async function onRequestPost(context: RequestContext) { if (isNaN(parseInt(user))) return jsonError("Invalid user ID", 400); - await insertLogs({ [user]: 3 }, ticket_link, context); + await context.env.D1.prepare( + "INSERT INTO game_mod_logs (action, evidence, executed_at, executor, id, target) VALUES (?, ?, ?, ?, ?, ?);", + ) + .bind( + "revoke", + ticket_link, + Date.now(), + context.data.current_user.id, + crypto.randomUUID(), + parseInt(user), + ) + .run(); const banList = (await getBanList(context)) as { [k: string]: { BanType: number }; diff --git a/functions/api/reports/[id]/action.ts b/functions/api/reports/[id]/action.ts index f91e859..12f72e8 100644 --- a/functions/api/reports/[id]/action.ts +++ b/functions/api/reports/[id]/action.ts @@ -1,5 +1,4 @@ import { getBanList, setBanList } from "../../../roblox-open-cloud.js"; -import { insertLogs } from "../../../gcloud.js"; import { jsonError } from "../../../common.js"; import sendEmail from "../../../email.js"; import { sendPushNotification } from "../../../gcloud.js"; @@ -32,14 +31,35 @@ export async function onRequestPost(context: RequestContext) { } if (Object.values(logMap).length) { - await insertLogs(logMap, context.params.id as string, context); + 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 }; }; - Object.assign(banList, newActions); - await setBanList(context, banList); + await setBanList(context, Object.assign(banList, newActions)); } reportData.open = false; diff --git a/functions/gcloud.ts b/functions/gcloud.ts index aa2c194..70d656e 100644 --- a/functions/gcloud.ts +++ b/functions/gcloud.ts @@ -125,94 +125,6 @@ async function getKeyIDs( return ((await keyRequest.json()) as { keys: { [k: string]: any }[] }).keys; } -export async function insertLogs( - userActionMap: { [k: string]: number }, - reportId: string, - context: RequestContext, -) { - const accessToken = await GetAccessToken(context.env); - const actionBaseURLs: { [k: number]: string } = { - 1: "https://carcrushers.cc/mod-queue?type=report&id=", - 2: "https://carcrushers.cc/mod-queue?type=report&id=", - 3: "", - 4: "https://carcrushers.cc/mod-queue?type=gma&id=", - }; - const actionIntegers: { [k: number]: string } = { - 1: "blacklist", - 2: "ban", - 3: "revoke", - 4: "accept_appeal", - }; - const incompleteLogKey = { - partitionId: { - projectId: context.env.DATASTORE_PROJECT, - }, - path: [ - { - kind: "log", - }, - ], - }; - const payload: { mode: string; mutations: { [k: string]: any }[] } = { - mode: "NON_TRANSACTIONAL", - mutations: [], - }; - const preAllocatedLogKeys = []; - - while (preAllocatedLogKeys.length < Object.keys(userActionMap).length) - preAllocatedLogKeys.push(incompleteLogKey); - - const keys = await getKeyIDs( - accessToken, - context.env.DATASTORE_PROJECT, - preAllocatedLogKeys, - ); - - for (const [user, action] of Object.entries(userActionMap)) { - payload.mutations.push({ - insert: { - key: keys.pop(), - properties: { - action: { - stringValue: actionIntegers[action], - }, - evidence: { - stringValue: actionBaseURLs[action] + reportId, - }, - executed_at: { - integerValue: Date.now(), - }, - executor: { - stringValue: context.data.current_user.id, - }, - target: { - integerValue: user, - }, - }, - }, - }); - } - - const mutationRequest = await fetch( - `https://datastore.googleapis.com/v1/projects/${context.env.DATASTORE_PROJECT}:commit`, - { - body: JSON.stringify(payload), - headers: { - authorization: `Bearer ${accessToken}`, - "content-type": "application/json", - }, - method: "POST", - }, - ); - - if (!mutationRequest.ok) { - console.log(await mutationRequest.json()); - throw new Error("Failed to commit mutation"); - } - - return await mutationRequest.json(); -} - export async function queryLogs(user: number, context: RequestContext) { const accessToken = await GetAccessToken(context.env);