diff --git a/functions/api/mod-queue/list.ts b/functions/api/mod-queue/list.ts index 02b9a47..f183cf5 100644 --- a/functions/api/mod-queue/list.ts +++ b/functions/api/mod-queue/list.ts @@ -1,12 +1,17 @@ export async function onRequestGet(context: RequestContext) { 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 showClosed = searchParams.get("showClosed") === "true"; + const tables: { [k: string]: string } = { + appeal: "appeals", + gma: "game_appeals", + report: "reports", + }; const types: { [k: string]: string } = { - appeal: `appeal_`, - gma: `gameappeal_`, - report: `report_`, + appeal: `appeal`, + gma: `gameappeal`, + report: `report`, }; const permissions: { [k: string]: number[] } = { appeal: [1 << 0, 1 << 1], @@ -31,20 +36,32 @@ export async function onRequestGet(context: RequestContext) { status: 403, }); - let prefix = types[entryType]; - - if (showClosed) prefix = `closed${prefix}`; + if (isNaN(before) || before > Date.now()) + return new Response('{"error":"Invalid `before` parameter"}', { + headers: { + "content-type": "application/json", + }, + status: 400, + }); - const { keys } = await context.env.DATA.list({ cursor, limit: 20, prefix }); + const prefix = types[entryType]; + const table = tables[entryType]; 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) { - const listItem = await context.env.DATA.get(key.name); - - if (listItem) items.push(JSON.parse(listItem)); - } + if (results) + for (const { id } of results) { + items.push( + await context.env.DATA.get(`${prefix}_${id}`, { type: "json" }) + ); + } - return new Response(JSON.stringify(items), { + return new Response(JSON.stringify(items.filter((v) => v === null)), { headers: { "content-type": "application/json", },