added user query by value to graph

This commit is contained in:
Wyatt Johnson
2018-01-03 14:01:51 -07:00
parent 0572a1c97d
commit 797cea4f63
8 changed files with 52 additions and 146 deletions
+38 -10
View File
@@ -8,6 +8,7 @@ const {
} = require('../../perms/constants');
const UsersService = require('../../services/users');
const {escapeRegExp} = require('../../services/regex');
const UserModel = require('../../models/user');
const mergeState = (query, state) => {
@@ -72,14 +73,48 @@ const genUserByIDs = async (context, ids) => {
* @param {Object} context graph context
* @param {Object} query query terms to apply to the users query
*/
const getUsersByQuery = async ({user}, {ids, limit, cursor, state, action_type, sortOrder}) => {
const getUsersByQuery = async ({user}, {limit, cursor, value = '', state, action_type, sortOrder}) => {
let query = UserModel.find();
if (action_type || state) {
if (action_type || state || value.length > 0) {
if (!user || !user.can(SEARCH_OTHER_USERS)) {
return null;
}
if (value.length > 0) {
// Lowercase the search term and escape any regex characters.
value = escapeRegExp(value).toLowerCase();
// Compile the prefix search regex.
const $regex = new RegExp(`^${value}`);
// Merge in the regex params.
query.merge({
$or: [
// Search by a prefix match on the username.
{
lowercaseUsername: {
$regex,
},
},
// Search by a prefix match on the email address.
{
profiles: {
$elemMatch: {
id: {
$regex,
},
provider: 'local',
},
},
},
],
});
}
if (state) {
mergeState(query, state);
}
@@ -93,14 +128,6 @@ const getUsersByQuery = async ({user}, {ids, limit, cursor, state, action_type,
}
}
if (ids) {
query = query.find({
id: {
$in: ids
}
});
}
if (cursor) {
if (sortOrder === 'DESC') {
query = query.where({
@@ -125,6 +152,7 @@ const getUsersByQuery = async ({user}, {ids, limit, cursor, state, action_type,
// Sort by created_at.
query.sort({created_at: sortOrder === 'DESC' ? -1 : 1});
// Execute the query.
const nodes = await query.exec();
// The hasNextPage is always handled the same (ask for one more than we need,
+1 -1
View File
@@ -78,7 +78,7 @@ const RootQuery = {
// This endpoint is used for loading the user moderation queues (users whose username has been flagged),
// so hide it in the event that we aren't an admin.
async users(_, {query}, {user, loaders: {Users}}) {
users(_, {query}, {user, loaders: {Users}}) {
if (user == null || !user.can(SEARCH_OTHER_USERS)) {
return null;
}
+2 -3
View File
@@ -50,7 +50,7 @@ const User = {
return tokens;
},
async ignoredUsers({id}, args, {user, loaders: {Users}}) {
ignoredUsers({id}, args, {user, loaders: {Users}}) {
// Only allow a logged in user that is either the current user or is a staff
// member to access the ignoredUsers of a given user.
@@ -63,8 +63,7 @@ const User = {
return [];
}
const connection = await Users.getByQuery({ids: user.ignoresUsers});
return connection.nodes;
return Users.getByID.loadMany(user.ignoresUsers);
},
role({id, role}, _, {user}) {
+4
View File
@@ -233,8 +233,12 @@ input UsersQuery {
# Users returned will only be ones which have at least one action of this.
action_type: ACTION_TYPE
# state will filter the users to a specific set of users that meet.
state: UserStateInput
# value is the search string to use to search for a pa
value: String = ""
# Limit the number of results to be returned.
limit: Int = 10