2016-11-29 19:33:13 +01:00
|
|
|
import Telescope, { removeMutation } from 'meteor/nova:lib';
|
|
|
|
import Posts from "meteor/nova:posts";
|
|
|
|
import Comments from '../collection.js';
|
|
|
|
import Users from 'meteor/nova:users';
|
|
|
|
|
|
|
|
const CommentsRemovePostCommenters = (comment, currentUser) => {
|
2016-12-06 09:45:36 +01:00
|
|
|
const { userId, postId } = comment;
|
2016-11-29 19:33:13 +01:00
|
|
|
|
2016-12-06 09:45:36 +01:00
|
|
|
// dec user's comment count
|
2016-11-29 19:33:13 +01:00
|
|
|
Users.update({_id: userId}, {
|
|
|
|
$inc: {'__commentCount': -1}
|
|
|
|
});
|
|
|
|
|
2016-12-06 09:45:36 +01:00
|
|
|
const postComments = Comments.find({postId}, {sort: {postedAt: -1}}).fetch();
|
|
|
|
|
|
|
|
const commenters = _.uniq(postComments.map(comment => comment.userId));
|
|
|
|
const lastCommentedAt = postComments[0] && postComments[0].postedAt;
|
|
|
|
|
|
|
|
// update post with a decremented comment count, a unique list of commenters and corresponding last commented at date
|
|
|
|
Posts.update(postId, {
|
2016-12-02 13:43:26 +01:00
|
|
|
$inc: {commentCount: -1},
|
2016-12-06 09:45:36 +01:00
|
|
|
$set: {lastCommentedAt, commenters},
|
2016-11-29 19:33:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return comment;
|
|
|
|
};
|
|
|
|
|
|
|
|
Telescope.callbacks.add("comments.remove.async", CommentsRemovePostCommenters);
|
|
|
|
|
|
|
|
const CommentsRemoveChildrenComments = (comment, currentUser) => {
|
|
|
|
|
|
|
|
const childrenComments = Comments.find({parentCommentId: comment._id}).fetch();
|
|
|
|
|
|
|
|
childrenComments.forEach(childComment => {
|
|
|
|
removeMutation({
|
|
|
|
action: 'comments.remove',
|
|
|
|
collection: Comments,
|
2016-12-06 09:45:36 +01:00
|
|
|
documentId: childComment._id,
|
2016-11-29 19:33:13 +01:00
|
|
|
currentUser: currentUser,
|
|
|
|
validate: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return comment;
|
|
|
|
};
|
|
|
|
|
2016-12-02 13:43:26 +01:00
|
|
|
Telescope.callbacks.add("comments.remove.async", CommentsRemoveChildrenComments);
|