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/app.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 0708081..85ee64e 100755 --- a/src/app.ts +++ b/src/app.ts @@ -15,16 +15,28 @@ import { activityPubContentType, alternateActivityPubContentType, } from "./lib/activitypub.js"; +import getConfig from "./lib/config.js"; const app = express(); +const config = getConfig(); -app.locals.sendEmails = initEmailService(); +initEmailService().then((sendEmails) => (app.locals.sendEmails = sendEmails)); // View engine // const hbsInstance = hbs.create({ defaultLayout: "main", partialsDir: ["views/partials/"], layoutsDir: "views/layouts/", + runtimeOptions: { + data: { + 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, + }, + }, helpers: { plural: function (number: number, text: string) { var singular = number === 1; @@ -46,6 +58,31 @@ const hbsInstance = hbs.create({ }, }, }); +app.locals.renderEmail = async function renderEmail( + template: string, + data: object +) { + const [html, text] = await Promise.all([ + hbsInstance.renderView( + `./views/emails/${template}Html.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + hbsInstance.renderView( + `./views/emails/${template}Text.handlebars`, + { + cache: true, + layout: "email.handlebars", + ...data, + } + ), + ]); + return { html, text } +} + app.engine("handlebars", hbsInstance.engine); app.set("view engine", "handlebars"); app.set("hbsInstance", hbsInstance); -- 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/app.ts | 68 +++----------------------------------------------------------- 1 file changed, 3 insertions(+), 65 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 85ee64e..5f7c024 100755 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,4 @@ import express from "express"; -import hbs from "express-handlebars"; import cookieParser from "cookie-parser"; import routes from "./routes.js"; @@ -15,77 +14,16 @@ import { activityPubContentType, alternateActivityPubContentType, } from "./lib/activitypub.js"; -import getConfig from "./lib/config.js"; +import { HandlebarsSingleton } from "./lib/handlebars.js"; const app = express(); -const config = getConfig(); initEmailService().then((sendEmails) => (app.locals.sendEmails = sendEmails)); // View engine // -const hbsInstance = hbs.create({ - defaultLayout: "main", - partialsDir: ["views/partials/"], - layoutsDir: "views/layouts/", - runtimeOptions: { - data: { - 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, - }, - }, - helpers: { - plural: function (number: number, text: string) { - var 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) - var 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: any) { - return JSON.stringify(context); - }, - }, -}); -app.locals.renderEmail = async function renderEmail( - template: string, - data: object -) { - const [html, text] = await Promise.all([ - hbsInstance.renderView( - `./views/emails/${template}Html.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - hbsInstance.renderView( - `./views/emails/${template}Text.handlebars`, - { - cache: true, - layout: "email.handlebars", - ...data, - } - ), - ]); - return { html, text } -} - -app.engine("handlebars", hbsInstance.engine); +app.engine("handlebars", HandlebarsSingleton.instance.engine); app.set("view engine", "handlebars"); -app.set("hbsInstance", hbsInstance); +app.set("hbsInstance", HandlebarsSingleton.instance); // Static files // app.use(express.static("public")); -- 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/app.ts | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 5f7c024..7ed535c 100755 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,6 @@ import express from "express"; import cookieParser from "cookie-parser"; +import { create as createHandlebars, ExpressHandlebars } from "express-handlebars"; import routes from "./routes.js"; import frontend from "./routes/frontend.js"; @@ -8,22 +9,56 @@ import event from "./routes/event.js"; import group from "./routes/group.js"; import staticPages from "./routes/static.js"; import magicLink from "./routes/magicLink.js"; - -import { initEmailService } from "./lib/email.js"; import { activityPubContentType, alternateActivityPubContentType, } from "./lib/activitypub.js"; -import { HandlebarsSingleton } from "./lib/handlebars.js"; +import { EmailService } from "./lib/email.js"; +import getConfig from "./lib/config.js"; const app = express(); +const config = getConfig(); + +const hbsInstance = createHandlebars({ + 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); + }, + }, +}); + +const emailService = new EmailService(config, hbsInstance); +emailService.verify(); -initEmailService().then((sendEmails) => (app.locals.sendEmails = sendEmails)); +app.use((req: express.Request, _: express.Response, next: express.NextFunction) => { + req.hbsInstance = hbsInstance; + req.emailService = emailService; + next() + return +}) // View engine // -app.engine("handlebars", HandlebarsSingleton.instance.engine); +app.engine("handlebars", hbsInstance.engine); app.set("view engine", "handlebars"); -app.set("hbsInstance", HandlebarsSingleton.instance); +app.set("hbsInstance", hbsInstance); // Static files // app.use(express.static("public")); -- cgit v1.2.3