2021-09-22 23:56:45 +03:00
|
|
|
const _ = require('lodash');
|
2023-05-02 23:43:47 +03:00
|
|
|
const BREAD = require('./CustomThemeSettingsBREADService');
|
2023-09-13 10:38:31 +03:00
|
|
|
const nql = require('@tryghost/nql');
|
2021-09-28 17:50:31 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-10-08 17:40:44 +03:00
|
|
|
const {ValidationError} = require('@tryghost/errors');
|
2021-09-22 23:56:45 +03:00
|
|
|
const debug = require('@tryghost/debug')('custom-theme-settings-service');
|
|
|
|
|
2021-09-28 17:50:31 +03:00
|
|
|
const messages = {
|
2021-10-08 17:40:44 +03:00
|
|
|
problemFindingSetting: 'Unknown setting: {key}.',
|
2021-10-20 12:23:11 +03:00
|
|
|
unallowedValueForSetting: 'Unallowed value for \'{key}\'. Allowed values: {allowedValues}.',
|
|
|
|
invalidValueForSetting: 'Invalid value for \'{key}\'. The value must follow this format: {format}.'
|
2021-09-28 17:50:31 +03:00
|
|
|
};
|
|
|
|
|
2023-09-13 10:38:31 +03:00
|
|
|
const HIDDEN_SETTING_VALUE = null;
|
|
|
|
|
2021-09-22 23:56:45 +03:00
|
|
|
module.exports = class CustomThemeSettingsService {
|
2021-09-23 22:34:35 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {any} options.model - Bookshelf-like model instance for storing theme setting key/value pairs
|
2023-05-02 23:43:47 +03:00
|
|
|
* @param {import('./CustomThemeSettingsCache')} options.cache - Instance of a custom key/value pair cache
|
2021-09-23 22:34:35 +03:00
|
|
|
*/
|
2021-09-22 23:56:45 +03:00
|
|
|
constructor({model, cache}) {
|
2021-09-23 22:34:35 +03:00
|
|
|
this.activeThemeName = null;
|
|
|
|
|
|
|
|
/** @private */
|
2022-08-04 13:29:58 +03:00
|
|
|
this._activatingPromise = null;
|
|
|
|
this._activatingName = null;
|
|
|
|
this._activatingSettings = null;
|
|
|
|
|
2021-10-07 17:28:02 +03:00
|
|
|
this._repository = new BREAD({model});
|
|
|
|
this._valueCache = cache;
|
|
|
|
this._activeThemeSettings = {};
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:34:35 +03:00
|
|
|
/**
|
|
|
|
* The service only deals with one theme at a time,
|
2022-08-04 13:29:58 +03:00
|
|
|
* that theme is changed by calling this method with the output from gscan.
|
|
|
|
*
|
|
|
|
* To avoid syncing issues with activateTheme being called in quick succession,
|
|
|
|
* any previous/still-running activation promise is awaited before re-starting
|
|
|
|
* if necessary.
|
2021-09-23 22:34:35 +03:00
|
|
|
*
|
2021-10-20 13:04:44 +03:00
|
|
|
* @param {string} name - the name of the theme (Ghost has different names to themes with duplicate package.json names)
|
2021-09-23 22:34:35 +03:00
|
|
|
* @param {Object} theme - checked theme output from gscan
|
|
|
|
*/
|
2021-10-20 13:04:44 +03:00
|
|
|
async activateTheme(name, theme) {
|
2022-08-04 13:29:58 +03:00
|
|
|
const activate = async () => {
|
|
|
|
this.activeThemeName = name;
|
|
|
|
|
|
|
|
// add/remove/edit key/value records in the respository to match theme settings
|
|
|
|
const settings = await this._syncRepositoryWithTheme(name, theme);
|
|
|
|
|
|
|
|
// populate the shared cache with all key/value pairs for this theme
|
|
|
|
this._populateValueCacheForTheme(theme, settings);
|
|
|
|
// populate the cache used for exposing full setting details for editing
|
|
|
|
this._populateInternalCacheForTheme(theme, settings);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._activatingPromise) {
|
|
|
|
// NOTE: must be calculated before awaiting promise as the promise finishing will clear the properties
|
|
|
|
const isSameName = name === this._activatingName;
|
|
|
|
const isSameSettings = JSON.stringify(theme.customSettings) === this._activatingSettings;
|
2021-09-23 22:34:35 +03:00
|
|
|
|
2022-08-04 13:29:58 +03:00
|
|
|
// wait for previous activation to finish
|
|
|
|
await this._activatingPromise;
|
2021-09-23 22:34:35 +03:00
|
|
|
|
2022-08-04 13:29:58 +03:00
|
|
|
// skip sync if we're re-activating exactly the same theme settings
|
|
|
|
if (isSameName && isSameSettings) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._activatingName = name;
|
|
|
|
this._activatingSettings = JSON.stringify(theme.customSettings);
|
|
|
|
this._activatingPromise = activate();
|
|
|
|
|
|
|
|
await this._activatingPromise;
|
|
|
|
} finally {
|
|
|
|
this._activatingPromise = null;
|
|
|
|
this._activatingName = null;
|
|
|
|
this._activatingSettings = null;
|
|
|
|
}
|
2021-09-23 22:34:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the key'd internal cache object to an array suitable for use with Ghost's API
|
|
|
|
*/
|
|
|
|
listSettings() {
|
2021-10-07 17:28:02 +03:00
|
|
|
const settingObjects = Object.entries(this._activeThemeSettings).map(([key, setting]) => {
|
2021-09-23 22:34:35 +03:00
|
|
|
return Object.assign({}, setting, {key});
|
|
|
|
});
|
2021-09-22 23:56:45 +03:00
|
|
|
|
2021-09-23 22:34:35 +03:00
|
|
|
return settingObjects;
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:50:31 +03:00
|
|
|
/**
|
|
|
|
* @param {Array} settings - array of setting objects with at least key and value properties
|
|
|
|
*/
|
|
|
|
async updateSettings(settings) {
|
|
|
|
// abort if any settings do not match known settings
|
2021-10-07 17:28:02 +03:00
|
|
|
const firstUnknownSetting = settings.find(setting => !this._activeThemeSettings[setting.key]);
|
2021-09-28 17:50:31 +03:00
|
|
|
|
|
|
|
if (firstUnknownSetting) {
|
2021-10-08 17:40:44 +03:00
|
|
|
throw new ValidationError({
|
2021-09-28 17:50:31 +03:00
|
|
|
message: tpl(messages.problemFindingSetting, {key: firstUnknownSetting.key})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:23:11 +03:00
|
|
|
settings.forEach((setting) => {
|
|
|
|
const definition = this._activeThemeSettings[setting.key];
|
|
|
|
switch (definition.type) {
|
|
|
|
case 'select':
|
|
|
|
if (!definition.options.includes(setting.value)) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.unallowedValueForSetting, {key: setting.key, allowedValues: definition.options.join(', ')})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
if (![true, false].includes(setting.value)) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.unallowedValueForSetting, {key: setting.key, allowedValues: [true, false].join(', ')})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'color':
|
|
|
|
if (!/^#[0-9a-f]{6}$/i.test(setting.value)) {
|
|
|
|
throw new ValidationError({
|
|
|
|
message: tpl(messages.invalidValueForSetting, {key: setting.key, format: '#1234AF'})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-09-28 17:50:31 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// save the new values
|
|
|
|
for (const setting of settings) {
|
|
|
|
const theme = this.activeThemeName;
|
|
|
|
const {key, value} = setting;
|
|
|
|
|
2021-10-07 17:28:02 +03:00
|
|
|
const settingRecord = await this._repository.read({theme, key});
|
2021-09-28 17:50:31 +03:00
|
|
|
|
|
|
|
settingRecord.set('value', value);
|
|
|
|
|
|
|
|
if (settingRecord.hasChanged()) {
|
|
|
|
await settingRecord.save(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the internal cache
|
2021-10-07 17:28:02 +03:00
|
|
|
this._activeThemeSettings[setting.key].value = setting.value;
|
2021-09-28 17:50:31 +03:00
|
|
|
}
|
|
|
|
|
2023-09-13 10:38:31 +03:00
|
|
|
const settingsObjects = this.listSettings();
|
|
|
|
|
2021-09-28 17:50:31 +03:00
|
|
|
// update the public cache
|
2023-09-13 10:38:31 +03:00
|
|
|
this._valueCache.populate(
|
|
|
|
this._computeCachedSettings(settingsObjects)
|
|
|
|
);
|
2021-09-28 17:50:31 +03:00
|
|
|
|
|
|
|
// return full setting objects
|
2023-09-13 10:38:31 +03:00
|
|
|
return settingsObjects;
|
2021-09-28 17:50:31 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:34:35 +03:00
|
|
|
// Private -----------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} theme - checked theme output from gscan
|
2021-10-07 18:57:52 +03:00
|
|
|
* @returns {Array} - list of stored theme record objects
|
2021-09-23 22:34:35 +03:00
|
|
|
* @private
|
|
|
|
*/
|
2021-10-20 13:04:44 +03:00
|
|
|
async _syncRepositoryWithTheme(name, theme) {
|
2021-09-22 23:56:45 +03:00
|
|
|
const themeSettings = theme.customSettings || {};
|
|
|
|
|
2021-10-26 12:14:39 +03:00
|
|
|
const settingsCollection = await this._repository.browse({filter: `theme:'${name}'`});
|
2021-09-23 22:34:35 +03:00
|
|
|
let knownSettings = settingsCollection.toJSON();
|
|
|
|
|
2021-09-22 23:56:45 +03:00
|
|
|
// exit early if there's nothing to sync for this theme
|
|
|
|
if (knownSettings.length === 0 && _.isEmpty(themeSettings)) {
|
2021-10-07 18:57:52 +03:00
|
|
|
return [];
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let removedIds = [];
|
|
|
|
|
|
|
|
// sync any knownSettings that have changed in the theme
|
|
|
|
for (const knownSetting of knownSettings) {
|
|
|
|
const themeSetting = themeSettings[knownSetting.key];
|
|
|
|
|
|
|
|
const hasBeenRemoved = !themeSetting;
|
|
|
|
const hasChangedType = themeSetting && themeSetting.type !== knownSetting.type;
|
|
|
|
|
|
|
|
if (hasBeenRemoved || hasChangedType) {
|
2021-10-20 13:04:44 +03:00
|
|
|
debug(`Removing custom theme setting '${name}.${knownSetting.key}' - ${hasBeenRemoved ? 'not found in theme' : 'type changed'}`);
|
2021-10-07 17:28:02 +03:00
|
|
|
await this._repository.destroy({id: knownSetting.id});
|
2021-09-22 23:56:45 +03:00
|
|
|
removedIds.push(knownSetting.id);
|
2021-10-04 13:50:31 +03:00
|
|
|
continue;
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// replace value with default if it's not a valid select option
|
|
|
|
if (themeSetting.options && !themeSetting.options.includes(knownSetting.value)) {
|
2021-10-20 13:04:44 +03:00
|
|
|
debug(`Resetting custom theme setting value '${name}.${themeSetting.key}' - "${knownSetting.value}" is not a valid option`);
|
2021-10-07 17:28:02 +03:00
|
|
|
await this._repository.edit({value: themeSetting.default}, {id: knownSetting.id});
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up any removed knownSettings now that we've finished looping over them
|
|
|
|
knownSettings = knownSettings.filter(setting => !removedIds.includes(setting.id));
|
|
|
|
|
|
|
|
// add any new settings found in theme (or re-add settings that were removed due to type change)
|
|
|
|
const knownSettingsKeys = knownSettings.map(setting => setting.key);
|
|
|
|
|
|
|
|
for (const [key, setting] of Object.entries(themeSettings)) {
|
|
|
|
if (!knownSettingsKeys.includes(key)) {
|
|
|
|
const newSettingValues = {
|
2021-10-20 13:04:44 +03:00
|
|
|
theme: name,
|
2021-09-22 23:56:45 +03:00
|
|
|
key,
|
|
|
|
type: setting.type,
|
|
|
|
value: setting.default
|
|
|
|
};
|
|
|
|
|
2021-10-20 13:04:44 +03:00
|
|
|
debug(`Adding custom theme setting '${name}.${key}'`);
|
2021-10-07 17:28:02 +03:00
|
|
|
await this._repository.add(newSettingValues);
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:14:39 +03:00
|
|
|
const updatedSettingsCollection = await this._repository.browse({filter: `theme:'${name}'`});
|
2021-09-23 22:34:35 +03:00
|
|
|
return updatedSettingsCollection.toJSON();
|
2021-09-23 11:16:59 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:34:35 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} theme - checked theme output from gscan
|
|
|
|
* @param {Array} settings - theme settings fetched from repository
|
|
|
|
* @private
|
|
|
|
*/
|
2021-10-07 17:28:02 +03:00
|
|
|
_populateValueCacheForTheme(theme, settings) {
|
2021-09-23 22:34:35 +03:00
|
|
|
if (_.isEmpty(theme.customSettings)) {
|
2021-10-07 17:28:02 +03:00
|
|
|
this._valueCache.populate([]);
|
2021-09-23 22:34:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:38:31 +03:00
|
|
|
this._valueCache.populate(
|
|
|
|
this._computeCachedSettings(settings)
|
|
|
|
);
|
2021-09-23 22:34:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Object} theme - checked theme output from gscan
|
|
|
|
* @param {Array} settings - theme settings fetched from repository
|
|
|
|
* @private
|
|
|
|
*/
|
2021-10-07 17:28:02 +03:00
|
|
|
_populateInternalCacheForTheme(theme, settings) {
|
2021-09-23 22:34:35 +03:00
|
|
|
if (_.isEmpty(theme.customSettings)) {
|
2021-10-07 17:28:02 +03:00
|
|
|
this._activeThemeSettings = new Map();
|
2021-09-23 22:34:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settingValues = settings.reduce((acc, setting) => {
|
|
|
|
acc[setting.key] = setting;
|
|
|
|
return acc;
|
|
|
|
}, new Object());
|
|
|
|
|
|
|
|
const activeThemeSettings = new Object();
|
|
|
|
|
|
|
|
for (const [key, setting] of Object.entries(theme.customSettings)) {
|
|
|
|
// value comes from the stored key/value pairs rather than theme, we don't need the ID - theme name + key is enough
|
|
|
|
activeThemeSettings[key] = Object.assign({}, setting, {
|
|
|
|
id: settingValues[key].id,
|
|
|
|
value: settingValues[key].value
|
|
|
|
});
|
|
|
|
}
|
2021-09-23 11:16:59 +03:00
|
|
|
|
2021-10-07 17:28:02 +03:00
|
|
|
this._activeThemeSettings = activeThemeSettings;
|
2021-09-22 23:56:45 +03:00
|
|
|
}
|
2023-09-13 10:38:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the settings to cache, taking into account visibility rules
|
|
|
|
*
|
|
|
|
* @param {Object[]} settings - list of setting objects
|
|
|
|
* @returns {Object[]} - list of setting objects with visibility rules applied
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_computeCachedSettings(settings) {
|
|
|
|
const settingsMap = settings.reduce((map, {key, value}) => ({...map, [key]: value}), {});
|
|
|
|
|
|
|
|
return settings.map((setting) => {
|
|
|
|
return {
|
|
|
|
...setting,
|
|
|
|
// If a setting is not visible, set the value to HIDDEN_SETTING_VALUE so that it is not exposed in the cache
|
|
|
|
// (meaning it also won't be exposed in the theme when rendering)
|
|
|
|
value: setting.visibility && nql(setting.visibility).queryJSON(settingsMap) === false
|
|
|
|
? HIDDEN_SETTING_VALUE
|
|
|
|
: setting.value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2021-09-22 23:56:45 +03:00
|
|
|
};
|