mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
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:
@@ -1,7 +1,8 @@
|
||||
import { relations, sql } from "drizzle-orm";
|
||||
import { int, integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
||||
import { int, integer, sqliteTable, text, primaryKey } from "drizzle-orm/sqlite-core";
|
||||
import type { CompressionMode, RepositoryBackend, repositoryConfigSchema, RepositoryStatus } from "~/schemas/restic";
|
||||
import type { BackendStatus, BackendType, volumeConfigSchema } from "~/schemas/volumes";
|
||||
import type { NotificationType, notificationConfigSchema } from "~/schemas/notifications";
|
||||
|
||||
/**
|
||||
* Volumes Table
|
||||
@@ -90,7 +91,7 @@ export const backupSchedulesTable = sqliteTable("backup_schedules_table", {
|
||||
createdAt: int("created_at", { mode: "number" }).notNull().default(sql`(unixepoch())`),
|
||||
updatedAt: int("updated_at", { mode: "number" }).notNull().default(sql`(unixepoch())`),
|
||||
});
|
||||
export const backupScheduleRelations = relations(backupSchedulesTable, ({ one }) => ({
|
||||
export const backupScheduleRelations = relations(backupSchedulesTable, ({ one, many }) => ({
|
||||
volume: one(volumesTable, {
|
||||
fields: [backupSchedulesTable.volumeId],
|
||||
references: [volumesTable.id],
|
||||
@@ -99,5 +100,54 @@ export const backupScheduleRelations = relations(backupSchedulesTable, ({ one })
|
||||
fields: [backupSchedulesTable.repositoryId],
|
||||
references: [repositoriesTable.id],
|
||||
}),
|
||||
notifications: many(backupScheduleNotificationsTable),
|
||||
}));
|
||||
export type BackupSchedule = typeof backupSchedulesTable.$inferSelect;
|
||||
|
||||
/**
|
||||
* Notification Destinations Table
|
||||
*/
|
||||
export const notificationDestinationsTable = sqliteTable("notification_destinations_table", {
|
||||
id: int().primaryKey({ autoIncrement: true }),
|
||||
name: text().notNull().unique(),
|
||||
enabled: int("enabled", { mode: "boolean" }).notNull().default(true),
|
||||
type: text().$type<NotificationType>().notNull(),
|
||||
config: text("config", { mode: "json" }).$type<typeof notificationConfigSchema.inferOut>().notNull(),
|
||||
createdAt: int("created_at", { mode: "number" }).notNull().default(sql`(unixepoch())`),
|
||||
updatedAt: int("updated_at", { mode: "number" }).notNull().default(sql`(unixepoch())`),
|
||||
});
|
||||
export const notificationDestinationRelations = relations(notificationDestinationsTable, ({ many }) => ({
|
||||
schedules: many(backupScheduleNotificationsTable),
|
||||
}));
|
||||
export type NotificationDestination = typeof notificationDestinationsTable.$inferSelect;
|
||||
|
||||
/**
|
||||
* Backup Schedule Notifications Junction Table (Many-to-Many)
|
||||
*/
|
||||
export const backupScheduleNotificationsTable = sqliteTable(
|
||||
"backup_schedule_notifications_table",
|
||||
{
|
||||
scheduleId: int("schedule_id")
|
||||
.notNull()
|
||||
.references(() => backupSchedulesTable.id, { onDelete: "cascade" }),
|
||||
destinationId: int("destination_id")
|
||||
.notNull()
|
||||
.references(() => notificationDestinationsTable.id, { onDelete: "cascade" }),
|
||||
notifyOnStart: int("notify_on_start", { mode: "boolean" }).notNull().default(false),
|
||||
notifyOnSuccess: int("notify_on_success", { mode: "boolean" }).notNull().default(false),
|
||||
notifyOnFailure: int("notify_on_failure", { mode: "boolean" }).notNull().default(true),
|
||||
createdAt: int("created_at", { mode: "number" }).notNull().default(sql`(unixepoch())`),
|
||||
},
|
||||
(table) => [primaryKey({ columns: [table.scheduleId, table.destinationId] })],
|
||||
);
|
||||
export const backupScheduleNotificationRelations = relations(backupScheduleNotificationsTable, ({ one }) => ({
|
||||
schedule: one(backupSchedulesTable, {
|
||||
fields: [backupScheduleNotificationsTable.scheduleId],
|
||||
references: [backupSchedulesTable.id],
|
||||
}),
|
||||
destination: one(notificationDestinationsTable, {
|
||||
fields: [backupScheduleNotificationsTable.destinationId],
|
||||
references: [notificationDestinationsTable.id],
|
||||
}),
|
||||
}));
|
||||
export type BackupScheduleNotification = typeof backupScheduleNotificationsTable.$inferSelect;
|
||||
|
||||
@@ -15,6 +15,7 @@ import { systemController } from "./modules/system/system.controller";
|
||||
import { volumeController } from "./modules/volumes/volume.controller";
|
||||
import { backupScheduleController } from "./modules/backups/backups.controller";
|
||||
import { eventsController } from "./modules/events/events.controller";
|
||||
import { notificationsController } from "./modules/notifications/notifications.controller";
|
||||
import { handleServiceError } from "./utils/errors";
|
||||
import { logger } from "./utils/logger";
|
||||
import { shutdown } from "./modules/lifecycle/shutdown";
|
||||
@@ -46,6 +47,7 @@ const app = new Hono()
|
||||
.route("/api/v1/volumes", volumeController.use(requireAuth))
|
||||
.route("/api/v1/repositories", repositoriesController.use(requireAuth))
|
||||
.route("/api/v1/backups", backupScheduleController.use(requireAuth))
|
||||
.route("/api/v1/notifications", notificationsController.use(requireAuth))
|
||||
.route("/api/v1/system", systemController.use(requireAuth))
|
||||
.route("/api/v1/events", eventsController.use(requireAuth));
|
||||
|
||||
|
||||
@@ -23,6 +23,14 @@ import {
|
||||
type UpdateBackupScheduleDto,
|
||||
} from "./backups.dto";
|
||||
import { backupsService } from "./backups.service";
|
||||
import {
|
||||
getScheduleNotificationsDto,
|
||||
updateScheduleNotificationsBody,
|
||||
updateScheduleNotificationsDto,
|
||||
type GetScheduleNotificationsDto,
|
||||
type UpdateScheduleNotificationsDto,
|
||||
} from "../notifications/notifications.dto";
|
||||
import { notificationsService } from "../notifications/notifications.service";
|
||||
|
||||
export const backupScheduleController = new Hono()
|
||||
.get("/", listBackupSchedulesDto, async (c) => {
|
||||
@@ -87,4 +95,22 @@ export const backupScheduleController = new Hono()
|
||||
await backupsService.runForget(Number(scheduleId));
|
||||
|
||||
return c.json<RunForgetDto>({ success: true }, 200);
|
||||
});
|
||||
})
|
||||
.get("/:scheduleId/notifications", getScheduleNotificationsDto, async (c) => {
|
||||
const scheduleId = Number.parseInt(c.req.param("scheduleId"), 10);
|
||||
const assignments = await notificationsService.getScheduleNotifications(scheduleId);
|
||||
|
||||
return c.json<GetScheduleNotificationsDto>(assignments, 200);
|
||||
})
|
||||
.put(
|
||||
"/:scheduleId/notifications",
|
||||
updateScheduleNotificationsDto,
|
||||
validator("json", updateScheduleNotificationsBody),
|
||||
async (c) => {
|
||||
const scheduleId = Number.parseInt(c.req.param("scheduleId"), 10);
|
||||
const body = c.req.valid("json");
|
||||
const assignments = await notificationsService.updateScheduleNotifications(scheduleId, body.assignments);
|
||||
|
||||
return c.json<UpdateScheduleNotificationsDto>(assignments, 200);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -10,6 +10,7 @@ import { getVolumePath } from "../volumes/helpers";
|
||||
import type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backups.dto";
|
||||
import { toMessage } from "../../utils/errors";
|
||||
import { serverEvents } from "../../core/events";
|
||||
import { notificationsService } from "../notifications/notifications.service";
|
||||
|
||||
const runningBackups = new Map<number, AbortController>();
|
||||
|
||||
@@ -195,6 +196,15 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
repositoryName: repository.name,
|
||||
});
|
||||
|
||||
notificationsService
|
||||
.sendBackupNotification(scheduleId, "start", {
|
||||
volumeName: volume.name,
|
||||
repositoryName: repository.name,
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(`Failed to send backup start notification: ${toMessage(error)}`);
|
||||
});
|
||||
|
||||
const nextBackupAt = calculateNextRun(schedule.cronExpression);
|
||||
|
||||
await db
|
||||
@@ -262,6 +272,15 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
repositoryName: repository.name,
|
||||
status: "success",
|
||||
});
|
||||
|
||||
notificationsService
|
||||
.sendBackupNotification(scheduleId, "success", {
|
||||
volumeName: volume.name,
|
||||
repositoryName: repository.name,
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(`Failed to send backup success notification: ${toMessage(error)}`);
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(`Backup failed for volume ${volume.name} to repository ${repository.name}: ${toMessage(error)}`);
|
||||
|
||||
@@ -282,6 +301,16 @@ const executeBackup = async (scheduleId: number, manual = false) => {
|
||||
status: "error",
|
||||
});
|
||||
|
||||
notificationsService
|
||||
.sendBackupNotification(scheduleId, "failure", {
|
||||
volumeName: volume.name,
|
||||
repositoryName: repository.name,
|
||||
error: toMessage(error),
|
||||
})
|
||||
.catch((notifError) => {
|
||||
logger.error(`Failed to send backup failure notification: ${toMessage(notifError)}`);
|
||||
});
|
||||
|
||||
throw error;
|
||||
} finally {
|
||||
runningBackups.delete(scheduleId);
|
||||
|
||||
5
app/server/modules/notifications/builders/custom.ts
Normal file
5
app/server/modules/notifications/builders/custom.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { NotificationConfig } from "~/schemas/notifications";
|
||||
|
||||
export function buildCustomShoutrrrUrl(config: Extract<NotificationConfig, { type: "custom" }>): string {
|
||||
return config.shoutrrrUrl;
|
||||
}
|
||||
28
app/server/modules/notifications/builders/discord.ts
Normal file
28
app/server/modules/notifications/builders/discord.ts
Normal 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;
|
||||
}
|
||||
10
app/server/modules/notifications/builders/email.ts
Normal file
10
app/server/modules/notifications/builders/email.ts
Normal 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}`;
|
||||
}
|
||||
15
app/server/modules/notifications/builders/gotify.ts
Normal file
15
app/server/modules/notifications/builders/gotify.ts
Normal 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;
|
||||
}
|
||||
29
app/server/modules/notifications/builders/index.ts
Normal file
29
app/server/modules/notifications/builders/index.ts
Normal 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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
app/server/modules/notifications/builders/ntfy.ts
Normal file
28
app/server/modules/notifications/builders/ntfy.ts
Normal 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;
|
||||
}
|
||||
31
app/server/modules/notifications/builders/slack.ts
Normal file
31
app/server/modules/notifications/builders/slack.ts
Normal 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;
|
||||
}
|
||||
51
app/server/modules/notifications/notifications.controller.ts
Normal file
51
app/server/modules/notifications/notifications.controller.ts
Normal 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);
|
||||
});
|
||||
251
app/server/modules/notifications/notifications.dto.ts
Normal file
251
app/server/modules/notifications/notifications.dto.ts
Normal 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),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
405
app/server/modules/notifications/notifications.service.ts
Normal file
405
app/server/modules/notifications/notifications.service.ts
Normal 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,
|
||||
};
|
||||
43
app/server/utils/shoutrrr.ts
Normal file
43
app/server/utils/shoutrrr.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { safeSpawn } from "./spawn";
|
||||
import { logger } from "./logger";
|
||||
import { toMessage } from "./errors";
|
||||
|
||||
export interface SendNotificationParams {
|
||||
shoutrrrUrl: string;
|
||||
title: string;
|
||||
body: string;
|
||||
}
|
||||
|
||||
export async function sendNotification(params: SendNotificationParams) {
|
||||
const { shoutrrrUrl, title, body } = params;
|
||||
|
||||
try {
|
||||
const args = ["send", "--url", shoutrrrUrl, "--title", title, "--message", body];
|
||||
|
||||
logger.debug(`Sending notification via Shoutrrr: ${title}`);
|
||||
|
||||
const result = await safeSpawn({
|
||||
command: "shoutrrr",
|
||||
args,
|
||||
});
|
||||
|
||||
if (result.exitCode === 0) {
|
||||
logger.debug(`Notification sent successfully: ${title}`);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
const errorMessage = result.stderr || result.stdout || "Unknown error";
|
||||
logger.error(`Failed to send notification: ${errorMessage}`);
|
||||
return {
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
};
|
||||
} catch (error) {
|
||||
const errorMessage = toMessage(error);
|
||||
logger.error(`Error sending notification: ${errorMessage}`);
|
||||
return {
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user