2013-04-13 15:05:18 +09:00
|
|
|
Comments = new Meteor.Collection('comments');
|
|
|
|
|
2014-05-10 16:57:17 +09:00
|
|
|
// Comments = new Meteor.Collection("comments", {
|
|
|
|
// schema: new SimpleSchema({
|
|
|
|
// body: {
|
|
|
|
// type: String,
|
|
|
|
// },
|
|
|
|
// baseScore: {
|
|
|
|
// type: Number
|
|
|
|
// },
|
|
|
|
// score: {
|
|
|
|
// type: Number
|
|
|
|
// }
|
|
|
|
// baseScore: {
|
|
|
|
// type: Number
|
|
|
|
// },
|
|
|
|
// upvotes: {
|
|
|
|
// type: Number
|
|
|
|
// },
|
|
|
|
// upvoters: {
|
|
|
|
// type: []
|
|
|
|
// },
|
|
|
|
// downvotes: {
|
|
|
|
// type: Number
|
|
|
|
// },
|
|
|
|
// downvoters: {
|
|
|
|
// type: []
|
|
|
|
// }
|
|
|
|
// score: {
|
|
|
|
// type: Number
|
|
|
|
// },
|
|
|
|
// author: {
|
|
|
|
// type: String
|
|
|
|
// },
|
|
|
|
// inactive: {
|
|
|
|
// type: Boolean
|
|
|
|
// },
|
|
|
|
// createdAt: {
|
|
|
|
// type: Date
|
|
|
|
// },
|
|
|
|
// postId: {
|
|
|
|
// type: "???"
|
|
|
|
// },
|
|
|
|
// userId: {
|
|
|
|
// type: "???"
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// });
|
|
|
|
|
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({
|
|
|
|
insert: canCommentById,
|
|
|
|
update: canEditById,
|
|
|
|
remove: canEditById
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-31 19:41:54 -04:00
|
|
|
Meteor.methods({
|
2012-10-01 11:45:18 +09:00
|
|
|
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),
|
|
|
|
cleanText= cleanUp(text),
|
|
|
|
commentInterval = Math.abs(parseInt(getSetting('commentInterval',15))),
|
|
|
|
properties={
|
|
|
|
'commentAuthorId': user._id,
|
|
|
|
'commentAuthorName': getDisplayName(user),
|
|
|
|
'commentExcerpt': trimWords(stripMarkdown(cleanText),20),
|
|
|
|
'postId': postId,
|
|
|
|
'postHeadline' : post.headline
|
|
|
|
};
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// 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.'));
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user waits more than 15 seconds between comments
|
2012-12-20 21:17:07 +01:00
|
|
|
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
|
|
|
|
2013-07-10 22:08:25 +02:00
|
|
|
// Don't allow empty comments
|
|
|
|
if (!cleanText)
|
2013-11-11 22:07:19 +01:00
|
|
|
throw new Meteor.Error(704,i18n.t('Your comment is empty.'));
|
2013-07-10 22:08:25 +02:00
|
|
|
|
2012-08-31 19:41:54 -04:00
|
|
|
var comment = {
|
2013-04-26 17:28:09 +09:00
|
|
|
post: postId,
|
|
|
|
body: cleanText,
|
|
|
|
userId: user._id,
|
|
|
|
submitted: new Date().getTime(),
|
|
|
|
author: getDisplayName(user)
|
2012-08-31 19:41:54 -04:00
|
|
|
};
|
2013-04-26 17:28:09 +09:00
|
|
|
|
2012-10-01 11:45:18 +09:00
|
|
|
if(parentCommentId)
|
|
|
|
comment.parent = parentCommentId;
|
2012-08-31 19:41:54 -04:00
|
|
|
|
2012-10-01 11:45:18 +09: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
|
2013-11-08 11:23:18 +09:00
|
|
|
Meteor.users.update({_id: user._id}, {$inc: {commentCount: 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});
|
|
|
|
|
2012-10-01 11:45:18 +09:00
|
|
|
Posts.update(postId, {$inc: {comments: 1}});
|
2012-09-20 08:09:45 +09:00
|
|
|
|
2013-11-03 13:23:11 +09:00
|
|
|
Meteor.call('upvoteComment', comment);
|
2012-10-01 11:45:18 +09:00
|
|
|
|
2013-01-19 18:24:18 +09:00
|
|
|
properties.commentId = newCommentId;
|
2012-10-08 11:23:12 +09:00
|
|
|
|
|
|
|
if(!this.isSimulation){
|
|
|
|
if(parentCommentId){
|
|
|
|
// child comment
|
2013-03-14 08:32:21 +09:00
|
|
|
var parentComment=Comments.findOne(parentCommentId);
|
|
|
|
var parentUser=Meteor.users.findOne(parentComment.userId);
|
2012-10-08 11:23:12 +09:00
|
|
|
|
2013-01-19 18:24:18 +09:00
|
|
|
properties.parentCommentId = parentCommentId;
|
|
|
|
properties.parentAuthorId = parentComment.userId;
|
|
|
|
properties.parentAuthorName = getDisplayName(parentUser);
|
|
|
|
|
2014-01-05 18:44:13 +02:00
|
|
|
if(!this.isSimulation){
|
|
|
|
// reply notification
|
|
|
|
// do not notify users of their own actions (i.e. they're replying to themselves)
|
|
|
|
if(parentUser._id != user._id){
|
|
|
|
createNotification({
|
|
|
|
event: 'newReply',
|
|
|
|
properties: properties,
|
|
|
|
userToNotify: parentUser,
|
|
|
|
userDoingAction: user,
|
|
|
|
sendEmail: getUserSetting('notifications.replies', false, parentUser)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// comment notification
|
|
|
|
// if the original poster is different from the author of the parent comment, notify them too
|
|
|
|
if(postUser._id != user._id && parentComment.userId != post.userId){
|
|
|
|
createNotification({
|
|
|
|
event: 'newComment',
|
|
|
|
properties: properties,
|
|
|
|
userToNotify: postUser,
|
|
|
|
userDoingAction: user,
|
|
|
|
sendEmail: getUserSetting('notifications.comments', false, postUser)
|
|
|
|
});
|
|
|
|
}
|
2013-10-23 20:20:10 +09:00
|
|
|
}
|
2012-10-08 11:23:12 +09:00
|
|
|
}else{
|
2014-01-05 18:44:13 +02:00
|
|
|
if(!this.isSimulation){
|
|
|
|
// root comment
|
|
|
|
// don't notify users of their own comments
|
|
|
|
if(postUser._id != user._id){
|
|
|
|
createNotification({
|
|
|
|
event: 'newComment',
|
|
|
|
properties: properties,
|
|
|
|
userToNotify: postUser,
|
|
|
|
userDoingAction: Meteor.user(),
|
|
|
|
sendEmail: getUserSetting('notifications.comments', false, postUser)
|
|
|
|
});
|
|
|
|
}
|
2013-10-23 20:20:10 +09:00
|
|
|
}
|
2012-10-08 11:23:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return properties;
|
2012-10-03 15:59:03 +09:00
|
|
|
},
|
|
|
|
removeComment: function(commentId){
|
|
|
|
var comment=Comments.findOne(commentId);
|
2013-10-04 19:51:34 +09:00
|
|
|
if(canEdit(Meteor.user(), comment)){
|
|
|
|
// decrement post comment count
|
|
|
|
Posts.update(comment.post, {$inc: {comments: -1}});
|
2013-11-08 11:10:23 +09:00
|
|
|
|
|
|
|
// decrement user comment count
|
|
|
|
Meteor.users.update({_id: comment.userId}, {$inc: {commentCount: -1}});
|
|
|
|
|
2013-10-04 19:51:34 +09:00
|
|
|
// note: should we also decrease user's comment karma ?
|
|
|
|
Comments.remove(commentId);
|
|
|
|
}else{
|
|
|
|
throwError("You don't have permission to delete this comment.");
|
|
|
|
}
|
2012-08-31 19:41:54 -04:00
|
|
|
}
|
|
|
|
});
|