From aace2c7e6ccb6e74df83faac74c427d43bfaf79b Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Wed, 23 Apr 2025 15:06:54 -0700 Subject: Fix ReferenceError: nodemailerTransporter is not defined Part of https://github.com/lowercasename/gathio/pull/200 was migrating more code to use the shared init email function, but all the local usages of nodemailerTransporter were missed --- src/lib/email.ts | 57 +++++++++++++++++++++++++++++++++++++++------------ src/lib/handlebars.ts | 27 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) (limited to 'src/lib') diff --git a/src/lib/email.ts b/src/lib/email.ts index 7b7a7a1..e7243aa 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,10 +1,10 @@ -import { Request } from "express"; import sgMail from "@sendgrid/mail"; import nodemailer, { Transporter } 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"; +import { ExpressHandlebars } from "express-handlebars"; const config = getConfig(); type EmailTemplate = @@ -36,10 +36,12 @@ export const initEmailService = async (): Promise => { sgMail.setApiKey(config.sendgrid.api_key); console.log("Sendgrid is ready to send emails."); return true; - case "nodemailer": - let nodemailerTransporter:Transporter|undefined = undefined; + case "nodemailer": { + let nodemailerTransporter: Transporter | undefined = undefined; if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport(config.nodemailer?.smtp_url); + nodemailerTransporter = nodemailer.createTransport( + config.nodemailer?.smtp_url, + ); } else { if ( !config.nodemailer?.smtp_server || @@ -52,7 +54,7 @@ export const initEmailService = async (): Promise => { const nodemailerConfig = { host: config.nodemailer?.smtp_server, port: Number(config.nodemailer?.smtp_port) || 587, - tls: { + tls: { // do not fail on invalid certs rejectUnauthorized: false, }, @@ -61,10 +63,11 @@ export const initEmailService = async (): Promise => { if (config.nodemailer?.smtp_username) { nodemailerConfig.auth = { user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password + pass: config.nodemailer?.smtp_password, }; } - nodemailerTransporter = nodemailer.createTransport(nodemailerConfig); + nodemailerTransporter = + nodemailer.createTransport(nodemailerConfig); } const nodemailerVerified = await nodemailerTransporter.verify(); @@ -76,6 +79,7 @@ export const initEmailService = async (): Promise => { "Error verifying Nodemailer transporter. Please check your Nodemailer configuration.", ); } + } case "none": default: console.warn( @@ -85,10 +89,34 @@ export const initEmailService = async (): Promise => { } }; -export const sendEmail = async ( +export const sendTemplatedEmail = async ( + hbs: ExpressHandlebars, to: string, bcc: string, subject: string, + template: string, + data: object, +): Promise => { + const [html, text] = await Promise.all([ + hbs.renderView(`./views/emails/${template}Html.handlebars`, { + cache: true, + layout: "email.handlebars", + ...data, + }), + hbs.renderView(`./views/emails/${template}Text.handlebars`, { + cache: true, + layout: "email.handlebars", + ...data, + }), + ]); + + return await sendEmail(to, bcc, subject, text, html); +}; + +export const sendEmail = async ( + to: string | string[], + bcc: string | string[] | undefined, + subject: string, text: string, html?: string, ): Promise => { @@ -104,7 +132,7 @@ export const sendEmail = async ( html, }); return true; - } catch (e: any) { + } catch (e: Error) { if (e.response) { console.error(e.response.body); } else { @@ -114,9 +142,11 @@ export const sendEmail = async ( } case "nodemailer": try { - let nodemailerTransporter:Transporter|undefined = undefined; + let nodemailerTransporter: Transporter | undefined = undefined; if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport(config.nodemailer?.smtp_url); + nodemailerTransporter = nodemailer.createTransport( + config.nodemailer?.smtp_url, + ); } else { const nodemailerConfig = { host: config.nodemailer?.smtp_server, @@ -126,11 +156,12 @@ export const sendEmail = async ( if (config.nodemailer?.smtp_username) { nodemailerConfig.auth = { user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password + pass: config.nodemailer?.smtp_password, }; } - nodemailerTransporter = nodemailer.createTransport(nodemailerConfig); + nodemailerTransporter = + nodemailer.createTransport(nodemailerConfig); } await nodemailerTransporter.sendMail({ envelope: { diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts index d5a8b6e..42f8010 100644 --- a/src/lib/handlebars.ts +++ b/src/lib/handlebars.ts @@ -1,4 +1,5 @@ import { Request } from "express"; +import { ExpressHandlebars } from "express-handlebars"; export const renderTemplate = async ( req: Request, @@ -21,3 +22,29 @@ export const renderTemplate = async ( ); }); }; + +export const renderEmail = async ( + hbsInstance: ExpressHandlebars, + templateName: string, + data: Record, +): Promise<{ html: string, text: string }> => { + const [html, text] = await Promise.all([ + hbsInstance.renderView( + `./views/emails/${templateName}Html.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + hbsInstance.renderView( + `./views/emails/${templateName}Text.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + ]); + return { html, text } +} -- cgit v1.2.3 From a8a17443c2d070d2d23920ffff7e4a43c905698c Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Wed, 23 Apr 2025 17:27:55 -0700 Subject: Refactor for everywhere to use sendEmailFromTemplate everywhere * Created a singleton to house handlebars so req doesn't need to be passed everywhere (should make unit testing easier later) * Subjectline for sendgrid and nodemailer are both always prefixed in sendEmail() * removed prefix subjectline from all other email places * added a couple if (!event) { return 404 } to help make typescript happy * some minor eslint auto fixes (looks like let => const where it can) --- src/lib/email.ts | 101 +++++++++++++++++++++++--------------------------- src/lib/handlebars.ts | 97 +++++++++++++++++++++++++----------------------- 2 files changed, 97 insertions(+), 101 deletions(-) (limited to 'src/lib') diff --git a/src/lib/email.ts b/src/lib/email.ts index e7243aa..57f69f5 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,13 +1,15 @@ import sgMail from "@sendgrid/mail"; +import sgHelpers from "@sendgrid/helpers"; + import nodemailer, { Transporter } 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"; -import { ExpressHandlebars } from "express-handlebars"; +import { HandlebarsSingleton } from "./handlebars.js"; + const config = getConfig(); -type EmailTemplate = +type EmailTemplateName = | "addEventAttendee" | "addEventComment" | "createEvent" @@ -16,6 +18,7 @@ type EmailTemplate = | "deleteEvent" | "editEvent" | "eventGroupUpdated" + | "removeEventAttendee" | "subscribed" | "unattendEvent"; @@ -89,30 +92,6 @@ export const initEmailService = async (): Promise => { } }; -export const sendTemplatedEmail = async ( - hbs: ExpressHandlebars, - to: string, - bcc: string, - subject: string, - template: string, - data: object, -): Promise => { - const [html, text] = await Promise.all([ - hbs.renderView(`./views/emails/${template}Html.handlebars`, { - cache: true, - layout: "email.handlebars", - ...data, - }), - hbs.renderView(`./views/emails/${template}Text.handlebars`, { - cache: true, - layout: "email.handlebars", - ...data, - }), - ]); - - return await sendEmail(to, bcc, subject, text, html); -}; - export const sendEmail = async ( to: string | string[], bcc: string | string[] | undefined, @@ -132,11 +111,11 @@ export const sendEmail = async ( html, }); return true; - } catch (e: Error) { - if (e.response) { - console.error(e.response.body); + } catch (e: unknown | sgHelpers.classes.ResponseError) { + if (e instanceof sgHelpers.classes.ResponseError) { + console.error('sendgrid error', e.response.body); } else { - console.error(e); + console.error('sendgrid error', e); } return false; } @@ -164,15 +143,10 @@ export const sendEmail = async ( nodemailer.createTransport(nodemailerConfig); } await nodemailerTransporter.sendMail({ - envelope: { - from: config.general.email, - to, - bcc, - }, from: config.general.email, to, bcc, - subject, + subject: `${config.general.site_name}: ${subject}`, text, html, }); @@ -187,25 +161,42 @@ export const sendEmail = async ( }; export const sendEmailFromTemplate = async ( - to: string, - bcc: string, + to: string | string[], + bcc: string | string[] | undefined, subject: string, - template: EmailTemplate, - templateData: Record, - req: Request, + templateName: EmailTemplateName, + templateData: object, ): 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, - ); + const [html, text] = await Promise.all([ + HandlebarsSingleton.instance.renderView( + `./views/emails/${templateName}/${templateName}Html.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + HandlebarsSingleton.instance.renderView( + `./views/emails/${templateName}/${templateName}Text.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + ]); + return await sendEmail(to, bcc, subject, text, html); }; diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts index 42f8010..6d4f796 100644 --- a/src/lib/handlebars.ts +++ b/src/lib/handlebars.ts @@ -1,50 +1,55 @@ -import { Request } from "express"; -import { ExpressHandlebars } from "express-handlebars"; +import hbs, { ExpressHandlebars } from "express-handlebars"; +import { RenderViewOptions } from "express-handlebars/types/index.js"; -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); +export class HandlebarsSingleton { + static #instance: HandlebarsSingleton; + hbsInstance: hbs.ExpressHandlebars; + + private constructor() { + this.hbsInstance = hbs.create({ + defaultLayout: "main", + partialsDir: ["views/partials/"], + layoutsDir: "views/layouts/", + helpers: { + plural: function (number: number, text: string) { + const singular = number === 1; + // If no text parameter was given, just return a conditional s. + if (typeof text !== "string") return singular ? "" : "s"; + // Split with regex into group1/group2 or group1(group3) + const match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); + // If no match, just append a conditional s. + if (!match) return text + (singular ? "" : "s"); + // We have a good match, so fire away + return ( + (singular && match[1]) || // Singular case + match[2] || // Plural case: 'bagel/bagels' --> bagels + match[1] + (match[3] || "s") + ); // Plural case: 'bagel(s)' or 'bagel' --> bagels + }, + json: function (context: object) { + return JSON.stringify(context); }, - ); - }); -}; + }, + }); + } + + public static get instance(): HandlebarsSingleton { + if (!HandlebarsSingleton.#instance) { + HandlebarsSingleton.#instance = new HandlebarsSingleton(); + } + + return HandlebarsSingleton.#instance; + } + + public get engine(): ExpressHandlebars["engine"] { + return this.hbsInstance.engine; + } -export const renderEmail = async ( - hbsInstance: ExpressHandlebars, - templateName: string, - data: Record, -): Promise<{ html: string, text: string }> => { - const [html, text] = await Promise.all([ - hbsInstance.renderView( - `./views/emails/${templateName}Html.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - hbsInstance.renderView( - `./views/emails/${templateName}Text.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - ]); - return { html, text } + /** + * Finally, any singleton can define some business logic, which can be + * executed on its instance. + */ + public renderView(viewPath: string, options: RenderViewOptions): Promise { + return this.hbsInstance.renderView(viewPath, options); + } } -- cgit v1.2.3 From 14041a319cace03cfc23c0a919ed81fb141f88ce Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Fri, 25 Apr 2025 21:43:39 -0700 Subject: Refactor to have email service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move hbsInstance back to app * Add email and hbs to req so typescript 🎉🎉🎉 * Init Email and config once --- src/lib/config.ts | 70 +++++++++----- src/lib/email.ts | 263 +++++++++++++++++++++++++------------------------- src/lib/handlebars.ts | 55 ----------- 3 files changed, 175 insertions(+), 213 deletions(-) delete mode 100644 src/lib/handlebars.ts (limited to 'src/lib') diff --git a/src/lib/config.ts b/src/lib/config.ts index 003a714..6642eef 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -110,46 +110,46 @@ export const instanceRules = (): InstanceRule[] => { rules.push( config.general.show_public_event_list ? { - text: "Public events and groups are displayed on the homepage", - icon: "fas fa-eye", - } + text: "Public events and groups are displayed on the homepage", + icon: "fas fa-eye", + } : { - text: "Events and groups can only be accessed by direct link", - icon: "fas fa-eye-slash", - }, + text: "Events and groups can only be accessed by direct link", + icon: "fas fa-eye-slash", + }, ); rules.push( config.general.creator_email_addresses?.length ? { - text: "Only specific people can create events and groups", - icon: "fas fa-user-check", - } + text: "Only specific people can create events and groups", + icon: "fas fa-user-check", + } : { - text: "Anyone can create events and groups", - icon: "fas fa-users", - }, + text: "Anyone can create events and groups", + icon: "fas fa-users", + }, ); rules.push( config.general.delete_after_days > 0 ? { - text: `Events are automatically deleted ${config.general.delete_after_days} days after they end`, - icon: "far fa-calendar-times", - } + text: `Events are automatically deleted ${config.general.delete_after_days} days after they end`, + icon: "far fa-calendar-times", + } : { - text: "Events are permanent, and are never automatically deleted", - icon: "far fa-calendar-check", - }, + text: "Events are permanent, and are never automatically deleted", + icon: "far fa-calendar-check", + }, ); rules.push( config.general.is_federated ? { - text: "This instance federates with other instances using ActivityPub", - icon: "fas fa-globe", - } + text: "This instance federates with other instances using ActivityPub", + icon: "fas fa-globe", + } : { - text: "This instance does not federate with other instances", - icon: "fas fa-globe", - }, + text: "This instance does not federate with other instances", + icon: "fas fa-globe", + }, ); return rules; }; @@ -179,17 +179,35 @@ export const instanceDescription = (): string => { } }; +let _resolvedConfig: GathioConfig | null = null; // Attempt to load our global config. Will stop the app if the config file // cannot be read (there's no point trying to continue!) export const getConfig = (): GathioConfig => { + if (_resolvedConfig) { + return _resolvedConfig; + } + try { const config = toml.parse( fs.readFileSync("./config/config.toml", "utf-8"), ) as GathioConfig; - return { + const resolvedConfig = { ...defaultConfig, ...config, - }; + } + if (process.env.CYPRESS || process.env.CI) { + config.general.mail_service = "none"; + console.log( + "Running in Cypress or CI, not initializing email service.", + ); + } else if (config.general.mail_service === "none") { + 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.", + ); + } + + _resolvedConfig = resolvedConfig; + return resolvedConfig; } catch { exitWithError( "Configuration file not found! Have you renamed './config/config-example.toml' to './config/config.toml'?", diff --git a/src/lib/email.ts b/src/lib/email.ts index 57f69f5..7c7c2dd 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,11 +1,10 @@ import sgMail from "@sendgrid/mail"; import sgHelpers from "@sendgrid/helpers"; - +import { ExpressHandlebars } from "express-handlebars"; import nodemailer, { Transporter } from "nodemailer"; -import { getConfig } from "./config.js"; +import { GathioConfig, getConfig } from "./config.js"; import SMTPTransport from "nodemailer/lib/smtp-transport/index.js"; import { exitWithError } from "./process.js"; -import { HandlebarsSingleton } from "./handlebars.js"; const config = getConfig(); @@ -22,58 +21,65 @@ type EmailTemplateName = | "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": { - let nodemailerTransporter: Transporter | undefined = undefined; - if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport( - config.nodemailer?.smtp_url, - ); - } else { - if ( - !config.nodemailer?.smtp_server || - !config.nodemailer?.smtp_port - ) { +export class EmailService { + nodemailerTransporter: Transporter | undefined = undefined; + sgMail: typeof sgMail | undefined = undefined; + hbs: ExpressHandlebars + + public constructor(config: GathioConfig, hbs: ExpressHandlebars) { + this.hbs = hbs; + switch (config.general.mail_service) { + case "sendgrid": { + if (!config.sendgrid?.api_key) { 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.", + "Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.", ); } - const nodemailerConfig = { - host: config.nodemailer?.smtp_server, - port: Number(config.nodemailer?.smtp_port) || 587, - tls: { - // do not fail on invalid certs - rejectUnauthorized: false, - }, - } as SMTPTransport.Options; + this.sgMail = sgMail; + this.sgMail.setApiKey(config.sendgrid.api_key); + console.log("Sendgrid is ready to send emails."); + break; + } + case "nodemailer": { + if (config.nodemailer?.smtp_url) { + this.nodemailerTransporter = nodemailer.createTransport( + config.nodemailer?.smtp_url, + ); + } else { + if ( + !config.nodemailer?.smtp_server || + !config.nodemailer?.smtp_port + ) { + 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, + tls: { + // do not fail on invalid certs + rejectUnauthorized: false, + }, + } as SMTPTransport.Options; - if (config.nodemailer?.smtp_username) { - nodemailerConfig.auth = { - user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password, - }; + if (config.nodemailer?.smtp_username) { + nodemailerConfig.auth = { + user: config.nodemailer?.smtp_username, + pass: config.nodemailer?.smtp_password, + }; + } + this.nodemailerTransporter = + nodemailer.createTransport(nodemailerConfig); } - nodemailerTransporter = - nodemailer.createTransport(nodemailerConfig); } - const nodemailerVerified = await nodemailerTransporter.verify(); + } + } + + public async verify(): Promise { + if (this.nodemailerTransporter) { + const nodemailerVerified = await this.nodemailerTransporter.verify(); if (nodemailerVerified) { console.log("Nodemailer is ready to send emails."); return true; @@ -83,30 +89,29 @@ export const initEmailService = async (): Promise => { ); } } - case "none": - 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; + return true; } -}; -export const sendEmail = async ( - to: string | string[], - bcc: string | string[] | undefined, - subject: string, - text: string, - html?: string, -): Promise => { - switch (config.general.mail_service) { - case "sendgrid": + public async sendEmail({ + to, + bcc, + subject, + text, + html, + }: { + to: string | string[]; + bcc?: string | string[]; + subject: string; + text: string; + html?: string; + }): Promise { + if (this.sgMail) { try { - await sgMail.send({ + await this.sgMail.send({ to, bcc, from: config.general.email, - subject: `${config.general.site_name}: ${subject}`, + subject, text, html, }); @@ -119,34 +124,13 @@ export const sendEmail = async ( } return false; } - case "nodemailer": + } else if (this.nodemailerTransporter) { try { - let nodemailerTransporter: Transporter | undefined = undefined; - if (config.nodemailer?.smtp_url) { - nodemailerTransporter = nodemailer.createTransport( - config.nodemailer?.smtp_url, - ); - } else { - const nodemailerConfig = { - host: config.nodemailer?.smtp_server, - port: Number(config.nodemailer?.smtp_port) || 587, - } as SMTPTransport.Options; - - if (config.nodemailer?.smtp_username) { - nodemailerConfig.auth = { - user: config.nodemailer?.smtp_username, - pass: config.nodemailer?.smtp_password, - }; - } - - nodemailerTransporter = - nodemailer.createTransport(nodemailerConfig); - } - await nodemailerTransporter.sendMail({ + await this.nodemailerTransporter.sendMail({ from: config.general.email, to, bcc, - subject: `${config.general.site_name}: ${subject}`, + subject, text, html, }); @@ -155,48 +139,63 @@ export const sendEmail = async ( console.error(e); return false; } - default: - return false; + } else { + // no mailer, so noop + return true; + } } -}; -export const sendEmailFromTemplate = async ( - to: string | string[], - bcc: string | string[] | undefined, - subject: string, - templateName: EmailTemplateName, - templateData: object, -): Promise => { - const [html, text] = await Promise.all([ - HandlebarsSingleton.instance.renderView( - `./views/emails/${templateName}/${templateName}Html.handlebars`, - { - domain: config.general.domain, - contactEmail: config.general.email, - siteName: config.general.site_name, - mailService: config.general.mail_service, - siteLogo: config.general.email_logo_url, - isFederated: config.general.is_federated || true, - cache: true, - layout: "email.handlebars", - ...templateData, - } - ), - HandlebarsSingleton.instance.renderView( - `./views/emails/${templateName}/${templateName}Text.handlebars`, - { - domain: config.general.domain, - contactEmail: config.general.email, - siteName: config.general.site_name, - mailService: config.general.mail_service, - siteLogo: config.general.email_logo_url, - isFederated: config.general.is_federated || true, - cache: true, - layout: "email.handlebars", - ...templateData, - } - ), - ]); + public async sendEmailFromTemplate({ + to, + bcc = "", + subject, + templateName, + templateData = {} + }: { + to: string | string[]; + bcc?: string | string[] | undefined; + subject: string; + templateName: EmailTemplateName; + templateData?: object; + }, + ): Promise { + const [html, text] = await Promise.all([ + this.hbs.renderView( + `./views/emails/${templateName}/${templateName}Html.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + this.hbs.renderView( + `./views/emails/${templateName}/${templateName}Text.handlebars`, + { + domain: config.general.domain, + contactEmail: config.general.email, + siteName: config.general.site_name, + mailService: config.general.mail_service, + siteLogo: config.general.email_logo_url, + isFederated: config.general.is_federated || true, + cache: true, + layout: "email.handlebars", + ...templateData, + } + ), + ]); - return await sendEmail(to, bcc, subject, text, html); -}; + return this.sendEmail({ + to, + bcc, + subject: `${config.general.site_name}: ${subject}`, + text, + html + }); + } +} diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts deleted file mode 100644 index 6d4f796..0000000 --- a/src/lib/handlebars.ts +++ /dev/null @@ -1,55 +0,0 @@ -import hbs, { ExpressHandlebars } from "express-handlebars"; -import { RenderViewOptions } from "express-handlebars/types/index.js"; - -export class HandlebarsSingleton { - static #instance: HandlebarsSingleton; - hbsInstance: hbs.ExpressHandlebars; - - private constructor() { - this.hbsInstance = hbs.create({ - defaultLayout: "main", - partialsDir: ["views/partials/"], - layoutsDir: "views/layouts/", - helpers: { - plural: function (number: number, text: string) { - const singular = number === 1; - // If no text parameter was given, just return a conditional s. - if (typeof text !== "string") return singular ? "" : "s"; - // Split with regex into group1/group2 or group1(group3) - const match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); - // If no match, just append a conditional s. - if (!match) return text + (singular ? "" : "s"); - // We have a good match, so fire away - return ( - (singular && match[1]) || // Singular case - match[2] || // Plural case: 'bagel/bagels' --> bagels - match[1] + (match[3] || "s") - ); // Plural case: 'bagel(s)' or 'bagel' --> bagels - }, - json: function (context: object) { - return JSON.stringify(context); - }, - }, - }); - } - - public static get instance(): HandlebarsSingleton { - if (!HandlebarsSingleton.#instance) { - HandlebarsSingleton.#instance = new HandlebarsSingleton(); - } - - return HandlebarsSingleton.#instance; - } - - public get engine(): ExpressHandlebars["engine"] { - return this.hbsInstance.engine; - } - - /** - * Finally, any singleton can define some business logic, which can be - * executed on its instance. - */ - public renderView(viewPath: string, options: RenderViewOptions): Promise { - return this.hbsInstance.renderView(viewPath, options); - } -} -- cgit v1.2.3 From 53e7e321d20cd7071ff617ecfcf42f6122020bcd Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Mon, 28 Apr 2025 19:16:28 -0700 Subject: switch to 3rd party merge which doesn't mutate config --- src/lib/middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib') diff --git a/src/lib/middleware.ts b/src/lib/middleware.ts index 5073137..69fbe4e 100644 --- a/src/lib/middleware.ts +++ b/src/lib/middleware.ts @@ -1,7 +1,7 @@ import { NextFunction, Request, Response } from "express"; import MagicLink from "../models/MagicLink.js"; import getConfig, { GathioConfig } from "../lib/config.js"; -import { deepMerge } from "../util/object.js"; +import { merge as deepMerge } from "ts-deepmerge"; export const checkMagicLink = async ( req: Request, -- cgit v1.2.3