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