diff --git a/core/server/api/canary/utils/serializers/output/emails.js b/core/server/api/canary/utils/serializers/output/emails.js index f355f66b7d..8493c6b517 100644 --- a/core/server/api/canary/utils/serializers/output/emails.js +++ b/core/server/api/canary/utils/serializers/output/emails.js @@ -1,7 +1,9 @@ +const mapper = require('./utils/mapper'); + module.exports = { read(email, apiConfig, frame) { frame.response = { - emails: [email.toJSON(frame.options)] + emails: [mapper.mapEmail(email, frame)] }; }, diff --git a/core/server/api/canary/utils/serializers/output/utils/mapper.js b/core/server/api/canary/utils/serializers/output/utils/mapper.js index bd2c5abaab..38909e37b2 100644 --- a/core/server/api/canary/utils/serializers/output/utils/mapper.js +++ b/core/server/api/canary/utils/serializers/output/utils/mapper.js @@ -7,6 +7,7 @@ const clean = require('./clean'); const extraAttrs = require('./extra-attrs'); const postsMetaSchema = require('../../../../../../data/schema').tables.posts_meta; const config = require('../../../../../../../shared/config'); +const mega = require('../../../../../../services/mega'); const mapUser = (model, frame) => { const jsonModel = model.toJSON ? model.toJSON(frame.options) : model; @@ -62,6 +63,10 @@ const mapPost = (model, frame) => { jsonModel.authors = jsonModel.authors.map(author => mapUser(author, frame)); } + if (relation === 'email' && jsonModel.email) { + jsonModel.email = mapEmail(jsonModel.email, frame); + } + if (relation === 'email' && _.isEmpty(jsonModel.email)) { jsonModel.email = null; } @@ -159,6 +164,22 @@ const mapLabel = (model, frame) => { return jsonModel; }; +const mapEmail = (model, frame) => { + const jsonModel = model.toJSON ? model.toJSON(frame.options) : model; + + // Ensure we're not outputting unwanted replacement strings when viewing email contents + // TODO: extract this to a utility, it's duplicated in the email-preview API controller + const replacements = mega.postEmailSerializer.parseReplacements(jsonModel); + replacements.forEach((replacement) => { + jsonModel[replacement.format] = jsonModel[replacement.format].replace( + replacement.match, + replacement.fallback || '' + ); + }); + + return jsonModel; +}; + module.exports.mapPost = mapPost; module.exports.mapPage = mapPage; module.exports.mapUser = mapUser; @@ -169,3 +190,4 @@ module.exports.mapSettings = mapSettings; module.exports.mapImage = mapImage; module.exports.mapAction = mapAction; module.exports.mapMember = mapMember; +module.exports.mapEmail = mapEmail;