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

44 lines
1.2 KiB
JavaScript
Raw Normal View History

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) => {
const userId = comment.userId;
// dec comment count
Users.update({_id: userId}, {
$inc: {'__commentCount': -1}
});
// update post
Posts.update(comment.postId, {
2016-12-02 13:43:26 +01:00
$inc: {commentCount: -1},
// note: the others parameters should be queried by graphql from the post
// $set: {lastCommentedAt: new Date()},
// $pull: {commenters: userId}
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,
document: childComment,
currentUser: currentUser,
validate: false
});
});
return comment;
};
2016-12-02 13:43:26 +01:00
Telescope.callbacks.add("comments.remove.async", CommentsRemoveChildrenComments);