From c03745e89683aa1fa37af42f834fcad0551ea24c Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Mon, 2 Aug 2021 11:50:20 -0700 Subject: [PATCH 1/3] workaround for getting the token in the web worker --- src/app/_services/user.service.ts | 41 ++++++++++++----------- src/app/_workers/fetch-accounts.worker.ts | 13 +++++-- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 5002026..53fd042 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -41,7 +41,7 @@ export class UserService { constructor( private httpClient: HttpClient, private loggingService: LoggingService, - private tokenService: TokenService + private tokenService: TokenService, ) {} async init(): Promise { @@ -76,7 +76,7 @@ export class UserService { userLocation: string, location: string, locationType: string, - oldPhoneNumber: string + oldPhoneNumber: string, ): Promise { const accountInfo: any = { vcard: { @@ -137,11 +137,11 @@ export class UserService { this.loggingService.sendErrorLevelMessage( 'Cannot find account info in meta service', this, - { error } + { error }, ); const syncableAccount: Syncable = new Syncable(accountKey, accountInfo); await this.updateMeta(syncableAccount, accountKey, this.headers); - } + }, ); if (phoneNumber !== oldPhoneNumber) { const oldPhoneKey: string = await Phone.toKey(oldPhoneNumber); @@ -157,7 +157,7 @@ export class UserService { async updateMeta( syncableAccount: Syncable, accountKey: string, - headers: HttpHeaders + headers: HttpHeaders, ): Promise { const envelope: Envelope = await this.wrap(syncableAccount, this.signer); const reqBody: string = envelope.toJSON(); @@ -214,22 +214,25 @@ export class UserService { if (typeof Worker !== 'undefined') { const worker = new Worker('@app/_workers/fetch-accounts.worker', { type: 'module' }); worker.onmessage = ({ data }) => { - this.tokenService.load.subscribe(async (status: boolean) => { - if (status) { - data.balance = await this.tokenService.getTokenBalance( - data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] - ); - } - }); - this.addAccount(data, limit); + if (data) { + this.tokenService.load.subscribe(async (status: boolean) => { + if (status) { + data.balance = await this.tokenService.getTokenBalance( + data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], + ); + } + }); + this.addAccount(data, limit); + } }; worker.postMessage({ addresses: accountAddresses.slice(offset, offset + limit), url: environment.cicMetaUrl, + token: sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')), }); } else { this.loggingService.sendInfoLevelMessage( - 'Web workers are not supported in this environment' + 'Web workers are not supported in this environment', ); for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { await this.getAccountByAddress(accountAddress, limit); @@ -243,7 +246,7 @@ export class UserService { async getAccountByAddress( accountAddress: string, - limit: number = 100 + limit: number = 100, ): Promise> { const accountSubject: Subject = new Subject(); this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress))) @@ -255,7 +258,7 @@ export class UserService { this.tokenService.load.subscribe(async (status: boolean) => { if (status) { accountInfo.balance = await this.tokenService.getTokenBalance( - accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] + accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], ); } }); @@ -269,7 +272,7 @@ export class UserService { async getAccountByPhone( phoneNumber: string, - limit: number = 100 + limit: number = 100, ): Promise> { const accountSubject: Subject = new Subject(); this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber)) @@ -301,7 +304,7 @@ export class UserService { const keywords = product.toLowerCase().split(' '); for (const keyword of keywords) { const queriedCategory: string = Object.keys(categories).find((key) => - categories[key].includes(keyword) + categories[key].includes(keyword), ); if (queriedCategory) { return queriedCategory; @@ -326,7 +329,7 @@ export class UserService { const savedIndex = this.accounts.findIndex( (acc) => acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] === - account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] + account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], ); if (savedIndex === 0) { return; diff --git a/src/app/_workers/fetch-accounts.worker.ts b/src/app/_workers/fetch-accounts.worker.ts index 519b760..21f5bbe 100644 --- a/src/app/_workers/fetch-accounts.worker.ts +++ b/src/app/_workers/fetch-accounts.worker.ts @@ -8,6 +8,7 @@ import * as vCard from 'vcard-parser'; const headers = { 'x-cic-automerge': 'client', }; + const options = { headers, }; @@ -16,17 +17,23 @@ addEventListener('message', async ({ data }) => { if (data.addresses instanceof Array) { for (const accountAddress of data.addresses) { try { - const account = await getAccountByAddress(accountAddress, data.url); + const account = await getAccountByAddress(accountAddress, data.url, data.token); postMessage(account); } catch (error) { - throw Error(error); + console.log(`ERROR we failed to get account {accountAddress}`, error); } } } }); -async function getAccountByAddress(accountAddress: string, metaUrl: string): Promise { +async function getAccountByAddress( + accountAddress: string, + metaUrl: string, + token: string, +): Promise { const userKey = await User.toKey(add0x(accountAddress)); + + headers['Authorization'] = 'Bearer ' + token; const response = await fetch(`${metaUrl}/${userKey}`, options) .then((res) => { if (res.ok) { From 45f2bc9ea8325f08b7f7400306a447424a270b5e Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Mon, 2 Aug 2021 22:22:38 +0300 Subject: [PATCH 2/3] Lint files. --- src/app/_helpers/index.ts | 2 +- src/app/_services/user.service.ts | 24 +++++++++++------------ src/app/_workers/fetch-accounts.worker.ts | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/app/_helpers/index.ts b/src/app/_helpers/index.ts index 1bdb553..3adb859 100644 --- a/src/app/_helpers/index.ts +++ b/src/app/_helpers/index.ts @@ -10,4 +10,4 @@ export * from '@app/_helpers/read-csv'; export * from '@app/_helpers/schema-validation'; export * from '@app/_helpers/sync'; export * from '@app/_helpers/online-status'; -export * from './to-hex'; +export * from '@app/_helpers/to-hex'; diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 707f3b8..49c99f4 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -46,7 +46,7 @@ export class UserService { constructor( private httpClient: HttpClient, private loggingService: LoggingService, - private tokenService: TokenService, + private tokenService: TokenService ) {} async init(): Promise { @@ -81,7 +81,7 @@ export class UserService { userLocation: string, location: string, locationType: string, - oldPhoneNumber: string, + oldPhoneNumber: string ): Promise { const accountInfo: any = { vcard: { @@ -142,11 +142,11 @@ export class UserService { this.loggingService.sendErrorLevelMessage( 'Cannot find account info in meta service', this, - { error }, + { error } ); const syncableAccount: Syncable = new Syncable(accountKey, accountInfo); await this.updateMeta(syncableAccount, accountKey, this.headers); - }, + } ); if (phoneNumber !== oldPhoneNumber) { const oldPhoneKey: string = await Phone.toKey(oldPhoneNumber); @@ -162,7 +162,7 @@ export class UserService { async updateMeta( syncableAccount: Syncable, accountKey: string, - headers: HttpHeaders, + headers: HttpHeaders ): Promise { const envelope: Envelope = await this.wrap(syncableAccount, this.signer); const reqBody: string = envelope.toJSON(); @@ -223,7 +223,7 @@ export class UserService { this.tokenService.load.subscribe(async (status: boolean) => { if (status) { data.balance = await this.tokenService.getTokenBalance( - data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], + data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ); } }); @@ -237,7 +237,7 @@ export class UserService { }); } else { this.loggingService.sendInfoLevelMessage( - 'Web workers are not supported in this environment', + 'Web workers are not supported in this environment' ); for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { await this.getAccountByAddress(accountAddress, limit); @@ -252,7 +252,7 @@ export class UserService { async getAccountByAddress( accountAddress: string, limit: number = 100, - history: boolean = false, + history: boolean = false ): Promise> { const accountSubject: Subject = new Subject(); this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress))) @@ -272,7 +272,7 @@ export class UserService { this.tokenService.load.subscribe(async (status: boolean) => { if (status) { accountInfo.balance = await this.tokenService.getTokenBalance( - accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], + accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ); } }); @@ -286,7 +286,7 @@ export class UserService { async getAccountByPhone( phoneNumber: string, - limit: number = 100, + limit: number = 100 ): Promise> { const accountSubject: Subject = new Subject(); this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber)) @@ -318,7 +318,7 @@ export class UserService { const keywords = product.toLowerCase().split(' '); for (const keyword of keywords) { const queriedCategory: string = Object.keys(categories).find((key) => - categories[key].includes(keyword), + categories[key].includes(keyword) ); if (queriedCategory) { return queriedCategory; @@ -343,7 +343,7 @@ export class UserService { const savedIndex = this.accounts.findIndex( (acc) => acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] === - account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0], + account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ); if (savedIndex === 0) { return; diff --git a/src/app/_workers/fetch-accounts.worker.ts b/src/app/_workers/fetch-accounts.worker.ts index 21f5bbe..2626e18 100644 --- a/src/app/_workers/fetch-accounts.worker.ts +++ b/src/app/_workers/fetch-accounts.worker.ts @@ -29,7 +29,7 @@ addEventListener('message', async ({ data }) => { async function getAccountByAddress( accountAddress: string, metaUrl: string, - token: string, + token: string ): Promise { const userKey = await User.toKey(add0x(accountAddress)); From 92a967a23d8e78e2ae020643055f94c3843976e2 Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Tue, 3 Aug 2021 10:46:08 -0700 Subject: [PATCH 3/3] catch account collector error --- src/app/_workers/fetch-accounts.worker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/_workers/fetch-accounts.worker.ts b/src/app/_workers/fetch-accounts.worker.ts index 2626e18..078aa80 100644 --- a/src/app/_workers/fetch-accounts.worker.ts +++ b/src/app/_workers/fetch-accounts.worker.ts @@ -20,7 +20,7 @@ addEventListener('message', async ({ data }) => { const account = await getAccountByAddress(accountAddress, data.url, data.token); postMessage(account); } catch (error) { - console.log(`ERROR we failed to get account {accountAddress}`, error); + console.log(`ERROR we failed to get account ${accountAddress}`, error); } } } @@ -29,7 +29,7 @@ addEventListener('message', async ({ data }) => { async function getAccountByAddress( accountAddress: string, metaUrl: string, - token: string + token: string, ): Promise { const userKey = await User.toKey(add0x(accountAddress));