From b795d07ed7a1b705b72b171f8e8de267a720223b Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Sat, 7 Oct 2023 14:30:24 +0100 Subject: refactor: event form and api routes --- src/lib/activitypub.ts | 9 +++ src/lib/config.ts | 5 +- src/lib/email.ts | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/handlebars.ts | 23 ++++++++ src/lib/process.ts | 4 ++ 5 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/lib/activitypub.ts create mode 100644 src/lib/email.ts create mode 100644 src/lib/handlebars.ts create mode 100644 src/lib/process.ts (limited to 'src/lib') 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 => { + 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 => { + 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, + req: Request, +): Promise => { + 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, +): Promise => { + return new Promise((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); +}; -- cgit v1.2.3