diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/config.ts | 70 | ||||
-rw-r--r-- | src/lib/email.ts | 263 | ||||
-rw-r--r-- | src/lib/handlebars.ts | 55 |
3 files changed, 175 insertions, 213 deletions
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<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; @@ -83,30 +89,29 @@ export const initEmailService = async (): Promise<boolean> => { ); } } - 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<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, }); @@ -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<boolean> => { - 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<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 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<string> { - return this.hbsInstance.renderView(viewPath, options); - } -} |