From ccac819eba82d3b1df6e1faae33a791c712801d6 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 17 Jun 2018 16:40:18 +0900 Subject: [PATCH] Improve user search --- src/server/api/endpoints/users/search.ts | 1 + .../api/endpoints/users/search_by_username.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/server/api/endpoints/users/search.ts b/src/server/api/endpoints/users/search.ts index cfbdc337b..95b9e4f4b 100644 --- a/src/server/api/endpoints/users/search.ts +++ b/src/server/api/endpoints/users/search.ts @@ -40,6 +40,7 @@ async function byNative(res, rej, me, query, offset, max) { // Search users const users = await User .find({ + host: null, $or: [{ usernameLower: new RegExp(escapedQuery.replace('@', '').toLowerCase()) }, { diff --git a/src/server/api/endpoints/users/search_by_username.ts b/src/server/api/endpoints/users/search_by_username.ts index 5927d00fa..7c9fbba0f 100644 --- a/src/server/api/endpoints/users/search_by_username.ts +++ b/src/server/api/endpoints/users/search_by_username.ts @@ -20,15 +20,27 @@ module.exports = (params, me) => new Promise(async (res, rej) => { const [limit = 10, limitErr] = $.num.optional().range(1, 100).get(params.limit); if (limitErr) return rej('invalid limit param'); - const users = await User + let users = await User .find({ + host: null, usernameLower: new RegExp(query.toLowerCase()) }, { limit: limit, skip: offset }); + if (users.length < limit) { + const remoteUsers = await User + .find({ + host: { $ne: null }, + usernameLower: new RegExp(query.toLowerCase()) + }, { + limit: limit - users.length + }); + + users = users.concat(remoteUsers); + } + // Serialize - res(await Promise.all(users.map(async user => - await pack(user, me, { detail: true })))); + res(await Promise.all(users.map(user => pack(user, me, { detail: true })))); });