diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/config.ts | 70 | ||||
-rw-r--r-- | src/lib/email.ts | 259 | ||||
-rw-r--r-- | src/lib/handlebars.ts | 23 | ||||
-rw-r--r-- | src/lib/middleware.ts | 2 |
4 files changed, 185 insertions, 169 deletions
diff --git a/src/lib/config.ts b/src/lib/config.ts index 4016c92..5b74e4a 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -111,46 +111,46 @@ export const instanceRules = (): InstanceRule[] => { rules.push( config.general.show_public_event_list ? { - text: i18next.t("config.instancerule.showpubliceventlist-true"), - icon: "fas fa-eye", - } + text: i18next.t("config.instancerule.showpubliceventlist-true"), + icon: "fas fa-eye", + } : { - text: i18next.t("config.instancerule..showpubliceventlist-false"), - icon: "fas fa-eye-slash", - }, + text: i18next.t("config.instancerule.showpubliceventlist-false"), + icon: "fas fa-eye-slash", + }, ); rules.push( config.general.creator_email_addresses?.length ? { - text: i18next.t("config.instancerule.creatoremail-true"), - icon: "fas fa-user-check", - } + text: i18next.t("config.instancerule.creatoremail-true"), + icon: "fas fa-user-check", + } : { - text: i18next.t("config.instancerule.creatoremail-false"), - icon: "fas fa-users", - }, + text: i18next.t("config.instancerule.creatoremail-false"), + icon: "fas fa-users", + }, ); rules.push( config.general.delete_after_days > 0 ? { - text: i18next.t("config.instancerule.deleteafterdays-true", { days: config.general.delete_after_days } ), - icon: "far fa-calendar-times", - } + text: i18next.t("config.instancerule.deleteafterdays-true", { days: config.general.delete_after_days } ), + icon: "far fa-calendar-times", + } : { - text: i18next.t("config.instancerule.deleteafterdays-false"), - icon: "far fa-calendar-check", - }, + text: i18next.t("config.instancerule.deleteafterdays-false"), + icon: "far fa-calendar-check", + }, ); rules.push( config.general.is_federated ? { - text: i18next.t("config.instancerule.isfederated-true"), - icon: "fas fa-globe", - } + text: i18next.t("config.instancerule.isfederated-true"), + icon: "fas fa-globe", + } : { - text: i18next.t("config.instancerule.isfederated-false"), - icon: "fas fa-globe", - }, + text: i18next.t("config.instancerule.isfederated-false"), + icon: "fas fa-globe", + }, ); return rules; }; @@ -181,17 +181,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 7b7a7a1..7c7c2dd 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -1,13 +1,14 @@ -import { Request } from "express"; 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 { renderTemplate } from "./handlebars.js"; + const config = getConfig(); -type EmailTemplate = +type EmailTemplateName = | "addEventAttendee" | "addEventComment" | "createEvent" @@ -16,58 +17,69 @@ type EmailTemplate = | "deleteEvent" | "editEvent" | "eventGroupUpdated" + | "removeEventAttendee" | "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": - 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<boolean> { + if (this.nodemailerTransporter) { + const nodemailerVerified = await this.nodemailerTransporter.verify(); if (nodemailerVerified) { console.log("Nodemailer is ready to send emails."); return true; @@ -76,68 +88,45 @@ export const initEmailService = async (): Promise<boolean> => { "Error verifying Nodemailer transporter. Please check your Nodemailer configuration.", ); } - 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, - bcc: string, - subject: string, - text: string, - html?: string, -): Promise<boolean> => { - 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<boolean> { + 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, }); return true; - } catch (e: any) { - 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; } - 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({ - envelope: { - from: config.general.email, - to, - bcc, - }, + await this.nodemailerTransporter.sendMail({ from: config.general.email, to, bcc, @@ -150,31 +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, - bcc: 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, bcc, subject, text, html); -}; + public async sendEmailFromTemplate({ + to, + bcc = "", + subject, + templateName, + templateData = {} + }: { + to: string | string[]; + bcc?: string | string[] | undefined; + subject: string; + templateName: EmailTemplateName; + templateData?: object; + }, + ): Promise<boolean> { + 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 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 d5a8b6e..0000000 --- a/src/lib/handlebars.ts +++ /dev/null @@ -1,23 +0,0 @@ -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/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, |