From 54b4a3c35134950827fdd8a34973c3d72440cc20 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 10 May 2022 12:55:58 +0100 Subject: [PATCH] Renamed settings index.js to settings-service.js - in line with our policy of not keeping logic in index.js files - Note: callign all services service.js is no more helpful :D --- core/boot.js | 4 +- core/server/api/canary/settings.js | 4 +- core/server/run-update-check.js | 2 +- .../api-version-compatibility/index.js | 2 +- core/server/services/settings/index.js | 78 +------------------ .../services/settings/settings-service.js | 77 ++++++++++++++++++ core/shared/settings-cache/public.js | 4 - test/utils/e2e-framework.js | 2 +- test/utils/e2e-utils.js | 2 +- test/utils/fixture-utils.js | 2 +- 10 files changed, 87 insertions(+), 90 deletions(-) create mode 100644 core/server/services/settings/settings-service.js diff --git a/core/boot.js b/core/boot.js index 4d8e9e318f..0195440474 100644 --- a/core/boot.js +++ b/core/boot.js @@ -97,7 +97,7 @@ async function initCore({ghostServer, config, bootLogger, frontend}) { // Settings are a core concept we use settings to store key-value pairs used in critical pathways as well as public data like the site title debug('Begin: settings'); - const settings = require('./server/services/settings'); + const settings = require('./server/services/settings/settings-service'); await settings.init(); await settings.syncEmailSettings(config.get('hostSettings:emailVerification:verified')); debug('End: settings'); @@ -235,7 +235,7 @@ async function initDynamicRouting() { routing.routerManager.start(routeSettings); const getRoutesHash = () => routeSettingsService.api.getCurrentHash(); - const settings = require('./server/services/settings'); + const settings = require('./server/services/settings/settings-service'); await settings.syncRoutesHash(getRoutesHash); debug('End: Dynamic Routing'); diff --git a/core/server/api/canary/settings.js b/core/server/api/canary/settings.js index 700a0e9143..43d041689b 100644 --- a/core/server/api/canary/settings.js +++ b/core/server/api/canary/settings.js @@ -4,7 +4,7 @@ const models = require('../../models'); const routeSettings = require('../../services/route-settings'); const tpl = require('@tryghost/tpl'); const {BadRequestError} = require('@tryghost/errors'); -const settingsService = require('../../services/settings'); +const settingsService = require('../../services/settings/settings-service'); const membersService = require('../../services/members'); const stripeService = require('../../services/stripe'); @@ -12,7 +12,7 @@ const settingsBREADService = settingsService.getSettingsBREADServiceInstance(); const messages = { failedSendingEmail: 'Failed Sending Email' - + }; module.exports = { diff --git a/core/server/run-update-check.js b/core/server/run-update-check.js index c7bece4583..7086071051 100644 --- a/core/server/run-update-check.js +++ b/core/server/run-update-check.js @@ -38,7 +38,7 @@ if (parentPort) { const permissions = require('./services/permissions'); await permissions.init(); - const settings = require('./services/settings'); + const settings = require('./services/settings/settings-service'); await settings.init(); // Finished INIT diff --git a/core/server/services/api-version-compatibility/index.js b/core/server/services/api-version-compatibility/index.js index f6f3539fe6..81f7a4a70d 100644 --- a/core/server/services/api-version-compatibility/index.js +++ b/core/server/services/api-version-compatibility/index.js @@ -2,7 +2,7 @@ const APIVersionCompatibilityService = require('@tryghost/api-version-compatibil const versionMismatchHandler = require('@tryghost/mw-api-version-mismatch'); const ghostVersion = require('@tryghost/version'); const {GhostMailer} = require('../mail'); -const settingsService = require('../../services/settings'); +const settingsService = require('../settings/settings-service'); const models = require('../../models'); const urlUtils = require('../../../shared/url-utils'); const settingsCache = require('../../../shared/settings-cache'); diff --git a/core/server/services/settings/index.js b/core/server/services/settings/index.js index 10f4396c7c..50df3ae1d7 100644 --- a/core/server/services/settings/index.js +++ b/core/server/services/settings/index.js @@ -1,77 +1 @@ -/** - * Settings Lib - * A collection of utilities for handling settings including a cache - */ -const events = require('../../lib/common/events'); -const models = require('../../models'); -const labs = require('../../../shared/labs'); -const SettingsCache = require('../../../shared/settings-cache'); -const SettingsBREADService = require('./settings-bread-service'); -const {obfuscatedSetting, isSecretSetting, hideValueIfSecret} = require('./settings-utils'); - -/** - * @returns {SettingsBREADService} instance of the PostsService - */ -const getSettingsBREADServiceInstance = () => { - return new SettingsBREADService({ - SettingsModel: models.Settings, - settingsCache: SettingsCache, - labsService: labs - }); -}; - -module.exports = { - /** - * Initialize the cache, used in boot and in testing - */ - async init() { - const settingsCollection = await models.Settings.populateDefaults(); - SettingsCache.init(events, settingsCollection); - }, - - /** - * Restore the cache, used during e2e testing only - */ - reset() { - SettingsCache.reset(events); - }, - - /** - * Handles synchronization of routes.yaml hash loaded in the frontend with - * the value stored in the settings table. - * getRoutesHash is a function to allow keeping "frontend" decoupled from settings - * - * @param {function} getRoutesHash function fetching currently loaded routes file hash - */ - async syncRoutesHash(getRoutesHash) { - const currentRoutesHash = await getRoutesHash(); - - if (SettingsCache.get('routes_hash') !== currentRoutesHash) { - return await models.Settings.edit([{ - key: 'routes_hash', - value: currentRoutesHash - }], {context: {internal: true}}); - } - }, - - /** - * Handles email setting synchronization when email has been verified per instance - * - * @param {boolean} configValue current email verification value from local config - */ - async syncEmailSettings(configValue) { - const isEmailDisabled = SettingsCache.get('email_verification_required'); - - if (configValue === true && isEmailDisabled) { - return await models.Settings.edit([{ - key: 'email_verification_required', - value: false - }], {context: {internal: true}}); - } - }, - - obfuscatedSetting, - isSecretSetting, - hideValueIfSecret, - getSettingsBREADServiceInstance -}; +module.exports = require('./settings-service'); diff --git a/core/server/services/settings/settings-service.js b/core/server/services/settings/settings-service.js new file mode 100644 index 0000000000..10f4396c7c --- /dev/null +++ b/core/server/services/settings/settings-service.js @@ -0,0 +1,77 @@ +/** + * Settings Lib + * A collection of utilities for handling settings including a cache + */ +const events = require('../../lib/common/events'); +const models = require('../../models'); +const labs = require('../../../shared/labs'); +const SettingsCache = require('../../../shared/settings-cache'); +const SettingsBREADService = require('./settings-bread-service'); +const {obfuscatedSetting, isSecretSetting, hideValueIfSecret} = require('./settings-utils'); + +/** + * @returns {SettingsBREADService} instance of the PostsService + */ +const getSettingsBREADServiceInstance = () => { + return new SettingsBREADService({ + SettingsModel: models.Settings, + settingsCache: SettingsCache, + labsService: labs + }); +}; + +module.exports = { + /** + * Initialize the cache, used in boot and in testing + */ + async init() { + const settingsCollection = await models.Settings.populateDefaults(); + SettingsCache.init(events, settingsCollection); + }, + + /** + * Restore the cache, used during e2e testing only + */ + reset() { + SettingsCache.reset(events); + }, + + /** + * Handles synchronization of routes.yaml hash loaded in the frontend with + * the value stored in the settings table. + * getRoutesHash is a function to allow keeping "frontend" decoupled from settings + * + * @param {function} getRoutesHash function fetching currently loaded routes file hash + */ + async syncRoutesHash(getRoutesHash) { + const currentRoutesHash = await getRoutesHash(); + + if (SettingsCache.get('routes_hash') !== currentRoutesHash) { + return await models.Settings.edit([{ + key: 'routes_hash', + value: currentRoutesHash + }], {context: {internal: true}}); + } + }, + + /** + * Handles email setting synchronization when email has been verified per instance + * + * @param {boolean} configValue current email verification value from local config + */ + async syncEmailSettings(configValue) { + const isEmailDisabled = SettingsCache.get('email_verification_required'); + + if (configValue === true && isEmailDisabled) { + return await models.Settings.edit([{ + key: 'email_verification_required', + value: false + }], {context: {internal: true}}); + } + }, + + obfuscatedSetting, + isSecretSetting, + hideValueIfSecret, + getSettingsBREADServiceInstance +}; diff --git a/core/shared/settings-cache/public.js b/core/shared/settings-cache/public.js index 861ad8157e..37b4ba21f1 100644 --- a/core/shared/settings-cache/public.js +++ b/core/shared/settings-cache/public.js @@ -1,8 +1,4 @@ /** - * The settings with type "site" were originally meant to be public - * This has been misused - unsplash and slack are incorrectly stored there - * https://github.com/TryGhost/Ghost/issues/10318 - * * This file acts as an allowlist for "public" settings */ diff --git a/test/utils/e2e-framework.js b/test/utils/e2e-framework.js index a7948ae76f..137825002c 100644 --- a/test/utils/e2e-framework.js +++ b/test/utils/e2e-framework.js @@ -33,7 +33,7 @@ const ContentAPITestAgent = require('./content-api-test-agent'); const db = require('./db-utils'); // Services that need resetting -const settingsService = require('../../core/server/services/settings'); +const settingsService = require('../../core/server/services/settings/settings-service'); /** * @param {Object} [options={}] diff --git a/test/utils/e2e-utils.js b/test/utils/e2e-utils.js index 4ffc147a0d..e724663983 100644 --- a/test/utils/e2e-utils.js +++ b/test/utils/e2e-utils.js @@ -15,7 +15,7 @@ const boot = require('../../core/boot'); const db = require('../../core/server/data/db'); const models = require('../../core/server/models'); const urlService = require('../../core/server/services/url'); -const settingsService = require('../../core/server/services/settings'); +const settingsService = require('../../core/server/services/settings/settings-service'); const routeSettingsService = require('../../core/server/services/route-settings'); const themeService = require('../../core/server/services/themes'); const limits = require('../../core/server/services/limits'); diff --git a/test/utils/fixture-utils.js b/test/utils/fixture-utils.js index f6a5e06ced..ad56d94bca 100644 --- a/test/utils/fixture-utils.js +++ b/test/utils/fixture-utils.js @@ -14,7 +14,7 @@ const models = require('../../core/server/models'); const {fixtureManager} = require('../../core/server/data/schema/fixtures'); const emailAnalyticsService = require('../../core/server/services/email-analytics'); const permissions = require('../../core/server/services/permissions'); -const settingsService = require('../../core/server/services/settings'); +const settingsService = require('../../core/server/services/settings/settings-service'); const labsService = require('../../core/shared/labs'); // Other Test Utilities