mirror of
https://github.com/wassname/talk.git
synced 2026-07-20 12:40:47 +08:00
Merge branch 'master' into new-flags
This commit is contained in:
+36
-1
@@ -107,6 +107,40 @@ const getUsersByQuery = async ({user, loaders: {Actions}}, {ids, limit, cursor,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the count of users based on the passed in query.
|
||||
* @param {Object} context graph context
|
||||
* @param {Object} query query to execute against the users collection
|
||||
* to compute the counts
|
||||
* @return {Promise} resolves to the counts of the users from the
|
||||
* query
|
||||
*/
|
||||
const getCountByQuery = async ({loaders: {Actions}}, {action_type, statuses}) => {
|
||||
let query = UserModel.find();
|
||||
|
||||
if (action_type) {
|
||||
const userIds = await Actions.getByTypes({action_type, item_type: 'USERS'});
|
||||
|
||||
query = query.find({
|
||||
id: {
|
||||
$in: userIds
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (statuses) {
|
||||
query = query.where({
|
||||
status: {
|
||||
$in: statuses
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return UserModel
|
||||
.find(query)
|
||||
.count();
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a set of loaders based on a GraphQL context.
|
||||
* @param {Object} context the context of the GraphQL request
|
||||
@@ -115,6 +149,7 @@ const getUsersByQuery = async ({user, loaders: {Actions}}, {ids, limit, cursor,
|
||||
module.exports = (context) => ({
|
||||
Users: {
|
||||
getByQuery: (query) => getUsersByQuery(context, query),
|
||||
getByID: new DataLoader((ids) => genUserByIDs(context, ids))
|
||||
getByID: new DataLoader((ids) => genUserByIDs(context, ids)),
|
||||
getCountByQuery: (query) => getCountByQuery(context, query)
|
||||
}
|
||||
});
|
||||
|
||||
@@ -50,6 +50,14 @@ const RootQuery = {
|
||||
return Comments.getCountByQuery(query);
|
||||
},
|
||||
|
||||
async userCount(_, {query}, {user, loaders: {Users}}) {
|
||||
if (user == null || !user.can(SEARCH_OTHER_USERS)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Users.getCountByQuery(query);
|
||||
},
|
||||
|
||||
assetMetrics(_, query, {user, loaders: {Metrics: {Assets}}}) {
|
||||
if (user == null || !user.can(SEARCH_ASSETS)) {
|
||||
return null;
|
||||
|
||||
@@ -343,6 +343,18 @@ input CommentCountQuery {
|
||||
tags: [String!]
|
||||
}
|
||||
|
||||
# UserCountQuery allows the ability to query user counts by specific
|
||||
# methods.
|
||||
input UserCountQuery {
|
||||
|
||||
# comments returned will only be ones which have at least one action of this
|
||||
# type.
|
||||
action_type: ACTION_TYPE
|
||||
|
||||
# Current status of a user.
|
||||
statuses: [USER_STATUS]
|
||||
}
|
||||
|
||||
type EditInfo {
|
||||
edited: Boolean!
|
||||
editableUntil: Date
|
||||
@@ -840,6 +852,10 @@ type RootQuery {
|
||||
# expensive as it is not batched. Requires the `ADMIN` role.
|
||||
commentCount(query: CommentCountQuery!): Int
|
||||
|
||||
# Return the count of users satisfied by the query. Note that this edge is
|
||||
# expensive as it is not batched. This field is restricted.
|
||||
userCount(query: UserCountQuery!): Int
|
||||
|
||||
# The currently logged in user based on the request. Requires any logged in
|
||||
# role.
|
||||
me: User
|
||||
|
||||
Reference in New Issue
Block a user