mirror of
https://github.com/wassname/talk.git
synced 2026-07-25 13:30:59 +08:00
Comment replyCount edge can now be filtered by notIgnoredBy
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
query LoadMoreComments($limit: Int = 5, $cursor: Date, $parent_id: ID, $asset_id: ID, $sort: SORT_ORDER, $notIgnoredBy: String) {
|
||||
new_top_level_comments: comments(query: {limit: $limit, cursor: $cursor, parent_id: $parent_id, asset_id: $asset_id, sort: $sort, notIgnoredBy: $notIgnoredBy}) {
|
||||
...commentView
|
||||
replyCount
|
||||
replyCount(notIgnoredBy: $notIgnoredBy)
|
||||
replies(limit: 3) {
|
||||
...commentView
|
||||
}
|
||||
|
||||
@@ -6,13 +6,13 @@ query AssetQuery($asset_id: ID, $asset_url: String, $comment_id: ID!, $has_comme
|
||||
# which is in turn pulled from the host page url
|
||||
comment(id: $comment_id) @include(if: $has_comment) {
|
||||
...commentView
|
||||
replyCount
|
||||
replyCount(notIgnoredBy: $notIgnoredBy)
|
||||
replies {
|
||||
...commentView
|
||||
}
|
||||
parent {
|
||||
...commentView
|
||||
replyCount
|
||||
replyCount(notIgnoredBy: $notIgnoredBy)
|
||||
replies {
|
||||
...commentView
|
||||
}
|
||||
@@ -40,7 +40,7 @@ query AssetQuery($asset_id: ID, $asset_url: String, $comment_id: ID!, $has_comme
|
||||
totalCommentCount
|
||||
comments(limit: 10, notIgnoredBy: $notIgnoredBy) {
|
||||
...commentView
|
||||
replyCount
|
||||
replyCount(notIgnoredBy: $notIgnoredBy)
|
||||
replies(limit: 3, notIgnoredBy: $notIgnoredBy) {
|
||||
...commentView
|
||||
}
|
||||
|
||||
@@ -105,6 +105,36 @@ const getCountsByParentID = (context, parent_ids) => {
|
||||
.then((results) => results.map((result) => result ? result.count : 0));
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the count of comments for the provided parent_id, also filtering by personalization options.
|
||||
*
|
||||
* @param {Array<String>} id The ID of the parent comment
|
||||
* @param {Array<String>} notIgnoredBy Exclude comments ignored by this User ID
|
||||
*/
|
||||
const getCountByParentIDPersonalized = async (context, {id, notIgnoredBy}) => {
|
||||
const query = {
|
||||
parent_id: {
|
||||
$in: [id]
|
||||
},
|
||||
status: {
|
||||
$in: ['NONE', 'ACCEPTED']
|
||||
}
|
||||
};
|
||||
if (notIgnoredBy) {
|
||||
const user = context.user;
|
||||
if (user.id !== notIgnoredBy) {
|
||||
throw new Error(`You are not authorized to query for comments counts notIgnoredBy ${notIgnoredBy}`);
|
||||
}
|
||||
|
||||
// load afresh, as `user` may be from cache and not have recent ignores
|
||||
const freshUser = await UsersService.findById(user.id);
|
||||
const ignoredUsers = freshUser.ignoresUsers;
|
||||
query.author_id = {$nin: ignoredUsers};
|
||||
}
|
||||
const count = await CommentModel.where(query).count();
|
||||
return count;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the count of comments based on the passed in query.
|
||||
* @param {Object} context graph context
|
||||
@@ -360,6 +390,7 @@ module.exports = (context) => ({
|
||||
countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', 3600, (ids) => getCountsByAssetID(context, ids)),
|
||||
parentCountByAssetID: new SharedCounterDataLoader('Comments.countByAssetID', 3600, (ids) => getParentCountsByAssetID(context, ids)),
|
||||
countByParentID: new SharedCounterDataLoader('Comments.countByParentID', 3600, (ids) => getCountsByParentID(context, ids)),
|
||||
countByParentIDPersonalized: (query) => getCountByParentIDPersonalized(context, query),
|
||||
genRecentReplies: new DataLoader((ids) => genRecentReplies(context, ids)),
|
||||
genRecentComments: new DataLoader((ids) => genRecentComments(context, ids))
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ const Comment = {
|
||||
return Comments.genRecentReplies.load(id);
|
||||
},
|
||||
replies({id, asset_id}, {sort, limit, notIgnoredBy}, {loaders: {Comments}}) {
|
||||
console.log('replies notIgnoredBy', notIgnoredBy);
|
||||
return Comments.getByQuery({
|
||||
asset_id,
|
||||
parent_id: id,
|
||||
@@ -22,8 +21,11 @@ const Comment = {
|
||||
notIgnoredBy,
|
||||
});
|
||||
},
|
||||
replyCount({id}, _, {loaders: {Comments}}) {
|
||||
return Comments.countByParentID.load(id);
|
||||
replyCount({id}, {notIgnoredBy}, {loaders: {Comments}}) {
|
||||
if ( ! notIgnoredBy) {
|
||||
return Comments.countByParentID.load(id);
|
||||
}
|
||||
return Comments.countByParentIDPersonalized({id, notIgnoredBy});
|
||||
},
|
||||
actions({id}, _, {user, loaders: {Actions}}) {
|
||||
|
||||
|
||||
@@ -191,7 +191,7 @@ type Comment {
|
||||
replies(sort: SORT_ORDER = CHRONOLOGICAL, limit: Int = 3, notIgnoredBy: String): [Comment]
|
||||
|
||||
# The count of replies on a comment.
|
||||
replyCount: Int
|
||||
replyCount(notIgnoredBy: String): Int
|
||||
|
||||
# Actions completed on the parent. Requires the `ADMIN` role.
|
||||
actions: [Action]
|
||||
|
||||
Reference in New Issue
Block a user