From 59407e99d7e16cc3793601b9a253f7bbae4a8c01 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Mon, 26 Jun 2017 09:59:28 -0300 Subject: [PATCH 1/2] commentCount by asset_url --- graph/loaders/assets.js | 17 ++++++++++++++++- graph/resolvers/asset.js | 2 +- graph/resolvers/root_query.js | 15 ++++++++++++--- graph/typeDefs.graphql | 3 +++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/graph/loaders/assets.js b/graph/loaders/assets.js index 020eafbcb..b1be7dafb 100644 --- a/graph/loaders/assets.js +++ b/graph/loaders/assets.js @@ -63,18 +63,33 @@ const getAssetsForMetrics = async ({loaders: {Actions, Comments}}) => { .then((connection) => connection.nodes); }; +const findByUrl = async (context, asset_url) => { + + // Verify that the asset_url is parsable. + let parsed_asset_url = url.parse(asset_url); + if (!parsed_asset_url.protocol) { + throw errors.ErrInvalidAssetURL; + } + + let asset = await AssetsService.findByUrl(asset_url); + + return asset; +}; + /** * Creates a set of loaders based on a GraphQL context. * @param {Object} context the context of the GraphQL request * @return {Object} object of loaders */ + module.exports = (context) => ({ Assets: { // TODO: decide whether we want to move these to mutators or not, as in fact // this operation create a new asset if one isn't found. getByURL: (url) => findOrCreateAssetByURL(context, url), - + + findByUrl: (url) => findByUrl(context, url), search: (query) => getAssetsByQuery(context, query), getByID: new DataLoader((ids) => genAssetsByID(context, ids)), getForMetrics: () => getAssetsForMetrics(context), diff --git a/graph/resolvers/asset.js b/graph/resolvers/asset.js index 47e0a9541..f6793fbf1 100644 --- a/graph/resolvers/asset.js +++ b/graph/resolvers/asset.js @@ -14,7 +14,7 @@ const Asset = { }); }, commentCount({id, commentCount}, {excludeIgnored}, {user, loaders: {Comments}}) { - + // TODO: remove if (user && excludeIgnored) { return Comments.parentCountByAssetIDPersonalized({assetId: id, excludeIgnored}); diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index 9889b62f2..d608316b0 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -40,17 +40,26 @@ const RootQuery = { return Comments.get.load(id); }, - async commentCount(_, {query}, {user, loaders: {Actions, Comments}}) { + async commentCount(_, {query}, {user, loaders: {Actions, Comments, Assets}}) { + if (user == null || !user.can(SEARCH_OTHERS_COMMENTS)) { return null; } - - const {action_type} = query; + + const {action_type, asset_url} = query; if (action_type) { query.ids = await Actions.getByTypes({action_type, item_type: 'COMMENTS'}); } + if (asset_url) { + return Assets.findByUrl(asset_url) + .then((asset) => { + const newQuery = Object.assign({}, query, {asset_id: asset.id}); + return Comments.getCountByQuery(newQuery); + }); + } + return Comments.getCountByQuery(query); }, diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 8764a5f66..7801872a6 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -255,6 +255,9 @@ input CommentCountQuery { # Asset that a comment is on. asset_id: ID + # The URL that the asset is located on. + asset_url: String + # the parent of the comment that we want to retrieve. parent_id: ID From 2745245564ebdd0057f296cb2242b58eeec1893b Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Mon, 26 Jun 2017 14:39:10 -0300 Subject: [PATCH 2/2] updated --- graph/loaders/assets.js | 4 +--- graph/resolvers/root_query.js | 9 ++++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/graph/loaders/assets.js b/graph/loaders/assets.js index b1be7dafb..d3d1e40a8 100644 --- a/graph/loaders/assets.js +++ b/graph/loaders/assets.js @@ -71,9 +71,7 @@ const findByUrl = async (context, asset_url) => { throw errors.ErrInvalidAssetURL; } - let asset = await AssetsService.findByUrl(asset_url); - - return asset; + return AssetsService.findByUrl(asset_url); }; /** diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index d608316b0..8bb9d31d3 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -53,11 +53,10 @@ const RootQuery = { } if (asset_url) { - return Assets.findByUrl(asset_url) - .then((asset) => { - const newQuery = Object.assign({}, query, {asset_id: asset.id}); - return Comments.getCountByQuery(newQuery); - }); + let asset = await Assets.findByUrl(asset_url); + if (asset) { + query.asset_id = asset.id; + } } return Comments.getCountByQuery(query);