Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of private apps data #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 177 additions & 17 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ let storeSessionId;
let checkoutSessionId;
let userDataCache = null;
let userFamilyDataCache = null;
let userPrivateAppsCache = null;
let userFamilySemaphore = null;
let userPrivateAppsSemaphore = null;
let tokenSemaphore = null;
let nextAllowedRequest = 0;

/** @type {browser} ExtensionApi */
Expand Down Expand Up @@ -47,11 +50,13 @@ ExtensionApi.runtime.onMessage.addListener( ( request, sender, callback ) =>
switch( request.contentScriptQuery )
{
case 'InvalidateCache': InvalidateCache(); callback(); return true;
case 'FetchPrivateApps': FetchPrivateApps( callback ); return true;
case 'FetchSteamUserData': FetchSteamUserData( callback ); return true;
case 'FetchSteamUserFamilyData': FetchSteamUserFamilyData( callback ); return true;
case 'GetApp': GetApp( request.appid, callback ); return true;
case 'GetAppPrice': GetAppPrice( request, callback ); return true;
case 'GetAchievementsGroups': GetAchievementsGroups( request.appid, callback ); return true;
case 'SetAppPrivate': SetAppPrivate( request.appids, request.private, callback ); return true;
case 'StoreWishlistAdd': StoreWishlistAdd( request.appid, callback ); return true;
case 'StoreWishlistRemove': StoreWishlistRemove( request.appid, callback ); return true;
case 'StoreFollow': StoreFollow( request.appid, callback ); return true;
Expand All @@ -77,6 +82,99 @@ function InvalidateCache()
SetLocalOption( 'userfamilydata', '{}' );
}

/**
* @param {Function} callback
*/
async function FetchPrivateApps( callback )
{
if( userPrivateAppsCache !== null )
{
callback( { data: userPrivateAppsCache } );
return;
}

if( userPrivateAppsSemaphore !== null )
{
callback( await userFamilySemaphore );
return;
}

const now = Date.now();
const cacheData = await GetLocalOption( { privateappsdata: false } );
const cache = cacheData.userfamilydata && cacheData.userfamilydata;

if( cache && cache.cached && cache.data && now < cache.cached + 21600000 )
{
callback( { data: cache.data } );
return;
}

let callbackResponse = null;
let semaphoreResolve = null;
userPrivateAppsSemaphore = new Promise( resolve =>
{
semaphoreResolve = resolve;
} );

try
{
const token = await GetStoreToken();
const paramsPrivateApps = new URLSearchParams();
paramsPrivateApps.set( 'access_token', token );
const responseFetch = await fetch(
`https://api.steampowered.com/IAccountPrivateAppsService/GetPrivateAppList/v1/?${paramsPrivateApps.toString()}`,
{
headers: {
Accept: 'application/json',
}
}
);
const response = await responseFetch.json();

if( !response || !response.response || !response.response.private_apps )
{
throw new Error( 'Is Steam okay?' );
}

userPrivateAppsCache =
{
rgPrivateApps: response.response.private_apps.appids || [],
};

callbackResponse =
{
data: userPrivateAppsCache
};

callback( callbackResponse );

await SetLocalOption( 'privateappsdata', JSON.stringify( {
data: userPrivateAppsCache,
cached: now
} ) );
}
catch( error )
{
callbackResponse =
{
error: error.message,
};

if( cache && cache.data )
{
callbackResponse.data = cache.data;
}

callback( callbackResponse );
}
finally
{
semaphoreResolve( callbackResponse );
userPrivateAppsSemaphore = null;
}

};

/**
* @param {Function} callback
*/
Expand Down Expand Up @@ -196,24 +294,9 @@ async function FetchSteamUserFamilyData( callback )

try
{
const tokenResponseFetch = await fetch(
`https://store.steampowered.com/pointssummary/ajaxgetasyncconfig`,
{
credentials: 'include',
headers: {
Accept: 'application/json',
},
}
);
const token = await tokenResponseFetch.json();

if( !token || !token.success || !token.data || !token.data.webapi_token )
{
throw new Error( 'Are you logged on the Steam Store in this browser?' );
}

const token = await GetStoreToken();
const paramsSharedLibrary = new URLSearchParams();
paramsSharedLibrary.set( 'access_token', token.data.webapi_token );
paramsSharedLibrary.set( 'access_token', token );
paramsSharedLibrary.set( 'family_groupid', '0' ); // family_groupid is ignored
paramsSharedLibrary.set( 'include_excluded', 'true' );
paramsSharedLibrary.set( 'include_free', 'true' );
Expand Down Expand Up @@ -402,6 +485,83 @@ function GetAchievementsGroups( appid, callback )
.catch( ( error ) => callback( { success: false, error: error.message } ) );
}

/**
* @param {Array<Number>} appids
* @param {Boolean} privateState
* @param {Function} callback
*/
// ? Api supports setting multiple apps at once (Are they even using that feature?), do we really need that?
async function SetAppPrivate( appids, privateState, callback )
{
const token = await GetStoreToken();
const paramsSetPrivate = new URLSearchParams();
paramsSetPrivate.set( 'access_token', token );
appids.forEach( ( appid, index ) =>
{
paramsSetPrivate.set( `appids[${index}]`, appid );
} );
paramsSetPrivate.set( 'private', privateState );
const responseFetch = await fetch(
`https://api.steampowered.com/IAccountPrivateAppsService/ToggleAppPrivacy/v1/?${paramsSetPrivate.toString()}`
);
}

/**
* @return {Promise<String>}
*/
async function GetStoreToken()
{
if( tokenSemaphore !== null )
{
return await tokenSemaphore ;
}
let token = null;
let semaphoreResolve = null;
tokenSemaphore = new Promise( resolve =>
{
semaphoreResolve = resolve;
} );

try
{
token = await GetLocalOption( { storetoken: false } ).then( data => data.storetoken );
if( token )
{
const jwt = token.split( '.' );
const payload = JSON.parse( atob( jwt[ 1 ] ) );
const expiration = payload.exp * 1000;
if( Date.now() < expiration )
{
return token;
}
}

token = await fetch(
`https://store.steampowered.com/pointssummary/ajaxgetasyncconfig`,
{
credentials: 'include',
headers: {
Accept: 'application/json',
},
}
).then( response =>response.json() );

if( !token || !token.success || !token.data || !token.data.webapi_token )
{
throw new Error( 'Are you logged on the Steam Store in this browser?' );
}

await SetLocalOption( 'storetoken', token.data.webapi_token );

return token.data.webapi_token;
}
finally
{
semaphoreResolve( token );
tokenSemaphore = null;
};
}

/**
* @param {Function} callback
*/
Expand Down
22 changes: 21 additions & 1 deletion scripts/steamdb/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
}, resolve );
} );

/** @type {Promise<{data?: object, error?: string}>} */
const PrivateDataPromise = new Promise( ( resolve ) =>
{
SendMessageToBackgroundScript( {
contentScriptQuery: 'FetchPrivateApps',
}, resolve );
} );

/** @type {Promise<{data?: object, error?: string}>} */
const familyDataPromise = new Promise( ( resolve ) =>
{
Expand All @@ -103,7 +111,7 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
}, 10000 ); // 10 seconds
} );

const userData = await userDataPromise;
const [ userData, privateData ] = await Promise.all( [ userDataPromise, PrivateDataPromise ] );

// If family data does not load fast enough, assume it failed
const familyData = await Promise.race( [
Expand All @@ -116,6 +124,11 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
WriteLog( 'Failed to load userdata', userData.error );
}

if( privateData.error )
{
WriteLog( 'Failed to load privatedata', privateData.error );
}

if( familyData.error )
{
WriteLog( 'Failed to load family userdata', familyData.error );
Expand All @@ -128,6 +141,11 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
{
response = userData.data;

if( privateData.data )
{
response.rgPrivateApps = privateData.data.rgPrivateApps;
}

if( familyData.data )
{
response.rgFamilySharedApps = familyData.data.rgFamilySharedApps;
Expand Down Expand Up @@ -159,6 +177,8 @@ GetOption( { 'steamdb-highlight': true, 'steamdb-highlight-family': true }, asyn
beforeDom ? '(before dom completed)' : '',
'Packages',
response.rgOwnedPackages?.length || 0,
'Private Apps',
response.rgPrivateApps?.length || 0,
'Family Apps',
response.rgFamilySharedApps?.length || 0,
);
Expand Down