Use d1 for listing in mod queue

This commit is contained in:
regalijan 2023-10-19 16:49:30 -04:00
parent d659a8122f
commit e51452a85e
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -1,12 +1,17 @@
export async function onRequestGet(context: RequestContext) { export async function onRequestGet(context: RequestContext) {
const { searchParams } = new URL(context.request.url); const { searchParams } = new URL(context.request.url);
const cursor = searchParams.get("cursor"); const before = parseInt(searchParams.get("before") || "0");
const entryType = searchParams.get("type"); const entryType = searchParams.get("type");
const showClosed = searchParams.get("showClosed") === "true"; const showClosed = searchParams.get("showClosed") === "true";
const tables: { [k: string]: string } = {
appeal: "appeals",
gma: "game_appeals",
report: "reports",
};
const types: { [k: string]: string } = { const types: { [k: string]: string } = {
appeal: `appeal_`, appeal: `appeal`,
gma: `gameappeal_`, gma: `gameappeal`,
report: `report_`, report: `report`,
}; };
const permissions: { [k: string]: number[] } = { const permissions: { [k: string]: number[] } = {
appeal: [1 << 0, 1 << 1], appeal: [1 << 0, 1 << 1],
@ -31,20 +36,32 @@ export async function onRequestGet(context: RequestContext) {
status: 403, status: 403,
}); });
let prefix = types[entryType]; if (isNaN(before) || before > Date.now())
return new Response('{"error":"Invalid `before` parameter"}', {
headers: {
"content-type": "application/json",
},
status: 400,
});
if (showClosed) prefix = `closed${prefix}`; const prefix = types[entryType];
const table = tables[entryType];
const { keys } = await context.env.DATA.list({ cursor, limit: 20, prefix });
const items = []; const items = [];
const { results }: { results?: { created_at: number; id: string }[] } =
await context.env.D1.prepare(
"SELECT created_at, id FROM ? WHERE created_at < ? ORDER BY created_at DESC LIMIT 25;"
)
.bind(table, before)
.all();
for (const key of keys) { if (results)
const listItem = await context.env.DATA.get(key.name); for (const { id } of results) {
items.push(
await context.env.DATA.get(`${prefix}_${id}`, { type: "json" })
);
}
if (listItem) items.push(JSON.parse(listItem)); return new Response(JSON.stringify(items.filter((v) => v === null)), {
}
return new Response(JSON.stringify(items), {
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
}, },