From 1f5566ca9bc6ad7d54c38502ad4661d3a643bd73 Mon Sep 17 00:00:00 2001 From: Tomas Bures Date: Sun, 20 May 2018 20:27:35 +0200 Subject: [PATCH] Some fixes so that the subscription forms work with the new mailer --- app-builder.js | 12 ++++++++++-- lib/mailers.js | 12 ++++++++---- lib/tools.js | 4 ++-- models/send-configurations.js | 2 +- routes/subscription.js | 1 + 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app-builder.js b/app-builder.js index 9586d67f..b8135091 100644 --- a/app-builder.js +++ b/app-builder.js @@ -65,11 +65,13 @@ hbs.registerPartials(__dirname + '/views/subscription/partials/'); * and the message is never displayed */ hbs.registerHelper('flash_messages', function () { // eslint-disable-line prefer-arrow-callback + console.log(this.flash); if (typeof this.flash !== 'function') { // eslint-disable-line no-invalid-this return ''; } let messages = this.flash(); // eslint-disable-line no-invalid-this + console.log(messages); let response = []; // group messages by type @@ -185,9 +187,15 @@ function createApp(trusted) { useWith404Fallback('/mailtrain', express.static(path.join(__dirname, 'client', 'dist'))); useWith404Fallback('/locales', express.static(path.join(__dirname, 'client', 'locales'))); - /* FIXME - can we remove this??? - // make sure flash messages are available + // Make sure flash messages are available + // Currently, flash messages are used only from routes/subscription.js + app.use((req, res, next) => { + res.locals.flash = req.flash.bind(req); + next(); + }); + + /* FIXME - can we remove this??? app.use((req, res, next) => { res.locals.flash = req.flash.bind(req); res.locals.user = req.user; diff --git a/lib/mailers.js b/lib/mailers.js index 82804096..7d007a03 100644 --- a/lib/mailers.js +++ b/lib/mailers.js @@ -35,9 +35,13 @@ Handlebars.registerHelper('translate', function (context, options) { // eslint-d const transports = new Map(); -async function getOrCreateMailer(sendConfiguration) { +async function getOrCreateMailer(sendConfigurationId) { + let sendConfiguration; + if (!sendConfiguration) { - sendConfiguration = sendConfigurations.getSystemSendConfiguration(); + sendConfiguration = await sendConfigurations.getSystemSendConfiguration(); + } else { + sendConfiguration = await sendConfigurations.getById(contextHelpers.getAdminContext(), sendConfigurationId, false); } const transport = transports.get(sendConfiguration.id) || await _createTransport(sendConfiguration); @@ -65,8 +69,8 @@ async function _sendMail(transport, mail, template) { const preparedHtml = await tools.prepareHtml(mail.html); - if (prepareHtml) { - mail.html = prepareHtml; + if (preparedHtml) { + mail.html = preparedHtml; } const textRenderer = await tools.getTemplate(template.text); diff --git a/lib/tools.js b/lib/tools.js index 0976e069..b6af3b96 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -67,7 +67,7 @@ async function mergeTemplateIntoLayout(template, layout) { } if (template.endsWith('.hbs')) { - template = readFile(template); + template = await readFile(template); } const source = layout.replace(/\{\{\{body\}\}\}/g, template); @@ -121,7 +121,7 @@ async function prepareHtml(html) { FetchExternalResources: false, // disables resource loading over HTTP / filesystem ProcessExternalResources: false // do not execute JS within script blocks } - }) + }); const head = win.document.querySelector('head'); let hasCharsetTag = false; diff --git a/models/send-configurations.js b/models/send-configurations.js index 0b808fa3..ce5d5263 100644 --- a/models/send-configurations.js +++ b/models/send-configurations.js @@ -114,7 +114,7 @@ async function remove(context, id) { } async function getSystemSendConfiguration() { - return await getById(contextHelpers.getAdminContext(), getSystemSendConfigurationId()); + return await getById(contextHelpers.getAdminContext(), getSystemSendConfigurationId(), false); } module.exports = { diff --git a/routes/subscription.js b/routes/subscription.js index ef54c35f..fbd7d16e 100644 --- a/routes/subscription.js +++ b/routes/subscription.js @@ -185,6 +185,7 @@ async function _renderSubscribe(req, res, list, subscription) { data.isWeb = true; data.needsJsWarning = true; + data.flashMessages = await captureFlashMessages(res); const result = htmlRenderer(data);