Files
EpicNext-Cms/src/lib/services/webhook.ts
T
openhands 17847545dd
Deploy / release (push) Skipped
Deploy / deploy (push) Successful in 1m52s
Improvements: remove dead config, fix ESM, add URL validation, unify types, add missing logging
- Remove .prettierrc (dead config, Biome replaces Prettier)
- Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat
- Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit
- Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts
- Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars
- Replace barrel export src/types/index.ts with direct @/types/common imports
- Make trustHost conditional (development only) in auth.ts
- Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations
- Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
2026-07-26 20:28:11 +02:00

129 lines
3.1 KiB
TypeScript

import { env } from "@/env";
import { logger } from "@/lib/logger";
import { prisma } from "../prisma";
export type { WebhookAction } from "@/types/admin";
import type { WebhookAction } from "@/types/admin";
interface WebhookPayload {
action: WebhookAction;
actor: string;
target: string;
targetId?: number;
details?: string;
}
const ACTION_COLORS: Record<WebhookAction, number> = {
ban: 0xf87272,
unban: 0x36d399,
kick: 0xfbbd23,
disconnect: 0xfbbd23,
room_delete: 0xf87272,
user_edit: 0x3abff8,
user_create: 0x36d399,
news_publish: 0x36d399,
news_delete: 0xf87272,
hotel_alert: 0xa78bfa,
staff_alert: 0xa78bfa,
ticket_create: 0x3abff8,
event_create: 0x36d399,
event_update: 0x3abff8,
event_cancel: 0xf87272,
poll_create: 0x36d399,
poll_close: 0xa78bfa,
};
async function getSetting(
cmsKey: string,
envValue?: string,
): Promise<string | null> {
if (envValue) return envValue;
try {
const setting = await prisma.websiteSetting.findUnique({
where: { key: cmsKey },
});
return setting?.value || null;
} catch {
return null;
}
}
async function sendDiscord(payload: WebhookPayload): Promise<void> {
const url = await getSetting("webhook_discord_url", env.DISCORD_WEBHOOK_URL);
if (!url) return;
const embed = {
title: `[${payload.action.toUpperCase().replace("_", " ")}]`,
color: ACTION_COLORS[payload.action] ?? 0x666666,
fields: [
{ name: "Staff", value: payload.actor, inline: true },
{ name: "Target", value: payload.target, inline: true },
],
timestamp: new Date().toISOString(),
};
if (payload.details) {
embed.fields.push({
name: "Details",
value: payload.details,
inline: false,
});
}
try {
await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ embeds: [embed] }),
signal: AbortSignal.timeout(5000),
});
} catch (err) {
logger.error("[Webhook] Discord send failed", {
action: payload.action,
error: (err as Error).message,
});
}
}
async function sendTelegram(payload: WebhookPayload): Promise<void> {
const token = await getSetting(
"webhook_telegram_bot_token",
env.TELEGRAM_BOT_TOKEN,
);
const chatId = await getSetting(
"webhook_telegram_chat_id",
env.TELEGRAM_CHAT_ID,
);
if (!token || !chatId) return;
const text = [
`<b>[${payload.action.toUpperCase().replace("_", " ")}]</b>`,
`<b>Staff:</b> ${payload.actor}`,
`<b>Target:</b> ${payload.target}`,
payload.details ? `<b>Details:</b> ${payload.details}` : "",
]
.filter(Boolean)
.join("\n");
try {
await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ chat_id: chatId, text, parse_mode: "HTML" }),
signal: AbortSignal.timeout(5000),
});
} catch (err) {
logger.error("[Webhook] Telegram send failed", {
action: payload.action,
error: (err as Error).message,
});
}
}
/** Fire-and-forget notification to Discord + Telegram */
export function notify(payload: WebhookPayload): void {
sendDiscord(payload).catch(() => {});
sendTelegram(payload).catch(() => {});
}