mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
a30e42404b
closes https://github.com/TryGhost/Team/issues/1626 - getLazyRelation is a safer shorthand for `model.related('relationName').fetch()` - prevents doing a `fetch` operation on a relation that is already loaded, which can cause issues when `formatOnWrite` has a custom implementation - uses the already loaded relation if it exists, or loads the relation - doesn't reload if already loaded - reload is forceable using the forceRefresh option
43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
const postEmailSerializer = require('./post-email-serializer');
|
|
const models = require('../../models');
|
|
|
|
class EmailPreview {
|
|
/**
|
|
* @param {Object} post - Post model object instance
|
|
* @param {Object} options
|
|
* @param {String} options.newsletter - newsletter slug
|
|
* @param {String} options.memberSegment - member segment filter
|
|
* @returns {Promise<Object>}
|
|
*/
|
|
async generateEmailContent(post, {newsletter, memberSegment} = {}) {
|
|
let newsletterModel = await post.getLazyRelation('newsletter');
|
|
if (!newsletterModel) {
|
|
if (newsletter) {
|
|
newsletterModel = await models.Newsletter.findOne({slug: newsletter});
|
|
} else {
|
|
newsletterModel = await models.Newsletter.getDefaultNewsletter();
|
|
}
|
|
}
|
|
|
|
let emailContent = await postEmailSerializer.serialize(post, newsletterModel, {
|
|
isBrowserPreview: true
|
|
});
|
|
|
|
if (memberSegment) {
|
|
emailContent = postEmailSerializer.renderEmailForSegment(emailContent, memberSegment);
|
|
}
|
|
|
|
const replacements = postEmailSerializer.parseReplacements(emailContent);
|
|
|
|
replacements.forEach((replacement) => {
|
|
emailContent[replacement.format] = emailContent[replacement.format].replace(
|
|
replacement.match,
|
|
replacement.fallback || ''
|
|
);
|
|
});
|
|
|
|
return emailContent;
|
|
}
|
|
}
|
|
|
|
module.exports = EmailPreview;
|