Vulcan/packages/vulcan-comments/lib/callbacks/callbacks_comments_remove.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-03-23 16:27:59 +09:00
import { removeMutation, addCallback } from 'meteor/vulcan:core';
import Posts from "meteor/vulcan:posts";
2016-11-29 19:33:13 +01:00
import Comments from '../collection.js';
2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users';
2016-11-29 19:33:13 +01:00
const CommentsRemovePostCommenters = (comment, currentUser) => {
const { userId, postId } = comment;
2016-11-29 19:33:13 +01:00
// dec user's comment count
2016-11-29 19:33:13 +01:00
Users.update({_id: userId}, {
$inc: {'commentCount': -1}
2016-11-29 19:33:13 +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},
$set: {lastCommentedAt, commenters},
2016-11-29 19:33:13 +01:00
});
return comment;
};
addCallback("comments.remove.async", CommentsRemovePostCommenters);
2016-11-29 19:33:13 +01:00
const CommentsRemoveChildrenComments = (comment, currentUser) => {
const childrenComments = Comments.find({parentCommentId: comment._id}).fetch();
childrenComments.forEach(childComment => {
removeMutation({
action: 'comments.remove',
collection: Comments,
documentId: childComment._id,
2016-11-29 19:33:13 +01:00
currentUser: currentUser,
validate: false
});
});
return comment;
};
addCallback("comments.remove.async", CommentsRemoveChildrenComments);