mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: async/await emailer
This commit is contained in:
@@ -20,16 +20,12 @@ settingsController.get = async function (req, res, next) {
|
|||||||
|
|
||||||
|
|
||||||
async function renderEmail(req, res) {
|
async function renderEmail(req, res) {
|
||||||
const [emails, services] = await Promise.all([
|
const emails = await emailer.getTemplates(meta.config);
|
||||||
emailer.getTemplates(meta.config),
|
|
||||||
emailer.listServices(),
|
|
||||||
]);
|
|
||||||
res.render('admin/settings/email', {
|
res.render('admin/settings/email', {
|
||||||
emails: emails,
|
emails: emails,
|
||||||
sendable: emails.filter(function (email) {
|
sendable: emails.filter(e => !e.path.includes('_plaintext') && !e.path.includes('partials')),
|
||||||
return !email.path.includes('_plaintext') && !email.path.includes('partials');
|
services: emailer.listServices(),
|
||||||
}),
|
|
||||||
services: services,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
371
src/emailer.js
371
src/emailer.js
@@ -1,26 +1,29 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const winston = require('winston');
|
||||||
var winston = require('winston');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
const Benchpress = require('benchpressjs');
|
||||||
var Benchpress = require('benchpressjs');
|
const nodemailer = require('nodemailer');
|
||||||
var nodemailer = require('nodemailer');
|
const wellKnownServices = require('nodemailer/lib/well-known/services');
|
||||||
var wellKnownServices = require('nodemailer/lib/well-known/services');
|
const htmlToText = require('html-to-text');
|
||||||
var htmlToText = require('html-to-text');
|
const url = require('url');
|
||||||
var url = require('url');
|
const path = require('path');
|
||||||
var path = require('path');
|
const fs = require('fs');
|
||||||
var fs = require('fs');
|
const util = require('util');
|
||||||
var _ = require('lodash');
|
const readFileAsync = util.promisify(fs.readFile);
|
||||||
var jwt = require('jsonwebtoken');
|
const writeFileAsync = util.promisify(fs.writeFile);
|
||||||
|
|
||||||
var User = require('./user');
|
const _ = require('lodash');
|
||||||
var Plugins = require('./plugins');
|
const jwt = require('jsonwebtoken');
|
||||||
var meta = require('./meta');
|
|
||||||
var translator = require('./translator');
|
|
||||||
var pubsub = require('./pubsub');
|
|
||||||
var file = require('./file');
|
|
||||||
|
|
||||||
var Emailer = module.exports;
|
const User = require('./user');
|
||||||
|
const Plugins = require('./plugins');
|
||||||
|
const meta = require('./meta');
|
||||||
|
const translator = require('./translator');
|
||||||
|
const pubsub = require('./pubsub');
|
||||||
|
const file = require('./file');
|
||||||
|
|
||||||
|
const Emailer = module.exports;
|
||||||
|
|
||||||
Emailer.transports = {
|
Emailer.transports = {
|
||||||
sendmail: nodemailer.createTransport({
|
sendmail: nodemailer.createTransport({
|
||||||
@@ -35,48 +38,30 @@ Emailer.transports = {
|
|||||||
|
|
||||||
var app;
|
var app;
|
||||||
|
|
||||||
var viewsDir = nconf.get('views_dir');
|
const viewsDir = nconf.get('views_dir');
|
||||||
|
|
||||||
Emailer.getTemplates = function (config, callback) {
|
Emailer.getTemplates = async function (config) {
|
||||||
var emailsPath = path.join(viewsDir, 'emails');
|
const emailsPath = path.join(viewsDir, 'emails');
|
||||||
async.waterfall([
|
let emails = await file.walk(emailsPath);
|
||||||
function (next) {
|
emails = emails.filter(email => !email.endsWith('.js'));
|
||||||
file.walk(emailsPath, next);
|
|
||||||
},
|
|
||||||
function (emails, next) {
|
|
||||||
// exclude .js files
|
|
||||||
emails = emails.filter(function (email) {
|
|
||||||
return !email.endsWith('.js');
|
|
||||||
});
|
|
||||||
|
|
||||||
async.map(emails, function (email, next) {
|
const templates = await Promise.all(emails.map(async (email) => {
|
||||||
var path = email.replace(emailsPath, '').substr(1).replace('.tpl', '');
|
const path = email.replace(emailsPath, '').substr(1).replace('.tpl', '');
|
||||||
|
const original = await readFileAsync(email, 'utf8');
|
||||||
|
|
||||||
async.waterfall([
|
return {
|
||||||
function (next) {
|
path: path,
|
||||||
fs.readFile(email, 'utf8', next);
|
fullpath: email,
|
||||||
},
|
text: config['email:custom:' + path] || original,
|
||||||
function (original, next) {
|
original: original,
|
||||||
var isCustom = !!config['email:custom:' + path];
|
isCustom: !!config['email:custom:' + path],
|
||||||
var text = config['email:custom:' + path] || original;
|
};
|
||||||
|
}));
|
||||||
next(null, {
|
return templates;
|
||||||
path: path,
|
|
||||||
fullpath: email,
|
|
||||||
text: text,
|
|
||||||
original: original,
|
|
||||||
isCustom: isCustom,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Emailer.listServices = function (callback) {
|
Emailer.listServices = function () {
|
||||||
var services = Object.keys(wellKnownServices);
|
return Object.keys(wellKnownServices);
|
||||||
setImmediate(callback, null, services);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Emailer._defaultPayload = {};
|
Emailer._defaultPayload = {};
|
||||||
@@ -123,9 +108,9 @@ Emailer.setupFallbackTransport = function (config) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var prevConfig = meta.config;
|
let prevConfig = meta.config;
|
||||||
function smtpSettingsChanged(config) {
|
function smtpSettingsChanged(config) {
|
||||||
var settings = [
|
const settings = [
|
||||||
'email:smtpTransport:enabled',
|
'email:smtpTransport:enabled',
|
||||||
'email:smtpTransport:user',
|
'email:smtpTransport:user',
|
||||||
'email:smtpTransport:pass',
|
'email:smtpTransport:pass',
|
||||||
@@ -135,9 +120,7 @@ function smtpSettingsChanged(config) {
|
|||||||
'email:smtpTransport:security',
|
'email:smtpTransport:security',
|
||||||
];
|
];
|
||||||
|
|
||||||
return settings.some(function (key) {
|
return settings.some(key => config[key] !== prevConfig[key]);
|
||||||
return config[key] !== prevConfig[key];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Emailer.registerApp = function (expressApp) {
|
Emailer.registerApp = function (expressApp) {
|
||||||
@@ -186,46 +169,36 @@ Emailer.registerApp = function (expressApp) {
|
|||||||
return Emailer;
|
return Emailer;
|
||||||
};
|
};
|
||||||
|
|
||||||
Emailer.send = function (template, uid, params, callback) {
|
Emailer.send = async function (template, uid, params) {
|
||||||
callback = callback || function () {};
|
|
||||||
if (!app) {
|
if (!app) {
|
||||||
winston.warn('[emailer] App not ready!');
|
winston.warn('[emailer] App not ready!');
|
||||||
return callback();
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combined passed-in payload with default values
|
// Combined passed-in payload with default values
|
||||||
params = { ...Emailer._defaultPayload, ...params };
|
params = { ...Emailer._defaultPayload, ...params };
|
||||||
|
|
||||||
async.waterfall([
|
const [userData, userSettings] = await Promise.all([
|
||||||
function (next) {
|
User.getUserFields(uid, ['email', 'username']),
|
||||||
async.parallel({
|
User.getSettings(uid),
|
||||||
userData: async.apply(User.getUserFields, uid, ['email', 'username']),
|
]);
|
||||||
settings: async.apply(User.getSettings, uid),
|
|
||||||
}, next);
|
if (!userData || !userData.email) {
|
||||||
},
|
winston.warn('uid : ' + uid + ' has no email, not sending.');
|
||||||
async function (results) {
|
return;
|
||||||
if (!results.userData || !results.userData.email) {
|
}
|
||||||
winston.warn('uid : ' + uid + ' has no email, not sending.');
|
params.uid = uid;
|
||||||
return;
|
params.username = userData.username;
|
||||||
}
|
params.rtl = await translator.translate('[[language:dir]]', userSettings.userLang) === 'rtl';
|
||||||
params.uid = uid;
|
try {
|
||||||
params.username = results.userData.username;
|
await Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);
|
||||||
params.rtl = await translator.translate('[[language:dir]]', results.settings.userLang) === 'rtl';
|
} catch (err) {
|
||||||
Emailer.sendToEmail(template, results.userData.email, results.settings.userLang, params, function (err) {
|
winston.error(err);
|
||||||
if (err) {
|
}
|
||||||
winston.error(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Emailer.sendToEmail = function (template, email, language, params, callback) {
|
Emailer.sendToEmail = async function (template, email, language, params) {
|
||||||
callback = callback || function () {};
|
const lang = language || meta.config.defaultLang || 'en-GB';
|
||||||
|
|
||||||
var lang = language || meta.config.defaultLang || 'en-GB';
|
|
||||||
|
|
||||||
// Add some default email headers based on local configuration
|
// Add some default email headers based on local configuration
|
||||||
params.headers = { 'List-Id': '<' + [template, params.uid, getHostname()].join('.') + '>',
|
params.headers = { 'List-Id': '<' + [template, params.uid, getHostname()].join('.') + '>',
|
||||||
@@ -238,83 +211,64 @@ Emailer.sendToEmail = function (template, email, language, params, callback) {
|
|||||||
uid: params.uid,
|
uid: params.uid,
|
||||||
};
|
};
|
||||||
|
|
||||||
switch (template) {
|
if (template === 'digest' || template === 'notification') {
|
||||||
case 'digest':
|
if (template === 'notification') {
|
||||||
|
payload.type = params.notification.type;
|
||||||
|
}
|
||||||
payload = jwt.sign(payload, nconf.get('secret'), {
|
payload = jwt.sign(payload, nconf.get('secret'), {
|
||||||
expiresIn: '30d',
|
expiresIn: '30d',
|
||||||
});
|
});
|
||||||
params.headers['List-Unsubscribe'] = '<' + [nconf.get('url'), 'email', 'unsubscribe', payload].join('/') + '>';
|
params.headers['List-Unsubscribe'] = '<' + [nconf.get('url'), 'email', 'unsubscribe', payload].join('/') + '>';
|
||||||
params.headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
|
params.headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
|
||||||
break;
|
|
||||||
|
|
||||||
case 'notification':
|
|
||||||
payload.type = params.notification.type;
|
|
||||||
payload = jwt.sign(payload, nconf.get('secret'), {
|
|
||||||
expiresIn: '30d',
|
|
||||||
});
|
|
||||||
params.headers['List-Unsubscribe'] = '<' + [nconf.get('url'), 'email', 'unsubscribe', payload].join('/') + '>';
|
|
||||||
params.headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
const result = await Plugins.fireHook('filter:email.params', {
|
||||||
function (next) {
|
template: template,
|
||||||
Plugins.fireHook('filter:email.params', {
|
email: email,
|
||||||
template: template,
|
language: lang,
|
||||||
email: email,
|
params: params,
|
||||||
language: lang,
|
|
||||||
params: params,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
template = result.template;
|
|
||||||
email = result.email;
|
|
||||||
params = result.params;
|
|
||||||
async.parallel({
|
|
||||||
html: function (next) {
|
|
||||||
Emailer.renderAndTranslate(template, params, result.language, next);
|
|
||||||
},
|
|
||||||
subject: function (next) {
|
|
||||||
translator.translate(params.subject, result.language, function (translated) {
|
|
||||||
next(null, translated);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
var data = {
|
|
||||||
_raw: params,
|
|
||||||
to: email,
|
|
||||||
from: meta.config['email:from'] || 'no-reply@' + getHostname(),
|
|
||||||
from_name: meta.config['email:from_name'] || 'NodeBB',
|
|
||||||
subject: '[' + meta.config.title + '] ' + _.unescape(results.subject),
|
|
||||||
html: results.html,
|
|
||||||
plaintext: htmlToText.fromString(results.html, {
|
|
||||||
ignoreImage: true,
|
|
||||||
}),
|
|
||||||
template: template,
|
|
||||||
uid: params.uid,
|
|
||||||
pid: params.pid,
|
|
||||||
fromUid: params.fromUid,
|
|
||||||
headers: params.headers,
|
|
||||||
rtl: params.rtl,
|
|
||||||
};
|
|
||||||
Plugins.fireHook('filter:email.modify', data, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
if (Plugins.hasListeners('filter:email.send')) {
|
|
||||||
Plugins.fireHook('filter:email.send', data, next);
|
|
||||||
} else {
|
|
||||||
Emailer.sendViaFallback(data, next);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
if (err && err.code === 'ENOENT') {
|
|
||||||
callback(new Error('[[error:sendmail-not-found]]'));
|
|
||||||
} else {
|
|
||||||
callback(err);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
template = result.template;
|
||||||
|
email = result.email;
|
||||||
|
params = result.params;
|
||||||
|
|
||||||
|
const [html, subject] = await Promise.all([
|
||||||
|
Emailer.renderAndTranslate(template, params, result.language),
|
||||||
|
translator.translate(params.subject, result.language),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const data = await Plugins.fireHook('filter:email.modify', {
|
||||||
|
_raw: params,
|
||||||
|
to: email,
|
||||||
|
from: meta.config['email:from'] || 'no-reply@' + getHostname(),
|
||||||
|
from_name: meta.config['email:from_name'] || 'NodeBB',
|
||||||
|
subject: '[' + meta.config.title + '] ' + _.unescape(subject),
|
||||||
|
html: html,
|
||||||
|
plaintext: htmlToText.fromString(html, {
|
||||||
|
ignoreImage: true,
|
||||||
|
}),
|
||||||
|
template: template,
|
||||||
|
uid: params.uid,
|
||||||
|
pid: params.pid,
|
||||||
|
fromUid: params.fromUid,
|
||||||
|
headers: params.headers,
|
||||||
|
rtl: params.rtl,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (Plugins.hasListeners('filter:email.send')) {
|
||||||
|
await Plugins.fireHook('filter:email.send', data);
|
||||||
|
} else {
|
||||||
|
await Emailer.sendViaFallback(data);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
if (err && err.code === 'ENOENT') {
|
||||||
|
throw new Error('[[error:sendmail-not-found]]');
|
||||||
|
} else {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Emailer.sendViaFallback = function (data, callback) {
|
Emailer.sendViaFallback = function (data, callback) {
|
||||||
@@ -335,75 +289,48 @@ Emailer.sendViaFallback = function (data, callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function buildCustomTemplates(config) {
|
async function buildCustomTemplates(config) {
|
||||||
async.waterfall([
|
try {
|
||||||
function (next) {
|
const [templates, allPaths] = await Promise.all([
|
||||||
async.parallel({
|
Emailer.getTemplates(config),
|
||||||
templates: function (cb) {
|
file.walk(viewsDir),
|
||||||
Emailer.getTemplates(config, cb);
|
]);
|
||||||
},
|
|
||||||
paths: function (cb) {
|
|
||||||
file.walk(viewsDir, cb);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
// If the new config contains any email override values, re-compile those templates
|
|
||||||
var toBuild = Object
|
|
||||||
.keys(config)
|
|
||||||
.filter(prop => prop.startsWith('email:custom:'))
|
|
||||||
.map(key => key.split(':')[2]);
|
|
||||||
|
|
||||||
var templates = result.templates.filter(template => toBuild.includes(template.path));
|
// If the new config contains any email override values, re-compile those templates
|
||||||
var paths = _.fromPairs(result.paths.map(function (p) {
|
const toBuild = Object
|
||||||
var relative = path.relative(viewsDir, p).replace(/\\/g, '/');
|
.keys(config)
|
||||||
return [relative, p];
|
.filter(prop => prop.startsWith('email:custom:'))
|
||||||
}));
|
.map(key => key.split(':')[2]);
|
||||||
async.each(templates, function (template, next) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
meta.templates.processImports(paths, template.path, template.text, next);
|
|
||||||
},
|
|
||||||
function (source, next) {
|
|
||||||
Benchpress.precompile(source, {
|
|
||||||
minify: global.env !== 'development',
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (compiled, next) {
|
|
||||||
fs.writeFile(template.fullpath.replace(/\.tpl$/, '.js'), compiled, next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
Benchpress.flush();
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('[emailer] Failed to build custom email templates', err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const templatesToBuild = templates.filter(template => toBuild.includes(template.path));
|
||||||
|
const paths = _.fromPairs(allPaths.map(function (p) {
|
||||||
|
const relative = path.relative(viewsDir, p).replace(/\\/g, '/');
|
||||||
|
return [relative, p];
|
||||||
|
}));
|
||||||
|
|
||||||
|
await Promise.all(templatesToBuild.map(async (template) => {
|
||||||
|
const source = await meta.templates.processImports(paths, template.path, template.text);
|
||||||
|
const compiled = await Benchpress.precompile(source, {
|
||||||
|
minify: global.env !== 'development',
|
||||||
|
});
|
||||||
|
await writeFileAsync(template.fullpath.replace(/\.tpl$/, '.js'), compiled);
|
||||||
|
}));
|
||||||
|
|
||||||
|
Benchpress.flush();
|
||||||
winston.verbose('[emailer] Built custom email templates');
|
winston.verbose('[emailer] Built custom email templates');
|
||||||
});
|
} catch (err) {
|
||||||
|
winston.error('[emailer] Failed to build custom email templates', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Emailer.renderAndTranslate = function (template, params, lang, callback) {
|
Emailer.renderAndTranslate = async function (template, params, lang) {
|
||||||
app.render('emails/' + template, params, function (err, html) {
|
const html = await app.renderAsync('emails/' + template, params);
|
||||||
if (err) {
|
return await translator.translate(html, lang);
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
translator.translate(html, lang, function (translated) {
|
|
||||||
callback(null, translated);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function getHostname() {
|
function getHostname() {
|
||||||
var configUrl = nconf.get('url');
|
const configUrl = nconf.get('url');
|
||||||
var parsed = url.parse(configUrl);
|
const parsed = url.parse(configUrl);
|
||||||
|
|
||||||
return parsed.hostname;
|
return parsed.hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user