add webhook endpoint on report change (#21)
All checks were successful
delpoy / build-and-deploy (push) Successful in 1m16s

This commit is contained in:
bytedream 2023-12-21 15:49:39 +01:00
parent 2a9869ca7d
commit ca16ce0603
3 changed files with 24 additions and 1 deletions

View File

@ -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=<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 |

11
src/lib/server/webhook.ts Normal file
View File

@ -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
})
});
}

View File

@ -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;