mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
parent
18eda54cf0
commit
3b8f08e0ec
4 changed files with 0 additions and 278 deletions
|
@ -17,7 +17,6 @@ var _ = require('lodash'),
|
|||
settings = require('./settings'),
|
||||
tags = require('./tags'),
|
||||
clients = require('./clients'),
|
||||
themes = require('./themes'),
|
||||
users = require('./users'),
|
||||
slugs = require('./slugs'),
|
||||
subscribers = require('./subscribers'),
|
||||
|
@ -267,7 +266,6 @@ module.exports = {
|
|||
settings: settings,
|
||||
tags: tags,
|
||||
clients: clients,
|
||||
themes: themes,
|
||||
users: users,
|
||||
slugs: slugs,
|
||||
subscribers: subscribers,
|
||||
|
|
|
@ -1,192 +0,0 @@
|
|||
// # Themes API
|
||||
// RESTful API for Themes
|
||||
var Promise = require('bluebird'),
|
||||
_ = require('lodash'),
|
||||
config = require('../config'),
|
||||
errors = require('../errors'),
|
||||
settings = require('./settings'),
|
||||
pipeline = require('../utils/pipeline'),
|
||||
utils = require('./utils'),
|
||||
i18n = require('../i18n'),
|
||||
|
||||
docName = 'themes',
|
||||
themes;
|
||||
|
||||
/**
|
||||
* ### Fetch Active Theme
|
||||
* @returns {Theme} theme
|
||||
*/
|
||||
|
||||
function fetchActiveTheme() {
|
||||
return settings.read({
|
||||
key: 'activeTheme',
|
||||
context: {
|
||||
internal: true
|
||||
}
|
||||
}).then(function (response) {
|
||||
return response.settings[0].value;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ### Fetch Available Themes
|
||||
* @returns {Themes} themes
|
||||
*/
|
||||
|
||||
function fetchAvailableThemes() {
|
||||
var themes = {};
|
||||
|
||||
_.each(config.paths.availableThemes, function (theme, name) {
|
||||
var isTheme = name.indexOf('.') !== 0 && name !== '_messages' && name.toLowerCase() !== 'readme.md';
|
||||
|
||||
if (!isTheme) {
|
||||
return;
|
||||
}
|
||||
|
||||
themes[name] = theme;
|
||||
});
|
||||
|
||||
return themes;
|
||||
}
|
||||
|
||||
/**
|
||||
* ### Activate Theme
|
||||
* @param {Theme} theme
|
||||
* @returns {Object} response
|
||||
*/
|
||||
|
||||
function activateTheme(theme) {
|
||||
return settings.edit({
|
||||
settings: [{
|
||||
key: 'activeTheme',
|
||||
value: theme.name
|
||||
}],
|
||||
context: {
|
||||
internal: true
|
||||
}
|
||||
}).then(function () {
|
||||
theme.active = true;
|
||||
|
||||
return {themes: [theme]};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ## Themes API Methods
|
||||
*
|
||||
* **See:** [API Methods](index.js.html#api%20methods)
|
||||
*/
|
||||
themes = {
|
||||
/**
|
||||
* ### Browse
|
||||
* Get a list of all the available themes
|
||||
* @param {{context}} options
|
||||
* @returns {Promise(Themes)}
|
||||
*/
|
||||
browse: function browse(options) {
|
||||
var tasks;
|
||||
|
||||
/**
|
||||
* ### Model Query
|
||||
* @returns {Object} result
|
||||
*/
|
||||
|
||||
function modelQuery() {
|
||||
var result = {
|
||||
availableThemes: fetchAvailableThemes(),
|
||||
activeTheme: fetchActiveTheme()
|
||||
};
|
||||
|
||||
return Promise.props(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* ### Build response
|
||||
* @param {Object} result - result from modelQuery()
|
||||
* @returns {Object} response
|
||||
*/
|
||||
|
||||
function buildResponse(result) {
|
||||
var themes = [];
|
||||
|
||||
_.each(result.availableThemes, function (theme, name) {
|
||||
var item = {
|
||||
active: result.activeTheme === name,
|
||||
uuid: name
|
||||
};
|
||||
|
||||
// if theme has package.json file,
|
||||
// merge its properties
|
||||
if (theme['package.json']) {
|
||||
item = _.merge(item, theme['package.json']);
|
||||
}
|
||||
|
||||
themes.push(item);
|
||||
});
|
||||
|
||||
return {themes: themes};
|
||||
}
|
||||
|
||||
tasks = [
|
||||
utils.validate(docName),
|
||||
utils.handlePublicPermissions(docName, 'browse'),
|
||||
modelQuery,
|
||||
buildResponse
|
||||
];
|
||||
|
||||
return pipeline(tasks, options || {});
|
||||
},
|
||||
|
||||
/**
|
||||
* ### Edit
|
||||
* Change the active theme
|
||||
* @param {Theme} object
|
||||
* @param {{context}} options
|
||||
* @returns {Promise(Theme)}
|
||||
*/
|
||||
edit: function edit(object, options) {
|
||||
var tasks, themeName;
|
||||
|
||||
// Check whether the request is properly formatted.
|
||||
if (!_.isArray(object.themes)) {
|
||||
return Promise.reject(new errors.BadRequestError(i18n.t('errors.api.themes.invalidRequest')));
|
||||
}
|
||||
|
||||
themeName = object.themes[0].uuid;
|
||||
|
||||
/**
|
||||
* ### Model Query
|
||||
* @param {Object} options
|
||||
* @returns {Theme} theme
|
||||
*/
|
||||
|
||||
function modelQuery(options) {
|
||||
return themes.browse(options).then(function (response) {
|
||||
var theme = _.find(response.themes, function (theme) {
|
||||
return theme.uuid === themeName;
|
||||
});
|
||||
|
||||
if (!theme) {
|
||||
return Promise.reject(new errors.BadRequestError(i18n.t('errors.api.themes.themeDoesNotExist')));
|
||||
}
|
||||
|
||||
if (!theme.name) {
|
||||
theme.name = themeName;
|
||||
}
|
||||
|
||||
return theme;
|
||||
});
|
||||
}
|
||||
|
||||
tasks = [
|
||||
utils.validate(docName),
|
||||
utils.handlePermissions(docName, 'edit'),
|
||||
modelQuery,
|
||||
activateTheme
|
||||
];
|
||||
|
||||
return pipeline(tasks, options || {});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = themes;
|
|
@ -99,10 +99,6 @@ apiRoutes = function apiRoutes(middleware) {
|
|||
// ## Slugs
|
||||
router.get('/slugs/:type/:name', authenticatePrivate, api.http(api.slugs.generate));
|
||||
|
||||
// ## Themes
|
||||
router.get('/themes', authenticatePrivate, api.http(api.themes.browse));
|
||||
router.put('/themes/:name', authenticatePrivate, api.http(api.themes.edit));
|
||||
|
||||
// ## Notifications
|
||||
router.get('/notifications', authenticatePrivate, api.http(api.notifications.browse));
|
||||
router.post('/notifications', authenticatePrivate, api.http(api.notifications.add));
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
var _ = require('lodash'),
|
||||
testUtils = require('../../utils'),
|
||||
rewire = require('rewire'),
|
||||
should = require('should'),
|
||||
sinon = require('sinon'),
|
||||
Promise = require('bluebird'),
|
||||
|
||||
// Stuff we are testing
|
||||
SettingsAPI = require('../../../server/api/settings'),
|
||||
ThemeAPI = rewire('../../../server/api/themes'),
|
||||
|
||||
sandbox = sinon.sandbox.create();
|
||||
|
||||
describe('Themes API', function () {
|
||||
var config,
|
||||
configStub;
|
||||
|
||||
// Keep the DB clean
|
||||
before(testUtils.teardown);
|
||||
afterEach(testUtils.teardown);
|
||||
afterEach(function () {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
beforeEach(testUtils.setup('users:roles', 'perms:theme', 'perms:init'));
|
||||
|
||||
beforeEach(function () {
|
||||
// Override settings.read for activeTheme
|
||||
sandbox.stub(SettingsAPI, 'read', function () {
|
||||
return Promise.resolve({settings: [{value: 'casper'}]});
|
||||
});
|
||||
|
||||
sandbox.stub(SettingsAPI, 'edit', function () {
|
||||
return Promise.resolve({settings: [{value: 'rasper'}]});
|
||||
});
|
||||
|
||||
configStub = {
|
||||
paths: {
|
||||
subdir: '',
|
||||
availableThemes: {
|
||||
casper: {
|
||||
'package.json': {name: 'Casper', version: '0.9.3'}
|
||||
},
|
||||
rasper: {
|
||||
'package.json': {name: 'Rasper', version: '0.9.6'}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
config = ThemeAPI.__get__('config');
|
||||
_.extend(config, configStub);
|
||||
});
|
||||
|
||||
should.exist(ThemeAPI);
|
||||
|
||||
it('can browse', function (done) {
|
||||
ThemeAPI.browse(testUtils.context.owner).then(function (result) {
|
||||
should.exist(result);
|
||||
result.themes.length.should.be.above(0);
|
||||
testUtils.API.checkResponse(result.themes[0], 'theme');
|
||||
done();
|
||||
}).catch(function (error) {
|
||||
done(new Error(JSON.stringify(error)));
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('can edit', function (done) {
|
||||
ThemeAPI.edit({themes: [{uuid: 'rasper', active: true}]}, testUtils.context.owner).then(function (result) {
|
||||
should.exist(result);
|
||||
should.exist(result.themes);
|
||||
result.themes.length.should.be.above(0);
|
||||
testUtils.API.checkResponse(result.themes[0], 'theme');
|
||||
result.themes[0].uuid.should.equal('rasper');
|
||||
done();
|
||||
}).catch(function (error) {
|
||||
done(new Error(JSON.stringify(error)));
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue