Greatly reduce repeated code

This commit is contained in:
2023-10-19 16:50:48 -04:00
parent 47e639be43
commit dd2d9f2672
34 changed files with 196 additions and 481 deletions

View File

@ -1,3 +1,4 @@
import { jsonError, jsonResponse } from "../../../common.js";
import { queryLogs } from "../../../gcloud.js";
export async function onRequestGet(context: RequestContext) {
@ -17,26 +18,15 @@ export async function onRequestGet(context: RequestContext) {
if (!robloxUserReq.ok) {
console.log(await robloxUserReq.json());
return new Response('{"error":"Failed to resolve username"}', {
headers: {
"content-type": "application/json",
},
status: 500,
});
return jsonError("Failed to resolve username", 500);
}
const { data: users }: { data: { [k: string]: any }[] } =
await robloxUserReq.json();
if (!users.length)
return new Response('{"error":"No user found with that name"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (!users.length) return jsonError("No user found with that name", 400);
return new Response(
return jsonResponse(
JSON.stringify(
(await queryLogs(users[0].id, context)).sort((a, b) =>
a.entity.properties.executed_at.integerValue >
@ -45,10 +35,5 @@ export async function onRequestGet(context: RequestContext) {
: -1,
),
),
{
headers: {
"content-type": "application/json",
},
},
);
}

View File

@ -1,5 +1,6 @@
import { insertLogs } from "../../../gcloud.js";
import { getBanList, setBanList } from "../../../roblox-open-cloud.js";
import { insertLogs } from "../../../gcloud.js";
import { jsonError } from "../../../common.js";
export async function onRequestPost(context: RequestContext) {
const { ticket_link } = context.data.body;
@ -9,22 +10,11 @@ export async function onRequestPost(context: RequestContext) {
/^https?:\/\/carcrushers\.modmail\.dev\/logs\/[a-z\d]{12}$/,
)
)
return new Response('{"error":"Invalid ticket link provided"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
return jsonError("Invalid ticket link provided", 400);
const user = context.params.user as string;
if (isNaN(parseInt(user)))
return new Response('{"error":"Invalid user ID"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (isNaN(parseInt(user))) return jsonError("Invalid user ID", 400);
await insertLogs({ [user]: 3 }, ticket_link, context);

View File

@ -1,21 +1,11 @@
import { jsonError } from "../../common.js";
export async function onRequest(context: RequestContext) {
const { current_user: currentUser } = context.data;
if (!currentUser)
return new Response('{"error":Not logged in"}', {
headers: {
"content-type": "application/json",
},
status: 401,
});
if (!currentUser) return jsonError("Not logged in", 401);
if (!(currentUser.permissions & (1 << 5)))
return new Response('{"error":"Forbidden"}', {
headers: {
"content-type": "application/json",
},
status: 403,
});
if (!(currentUser.permissions & (1 << 5))) return jsonError("Forbidden", 403);
return await context.next();
}