2013-06-25 15:43:15 +04:00
|
|
|
var Post,
|
|
|
|
Posts,
|
|
|
|
_ = require('underscore'),
|
|
|
|
uuid = require('node-uuid'),
|
|
|
|
when = require('when'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
Showdown = require('showdown'),
|
2013-08-20 01:52:50 +04:00
|
|
|
github = require('../../shared/vendor/showdown/extensions/github'),
|
|
|
|
converter = new Showdown.converter({extensions: [github]}),
|
2013-06-25 15:43:15 +04:00
|
|
|
User = require('./user').User,
|
2013-08-21 16:55:58 +04:00
|
|
|
Tag = require('./tag').Tag,
|
2013-06-25 15:43:15 +04:00
|
|
|
GhostBookshelf = require('./base');
|
|
|
|
|
|
|
|
Post = GhostBookshelf.Model.extend({
|
|
|
|
|
|
|
|
tableName: 'posts',
|
|
|
|
|
2013-08-25 14:49:31 +04:00
|
|
|
permittedAttributes: [
|
|
|
|
'id', 'uuid', 'title', 'slug', 'content_raw', 'content', 'meta_title', 'meta_description', 'meta_keywords',
|
|
|
|
'featured', 'image', 'status', 'language', 'author_id', 'created_at', 'created_by', 'updated_at', 'updated_by',
|
|
|
|
'published_at', 'published_by'
|
|
|
|
],
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
hasTimestamps: true,
|
|
|
|
|
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
|
|
|
status: 'draft'
|
|
|
|
// TODO: language: ghost.config().defaultLang);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.on('creating', this.creating, this);
|
2013-08-21 16:55:58 +04:00
|
|
|
this.on('saving', this.updateTags, this);
|
2013-06-25 15:43:15 +04:00
|
|
|
this.on('saving', this.saving, this);
|
2013-08-25 14:49:31 +04:00
|
|
|
this.on('saving', this.validate, this);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
|
|
|
|
2013-08-20 22:52:44 +04:00
|
|
|
validate: function () {
|
|
|
|
GhostBookshelf.validator.check(this.get('title'), "Post title cannot be blank").notEmpty();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
saving: function () {
|
2013-08-25 14:49:31 +04:00
|
|
|
// Deal with the related data here
|
|
|
|
|
|
|
|
// Remove any properties which don't belong on the post model
|
|
|
|
this.attributes = this.pick(this.permittedAttributes);
|
|
|
|
|
2013-07-11 19:27:19 +04:00
|
|
|
this.set('content', converter.makeHtml(this.get('content_raw')));
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-08-30 08:18:55 +04:00
|
|
|
this.set('title', this.get('title').trim());
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (this.hasChanged('status') && this.get('status') === 'published') {
|
|
|
|
this.set('published_at', new Date());
|
|
|
|
// This will need to go elsewhere in the API layer.
|
|
|
|
this.set('published_by', 1);
|
|
|
|
}
|
2013-06-15 20:11:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
this.set('updated_by', 1);
|
2013-08-21 16:55:58 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// refactoring of ghost required in order to make these details available here
|
2013-08-21 16:55:58 +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
|
|
|
creating: function () {
|
2013-08-20 22:52:44 +04:00
|
|
|
// set any dynamic default properties
|
2013-06-26 05:42:51 +04:00
|
|
|
var self = this;
|
2013-06-25 15:43:15 +04:00
|
|
|
if (!this.get('created_by')) {
|
|
|
|
this.set('created_by', 1);
|
|
|
|
}
|
2013-06-18 02:59:08 +04:00
|
|
|
|
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
|
|
|
|
|
|
|
if (!this.get('slug')) {
|
|
|
|
// Generating a slug requires a db call to look for conflicting slugs
|
|
|
|
return this.generateSlug(this.get('title'))
|
|
|
|
.then(function (slug) {
|
|
|
|
self.set({slug: slug});
|
|
|
|
});
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 22:07:19 +04:00
|
|
|
// #### generateSlug
|
|
|
|
// Create a string act as the permalink for a post.
|
2013-06-26 05:42:51 +04:00
|
|
|
generateSlug: function (title) {
|
|
|
|
var slug,
|
|
|
|
slugTryCount = 1,
|
|
|
|
// Look for a post with a matching slug, append an incrementing number if so
|
|
|
|
checkIfSlugExists = function (slugToFind) {
|
|
|
|
return Post.read({slug: slugToFind}).then(function (found) {
|
2013-08-18 22:16:37 +04:00
|
|
|
var trimSpace;
|
|
|
|
|
2013-06-26 05:42:51 +04:00
|
|
|
if (!found) {
|
|
|
|
return when.resolve(slugToFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
slugTryCount += 1;
|
|
|
|
|
2013-08-18 22:16:37 +04:00
|
|
|
// 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;
|
2013-06-26 05:42:51 +04:00
|
|
|
|
2013-08-18 22:16:37 +04:00
|
|
|
return checkIfSlugExists(slugToFind);
|
2013-06-26 05:42:51 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-08 12:12:23 +04:00
|
|
|
// Remove URL reserved chars: `:/?#[]@!$&'()*+,;=` as well as `\%<>|^~£"`
|
2013-08-20 06:18:39 +04:00
|
|
|
slug = title.trim().replace(/[:\/\?#\[\]@!$&'()*+,;=\\%<>\|\^~£"]/g, '')
|
2013-07-08 12:12:23 +04:00
|
|
|
// 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();
|
|
|
|
|
2013-07-04 23:53:45 +04:00
|
|
|
// Remove trailing hypen
|
|
|
|
slug = slug.charAt(slug.length - 1) === '-' ? slug.substr(0, slug.length - 1) : slug;
|
2013-06-26 01:27:38 +04:00
|
|
|
// Check the filtered slug doesn't match any of the reserved keywords
|
|
|
|
slug = /^(ghost|ghost\-admin|admin|wp\-admin|dashboard|login|archive|archives|category|categories|tag|tags|page|pages|post|posts)$/g
|
|
|
|
.test(slug) ? slug + '-post' : slug;
|
2013-06-26 05:42:51 +04:00
|
|
|
|
2013-08-23 20:49:45 +04:00
|
|
|
//if slug is empty after trimming use "post"
|
|
|
|
if (!slug) {
|
|
|
|
slug = "post";
|
|
|
|
}
|
2013-06-26 05:42:51 +04:00
|
|
|
// Test for duplicate slugs.
|
2013-07-08 12:12:23 +04:00
|
|
|
return checkIfSlugExists(slug);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
updateTags: function () {
|
|
|
|
var self = this,
|
|
|
|
tagOperations = [],
|
|
|
|
newTags = this.get('tags'),
|
|
|
|
tagsToDetach,
|
|
|
|
existingTagIDs,
|
|
|
|
tagsToCreateAndAdd,
|
|
|
|
tagsToAddByID,
|
|
|
|
fetchOperation;
|
|
|
|
|
|
|
|
if (!newTags) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchOperation = Post.forge({id: this.id}).fetch({withRelated: ['tags']});
|
|
|
|
return fetchOperation.then(function (thisModelWithTags) {
|
|
|
|
var existingTags = thisModelWithTags.related('tags').models;
|
|
|
|
|
|
|
|
tagsToDetach = existingTags.filter(function (existingTag) {
|
|
|
|
var tagStillRemains = newTags.some(function (newTag) {
|
|
|
|
return newTag.id === existingTag.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return !tagStillRemains;
|
|
|
|
});
|
|
|
|
if (tagsToDetach.length > 0) {
|
|
|
|
tagOperations.push(self.tags().detach(tagsToDetach));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect any tags that have been added by ID
|
|
|
|
existingTagIDs = existingTags.map(function (existingTag) {
|
|
|
|
return existingTag.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
tagsToAddByID = newTags.filter(function (newTag) {
|
|
|
|
return existingTagIDs.indexOf(newTag.id) === -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (tagsToAddByID.length > 0) {
|
|
|
|
tagsToAddByID = _.pluck(tagsToAddByID, 'id');
|
|
|
|
tagOperations.push(self.tags().attach(tagsToAddByID));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect any tags that have been added, but don't already exist in the database
|
|
|
|
tagsToCreateAndAdd = newTags.filter(function (newTag) {
|
|
|
|
return newTag.id === null || newTag.id === undefined;
|
|
|
|
});
|
|
|
|
tagsToCreateAndAdd.forEach(function (tagToCreateAndAdd) {
|
|
|
|
var createAndAddOperation = Tag.add({name: tagToCreateAndAdd.name}).then(function (createdTag) {
|
|
|
|
return self.tags().attach(createdTag.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
tagOperations.push(createAndAddOperation);
|
|
|
|
});
|
|
|
|
|
|
|
|
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-08-21 16:55:58 +04:00
|
|
|
options.withRelated = [ "author", "user", "tags" ];
|
2013-08-23 13:13:32 +04:00
|
|
|
return GhostBookshelf.Model.findAll.call(this, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
// #### findOne
|
|
|
|
// Extends base model findOne to eager-fetch author and user relationships.
|
|
|
|
findOne: function (args, options) {
|
|
|
|
options = options || {};
|
2013-08-21 16:55:58 +04:00
|
|
|
options.withRelated = [ "author", "user", "tags" ];
|
2013-08-23 13:13:32 +04:00
|
|
|
return GhostBookshelf.Model.findOne.call(this, args, options);
|
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
var postCollection;
|
|
|
|
|
|
|
|
// Allow findPage(n)
|
|
|
|
if (_.isString(opts) || _.isNumber(opts)) {
|
|
|
|
opts = {page: opts};
|
|
|
|
}
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
opts = _.extend({
|
|
|
|
page: 1,
|
|
|
|
limit: 15,
|
|
|
|
where: {},
|
|
|
|
status: 'published',
|
|
|
|
orderBy: ['published_at', 'DESC']
|
|
|
|
}, opts);
|
2013-06-01 23:30:42 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
postCollection = Posts.forge();
|
2013-06-01 18:47:41 +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') {
|
|
|
|
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
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
opts.withRelated = [ "author", "user", "tags" ];
|
2013-08-23 13:13:32 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// 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.
|
|
|
|
return postCollection
|
|
|
|
.query('limit', opts.limit)
|
|
|
|
.query('offset', opts.limit * (opts.page - 1))
|
|
|
|
.query('orderBy', opts.orderBy[0], opts.orderBy[1])
|
|
|
|
.fetch(_.omit(opts, 'page', 'limit', 'where', 'status', 'orderBy'))
|
|
|
|
.then(function (collection) {
|
|
|
|
var qb;
|
|
|
|
|
|
|
|
// After we're done, we need to figure out what
|
|
|
|
// the limits are for the pagination values.
|
|
|
|
qb = GhostBookshelf.Knex(_.result(collection, 'tableName'));
|
|
|
|
|
|
|
|
if (opts.where) {
|
|
|
|
qb.where(opts.where);
|
2013-06-09 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return qb.count(_.result(collection, 'idAttribute')).then(function (resp) {
|
2013-06-25 19:13:19 +04:00
|
|
|
var totalPosts = resp[0].aggregate,
|
|
|
|
data = {
|
|
|
|
posts: collection.toJSON(),
|
|
|
|
page: opts.page,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
2013-06-25 15:43:15 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
},
|
|
|
|
|
|
|
|
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-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
|
|
|
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
|
2013-07-04 23:53:45 +04:00
|
|
|
};
|