mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
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
This commit is contained in:
parent
ea8533842d
commit
4bfacf6b86
7 changed files with 53 additions and 63 deletions
|
@ -11,7 +11,7 @@ var _ = require('lodash'),
|
||||||
docName = 'settings',
|
docName = 'settings',
|
||||||
settings,
|
settings,
|
||||||
|
|
||||||
updateConfigTheme,
|
updateConfigCache,
|
||||||
updateSettingsCache,
|
updateSettingsCache,
|
||||||
settingsFilter,
|
settingsFilter,
|
||||||
filterPaths,
|
filterPaths,
|
||||||
|
@ -34,7 +34,21 @@ var _ = require('lodash'),
|
||||||
* Maintains the cache of theme specific variables that are reliant on settings.
|
* Maintains the cache of theme specific variables that are reliant on settings.
|
||||||
* @private
|
* @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({
|
config.set({
|
||||||
theme: {
|
theme: {
|
||||||
title: (settingsCache.title && settingsCache.title.value) || '',
|
title: (settingsCache.title && settingsCache.title.value) || '',
|
||||||
|
@ -42,7 +56,8 @@ updateConfigTheme = function () {
|
||||||
logo: (settingsCache.logo && settingsCache.logo.value) || '',
|
logo: (settingsCache.logo && settingsCache.logo.value) || '',
|
||||||
cover: (settingsCache.cover && settingsCache.cover.value) || '',
|
cover: (settingsCache.cover && settingsCache.cover.value) || '',
|
||||||
navigation: (settingsCache.navigation && JSON.parse(settingsCache.navigation.value)) || []
|
navigation: (settingsCache.navigation && JSON.parse(settingsCache.navigation.value)) || []
|
||||||
}
|
},
|
||||||
|
labs: labsValue
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,7 +76,7 @@ updateSettingsCache = function (settings) {
|
||||||
settingsCache[key] = setting;
|
settingsCache[key] = setting;
|
||||||
});
|
});
|
||||||
|
|
||||||
updateConfigTheme();
|
updateConfigCache();
|
||||||
|
|
||||||
return Promise.resolve(settingsCache);
|
return Promise.resolve(settingsCache);
|
||||||
}
|
}
|
||||||
|
@ -70,7 +85,7 @@ updateSettingsCache = function (settings) {
|
||||||
.then(function (result) {
|
.then(function (result) {
|
||||||
settingsCache = readSettingsResult(result.models);
|
settingsCache = readSettingsResult(result.models);
|
||||||
|
|
||||||
updateConfigTheme();
|
updateConfigCache();
|
||||||
|
|
||||||
return settingsCache;
|
return settingsCache;
|
||||||
});
|
});
|
||||||
|
|
|
@ -149,15 +149,13 @@ module.exports = function getWithLabs(context, options) {
|
||||||
'See http://support.ghost.org/public-api-beta'
|
'See http://support.ghost.org/public-api-beta'
|
||||||
];
|
];
|
||||||
|
|
||||||
return labs.isSet('publicAPI').then(function (publicAPI) {
|
if (labs.isSet('publicAPI') === true) {
|
||||||
if (publicAPI === true) {
|
// get helper is active
|
||||||
// get helper is active
|
return get.call(self, context, options);
|
||||||
return get.call(self, context, options);
|
} else {
|
||||||
} else {
|
errors.logError.apply(this, errorMessages);
|
||||||
errors.logError.apply(this, errorMessages);
|
return Promise.resolve(function noGetHelper() {
|
||||||
return Promise.resolve(function noGetHelper() {
|
return '<script>console.error("' + errorMessages.join(' ') + '");</script>';
|
||||||
return '<script>console.error("' + errorMessages.join(' ') + '");</script>';
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,30 +23,27 @@ var hbs = require('express-hbs'),
|
||||||
excerpt = require('./excerpt'),
|
excerpt = require('./excerpt'),
|
||||||
tagsHelper = require('./tags'),
|
tagsHelper = require('./tags'),
|
||||||
imageHelper = require('./image'),
|
imageHelper = require('./image'),
|
||||||
|
|
||||||
labs = require('../utils/labs'),
|
labs = require('../utils/labs'),
|
||||||
|
|
||||||
blog,
|
blog,
|
||||||
ghost_head;
|
ghost_head;
|
||||||
|
|
||||||
function getClient() {
|
function getClient() {
|
||||||
return labs.isSet('publicAPI').then(function (publicAPI) {
|
if (labs.isSet('publicAPI') === true) {
|
||||||
if (publicAPI === true) {
|
return api.clients.read({slug: 'ghost-frontend'}).then(function (client) {
|
||||||
return api.clients.read({slug: 'ghost-frontend'}).then(function (client) {
|
client = client.clients[0];
|
||||||
client = client.clients[0];
|
if (client.status === 'enabled') {
|
||||||
if (client.status === 'enabled') {
|
return {
|
||||||
return {
|
id: client.slug,
|
||||||
id: client.slug,
|
secret: client.secret
|
||||||
secret: client.secret
|
};
|
||||||
};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeMetaTag(property, content, type) {
|
function writeMetaTag(property, content, type) {
|
||||||
|
|
|
@ -158,17 +158,15 @@ auth = {
|
||||||
|
|
||||||
// ### Require user depending on public API being activated.
|
// ### Require user depending on public API being activated.
|
||||||
requiresAuthorizedUserPublicAPI: function requiresAuthorizedUserPublicAPI(req, res, next) {
|
requiresAuthorizedUserPublicAPI: function requiresAuthorizedUserPublicAPI(req, res, next) {
|
||||||
return labs.isSet('publicAPI').then(function (publicAPI) {
|
if (labs.isSet('publicAPI') === true) {
|
||||||
if (publicAPI === true) {
|
return next();
|
||||||
|
} else {
|
||||||
|
if (req.user) {
|
||||||
return next();
|
return next();
|
||||||
} else {
|
} else {
|
||||||
if (req.user) {
|
return errors.handleAPIError(new errors.NoPermissionError('Please Sign In'), req, res, next);
|
||||||
return next();
|
|
||||||
} else {
|
|
||||||
return errors.handleAPIError(new errors.NoPermissionError('Please Sign In'), req, res, next);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,10 @@
|
||||||
var _ = require('lodash'),
|
var config = require('../config'),
|
||||||
api = require('../api'),
|
|
||||||
flagIsSet;
|
flagIsSet;
|
||||||
|
|
||||||
flagIsSet = function flagIsSet(flag) {
|
flagIsSet = function flagIsSet(flag) {
|
||||||
return api.settings.read({key: 'labs', context: {internal: true}}).then(function (response) {
|
var labsConfig = config.labs;
|
||||||
var labs,
|
|
||||||
labsValue;
|
|
||||||
|
|
||||||
labs = _.find(response.settings, function (setting) {
|
return !!labsConfig[flag] && labsConfig[flag] === true;
|
||||||
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;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.isSet = flagIsSet;
|
module.exports.isSet = flagIsSet;
|
||||||
|
|
|
@ -25,7 +25,7 @@ describe('{{#get}} helper', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
fn = sandbox.spy();
|
fn = sandbox.spy();
|
||||||
inverse = sandbox.spy();
|
inverse = sandbox.spy();
|
||||||
sandbox.stub(labs, 'isSet').returns(new Promise.resolve(true));
|
sandbox.stub(labs, 'isSet').returns(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
|
|
|
@ -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) {
|
function expectGhostClientMeta(rendered) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue