summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/config.ts88
-rw-r--r--src/lib/email.ts292
-rw-r--r--src/lib/event.ts4
-rw-r--r--src/lib/handlebars.ts23
-rw-r--r--src/lib/middleware.ts4
5 files changed, 243 insertions, 168 deletions
diff --git a/src/lib/config.ts b/src/lib/config.ts
index 003a714..35fc42c 100644
--- a/src/lib/config.ts
+++ b/src/lib/config.ts
@@ -3,6 +3,7 @@ import toml from "toml";
import { exitWithError } from "./process.js";
import { Response } from "express";
import { markdownToSanitizedHTML } from "../util/markdown.js";
+import i18next from "i18next";
interface StaticPage {
title: string;
@@ -21,7 +22,7 @@ export interface GathioConfig {
email_logo_url: string;
show_kofi: boolean;
show_public_event_list: boolean;
- mail_service: "nodemailer" | "sendgrid" | "none";
+ mail_service: "nodemailer" | "sendgrid" | "mailgun" | "none";
creator_email_addresses: string[];
};
database: {
@@ -37,6 +38,11 @@ export interface GathioConfig {
sendgrid?: {
api_key: string;
};
+ mailgun?: {
+ api_key: string;
+ api_url: string;
+ domain: string;
+ };
static_pages?: StaticPage[];
}
@@ -110,59 +116,61 @@ 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: i18next.t("config.instancerule.showpubliceventlist-true"),
+ icon: "fas fa-eye",
+ }
: {
- text: "Events and groups can only be accessed by direct link",
- 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: "Only specific people can create events and groups",
- icon: "fas fa-user-check",
- }
+ text: i18next.t("config.instancerule.creatoremail-true"),
+ icon: "fas fa-user-check",
+ }
: {
- text: "Anyone can create events and groups",
- icon: "fas fa-users",
- },
+ text: i18next.t("config.instancerule.creatoremail-false"),
+ 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: i18next.t("config.instancerule.deleteafterdays-true", { days: config.general.delete_after_days } ),
+ icon: "far fa-calendar-times",
+ }
: {
- text: "Events are permanent, and are never automatically deleted",
- icon: "far fa-calendar-check",
- },
+ text: i18next.t("config.instancerule.deleteafterdays-false"),
+ 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: i18next.t("config.instancerule.isfederated-true"),
+ icon: "fas fa-globe",
+ }
: {
- text: "This instance does not federate with other instances",
- icon: "fas fa-globe",
- },
+ text: i18next.t("config.instancerule.isfederated-false"),
+ icon: "fas fa-globe",
+ },
);
return rules;
};
export const instanceDescription = (): string => {
const config = getConfig();
- const defaultInstanceDescription =
- "**{{ siteName }}** is running on Gathio — a simple, federated, privacy-first event hosting platform.";
+ const defaultInstanceDescription = markdownToSanitizedHTML(
+ i18next.t("config.defaultinstancedesc", "Welcome to this Gathio instance!")
+ );
let instanceDescription = defaultInstanceDescription;
+ let instancedescfile = "./static/instance-description-" + i18next.language + ".md";
try {
- if (fs.existsSync("./static/instance-description.md")) {
+ if (fs.existsSync(instancedescfile)) {
const fileBody = fs.readFileSync(
- "./static/instance-description.md",
+ instancedescfile,
"utf-8",
);
instanceDescription = markdownToSanitizedHTML(fileBody);
@@ -179,17 +187,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..fc585a1 100644
--- a/src/lib/email.ts
+++ b/src/lib/email.ts
@@ -1,13 +1,16 @@
-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";
+import Mailgun from "mailgun.js";
+import { IMailgunClient } from "node_modules/mailgun.js/Types/Interfaces/index.js";
+
const config = getConfig();
-type EmailTemplate =
+type EmailTemplateName =
| "addEventAttendee"
| "addEventComment"
| "createEvent"
@@ -16,58 +19,90 @@ 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.",
- );
+export class EmailService {
+ nodemailerTransporter: Transporter | undefined = undefined;
+ sgMail: typeof sgMail | undefined = undefined;
+ mailgunClient: IMailgunClient | 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(
+ "Sendgrid is configured as the email service, but no API key is provided. Please provide an API key in the config file.",
+ );
+ }
+ this.sgMail = sgMail;
+ this.sgMail.setApiKey(config.sendgrid.api_key);
+ console.log("Sendgrid is ready to send emails.");
+ break;
}
- 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 {
+ case "mailgun": {
if (
- !config.nodemailer?.smtp_server ||
- !config.nodemailer?.smtp_port
+ !config.mailgun?.api_key ||
+ !config.mailgun?.api_url ||
+ !config.mailgun?.domain
) {
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.",
+ "Mailgun 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;
+ const mailgun = new Mailgun(FormData);
+ this.mailgunClient = mailgun.client({
+ username: "api",
+ key: config.mailgun.api_key,
+ url: config.mailgun.api_url,
+ });
+ // TODO: Can we verify the Mailgun connection?
+ console.log("Mailgun 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 +111,72 @@ 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.mailgunClient) {
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);
+ if (!config.mailgun?.domain) {
+ return exitWithError(
+ "Mailgun is configured as the email service, but no domain is provided. Please provide a domain in the config file.",
+ );
}
- await nodemailerTransporter.sendMail({
- envelope: {
+ await this.mailgunClient.messages.create(
+ config.mailgun.domain,
+ {
from: config.general.email,
to,
bcc,
+ 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;
+ }
+ } else if (this.nodemailerTransporter) {
+ try {
+ await this.nodemailerTransporter.sendMail({
from: config.general.email,
to,
bcc,
@@ -150,31 +189,62 @@ 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/event.ts b/src/lib/event.ts
index 334ddf6..bcb7cd9 100644
--- a/src/lib/event.ts
+++ b/src/lib/event.ts
@@ -1,3 +1,4 @@
+import i18next from "i18next";
import { IEventGroup } from "../models/EventGroup.js";
export interface EventListEvent {
@@ -15,7 +16,8 @@ export const bucketEventsByMonth = (
acc: Record<string, any>[],
event: EventListEvent,
) => {
- const month = event.startMoment.format("MMMM YYYY");
+ event.startMoment.locale(i18next.language);
+ const month = event.startMoment.format(i18next.t("common.year-month-format" ));
const matchingBucket = acc.find((bucket) => bucket.title === month);
if (!matchingBucket) {
acc.push({
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..be05c3f 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,
@@ -62,7 +62,7 @@ export const getConfigMiddleware = (
if (process.env.CYPRESS === "true" && req.cookies?.cypressConfigOverride) {
console.log("Overriding config with Cypress config");
const override = JSON.parse(req.cookies.cypressConfigOverride);
- res.locals.config = deepMerge<GathioConfig>(config, override);
+ res.locals.config = deepMerge(config, override);
return next();
}
res.locals.config = config;