diff --git a/src/server/api/endpoints/users/search_by_username.ts b/src/server/api/endpoints/users/search_by_username.ts index d347ba6056..1d8553baef 100644 --- a/src/server/api/endpoints/users/search_by_username.ts +++ b/src/server/api/endpoints/users/search_by_username.ts @@ -21,12 +21,36 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => let users = await User .find({ host: null, - usernameLower: new RegExp(escapeRegexp(query.toLowerCase())) + usernameLower: new RegExp('^' + escapeRegexp(query.toLowerCase())) }, { limit: limit, skip: offset }); + if (users.length < limit) { + const remoteUsers = await User + .find({ + host: { $ne: null }, + usernameLower: new RegExp('^' + escapeRegexp(query.toLowerCase())) + }, { + limit: limit - users.length + }); + + users = users.concat(remoteUsers); + } + + if (users.length < limit) { + const remoteUsers = await User + .find({ + host: null, + usernameLower: new RegExp(escapeRegexp(query.toLowerCase())) + }, { + limit: limit - users.length + }); + + users = users.concat(remoteUsers); + } + if (users.length < limit) { const remoteUsers = await User .find({