Skip to content
Permalink
Newer
Older
100644 79 lines (72 sloc) 2.35 KB
October 19, 2023 16:49
1
export async function onRequestGet(context: RequestContext) {
2
const { searchParams } = new URL(context.request.url);
3
const before = parseInt(searchParams.get("before") || `${Date.now()}`);
October 19, 2023 16:49
4
const entryType = searchParams.get("type");
October 19, 2023 16:49
5
const showClosed = searchParams.get("showClosed") === "true";
October 19, 2023 16:49
6
const tables: { [k: string]: string } = {
7
appeal: "appeals",
8
gma: "game_appeals",
9
report: "reports",
10
};
October 19, 2023 16:49
11
const types: { [k: string]: string } = {
12
appeal: "appeal",
13
gma: "gameappeal",
14
report: "report",
October 19, 2023 16:49
16
const permissions: { [k: string]: number[] } = {
17
appeal: [1 << 0, 1 << 1],
18
gma: [1 << 5],
19
report: [1 << 5],
20
};
21
const { current_user: currentUser } = context.data;
October 19, 2023 16:49
22
October 19, 2023 16:49
23
if (!entryType || !types[entryType])
24
return new Response('{"error":"Invalid filter type"}', {
25
headers: {
26
"content-type": "application/json",
27
},
28
status: 400,
29
});
30
31
if (!permissions[entryType].find((p) => currentUser.permissions & p))
32
return new Response('{"error":"You cannot use this filter"}', {
33
headers: {
34
"content-type": "application/json",
35
},
36
status: 403,
37
});
38
October 19, 2023 16:49
39
if (isNaN(before) || before > Date.now())
40
return new Response('{"error":"Invalid `before` parameter"}', {
41
headers: {
42
"content-type": "application/json",
43
},
44
status: 400,
45
});
October 19, 2023 16:49
46
October 19, 2023 16:49
47
const prefix = types[entryType];
48
const table = tables[entryType];
October 19, 2023 16:49
49
const items = [];
October 19, 2023 16:49
50
const { results }: { results?: { created_at: number; id: string }[] } =
51
/*
52
This is normally VERY BAD and can lead to injection attacks
53
However, there is no other way to do this, as using bindings for table names is unsupported apparently
54
To avoid any potential injection attacks we enforce a list of specific values and permissions for table names
55
*/
October 19, 2023 16:49
56
await context.env.D1.prepare(
57
`SELECT id
58
FROM ${table}
October 19, 2023 16:50
59
WHERE created_at < ? AND open = ?
60
ORDER BY created_at DESC LIMIT 25;`,
October 19, 2023 16:49
61
)
October 19, 2023 16:50
62
.bind(before, Number(!showClosed))
October 19, 2023 16:49
63
.all();
October 19, 2023 16:49
64
October 19, 2023 16:49
65
if (results)
66
for (const { id } of results) {
October 19, 2023 16:49
67
const item = await context.env.DATA.get(`${prefix}_${id}`, {
68
type: "json",
69
});
70
71
if (item) items.push({ ...item, id });
October 19, 2023 16:49
72
}
October 19, 2023 16:49
73
74
return new Response(JSON.stringify(items.filter((v) => v !== null)), {
October 19, 2023 16:49
75
headers: {
76
"content-type": "application/json",
77
},
78
});
79
}