diff --git a/client/coral-framework/graphql/queries/loadMore.graphql b/client/coral-framework/graphql/queries/loadMore.graphql index 6b15117da..d4ee818a1 100644 --- a/client/coral-framework/graphql/queries/loadMore.graphql +++ b/client/coral-framework/graphql/queries/loadMore.graphql @@ -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 } diff --git a/client/coral-framework/graphql/queries/streamQuery.graphql b/client/coral-framework/graphql/queries/streamQuery.graphql index a5e3982c6..9b95a9c2b 100644 --- a/client/coral-framework/graphql/queries/streamQuery.graphql +++ b/client/coral-framework/graphql/queries/streamQuery.graphql @@ -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 } diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index 684821345..13aa47fb4 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -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} id The ID of the parent comment + * @param {Array} 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)) } diff --git a/graph/resolvers/comment.js b/graph/resolvers/comment.js index b99aeeee5..1f57cb939 100644 --- a/graph/resolvers/comment.js +++ b/graph/resolvers/comment.js @@ -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}}) { diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 6bc68c599..1980a3bbc 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -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]