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

Filter banned users from leaderboards #3214

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
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
43 changes: 36 additions & 7 deletions backend/api/src/get-leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export const getLeaderboard: APIHandler<'leaderboard'> = async ({

if (kind === 'referral') {
const data = await pg.any(
`select id, total_referrals, total_referred_profit, total_referred_cash_profit
from user_referrals_profit limit $1`,
`select ur.id, ur.total_referrals, ur.total_referred_profit, ur.total_referred_cash_profit
from user_referrals_profit ur
join users u on ur.id = u.id
where coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true
limit $1`,
[limitValue]
)

Expand All @@ -41,13 +44,17 @@ export const getLeaderboard: APIHandler<'leaderboard'> = async ({
if ((kind == 'profit' || kind == 'loss') && !groupId) {
const query = renderSql(
from('user_portfolio_history_latest uph'),
join('users u on u.id = uph.user_id'),
select('uph.user_id as user_id'),
token === 'MANA'
? select('uph.profit as score') // excludes unranked
: select(
'uph.cash_balance + uph.cash_investment_value - uph.total_cash_deposits as score'
),
where('user_id not in ($1:list)', [HIDE_FROM_LEADERBOARD_USER_IDS]),
where(
`coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true`
),
orderBy(kind === 'loss' ? 'score asc' : 'score desc nulls last'),
limit(limitValue)
)
Expand All @@ -56,17 +63,39 @@ export const getLeaderboard: APIHandler<'leaderboard'> = async ({
score: r.score,
}))
}
if (kind === 'creator') {
const query = renderSql(
from('contracts c'),
join('users u on u.id = c.creator_id'),
select(
`c.creator_id as user_id, sum((c.data->'uniqueBettorCount')::bigint) as score`
),
groupBy('c.creator_id'),
where(`coalesce((c.data->'isRanked')::boolean, true) = true`), // unranked included in portfolio
where(
`coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true`
),
where('c.token = ${token}', { token }),
where('c.outcome_type != ${outcomeType}', { outcomeType: 'POLL' }),
where('c.outcome_type != ${outcomeType}', { outcomeType: 'BOUNTY' }),
orderBy('score desc nulls last'),
limit(limitValue)
)
return await pg.map(query, [], (r) => ({
userId: r.user_id,
score: r.score,
}))
}

const query = renderSql(
from('contracts c'),
join('user_contract_metrics ucm on ucm.contract_id = c.id'),
join('users u on u.id = ucm.user_id'),
where('ucm.answer_id is null'),
where(`coalesce((c.data->'isRanked')::boolean, true) = true`),

kind === 'creator' && [
select('c.creator_id as user_id, count(*) as score'),
groupBy('c.creator_id'),
],
where(
`coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true`
),

(kind === 'profit' || kind === 'loss') && [
select(`user_id, sum(profit) as score`),
Expand Down
5 changes: 4 additions & 1 deletion web/pages/leaderboards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ const LEADERBOARD_TYPES = [

type LeaderboardType = (typeof LEADERBOARD_TYPES)[number]['value']

type Entry = LeaderboardEntry & { totalReferredProfit?: number }
type Entry = LeaderboardEntry & {
totalReferredProfit?: number
isBannedFromPosting?: boolean
}
type MyEntry = Omit<Entry, 'userId'>
type MyScores = {
[key in LeaderboardType]?: { mana: MyEntry; cash: MyEntry }
Expand Down
Loading