mirror of
https://github.com/wassname/talk.git
synced 2026-07-25 13:30:59 +08:00
added support for deep comment counts
This commit is contained in:
@@ -25,5 +25,6 @@ plugins/*
|
||||
!plugins/talk-plugin-moderation-actions
|
||||
!plugins/talk-plugin-toxic-comments
|
||||
!plugins/talk-plugin-remember-sort
|
||||
!plugins/talk-plugin-deep-reply-count
|
||||
|
||||
node_modules
|
||||
|
||||
@@ -42,5 +42,6 @@ plugins/*
|
||||
!plugins/talk-plugin-moderation-actions
|
||||
!plugins/talk-plugin-toxic-comments
|
||||
!plugins/talk-plugin-remember-sort
|
||||
!plugins/talk-plugin-deep-reply-count
|
||||
|
||||
**/node_modules/*
|
||||
|
||||
@@ -369,7 +369,8 @@ type Comment {
|
||||
# the replies that were made to the comment.
|
||||
replies(query: RepliesQuery = {}): CommentConnection!
|
||||
|
||||
# The count of replies on a comment.
|
||||
# replyCount is the number of replies with a depth of 1. Only direct replies
|
||||
# to this comment are counted.
|
||||
replyCount: Int
|
||||
|
||||
# Actions completed on the parent. Requires the `ADMIN` role.
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
const _ = require('lodash');
|
||||
const DataLoader = require('dataloader');
|
||||
|
||||
const CommentModel = require('../../models/comment');
|
||||
|
||||
console.warn('Enabling the talk-plugin-deep-reply-count plugin introduces a signifigant performance impact on larger sites, use with care.');
|
||||
|
||||
// genDeepCommentCount will return the deep comment count for a given parent id.
|
||||
const genDeepCommentCount = async (context, parent_ids) => {
|
||||
|
||||
// Get all the replies to the parent comments.
|
||||
const replies = await CommentModel
|
||||
.find({
|
||||
parent_id: {
|
||||
$in: _.uniq(parent_ids),
|
||||
},
|
||||
}, {
|
||||
id: 1,
|
||||
reply_count: 1,
|
||||
parent_id: 1,
|
||||
});
|
||||
|
||||
// Get all the replies that have comments on them.
|
||||
const commentedOnReplies = replies.filter(({reply_count}) => {
|
||||
return reply_count && reply_count > 0;
|
||||
});
|
||||
|
||||
let deepReplyCount = [];
|
||||
|
||||
// And if there were any..
|
||||
if (commentedOnReplies.length > 0) {
|
||||
|
||||
// Load the reply count for each of them.
|
||||
deepReplyCount = await context.loaders.Comments.getDeepCount.loadMany(_.uniq(commentedOnReplies.map(({id}) => {
|
||||
return id;
|
||||
})));
|
||||
}
|
||||
|
||||
// Get all the direct replies to the parent comments.
|
||||
const allDirectReplies = _.groupBy(replies, 'parent_id');
|
||||
|
||||
// Collect all the ancestor replies.
|
||||
const allAncestorReplies = _.groupBy(_.zip(commentedOnReplies, deepReplyCount), ([{parent_id}]) => {
|
||||
return parent_id;
|
||||
});
|
||||
|
||||
// Return the replies in an array matching that of the input parent_ids array.
|
||||
return parent_ids.map((parent_id) => {
|
||||
|
||||
// Get the direct replies to this comment.
|
||||
const directReplies = parent_id in allDirectReplies ? allDirectReplies[parent_id] : [];
|
||||
const ancestorReplies = parent_id in allAncestorReplies ? allAncestorReplies[parent_id] : [];
|
||||
|
||||
// Reduce this array.
|
||||
return ancestorReplies.reduce((acc, [, count]) => {
|
||||
return acc + count;
|
||||
}, directReplies.length);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
loaders: (context) => ({
|
||||
Comments: {
|
||||
getDeepCount: new DataLoader((parent_ids) => genDeepCommentCount(context, parent_ids)),
|
||||
}
|
||||
}),
|
||||
resolvers: {
|
||||
Comment: {
|
||||
replyCount({id}, args, {loaders: {Comments}}) {
|
||||
return Comments.getDeepCount.load(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user