refactor: async/await emailer

v1.18.x
Barış Soner Uşaklı 6 years ago
parent f76d35bcf2
commit 63bd3fc597

@ -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,
}); });
} }

@ -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'); const User = require('./user');
var pubsub = require('./pubsub'); const Plugins = require('./plugins');
var file = require('./file'); const meta = require('./meta');
const translator = require('./translator');
var Emailer = module.exports; 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);
}, const templates = await Promise.all(emails.map(async (email) => {
function (emails, next) { const path = email.replace(emailsPath, '').substr(1).replace('.tpl', '');
// exclude .js files const original = await readFileAsync(email, 'utf8');
emails = emails.filter(function (email) {
return !email.endsWith('.js'); return {
}); path: path,
fullpath: email,
async.map(emails, function (email, next) { text: config['email:custom:' + path] || original,
var path = email.replace(emailsPath, '').substr(1).replace('.tpl', ''); original: original,
isCustom: !!config['email:custom:' + path],
async.waterfall([ };
function (next) { }));
fs.readFile(email, 'utf8', next); return templates;
},
function (original, next) {
var isCustom = !!config['email:custom:' + path];
var text = config['email:custom:' + path] || original;
next(null, {
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);
},
async function (results) {
if (!results.userData || !results.userData.email) {
winston.warn('uid : ' + uid + ' has no email, not sending.');
return;
}
params.uid = uid;
params.username = results.userData.username;
params.rtl = await translator.translate('[[language:dir]]', results.settings.userLang) === 'rtl';
Emailer.sendToEmail(template, results.userData.email, results.settings.userLang, params, function (err) {
if (err) {
winston.error(err);
}
});
},
], function (err) {
callback(err);
});
};
Emailer.sendToEmail = function (template, email, language, params, callback) { if (!userData || !userData.email) {
callback = callback || function () {}; winston.warn('uid : ' + uid + ' has no email, not sending.');
return;
}
params.uid = uid;
params.username = userData.username;
params.rtl = await translator.translate('[[language:dir]]', userSettings.userLang) === 'rtl';
try {
await Emailer.sendToEmail(template, userData.email, userSettings.userLang, params);
} catch (err) {
winston.error(err);
}
};
var lang = language || meta.config.defaultLang || 'en-GB'; Emailer.sendToEmail = async function (template, email, language, params) {
const 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 = jwt.sign(payload, nconf.get('secret'), { payload.type = params.notification.type;
expiresIn: '30d', }
});
params.headers['List-Unsubscribe'] = '<' + [nconf.get('url'), 'email', 'unsubscribe', payload].join('/') + '>';
params.headers['List-Unsubscribe-Post'] = 'List-Unsubscribe=One-Click';
break;
case '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;
} }
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); template = result.template;
}, email = result.email;
function (result, next) { params = result.params;
template = result.template;
email = result.email; const [html, subject] = await Promise.all([
params = result.params; Emailer.renderAndTranslate(template, params, result.language),
async.parallel({ translator.translate(params.subject, result.language),
html: function (next) { ]);
Emailer.renderAndTranslate(template, params, result.language, next);
}, const data = await Plugins.fireHook('filter:email.modify', {
subject: function (next) { _raw: params,
translator.translate(params.subject, result.language, function (translated) { to: email,
next(null, translated); from: meta.config['email:from'] || 'no-reply@' + getHostname(),
}); from_name: meta.config['email:from_name'] || 'NodeBB',
}, subject: '[' + meta.config.title + '] ' + _.unescape(subject),
}, next); html: html,
}, plaintext: htmlToText.fromString(html, {
function (results, next) { ignoreImage: true,
var data = { }),
_raw: params, template: template,
to: email, uid: params.uid,
from: meta.config['email:from'] || 'no-reply@' + getHostname(), pid: params.pid,
from_name: meta.config['email:from_name'] || 'NodeBB', fromUid: params.fromUid,
subject: '[' + meta.config.title + '] ' + _.unescape(results.subject), headers: params.headers,
html: results.html, rtl: params.rtl,
plaintext: htmlToText.fromString(results.html, { });
ignoreImage: true,
}), try {
template: template, if (Plugins.hasListeners('filter:email.send')) {
uid: params.uid, await Plugins.fireHook('filter:email.send', data);
pid: params.pid, } else {
fromUid: params.fromUid, await Emailer.sendViaFallback(data);
headers: params.headers, }
rtl: params.rtl, } catch (err) {
};
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') { if (err && err.code === 'ENOENT') {
callback(new Error('[[error:sendmail-not-found]]')); throw new Error('[[error:sendmail-not-found]]');
} else { } else {
callback(err); 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) { // If the new config contains any email override values, re-compile those templates
file.walk(viewsDir, cb); const toBuild = Object
}, .keys(config)
}, next); .filter(prop => prop.startsWith('email:custom:'))
}, .map(key => key.split(':')[2]);
function (result, next) {
// If the new config contains any email override values, re-compile those templates const templatesToBuild = templates.filter(template => toBuild.includes(template.path));
var toBuild = Object const paths = _.fromPairs(allPaths.map(function (p) {
.keys(config) const relative = path.relative(viewsDir, p).replace(/\\/g, '/');
.filter(prop => prop.startsWith('email:custom:')) return [relative, p];
.map(key => key.split(':')[2]); }));
var templates = result.templates.filter(template => toBuild.includes(template.path)); await Promise.all(templatesToBuild.map(async (template) => {
var paths = _.fromPairs(result.paths.map(function (p) { const source = await meta.templates.processImports(paths, template.path, template.text);
var relative = path.relative(viewsDir, p).replace(/\\/g, '/'); const compiled = await Benchpress.precompile(source, {
return [relative, p]; minify: global.env !== 'development',
})); });
async.each(templates, function (template, next) { await writeFileAsync(template.fullpath.replace(/\.tpl$/, '.js'), compiled);
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;
}
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;
} }

Loading…
Cancel
Save