Feat/notifications alerts (#52)

* feat: notifications backend & creation

* feat: assign notification to backup schedule

* refactor: status dot one component

* chore(notification-details): remove refetchInterval
This commit is contained in:
Nico
2025-11-22 14:58:21 +01:00
committed by GitHub
parent 043f73ea87
commit 6c30e7e357
37 changed files with 3940 additions and 172 deletions

View File

@@ -0,0 +1,5 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildCustomShoutrrrUrl(config: Extract<NotificationConfig, { type: "custom" }>): string {
return config.shoutrrrUrl;
}

View File

@@ -0,0 +1,28 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildDiscordShoutrrrUrl(config: Extract<NotificationConfig, { type: "discord" }>): string {
const url = new URL(config.webhookUrl);
const pathParts = url.pathname.split("/").filter(Boolean);
if (pathParts.length < 4 || pathParts[0] !== "api" || pathParts[1] !== "webhooks") {
throw new Error("Invalid Discord webhook URL format");
}
const [, , webhookId, webhookToken] = pathParts;
let shoutrrrUrl = `discord://${webhookToken}@${webhookId}`;
const params = new URLSearchParams();
if (config.username) {
params.append("username", config.username);
}
if (config.avatarUrl) {
params.append("avatar_url", config.avatarUrl);
}
if (params.toString()) {
shoutrrrUrl += `?${params.toString()}`;
}
return shoutrrrUrl;
}

View File

@@ -0,0 +1,10 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildEmailShoutrrrUrl(config: Extract<NotificationConfig, { type: "email" }>): string {
const protocol = config.useTLS ? "smtps" : "smtp";
const auth = `${encodeURIComponent(config.username)}:${encodeURIComponent(config.password)}`;
const host = `${config.smtpHost}:${config.smtpPort}`;
const toRecipients = config.to.map((email) => encodeURIComponent(email)).join(",");
return `${protocol}://${auth}@${host}/?from=${encodeURIComponent(config.from)}&to=${toRecipients}`;
}

View File

@@ -0,0 +1,15 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildGotifyShoutrrrUrl(config: Extract<NotificationConfig, { type: "gotify" }>): string {
const url = new URL(config.serverUrl);
const hostname = url.hostname;
const port = url.port ? `:${url.port}` : "";
let shoutrrrUrl = `gotify://${hostname}${port}/${config.token}`;
if (config.priority !== undefined) {
shoutrrrUrl += `?priority=${config.priority}`;
}
return shoutrrrUrl;
}

View File

@@ -0,0 +1,29 @@
import type { NotificationConfig } from "~/schemas/notifications";
import { buildEmailShoutrrrUrl } from "./email";
import { buildSlackShoutrrrUrl } from "./slack";
import { buildDiscordShoutrrrUrl } from "./discord";
import { buildGotifyShoutrrrUrl } from "./gotify";
import { buildNtfyShoutrrrUrl } from "./ntfy";
import { buildCustomShoutrrrUrl } from "./custom";
export function buildShoutrrrUrl(config: NotificationConfig): string {
switch (config.type) {
case "email":
return buildEmailShoutrrrUrl(config);
case "slack":
return buildSlackShoutrrrUrl(config);
case "discord":
return buildDiscordShoutrrrUrl(config);
case "gotify":
return buildGotifyShoutrrrUrl(config);
case "ntfy":
return buildNtfyShoutrrrUrl(config);
case "custom":
return buildCustomShoutrrrUrl(config);
default: {
// TypeScript exhaustiveness check
const _exhaustive: never = config;
throw new Error(`Unsupported notification type: ${(_exhaustive as NotificationConfig).type}`);
}
}
}

View File

@@ -0,0 +1,28 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildNtfyShoutrrrUrl(config: Extract<NotificationConfig, { type: "ntfy" }>): string {
let shoutrrrUrl: string;
if (config.serverUrl) {
const url = new URL(config.serverUrl);
const hostname = url.hostname;
const port = url.port ? `:${url.port}` : "";
shoutrrrUrl = `ntfy://${hostname}${port}/${config.topic}`;
} else {
shoutrrrUrl = `ntfy://ntfy.sh/${config.topic}`;
}
const params = new URLSearchParams();
if (config.token) {
params.append("token", config.token);
}
if (config.priority) {
params.append("priority", config.priority);
}
if (params.toString()) {
shoutrrrUrl += `?${params.toString()}`;
}
return shoutrrrUrl;
}

View File

@@ -0,0 +1,31 @@
import type { NotificationConfig } from "~/schemas/notifications";
export function buildSlackShoutrrrUrl(config: Extract<NotificationConfig, { type: "slack" }>): string {
const url = new URL(config.webhookUrl);
const pathParts = url.pathname.split("/").filter(Boolean);
if (pathParts.length < 4 || pathParts[0] !== "services") {
throw new Error("Invalid Slack webhook URL format");
}
const [, tokenA, tokenB, tokenC] = pathParts;
let shoutrrrUrl = `slack://hook:${tokenA}-${tokenB}-${tokenC}@webhook`;
const params = new URLSearchParams();
if (config.channel) {
params.append("channel", config.channel);
}
if (config.username) {
params.append("username", config.username);
}
if (config.iconEmoji) {
params.append("icon", config.iconEmoji);
}
if (params.toString()) {
shoutrrrUrl += `?${params.toString()}`;
}
return shoutrrrUrl;
}

View File

@@ -0,0 +1,51 @@
import { Hono } from "hono";
import { validator } from "hono-openapi";
import {
createDestinationBody,
createDestinationDto,
deleteDestinationDto,
getDestinationDto,
listDestinationsDto,
testDestinationDto,
updateDestinationBody,
updateDestinationDto,
type CreateDestinationDto,
type DeleteDestinationDto,
type GetDestinationDto,
type ListDestinationsDto,
type TestDestinationDto,
type UpdateDestinationDto,
} from "./notifications.dto";
import { notificationsService } from "./notifications.service";
export const notificationsController = new Hono()
.get("/destinations", listDestinationsDto, async (c) => {
const destinations = await notificationsService.listDestinations();
return c.json<ListDestinationsDto>(destinations, 200);
})
.post("/destinations", createDestinationDto, validator("json", createDestinationBody), async (c) => {
const body = c.req.valid("json");
const destination = await notificationsService.createDestination(body.name, body.config);
return c.json<CreateDestinationDto>(destination, 201);
})
.get("/destinations/:id", getDestinationDto, async (c) => {
const id = Number.parseInt(c.req.param("id"), 10);
const destination = await notificationsService.getDestination(id);
return c.json<GetDestinationDto>(destination, 200);
})
.patch("/destinations/:id", updateDestinationDto, validator("json", updateDestinationBody), async (c) => {
const id = Number.parseInt(c.req.param("id"), 10);
const body = c.req.valid("json");
const destination = await notificationsService.updateDestination(id, body);
return c.json<UpdateDestinationDto>(destination, 200);
})
.delete("/destinations/:id", deleteDestinationDto, async (c) => {
const id = Number.parseInt(c.req.param("id"), 10);
await notificationsService.deleteDestination(id);
return c.json<DeleteDestinationDto>({ message: "Notification destination deleted" }, 200);
})
.post("/destinations/:id/test", testDestinationDto, async (c) => {
const id = Number.parseInt(c.req.param("id"), 10);
const result = await notificationsService.testDestination(id);
return c.json<TestDestinationDto>(result, 200);
});

View File

@@ -0,0 +1,251 @@
import { type } from "arktype";
import { describeRoute, resolver } from "hono-openapi";
import { NOTIFICATION_TYPES, notificationConfigSchema } from "~/schemas/notifications";
/**
* Notification Destination Schema
*/
export const notificationDestinationSchema = type({
id: "number",
name: "string",
enabled: "boolean",
type: type.valueOf(NOTIFICATION_TYPES),
config: notificationConfigSchema,
createdAt: "number",
updatedAt: "number",
});
export type NotificationDestinationDto = typeof notificationDestinationSchema.infer;
/**
* List all notification destinations
*/
export const listDestinationsResponse = notificationDestinationSchema.array();
export type ListDestinationsDto = typeof listDestinationsResponse.infer;
export const listDestinationsDto = describeRoute({
description: "List all notification destinations",
tags: ["Notifications"],
operationId: "listNotificationDestinations",
responses: {
200: {
description: "A list of notification destinations",
content: {
"application/json": {
schema: resolver(listDestinationsResponse),
},
},
},
},
});
/**
* Create a new notification destination
*/
export const createDestinationBody = type({
name: "string",
config: notificationConfigSchema,
});
export const createDestinationResponse = notificationDestinationSchema;
export type CreateDestinationDto = typeof createDestinationResponse.infer;
export const createDestinationDto = describeRoute({
description: "Create a new notification destination",
operationId: "createNotificationDestination",
tags: ["Notifications"],
responses: {
201: {
description: "Notification destination created successfully",
content: {
"application/json": {
schema: resolver(createDestinationResponse),
},
},
},
},
});
/**
* Get a single notification destination
*/
export const getDestinationResponse = notificationDestinationSchema;
export type GetDestinationDto = typeof getDestinationResponse.infer;
export const getDestinationDto = describeRoute({
description: "Get a notification destination by ID",
operationId: "getNotificationDestination",
tags: ["Notifications"],
responses: {
200: {
description: "Notification destination details",
content: {
"application/json": {
schema: resolver(getDestinationResponse),
},
},
},
404: {
description: "Notification destination not found",
},
},
});
/**
* Update a notification destination
*/
export const updateDestinationBody = type({
"name?": "string",
"enabled?": "boolean",
"config?": notificationConfigSchema,
});
export const updateDestinationResponse = notificationDestinationSchema;
export type UpdateDestinationDto = typeof updateDestinationResponse.infer;
export const updateDestinationDto = describeRoute({
description: "Update a notification destination",
operationId: "updateNotificationDestination",
tags: ["Notifications"],
responses: {
200: {
description: "Notification destination updated successfully",
content: {
"application/json": {
schema: resolver(updateDestinationResponse),
},
},
},
404: {
description: "Notification destination not found",
},
},
});
/**
* Delete a notification destination
*/
export const deleteDestinationResponse = type({
message: "string",
});
export type DeleteDestinationDto = typeof deleteDestinationResponse.infer;
export const deleteDestinationDto = describeRoute({
description: "Delete a notification destination",
operationId: "deleteNotificationDestination",
tags: ["Notifications"],
responses: {
200: {
description: "Notification destination deleted successfully",
content: {
"application/json": {
schema: resolver(deleteDestinationResponse),
},
},
},
404: {
description: "Notification destination not found",
},
},
});
/**
* Test a notification destination
*/
export const testDestinationResponse = type({
success: "boolean",
});
export type TestDestinationDto = typeof testDestinationResponse.infer;
export const testDestinationDto = describeRoute({
description: "Test a notification destination by sending a test message",
operationId: "testNotificationDestination",
tags: ["Notifications"],
responses: {
200: {
description: "Test notification sent successfully",
content: {
"application/json": {
schema: resolver(testDestinationResponse),
},
},
},
404: {
description: "Notification destination not found",
},
409: {
description: "Cannot test disabled destination",
},
500: {
description: "Failed to send test notification",
},
},
});
/**
* Backup Schedule Notification Assignment Schema
*/
export const scheduleNotificationAssignmentSchema = type({
scheduleId: "number",
destinationId: "number",
notifyOnStart: "boolean",
notifyOnSuccess: "boolean",
notifyOnFailure: "boolean",
createdAt: "number",
destination: notificationDestinationSchema,
});
export type ScheduleNotificationAssignmentDto = typeof scheduleNotificationAssignmentSchema.infer;
/**
* Get notifications for a backup schedule
*/
export const getScheduleNotificationsResponse = scheduleNotificationAssignmentSchema.array();
export type GetScheduleNotificationsDto = typeof getScheduleNotificationsResponse.infer;
export const getScheduleNotificationsDto = describeRoute({
description: "Get notification assignments for a backup schedule",
operationId: "getScheduleNotifications",
tags: ["Backups", "Notifications"],
responses: {
200: {
description: "List of notification assignments for the schedule",
content: {
"application/json": {
schema: resolver(getScheduleNotificationsResponse),
},
},
},
},
});
/**
* Update notifications for a backup schedule
*/
export const updateScheduleNotificationsBody = type({
assignments: type({
destinationId: "number",
notifyOnStart: "boolean",
notifyOnSuccess: "boolean",
notifyOnFailure: "boolean",
}).array(),
});
export const updateScheduleNotificationsResponse = scheduleNotificationAssignmentSchema.array();
export type UpdateScheduleNotificationsDto = typeof updateScheduleNotificationsResponse.infer;
export const updateScheduleNotificationsDto = describeRoute({
description: "Update notification assignments for a backup schedule",
operationId: "updateScheduleNotifications",
tags: ["Backups", "Notifications"],
responses: {
200: {
description: "Notification assignments updated successfully",
content: {
"application/json": {
schema: resolver(updateScheduleNotificationsResponse),
},
},
},
},
});

View File

@@ -0,0 +1,405 @@
import { eq, and } from "drizzle-orm";
import { ConflictError, InternalServerError, NotFoundError } from "http-errors-enhanced";
import slugify from "slugify";
import { db } from "../../db/db";
import {
notificationDestinationsTable,
backupScheduleNotificationsTable,
type NotificationDestination,
} from "../../db/schema";
import { cryptoUtils } from "../../utils/crypto";
import { logger } from "../../utils/logger";
import { sendNotification } from "../../utils/shoutrrr";
import { buildShoutrrrUrl } from "./builders";
import type { NotificationConfig, NotificationEvent } from "~/schemas/notifications";
import { toMessage } from "../../utils/errors";
const listDestinations = async () => {
const destinations = await db.query.notificationDestinationsTable.findMany({
orderBy: (destinations, { asc }) => [asc(destinations.name)],
});
return destinations;
};
const getDestination = async (id: number) => {
const destination = await db.query.notificationDestinationsTable.findFirst({
where: eq(notificationDestinationsTable.id, id),
});
if (!destination) {
throw new NotFoundError("Notification destination not found");
}
return destination;
};
async function encryptSensitiveFields(config: NotificationConfig): Promise<NotificationConfig> {
switch (config.type) {
case "email":
return {
...config,
password: await cryptoUtils.encrypt(config.password),
};
case "slack":
return {
...config,
webhookUrl: await cryptoUtils.encrypt(config.webhookUrl),
};
case "discord":
return {
...config,
webhookUrl: await cryptoUtils.encrypt(config.webhookUrl),
};
case "gotify":
return {
...config,
token: await cryptoUtils.encrypt(config.token),
};
case "ntfy":
return {
...config,
token: config.token ? await cryptoUtils.encrypt(config.token) : undefined,
};
case "custom":
return {
...config,
shoutrrrUrl: await cryptoUtils.encrypt(config.shoutrrrUrl),
};
default:
return config;
}
}
async function decryptSensitiveFields(config: NotificationConfig): Promise<NotificationConfig> {
switch (config.type) {
case "email":
return {
...config,
password: await cryptoUtils.decrypt(config.password),
};
case "slack":
return {
...config,
webhookUrl: await cryptoUtils.decrypt(config.webhookUrl),
};
case "discord":
return {
...config,
webhookUrl: await cryptoUtils.decrypt(config.webhookUrl),
};
case "gotify":
return {
...config,
token: await cryptoUtils.decrypt(config.token),
};
case "ntfy":
return {
...config,
token: config.token ? await cryptoUtils.decrypt(config.token) : undefined,
};
case "custom":
return {
...config,
shoutrrrUrl: await cryptoUtils.decrypt(config.shoutrrrUrl),
};
default:
return config;
}
}
const createDestination = async (name: string, config: NotificationConfig) => {
const slug = slugify(name, { lower: true, strict: true });
const existing = await db.query.notificationDestinationsTable.findFirst({
where: eq(notificationDestinationsTable.name, slug),
});
if (existing) {
throw new ConflictError("Notification destination with this name already exists");
}
const encryptedConfig = await encryptSensitiveFields(config);
const [created] = await db
.insert(notificationDestinationsTable)
.values({
name: slug,
type: config.type,
config: encryptedConfig,
})
.returning();
if (!created) {
throw new InternalServerError("Failed to create notification destination");
}
return created;
};
const updateDestination = async (
id: number,
updates: { name?: string; enabled?: boolean; config?: NotificationConfig },
) => {
const existing = await getDestination(id);
if (!existing) {
throw new NotFoundError("Notification destination not found");
}
const updateData: Partial<NotificationDestination> = {
updatedAt: Math.floor(Date.now() / 1000),
};
if (updates.name !== undefined) {
const slug = slugify(updates.name, { lower: true, strict: true });
const conflict = await db.query.notificationDestinationsTable.findFirst({
where: and(eq(notificationDestinationsTable.name, slug), eq(notificationDestinationsTable.id, id)),
});
if (conflict && conflict.id !== id) {
throw new ConflictError("Notification destination with this name already exists");
}
updateData.name = slug;
}
if (updates.enabled !== undefined) {
updateData.enabled = updates.enabled;
}
if (updates.config !== undefined) {
const encryptedConfig = await encryptSensitiveFields(updates.config);
updateData.config = encryptedConfig;
updateData.type = updates.config.type;
}
const [updated] = await db
.update(notificationDestinationsTable)
.set(updateData)
.where(eq(notificationDestinationsTable.id, id))
.returning();
if (!updated) {
throw new InternalServerError("Failed to update notification destination");
}
return updated;
};
const deleteDestination = async (id: number) => {
await db.delete(notificationDestinationsTable).where(eq(notificationDestinationsTable.id, id));
};
const testDestination = async (id: number) => {
const destination = await getDestination(id);
if (!destination.enabled) {
throw new ConflictError("Cannot test disabled notification destination");
}
const decryptedConfig = await decryptSensitiveFields(destination.config);
const shoutrrrUrl = buildShoutrrrUrl(decryptedConfig);
console.log("Testing notification with Shoutrrr URL:", shoutrrrUrl);
const result = await sendNotification({
shoutrrrUrl,
title: "Zerobyte Test Notification",
body: `This is a test notification from Zerobyte for destination: ${destination.name}`,
});
if (!result.success) {
throw new InternalServerError(`Failed to send test notification: ${result.error}`);
}
return { success: true };
};
const getScheduleNotifications = async (scheduleId: number) => {
const assignments = await db.query.backupScheduleNotificationsTable.findMany({
where: eq(backupScheduleNotificationsTable.scheduleId, scheduleId),
with: {
destination: true,
},
});
return assignments;
};
const updateScheduleNotifications = async (
scheduleId: number,
assignments: Array<{
destinationId: number;
notifyOnStart: boolean;
notifyOnSuccess: boolean;
notifyOnFailure: boolean;
}>,
) => {
await db.delete(backupScheduleNotificationsTable).where(eq(backupScheduleNotificationsTable.scheduleId, scheduleId));
if (assignments.length > 0) {
await db.insert(backupScheduleNotificationsTable).values(
assignments.map((assignment) => ({
scheduleId,
...assignment,
})),
);
}
return getScheduleNotifications(scheduleId);
};
const sendBackupNotification = async (
scheduleId: number,
event: NotificationEvent,
context: {
volumeName: string;
repositoryName: string;
scheduleName?: string;
error?: string;
duration?: number;
filesProcessed?: number;
bytesProcessed?: string;
snapshotId?: string;
},
) => {
try {
const assignments = await db.query.backupScheduleNotificationsTable.findMany({
where: eq(backupScheduleNotificationsTable.scheduleId, scheduleId),
with: {
destination: true,
},
});
const relevantAssignments = assignments.filter((assignment) => {
if (!assignment.destination.enabled) return false;
switch (event) {
case "start":
return assignment.notifyOnStart;
case "success":
return assignment.notifyOnSuccess;
case "failure":
return assignment.notifyOnFailure;
default:
return false;
}
});
if (!relevantAssignments.length) {
logger.debug(`No notification destinations configured for backup ${scheduleId} event ${event}`);
return;
}
const { title, body } = buildNotificationMessage(event, context);
for (const assignment of relevantAssignments) {
try {
const decryptedConfig = await decryptSensitiveFields(assignment.destination.config);
const shoutrrrUrl = buildShoutrrrUrl(decryptedConfig);
const result = await sendNotification({
shoutrrrUrl,
title,
body,
});
if (result.success) {
logger.info(
`Notification sent successfully to ${assignment.destination.name} for backup ${scheduleId} event ${event}`,
);
} else {
logger.error(
`Failed to send notification to ${assignment.destination.name} for backup ${scheduleId}: ${result.error}`,
);
}
} catch (error) {
logger.error(
`Error sending notification to ${assignment.destination.name} for backup ${scheduleId}: ${toMessage(error)}`,
);
}
}
} catch (error) {
logger.error(`Error processing backup notifications for schedule ${scheduleId}: ${toMessage(error)}`);
}
};
function buildNotificationMessage(
event: NotificationEvent,
context: {
volumeName: string;
repositoryName: string;
scheduleName?: string;
error?: string;
duration?: number;
filesProcessed?: number;
bytesProcessed?: string;
snapshotId?: string;
},
) {
const date = new Date().toLocaleDateString();
const time = new Date().toLocaleTimeString();
switch (event) {
case "start":
return {
title: "🔵 Backup Started",
body: [
`Volume: ${context.volumeName}`,
`Repository: ${context.repositoryName}`,
context.scheduleName ? `Schedule: ${context.scheduleName}` : null,
`Time: ${date} - ${time}`,
]
.filter(Boolean)
.join("\n"),
};
case "success":
return {
title: "✅ Backup Completed Successfully",
body: [
`Volume: ${context.volumeName}`,
`Repository: ${context.repositoryName}`,
context.duration ? `Duration: ${Math.round(context.duration / 1000)}s` : null,
context.filesProcessed !== undefined ? `Files: ${context.filesProcessed}` : null,
context.bytesProcessed ? `Size: ${context.bytesProcessed}` : null,
context.snapshotId ? `Snapshot: ${context.snapshotId}` : null,
`Time: ${date} - ${time}`,
]
.filter(Boolean)
.join("\n"),
};
case "failure":
return {
title: "❌ Backup Failed",
body: [
`Volume: ${context.volumeName}`,
`Repository: ${context.repositoryName}`,
context.error ? `Error: ${context.error}` : null,
`Time: ${date} - ${time}`,
]
.filter(Boolean)
.join("\n"),
};
default:
return {
title: "Backup Notification",
body: `Volume: ${context.volumeName}\nRepository: ${context.repositoryName}\nTime: ${date} - ${time}`,
};
}
}
export const notificationsService = {
listDestinations,
getDestination,
createDestination,
updateDestination,
deleteDestination,
testDestination,
getScheduleNotifications,
updateScheduleNotifications,
sendBackupNotification,
};