Start inserting game mod logs into D1

This commit is contained in:
2024-03-20 15:43:16 -04:00
parent 4edf232d90
commit 8e5ff1f974
5 changed files with 56 additions and 107 deletions

View File

@ -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);