From ca16ce06037f2db3e7f26d49caa73558befad07b Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 21 Dec 2023 15:49:39 +0100 Subject: [PATCH] add webhook endpoint on report change (#21) --- README.md | 1 + src/lib/server/webhook.ts | 11 +++++++++++ src/routes/admin/reports/+server.ts | 13 ++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/lib/server/webhook.ts diff --git a/README.md b/README.md index 43a0a83..b6746ae 100644 --- a/README.md +++ b/README.md @@ -37,5 +37,6 @@ Configurations can be done with env variables | `ADMIN_USER` | Name for the root admin user. The admin user won't be available if `ADMIN_USER` or `ADMIN_PASSWORD` is set | | `ADMIN_PASSWORD` | Password for the root admin user defined via `ADMIN_USER`. The admin user won't be available if `ADMIN_USER` or `ADMIN_PASSWORD` is set | | `REPORT_SECRET` | Secret which may be required (as `?secret=` query parameter) to create reports on the public endpoint. Isn't required to be in the request if this variable is empty | +| `REPORTED_WEBHOOK` | URL to send POST request to when a report got finished | | `PUBLIC_BASE_PATH` | If running the website on a sub-path, set this variable to the path so that assets etc. can find the correct location | | `PUBLIC_START_DATE` | The start date when the event starts | diff --git a/src/lib/server/webhook.ts b/src/lib/server/webhook.ts new file mode 100644 index 0000000..d91e0d5 --- /dev/null +++ b/src/lib/server/webhook.ts @@ -0,0 +1,11 @@ +async function webhookUserReported(endpoint: string, uuid: string) { + await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + user: uuid + }) + }); +} diff --git a/src/routes/admin/reports/+server.ts b/src/routes/admin/reports/+server.ts index 75c669b..aa346f1 100644 --- a/src/routes/admin/reports/+server.ts +++ b/src/routes/admin/reports/+server.ts @@ -114,7 +114,12 @@ export const PATCH = (async ({ request, cookies }) => { if (report === null || (admin === null && data.auditor != -1) || reported === null) return new Response(null, { status: 400 }); - report.reported_id = reported?.id || null; + let reportedChanged: string | undefined; + if (report.reported_id != reported?.id) { + reportedChanged = reported ? reported.uuid : (await User.findByPk(report.reported_id))?.uuid; + } + + report.reported_id = reported?.id ?? null; if (data.notice != null) report.notice = data.notice; if (data.statement != null) report.statement = data.statement; if (data.status != null) report.status = data.status; @@ -129,6 +134,7 @@ export const PATCH = (async ({ request, cookies }) => { } } else if (data.strike_reason == -1 && report.strike_reason_id != null) { report.strike_reason_id = null; + report.striked_at = null; } else if (data.strike_reason != report.strike_reason_id) { if (!report.reported_id) return new Response(null, { status: 400 }); const strike_reason = await StrikeReason.findByPk(data.strike_reason); @@ -141,6 +147,11 @@ export const PATCH = (async ({ request, cookies }) => { await report.save(); + if (reportedChanged != null && data.status == 'reviewed' && env.REPORTED_WEBHOOK) { + // no `await` to avoid blocking + webhookUserReported(env.REPORTED_WEBHOOK, reportedChanged); + } + return new Response(); }) satisfies RequestHandler;