Skip to content

Commit 3a1cda9

Browse files
committed
reports: Add basic report routes
1 parent baf0c1f commit 3a1cda9

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

src/routes/reports.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { ElysiaApp } from "..";
2+
import { Report } from "../database/schemas/Report";
3+
import { formatUUID } from "../libs/game-profiles";
4+
import { Permission } from "../types/Permission";
5+
6+
// TODO: Add docs
7+
8+
export default (app: ElysiaApp) => app.get('/', async ({ session, status, i18n }) => {
9+
if(!session?.player?.hasPermission(Permission.ViewReports)) return status(403, { error: i18n('$.error.notAllowed') });
10+
11+
const reports = await Report.find();
12+
13+
return Promise.all(reports.map(async report => {
14+
const reported = await report.getReportedProfile();
15+
const reporter = await report.getReporterProfile();
16+
17+
return {
18+
id: report.id,
19+
reported_player: {
20+
uuid: formatUUID(reported.uuid!),
21+
username: reported.username || 'Unknown username'
22+
},
23+
reporter: {
24+
uuid: formatUUID(reporter.uuid!),
25+
username: reporter.username || 'Unknown username'
26+
},
27+
reason: report.reason,
28+
actions: report.actions.length,
29+
context: {
30+
tag: report.context.tag,
31+
position: report.context.position,
32+
icon: {
33+
type: report.context.icon.type,
34+
hash: report.context.icon.hash
35+
}
36+
},
37+
is_resolved: report.isResolved(),
38+
created_at: report.created_at,
39+
last_updated: report.last_updated,
40+
};
41+
}));
42+
}).get('/:id', async ({ session, params, status, i18n }) => {
43+
if(!session?.player?.hasPermission(Permission.ViewReports)) return status(403, { error: i18n('$.error.notAllowed') });
44+
45+
const report = await Report.findOne({ id: params.id });
46+
if(!report) return status(404, { error: i18n('$.reports.not_found') });
47+
48+
const reported = await report.getReportedProfile();
49+
const reporter = await report.getReporterProfile();
50+
51+
return {
52+
id: report.id,
53+
reported_player: {
54+
uuid: formatUUID(reported.uuid!),
55+
username: reported.username
56+
},
57+
reporter: {
58+
uuid: formatUUID(reporter.uuid!),
59+
username: reporter.username
60+
},
61+
reason: report.reason,
62+
actions: report.actions.map(action => ({
63+
user: action.user,
64+
type: action.type,
65+
comment: action.comment,
66+
added_at: action.added_at
67+
})),
68+
context: {
69+
tag: report.context.tag,
70+
position: report.context.position,
71+
icon: {
72+
type: report.context.icon.type,
73+
hash: report.context.icon.hash
74+
}
75+
},
76+
is_resolved: report.isResolved(),
77+
created_at: report.created_at,
78+
last_updated: report.last_updated,
79+
};
80+
}) // TODO: Add route to manage report actions and status
81+
.delete('/:id', async ({ session, params, status, i18n }) => {
82+
if(!session?.player?.hasPermission(Permission.DeleteReports)) return status(403, { error: i18n('$.error.notAllowed') });
83+
84+
const report = await Report.findOne({ id: params.id });
85+
if(!report) return status(404, { error: i18n('$.reports.not_found') });
86+
87+
await report.deleteOne();
88+
89+
return { message: i18n('$.reports.deleted') };
90+
});

0 commit comments

Comments
 (0)