From c5f7adf9177eaed5e3ab86561ef22600d6b60665 Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Mon, 20 Apr 2020 15:35:33 +0100 Subject: [PATCH] Fixed "view sent email" showing mailgun template variables no issue - the `email.{html,plaintext}` fields are only used to display what was sent in the email so it doesn't make sense to store the mailgun-specific content which can be confusing when viewing in the admin area - store the raw serialized post content with a basic no-data replacement of replacement strings rather than the output of full data fetching and mailgun transformation --- core/server/services/mega/mega.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index bd261ad06d..91557e7dfa 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -83,10 +83,9 @@ const addEmail = async (postModel, options) => { const membersToSendTo = members.filter((member) => { return membersService.contentGating.checkPostAccess(postModel.toJSON(), member); }); - const {emailTmpl, emails} = await getEmailData(postModel, membersToSendTo); // NOTE: don't create email object when there's nobody to send the email to - if (!emails.length) { + if (!membersToSendTo.length) { return null; } @@ -94,10 +93,20 @@ const addEmail = async (postModel, options) => { const existing = await models.Email.findOne({post_id: postId}, knexOptions); if (!existing) { + // get email contents and perform replacements using no member data so + // we have a decent snapshot of email content for later display + const {emailTmpl, replacements} = await postEmailSerializer.serialize(postModel, {isBrowserPreview: true}); + replacements.forEach((replacement) => { + emailTmpl[replacement.format] = emailTmpl[replacement.format].replace( + replacement.match, + replacement.fallback || '' + ); + }); + return models.Email.add({ post_id: postId, status: 'pending', - email_count: emails.length, + email_count: membersToSendTo.length, subject: emailTmpl.subject, html: emailTmpl.html, plaintext: emailTmpl.plaintext,