diff --git a/graph/loaders/assets.js b/graph/loaders/assets.js index 020eafbcb..d3d1e40a8 100644 --- a/graph/loaders/assets.js +++ b/graph/loaders/assets.js @@ -63,18 +63,31 @@ 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; + } + + return AssetsService.findByUrl(asset_url); +}; + /** * 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..8bb9d31d3 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -40,17 +40,25 @@ 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) { + let asset = await Assets.findByUrl(asset_url); + if (asset) { + query.asset_id = asset.id; + } + } + 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