From 6282ff646871d763c58337e2d7648f71d0c61f20 Mon Sep 17 00:00:00 2001 From: Step7750 Date: Tue, 21 May 2024 18:18:22 -0600 Subject: [PATCH] Improves Ability to Debug Status --- src/lib/alarms/access_token.ts | 27 ++++++++---- src/lib/alarms/csfloat_trade_pings.ts | 43 +++++++++++++++++-- src/lib/alarms/trade_history.ts | 4 +- src/lib/alarms/trade_offer.ts | 8 ++-- .../bridge/handlers/ping_extension_status.ts | 9 +++- 5 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/lib/alarms/access_token.ts b/src/lib/alarms/access_token.ts index c0367e81..422a2b26 100644 --- a/src/lib/alarms/access_token.ts +++ b/src/lib/alarms/access_token.ts @@ -1,17 +1,18 @@ import {gStore} from '../storage/store'; import {StorageKey} from '../storage/keys'; -interface AccessToken { +export interface AccessToken { token: string; + steam_id?: string | null; updated_at: number; } -export async function getAccessToken(): Promise { +export async function getAccessToken(): Promise { // Do we have a fresh local copy? const tokenData = await gStore.getWithStorage(chrome.storage.local, StorageKey.ACCESS_TOKEN); if (tokenData?.token && tokenData.updated_at > Date.now() - 30 * 60 * 1000) { // Token refreshed within the last 30 min, we can re-use - return tokenData.token; + return tokenData; } // Need to fetch a new one @@ -23,25 +24,35 @@ export async function getAccessToken(): Promise { const webAPITokenMatch = /data-loyalty_webapi_token=""([a-zA-Z0-9_.-]+)""/.exec(body); if (!webAPITokenMatch || webAPITokenMatch.length === 0) { - console.error('failed to parse web api token'); - return null; + throw new Error('failed to parse web api token'); } const token = webAPITokenMatch[1]; + const steamID = extractSteamID(body); try { - await saveAccessToken(token); + await saveAccessToken(token, steamID); } catch (e) { console.error('failed ot save access token to storage', e); } - return token; + return {token, steam_id: steamID, updated_at: Date.now()}; +} + +function extractSteamID(body: string): string | null { + const steamIDMatch = /g_steamID = "(\d+?)"/.exec(body); + if (!steamIDMatch || steamIDMatch.length === 0) { + return null; + } + + return steamIDMatch[1]; } -export function saveAccessToken(token: string): Promise { +export function saveAccessToken(token: string, steamID: string | null): Promise { // Explicitly use local storage to prevent issues with sync storage quota or connectivity issues return gStore.setWithStorage(chrome.storage.local, StorageKey.ACCESS_TOKEN, { token, + steam_id: steamID, updated_at: Date.now(), } as AccessToken); } diff --git a/src/lib/alarms/csfloat_trade_pings.ts b/src/lib/alarms/csfloat_trade_pings.ts index 45e3393c..8635a765 100644 --- a/src/lib/alarms/csfloat_trade_pings.ts +++ b/src/lib/alarms/csfloat_trade_pings.ts @@ -4,6 +4,7 @@ import {pingTradeHistory} from './trade_history'; import {pingCancelTrades, pingSentTradeOffers} from './trade_offer'; import {HasPermissions} from '../bridge/handlers/has_permissions'; import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status'; +import {AccessToken, getAccessToken} from './access_token'; export const PING_CSFLOAT_TRADE_STATUS_ALARM_NAME = 'ping_csfloat_trade_status_alarm'; @@ -37,21 +38,55 @@ export async function pingTradeStatus() { return; } - if (pendingTrades.length === 0) { - // No active trades, return early - return; + let access: AccessToken | null = null; + + try { + access = await getAccessToken(); + } catch (e) { + console.error('failed to fetch access token', e); + } + + let errors; + + if (pendingTrades.length > 0) { + errors = await pingUpdates(pendingTrades); + } + + // Ping status of ext + permissions + try { + await PingExtensionStatus.handleRequest( + { + access_token_steam_id: access?.steam_id, + history_error: errors?.history_error, + trade_offer_error: errors?.trade_offer_error, + }, + {} + ); + } catch (e) { + console.error('failed to ping extension status to csfloat', e); } +} + +interface UpdateErrors { + history_error?: string; + trade_offer_error?: string; +} + +async function pingUpdates(pendingTrades: Trade[]): Promise { + const errors: UpdateErrors = {}; try { await pingTradeHistory(pendingTrades); } catch (e) { console.error('failed to ping trade history', e); + errors.history_error = (e as any).toString(); } try { await pingSentTradeOffers(pendingTrades); } catch (e) { console.error('failed to ping sent trade offer state', e); + errors.trade_offer_error = (e as any).toString(); } try { @@ -59,4 +94,6 @@ export async function pingTradeStatus() { } catch (e) { console.error('failed to ping cancel ping trade offers', e); } + + return errors; } diff --git a/src/lib/alarms/trade_history.ts b/src/lib/alarms/trade_history.ts index cabe03f1..62d0a187 100644 --- a/src/lib/alarms/trade_history.ts +++ b/src/lib/alarms/trade_history.ts @@ -65,11 +65,11 @@ interface TradeHistoryAPIResponse { } async function getTradeHistoryFromAPI(): Promise { - const accessToken = await getAccessToken(); + const access = await getAccessToken(); // This only works if they have granted permission for https://api.steampowered.com const resp = await fetch( - `https://api.steampowered.com/IEconService/GetTradeHistory/v1/?access_token=${accessToken}&max_trades=200`, + `https://api.steampowered.com/IEconService/GetTradeHistory/v1/?access_token=${access.token}&max_trades=200`, { credentials: 'include', } diff --git a/src/lib/alarms/trade_offer.ts b/src/lib/alarms/trade_offer.ts index 22fb9a18..44d0b217 100644 --- a/src/lib/alarms/trade_offer.ts +++ b/src/lib/alarms/trade_offer.ts @@ -173,10 +173,10 @@ function offerStateMapper(e: TradeOffersAPIOffer): OfferStatus { } async function getSentTradeOffersFromAPI(): Promise { - const accessToken = await getAccessToken(); + const access = await getAccessToken(); const resp = await fetch( - `https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${accessToken}&get_sent_offers=true`, + `https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${access.token}&get_sent_offers=true`, { credentials: 'include', } @@ -191,10 +191,10 @@ async function getSentTradeOffersFromAPI(): Promise { } async function getSentAndReceivedTradeOffersFromAPI(): Promise<{received: OfferStatus[]; sent: OfferStatus[]}> { - const accessToken = await getAccessToken(); + const access = await getAccessToken(); const resp = await fetch( - `https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${accessToken}&get_received_offers=true&get_sent_offers=true`, + `https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${access.token}&get_received_offers=true&get_sent_offers=true`, { credentials: 'include', } diff --git a/src/lib/bridge/handlers/ping_extension_status.ts b/src/lib/bridge/handlers/ping_extension_status.ts index ae6e4fcf..3cbd0281 100644 --- a/src/lib/bridge/handlers/ping_extension_status.ts +++ b/src/lib/bridge/handlers/ping_extension_status.ts @@ -4,7 +4,11 @@ import {environment} from '../../../environment'; import {HasPermissions} from './has_permissions'; import {ExtensionVersion} from './extension_version'; -export interface PingExtensionStatusRequest {} +export interface PingExtensionStatusRequest { + access_token_steam_id?: string | null; + history_error?: string | null; + trade_offer_error?: string | null; +} export interface PingExtensionStatusResponse {} @@ -39,6 +43,9 @@ export const PingExtensionStatus = new SimpleHandler