feat: move from matrix to email notifs (#2)
Some checks failed
Node.js CI / Lint and Test (push) Successful in 56s
Code Analysis / SonarQube (push) Failing after 1m12s

### Explanation

_No response_

### Issue

_No response_

### Attestations

- [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [x] I have pinned the dependencies to a specific patch version.

### Style

- [x] I have run the linter and resolved any errors.
- [x] My pull request uses an appropriate title, matching the conventional commit standards.
- [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [ ] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [ ] All new and existing tests pass locally with my changes.
- [ ] Code coverage remains at or above the configured threshold.

### Documentation

_No response_

### Versioning

Minor - My pull request introduces a new non-breaking feature.

Reviewed-on: #2
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit is contained in:
2025-03-11 14:18:35 -07:00
committed by Naomi Carrigan
parent 11a3249bdb
commit 30bc85772f
6 changed files with 71 additions and 235 deletions

View File

@ -4,37 +4,6 @@
* @author Naomi Carrigan
*/
import { createClient } from "matrix-js-sdk";
import { instantiateServer } from "./server/serve.js";
import type { Logger } from "matrix-js-sdk/lib/logger.js";
const logger: Logger = {
debug: () => {
return null;
},
error: () => {
return null;
},
getChild: () => {
return logger;
},
info: () => {
return null;
},
trace: () => {
return null;
},
warn: () => {
return null;
},
};
const client = createClient({
accessToken: process.env.MATRIX_ACCESS_TOKEN ?? "",
baseUrl: "https://matrix.nhcarrigan.com",
logger: logger,
userId: "@alerts:matrix.nhcarrigan.com",
});
await client.startClient({ initialSyncLimit: 10 });
instantiateServer(client);
instantiateServer();

36
src/modules/sendMail.ts Normal file
View File

@ -0,0 +1,36 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { createTransport } from "nodemailer";
// eslint-disable-next-line @typescript-eslint/naming-convention -- This is a type import.
import type SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
/**
* Sends an email.
* @param subject - The subject of the email.
* @param body - The text content of the email.
*/
export const sendMail = async(subject: string, body: string): Promise<void> => {
const options: SMTPTransport["options"] = {
auth: {
pass: process.env.EMAIL_PASSWORD ?? "",
type: "login",
user: "noreply@nhcarrigan.com",
},
host: "mail.nhcarrigan.com",
port: 465,
secure: true,
};
const defaults: SMTPTransport["options"] = {
from: "noreply@nhcarrigan.com",
to: "logs@nhcarrigan.com",
};
const transport = createTransport(options, defaults);
await transport.sendMail({
subject: subject,
text: body,
});
};

View File

@ -5,8 +5,8 @@
*/
import fastify from "fastify";
import { MsgType, type MatrixClient } from "matrix-js-sdk";
import { auth } from "../modules/auth.js";
import { sendMail } from "../modules/sendMail.js";
import { errorSchema } from "../schemas/errorSchema.js";
import { logSchema } from "../schemas/logSchema.js";
import { uptimeSchema } from "../schemas/uptimeSchema.js";
@ -20,14 +20,14 @@ const html = `<!DOCTYPE html>
<title>Rosalia Nightsong</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="A basic web server and Matrix bot that allows us to pipe logs and errors from our applications into a Matrix room." />
<meta name="description" content="A basic web server that allows us to pipe logs and errors from our applications into our inbox." />
<script src="https://cdn.nhcarrigan.com/headers/index.js" async defer></script>
</head>
<body>
<main>
<h1>Rosalia Nightsong</h1>
<section>
<p>A basic web server and Matrix bot that allows us to pipe logs and errors from our applications into a Matrix room.</p>
<p>A basic web server that allows us to pipe logs and errors from our applications into our inbox.</p>
</section>
<section>
<h2>Links</h2>
@ -53,10 +53,9 @@ const html = `<!DOCTYPE html>
/**
* Starts up the server to receive events.
* @param client - The authenticated Matrix client.
*/
// eslint-disable-next-line max-lines-per-function -- This function is long because it is setting up a server.
export const instantiateServer = (client: MatrixClient): void => {
export const instantiateServer = (): void => {
try {
const server = fastify({
logger: false,
@ -74,13 +73,7 @@ export const instantiateServer = (client: MatrixClient): void => {
return;
}
const { application, level, message } = request.body;
await client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `**${application}** - *${level}*\n${message}`,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `<strong>${application}</strong> - <em>${level}</em><br>${message}`,
msgtype: MsgType.Text,
});
await sendMail(`[${level}]: ${application}`, message);
await response.status(200).send({ success: true });
});
@ -94,13 +87,7 @@ export const instantiateServer = (client: MatrixClient): void => {
return;
}
const { application, context, stack, message } = request.body;
await client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `**${application}** - *Error in ${context}*\n${message}\n\`\`\`\n${stack}\n\`\`\``,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `<strong>${application}</strong> - <em>Error in ${context}</em><br>${message}<br><code>${stack}</code>`,
msgtype: MsgType.Text,
});
await sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${stack}`);
await response.status(200).send({ success: true });
},
);
@ -115,13 +102,7 @@ export const instantiateServer = (client: MatrixClient): void => {
return;
}
const { application, message } = request.body;
await client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `${message}\n${application}`,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `${message}<br><sub>${application}</sub>`,
msgtype: MsgType.Text,
});
await sendMail(`[UPTIME]: ${application}`, message);
await response.status(200).send({ success: true });
},
);
@ -131,36 +112,18 @@ export const instantiateServer = (client: MatrixClient): void => {
if (error) {
const { message, stack } = error;
const context = "Server Startup";
void client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `**${application}** - *Error in ${context}*\n${message}\n\`\`\`\n${stack ?? "No stack trace available."}\n\`\`\``,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `<strong>${application}</strong> - <em>Error in ${context}</em><br>${message}<br><code>${stack ?? "No stack trace available."}</code>`,
msgtype: MsgType.Text,
});
void sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${String(stack)}`);
return;
}
const level = "debug";
const message = `Server listening on port 5003.`;
void client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `**${application}** - *${level}*\n${message}`,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `<strong>${application}</strong> - <em>${level}</em><br>${message}`,
msgtype: MsgType.Text,
});
void sendMail(`[${level}]: ${application}`, message);
});
} catch (error) {
const application = "Alert Server";
const context = "Server Startup";
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Totally being lazy.
const { message, stack } = error as Error;
void client.sendMessage(process.env.MATRIX_ROOM_ID ?? "", {
body: `**${application}** - *Error in ${context}*\n${message}\n\`\`\`\n${stack}\n\`\`\``,
format: "org.matrix.custom.html",
// eslint-disable-next-line @typescript-eslint/naming-convention -- Requirement of the SDK.
formatted_body: `<strong>${application}</strong> - <em>Error in ${context}</em><br>${message}<br><code>${stack}</code>`,
msgtype: MsgType.Text,
});
void sendMail(`[ERROR]: ${context} - ${application}`, `${message}\n\n${stack}`);
}
};