Skip to content

Commit

Permalink
fix: Change out so that we use LinkedProfiles to grab the correct mem…
Browse files Browse the repository at this point in the history
…bership from the start
  • Loading branch information
itssimple committed Jun 2, 2022
1 parent 253130f commit 92dea18
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function handleUrlLaunch(urlSchemeStart) {
} else {
log("BUNGIEAUTH", "Successful");
await destinyApiClient.checkManifestVersion();
await destinyApiClient.getUserMemberships();
await destinyApiClient.getLinkedProfiles();
await destinyApiClient.getTrackableData(true);

eventEmitter.emit("auth-successful");
Expand Down Expand Up @@ -412,7 +412,7 @@ if (firstLaunch) {
window.eventEmitter.addEventListener("destiny-data-loaded", async function () {
if (await destinyApiClient.isAuthenticated()) {
await destinyApiClient.checkManifestVersion();
await destinyApiClient.getUserMemberships();
await destinyApiClient.getLinkedProfiles();
await destinyApiClient.getTrackableData(true);
} else {
window.eventEmitter.emit("destiny-not-authed");
Expand Down
37 changes: 19 additions & 18 deletions src/scripts/destiny2/apiClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class DestinyApiClient {
getAuthenticationUrl: () => string;
lastVersion: string | null;
profile: any;
userMembership: any;
linkedProfiles: any;
trackedGoals: any[];
cachedManifest: any;
checkManifestVersion: () => Promise<unknown>;
Expand All @@ -18,7 +18,7 @@ export class DestinyApiClient {
loadCommonSettings: () => Promise<unknown>;
getToken: (state: any, code: any) => Promise<unknown>;
refreshToken: () => Promise<unknown>;
getUserMemberships: () => Promise<unknown>;
getLinkedProfiles: () => Promise<unknown>;
getUserProfile: (membershipId: any, membershipType: any) => Promise<unknown>;
getLastPlayedCharacter: (forceRefresh?: boolean) => Promise<{
characterInfo: any;
Expand Down Expand Up @@ -136,7 +136,7 @@ export class DestinyApiClient {
this.randomState = null;

this.profile = null;
this.userMembership = null;
this.linkedProfiles = null;

this.trackedGoals = [];

Expand Down Expand Up @@ -235,8 +235,8 @@ export class DestinyApiClient {
self.profile = JSON.parse(await db.getItem("destiny-profile"));
}

if ((await db.getItem("destiny-userMembership")) !== null) {
self.userMembership = JSON.parse(await db.getItem("destiny-userMembership"));
if ((await db.getItem("destiny-linkedProfiles")) !== null) {
self.linkedProfiles = JSON.parse(await db.getItem("destiny-linkedProfiles"));
}

log("D2API", "Data loaded from storage");
Expand Down Expand Up @@ -346,6 +346,7 @@ export class DestinyApiClient {
db.setItem("destinyRefreshToken", tokenResponse.refresh_token);
db.setItem("destinyExpires", Date.now() + tokenResponse.expires_in * 1000);
db.setItem("destinyRefreshTokenExpires", Date.now() + tokenResponse.refresh_expires_in * 1000);
db.setItem("destinyBungieMembershipId", tokenResponse.membership_id);
}
}

Expand Down Expand Up @@ -428,22 +429,24 @@ export class DestinyApiClient {
return await db.getItem("destinyToken");
}

this.getUserMemberships = async function () {
this.getLinkedProfiles = async function () {
await refreshTokenIfExpired();

return new Promise(async (resolve, reject) => {
var bnetMemberId = await db.getItem("destinyBungieMembershipId");

await pluginClient.GET(
`${destinyApiUrl}/User/GetMembershipsForCurrentUser/`,
`${destinyApiUrl}/Destiny2/-1/Profile/${bnetMemberId}/LinkedProfiles/`,
await getUserToken(),
(result) => {
if (result.statusCode === 200) {
let memberships = JSON.parse(result.content);
let profiles = JSON.parse(result.content);

db.setItem("destiny-userMembership", JSON.stringify(memberships.Response));
db.setItem("destiny-linkedProfiles", JSON.stringify(profiles.Response));

self.userMembership = memberships.Response;
self.linkedProfiles = profiles.Response;

resolve(memberships.Response);
resolve(profiles.Response);
} else {
reject(result);
}
Expand Down Expand Up @@ -546,19 +549,17 @@ export class DestinyApiClient {
_profile = null;
}

if (self.userMembership === null) {
if (self.linkedProfiles === null) {
return null;
}

if (
_profile == null &&
self.userMembership !== null &&
self.userMembership.destinyMemberships !== null &&
self.userMembership.destinyMemberships.length > 0
self.linkedProfiles !== null &&
self.linkedProfiles.profiles !== null &&
self.linkedProfiles.profiles.length > 0
) {
var primaryMembership = self.userMembership.destinyMemberships.find(
(m) => m.membershipId == self.userMembership.primaryMembershipId
);
var primaryMembership = self.linkedProfiles.profiles[0];

_profile = await self.getUserProfile(primaryMembership.membershipId, primaryMembership.membershipType);
}
Expand Down

0 comments on commit 92dea18

Please sign in to comment.