From 7aa14524d154bee4fcf8ed452c66ff58bc25c5c2 Mon Sep 17 00:00:00 2001 From: David Chee Date: Thu, 12 Dec 2024 13:53:08 -0800 Subject: [PATCH 1/2] Filter banned users --- backend/api/src/get-leaderboard.ts | 13 ++++++++++--- web/pages/leaderboards.tsx | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/api/src/get-leaderboard.ts b/backend/api/src/get-leaderboard.ts index 388514f353..6d869df4ef 100644 --- a/backend/api/src/get-leaderboard.ts +++ b/backend/api/src/get-leaderboard.ts @@ -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] ) @@ -41,13 +44,15 @@ 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.profit as score') : 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) ) @@ -60,8 +65,10 @@ export const getLeaderboard: APIHandler<'leaderboard'> = async ({ const query = renderSql( from('contracts c'), join('user_contract_metrics ucm on ucm.contract_id = c.id'), + join('users u on u.id = ' + (kind === 'creator' ? 'c.creator_id' : 'ucm.user_id')), where('ucm.answer_id is null'), where(`coalesce((c.data->'isRanked')::boolean, true) = true`), + where(`coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true`), kind === 'creator' && [ select('c.creator_id as user_id, count(*) as score'), diff --git a/web/pages/leaderboards.tsx b/web/pages/leaderboards.tsx index 3f07b454d2..fb4c4d6e0b 100644 --- a/web/pages/leaderboards.tsx +++ b/web/pages/leaderboards.tsx @@ -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 type MyScores = { [key in LeaderboardType]?: { mana: MyEntry; cash: MyEntry } From 284dfc579b97b2a513c2f279c4f6367bc00d6357 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Thu, 12 Dec 2024 18:28:59 -0800 Subject: [PATCH 2/2] Make creator numbers match portfolio style --- backend/api/src/get-leaderboard.ts | 40 +++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/backend/api/src/get-leaderboard.ts b/backend/api/src/get-leaderboard.ts index 6d869df4ef..f71372a168 100644 --- a/backend/api/src/get-leaderboard.ts +++ b/backend/api/src/get-leaderboard.ts @@ -47,12 +47,14 @@ export const getLeaderboard: APIHandler<'leaderboard'> = async ({ join('users u on u.id = uph.user_id'), select('uph.user_id as user_id'), token === 'MANA' - ? select('uph.profit as score') + ? 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`), + where( + `coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not true` + ), orderBy(kind === 'loss' ? 'score asc' : 'score desc nulls last'), limit(limitValue) ) @@ -61,19 +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 = ' + (kind === 'creator' ? 'c.creator_id' : 'ucm.user_id')), + join('users u on u.id = ucm.user_id'), where('ucm.answer_id is null'), where(`coalesce((c.data->'isRanked')::boolean, true) = true`), - where(`coalesce((u.data->>'isBannedFromPosting')::boolean, false) is not 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`),