2013-06-25 15:43:15 +04:00
|
|
|
var Settings,
|
2013-09-23 02:20:08 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
2013-09-24 14:46:30 +04:00
|
|
|
uuid = require('node-uuid'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
errors = require('../errorHandling'),
|
|
|
|
when = require('when'),
|
2014-02-19 21:32:23 +04:00
|
|
|
validation = require('../data/validation'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
defaultSettings;
|
|
|
|
|
|
|
|
// For neatness, the defaults file is split into categories.
|
|
|
|
// It's much easier for us to work with it as a single level
|
|
|
|
// instead of iterating those categories every time
|
|
|
|
function parseDefaultSettings() {
|
|
|
|
var defaultSettingsInCategories = require('../data/default-settings.json'),
|
|
|
|
defaultSettingsFlattened = {};
|
|
|
|
|
|
|
|
_.each(defaultSettingsInCategories, function (settings, categoryName) {
|
|
|
|
_.each(settings, function (setting, settingName) {
|
|
|
|
setting.type = categoryName;
|
|
|
|
setting.key = settingName;
|
|
|
|
defaultSettingsFlattened[settingName] = setting;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultSettingsFlattened;
|
|
|
|
}
|
|
|
|
defaultSettings = parseDefaultSettings();
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Each setting is saved as a separate row in the database,
|
|
|
|
// but the overlying API treats them as a single key:value mapping
|
2013-09-23 02:20:08 +04:00
|
|
|
Settings = ghostBookshelf.Model.extend({
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
tableName: 'settings',
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 22:04:41 +04:00
|
|
|
type: 'core'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-08-25 14:49:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
validate: function () {
|
2014-02-19 21:32:23 +04:00
|
|
|
validation.validateSchema(this.tableName, this.toJSON());
|
|
|
|
validation.validateSettings(defaultSettings, this);
|
2013-10-07 21:02:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
saving: function () {
|
2014-01-07 00:17:20 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
|
|
|
// All blog setting keys that need their values to be escaped.
|
|
|
|
// if (this.get('type') === 'blog' && _.contains(['title', 'description', 'email'], this.get('key'))) {
|
|
|
|
// this.set('value', this.sanitize('value'));
|
|
|
|
// }
|
2013-10-07 21:02:57 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.prototype.saving.apply(this, arguments);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-10-07 21:02:57 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, {
|
|
|
|
read: function (_key) {
|
|
|
|
// Allow for just passing the key instead of attributes
|
|
|
|
if (!_.isObject(_key)) {
|
|
|
|
_key = { key: _key };
|
2013-06-15 18:10:30 +04:00
|
|
|
}
|
2014-04-28 03:28:50 +04:00
|
|
|
return when(ghostBookshelf.Model.read.call(this, _key));
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2014-04-03 17:03:09 +04:00
|
|
|
edit: function (_data, options) {
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!Array.isArray(_data)) {
|
|
|
|
_data = [_data];
|
2013-06-08 09:03:55 +04:00
|
|
|
}
|
2014-04-03 17:03:09 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.map(_data, function (item) {
|
|
|
|
// Accept an array of models as input
|
|
|
|
if (item.toJSON) { item = item.toJSON(); }
|
2014-04-28 03:28:50 +04:00
|
|
|
if (!(_.isString(item.key) && item.key.length > 0)) {
|
|
|
|
return when.reject(new Error('Setting key cannot be empty.'));
|
|
|
|
}
|
2014-04-03 17:03:09 +04:00
|
|
|
return Settings.forge({ key: item.key }).fetch(options).then(function (setting) {
|
2013-11-21 00:36:02 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
if (setting) {
|
2014-04-03 17:03:09 +04:00
|
|
|
return setting.save({value: item.value}, options);
|
2013-09-02 05:49:08 +04:00
|
|
|
}
|
2014-04-03 17:03:09 +04:00
|
|
|
|
2014-04-28 03:28:50 +04:00
|
|
|
return when.reject(new Error('Unable to find setting to update: ' + item.key));
|
2013-09-02 05:49:08 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
populateDefaults: function () {
|
|
|
|
return this.findAll().then(function (allSettings) {
|
|
|
|
var usedKeys = allSettings.models.map(function (setting) { return setting.get('key'); }),
|
|
|
|
insertOperations = [];
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
_.each(defaultSettings, function (defaultSetting, defaultSettingKey) {
|
|
|
|
var isMissingFromDB = usedKeys.indexOf(defaultSettingKey) === -1;
|
2013-09-15 15:11:47 +04:00
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
if (defaultSettingKey === 'databaseVersion' && usedKeys.indexOf('currentVersion') !== -1) {
|
|
|
|
isMissingFromDB = false;
|
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
if (isMissingFromDB) {
|
2013-09-15 01:39:31 +04:00
|
|
|
defaultSetting.value = defaultSetting.defaultValue;
|
2014-04-03 17:03:09 +04:00
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save(null, {user: 1}));
|
2013-09-02 05:49:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(insertOperations);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
Settings: Settings
|
2013-09-02 05:49:08 +04:00
|
|
|
};
|