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'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-02-19 21:32:23 +04:00
|
|
|
validation = require('../data/validation'),
|
2015-06-15 11:36:01 +03:00
|
|
|
events = require('../events'),
|
2014-07-15 15:03:12 +04:00
|
|
|
internal = {context: {internal: true}},
|
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 = {};
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
_.each(defaultSettingsInCategories, function each(settings, categoryName) {
|
|
|
|
_.each(settings, function each(setting, settingName) {
|
2013-09-02 05:49:08 +04:00
|
|
|
setting.type = categoryName;
|
|
|
|
setting.key = settingName;
|
2014-07-28 01:04:58 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
defaultSettingsFlattened[settingName] = setting;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultSettingsFlattened;
|
|
|
|
}
|
2014-06-17 19:36:47 +04:00
|
|
|
|
|
|
|
function getDefaultSettings() {
|
|
|
|
if (!defaultSettings) {
|
|
|
|
defaultSettings = parseDefaultSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultSettings;
|
|
|
|
}
|
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
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
defaults: function defaults() {
|
2013-06-25 15:43:15 +04:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2015-06-15 11:36:01 +03:00
|
|
|
emitChange: function emitChange(event) {
|
|
|
|
events.emit('settings' + '.' + event, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function initialize() {
|
|
|
|
ghostBookshelf.Model.prototype.initialize.apply(this, arguments);
|
|
|
|
|
|
|
|
this.on('created', function (model) {
|
|
|
|
model.emitChange('added');
|
2015-06-23 11:31:52 +03:00
|
|
|
model.emitChange(model.attributes.key + '.' + 'added');
|
2015-06-15 11:36:01 +03:00
|
|
|
});
|
|
|
|
this.on('updated', function (model) {
|
|
|
|
model.emitChange('edited');
|
2015-06-23 11:31:52 +03:00
|
|
|
model.emitChange(model.attributes.key + '.' + 'edited');
|
2015-06-15 11:36:01 +03:00
|
|
|
});
|
|
|
|
this.on('destroyed', function (model) {
|
|
|
|
model.emitChange('deleted');
|
2015-06-23 11:31:52 +03:00
|
|
|
model.emitChange(model.attributes.key + '.' + 'deleted');
|
2015-06-15 11:36:01 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
validate: function validate() {
|
2014-07-10 02:11:04 +04:00
|
|
|
var self = this,
|
|
|
|
setting = this.toJSON();
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return validation.validateSchema(self.tableName, setting).then(function then() {
|
2014-06-17 19:36:47 +04:00
|
|
|
return validation.validateSettings(getDefaultSettings(), self);
|
2014-07-10 02:11:04 +04:00
|
|
|
}).then(function () {
|
|
|
|
var themeName = setting.value || '';
|
|
|
|
|
|
|
|
if (setting.key !== 'activeTheme') {
|
2014-08-17 10:17:23 +04:00
|
|
|
return;
|
2014-07-10 02:11:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return validation.validateActiveTheme(themeName);
|
2014-05-05 17:51:21 +04:00
|
|
|
});
|
2013-10-07 21:02:57 +04:00
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
saving: function saving() {
|
2015-05-01 00:14:19 +03: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
|
|
|
}, {
|
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
|
|
|
findOne: function (options) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// Allow for just passing the key instead of attributes
|
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 (!_.isObject(options)) {
|
2014-09-10 08:06:24 +04:00
|
|
|
options = {key: options};
|
2013-06-15 18:10:30 +04:00
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(ghostBookshelf.Model.findOne.call(this, options));
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-08 09:03:55 +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: function (data, options) {
|
2014-05-06 05:45:08 +04:00
|
|
|
var self = this;
|
|
|
|
options = this.filterOptions(options, 'edit');
|
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
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
data = [data];
|
2013-06-08 09:03:55 +04:00
|
|
|
}
|
2014-04-03 17:03:09 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.map(data, function (item) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// 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)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.ValidationError('Value in [settings.key] cannot be blank.'));
|
2014-04-28 03:28:50 +04:00
|
|
|
}
|
2014-05-06 05:45:08 +04:00
|
|
|
|
|
|
|
item = self.filterData(item);
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return Settings.forge({key: item.key}).fetch(options).then(function then(setting) {
|
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-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError('Unable to find setting to update: ' + item.key));
|
2013-06-25 15:43:15 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
},
|
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
populateDefault: function (key) {
|
|
|
|
if (!getDefaultSettings()[key]) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(new errors.NotFoundError('Unable to find default setting: ' + key));
|
2014-06-17 19:36:47 +04:00
|
|
|
}
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
return this.findOne({key: key}).then(function then(foundSetting) {
|
2014-06-17 19:36:47 +04:00
|
|
|
if (foundSetting) {
|
|
|
|
return foundSetting;
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultSetting = _.clone(getDefaultSettings()[key]);
|
|
|
|
defaultSetting.value = defaultSetting.defaultValue;
|
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
return Settings.forge(defaultSetting).save(null, internal);
|
2014-06-17 19:36:47 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
populateDefaults: function populateDefaults() {
|
|
|
|
return this.findAll().then(function then(allSettings) {
|
|
|
|
var usedKeys = allSettings.models.map(function mapper(setting) { return setting.get('key'); }),
|
2013-09-02 05:49:08 +04:00
|
|
|
insertOperations = [];
|
|
|
|
|
2015-06-14 18:58:49 +03:00
|
|
|
_.each(getDefaultSettings(), function each(defaultSetting, defaultSettingKey) {
|
2013-09-02 05:49:08 +04:00
|
|
|
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-07-15 15:03:12 +04:00
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save(null, internal));
|
2013-09-02 05:49:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(insertOperations);
|
2013-09-02 05:49:08 +04:00
|
|
|
});
|
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 = {
|
2014-07-13 15:17:18 +04:00
|
|
|
Settings: ghostBookshelf.model('Settings', Settings)
|
2013-09-02 05:49:08 +04:00
|
|
|
};
|