2015-06-25 12:03:10 +09:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// Collection Hooks //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate HTML body from Markdown on post insert
|
|
|
|
*/
|
|
|
|
Posts.before.insert(function (userId, doc) {
|
|
|
|
if(!!doc.body)
|
|
|
|
doc.htmlBody = Telescope.utils.sanitize(marked(doc.body));
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate HTML body from Markdown when post body is updated
|
|
|
|
*/
|
|
|
|
Posts.before.update(function (userId, doc, fieldNames, modifier) {
|
2015-07-14 11:39:58 +09:00
|
|
|
// if body is being modified or $unset, update htmlBody too
|
2015-06-25 12:03:10 +09:00
|
|
|
if (Meteor.isServer && modifier.$set && modifier.$set.body) {
|
|
|
|
modifier.$set.htmlBody = Telescope.utils.sanitize(marked(modifier.$set.body));
|
|
|
|
}
|
2015-07-14 11:39:58 +09:00
|
|
|
if (Meteor.isServer && modifier.$unset && (typeof modifier.$unset.body !== "undefined")) {
|
|
|
|
modifier.$unset.htmlBody = "";
|
|
|
|
}
|
2015-06-25 12:03:10 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate slug when post title is updated
|
|
|
|
*/
|
|
|
|
Posts.before.update(function (userId, doc, fieldNames, modifier) {
|
|
|
|
// if title is being modified, update slug too
|
|
|
|
if (Meteor.isServer && modifier.$set && modifier.$set.title) {
|
2015-06-30 19:31:09 +09:00
|
|
|
modifier.$set.slug = Telescope.utils.slugify(modifier.$set.title);
|
2015-06-25 12:03:10 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-30 19:11:25 +09:00
|
|
|
/**
|
|
|
|
* Disallow $rename
|
|
|
|
*/
|
|
|
|
Posts.before.update(function (userId, doc, fieldNames, modifier) {
|
|
|
|
if (!!modifier.$rename) {
|
|
|
|
throw new Meteor.Error("illegal $rename operator detected!");
|
|
|
|
}
|
|
|
|
});
|
2015-06-25 12:03:10 +09:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// Callbacks //
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
/**
|
|
|
|
* Increment the user's post count and upvote the post
|
|
|
|
*/
|
|
|
|
function afterPostSubmitOperations (post) {
|
2015-06-21 11:58:46 +09:00
|
|
|
var userId = post.userId;
|
2015-05-06 12:16:50 +09:00
|
|
|
Meteor.users.update({_id: userId}, {$inc: {"telescope.postCount": 1}});
|
2015-04-23 15:42:05 +09:00
|
|
|
return post;
|
|
|
|
}
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add("postSubmitAsync", afterPostSubmitOperations);
|
2015-06-21 11:58:46 +09:00
|
|
|
|
|
|
|
function upvoteOwnPost (post) {
|
|
|
|
var postAuthor = Meteor.users.findOne(post.userId);
|
2015-09-08 09:48:39 +09:00
|
|
|
Telescope.upvoteItem(Posts, post._id, postAuthor);
|
2015-06-21 11:58:46 +09:00
|
|
|
return post;
|
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postSubmitAsync", upvoteOwnPost);
|
2015-09-06 11:37:48 +09:00
|
|
|
|
|
|
|
function setPostedAtOnApprove (post) {
|
|
|
|
// unless post is already scheduled and has a postedAt date, set its postedAt date to now
|
|
|
|
if (!post.postedAt) {
|
|
|
|
Posts.update(post._id, {postedAt: new Date()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postApproveAsync", setPostedAtOnApprove);
|