From 4bfacf6b8670a279d0bfe58a0ebe5b40f59eda65 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 3 Dec 2015 15:50:52 +0000 Subject: [PATCH] Change server-side labs utility to be synchronous refs #6165 - Use the settings cache to populate config.labs whenever settings change - Use the labs util just to check if a flag isSet synchronously --- core/server/api/settings.js | 25 ++++++++++++---- core/server/helpers/get.js | 20 ++++++------- core/server/helpers/ghost_head.js | 29 +++++++++---------- core/server/middleware/auth.js | 14 ++++----- core/server/utils/labs.js | 24 ++------------- core/test/unit/server_helpers/get_spec.js | 2 +- .../unit/server_helpers/ghost_head_spec.js | 2 +- 7 files changed, 53 insertions(+), 63 deletions(-) diff --git a/core/server/api/settings.js b/core/server/api/settings.js index ad1e21439c..f1f9c697c5 100644 --- a/core/server/api/settings.js +++ b/core/server/api/settings.js @@ -11,7 +11,7 @@ var _ = require('lodash'), docName = 'settings', settings, - updateConfigTheme, + updateConfigCache, updateSettingsCache, settingsFilter, filterPaths, @@ -34,7 +34,21 @@ var _ = require('lodash'), * Maintains the cache of theme specific variables that are reliant on settings. * @private */ -updateConfigTheme = function () { +updateConfigCache = function () { + var errorMessages = [ + 'Error: Invalid JSON in settings.labs', + 'The column with key "labs" could not be parsed as JSON', + 'Please try updating a setting on the labs page, or manually editing your DB' + ], labsValue = {}; + + if (settingsCache.labs && settingsCache.labs.value) { + try { + labsValue = JSON.parse(settingsCache.labs.value); + } catch (e) { + errors.logError.apply(this, errorMessages); + } + } + config.set({ theme: { title: (settingsCache.title && settingsCache.title.value) || '', @@ -42,7 +56,8 @@ updateConfigTheme = function () { logo: (settingsCache.logo && settingsCache.logo.value) || '', cover: (settingsCache.cover && settingsCache.cover.value) || '', navigation: (settingsCache.navigation && JSON.parse(settingsCache.navigation.value)) || [] - } + }, + labs: labsValue }); }; @@ -61,7 +76,7 @@ updateSettingsCache = function (settings) { settingsCache[key] = setting; }); - updateConfigTheme(); + updateConfigCache(); return Promise.resolve(settingsCache); } @@ -70,7 +85,7 @@ updateSettingsCache = function (settings) { .then(function (result) { settingsCache = readSettingsResult(result.models); - updateConfigTheme(); + updateConfigCache(); return settingsCache; }); diff --git a/core/server/helpers/get.js b/core/server/helpers/get.js index 041ea91a87..2787ca4490 100644 --- a/core/server/helpers/get.js +++ b/core/server/helpers/get.js @@ -149,15 +149,13 @@ module.exports = function getWithLabs(context, options) { 'See http://support.ghost.org/public-api-beta' ]; - return labs.isSet('publicAPI').then(function (publicAPI) { - if (publicAPI === true) { - // get helper is active - return get.call(self, context, options); - } else { - errors.logError.apply(this, errorMessages); - return Promise.resolve(function noGetHelper() { - return ''; - }); - } - }); + if (labs.isSet('publicAPI') === true) { + // get helper is active + return get.call(self, context, options); + } else { + errors.logError.apply(this, errorMessages); + return Promise.resolve(function noGetHelper() { + return ''; + }); + } }; diff --git a/core/server/helpers/ghost_head.js b/core/server/helpers/ghost_head.js index 38e857c779..69de00cd39 100644 --- a/core/server/helpers/ghost_head.js +++ b/core/server/helpers/ghost_head.js @@ -23,30 +23,27 @@ var hbs = require('express-hbs'), excerpt = require('./excerpt'), tagsHelper = require('./tags'), imageHelper = require('./image'), - labs = require('../utils/labs'), blog, ghost_head; function getClient() { - return labs.isSet('publicAPI').then(function (publicAPI) { - if (publicAPI === true) { - return api.clients.read({slug: 'ghost-frontend'}).then(function (client) { - client = client.clients[0]; - if (client.status === 'enabled') { - return { - id: client.slug, - secret: client.secret - }; - } + if (labs.isSet('publicAPI') === true) { + return api.clients.read({slug: 'ghost-frontend'}).then(function (client) { + client = client.clients[0]; + if (client.status === 'enabled') { + return { + id: client.slug, + secret: client.secret + }; + } - return {}; - }); - } + return {}; + }); + } - return {}; - }); + return {}; } function writeMetaTag(property, content, type) { diff --git a/core/server/middleware/auth.js b/core/server/middleware/auth.js index 8b112ba06d..dae694cf5b 100644 --- a/core/server/middleware/auth.js +++ b/core/server/middleware/auth.js @@ -158,17 +158,15 @@ auth = { // ### Require user depending on public API being activated. requiresAuthorizedUserPublicAPI: function requiresAuthorizedUserPublicAPI(req, res, next) { - return labs.isSet('publicAPI').then(function (publicAPI) { - if (publicAPI === true) { + if (labs.isSet('publicAPI') === true) { + return next(); + } else { + if (req.user) { return next(); } else { - if (req.user) { - return next(); - } else { - return errors.handleAPIError(new errors.NoPermissionError('Please Sign In'), req, res, next); - } + return errors.handleAPIError(new errors.NoPermissionError('Please Sign In'), req, res, next); } - }); + } } }; diff --git a/core/server/utils/labs.js b/core/server/utils/labs.js index 79b30f37a4..ba3856ddb9 100644 --- a/core/server/utils/labs.js +++ b/core/server/utils/labs.js @@ -1,28 +1,10 @@ -var _ = require('lodash'), - api = require('../api'), +var config = require('../config'), flagIsSet; flagIsSet = function flagIsSet(flag) { - return api.settings.read({key: 'labs', context: {internal: true}}).then(function (response) { - var labs, - labsValue; + var labsConfig = config.labs; - labs = _.find(response.settings, function (setting) { - return setting.key === 'labs'; - }); - - if (!labs || !labs.value) { - return false; - } - - try { - labsValue = JSON.parse(labs.value); - } catch (e) { - return false; - } - - return !!labsValue[flag] && labsValue[flag] === true; - }); + return !!labsConfig[flag] && labsConfig[flag] === true; }; module.exports.isSet = flagIsSet; diff --git a/core/test/unit/server_helpers/get_spec.js b/core/test/unit/server_helpers/get_spec.js index 36ec8ee918..80eed22a79 100644 --- a/core/test/unit/server_helpers/get_spec.js +++ b/core/test/unit/server_helpers/get_spec.js @@ -25,7 +25,7 @@ describe('{{#get}} helper', function () { beforeEach(function () { fn = sandbox.spy(); inverse = sandbox.spy(); - sandbox.stub(labs, 'isSet').returns(new Promise.resolve(true)); + sandbox.stub(labs, 'isSet').returns(true); }); afterEach(function () { diff --git a/core/test/unit/server_helpers/ghost_head_spec.js b/core/test/unit/server_helpers/ghost_head_spec.js index f7fc2a2458..5115bbe97e 100644 --- a/core/test/unit/server_helpers/ghost_head_spec.js +++ b/core/test/unit/server_helpers/ghost_head_spec.js @@ -43,7 +43,7 @@ describe('{{ghost_head}} helper', function () { ] })); - sandbox.stub(labs, 'isSet').returns(new Promise.resolve(true)); + sandbox.stub(labs, 'isSet').returns(true); }); function expectGhostClientMeta(rendered) {