Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// # Settings API
|
2014-06-03 17:05:25 +04:00
|
|
|
// RESTful API for the Setting resource
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-12-06 12:51:35 +04:00
|
|
|
dataProvider = require('../models'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2013-12-06 12:51:35 +04:00
|
|
|
config = require('../config'),
|
2014-05-07 04:49:25 +04:00
|
|
|
canThis = require('../permissions').canThis,
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
utils = require('./utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
docName = 'settings',
|
2013-12-06 12:51:35 +04:00
|
|
|
settings,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2015-12-03 18:50:52 +03:00
|
|
|
updateConfigCache,
|
2013-12-06 12:51:35 +04:00
|
|
|
updateSettingsCache,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
settingsFilter,
|
2014-02-23 16:32:35 +04:00
|
|
|
filterPaths,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
readSettingsResult,
|
2014-04-28 03:28:50 +04:00
|
|
|
settingsResult,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
canEditAllSettings,
|
2014-06-17 19:36:47 +04:00
|
|
|
populateDefaultSetting,
|
|
|
|
hasPopulatedDefaults = false,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Cache
|
|
|
|
* Holds cached settings
|
|
|
|
* @private
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
settingsCache = {};
|
|
|
|
|
2014-09-03 07:15:15 +04:00
|
|
|
/**
|
|
|
|
* ### Updates Config Theme Settings
|
|
|
|
* Maintains the cache of theme specific variables that are reliant on settings.
|
|
|
|
* @private
|
|
|
|
*/
|
2015-12-03 18:50:52 +03:00
|
|
|
updateConfigCache = function () {
|
|
|
|
var errorMessages = [
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('errors.api.settings.invalidJsonInLabs'),
|
|
|
|
i18n.t('errors.api.settings.labsColumnCouldNotBeParsed'),
|
|
|
|
i18n.t('errors.api.settings.tryUpdatingLabs')
|
2015-12-03 18:50:52 +03:00
|
|
|
], labsValue = {};
|
|
|
|
|
|
|
|
if (settingsCache.labs && settingsCache.labs.value) {
|
|
|
|
try {
|
|
|
|
labsValue = JSON.parse(settingsCache.labs.value);
|
|
|
|
} catch (e) {
|
|
|
|
errors.logError.apply(this, errorMessages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 07:15:15 +04:00
|
|
|
config.set({
|
|
|
|
theme: {
|
2014-11-20 17:50:01 +03:00
|
|
|
title: (settingsCache.title && settingsCache.title.value) || '',
|
|
|
|
description: (settingsCache.description && settingsCache.description.value) || '',
|
|
|
|
logo: (settingsCache.logo && settingsCache.logo.value) || '',
|
2015-02-28 15:53:00 +03:00
|
|
|
cover: (settingsCache.cover && settingsCache.cover.value) || '',
|
2015-12-02 13:06:44 +03:00
|
|
|
navigation: (settingsCache.navigation && JSON.parse(settingsCache.navigation.value)) || [],
|
|
|
|
postsPerPage: (settingsCache.postsPerPage && settingsCache.postsPerPage.value) || 5,
|
|
|
|
permalinks: (settingsCache.permalinks && settingsCache.permalinks.value) || '/:slug/'
|
2015-12-03 18:50:52 +03:00
|
|
|
},
|
|
|
|
labs: labsValue
|
2014-09-03 07:15:15 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Update Settings Cache
|
|
|
|
* Maintain the internal cache of the settings object
|
|
|
|
* @public
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settings
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {Settings}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
updateSettingsCache = function (settings) {
|
|
|
|
settings = settings || {};
|
|
|
|
|
|
|
|
if (!_.isEmpty(settings)) {
|
|
|
|
_.map(settings, function (setting, key) {
|
2014-04-28 03:28:50 +04:00
|
|
|
settingsCache[key] = setting;
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2015-12-03 18:50:52 +03:00
|
|
|
updateConfigCache();
|
2014-09-03 07:15:15 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(settingsCache);
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 03:28:50 +04:00
|
|
|
return dataProvider.Settings.findAll()
|
|
|
|
.then(function (result) {
|
|
|
|
settingsCache = readSettingsResult(result.models);
|
|
|
|
|
2015-12-03 18:50:52 +03:00
|
|
|
updateConfigCache();
|
2014-09-03 07:15:15 +04:00
|
|
|
|
2014-04-28 03:28:50 +04:00
|
|
|
return settingsCache;
|
2014-02-23 16:32:35 +04:00
|
|
|
});
|
2014-04-28 03:28:50 +04:00
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// ## Helpers
|
2014-04-28 03:28:50 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Settings Filter
|
|
|
|
* Filters an object based on a given filter object
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settings
|
|
|
|
* @param {String} filter
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
settingsFilter = function (settings, filter) {
|
|
|
|
return _.object(_.filter(_.pairs(settings), function (setting) {
|
|
|
|
if (filter) {
|
|
|
|
return _.some(filter.split(','), function (f) {
|
|
|
|
return setting[1].type === f;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}));
|
2014-02-23 16:32:35 +04:00
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Filter Paths
|
|
|
|
* Normalizes paths read by require-tree so that the apps and themes modules can use them. Creates an empty
|
|
|
|
* array (res), and populates it with useful info about the read packages like name, whether they're active
|
|
|
|
* (comparison with the second argument), and if they have a package.json, that, otherwise false
|
|
|
|
* @private
|
|
|
|
* @param {object} paths as returned by require-tree()
|
|
|
|
* @param {array/string} active as read from the settings object
|
|
|
|
* @returns {Array} of objects with useful info about apps / themes
|
|
|
|
*/
|
2014-02-23 16:32:35 +04:00
|
|
|
filterPaths = function (paths, active) {
|
|
|
|
var pathKeys = Object.keys(paths),
|
|
|
|
res = [],
|
|
|
|
item;
|
|
|
|
|
|
|
|
// turn active into an array (so themes and apps can be checked the same)
|
|
|
|
if (!Array.isArray(active)) {
|
|
|
|
active = [active];
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(pathKeys, function (key) {
|
2014-09-10 08:06:24 +04:00
|
|
|
// do not include hidden files or _messages
|
2014-04-28 03:28:50 +04:00
|
|
|
if (key.indexOf('.') !== 0 &&
|
|
|
|
key !== '_messages' &&
|
|
|
|
key !== 'README.md'
|
2014-02-23 16:32:35 +04:00
|
|
|
) {
|
|
|
|
item = {
|
|
|
|
name: key
|
|
|
|
};
|
|
|
|
if (paths[key].hasOwnProperty('package.json')) {
|
|
|
|
item.package = paths[key]['package.json'];
|
|
|
|
} else {
|
|
|
|
item.package = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.indexOf(active, key) !== -1) {
|
|
|
|
item.active = true;
|
|
|
|
}
|
|
|
|
res.push(item);
|
|
|
|
}
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
2014-02-23 16:32:35 +04:00
|
|
|
return res;
|
2013-12-06 12:51:35 +04:00
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Read Settings Result
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Array} settingsModels
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {Settings}
|
|
|
|
*/
|
|
|
|
readSettingsResult = function (settingsModels) {
|
|
|
|
var settings = _.reduce(settingsModels, function (memo, member) {
|
|
|
|
if (!memo.hasOwnProperty(member.attributes.key)) {
|
|
|
|
memo[member.attributes.key] = member.attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return memo;
|
|
|
|
}, {}),
|
2014-07-17 18:33:21 +04:00
|
|
|
themes = config.paths.availableThemes,
|
|
|
|
apps = config.paths.availableApps,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
res;
|
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
if (settings.activeTheme && themes) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
res = filterPaths(themes, settings.activeTheme.value);
|
|
|
|
|
|
|
|
settings.availableThemes = {
|
|
|
|
key: 'availableThemes',
|
|
|
|
value: res,
|
|
|
|
type: 'theme'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-07-17 18:33:21 +04:00
|
|
|
if (settings.activeApps && apps) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
res = filterPaths(apps, JSON.parse(settings.activeApps.value));
|
|
|
|
|
|
|
|
settings.availableApps = {
|
|
|
|
key: 'availableApps',
|
|
|
|
value: res,
|
|
|
|
type: 'app'
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Settings Result
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settings
|
|
|
|
* @param {String} type
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {{settings: *}}
|
|
|
|
*/
|
2014-04-28 03:28:50 +04:00
|
|
|
settingsResult = function (settings, type) {
|
|
|
|
var filteredSettings = _.values(settingsFilter(settings, type)),
|
|
|
|
result = {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
settings: filteredSettings,
|
|
|
|
meta: {}
|
2014-04-28 03:28:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
result.meta.filters = {
|
|
|
|
type: type
|
2014-04-28 03:28:50 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
/**
|
|
|
|
* ### Populate Default Setting
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {String} key
|
2014-06-17 19:36:47 +04:00
|
|
|
* @returns Promise(Setting)
|
|
|
|
*/
|
|
|
|
populateDefaultSetting = function (key) {
|
|
|
|
// Call populateDefault and update the settings cache
|
|
|
|
return dataProvider.Settings.populateDefault(key).then(function (defaultSetting) {
|
|
|
|
// Process the default result and add to settings cache
|
2014-07-28 01:04:58 +04:00
|
|
|
var readResult = readSettingsResult([defaultSetting]);
|
2014-06-17 19:36:47 +04:00
|
|
|
|
|
|
|
// Add to the settings cache
|
|
|
|
return updateSettingsCache(readResult).then(function () {
|
|
|
|
// Get the result from the cache with permission checks
|
|
|
|
});
|
2014-08-17 10:17:23 +04:00
|
|
|
}).catch(function (err) {
|
2014-06-17 19:36:47 +04:00
|
|
|
// Pass along NotFoundError
|
|
|
|
if (typeof err === errors.NotFoundError) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(err);
|
2014-06-17 19:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Different kind of error?
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError(i18n.t('errors.api.settings.problemFindingSetting', {key: key})));
|
2014-06-17 19:36:47 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Can Edit All Settings
|
|
|
|
* Check that this edit request is allowed for all settings requested to be updated
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settingsInfo
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
canEditAllSettings = function (settingsInfo, options) {
|
2014-06-17 19:36:47 +04:00
|
|
|
var checkSettingPermissions = function (setting) {
|
|
|
|
if (setting.type === 'core' && !(options.context && options.context.internal)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(
|
2015-11-12 15:29:45 +03:00
|
|
|
new errors.NoPermissionError(i18n.t('errors.api.settings.accessCoreSettingFromExtReq'))
|
2014-06-17 19:36:47 +04:00
|
|
|
);
|
|
|
|
}
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-07-30 05:25:53 +04:00
|
|
|
return canThis(options.context).edit.setting(setting.key).catch(function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError(i18n.t('errors.api.settings.noPermissionToEditSettings')));
|
2014-07-30 05:25:53 +04:00
|
|
|
});
|
2014-06-17 19:36:47 +04:00
|
|
|
},
|
|
|
|
checks = _.map(settingsInfo, function (settingInfo) {
|
|
|
|
var setting = settingsCache[settingInfo.key];
|
|
|
|
|
|
|
|
if (!setting) {
|
|
|
|
// Try to populate a default setting if not in the cache
|
|
|
|
return populateDefaultSetting(settingInfo.key).then(function (defaultSetting) {
|
|
|
|
// Get the result from the cache with permission checks
|
|
|
|
return checkSettingPermissions(defaultSetting);
|
|
|
|
});
|
|
|
|
}
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
return checkSettingPermissions(setting);
|
|
|
|
});
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(checks);
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
};
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Settings API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
settings = {
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Browse
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
browse: function browse(options) {
|
2014-06-17 19:36:47 +04:00
|
|
|
// First, check if we have populated the settings from default-settings yet
|
|
|
|
if (!hasPopulatedDefaults) {
|
|
|
|
return dataProvider.Settings.populateDefaults().then(function () {
|
|
|
|
hasPopulatedDefaults = true;
|
|
|
|
return settings.browse(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var result = settingsResult(settingsCache, options.type);
|
2014-05-07 04:49:25 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// If there is no context, return only blog settings
|
|
|
|
if (!options.context) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(_.filter(result.settings, function (setting) { return setting.type === 'blog'; }));
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2014-05-07 04:49:25 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Otherwise return whatever this context is allowed to browse
|
|
|
|
return canThis(options.context).browse.setting().then(function () {
|
2014-05-07 04:49:25 +04:00
|
|
|
// Omit core settings unless internal request
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (!options.context.internal) {
|
2014-05-07 04:49:25 +04:00
|
|
|
result.settings = _.filter(result.settings, function (setting) { return setting.type !== 'core'; });
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Read
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
read: function read(options) {
|
|
|
|
if (_.isString(options)) {
|
2014-09-10 08:06:24 +04:00
|
|
|
options = {key: options};
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
var getSettingsResult = function () {
|
|
|
|
var setting = settingsCache[options.key],
|
|
|
|
result = {};
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
result[options.key] = setting;
|
2014-05-07 04:49:25 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
if (setting.type === 'core' && !(options.context && options.context.internal)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(
|
2015-11-12 15:29:45 +03:00
|
|
|
new errors.NoPermissionError(i18n.t('errors.api.settings.accessCoreSettingFromExtReq'))
|
2014-06-17 19:36:47 +04:00
|
|
|
);
|
|
|
|
}
|
2014-05-07 04:49:25 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
if (setting.type === 'blog') {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(settingsResult(result));
|
2014-06-17 19:36:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return canThis(options.context).read.setting(options.key).then(function () {
|
|
|
|
return settingsResult(result);
|
|
|
|
}, function () {
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError(i18n.t('errors.api.settings.noPermissionToReadSettings')));
|
2014-06-17 19:36:47 +04:00
|
|
|
});
|
|
|
|
};
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
// If the setting is not already in the cache
|
|
|
|
if (!settingsCache[options.key]) {
|
|
|
|
// Try to populate the setting from default-settings file
|
|
|
|
return populateDefaultSetting(options.key).then(function () {
|
|
|
|
// Get the result from the cache with permission checks
|
|
|
|
return getSettingsResult();
|
|
|
|
});
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
// Get the result from the cache with permission checks
|
|
|
|
return getSettingsResult();
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Edit
|
2015-12-02 10:28:36 +03:00
|
|
|
* Update properties of a setting
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @param {{settings: }} object Setting or a single string name
|
|
|
|
* @param {{id (required), include,...}} options (optional) or a single string value
|
|
|
|
* @return {Promise(Setting)} Edited Setting
|
|
|
|
*/
|
|
|
|
edit: function edit(object, options) {
|
|
|
|
options = options || {};
|
2014-04-03 17:03:09 +04:00
|
|
|
var self = this,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
type;
|
2014-04-03 17:03:09 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Allow shorthand syntax where a single key and value are passed to edit instead of object and options
|
|
|
|
if (_.isString(object)) {
|
2014-09-10 08:06:24 +04:00
|
|
|
object = {settings: [{key: object, value: options}]};
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
// clean data
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
_.each(object.settings, function (setting) {
|
|
|
|
if (!_.isString(setting.value)) {
|
|
|
|
setting.value = JSON.stringify(setting.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
type = _.find(object.settings, function (setting) { return setting.key === 'type'; });
|
2014-05-06 22:52:11 +04:00
|
|
|
if (_.isObject(type)) {
|
|
|
|
type = type.value;
|
|
|
|
}
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
object.settings = _.reject(object.settings, function (setting) {
|
2014-05-06 22:52:11 +04:00
|
|
|
return setting.key === 'type' || setting.key === 'availableThemes' || setting.key === 'availableApps';
|
|
|
|
});
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return canEditAllSettings(object.settings, options).then(function () {
|
|
|
|
return utils.checkObject(object, docName).then(function (checkedData) {
|
|
|
|
options.user = self.user;
|
|
|
|
return dataProvider.Settings.edit(checkedData.settings, options);
|
|
|
|
}).then(function (result) {
|
|
|
|
var readResult = readSettingsResult(result);
|
|
|
|
|
|
|
|
return updateSettingsCache(readResult).then(function () {
|
|
|
|
return settingsResult(readResult, type);
|
|
|
|
});
|
2014-05-06 22:52:11 +04:00
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = settings;
|
2014-05-13 08:20:57 +04:00
|
|
|
module.exports.updateSettingsCache = updateSettingsCache;
|