2014-02-19 17:57:26 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
when = require('when'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
Showdown = require('showdown'),
|
2014-03-20 17:52:16 +04:00
|
|
|
ghostgfm = require('../../shared/lib/showdown/extensions/ghostgfm'),
|
|
|
|
converter = new Showdown.converter({extensions: [ghostgfm]}),
|
2014-02-19 17:57:26 +04:00
|
|
|
User = require('./user').User,
|
|
|
|
Tag = require('./tag').Tag,
|
|
|
|
Tags = require('./tag').Tags,
|
|
|
|
ghostBookshelf = require('./base'),
|
2014-02-19 21:32:23 +04:00
|
|
|
validation = require('../data/validation'),
|
2014-03-15 02:10:50 +04:00
|
|
|
xmlrpc = require('../xmlrpc'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
|
|
|
Post,
|
2014-02-22 01:54:56 +04:00
|
|
|
Posts;
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Post = ghostBookshelf.Model.extend({
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
tableName: 'posts',
|
|
|
|
|
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 23:01:46 +04:00
|
|
|
status: 'draft'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
2014-02-19 17:57:26 +04:00
|
|
|
var self = this;
|
2013-06-25 15:43:15 +04:00
|
|
|
this.on('creating', this.creating, this);
|
2014-03-15 02:10:50 +04:00
|
|
|
this.on('saved', function (model, attributes, options) {
|
|
|
|
if (model.get('status') === 'published') {
|
|
|
|
xmlrpc.ping(model.attributes);
|
|
|
|
}
|
|
|
|
return self.updateTags(model, attributes, options);
|
|
|
|
});
|
2014-02-19 17:57:26 +04:00
|
|
|
this.on('saving', function (model, attributes, options) {
|
|
|
|
return when(self.saving(model, attributes, options)).then(function () {
|
|
|
|
return self.validate(model, attributes, options);
|
|
|
|
});
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
2013-08-20 22:52:44 +04:00
|
|
|
validate: function () {
|
2014-02-19 21:32:23 +04:00
|
|
|
validation.validateSchema(this.tableName, this.toJSON());
|
2013-08-20 22:52:44 +04:00
|
|
|
},
|
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
saving: function (newPage, attr, options) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2013-09-13 21:29:08 +04:00
|
|
|
var self = this;
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
// keep tags for 'saved' event
|
2014-02-22 01:54:56 +04:00
|
|
|
this.myTags = this.get('tags');
|
2014-02-19 17:57:26 +04:00
|
|
|
|
|
|
|
ghostBookshelf.Model.prototype.saving.call(this);
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-09-14 23:01:46 +04:00
|
|
|
this.set('html', converter.makeHtml(this.get('markdown')));
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-01-07 00:17:20 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
|
|
|
//this.set('title', this.sanitize('title').trim());
|
|
|
|
this.set('title', this.get('title').trim());
|
2013-08-30 08:18:55 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
if ((this.hasChanged('status') || !this.get('published_at')) && this.get('status') === 'published') {
|
2013-09-17 14:39:27 +04:00
|
|
|
if (!this.get('published_at')) {
|
|
|
|
this.set('published_at', new Date());
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
// This will need to go elsewhere in the API layer.
|
|
|
|
this.set('published_by', 1);
|
|
|
|
}
|
2013-06-15 20:11:15 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
if (this.hasChanged('slug') || !this.get('slug')) {
|
2013-09-14 23:01:46 +04:00
|
|
|
// Pass the new slug through the generator to strip illegal characters, detect duplicates
|
2014-02-19 17:57:26 +04:00
|
|
|
return ghostBookshelf.Model.generateSlug(Post, this.get('slug') || this.get('title'),
|
|
|
|
{status: 'all', transacting: options.transacting})
|
2013-09-14 23:01:46 +04:00
|
|
|
.then(function (slug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
creating: function (newPage, attr, options) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2013-06-18 02:59:08 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
// set any dynamic default properties
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!this.get('author_id')) {
|
|
|
|
this.set('author_id', 1);
|
|
|
|
}
|
2013-06-26 05:42:51 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
ghostBookshelf.Model.prototype.creating.call(this);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
updateTags: function (newPost, attr, options) {
|
2014-02-27 06:44:09 +04:00
|
|
|
/*jshint unused:false*/
|
2014-02-22 01:54:56 +04:00
|
|
|
var self = this;
|
2013-11-21 00:36:02 +04:00
|
|
|
options = options || {};
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2014-02-22 01:54:56 +04:00
|
|
|
if (!this.myTags) {
|
2013-08-21 16:55:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
return Post.forge({id: newPost.id}).fetch({withRelated: ['tags'], transacting: options.transacting}).then(function (thisPostWithTags) {
|
2013-11-21 00:36:02 +04:00
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
var existingTags = thisPostWithTags.related('tags').toJSON(),
|
|
|
|
tagOperations = [],
|
|
|
|
tagsToDetach = [],
|
2014-01-13 18:29:40 +04:00
|
|
|
tagsToAttach = [],
|
|
|
|
createdTagsToAttach = [];
|
2013-09-14 00:38:53 +04:00
|
|
|
|
|
|
|
// First find any tags which have been removed
|
|
|
|
_.each(existingTags, function (existingTag) {
|
2014-02-22 01:54:56 +04:00
|
|
|
if (!_.some(self.myTags, function (newTag) { return newTag.name === existingTag.name; })) {
|
2013-09-14 00:38:53 +04:00
|
|
|
tagsToDetach.push(existingTag.id);
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
});
|
2013-09-14 00:38:53 +04:00
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
if (tagsToDetach.length > 0) {
|
2014-02-19 17:57:26 +04:00
|
|
|
tagOperations.push(newPost.tags().detach(tagsToDetach, options));
|
2013-08-21 16:55:58 +04:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
// Next check if new tags are all exactly the same as what is set on the model
|
2014-02-22 01:54:56 +04:00
|
|
|
_.each(self.myTags, function (newTag) {
|
2013-09-14 00:38:53 +04:00
|
|
|
if (!_.some(existingTags, function (existingTag) { return newTag.name === existingTag.name; })) {
|
|
|
|
// newTag isn't on this post yet
|
|
|
|
tagsToAttach.push(newTag);
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
});
|
|
|
|
|
2013-09-27 14:55:02 +04:00
|
|
|
if (!_.isEmpty(tagsToAttach)) {
|
2013-11-21 00:36:02 +04:00
|
|
|
return Tags.forge().query('whereIn', 'name', _.pluck(tagsToAttach, 'name')).fetch(options).then(function (matchingTags) {
|
2013-09-14 00:38:53 +04:00
|
|
|
_.each(matchingTags.toJSON(), function (matchingTag) {
|
2014-02-19 17:57:26 +04:00
|
|
|
tagOperations.push(newPost.tags().attach(matchingTag.id, options));
|
2013-09-14 00:38:53 +04:00
|
|
|
tagsToAttach = _.reject(tagsToAttach, function (tagToAttach) {
|
|
|
|
return tagToAttach.name === matchingTag.name;
|
|
|
|
});
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2014-01-13 18:29:40 +04:00
|
|
|
// Return if no tags to add
|
|
|
|
if (tagsToAttach.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set method to insert, so each tag gets inserted with the appropriate options
|
|
|
|
var opt = options.method;
|
|
|
|
options.method = 'insert';
|
|
|
|
|
|
|
|
// Create each tag that doesn't yet exist
|
2013-09-14 00:38:53 +04:00
|
|
|
_.each(tagsToAttach, function (tagToCreateAndAttach) {
|
2014-01-13 18:29:40 +04:00
|
|
|
var createAndAttachOperation = Tag.add({name: tagToCreateAndAttach.name}, options).then(function (createdTag) {
|
|
|
|
createdTagsToAttach.push(createdTag);
|
|
|
|
|
|
|
|
// If the tags are all inserted, process them
|
|
|
|
if (tagsToAttach.length === createdTagsToAttach.length) {
|
|
|
|
|
|
|
|
// Set method back to whatever it was, for tag attachment
|
|
|
|
options.method = opt;
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2014-01-13 18:29:40 +04:00
|
|
|
// Attach each newly created tag
|
|
|
|
_.each(createdTagsToAttach, function (tagToAttach) {
|
2014-02-19 17:57:26 +04:00
|
|
|
newPost.tags().attach(tagToAttach.id, tagToAttach.name, options);
|
2014-01-13 18:29:40 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
tagOperations.push(createAndAttachOperation);
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
|
|
|
|
2014-01-13 18:29:40 +04:00
|
|
|
// Return when all tags attached
|
2013-09-14 00:38:53 +04:00
|
|
|
return when.all(tagOperations);
|
2014-01-13 18:29:40 +04:00
|
|
|
|
2013-09-14 00:38:53 +04:00
|
|
|
});
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
|
|
|
|
return when.all(tagOperations);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Relations
|
2013-06-25 15:43:15 +04:00
|
|
|
user: function () {
|
|
|
|
return this.belongsTo(User, 'created_by');
|
|
|
|
},
|
2013-06-07 00:36:17 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
author: function () {
|
|
|
|
return this.belongsTo(User, 'author_id');
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
tags: function () {
|
|
|
|
return this.belongsToMany(Tag);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, {
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-08-23 13:13:32 +04:00
|
|
|
// #### findAll
|
|
|
|
// Extends base model findAll to eager-fetch author and user relationships.
|
|
|
|
findAll: function (options) {
|
|
|
|
options = options || {};
|
2013-09-24 14:46:30 +04:00
|
|
|
options.withRelated = [ 'author', 'user', 'tags' ];
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.findAll.call(this, options);
|
2013-08-23 13:13:32 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// #### findOne
|
|
|
|
// Extends base model findOne to eager-fetch author and user relationships.
|
|
|
|
findOne: function (args, options) {
|
|
|
|
options = options || {};
|
2013-10-18 21:18:49 +04:00
|
|
|
|
|
|
|
args = _.extend({
|
|
|
|
status: 'published'
|
|
|
|
}, args || {});
|
|
|
|
|
|
|
|
if (args.status === 'all') {
|
|
|
|
delete args.status;
|
|
|
|
}
|
|
|
|
|
2013-09-24 14:46:30 +04:00
|
|
|
options.withRelated = [ 'author', 'user', 'tags' ];
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.findOne.call(this, args, options);
|
2013-08-23 13:13:32 +04:00
|
|
|
},
|
|
|
|
|
2013-06-25 22:07:19 +04:00
|
|
|
// #### findPage
|
|
|
|
// Find results by page - returns an object containing the
|
|
|
|
// information about the request (page, limit), along with the
|
|
|
|
// info needed for pagination (pages, total).
|
|
|
|
|
|
|
|
// **response:**
|
|
|
|
|
|
|
|
// {
|
|
|
|
// posts: [
|
|
|
|
// {...}, {...}, {...}
|
|
|
|
// ],
|
|
|
|
// page: __,
|
|
|
|
// limit: __,
|
|
|
|
// pages: __,
|
|
|
|
// total: __
|
|
|
|
// }
|
|
|
|
|
|
|
|
/*
|
2013-06-25 15:43:15 +04:00
|
|
|
* @params opts
|
|
|
|
*/
|
|
|
|
findPage: function (opts) {
|
2014-02-13 07:26:56 +04:00
|
|
|
var postCollection = Posts.forge(),
|
|
|
|
tagInstance = opts.tag !== undefined ? Tag.forge({slug: opts.tag}) : false,
|
2013-12-20 02:51:28 +04:00
|
|
|
permittedOptions = ['page', 'limit', 'status', 'staticPages'];
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// sanitize opts so we are not automatically passing through any and all
|
|
|
|
// query strings to Bookshelf / Knex. Although the API requires auth, we
|
|
|
|
// should prevent this until such time as we can design the API properly and safely.
|
2013-12-20 02:51:28 +04:00
|
|
|
opts = _.pick(opts, permittedOptions);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// Set default settings for options
|
2013-06-25 15:43:15 +04:00
|
|
|
opts = _.extend({
|
2013-12-20 02:51:28 +04:00
|
|
|
page: 1, // pagination page
|
2013-06-25 15:43:15 +04:00
|
|
|
limit: 15,
|
2013-12-20 02:51:28 +04:00
|
|
|
staticPages: false, // include static pages
|
2014-02-13 07:26:56 +04:00
|
|
|
status: 'published',
|
|
|
|
where: {}
|
2013-06-25 15:43:15 +04:00
|
|
|
}, opts);
|
2013-06-01 23:30:42 +04:00
|
|
|
|
2013-12-20 02:51:28 +04:00
|
|
|
if (opts.staticPages !== 'all') {
|
|
|
|
// convert string true/false to boolean
|
|
|
|
if (!_.isBoolean(opts.staticPages)) {
|
|
|
|
opts.staticPages = opts.staticPages === 'true' || opts.staticPages === '1' ? true : false;
|
|
|
|
}
|
|
|
|
opts.where.page = opts.staticPages;
|
2013-10-17 09:21:56 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Unless `all` is passed as an option, filter on
|
|
|
|
// the status provided.
|
|
|
|
if (opts.status !== 'all') {
|
2013-12-20 02:51:28 +04:00
|
|
|
// make sure that status is valid
|
|
|
|
opts.status = _.indexOf(['published', 'draft'], opts.status) !== -1 ? opts.status : 'published';
|
2013-06-25 15:43:15 +04:00
|
|
|
opts.where.status = opts.status;
|
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// If there are where conditionals specified, add those
|
|
|
|
// to the query.
|
|
|
|
if (opts.where) {
|
|
|
|
postCollection.query('where', opts.where);
|
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// Fetch related models
|
2013-09-24 14:46:30 +04:00
|
|
|
opts.withRelated = [ 'author', 'user', 'tags' ];
|
2013-08-23 13:13:32 +04:00
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
// If a query param for a tag is attached
|
|
|
|
// we need to fetch the tag model to find its id
|
|
|
|
function fetchTagQuery() {
|
|
|
|
if (tagInstance) {
|
|
|
|
return tagInstance.fetch();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return when(fetchTagQuery())
|
|
|
|
|
|
|
|
// Set the limit & offset for the query, fetching
|
|
|
|
// with the opts (to specify any eager relations, etc.)
|
|
|
|
// Omitting the `page`, `limit`, `where` just to be sure
|
|
|
|
// aren't used for other purposes.
|
|
|
|
.then(function () {
|
|
|
|
// If we have a tag instance we need to modify our query.
|
|
|
|
// We need to ensure we only select posts that contain
|
|
|
|
// the tag given in the query param.
|
|
|
|
if (tagInstance) {
|
|
|
|
postCollection
|
|
|
|
.query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id')
|
|
|
|
.query('where', 'posts_tags.tag_id', '=', tagInstance.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return postCollection
|
|
|
|
.query('limit', opts.limit)
|
|
|
|
.query('offset', opts.limit * (opts.page - 1))
|
|
|
|
.query('orderBy', 'status', 'ASC')
|
|
|
|
.query('orderBy', 'published_at', 'DESC')
|
|
|
|
.query('orderBy', 'updated_at', 'DESC')
|
|
|
|
.fetch(_.omit(opts, 'page', 'limit'));
|
|
|
|
})
|
|
|
|
|
|
|
|
// Fetch pagination information
|
|
|
|
.then(function () {
|
|
|
|
var qb,
|
|
|
|
tableName = _.result(postCollection, 'tableName'),
|
|
|
|
idAttribute = _.result(postCollection, 'idAttribute');
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// After we're done, we need to figure out what
|
|
|
|
// the limits are for the pagination values.
|
2014-02-13 07:26:56 +04:00
|
|
|
qb = ghostBookshelf.knex(tableName);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
if (opts.where) {
|
|
|
|
qb.where(opts.where);
|
2013-06-09 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:26:56 +04:00
|
|
|
if (tagInstance) {
|
|
|
|
qb.join('posts_tags', 'posts_tags.post_id', '=', 'posts.id');
|
|
|
|
qb.where('posts_tags.tag_id', '=', tagInstance.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qb.count(tableName + '.' + idAttribute + ' as aggregate');
|
|
|
|
})
|
|
|
|
|
|
|
|
// Format response of data
|
|
|
|
.then(function (resp) {
|
|
|
|
var totalPosts = parseInt(resp[0].aggregate, 10),
|
|
|
|
data = {
|
|
|
|
posts: postCollection.toJSON(),
|
|
|
|
page: parseInt(opts.page, 10),
|
|
|
|
limit: opts.limit,
|
|
|
|
pages: Math.ceil(totalPosts / opts.limit),
|
|
|
|
total: totalPosts
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data.pages > 1) {
|
|
|
|
if (data.page === 1) {
|
|
|
|
data.next = data.page + 1;
|
|
|
|
} else if (data.page === data.pages) {
|
|
|
|
data.prev = data.page - 1;
|
|
|
|
} else {
|
|
|
|
data.next = data.page + 1;
|
|
|
|
data.prev = data.page - 1;
|
2013-06-25 19:13:19 +04:00
|
|
|
}
|
2014-02-13 07:26:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tagInstance) {
|
|
|
|
data.aspect = {
|
|
|
|
tag: tagInstance.toJSON()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
})
|
|
|
|
.catch(errors.logAndThrowError);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
permissable: function (postModelOrId, userId, action_type, userPermissions) {
|
|
|
|
var self = this,
|
|
|
|
hasPermission,
|
|
|
|
postModel = postModelOrId;
|
|
|
|
|
|
|
|
// If we passed in an id instead of a model, get the model
|
|
|
|
// then check the permissions
|
|
|
|
if (_.isNumber(postModelOrId) || _.isString(postModelOrId)) {
|
|
|
|
return this.read({id: postModelOrId}).then(function (foundPostModel) {
|
|
|
|
return self.permissable(foundPostModel, userId, action_type, userPermissions);
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
// Check if any permissions apply for this user and post.
|
2013-06-25 15:43:15 +04:00
|
|
|
hasPermission = _.any(userPermissions, function (perm) {
|
2013-08-16 03:22:08 +04:00
|
|
|
// Check for matching action type and object type
|
|
|
|
if (perm.get('action_type') !== action_type ||
|
|
|
|
perm.get('object_type') !== 'post') {
|
2013-06-25 15:43:15 +04:00
|
|
|
return false;
|
2013-06-09 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
// If asking whether we can create posts,
|
|
|
|
// and we have a create posts permission then go ahead and say yes
|
|
|
|
if (action_type === 'create' && perm.get('action_type') === action_type) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for either no object id or a matching one
|
2013-06-25 15:43:15 +04:00
|
|
|
return !perm.get('object_id') || perm.get('object_id') === postModel.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If this is the author of the post, allow it.
|
2013-08-16 03:22:08 +04:00
|
|
|
// Moved below the permissions checks because there may not be a postModel
|
|
|
|
// in the case like canThis(user).create.post()
|
|
|
|
hasPermission = hasPermission || (postModel && userId === postModel.get('author_id'));
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
// Resolve if we have appropriate permissions
|
2013-06-25 15:43:15 +04:00
|
|
|
if (hasPermission) {
|
|
|
|
return when.resolve();
|
2013-06-01 18:47:41 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Otherwise, you shall not pass.
|
|
|
|
return when.reject();
|
2013-09-13 17:29:59 +04:00
|
|
|
},
|
|
|
|
add: function (newPostData, options) {
|
2013-11-03 21:13:19 +04:00
|
|
|
var self = this;
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-11-03 21:13:19 +04:00
|
|
|
return ghostBookshelf.Model.add.call(this, newPostData, options).then(function (post) {
|
2014-02-19 17:57:26 +04:00
|
|
|
return self.findOne({status: 'all', id: post.id}, options);
|
2013-11-03 21:13:19 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
edit: function (editedPost, options) {
|
|
|
|
var self = this;
|
|
|
|
|
2014-02-19 17:57:26 +04:00
|
|
|
return ghostBookshelf.Model.edit.call(this, editedPost, options).then(function (post) {
|
|
|
|
return self.findOne({status: 'all', id: post.id}, options);
|
2013-09-13 17:29:59 +04:00
|
|
|
});
|
2013-09-27 14:56:20 +04:00
|
|
|
},
|
|
|
|
destroy: function (_identifier, options) {
|
|
|
|
options = options || {};
|
|
|
|
return this.forge({id: _identifier}).fetch({withRelated: ['tags']}).then(function destroyTags(post) {
|
|
|
|
var tagIds = _.pluck(post.related('tags').toJSON(), 'id');
|
|
|
|
if (tagIds) {
|
|
|
|
return post.tags().detach(tagIds).then(function destroyPost() {
|
|
|
|
return post.destroy(options);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return post.destroy(options);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
Posts = ghostBookshelf.Collection.extend({
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
model: Post
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
Post: Post,
|
|
|
|
Posts: Posts
|
2014-02-25 14:18:33 +04:00
|
|
|
};
|