Skip to content

Commit

Permalink
Merge pull request #227 from csfloat/feature/extension-status
Browse files Browse the repository at this point in the history
Adds Ping for Extension Status
  • Loading branch information
Step7750 authored May 6, 2024
2 parents 862e136 + 592f9d0 commit 627a655
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/alarms/csfloat_trade_pings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {FetchPendingTrades} from '../bridge/handlers/fetch_pending_trades';
import {pingTradeHistory} from './trade_history';
import {pingSentTradeOffers} from './trade_offer';
import {HasPermissions} from '../bridge/handlers/has_permissions';
import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status';

export const PING_CSFLOAT_TRADE_STATUS_ALARM_NAME = 'ping_csfloat_trade_status_alarm';

Expand All @@ -19,6 +20,13 @@ export async function pingTradeStatus() {
return;
}

// Ping status of ext + permissions
try {
await PingExtensionStatus.handleRequest({}, {});
} catch (e) {
console.error('failed to ping extension status to csfloat', e);
}

let pendingTrades: Trade[];
try {
const resp = await FetchPendingTrades.handleRequest({limit: 500}, {});
Expand Down
2 changes: 2 additions & 0 deletions src/lib/bridge/handlers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {TradeHistoryStatus} from './trade_history_status';
import {TradeOfferStatus} from './trade_offer_status';
import {HasPermissions} from './has_permissions';
import {PingSetupExtension} from './ping_setup_extension';
import {PingExtensionStatus} from './ping_extension_status';

export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage,
Expand All @@ -34,4 +35,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
[RequestType.TRADE_OFFER_STATUS]: TradeOfferStatus,
[RequestType.HAS_PERMISSIONS]: HasPermissions,
[RequestType.PING_SETUP_EXTENSION]: PingSetupExtension,
[RequestType.PING_EXTENSION_STATUS]: PingExtensionStatus,
};
51 changes: 51 additions & 0 deletions src/lib/bridge/handlers/ping_extension_status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {SimpleHandler} from './main';
import {RequestType} from './types';
import {environment} from '../../../environment';
import {HasPermissions} from './has_permissions';
import {ExtensionVersion} from './extension_version';

export interface PingExtensionStatusRequest {}

export interface PingExtensionStatusResponse {}

export const PingExtensionStatus = new SimpleHandler<PingExtensionStatusRequest, PingExtensionStatusResponse>(
RequestType.PING_EXTENSION_STATUS,
async (req) => {
const steamPoweredPermissions = await HasPermissions.handleRequest(
{
permissions: [],
origins: ['*://*.steampowered.com/*'],
},
{}
);

const steamCommunityPermissions = await HasPermissions.handleRequest(
{
permissions: [],
origins: ['*://*.steamcommunity.com/*'],
},
{}
);

const versionResp = await ExtensionVersion.handleRequest({}, {});

const resp = await fetch(`${environment.csfloat_base_api_url}/v1/me/extension/status`, {
credentials: 'include',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
steam_community_permission: steamCommunityPermissions.granted,
steam_powered_permission: steamPoweredPermissions.granted,
version: versionResp.version,
}),
});

if (resp.status !== 200) {
throw new Error('invalid status');
}

return resp.json() as Promise<PingExtensionStatusResponse>;
}
);
1 change: 1 addition & 0 deletions src/lib/bridge/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export enum RequestType {
TRADE_OFFER_STATUS,
HAS_PERMISSIONS,
PING_SETUP_EXTENSION,
PING_EXTENSION_STATUS,
}
2 changes: 2 additions & 0 deletions src/lib/page_scripts/trade_offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../components/trade_offers/better_tracking';
import {inPageContext} from '../utils/snips';
import {ClientSend} from '../bridge/client';
import {PingSetupExtension} from '../bridge/handlers/ping_setup_extension';
import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status';

init('src/lib/page_scripts/trade_offers.js', main);

Expand Down Expand Up @@ -31,6 +32,7 @@ if (!inPageContext()) {
if (granted) {
widget[0].parentElement?.removeChild(widget[0]);
ClientSend(PingSetupExtension, {});
ClientSend(PingExtensionStatus, {});
} else {
alert('Failed to obtain permissions');
}
Expand Down

0 comments on commit 627a655

Please sign in to comment.