rewrite website
This commit is contained in:
48
src/util/webhook.ts
Normal file
48
src/util/webhook.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { sleepSeconds } from './sleep.ts';
|
||||
import { WEBHOOK_ENDPOINT } from 'astro:env/server';
|
||||
|
||||
export enum WebhookAction {
|
||||
Signup = 'signup',
|
||||
Report = 'report',
|
||||
Strike = 'strike'
|
||||
}
|
||||
|
||||
export type WebhookActionType<T extends WebhookAction> = {
|
||||
[WebhookAction.Signup]: {
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
birthday: string;
|
||||
telephone: string | null;
|
||||
username: string;
|
||||
edition: 'java' | 'bedrock';
|
||||
};
|
||||
[WebhookAction.Report]: {
|
||||
reporter: string | null;
|
||||
reported: string | null;
|
||||
reason: string;
|
||||
};
|
||||
[WebhookAction.Strike]: {
|
||||
user: string;
|
||||
};
|
||||
}[T];
|
||||
|
||||
export async function sendWebhook<T extends WebhookAction>(action: T, data: WebhookActionType<T>) {
|
||||
if (!WEBHOOK_ENDPOINT || !/^https?:\/\/.+$/.test(WEBHOOK_ENDPOINT)) return;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
const response = await fetch(WEBHOOK_ENDPOINT, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-webhook-action': action
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
|
||||
if (response.status === 200) return;
|
||||
} catch (_) {}
|
||||
|
||||
await sleepSeconds(60);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user