From eec0e18600da1a3b1cbe64313cfe039992ef6888 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 21 Aug 2017 16:05:35 -0600 Subject: [PATCH] bug fixes, removed dead code --- graph/loaders/comments.js | 35 +---------------------------------- graph/mutators/comment.js | 8 ++------ graph/resolvers/asset.js | 8 ++++++-- 3 files changed, 9 insertions(+), 42 deletions(-) diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index ffb52ea86..170f0ed4b 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -79,38 +79,6 @@ const getParentCountsByAssetID = (context, asset_ids) => { .then((results) => results.map((result) => result ? result.count : 0)); }; -/** - * Returns the comment count for all comments that are public based on their - * parent ids. - * - * @param {Array} parent_ids the ids of parents for which there are - * comments that we want to get - */ -const getCountsByParentID = (context, parent_ids) => { - return CommentModel.aggregate([ - { - $match: { - parent_id: { - $in: parent_ids - }, - status: { - $in: ['NONE', 'ACCEPTED'] - } - } - }, - { - $group: { - _id: '$parent_id', - count: { - $sum: 1 - } - } - } - ]) - .then(singleJoinBy(parent_ids, '_id')) - .then((results) => results.map((result) => result ? result.count : 0)); -}; - /** * Retrieves the count of comments based on the passed in query. * @param {Object} context graph context @@ -311,7 +279,6 @@ module.exports = (context) => ({ getByQuery: (query) => getCommentsByQuery(context, query), getCountByQuery: (query) => getCommentCountByQuery(context, query), countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getCountsByAssetID(context, ids)), - parentCountByAssetID: new SharedCounterDataLoader('Comments.countByAssetID', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getParentCountsByAssetID(context, ids)), - countByParentID: new SharedCounterDataLoader('Comments.countByParentID', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getCountsByParentID(context, ids)), + parentCountByAssetID: new SharedCounterDataLoader('Comments.countByAssetID', ms(CACHE_EXPIRY_COMMENT_COUNT), (ids) => getParentCountsByAssetID(context, ids)) } }); diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index b32b8ed79..3f9e72fc3 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -174,9 +174,7 @@ const createComment = async (context, {tags = [], body, asset_id, parent_id = nu // perform these increments in the event that we do have a new comment that // is approved or without a comment. if (status === 'NONE' || status === 'APPROVED') { - if (parent_id != null) { - Comments.countByParentID.incr(parent_id); - } else { + if (parent_id === null) { Comments.parentCountByAssetID.incr(asset_id); } Comments.countByAssetID.incr(asset_id); @@ -338,9 +336,7 @@ const setStatus = async ({user, loaders: {Comments}, pubsub}, {id, status}) => { // be nice if we could decrement the counters here, but that would result // in us having to know the initial state of the comment, which would // require another database query. - if (comment.parent_id != null) { - Comments.countByParentID.clear(comment.parent_id); - } else { + if (comment.parent_id === null) { Comments.parentCountByAssetID.clear(comment.asset_id); } diff --git a/graph/resolvers/asset.js b/graph/resolvers/asset.js index f5b3f04bc..48403ddd0 100644 --- a/graph/resolvers/asset.js +++ b/graph/resolvers/asset.js @@ -39,7 +39,7 @@ const Asset = { return Comments.getCountByQuery({ tags, asset_id: id, - parent_id: id, + parent_id: null, statuses: ['NONE', 'ACCEPTED'], }); } @@ -55,7 +55,11 @@ const Asset = { if (tags && tags.length > 0) { // Then count the comments with those tags. - return Comments.getCountByQuery({tags, asset_id: id, statuses: ['NONE', 'ACCEPTED']}); + return Comments.getCountByQuery({ + tags, + asset_id: id, + statuses: ['NONE', 'ACCEPTED'], + }); } return Comments.countByAssetID.load(id);