Ghost/core/server/models/base.js

308 lines
9.3 KiB
JavaScript
Raw Normal View History

var Bookshelf = require('bookshelf'),
when = require('when'),
moment = require('moment'),
_ = require('lodash'),
uuid = require('node-uuid'),
config = require('../config'),
unidecode = require('unidecode'),
sanitize = require('validator').sanitize,
schema = require('../data/schema'),
validation = require('../data/validation'),
ghostBookshelf;
// Initializes a new Bookshelf instance, for reference elsewhere in Ghost.
ghostBookshelf = Bookshelf.ghost = Bookshelf.initialize(config().database);
ghostBookshelf.client = config().database.client;
// The Base Model which other Ghost objects will inherit from,
// including some convenience functions as static properties on the model.
ghostBookshelf.Model = ghostBookshelf.Model.extend({
hasTimestamps: true,
// get permitted attributs from schema.js
permittedAttributes: function () {
return _.keys(schema.tables[this.tableName]);
},
defaults: function () {
return {
uuid: uuid.v4()
};
},
initialize: function () {
var self = this,
options = arguments[1] || {};
// make options include available for toJSON()
if (options.include) {
this.include = _.clone(options.include);
}
this.on('creating', this.creating, this);
this.on('saving', function (model, attributes, options) {
return when(self.saving(model, attributes, options)).then(function () {
return self.validate(model, attributes, options);
});
});
},
validate: function () {
return validation.validateSchema(this.tableName, this.toJSON());
},
creating: function (newObj, attr, options) {
if (!this.get('created_by')) {
this.set('created_by', options.user);
}
},
saving: function (newObj, attr, options) {
// Remove any properties which don't belong on the model
this.attributes = this.pick(this.permittedAttributes());
// Store the previous attributes so we can tell what was updated later
this._updatedAttributes = newObj.previousAttributes();
this.set('updated_by', options.user);
},
// Base prototype properties will go here
// Fix problems with dates
fixDates: function (attrs) {
_.each(attrs, function (value, key) {
if (key.substr(-3) === '_at' && value !== null) {
attrs[key] = moment(attrs[key]).toDate();
}
});
return attrs;
},
// Convert integers to real booleans
fixBools: function (attrs) {
var self = this;
_.each(attrs, function (value, key) {
if (schema.tables[self.tableName][key].type === "bool") {
attrs[key] = value ? true : false;
}
});
return attrs;
},
// format date before writing to DB, bools work
format: function (attrs) {
return this.fixDates(attrs);
},
// format data and bool when fetching from DB
parse: function (attrs) {
return this.fixBools(this.fixDates(attrs));
},
toJSON: function (options) {
var attrs = _.extend({}, this.attributes),
self = this;
if (options && options.shallow) {
return attrs;
}
if (options && options.idOnly) {
return attrs.id;
}
_.each(this.relations, function (relation, key) {
if (key.substring(0, 7) !== '_pivot_') {
// if include is set, expand to full object
// toMany relationships are included with ids if not expanded
if (_.contains(self.include, key)) {
attrs[key] = relation.toJSON();
} else if (relation.hasOwnProperty('length')) {
attrs[key] = relation.toJSON({idOnly: true});
}
}
});
return attrs;
},
sanitize: function (attr) {
return sanitize(this.get(attr)).xss();
},
// Get attributes that have been updated (values before a .save() call)
updatedAttributes: function () {
return this._updatedAttributes || {};
},
// Get a specific updated attribute value
updated: function (attr) {
return this.updatedAttributes()[attr];
}
}, {
/**
* Naive find all
* @param {Object} options (optional)
*/
findAll: function (options) {
options = options || {};
return ghostBookshelf.Collection.forge([], {model: this}).fetch(options).then(function (result) {
if (options.include) {
_.each(result.models, function (item) {
item.include = options.include;
});
}
return result;
});
},
browse: function () {
return this.findAll.apply(this, arguments);
},
/**
* Naive find one where args match
* @param {Object} args
* @param {Object} options (optional)
*/
findOne: function (args, options) {
options = options || {};
return this.forge(args, {include: options.include}).fetch(options);
},
read: function () {
return this.findOne.apply(this, arguments);
},
/**
* Naive edit
* @param {Object} editedObj
* @param {Object} options (optional)
*/
edit: function (editedObj, options) {
options = options || {};
return this.forge({id: editedObj.id}).fetch(options).then(function (foundObj) {
if (foundObj) {
return foundObj.save(editedObj, options);
}
});
},
update: function () {
return this.edit.apply(this, arguments);
},
/**
* Naive create
* @param {Object} newObj
* @param {Object} options (optional)
*/
add: function (newObj, options) {
options = options || {};
var instance = this.forge(newObj);
// We allow you to disable timestamps
// when importing posts so that
// the new posts `updated_at` value
// is the same as the import json blob.
// More details refer to https://github.com/TryGhost/Ghost/issues/1696
if (options.importing) {
instance.hasTimestamps = false;
}
return instance.save(null, options);
},
create: function () {
return this.add.apply(this, arguments);
},
/**
* Naive destroy
* @param {Object} _identifier
* @param {Object} options (optional)
*/
destroy: function (_identifier, options) {
options = options || {};
return this.forge({id: _identifier}).destroy(options);
},
'delete': function () {
return this.destroy.apply(this, arguments);
},
// #### generateSlug
// Create a string act as the permalink for an object.
generateSlug: function (Model, base, readOptions) {
var slug,
slugTryCount = 1,
baseName = Model.prototype.tableName.replace(/s$/, ''),
// Look for a post with a matching slug, append an incrementing number if so
checkIfSlugExists;
checkIfSlugExists = function (slugToFind) {
var args = {slug: slugToFind};
//status is needed for posts
if (readOptions && readOptions.status) {
args.status = readOptions.status;
}
return Model.findOne(args, readOptions).then(function (found) {
var trimSpace;
if (!found) {
return when.resolve(slugToFind);
}
slugTryCount += 1;
// If this is the first time through, add the hyphen
if (slugTryCount === 2) {
slugToFind += '-';
} else {
// Otherwise, trim the number off the end
trimSpace = -(String(slugTryCount - 1).length);
slugToFind = slugToFind.slice(0, trimSpace);
}
slugToFind += slugTryCount;
return checkIfSlugExists(slugToFind);
});
};
slug = base.trim();
// Remove non ascii characters
slug = unidecode(slug);
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
slug = slug.replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '')
// Replace dots and spaces with a dash
.replace(/(\s|\.)/g, '-')
// Convert 2 or more dashes into a single dash
.replace(/-+/g, '-')
// Make the whole thing lowercase
.toLowerCase();
// Remove trailing hyphen
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
// Check the filtered slug doesn't match any of the reserved keywords
slug = /^(ghost|ghost\-admin|admin|wp\-admin|wp\-login|dashboard|logout|login|signin|signup|signout|register|archive|archives|category|categories|tag|tags|page|pages|post|posts|public|user|users|rss|feed)$/g
.test(slug) ? slug + '-' + baseName : slug;
//if slug is empty after trimming use "post"
if (!slug) {
slug = baseName;
}
// Test for duplicate slugs.
return checkIfSlugExists(slug);
}
});
module.exports = ghostBookshelf;