Vulcan/collections/comments.js

287 lines
7.8 KiB
JavaScript
Raw Normal View History

2015-01-22 12:03:44 +09:00
commentSchemaObject = {
2014-09-29 10:15:21 +09:00
_id: {
2015-01-22 11:58:14 +09:00
type: String,
optional: true
2014-09-29 10:15:21 +09:00
},
parentCommentId: {
2015-01-22 11:58:14 +09:00
type: String,
optional: true,
autoform: {
editable: true,
omit: true
}
2014-09-29 10:15:21 +09:00
},
createdAt: {
2015-01-22 11:58:14 +09:00
type: Date,
optional: true
2014-09-29 10:15:21 +09:00
},
postedAt: { // for now, comments are always created and posted at the same time
2015-01-22 11:58:14 +09:00
type: Date,
optional: true
2014-09-29 10:15:21 +09:00
},
body: {
2015-01-22 11:58:14 +09:00
type: String,
autoform: {
editable: true
}
2014-09-29 10:15:21 +09:00
},
htmlBody: {
2015-01-22 11:58:14 +09:00
type: String,
optional: true
2014-09-29 10:15:21 +09:00
},
baseScore: {
2015-01-22 11:58:14 +09:00
type: Number,
decimal: true,
optional: true
2014-09-29 10:15:21 +09:00
},
score: {
2015-01-22 11:58:14 +09:00
type: Number,
decimal: true,
optional: true
2014-09-29 10:15:21 +09:00
},
upvotes: {
2015-01-22 11:58:14 +09:00
type: Number,
optional: true
2014-09-29 10:15:21 +09:00
},
upvoters: {
2015-01-22 11:58:14 +09:00
type: [String], // XXX
optional: true
2014-09-29 10:15:21 +09:00
},
downvotes: {
type: Number,
optional: true
},
downvoters: {
2015-01-22 11:58:14 +09:00
type: [String], // XXX
optional: true
2014-09-29 10:15:21 +09:00
},
author: {
2015-01-22 11:58:14 +09:00
type: String,
optional: true
2014-09-29 10:15:21 +09:00
},
inactive: {
2015-01-22 11:58:14 +09:00
type: Boolean,
optional: true
2014-09-29 10:15:21 +09:00
},
postId: {
2015-01-22 11:58:14 +09:00
type: String, // XXX
optional: true,
autoform: {
editable: true,
omit: true
}
2014-09-29 10:15:21 +09:00
},
userId: {
2015-01-22 11:58:14 +09:00
type: String, // XXX
optional: true
2014-09-29 10:15:21 +09:00
},
isDeleted: {
2015-01-22 11:58:14 +09:00
type: Boolean,
optional: true
2014-09-29 10:15:21 +09:00
}
2014-12-31 17:44:21 +09:00
};
2015-01-22 12:03:44 +09:00
// add any extra properties to commentSchemaObject (provided by packages for example)
2014-12-31 17:44:21 +09:00
_.each(addToCommentsSchema, function(item){
2015-01-22 12:03:44 +09:00
commentSchemaObject[item.propertyName] = item.propertySchema;
});
2014-05-10 16:57:17 +09:00
Comments = new Meteor.Collection("comments");
2014-12-31 17:44:21 +09:00
2015-01-22 12:03:44 +09:00
commentSchema = new SimpleSchema(commentSchemaObject);
2014-12-31 17:44:21 +09:00
2015-01-22 12:03:44 +09:00
Comments.attachSchema(commentSchema);
2013-07-04 12:51:26 +09:00
Comments.deny({
update: function(userId, post, fieldNames) {
if(Users.isAdminById(userId))
2013-07-05 07:09:15 +09:00
return false;
2013-11-05 09:32:21 +09:00
// deny the update if it contains something other than the following fields
return (_.without(fieldNames, 'body').length > 0);
2013-07-04 12:51:26 +09:00
}
});
2013-11-05 09:32:21 +09:00
Comments.allow({
update: Users.can.editById,
remove: Users.can.editById
2013-11-05 09:32:21 +09:00
});
2014-12-24 10:13:48 +09:00
// ------------------------------------------------------------------------------------------- //
// ------------------------------------------ Hooks ------------------------------------------ //
// ------------------------------------------------------------------------------------------- //
Comments.before.insert(function (userId, doc) {
// note: only actually sanitizes on the server
doc.htmlBody = Telescope.utils.sanitize(marked(doc.body));
});
2013-11-05 09:32:21 +09:00
Comments.before.update(function (userId, doc, fieldNames, modifier, options) {
// if body is being modified, update htmlBody too
2014-09-11 14:11:41 +09:00
if (Meteor.isServer && modifier.$set && modifier.$set.body) {
modifier.$set = modifier.$set || {};
modifier.$set.htmlBody = Telescope.utils.sanitize(marked(modifier.$set.body));
}
});
2013-11-05 09:32:21 +09:00
2014-12-24 10:13:48 +09:00
commentAfterSubmitMethodCallbacks.push(function (comment) {
var userId = comment.userId,
commentAuthor = Meteor.users.findOne(userId);
// increment comment count
Meteor.users.update({_id: userId}, {
$inc: {'commentCount': 1}
});
// update post
Posts.update(comment.postId, {
$inc: {commentCount: 1},
$set: {lastCommentedAt: new Date()},
$addToSet: {commenters: userId}
});
// upvote comment
upvoteItem(Comments, comment, commentAuthor);
2014-12-24 10:13:48 +09:00
});
// ------------------------------------------------------------------------------------------- //
// -------------------------------------- Submit Comment ------------------------------------- //
// ------------------------------------------------------------------------------------------- //
submitComment = function (comment) {
var userId = comment.userId; // at this stage, a userId is expected
// ------------------------------ Checks ------------------------------ //
// Don't allow empty comments
if (!comment.body)
throw new Meteor.Error(704,i18n.t('your_comment_is_empty'));
2014-12-24 10:13:48 +09:00
// ------------------------------ Properties ------------------------------ //
var defaultProperties = {
createdAt: new Date(),
postedAt: new Date(),
upvotes: 0,
downvotes: 0,
baseScore: 0,
score: 0,
author: Users.getDisplayNameById(userId)
2014-12-24 10:13:48 +09:00
};
comment = _.extend(defaultProperties, comment);
// ------------------------------ Callbacks ------------------------------ //
// run all post submit server callbacks on comment object successively
comment = commentSubmitMethodCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, comment);
// -------------------------------- Insert -------------------------------- //
comment._id = Comments.insert(comment);
// --------------------- Server-side Async Callbacks --------------------- //
// run all post submit server callbacks on comment object successively
if (Meteor.isServer) {
Meteor.setTimeout(function () { // use setTimeout to avoid holding up client
comment = commentAfterSubmitMethodCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, comment);
}, 1);
}
return comment;
}
// ------------------------------------------------------------------------------------------- //
// ----------------------------------------- Methods ----------------------------------------- //
// ------------------------------------------------------------------------------------------- //
2012-08-31 19:41:54 -04:00
Meteor.methods({
2014-12-24 10:13:48 +09:00
submitComment: function(comment){
2014-12-24 10:13:48 +09:00
// required properties:
// postId
2014-12-31 17:44:21 +09:00
// body
2014-12-24 10:13:48 +09:00
// optional properties:
// parentCommentId
2013-04-26 17:28:09 +09:00
var user = Meteor.user(),
hasAdminRights = Users.isAdmin(user);
2014-12-24 10:13:48 +09:00
// ------------------------------ Checks ------------------------------ //
// check that user can comment
if (!user || !Users.can.comment(user))
throw new Meteor.Error(i18n.t('you_need_to_login_or_be_invited_to_post_new_comments'));
2014-12-24 10:13:48 +09:00
// ------------------------------ Rate Limiting ------------------------------ //
2014-12-24 10:13:48 +09:00
if (!hasAdminRights) {
var timeSinceLastComment = Users.timeSinceLast(user, Comments),
2015-03-28 18:30:26 +09:00
commentInterval = Math.abs(parseInt(Settings.get('commentInterval',15)));
2014-09-20 10:42:42 +09:00
2014-12-24 10:13:48 +09:00
// check that user waits more than 15 seconds between comments
if((timeSinceLastComment < commentInterval))
throw new Meteor.Error(704, i18n.t('please_wait')+(commentInterval-timeSinceLastComment)+i18n.t('seconds_before_commenting_again'));
2014-12-24 10:13:48 +09:00
}
2014-09-20 10:42:42 +09:00
2014-12-24 10:13:48 +09:00
// ------------------------------ Properties ------------------------------ //
2013-10-23 10:21:08 +08:00
2014-12-24 10:13:48 +09:00
// admin-only properties
// userId
2013-11-08 11:10:23 +09:00
2014-12-24 10:13:48 +09:00
// if user is not admin, clear restricted properties
if (!hasAdminRights) {
_.keys(comment).forEach(function (propertyName) {
2015-01-22 12:03:44 +09:00
var property = commentSchemaObject[propertyName];
if (!property || !property.autoform || !property.autoform.editable) {
console.log("// Disallowed property detected: "+propertyName+" (nice try!)");
delete comment[propertyName]
}
});
2014-12-24 10:13:48 +09:00
}
2014-12-24 10:13:48 +09:00
// if no userId has been set, default to current user id
if (!comment.userId) {
comment.userId = user._id
}
2014-12-24 10:13:48 +09:00
return submitComment(comment);
},
removeComment: function(commentId){
var comment = Comments.findOne(commentId);
if(Users.can.edit(Meteor.user(), comment)){
// decrement post comment count and remove user ID from post
Posts.update(comment.postId, {
2014-10-16 00:32:08 +02:00
$inc: {commentCount: -1},
$pull: {commenters: comment.userId}
});
2013-11-08 11:10:23 +09:00
// decrement user comment count and remove comment ID from user
Meteor.users.update({_id: comment.userId}, {
2014-10-16 00:32:08 +02:00
$inc: {'commentCount': -1}
});
2013-11-08 11:10:23 +09:00
// note: should we also decrease user's comment karma ?
2014-09-16 12:12:48 +09:00
// We don't actually delete the comment to avoid losing all child comments.
// Instead, we give it a special flag
Comments.update({_id: commentId}, {$set: {
body: 'Deleted',
htmlBody: 'Deleted',
isDeleted: true
}});
}else{
2015-03-27 16:24:21 +08:00
Messages.flash("You don't have permission to delete this comment.", "error");
}
2012-08-31 19:41:54 -04:00
}
});