2018-11-09 14:42:21 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
2021-04-19 17:09:35 +03:00
|
|
|
const SafeString = require('../../frontend/services/theme-engine/engine').SafeString;
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../shared/i18n');
|
2020-05-28 21:30:23 +03:00
|
|
|
const logging = require('../../shared/logging');
|
2021-04-19 20:28:51 +03:00
|
|
|
const settingsCache = require('../services/settings/cache');
|
2019-11-06 10:42:39 +03:00
|
|
|
|
2021-06-04 18:56:16 +03:00
|
|
|
// NOTE: this allowlist is meant to be used to filter out any unexpected
|
|
|
|
// input for the "labs" setting value
|
|
|
|
const WRITABLE_KEYS_ALLOWLIST = [
|
2021-06-07 22:22:06 +03:00
|
|
|
'activitypub',
|
2021-06-09 13:08:47 +03:00
|
|
|
'matchHelper',
|
|
|
|
'multipleProducts'
|
2021-06-04 18:56:16 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
module.exports.WRITABLE_KEYS_ALLOWLIST = WRITABLE_KEYS_ALLOWLIST;
|
|
|
|
|
2021-06-07 19:51:37 +03:00
|
|
|
module.exports.getAll = () => {
|
|
|
|
const labs = _.cloneDeep(settingsCache.get('labs')) || {};
|
|
|
|
|
|
|
|
labs.members = settingsCache.get('members_signup_access') !== 'none';
|
|
|
|
|
|
|
|
return labs;
|
|
|
|
};
|
2015-11-03 22:17:24 +03:00
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
module.exports.isSet = function isSet(flag) {
|
|
|
|
const labsConfig = module.exports.getAll();
|
2019-11-06 10:42:39 +03:00
|
|
|
|
|
|
|
return !!(labsConfig && labsConfig[flag] && labsConfig[flag] === true);
|
2019-02-24 20:37:11 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
module.exports.enabledHelper = function enabledHelper(options, callback) {
|
2019-02-04 20:58:35 +03:00
|
|
|
const errDetails = {};
|
|
|
|
let errString;
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
if (module.exports.isSet(options.flagKey) === true) {
|
2017-03-23 22:00:58 +03:00
|
|
|
// helper is active, use the callback
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else, the helper is not active and we need to handle this as an error
|
2020-04-30 22:26:12 +03:00
|
|
|
errDetails.message = i18n.t(options.errMessagePath || 'warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
|
|
errDetails.context = i18n.t(options.errContextPath || 'warnings.helpers.flagMustBeEnabled', {
|
2019-02-04 20:58:35 +03:00
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
});
|
2020-04-30 22:26:12 +03:00
|
|
|
errDetails.help = i18n.t(options.errHelpPath || 'warnings.helpers.seeLink', {url: options.helpUrl});
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(new errors.DisabledFeatureError(errDetails));
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2019-07-15 09:18:58 +03:00
|
|
|
errString = new SafeString(`<script>console.error("${_.values(errDetails).join(' ')}");</script>`);
|
2017-03-23 22:00:58 +03:00
|
|
|
|
|
|
|
if (options.async) {
|
2019-07-15 09:18:58 +03:00
|
|
|
return Promise.resolve(errString);
|
2017-03-23 22:00:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return errString;
|
|
|
|
};
|