|
| 1 | +import {Trade} from '../types/float_market'; |
| 2 | +import {gStore} from '../storage/store'; |
| 3 | +import {StorageKey} from '../storage/keys'; |
| 4 | +import {FetchSteamUser} from '../bridge/handlers/fetch_steam_user'; |
| 5 | +import {FetchBlockedUsers} from '../bridge/handlers/fetch_blocked_users'; |
| 6 | +import {PingBlockedUsers} from '../bridge/handlers/ping_blocked_users'; |
| 7 | + |
| 8 | +export async function reportBlockedBuyers(pendingTrades: Trade[]) { |
| 9 | + const lastPing = await gStore.getWithStorage<number>( |
| 10 | + chrome.storage.local, |
| 11 | + StorageKey.LAST_TRADE_BLOCKED_PING_ATTEMPT |
| 12 | + ); |
| 13 | + if (lastPing && lastPing > Date.now() - 10 * 60 * 1000) { |
| 14 | + // Report blocked users at most once every 10 minutes |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + await gStore.setWithStorage<number>(chrome.storage.local, StorageKey.LAST_TRADE_BLOCKED_PING_ATTEMPT, Date.now()); |
| 19 | + |
| 20 | + const steamUser = await FetchSteamUser.handleRequest({}, {}); |
| 21 | + if (!steamUser.sessionID || !steamUser.steamID) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const hasTrade = pendingTrades.some((e) => e.seller_id === steamUser.steamID || e.buyer_id === steamUser.steamID); |
| 26 | + if (!hasTrade) { |
| 27 | + // Logged-in user on Steam doesn't have any CSFloat trades, not relevant |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const {blocked_steam_ids} = await FetchBlockedUsers.handleRequest( |
| 32 | + { |
| 33 | + steam_id: steamUser.steamID, |
| 34 | + }, |
| 35 | + {} |
| 36 | + ); |
| 37 | + |
| 38 | + const filteredIDs = blocked_steam_ids.filter((steamID) => { |
| 39 | + // Is it a buyer or seller in one of the trades? |
| 40 | + return pendingTrades.some((e) => e.seller_id == steamID || e.buyer_id == steamID); |
| 41 | + }); |
| 42 | + |
| 43 | + if (filteredIDs.length === 0) { |
| 44 | + // Nothing to report |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + await PingBlockedUsers.handleRequest({blocked_steam_ids: filteredIDs}, {}); |
| 49 | +} |
0 commit comments