Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use d1 for listing in mod queue
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent d659a81 commit e51452a
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions 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],
Expand All @@ -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",
},
Expand Down

0 comments on commit e51452a

Please sign in to comment.