initial pagination rewrite, cleanups to comment edit

This commit is contained in:
Wyatt Johnson
2017-05-11 17:27:02 -06:00
parent 5c5d014e23
commit d497ad3369
14 changed files with 721 additions and 743 deletions
+14 -17
View File
@@ -25,34 +25,31 @@ const genAssetsByID = (context, ids) => AssetModel.find({
* @param {String} asset_url the url passed in from the query
* @returns {Promise} resolves to the asset
*/
const findOrCreateAssetByURL = (context, asset_url) => {
const findOrCreateAssetByURL = async (context, asset_url) => {
// Verify that the asset_url is parsable.
let parsed_asset_url = url.parse(asset_url);
if (!parsed_asset_url.protocol) {
return Promise.reject(errors.ErrInvalidAssetURL);
throw errors.ErrInvalidAssetURL;
}
return AssetsService.findOrCreateByUrl(asset_url)
.then((asset) => {
let asset = await AssetsService.findOrCreateByUrl(asset_url);
// If the asset wasn't scraped before, scrape it! Otherwise just return
// the asset.
if (!asset.scraped) {
return scraper.create(asset).then(() => asset);
}
// If the asset wasn't scraped before, scrape it! Otherwise just return
// the asset.
if (!asset.scraped) {
await scraper.create(asset);
}
return asset;
});
return asset;
};
const getAssetsForMetrics = ({loaders: {Actions, Comments}}) => {
return Actions.getByTypes({action_type: 'FLAG', item_type: 'COMMENT'})
.then((actions) => { // ALL ACTIONS :O
const ids = actions.map(({item_id}) => item_id);
const getAssetsForMetrics = async ({loaders: {Actions, Comments}}) => {
let actions = await Actions.getByTypes({action_type: 'FLAG', item_type: 'COMMENT'});
return Comments.getByQuery({ids});
});
const ids = actions.map(({item_id}) => item_id);
return Comments.getByQuery({ids});
};
/**
+26 -8
View File
@@ -120,8 +120,8 @@ const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgn
const ignoredUsers = freshUser.ignoresUsers;
query.author_id = {$nin: ignoredUsers};
}
const count = await CommentModel.where(query).count();
return count;
return CommentModel.where(query).count();
};
/**
@@ -263,13 +263,9 @@ const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, a
comments = comments.where({parent_id});
}
if (excludeIgnored && user) {
// load afresh, as `user` may be from cache and not have recent ignores
const freshUser = await UsersService.findById(user.id);
const ignoredUsers = freshUser.ignoresUsers;
if (excludeIgnored && user && user.ignoresUsers) {
comments = comments.where({
author_id: {$nin: ignoredUsers}
author_id: {$nin: user.ignoresUsers}
});
}
@@ -294,6 +290,27 @@ const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, a
.limit(limit);
};
const getCommentsConnection = async ({user}, query) => {
let {limit} = query;
// Increate the limit by one.
query.limit++;
let comments = await getCommentsByQuery({user}, query);
if (!comments) {
comments = [];
}
return {
edges: comments.slice(0, limit),
pageInfo: {
hasNextPage: Boolean(comments.length > limit),
cursor: comments.length > 0 ? comments[comments.length - 1].created_at : null
}
};
};
/**
* Gets the recent replies.
* @param {Object} context graph context
@@ -431,6 +448,7 @@ module.exports = (context) => ({
Comments: {
get: new DataLoader((ids) => genComments(context, ids)),
getByQuery: (query) => getCommentsByQuery(context, query),
getConnection: (query) => getCommentsConnection(context, query),
getCountByQuery: (query) => getCommentCountByQuery(context, query),
countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', 3600, (ids) => getCountsByAssetID(context, ids)),
countByAssetIDPersonalized: (query) => getCountsByAssetIDPersonalized(context, query),