diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index 8360f88ce..b0475d3ef 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -50,13 +50,20 @@ const getCountsByAssetID = (context, asset_ids) => { * @param {Array} id The ID of the asset * @param {Array} excludeIgnored Exclude comments ignored by the requesting user */ -const getCountsByAssetIDPersonalized = async (context, {assetId, excludeIgnored}) => { +const getCountsByAssetIDPersonalized = async (context, {assetId, excludeIgnored, tags}) => { const query = { asset_id: assetId, status: { $in: ['NONE', 'ACCEPTED'], }, }; + + if (tags) { + query['tags.tag.name'] = { + $in: tags, + }; + } + const user = context.user; if (excludeIgnored && user) { @@ -108,7 +115,7 @@ const getParentCountsByAssetID = (context, asset_ids) => { * @param {Array} id The ID of the asset * @param {Array} excludeIgnored Exclude comments ignored by the requesting user */ -const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgnored}) => { +const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgnored, tags}) => { const query = { asset_id: assetId, parent_id: null, @@ -116,6 +123,13 @@ const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgn $in: ['NONE', 'ACCEPTED'], }, }; + + if (tags) { + query['tags.tag.name'] = { + $in: tags, + }; + } + const user = context.user; if (excludeIgnored && user) { @@ -195,7 +209,7 @@ const getCountByParentIDPersonalized = async (context, {id, excludeIgnored}) => * @return {Promise} resolves to the counts of the comments from the * query */ -const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id, author_id}) => { +const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id, author_id, tags}) => { let query = CommentModel.find(); if (ids) { @@ -218,6 +232,14 @@ const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id, au query = query.where({author_id}); } + if (tags) { + query = query.find({ + 'tags.tag.name': { + $in: tags, + }, + }); + } + return CommentModel .find(query) .count(); @@ -229,7 +251,7 @@ const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id, au * @param {Object} context graph context * @param {Object} query query terms to apply to the comments query */ -const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, author_id, limit, cursor, sort, excludeIgnored}) => { +const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, author_id, limit, cursor, sort, excludeIgnored, tags}) => { let comments = CommentModel.find(); // Only administrators can search for comments with statuses that are not @@ -256,6 +278,14 @@ const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, a }); } + if (tags) { + comments = comments.find({ + 'tags.tag.name': { + $in: tags, + }, + }); + } + // Only let an admin request any user or the current user request themself. if (user && (user.can(SEARCH_OTHERS_COMMENTS) || user.id === author_id) && author_id != null) { comments = comments.where({author_id}); diff --git a/graph/resolvers/asset.js b/graph/resolvers/asset.js index f6793fbf1..86e102631 100644 --- a/graph/resolvers/asset.js +++ b/graph/resolvers/asset.js @@ -4,31 +4,32 @@ const Asset = { recentComments({id}, _, {loaders: {Comments}}) { return Comments.genRecentComments.load(id); }, - comments({id}, {sort, limit, excludeIgnored}, {loaders: {Comments}}) { + comments({id}, {sort, limit, deep, excludeIgnored, tags}, {loaders: {Comments}}) { return Comments.getByQuery({ asset_id: id, sort, limit, - parent_id: null, + parent_id: deep ? undefined : null, + tags, excludeIgnored, }); }, - commentCount({id, commentCount}, {excludeIgnored}, {user, loaders: {Comments}}) { - + commentCount({id, commentCount}, {excludeIgnored, tags}, {user, loaders: {Comments}}) { + // TODO: remove - if (user && excludeIgnored) { - return Comments.parentCountByAssetIDPersonalized({assetId: id, excludeIgnored}); + if ((user && excludeIgnored) || tags) { + return Comments.parentCountByAssetIDPersonalized({assetId: id, excludeIgnored, tags}); } if (commentCount != null) { return commentCount; } return Comments.parentCountByAssetID.load(id); }, - totalCommentCount({id, totalCommentCount}, {excludeIgnored}, {user, loaders: {Comments}}) { + totalCommentCount({id, totalCommentCount}, {excludeIgnored, tags}, {user, loaders: {Comments}}) { // TODO: remove - if (user && excludeIgnored) { - return Comments.countByAssetIDPersonalized({assetId: id, excludeIgnored}); + if ((user && excludeIgnored) || tags) { + return Comments.countByAssetIDPersonalized({assetId: id, excludeIgnored, tags}); } if (totalCommentCount != null) { return totalCommentCount; diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 4a38ce266..b9febbf24 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -243,12 +243,12 @@ input CommentsQuery { # Skip results from the last created_at timestamp. cursor: Date - # Filter by a specific tag name. - tag: [String] - # Sort the results by created_at. sort: SORT_ORDER = REVERSE_CHRONOLOGICAL + # Filter by a specific tag name. + tags: [String!] + # Exclude comments ignored by the requesting user excludeIgnored: Boolean } @@ -278,7 +278,7 @@ input CommentCountQuery { author_id: ID # Filter by a specific tag name. - tag: [String] + tags: [String!] } type EditInfo { @@ -570,13 +570,19 @@ type Asset { recentComments: [Comment!] # The top level comments that are attached to the asset. - comments(sort: SORT_ORDER = REVERSE_CHRONOLOGICAL, limit: Int = 10, excludeIgnored: Boolean): CommentConnection! + comments( + sort: SORT_ORDER = REVERSE_CHRONOLOGICAL, + limit: Int = 10, + deep: Boolean, + excludeIgnored: Boolean, + tags: [String!] + ): CommentConnection! # The count of top level comments on the asset. - commentCount(excludeIgnored: Boolean): Int + commentCount(excludeIgnored: Boolean, tags: [String!]): Int # The total count of all comments made on the asset. - totalCommentCount(excludeIgnored: Boolean): Int + totalCommentCount(excludeIgnored: Boolean, tags: [String!]): Int # The settings (rectified with the global settings) that should be applied to # this asset.