Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search users by username first #2764

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions backend/api/src/supabase-search-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
)
}
Loading