From 8b6ec4d922a6859e60956cc5a8f308bd7719c5c2 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Mon, 6 Jul 2020 17:09:43 +0200 Subject: [PATCH] Emitted all settings events on reinit of cache (#12012) closes #12003 There are a few parts of Ghost that rely on the settings events being emitted anytime a setting is changed, so that the data is kept in sync. When a setting is renamed in a migration essentially what happens is that the settings value is changed from a default value to its actual value, but this does no emit an event. Anything that is initialised before migrations have run that relies on the events to keep it up to date will have stale data - e.g. the themes i18n service. This change ensures that when we `reinit` after migrations have been run, we emit events for every setting to tell the rest of Ghost that it has changed. --- core/server/services/settings/index.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/core/server/services/settings/index.js b/core/server/services/settings/index.js index 3c4991fbe4..7f1e77876b 100644 --- a/core/server/services/settings/index.js +++ b/core/server/services/settings/index.js @@ -6,18 +6,17 @@ const models = require('../../models'); const SettingsCache = require('./cache'); module.exports = { - init: function init() { - // Update the defaults - return models.Settings.populateDefaults() - .then((settingsCollection) => { - // Initialise the cache with the result - // This will bind to events for further updates - SettingsCache.init(settingsCollection); - }); + async init() { + const settingsCollection = await models.Settings.populateDefaults(); + SettingsCache.init(settingsCollection); }, - reinit: function reinit() { + async reinit() { SettingsCache.shutdown(); - return this.init(); + const settingsCollection = await models.Settings.populateDefaults(); + SettingsCache.init(settingsCollection); + for (const model of settingsCollection.models) { + model.emitChange(model.attributes.key + '.' + 'edited', {}); + } } };