Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
car-crushers-portal/functions/api/mod-queue/list.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52 lines (44 sloc)
1.42 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function onRequestGet(context: RequestContext) { | |
const { searchParams } = new URL(context.request.url); | |
const cursor = searchParams.get("cursor"); | |
const entryType = searchParams.get("type"); | |
const showClosed = searchParams.get("showClosed") === "true"; | |
const types: { [k: string]: string } = { | |
appeal: `appeal_`, | |
gma: `gameappeal_`, | |
report: `report_`, | |
}; | |
const permissions: { [k: string]: number[] } = { | |
appeal: [1 << 0, 1 << 1], | |
gma: [1 << 5], | |
report: [1 << 5], | |
}; | |
const { current_user: currentUser } = context.data; | |
if (!entryType || !types[entryType]) | |
return new Response('{"error":"Invalid filter type"}', { | |
headers: { | |
"content-type": "application/json", | |
}, | |
status: 400, | |
}); | |
if (!permissions[entryType].find((p) => currentUser.permissions & p)) | |
return new Response('{"error":"You cannot use this filter"}', { | |
headers: { | |
"content-type": "application/json", | |
}, | |
status: 403, | |
}); | |
let prefix = types[entryType]; | |
if (showClosed) prefix = `closed${prefix}`; | |
const { keys } = await context.env.DATA.list({ cursor, limit: 20, prefix }); | |
const items = []; | |
for (const key of keys) { | |
const listItem = await context.env.DATA.get(key.name); | |
if (listItem) items.push(JSON.parse(listItem)); | |
} | |
return new Response(JSON.stringify(items), { | |
headers: { | |
"content-type": "application/json", | |
}, | |
}); | |
} |