diff --git a/src/js/Background/Modules/SteamCommunityApi.js b/src/js/Background/Modules/Community/SteamCommunityApi.ts similarity index 97% rename from src/js/Background/Modules/SteamCommunityApi.js rename to src/js/Background/Modules/Community/SteamCommunityApi.ts index c2d5a68a6..aa1d61e85 100644 --- a/src/js/Background/Modules/SteamCommunityApi.js +++ b/src/js/Background/Modules/Community/SteamCommunityApi.ts @@ -1,7 +1,8 @@ -import {Errors, GameId, HTMLParser, LocalStorage} from "../../modulesCore"; -import {Api} from "./Api"; -import CacheStorage from "./CacheStorage"; -import {IndexedDB} from "./IndexedDB"; +import {Errors, GameId, HTMLParser, LocalStorage} from "../../../modulesCore"; +import {Api} from "../Api"; +import CacheStorage from "../CacheStorage"; +import {IndexedDB} from "../IndexedDB"; +import type {TFetchBadgeInfoResponse} from "./_types"; class SteamCommunityApi extends Api { @@ -10,7 +11,7 @@ class SteamCommunityApi extends Api { * static params = { 'credentials': 'include', }; */ - static cards(steamid, appid) { + static fetchBadgeInfo(steamid: string, appid: number): Promise { return SteamCommunityApi.getEndpoint(`/profiles/${steamid}/ajaxgetbadgeinfo/${appid}`); } diff --git a/src/js/Background/Modules/Community/_types.ts b/src/js/Background/Modules/Community/_types.ts new file mode 100644 index 000000000..1f39ab558 --- /dev/null +++ b/src/js/Background/Modules/Community/_types.ts @@ -0,0 +1,37 @@ + +interface TCardInfo { + name: string; + title: string; + imgurl: string; + arturl: string; + owned: 0|1; + markethash: string; +} + +export interface TBadgeData { + appid: number; + border: 0|1; + series: number; + level: number; + maxlevel: number; + name: string; // empty if no badge level + xp: number; + nextlevelname: string; + nextlevelxp: number; + iconurl: string; + bMaxed: null|unknown; + rgCards: TCardInfo[] +} + +export interface TFetchBadgeInfoMessage { + action: "community.badgeinfo", + params: { + steamId: string, + appid: number + } +} + +export interface TFetchBadgeInfoResponse { + eresult: number, + badgedata?: TBadgeData +} diff --git a/src/js/Background/background.ts b/src/js/Background/background.ts index d4a4bdb0a..41792f1ce 100644 --- a/src/js/Background/background.ts +++ b/src/js/Background/background.ts @@ -2,7 +2,7 @@ import setup from "../setup"; import {LocalStorage, Permissions, SyncedStorage} from "../modulesCore"; import {ContextMenu} from "./Modules/ContextMenu"; import {IndexedDB} from "./Modules/IndexedDB"; -import {SteamCommunityApi} from "./Modules/SteamCommunityApi"; +import {SteamCommunityApi} from "./Modules/Community/SteamCommunityApi"; import {SteamStoreApi} from "./Modules/SteamStoreApi"; import {StaticResources} from "./Modules/StaticResources"; import {ITADApi} from "./Modules/IsThereAnyDeal/ITADApi"; @@ -12,6 +12,7 @@ import CacheStorage from "./Modules/CacheStorage"; import browser, {type Runtime} from "webextension-polyfill"; import type {TFetchPricesMessage} from "./Modules/AugmentedSteam/_types"; import type {TGetStoreListMessage} from "./Modules/IsThereAnyDeal/_types"; +import type {TFetchBadgeInfoMessage} from "./Modules/Community/_types"; type MessageSender = Runtime.MessageSender; @@ -99,7 +100,7 @@ const actionCallbacks = new Map([ ["login", SteamCommunityApi.login], ["logout", SteamCommunityApi.logout], ["storecountry", SteamCommunityApi.storeCountry], - ["cards", SteamCommunityApi.cards], + ["cards", SteamCommunityApi.fetchBadgeInfo], ["coupon", SteamCommunityApi.getCoupon], ["hasgiftsandpasses", SteamCommunityApi.hasGiftsAndPasses], ["hascoupon", SteamCommunityApi.hasCoupon], @@ -136,6 +137,8 @@ type Message = | TGetStoreListMessage // AugmentedSteamApi | TFetchPricesMessage + // SteamCommunityApi + | TFetchBadgeInfoMessage // old | GenericMessage; @@ -152,12 +155,17 @@ browser.runtime.onMessage.addListener(( return; } - (async function(): void { + (async function(): Promise { try { await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]); let response: any; + /* + * TODO: (<>message.)params typecast should be needed only until we allow GenericMessage, once we get rid of it, + * remove type cast, which should also ensure better checks + */ + switch (message.action) { // TODO rename to "api"? case "itad.storelist": @@ -169,6 +177,13 @@ browser.runtime.onMessage.addListener(( response = await AugmentedSteamApi.fetchPrices(country, apps, subs, bundles, voucher, shops); break; } + + case "community.badgeinfo": + const {steamId, appid} = (message).params; + response = await SteamCommunityApi.fetchBadgeInfo(steamId, appid); + break; + + default: { /* * TODO deprecated diff --git a/src/js/Content/Features/Store/App/FBadgeProgress.svelte b/src/js/Content/Features/Store/App/FBadgeProgress.svelte index 4806afa91..8faf654da 100644 --- a/src/js/Content/Features/Store/App/FBadgeProgress.svelte +++ b/src/js/Content/Features/Store/App/FBadgeProgress.svelte @@ -2,8 +2,10 @@ // @ts-ignore import self_ from "./FBadgeProgress.svelte"; import {SyncedStorage} from "../../../../modulesCore"; - import {Background, Feature, User} from "../../../modulesContent"; + import {Feature, User} from "../../../modulesContent"; import type {CApp} from "./CApp"; + import type {TFetchBadgeInfoResponse} from "../../../../Background/Modules/Community/_types"; + import SteamCommunityApiFacade from "../../../Modules/Facades/SteamCommunityApiFacade"; export class FBadgeProgress extends Feature { @@ -14,15 +16,12 @@ } override async apply(): Promise { - let data; + let response: TFetchBadgeInfoResponse = await SteamCommunityApiFacade.fetchBadgeInfo( + User.steamId, + this.context.communityAppid + ); - try { - data = await Background.action("cards", User.steamId, this.context.communityAppid); - } catch (err) { - throw new Error("Failed to fetch badges", {"cause": err}); - } - - data = data.badgedata; + let data = response.badgedata; // No badge data if game doesn't have cards or not logged in if (!data) { @@ -45,28 +44,9 @@