-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stale names and usernames. (get from native)
- Loading branch information
Showing
4 changed files
with
52 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters