const SERVICE_NAME = "MatrixProvider"; import got from "got"; import { NotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js"; import type { AlertMatrixPayload, Notification } from "@/domain/notifications/notification.type.js"; import type { NotificationMessage } from "@/domain/notifications/notification.type.js"; import { getTestMessage, incidentUrl } from "@/domain/notifications/providers/utils.js"; import { randomUUID } from "crypto"; export class MatrixProvider extends NotificationProvider { sendTestAlert = async (notification: Partial) => { const { homeserverUrl, accessToken, roomId } = notification; if (!homeserverUrl || !accessToken || !roomId) { return false; } const body = { msgtype: "m.text", body: getTestMessage(), }; return await this.sendMessageWithBody(notification, body); }; sendMessage = async (notification: Notification, message: NotificationMessage): Promise => { const { homeserverUrl, accessToken, roomId } = notification; if (!homeserverUrl || !accessToken || !roomId) { return false; } const { plainText, htmlText } = this.buildMatrixMessage(message); const body = { msgtype: "m.text", body: plainText, format: "org.matrix.custom.html", formatted_body: htmlText, }; return this.sendMessageWithBody(notification, body); }; private sendMessageWithBody = async (notification: Partial, body: unknown): Promise => { const { homeserverUrl, accessToken, roomId } = notification; const baseUrl = this.normalizeHomeserverUrl(homeserverUrl as string); try { const resolvedRoomId = await this.resolveRoomId(baseUrl, accessToken as string, roomId as string); const txnId = randomUUID(); const url = `${baseUrl}/_matrix/client/v3/rooms/${encodeURIComponent(resolvedRoomId)}/send/m.room.message/${txnId}`; await got.put(url, { json: body, headers: { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, }, ...this.gotRequestOptions(), }); return true; } catch (error) { const err = error as Error; this.logger.warn({ message: `Matrix notification failed : ${err.message}`, service: SERVICE_NAME, method: "sendMessage", stack: err.stack, }); return false; } }; private normalizeHomeserverUrl(homeserverUrl: string): string { return homeserverUrl.replace(/\/+$/, ""); } /** * The send endpoint only accepts a room ID (`!id:server`). Element shows users the room * alias (`#name:server`) instead, and an unresolved alias breaks the request outright: * the `#` opens a URI fragment, so the PUT lands on /rooms/ rather than the send * endpoint. Resolve aliases through the directory first, per the client-server spec. */ private resolveRoomId = async (baseUrl: string, accessToken: string, roomIdOrAlias: string): Promise => { if (!roomIdOrAlias.startsWith("#")) { return roomIdOrAlias; } const url = `${baseUrl}/_matrix/client/v3/directory/room/${encodeURIComponent(roomIdOrAlias)}`; const response = await got.get(url, { headers: { Authorization: `Bearer ${accessToken}` }, responseType: "json", ...this.gotRequestOptions(), }); const roomId = (response.body as { room_id?: string })?.room_id; if (!roomId) { throw new Error(`Matrix directory returned no room_id for alias ${roomIdOrAlias}`); } return roomId; }; /** * Build Matrix message from NotificationMessage * Returns both plain text and HTML formatted versions * Matrix supports HTML subset for rich formatting */ private buildMatrixMessage(message: NotificationMessage): AlertMatrixPayload { const plainLines: string[] = []; const htmlLines: string[] = []; // Determine color based on severity const colorMap = { critical: "#FF0000", // Red warning: "#FFA500", // Orange success: "#00FF00", // Green info: "#0000FF", // Blue }; const color = colorMap[message.severity] || "#808080"; // Title plainLines.push(`# ${message.content.title}`); htmlLines.push(`

${this.escapeHtml(message.content.title)}

`); // Summary plainLines.push(""); plainLines.push(message.content.summary); htmlLines.push(`

${this.escapeHtml(message.content.summary)}

`); // Monitor details plainLines.push(""); plainLines.push("## Monitor Details"); plainLines.push(`- Name: ${message.monitor.name}`); plainLines.push(`- URL: ${message.monitor.url}`); plainLines.push(`- Type: ${message.monitor.type}`); plainLines.push(`- Status: ${message.monitor.status}`); htmlLines.push(`

Monitor Details

`); htmlLines.push(`
    `); htmlLines.push(`
  • Name: ${this.escapeHtml(message.monitor.name)}
  • `); htmlLines.push(`
  • URL: ${this.escapeHtml(message.monitor.url)}
  • `); htmlLines.push(`
  • Type: ${this.escapeHtml(message.monitor.type)}
  • `); htmlLines.push(`
  • Status: ${this.escapeHtml(message.monitor.status)}
  • `); htmlLines.push(`
`); // Threshold breaches (if any) if (message.content.thresholds && message.content.thresholds.length > 0) { plainLines.push(""); plainLines.push("## Threshold Breaches"); htmlLines.push(`

Threshold Breaches

`); htmlLines.push(`
    `); message.content.thresholds.forEach((breach) => { plainLines.push(`- ${breach.metric.toUpperCase()}: ${breach.formattedValue} (threshold: ${breach.threshold}${breach.unit})`); htmlLines.push( `
  • ${this.escapeHtml(breach.metric.toUpperCase())}: ${this.escapeHtml(breach.formattedValue)} (threshold: ${breach.threshold}${this.escapeHtml(breach.unit)})
  • ` ); }); htmlLines.push(`
`); } // Additional details (if any) if (message.content.details && message.content.details.length > 0) { plainLines.push(""); plainLines.push("## Additional Information"); htmlLines.push(`

Additional Information

`); htmlLines.push(`
    `); message.content.details.forEach((detail) => { plainLines.push(`- ${detail}`); htmlLines.push(`
  • ${this.escapeHtml(detail)}
  • `); }); htmlLines.push(`
`); } // Incident link (if incident exists) if (message.content.incident) { const url = incidentUrl(message); plainLines.push(""); plainLines.push(`View Incident: ${url}`); htmlLines.push(`

View Incident

`); } // Footer with timestamp plainLines.push(""); plainLines.push(`Checkmate | ${new Date(message.content.timestamp).toUTCString()}`); htmlLines.push(`
`); htmlLines.push(`

Checkmate | ${new Date(message.content.timestamp).toUTCString()}

`); return { plainText: plainLines.join("\n"), htmlText: htmlLines.join(""), }; } /** * Escape HTML special characters for safe rendering */ private escapeHtml(text: string): string { return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } }