Skip to content

Commit

Permalink
Fix stale names and usernames. (get from native)
Browse files Browse the repository at this point in the history
  • Loading branch information
sipec committed Nov 21, 2024
1 parent 5120006 commit d048201
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
46 changes: 28 additions & 18 deletions backend/api/src/get-users.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
import { toUserAPIResponse } from 'common/api/user-types'
import { run } from 'common/supabase/utils'
import { User } from 'common/user'
import { createSupabaseClient } from 'shared/supabase/init'
import { createSupabaseDirectClient } from 'shared/supabase/init'
import { APIError, type APIHandler } from './helpers/endpoint'
import {
select,
from,
limit as limitClause,
orderBy,
where,
renderSql,
} from 'shared/supabase/sql-builder'
import { convertUser } from 'common/supabase/users'

export const getUsers: APIHandler<'users'> = async ({ limit, before }) => {
const db = createSupabaseClient()
const pg = createSupabaseDirectClient()

const q = db
.from('users')
.select('data')
.limit(limit)
.order('created_time', { ascending: false })
const q = [
select('*'),
from('users'),
orderBy('created_time', 'desc'),
limitClause(limit),
]

if (before) {
const { data, error } = await db
.from('users')
.select('created_time')
.eq('id', before)
.single()
if (error) throw new APIError(404, `Could not find user with id: ${before}`)
q.lt('created_time', data.created_time)
const beforeUser = await pg.oneOrNone(
`select created_time from users where id = $1`,
[before]
)
if (!beforeUser)
throw new APIError(404, `Could not find user with id: ${before}`)

q.push(where('created_time < $1', beforeUser.created_time))
}

const { data } = await run(q)
return data.map((data) => toUserAPIResponse(data.data as unknown as User))
return await pg.map(renderSql(q), [], (r) =>
toUserAPIResponse(convertUser(r))
)
}
10 changes: 0 additions & 10 deletions common/src/supabase/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ export function selectJson<T extends TableName | ViewName>(
return db.from(table).select<string, { data: DataFor<T> }>('data')
}

export function selectFrom<
T extends TableName,
TData extends DataFor<T>,
TFields extends (string & keyof TData)[],
TResult = Pick<TData, TFields[number]>
>(db: SupabaseClient, table: T, ...fields: TFields) {
const query = fields.map((f) => `data->${f}`).join(', ')
return db.from(table).select<string, TResult>(query)
}

export function millisToTs(millis: number) {
return new Date(millis).toISOString()
}
Expand Down
22 changes: 14 additions & 8 deletions web/lib/supabase/referrals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { run, selectFrom } from 'common/supabase/utils'
import { run } from 'common/supabase/utils'
import { db } from 'web/lib/supabase/db'
import { User } from 'common/user'
export async function getReferrals(userId: string) {
const fields: (keyof User)[] = ['id', 'name', 'username', 'avatarUrl']
import { type DisplayUser } from 'common/api/user-types'

const query = selectFrom(db, 'users', ...fields).contains('data', {
referredByUserId: userId,
})
export async function getReferrals(userId: string) {
const { data } = await run(
db
.from('users')
.select(
`id, name, username, data->'avatarUrl', data->'isBannedFromPosting'`
)
.contains('data', {
referredByUserId: userId,
})
)

return (await run(query)).data
return data as unknown as DisplayUser[]
}
20 changes: 10 additions & 10 deletions web/lib/supabase/users.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { db } from './db'
import { run, selectFrom } from 'common/supabase/utils'
import { run } from 'common/supabase/utils'
import { type User } from 'common/user'
import { APIError, api } from '../api/api'
import { DAY_MS, WEEK_MS } from 'common/util/time'
import { HIDE_FROM_LEADERBOARD_USER_IDS } from 'common/envs/constants'
export type { DisplayUser } from 'common/api/user-types'

const defaultFields = ['id', 'name', 'username', 'avatarUrl'] as const
import type { DisplayUser } from 'common/api/user-types'
export type { DisplayUser }

export async function getUserById(id: string) {
return api('user/by-id/:id/lite', { id })
Expand Down Expand Up @@ -48,15 +47,16 @@ export async function searchUsers(prompt: string, limit: number) {
}

export async function getDisplayUsers(userIds: string[]) {
// note: random order
const { data } = await run(
selectFrom(db, 'users', ...defaultFields, 'isBannedFromPosting').in(
'id',
userIds
)
db
.from('users')
.select(
`id, name, username, data->'avatarUrl', data->'isBannedFromPosting'`
)
.in('id', userIds)
)

return data
return data as unknown as DisplayUser[]
}

// leaderboards
Expand Down

0 comments on commit d048201

Please sign in to comment.