summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorRaphael Kabo <raphaelkabo@hey.com>2023-10-07 14:30:24 +0100
committerRaphael Kabo <raphaelkabo@hey.com>2023-10-07 15:38:47 +0100
commitb795d07ed7a1b705b72b171f8e8de267a720223b (patch)
treeb8ae3df8dbb89f839f29328e817f030dc22b89f8 /src/lib
parent9341659fd7a791d77454dd33743e42d952dbd202 (diff)
refactor: event form and api routes
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/activitypub.ts9
-rw-r--r--src/lib/config.ts5
-rw-r--r--src/lib/email.ts151
-rw-r--r--src/lib/handlebars.ts23
-rw-r--r--src/lib/process.ts4
5 files changed, 190 insertions, 2 deletions
diff --git a/src/lib/activitypub.ts b/src/lib/activitypub.ts
new file mode 100644
index 0000000..0a3db7b
--- /dev/null
+++ b/src/lib/activitypub.ts
@@ -0,0 +1,9 @@
+import { Request } from "express";
+
+export const acceptsActivityPub = (req: Request) => {
+ return (
+ req.headers.accept &&
+ (req.headers.accept.includes("application/activity+json") ||
+ req.headers.accept.includes("application/ld+json"))
+ );
+};
diff --git a/src/lib/config.ts b/src/lib/config.ts
index 9577fd6..7b35b98 100644
--- a/src/lib/config.ts
+++ b/src/lib/config.ts
@@ -1,5 +1,6 @@
import fs from "fs";
import toml from "toml";
+import { exitWithError } from "./process.js";
interface GathioConfig {
general: {
@@ -46,8 +47,8 @@ export const getConfig = (): GathioConfig => {
) as GathioConfig;
return config;
} catch {
- console.error(
- "\x1b[31mConfiguration file not found! Have you renamed './config/config-example.toml' to './config/config.toml'?",
+ exitWithError(
+ "Configuration file not found! Have you renamed './config/config-example.toml' to './config/config.toml'?",
);
return process.exit(1);
}
diff --git a/src/lib/email.ts b/src/lib/email.ts
new file mode 100644
index 0000000..f1dc1ae
--- /dev/null
+++ b/src/lib/email.ts
@@ -0,0 +1,151 @@
+import { Request } from "express";
+import sgMail from "@sendgrid/mail";
+import nodemailer, { TransportOptions } from "nodemailer";
+import { getConfig } from "./config.js";
+import SMTPTransport from "nodemailer/lib/smtp-transport/index.js";
+import { exitWithError } from "./process.js";
+import { renderTemplate } from "./handlebars.js";
+const config = getConfig();
+
+type EmailTemplate =
+ | "addEventAttendee"
+ | "addEventComment"
+ | "createEvent"
+ | "createEventGroup"
+ | "deleteEvent"
+ | "editEvent"
+ | "eventGroupUpdated"
+ | "subscribed"
+ | "unattendEvent";
+
+export const initEmailService = async (): Promise<boolean> => {
+ if (process.env.CYPRESS || process.env.CI) {
+ console.log(
+ "Running in Cypress or CI, not initializing email service.",
+ );
+ return false;
+ }
+ switch (config.general.mail_service) {
+ case "sendgrid":
+ if (!config.sendgrid?.api_key) {
+ return exitWithError(
+ "Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.",
+ );
+ }
+ sgMail.setApiKey(config.sendgrid.api_key);
+ console.log("Sendgrid is ready to send emails.");
+ return true;
+ case "nodemailer":
+ if (
+ !config.nodemailer?.smtp_server ||
+ !config.nodemailer?.smtp_port ||
+ !config.nodemailer?.smtp_username ||
+ !config.nodemailer?.smtp_password
+ ) {
+ return exitWithError(
+ "Nodemailer is configured as the email service, but not all required fields are provided. Please provide all required fields in the config file.",
+ );
+ }
+ const nodemailerConfig = {
+ host: config.nodemailer?.smtp_server,
+ port: Number(config.nodemailer?.smtp_port) || 587,
+ auth: {
+ user: config.nodemailer?.smtp_username,
+ pass: config.nodemailer?.smtp_password,
+ },
+ } as SMTPTransport.Options;
+ const nodemailerTransporter =
+ nodemailer.createTransport(nodemailerConfig);
+ const nodemailerVerified = await nodemailerTransporter.verify();
+ if (nodemailerVerified) {
+ console.log("Nodemailer is ready to send emails.");
+ return true;
+ } else {
+ return exitWithError(
+ "Error verifying Nodemailer transporter. Please check your Nodemailer configuration.",
+ );
+ }
+ default:
+ console.warn(
+ "You have not configured this Gathio instance to send emails! This means that event creators will not receive emails when their events are created, which means they may end up locked out of editing events. Consider setting up an email service.",
+ );
+ return false;
+ }
+};
+
+export const sendEmail = async (
+ to: string,
+ subject: string,
+ text: string,
+ html?: string,
+): Promise<boolean> => {
+ switch (config.general.mail_service) {
+ case "sendgrid":
+ try {
+ await sgMail.send({
+ to,
+ from: config.general.email,
+ subject: `${config.general.site_name}: ${subject}`,
+ text,
+ html,
+ });
+ return true;
+ } catch (e: any) {
+ if (e.response) {
+ console.error(e.response.body);
+ } else {
+ console.error(e);
+ }
+ return false;
+ }
+ case "nodemailer":
+ try {
+ const nodemailerConfig = {
+ host: config.nodemailer?.smtp_server,
+ port: Number(config.nodemailer?.smtp_port) || 587,
+ auth: {
+ user: config.nodemailer?.smtp_username,
+ pass: config.nodemailer?.smtp_password,
+ },
+ } as SMTPTransport.Options;
+ const nodemailerTransporter =
+ nodemailer.createTransport(nodemailerConfig);
+ await nodemailerTransporter.sendMail({
+ from: config.general.email,
+ to,
+ subject,
+ text,
+ html,
+ });
+ return true;
+ } catch (e) {
+ console.error(e);
+ return false;
+ }
+ default:
+ return false;
+ }
+};
+
+export const sendEmailFromTemplate = async (
+ to: string,
+ subject: string,
+ template: EmailTemplate,
+ templateData: Record<string, unknown>,
+ req: Request,
+): Promise<boolean> => {
+ const html = await renderTemplate(req, `${template}/${template}Html`, {
+ siteName: config.general.site_name,
+ siteLogo: config.general.email_logo_url,
+ domain: config.general.domain,
+ cache: true,
+ layout: "email.handlebars",
+ ...templateData,
+ });
+ const text = await renderTemplate(
+ req,
+ `${template}/${template}Text`,
+ templateData,
+ );
+ return await sendEmail(to, subject, text, html);
+};
diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts
new file mode 100644
index 0000000..d5a8b6e
--- /dev/null
+++ b/src/lib/handlebars.ts
@@ -0,0 +1,23 @@
+import { Request } from "express";
+
+export const renderTemplate = async (
+ req: Request,
+ templateName: string,
+ data: Record<string, unknown>,
+): Promise<string> => {
+ return new Promise<string>((resolve, reject) => {
+ req.app
+ .get("hbsInstance")
+ .renderView(
+ `./views/emails/${templateName}.handlebars`,
+ data,
+ (err: any, html: string) => {
+ if (err) {
+ console.error(err);
+ reject(err);
+ }
+ resolve(html);
+ },
+ );
+ });
+};
diff --git a/src/lib/process.ts b/src/lib/process.ts
new file mode 100644
index 0000000..d43b3c7
--- /dev/null
+++ b/src/lib/process.ts
@@ -0,0 +1,4 @@
+export const exitWithError = (message: string) => {
+ console.error(`\x1b[31m${message}`);
+ process.exit(1);
+};