Merge remote-tracking branch 'origin/development' into development

This commit is contained in:
Tomas Bures 2019-04-03 12:14:09 +02:00
commit ec0f288d81
9 changed files with 171 additions and 6 deletions

View file

@ -1,3 +1,4 @@
{
"extends": "nodemailer"
"extends": "nodemailer",
"parserOptions": { "ecmaVersion": 2018 }
}

View file

@ -8,6 +8,10 @@ const fs = require('fs-extra-promise');
const tryRequire = require('try-require');
const posix = tryRequire('posix');
// process.getuid and process.getgid are not supported on Windows
process.getuid = process.getuid || (() => 100);
process.getgid = process.getuid || (() => 100);
function _getConfigUidGid(userKey, groupKey, defaultUid, defaultGid) {
let uid = defaultUid;
let gid = defaultGid;

View file

@ -0,0 +1,79 @@
'use strict';
const mailers = require('./mailers');
const tools = require('./tools');
const templates = require('../models/templates');
class TemplateSender {
constructor(options) {
this.defaultOptions = {
maxMails: 100,
...options
};
}
async send(params) {
const options = { ...this.defaultOptions, ...params };
this._validateMailOptions(options);
const [mailer, template] = await Promise.all([
mailers.getOrCreateMailer(
options.sendConfigurationId
),
templates.getById(
options.context,
options.templateId,
false
)
]);
const html = tools.formatTemplate(
template.html,
null,
options.variables,
true
);
const subject = tools.formatTemplate(
options.subject || template.description || template.name,
options.variables
);
return mailer.sendTransactionalMail(
{
to: options.email,
subject
},
{
html: { template: html },
data: options.data,
locale: options.locale
}
);
}
_validateMailOptions(options) {
let { context, email, locale, templateId } = options;
if (!templateId) {
throw new Error('Missing templateId');
}
if (!context) {
throw new Error('Missing context');
}
if (!email || email.length === 0) {
throw new Error('Missing email');
}
if (typeof email === 'string') {
email = email.split(',');
}
if (email.length > options.maxMails) {
throw new Error(
`Cannot send more than ${options.maxMails} emails at once`
);
}
if (!locale) {
throw new Error('Missing locale');
}
}
}
module.exports = TemplateSender;

View file

@ -14,6 +14,7 @@ const mjml2html = require('mjml');
const hbs = require('hbs');
const juice = require('juice');
const he = require('he');
const htmlToText = require('html-to-text');
const fs = require('fs-extra');
@ -148,14 +149,21 @@ function validateEmailGetMessage(result, address, language) {
function formatMessage(campaign, list, subscription, mergeTags, message, isHTML) {
const links = getMessageLinks(campaign, list, subscription);
return formatTemplate(message, links, mergeTags, isHTML);
}
function formatTemplate(template, links, mergeTags, isHTML) {
if (!links && !mergeTags) { return template; }
const getValue = fullKey => {
const keys = (fullKey || '').split('.');
if (links.hasOwnProperty(keys[0])) {
if (links && links.hasOwnProperty(keys[0])) {
return links[keys[0]];
}
if (!mergeTags) { return false; }
let value = mergeTags;
while (keys.length > 0) {
let key = keys.shift();
@ -173,7 +181,7 @@ function formatMessage(campaign, list, subscription, mergeTags, message, isHTML)
}) : (containsHTML ? htmlToText.fromString(value) : value);
};
return message.replace(/\[([a-z0-9_.]+)(?:\/([^\]]+))?\]/ig, (match, identifier, fallback) => {
return template.replace(/\[([a-z0-9_.]+)(?:\/([^\]]+))?\]/ig, (match, identifier, fallback) => {
let value = getValue(identifier);
if (value === false) {
return match;
@ -229,6 +237,7 @@ module.exports = {
getTemplate,
prepareHtml,
getMessageLinks,
formatMessage
formatMessage,
formatTemplate
};

View file

@ -16,6 +16,7 @@ const contextHelpers = require('../lib/context-helpers');
const shares = require('../models/shares');
const slugify = require('slugify');
const passport = require('../lib/passport');
const TemplateSender = require('../lib/template-sender');
const campaigns = require('../models/campaigns');
class APIError extends Error {
@ -285,5 +286,34 @@ router.getAsync('/rss/fetch/:campaignCid', passport.loggedIn, async (req, res) =
return res.json();
});
router.postAsync('/templates/:templateId/send', async (req, res) => {
const input = {};
Object.keys(req.body).forEach(key => {
input[
(key || '')
.toString()
.trim()
.toUpperCase()
] = req.body[key] || '';
});
try {
const templateSender = new TemplateSender({
context: req.context,
locale: req.locale,
templateId: req.params.templateId
});
const info = await templateSender.send({
data: input.DATA,
email: input.EMAIL,
sendConfigurationId: input.SEND_CONFIGURATION_ID,
subject: input.SUBJECT,
variables: input.VARIABLES
});
res.status(200).json({ data: info });
} catch (e) {
throw new APIError(e.message, 400);
}
});
module.exports = router;