mirror of
https://github.com/wassname/talk.git
synced 2026-09-11 12:51:33 +08:00
Added loader, graphql
This commit is contained in:
@@ -4,6 +4,52 @@ const {objectCacheKeyFn} = require('./util');
|
||||
|
||||
const ActionModel = require('../../models/action');
|
||||
|
||||
/**
|
||||
* Returns the assets which have had comments made within the last time period.
|
||||
*/
|
||||
const getCommentActivityMetrics = ({loaders: {Assets, Comments}}, {from, to, limit}) => {
|
||||
let assetMetrics = [];
|
||||
|
||||
return Comments.aggregate([
|
||||
{$match: {
|
||||
parent_id: null,
|
||||
created_at: {
|
||||
$gt: from,
|
||||
$lt: to
|
||||
}
|
||||
}},
|
||||
{$group: {
|
||||
_id: '$asset_id',
|
||||
commentCount: {
|
||||
$sum: 1
|
||||
}
|
||||
}},
|
||||
{$project: {
|
||||
_id: false,
|
||||
asset_id: '$_id',
|
||||
commentCount: '$commentCount'
|
||||
}},
|
||||
{$sort: {
|
||||
commentCount: -1
|
||||
}},
|
||||
{$limit: limit}
|
||||
])
|
||||
.then((results) => {
|
||||
results = assetMetrics;
|
||||
|
||||
return Assets.getByID.loadMany(results.map((result) => result.asset_id));
|
||||
})
|
||||
.then((assets) => assets.map((asset, i) => {
|
||||
|
||||
// We're leveraging the fact that the comments returned by the aggregation
|
||||
// query are in the request order that we just made, it's what the
|
||||
// Assets.getByID loader does.
|
||||
asset.commentCount = assetMetrics[i].commentCount;
|
||||
|
||||
return asset;
|
||||
}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a list of assets with action metadata included on the models.
|
||||
*/
|
||||
@@ -211,7 +257,8 @@ module.exports = (context) => ({
|
||||
get: ({from, to, sort, limit}) => getAssetMetrics(context, {from, to, sort, limit})
|
||||
},
|
||||
Comments: {
|
||||
get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit})
|
||||
get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit}),
|
||||
getActivity: ({from, to, limit}) => getCommentActivityMetrics(context, {from, to, limit}),
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -10,7 +10,11 @@ const Asset = {
|
||||
parent_id: null
|
||||
});
|
||||
},
|
||||
commentCount({id}, _, {loaders: {Comments}}) {
|
||||
commentCount({id, commentCount}, _, {loaders: {Comments}}) {
|
||||
if (commentCount) {
|
||||
return commentCount;
|
||||
}
|
||||
|
||||
return Comments.countByAssetID.load(id);
|
||||
},
|
||||
settings({settings = null}, _, {loaders: {Settings}}) {
|
||||
|
||||
@@ -71,6 +71,10 @@ const RootQuery = {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (sort === 'COMMENTS') {
|
||||
return Comments.getActivity({from, to, limit});
|
||||
}
|
||||
|
||||
return Comments.get({from, to, sort, limit});
|
||||
},
|
||||
|
||||
|
||||
+18
-3
@@ -476,6 +476,21 @@ enum USER_STATUS {
|
||||
APPROVED
|
||||
}
|
||||
|
||||
enum COMMENT_METRICS_SORT {
|
||||
|
||||
# Represents a LikeAction.
|
||||
LIKE
|
||||
|
||||
# Represents a FlagAction.
|
||||
FLAG
|
||||
|
||||
# Represents a don't agree action.
|
||||
DONTAGREE
|
||||
|
||||
# Represents activity.
|
||||
ACTIVITY
|
||||
}
|
||||
|
||||
type RootQuery {
|
||||
|
||||
# Site wide settings and defaults.
|
||||
@@ -507,7 +522,7 @@ type RootQuery {
|
||||
|
||||
# Comment metrics related to user actions are saturated into the comments
|
||||
# returned.
|
||||
commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment!]
|
||||
commentMetrics(from: Date!, to: Date!, sort: COMMENT_METRICS_SORT!, limit: Int = 10): [Comment!]
|
||||
}
|
||||
|
||||
################################################################################
|
||||
@@ -646,14 +661,14 @@ type SetCommentStatusResponse implements Response {
|
||||
type AddCommentTagResponse implements Response {
|
||||
# An array of errors relating to the mutation that occured.
|
||||
comment: Comment
|
||||
errors: [UserError]
|
||||
errors: [UserError]
|
||||
}
|
||||
|
||||
# Response to removeCommentTag mutation
|
||||
type RemoveCommentTagResponse implements Response {
|
||||
# An array of errors relating to the mutation that occured.
|
||||
comment: Comment
|
||||
errors: [UserError]
|
||||
errors: [UserError]
|
||||
}
|
||||
|
||||
# All mutations for the application are defined on this object.
|
||||
|
||||
Reference in New Issue
Block a user