Skip to content

Commit 4bd5d98

Browse files
authored
Merge pull request #236 from csfloat/feature/improve-status-debugability
Improves Ability to Debug Status
2 parents 11f0931 + 6282ff6 commit 4bd5d98

5 files changed

Lines changed: 73 additions & 18 deletions

File tree

src/lib/alarms/access_token.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import {gStore} from '../storage/store';
22
import {StorageKey} from '../storage/keys';
33

4-
interface AccessToken {
4+
export interface AccessToken {
55
token: string;
6+
steam_id?: string | null;
67
updated_at: number;
78
}
89

9-
export async function getAccessToken(): Promise<string | null> {
10+
export async function getAccessToken(): Promise<AccessToken> {
1011
// Do we have a fresh local copy?
1112
const tokenData = await gStore.getWithStorage<AccessToken>(chrome.storage.local, StorageKey.ACCESS_TOKEN);
1213
if (tokenData?.token && tokenData.updated_at > Date.now() - 30 * 60 * 1000) {
1314
// Token refreshed within the last 30 min, we can re-use
14-
return tokenData.token;
15+
return tokenData;
1516
}
1617

1718
// Need to fetch a new one
@@ -23,25 +24,35 @@ export async function getAccessToken(): Promise<string | null> {
2324

2425
const webAPITokenMatch = /data-loyalty_webapi_token="&quot;([a-zA-Z0-9_.-]+)&quot;"/.exec(body);
2526
if (!webAPITokenMatch || webAPITokenMatch.length === 0) {
26-
console.error('failed to parse web api token');
27-
return null;
27+
throw new Error('failed to parse web api token');
2828
}
2929

3030
const token = webAPITokenMatch[1];
31+
const steamID = extractSteamID(body);
3132

3233
try {
33-
await saveAccessToken(token);
34+
await saveAccessToken(token, steamID);
3435
} catch (e) {
3536
console.error('failed ot save access token to storage', e);
3637
}
3738

38-
return token;
39+
return {token, steam_id: steamID, updated_at: Date.now()};
40+
}
41+
42+
function extractSteamID(body: string): string | null {
43+
const steamIDMatch = /g_steamID = "(\d+?)"/.exec(body);
44+
if (!steamIDMatch || steamIDMatch.length === 0) {
45+
return null;
46+
}
47+
48+
return steamIDMatch[1];
3949
}
4050

41-
export function saveAccessToken(token: string): Promise<void> {
51+
export function saveAccessToken(token: string, steamID: string | null): Promise<void> {
4252
// Explicitly use local storage to prevent issues with sync storage quota or connectivity issues
4353
return gStore.setWithStorage(chrome.storage.local, StorageKey.ACCESS_TOKEN, {
4454
token,
55+
steam_id: steamID,
4556
updated_at: Date.now(),
4657
} as AccessToken);
4758
}

src/lib/alarms/csfloat_trade_pings.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {pingTradeHistory} from './trade_history';
44
import {pingCancelTrades, pingSentTradeOffers} from './trade_offer';
55
import {HasPermissions} from '../bridge/handlers/has_permissions';
66
import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status';
7+
import {AccessToken, getAccessToken} from './access_token';
78

89
export const PING_CSFLOAT_TRADE_STATUS_ALARM_NAME = 'ping_csfloat_trade_status_alarm';
910

@@ -37,26 +38,62 @@ export async function pingTradeStatus() {
3738
return;
3839
}
3940

40-
if (pendingTrades.length === 0) {
41-
// No active trades, return early
42-
return;
41+
let access: AccessToken | null = null;
42+
43+
try {
44+
access = await getAccessToken();
45+
} catch (e) {
46+
console.error('failed to fetch access token', e);
47+
}
48+
49+
let errors;
50+
51+
if (pendingTrades.length > 0) {
52+
errors = await pingUpdates(pendingTrades);
53+
}
54+
55+
// Ping status of ext + permissions
56+
try {
57+
await PingExtensionStatus.handleRequest(
58+
{
59+
access_token_steam_id: access?.steam_id,
60+
history_error: errors?.history_error,
61+
trade_offer_error: errors?.trade_offer_error,
62+
},
63+
{}
64+
);
65+
} catch (e) {
66+
console.error('failed to ping extension status to csfloat', e);
4367
}
68+
}
69+
70+
interface UpdateErrors {
71+
history_error?: string;
72+
trade_offer_error?: string;
73+
}
74+
75+
async function pingUpdates(pendingTrades: Trade[]): Promise<UpdateErrors> {
76+
const errors: UpdateErrors = {};
4477

4578
try {
4679
await pingTradeHistory(pendingTrades);
4780
} catch (e) {
4881
console.error('failed to ping trade history', e);
82+
errors.history_error = (e as any).toString();
4983
}
5084

5185
try {
5286
await pingSentTradeOffers(pendingTrades);
5387
} catch (e) {
5488
console.error('failed to ping sent trade offer state', e);
89+
errors.trade_offer_error = (e as any).toString();
5590
}
5691

5792
try {
5893
await pingCancelTrades(pendingTrades);
5994
} catch (e) {
6095
console.error('failed to ping cancel ping trade offers', e);
6196
}
97+
98+
return errors;
6299
}

src/lib/alarms/trade_history.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ interface TradeHistoryAPIResponse {
6565
}
6666

6767
async function getTradeHistoryFromAPI(): Promise<TradeHistoryStatus[]> {
68-
const accessToken = await getAccessToken();
68+
const access = await getAccessToken();
6969

7070
// This only works if they have granted permission for https://api.steampowered.com
7171
const resp = await fetch(
72-
`https://api.steampowered.com/IEconService/GetTradeHistory/v1/?access_token=${accessToken}&max_trades=200`,
72+
`https://api.steampowered.com/IEconService/GetTradeHistory/v1/?access_token=${access.token}&max_trades=200`,
7373
{
7474
credentials: 'include',
7575
}

src/lib/alarms/trade_offer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ function offerStateMapper(e: TradeOffersAPIOffer): OfferStatus {
173173
}
174174

175175
async function getSentTradeOffersFromAPI(): Promise<OfferStatus[]> {
176-
const accessToken = await getAccessToken();
176+
const access = await getAccessToken();
177177

178178
const resp = await fetch(
179-
`https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${accessToken}&get_sent_offers=true`,
179+
`https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${access.token}&get_sent_offers=true`,
180180
{
181181
credentials: 'include',
182182
}
@@ -191,10 +191,10 @@ async function getSentTradeOffersFromAPI(): Promise<OfferStatus[]> {
191191
}
192192

193193
async function getSentAndReceivedTradeOffersFromAPI(): Promise<{received: OfferStatus[]; sent: OfferStatus[]}> {
194-
const accessToken = await getAccessToken();
194+
const access = await getAccessToken();
195195

196196
const resp = await fetch(
197-
`https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${accessToken}&get_received_offers=true&get_sent_offers=true`,
197+
`https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${access.token}&get_received_offers=true&get_sent_offers=true`,
198198
{
199199
credentials: 'include',
200200
}

src/lib/bridge/handlers/ping_extension_status.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {environment} from '../../../environment';
44
import {HasPermissions} from './has_permissions';
55
import {ExtensionVersion} from './extension_version';
66

7-
export interface PingExtensionStatusRequest {}
7+
export interface PingExtensionStatusRequest {
8+
access_token_steam_id?: string | null;
9+
history_error?: string | null;
10+
trade_offer_error?: string | null;
11+
}
812

913
export interface PingExtensionStatusResponse {}
1014

@@ -39,6 +43,9 @@ export const PingExtensionStatus = new SimpleHandler<PingExtensionStatusRequest,
3943
steam_community_permission: steamCommunityPermissions.granted,
4044
steam_powered_permission: steamPoweredPermissions.granted,
4145
version: versionResp.version,
46+
access_token_steam_id: req.access_token_steam_id || '',
47+
history_error: req.history_error || '',
48+
trade_offer_error: req.trade_offer_error || '',
4249
}),
4350
});
4451

0 commit comments

Comments
 (0)