mirror of
https://github.com/wassname/talk.git
synced 2026-08-01 13:00:55 +08:00
added user query by value to graph
This commit is contained in:
+38
-10
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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}) {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user