Vulcan/collections/comments.js

216 lines
5.8 KiB
JavaScript
Raw Normal View History

Comments = new Meteor.Collection("comments", {
schema: new SimpleSchema({
2014-05-16 09:19:35 +09:00
_id: {
type: String,
2014-06-22 10:34:39 +09:00
optional: true
2014-05-16 09:19:35 +09:00
},
2014-07-05 16:21:28 +09:00
parentCommentId: {
type: String,
optional: true
},
2014-05-16 09:19:35 +09:00
createdAt: {
type: Date,
optional: true
},
2014-07-03 13:15:23 +09:00
postedAt: { // for now, comments are always created and posted at the same time
type: Date,
optional: true
},
body: {
type: String
},
htmlBody: {
type: String,
optional: true
},
baseScore: {
type: Number,
2014-06-22 10:34:39 +09:00
decimal: true,
optional: true
},
score: {
type: Number,
2014-06-22 10:34:39 +09:00
decimal: true,
optional: true
},
upvotes: {
type: Number,
optional: true
},
upvoters: {
type: [String], // XXX
optional: true
},
downvotes: {
type: Number,
optional: true
},
downvoters: {
type: [String], // XXX
optional: true
},
author: {
type: String,
optional: true
},
inactive: {
type: Boolean,
optional: true
},
postId: {
type: String, // XXX
optional: true
},
userId: {
type: String, // XXX
optional: true
2014-09-16 12:12:48 +09:00
},
isDeleted: {
type: Boolean,
optional: true
}
})
});
2014-05-10 16:57:17 +09:00
2013-07-04 12:51:26 +09:00
Comments.deny({
update: function(userId, post, fieldNames) {
if(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: canEditById,
remove: canEditById
});
Comments.before.insert(function (userId, doc) {
2014-09-11 14:11:41 +09:00
if(Meteor.isServer)
doc.htmlBody = 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 = sanitize(marked(modifier.$set.body));
}
});
2013-11-05 09:32:21 +09:00
2012-08-31 19:41:54 -04:00
Meteor.methods({
comment: function(postId, parentCommentId, text){
2013-04-26 17:28:09 +09:00
var user = Meteor.user(),
post = Posts.findOne(postId),
postUser = Meteor.users.findOne(post.userId),
timeSinceLastComment = timeSinceLast(user, Comments),
2013-04-26 17:28:09 +09:00
commentInterval = Math.abs(parseInt(getSetting('commentInterval',15))),
2014-08-04 09:47:10 +09:00
now = new Date();
// check that user can comment
2013-04-26 17:28:09 +09:00
if (!user || !canComment(user))
2013-11-11 22:07:19 +01:00
throw new Meteor.Error(i18n.t('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))
2013-11-11 22:07:19 +01:00
throw new Meteor.Error(704, i18n.t('Please wait ')+(commentInterval-timeSinceLastComment)+i18n.t(' seconds before commenting again'));
2012-10-05 13:59:40 +09:00
// Don't allow empty comments
if (!text)
2013-11-11 22:07:19 +01:00
throw new Meteor.Error(704,i18n.t('Your comment is empty.'));
2012-08-31 19:41:54 -04:00
var comment = {
2014-06-22 10:34:39 +09:00
postId: postId,
body: text,
2014-06-22 10:34:39 +09:00
userId: user._id,
2014-07-19 15:08:28 +09:00
createdAt: now,
postedAt: now,
2014-06-22 10:34:39 +09:00
upvotes: 0,
downvotes: 0,
baseScore: 0,
score: 0,
author: getDisplayName(user)
2012-08-31 19:41:54 -04:00
};
2013-04-26 17:28:09 +09:00
if(parentCommentId)
2014-07-05 16:21:28 +09:00
comment.parentCommentId = parentCommentId;
2012-08-31 19:41:54 -04:00
var newCommentId=Comments.insert(comment);
2013-10-23 10:21:08 +08:00
2013-11-08 11:10:23 +09:00
// increment comment count
Meteor.users.update({_id: user._id}, {
$inc: {'data.commentsCount': 1}
});
2013-11-08 11:10:23 +09:00
2013-11-03 13:23:11 +09:00
// extend comment with newly created _id
comment = _.extend(comment, {_id: newCommentId});
Posts.update(postId, {
$inc: {commentsCount: 1},
$set: {lastCommentedAt: now},
$addToSet: {commenters: user._id}
});
2013-11-03 13:23:11 +09:00
Meteor.call('upvoteComment', comment);
var notificationProperties = {
comment: _.pick(comment, '_id', 'userId', 'author', 'body'),
post: _.pick(post, '_id', 'title', 'url')
};
if(!this.isSimulation){
if(parentCommentId){
// child comment
var parentComment = Comments.findOne(parentCommentId);
var parentUser = Meteor.users.findOne(parentComment.userId);
notificationProperties.parentComment = _.pick(parentComment, '_id', 'userId', 'author');
2014-08-04 09:47:10 +09:00
// reply notification
// do not notify users of their own actions (i.e. they're replying to themselves)
2014-08-04 09:47:10 +09:00
if(parentUser._id != user._id)
createNotification('newReply', notificationProperties, parentUser);
// comment notification
// if the original poster is different from the author of the parent comment, notify them too
2014-08-04 09:47:10 +09:00
if(postUser._id != user._id && parentComment.userId != post.userId)
createNotification('newComment', notificationProperties, postUser);
2014-08-04 09:47:10 +09:00
}else{
// root comment
// don't notify users of their own comments
2014-08-04 09:47:10 +09:00
if(postUser._id != user._id)
createNotification('newComment', notificationProperties, postUser);
}
}
return comment;
},
removeComment: function(commentId){
var comment = Comments.findOne(commentId);
if(canEdit(Meteor.user(), comment)){
// decrement post comment count and remove user ID from post
Posts.update(comment.postId, {
$inc: {commentsCount: -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}, {
$inc: {'data.commentsCount': -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{
throwError("You don't have permission to delete this comment.");
}
2012-08-31 19:41:54 -04:00
}
});