From c8230060a9ea4eb8f56be25b1169c04b0f2a0172 Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Fri, 22 Apr 2022 14:27:19 +0200 Subject: [PATCH] Added the default newsletter (#14468) refs https://github.com/TryGhost/Team/issues/1473 - Added the default newsletter - We use the title to populate the newsletter same, slug and sender name - We use the description to populate the newsletter description - We use the global design settings to populate the corresponding newsletter design settings Co-authored-by: Matt Hanley --- ...2022-04-13-13-00-add-default-newsletter.js | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 core/server/data/migrations/versions/4.46/2022-04-13-13-00-add-default-newsletter.js diff --git a/core/server/data/migrations/versions/4.46/2022-04-13-13-00-add-default-newsletter.js b/core/server/data/migrations/versions/4.46/2022-04-13-13-00-add-default-newsletter.js new file mode 100644 index 0000000000..b0cbdac471 --- /dev/null +++ b/core/server/data/migrations/versions/4.46/2022-04-13-13-00-add-default-newsletter.js @@ -0,0 +1,83 @@ +const ObjectId = require('bson-objectid'); +const logging = require('@tryghost/logging'); +const startsWith = require('lodash/startsWith'); +const {createTransactionalMigration} = require('../../utils'); + +// This uses the default settings from core/server/data/schema/default-settings/default-settings.json +const newsletter = { + id: (new ObjectId()).toHexString(), + name: 'Ghost', + description: '', + slug: 'default-newsletter', + sender_name: null, + sender_email: null, + sender_reply_to: 'newsletter', + status: 'active', + visibility: 'members', + subscribe_on_signup: true, + sort_order: 0, + body_font_category: 'sans_serif', + footer_content: '', + header_image: null, + show_badge: true, + show_feature_image: true, + show_header_icon: true, + show_header_title: true, + show_header_name: false, + title_alignment: 'center', + title_font_category: 'sans_serif' +}; + +module.exports = createTransactionalMigration( + async function up(knex) { + // Make sure the newsletter table is empty + const newsletters = await knex('newsletters').count('*', {as: 'total'}); + + if (newsletters[0].total !== 0) { + logging.info('Skipping adding the default newsletter - There is already at least one newsletter'); + return; + } + + // Get all settings in one query + const settings = await knex('settings') + .whereIn('key', [ + 'title', + 'description', + 'newsletter_body_font_category', + 'newsletter_footer_content', + 'newsletter_header_image', + 'newsletter_show_badge', + 'newsletter_show_feature_image', + 'newsletter_show_header_icon', + 'newsletter_show_header_title', + 'newsletter_title_alignment', + 'newsletter_title_font_category' + ]) + .select(['key', 'value']); + + // eslint-disable-next-line no-restricted-syntax + for (let {key, value} of settings) { + // Use site title for the newsletter name + if (key === 'title') { + key = 'name'; + } + // Settings have a `newsletter_` prefix which isn't present on the newsletters table + if (startsWith(key, 'newsletter_')) { + key = key.slice(11); + } + if (typeof newsletter[key] === 'boolean') { + newsletter[key] = value === 'true'; + } else { + newsletter[key] = value; + } + } + + logging.info('Adding the default newsletter'); + await knex('newsletters').insert(newsletter); + }, + async function down() { + // Noop because we don't want to reset the default newsletter values + logging.info(`Skipping newsletter design settings backfill rollback - not needed`); + return Promise.resolve(); + } +);