Skip to content

Commit

Permalink
Handle access token caching
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Dec 19, 2024
1 parent 16d228b commit bbb6d72
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/lib/alarms/trade_offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,12 @@ async function getSentAndReceivedTradeOffersFromAPI(): Promise<{
};
}

export async function getTradeOffersWithDescriptionFromAPI(): Promise<{
export async function getTradeOffersWithDescriptionFromAPI(steam_id?: string): Promise<{
received: ExtendedOfferStatus[];
sent: ExtendedOfferStatus[];
descriptions: rgDescription[];
steam_id?: string | null;
}> {
const access = await getAccessToken();

// check if permissions are granted
const steamPoweredPermissions = await HasPermissions.handleRequest(
{
Expand All @@ -337,10 +335,12 @@ export async function getTradeOffersWithDescriptionFromAPI(): Promise<{
received: [],
sent: [],
descriptions: [],
steam_id: access.steam_id,
steam_id: steam_id,
};
}

const access = await getAccessToken(steam_id);

const resp = await fetch(
`https://api.steampowered.com/IEconService/GetTradeOffers/v1/?access_token=${access.token}&get_received_offers=true&get_sent_offers=true&get_descriptions=true`,
{
Expand Down
6 changes: 4 additions & 2 deletions src/lib/bridge/handlers/fetch_steam_trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {rgDescription} from '../../types/steam';
import {SimpleHandler} from './main';
import {RequestType} from './types';

interface FetchSteamTradesRequest {}
interface FetchSteamTradesRequest {
steam_id?: string;
}

export interface FetchSteamTradesResponse {
received: ExtendedOfferStatus[];
Expand All @@ -15,7 +17,7 @@ export interface FetchSteamTradesResponse {
export const FetchSteamTrades = new SimpleHandler<FetchSteamTradesRequest, FetchSteamTradesResponse>(
RequestType.FETCH_STEAM_TRADES,
async (req) => {
const resp = await getTradeOffersWithDescriptionFromAPI();
const resp = await getTradeOffersWithDescriptionFromAPI(req.steam_id);
if (!resp) {
throw new Error('Error fetching Steam trade offers from API');
}
Expand Down
17 changes: 13 additions & 4 deletions src/lib/page_scripts/trade_offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function main() {}
* @param isSentPage if the current page is the sent trade offers page
* @returns the trade offers
*/
async function fetchTradeOffers(isSentPage: boolean) {
async function fetchTradeOffers(steam_id: string, isSentPage: boolean) {
const g_steamTrades = JSON.parse(localStorage.getItem('g_steamTrades') || '{}') as FetchSteamTradesResponse;
let refetchRequired = true;
if (g_steamTrades.sent || g_steamTrades.received) {
Expand All @@ -32,7 +32,7 @@ async function fetchTradeOffers(isSentPage: boolean) {
return g_steamTrades;
}

const steamTrades = await ClientSend(FetchSteamTrades, {});
const steamTrades = await ClientSend(FetchSteamTrades, {steam_id});

localStorage.setItem('g_steamTrades', JSON.stringify(steamTrades));
return steamTrades;
Expand All @@ -42,9 +42,16 @@ async function fetchTradeOffers(isSentPage: boolean) {
* Fetches the api data for trade offers and stores relevant data in the DOM to be used by Lit components.
*/
async function annotateTradeOfferItemElements() {
const steam_id = getUserSteamID();

if (!steam_id) {
console.error('Failed to get steam_id', steam_id);
return;
}

const isSentPage = location.pathname.includes('sent');

const steamTrades = await fetchTradeOffers(isSentPage);
const steamTrades = await fetchTradeOffers(steam_id, isSentPage);

const tradeOffers = document.querySelectorAll('.tradeoffer');

Expand All @@ -56,6 +63,8 @@ async function annotateTradeOfferItemElements() {
: steamTrades.received.find((t) => t.offer_id === tradeOfferID);

for (const tradeItemElement of tradeItemElements) {
// Format: classinfo/{appid}/{classid}/{instanceid}
// Example: data-economy-item="classinfo/730/310777185/302028390"
const economyItemParts = tradeItemElement.getAttribute('data-economy-item')?.split('/');
const classId = economyItemParts?.[2];
const instanceId = economyItemParts?.[3];
Expand All @@ -78,7 +87,7 @@ async function annotateTradeOfferItemElements() {
apiItem = trade?.received_asset_ids?.find((a) => a.classid === classId && a.instanceid === instanceId);
}

const ownerId = isOwnItem ? getUserSteamID() : trade?.other_steam_id64;
const ownerId = isOwnItem ? steam_id : trade?.other_steam_id64;

if (ownerId) {
tradeItemElement.setAttribute('data-csfloat-owner-steamid', ownerId);
Expand Down

0 comments on commit bbb6d72

Please sign in to comment.