diff --git a/backend/api/src/supabase-search-users.ts b/backend/api/src/supabase-search-users.ts index d6098380e1..0eef3fcd42 100644 --- a/backend/api/src/supabase-search-users.ts +++ b/backend/api/src/supabase-search-users.ts @@ -21,14 +21,16 @@ export const searchUsers: APIHandler<'search-users'> = async (props, auth) => { const offset = page * limit const userId = auth?.uid + const searchExactSQL = getSearchExactUserSQL({ term }) const searchFollowersSQL = getSearchUserSQL({ term, offset, limit, userId }) const searchAllSQL = getSearchUserSQL({ term, offset, limit }) - const [followers, all] = await Promise.all([ + const [exact, followers, all] = await Promise.all([ + pg.map(searchExactSQL, null, convertUser), pg.map(searchFollowersSQL, null, convertUser), pg.map(searchAllSQL, null, convertUser), ]) - return uniqBy([...followers, ...all], 'id') + return uniqBy([...exact, ...followers, ...all], 'id') .map(toUserAPIResponse) .slice(0, limit) } @@ -68,3 +70,16 @@ function getSearchUserSQL(props: { limit(props.limit, props.offset) ) } + +function getSearchExactUserSQL(props: { + term: string +}) { + const { term } = props + + return renderSql( + select('*'), + from('users'), + where(`username = $1`, [term]), + limit(1) + ) +}