0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Moved to using newsletter design settings in email serializer (#14562)

refs https://github.com/TryGhost/Team/issues/1550

- Switched to using the newsletter design settings over the global settings
- Made the `newsletter_id` property available in the Admin API Post resource
- Added the `showHeaderName` variable that can be used in the post html template
This commit is contained in:
Thibaut Patel 2022-04-26 13:01:27 +02:00 committed by Matt Hanley
parent ce5f55e43f
commit 7422f3e64d
9 changed files with 139 additions and 18 deletions

View file

@ -6,6 +6,7 @@ module.exports = async (model, frame, options) => {
delete jsonModel.email_subject;
delete jsonModel.email_recipient_filter;
delete jsonModel.email_only;
delete jsonModel.newsletter_id;
return jsonModel;
};

View file

@ -57,6 +57,8 @@ module.exports = async (model, frame, options = {}) => {
}
date.forPost(jsonModel);
gating.forPost(jsonModel, frame);
delete jsonModel.newsletter_id;
}
// Transforms post/page metadata to flat structure

View file

@ -48,6 +48,8 @@ const mapPost = (model, frame) => {
}
date.forPost(jsonModel);
gating.forPost(jsonModel, frame);
delete jsonModel.newsletter_id;
}
// Transforms post/page metadata to flat structure

View file

@ -45,6 +45,8 @@ const mapPost = (model, frame) => {
}
date.forPost(jsonModel);
gating.forPost(jsonModel, frame);
delete jsonModel.newsletter_id;
}
if (typeof jsonModel.email_recipient_filter === 'undefined') {
@ -100,6 +102,7 @@ const mapPage = (model, frame) => {
delete jsonModel.email_subject;
delete jsonModel.send_email_when_published;
delete jsonModel.email_recipient_filter;
delete jsonModel.newsletter_id;
return jsonModel;
};

View file

@ -891,9 +891,6 @@ Post = ghostBookshelf.Model.extend({
// CASE: never expose the revisions
delete attrs.mobiledoc_revisions;
// CASE: hide the newsletter_id for now
delete attrs.newsletter_id;
// If the current column settings allow it...
if (!options.columns || (options.columns && options.columns.indexOf('primary_tag') > -1)) {
// ... attach a computed property of primary_tag which is the first tag if it is public, else null

View file

@ -10,6 +10,7 @@ const htmlToText = require('html-to-text');
const {isUnsplashImage, isLocalContentImage} = require('@tryghost/kg-default-cards/lib/utils');
const {textColorForBackgroundColor, darkenToContrastThreshold} = require('@tryghost/color-utils');
const logging = require('@tryghost/logging');
const models = require('../../models');
const ALLOWED_REPLACEMENTS = ['first_name'];
@ -169,21 +170,30 @@ const parseReplacements = (email) => {
return replacements;
};
const getTemplateSettings = async () => {
const getTemplateSettings = async (newsletterId = null) => {
let newsletter;
if (newsletterId) {
newsletter = await models.Newsletter.findOne({id: newsletterId, filter: 'status:active'});
} else {
const newsletters = await models.Newsletter.findPage({filter: 'status:active', limit: 1});
newsletter = newsletters.data[0];
}
const accentColor = settingsCache.get('accent_color');
const adjustedAccentColor = accentColor && darkenToContrastThreshold(accentColor, '#ffffff', 2).hex();
const adjustedAccentContrastColor = accentColor && textColorForBackgroundColor(adjustedAccentColor).hex();
const templateSettings = {
headerImage: settingsCache.get('newsletter_header_image'),
showHeaderIcon: settingsCache.get('newsletter_show_header_icon') && settingsCache.get('icon'),
showHeaderTitle: settingsCache.get('newsletter_show_header_title'),
showFeatureImage: settingsCache.get('newsletter_show_feature_image'),
titleFontCategory: settingsCache.get('newsletter_title_font_category'),
titleAlignment: settingsCache.get('newsletter_title_alignment'),
bodyFontCategory: settingsCache.get('newsletter_body_font_category'),
showBadge: settingsCache.get('newsletter_show_badge'),
footerContent: settingsCache.get('newsletter_footer_content'),
headerImage: newsletter.get('header_image'),
showHeaderIcon: newsletter.get('show_header_icon') && settingsCache.get('icon'),
showHeaderTitle: newsletter.get('show_header_title'),
showFeatureImage: newsletter.get('show_feature_image'),
titleFontCategory: newsletter.get('title_font_category'),
titleAlignment: newsletter.get('title_alignment'),
bodyFontCategory: newsletter.get('body_font_category'),
showBadge: newsletter.get('show_badge'),
footerContent: newsletter.get('footer_content'),
showHeaderName: newsletter.get('show_header_name'),
accentColor,
adjustedAccentColor,
adjustedAccentContrastColor
@ -290,7 +300,7 @@ const serialize = async (postModel, options = {isBrowserPreview: false, apiVersi
}
}
const templateSettings = await getTemplateSettings();
const templateSettings = await getTemplateSettings(post.newsletter_id);
const render = template;
@ -339,5 +349,7 @@ module.exports = {
serialize,
createUnsubscribeUrl,
renderEmailForSegment,
parseReplacements
parseReplacements,
// Export for tests
_getTemplateSettings: getTemplateSettings
};

View file

@ -71,7 +71,8 @@ const expectedProperties = {
'email_subject',
'frontmatter',
'email_only',
'tiers'
'tiers',
'newsletter_id'
],
page: [

View file

@ -65,7 +65,8 @@ const expectedProperties = {
'email_subject',
'frontmatter',
'email_only',
'tiers'
'tiers',
'newsletter_id'
],
user: [
'id',

View file

@ -1,6 +1,9 @@
const should = require('should');
const sinon = require('sinon');
const settingsCache = require('../../../../../core/shared/settings-cache');
const models = require('../../../../../core/server/models');
const {parseReplacements, renderEmailForSegment} = require('../../../../../core/server/services/mega/post-email-serializer');
const {parseReplacements, renderEmailForSegment, _getTemplateSettings} = require('../../../../../core/server/services/mega/post-email-serializer');
describe('Post Email Serializer', function () {
it('creates replacement pattern for valid format and value', function () {
@ -81,4 +84,103 @@ describe('Post Email Serializer', function () {
output.plaintext.should.equal('hello');
});
});
describe('getTemplateSettings', function () {
before(function () {
models.init();
});
afterEach(function () {
sinon.restore();
});
it('uses the default newsletter settings when no newsletter_id is defined', async function () {
sinon.stub(settingsCache, 'get').callsFake(function (key) {
return {
icon: 'icon',
accent_color: '#990000'
}[key];
});
sinon.stub(models.Newsletter, 'findPage').returns(
Promise.resolve({
data: [{
get: function (key) {
return {
header_image: 'image',
show_header_icon: true,
show_header_title: true,
show_feature_image: true,
title_font_category: 'sans_serif',
title_alignment: 'center',
body_font_category: 'serif',
show_badge: true,
footer_content: '',
show_header_name: true
}[key];
}
}]
})
);
const res = await _getTemplateSettings();
should(res).eql({
headerImage: 'image',
showHeaderIcon: 'icon',
showHeaderTitle: true,
showHeaderName: true,
showFeatureImage: true,
titleFontCategory: 'sans_serif',
titleAlignment: 'center',
bodyFontCategory: 'serif',
showBadge: true,
footerContent: '',
accentColor: '#990000',
adjustedAccentColor: '#990000',
adjustedAccentContrastColor: '#FFFFFF'
});
});
it('uses the newsletter settings when a newsletter_id is defined', async function () {
sinon.stub(settingsCache, 'get').callsFake(function (key) {
return {
icon: 'icon2',
accent_color: '#000099'
}[key];
});
sinon.stub(models.Newsletter, 'findOne').returns(
Promise.resolve({
get: function (key) {
return {
header_image: 'image',
show_header_icon: true,
show_header_title: true,
show_feature_image: true,
title_font_category: 'sans-serif',
title_alignment: 'center',
body_font_category: 'serif',
show_badge: true,
footer_content: 'footer',
show_header_name: true
}[key];
}
})
);
const res = await _getTemplateSettings('123');
should(res).eql({
headerImage: 'image',
showHeaderIcon: 'icon2',
showHeaderTitle: true,
showFeatureImage: true,
titleFontCategory: 'sans-serif',
titleAlignment: 'center',
bodyFontCategory: 'serif',
showBadge: true,
footerContent: 'footer',
accentColor: '#000099',
adjustedAccentColor: '#000099',
adjustedAccentContrastColor: '#FFFFFF',
showHeaderName: true
});
});
});
});