Vulcan/common/comment.js

74 lines
2.5 KiB
JavaScript
Raw Normal View History

2012-08-31 19:41:54 -04:00
Meteor.methods({
comment: function(postId, parentCommentId, text){
2012-10-04 11:53:15 +10:00
var user = Meteor.user();
var post=Posts.findOne(postId);
var postUser=Meteor.users.findOne(post.userId);
var timeSinceLastComment=timeSinceLast(user, Comments);
2012-12-15 13:27:31 +01:00
var cleanText= cleanUp(text);
var commentInterval = Math.abs(parseInt(getSetting('commentInterval'))) || 15;
var properties={
'commentAuthorId': user._id,
'commentAuthorName': getDisplayName(user),
'postId': postId,
'postHeadline' : post.headline
};
// check that user can comment
2012-10-05 13:59:40 +09:00
if (!user || !canPost(user))
2012-10-17 16:04:47 +09:00
throw new Meteor.Error('You need to login or be invited to post new comments.');
// check that user waits more than 15 seconds between comments
if(!this.isSimulation && (timeSinceLastComment < commentInterval))
throw new Meteor.Error(704, 'Please wait '+(commentInterval-timeSinceLastComment)+' seconds before commenting again');
2012-10-05 13:59:40 +09:00
2012-08-31 19:41:54 -04:00
var comment = {
post: postId
, body: cleanText
2012-10-01 16:08:30 +09:00
, userId: user._id
2012-08-31 19:41:54 -04:00
, submitted: new Date().getTime()
2012-10-01 16:08:30 +09:00
, author: getDisplayName(user)
2012-08-31 19:41:54 -04:00
};
if(parentCommentId)
comment.parent = parentCommentId;
2012-08-31 19:41:54 -04:00
var newCommentId=Comments.insert(comment);
Posts.update(postId, {$inc: {comments: 1}});
Meteor.call('upvoteComment', newCommentId);
properties.commentId = newCommentId;
if(!this.isSimulation){
if(parentCommentId){
// child comment
var parentComment=Comments.find(parentCommentId);
var parentUser=Meteor.users.find(parentComment.userId);
properties.parentCommentId = parentCommentId;
properties.parentAuthorId = parentComment.userId;
properties.parentAuthorName = getDisplayName(parentUser);
createNotification('newReply', properties, parentUser, user);
if(parentComment.userId!=post.userId){
// if the original poster is different from the author of the parent comment, notify them too
createNotification('newComment', properties, postUser, Meteor.user());
}
}else{
// root comment
createNotification('newComment', properties, postUser, Meteor.user());
}
}
return properties;
},
removeComment: function(commentId){
var comment=Comments.findOne(commentId);
// decrement post comment count
Posts.update(comment.post, {$inc: {comments: -1}});
// note: should we also decrease user's comment karma ?
Comments.remove(commentId);
2012-08-31 19:41:54 -04:00
}
});