Refactor update of data on meta service.

This commit is contained in:
Spencer Ofwiti
2021-04-25 13:32:23 +03:00
parent affffa8ffe
commit a2e320d923
5 changed files with 59 additions and 46 deletions

View File

@@ -22,12 +22,17 @@ export class AuthService {
private loggingService: LoggingService,
private errorDialogService: ErrorDialogService
) {
// TODO setting these together shoulds be atomic
}
async init(): Promise<any> {
await this.mutableKeyStore.loadKeyring();
// TODO setting these together should be atomic
if (sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'))) {
this.sessionToken = sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN'));
}
if (localStorage.getItem(btoa('CICADA_PRIVATE_KEY'))) {
this.privateKey = localStorage.getItem(btoa('CICADA_PRIVATE_KEY'));
await this.getPrivateKeys();
}
}

View File

@@ -1,16 +1,17 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of, Subject} from 'rxjs';
import {BehaviorSubject, Observable, Subject} from 'rxjs';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {environment} from '@src/environments/environment';
import {first} from 'rxjs/operators';
import {ArgPair, Envelope, Phone, Syncable, User} from 'cic-client-meta';
import {AccountDetails, MetaResponse} from '@app/_models';
import {AccountDetails} from '@app/_models';
import {LoggingService} from '@app/_services/logging.service';
import {TokenService} from '@app/_services/token.service';
import {AccountIndex} from '@app/_eth';
import {MutableKeyStore, MutablePgpKeyStore, PGPSigner, Signer} from '@app/_pgp';
import {MutableKeyStore, PGPSigner, Signer} from '@app/_pgp';
import {RegistryService} from '@app/_services/registry.service';
import {CICRegistry} from 'cic-client';
import {AuthService} from './auth.service';
const vCard = require('vcard-parser');
@Injectable({
@@ -18,8 +19,8 @@ const vCard = require('vcard-parser');
})
export class UserService {
headers: HttpHeaders = new HttpHeaders({'x-cic-automerge': 'client'});
keystore: MutableKeyStore = new MutablePgpKeyStore();
signer: Signer = new PGPSigner(this.keystore);
keystore: MutableKeyStore;
signer: Signer;
registry: CICRegistry;
accountsMeta = [];
@@ -40,7 +41,12 @@ export class UserService {
private loggingService: LoggingService,
private tokenService: TokenService,
private registryService: RegistryService,
private authService: AuthService,
) {
this.authService.init().then(() => {
this.keystore = authService.mutableKeyStore;
this.signer = new PGPSigner(this.keystore);
});
this.registry = registryService.getRegistry();
this.registry.load();
}
@@ -60,10 +66,16 @@ export class UserService {
}
async changeAccountInfo(address: string, name: string, phoneNumber: string, age: string, type: string, bio: string, gender: string,
businessCategory: string, userLocation: string, location: string, locationType: string, metaAccount: MetaResponse
businessCategory: string, userLocation: string, location: string, locationType: string,
): Promise<any> {
let reqBody = metaAccount;
let accountInfo = reqBody.m.data;
let accountInfo: any = {
vcard: {
fn: [{}],
n: [{}],
tel: [{}]
},
location: {}
};
accountInfo.vcard.fn[0].value = name;
accountInfo.vcard.n[0].value = name.split(' ');
accountInfo.vcard.tel[0].value = phoneNumber;
@@ -75,14 +87,13 @@ export class UserService {
accountInfo.location.area = location;
accountInfo.location.area_name = userLocation;
accountInfo.location.area_type = locationType;
accountInfo.vcard = vCard.generate(accountInfo.vcard);
reqBody.m.data = accountInfo;
accountInfo.vcard = btoa(vCard.generate(accountInfo.vcard));
const accountKey = await User.toKey(address);
this.httpClient.get(`${environment.cicMetaUrl}/${accountKey}`, { headers: this.headers }).pipe(first()).subscribe(async res => {
this.getAccountDetailsFromMeta(accountKey).pipe(first()).subscribe(async res => {
const syncableAccount: Syncable = Envelope.fromJSON(JSON.stringify(res)).unwrap();
let update = [];
for (const prop in reqBody) {
update.push(new ArgPair(prop, reqBody[prop]));
for (const prop in accountInfo) {
update.push(new ArgPair(prop, accountInfo[prop]));
}
syncableAccount.update(update, 'client-branch');
await this.updateMeta(syncableAccount, accountKey, this.headers);
@@ -184,7 +195,7 @@ export class UserService {
return accountSubject.asObservable();
}
async getAccountByPhone(phoneNumber: string, limit: number = 100): Promise<any> {
async getAccountByPhone(phoneNumber: string, limit: number = 100): Promise<Observable<AccountDetails>> {
let accountSubject = new Subject<any>();
this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber)).pipe(first()).subscribe(async res => {
const response = Envelope.fromJSON(JSON.stringify(res)).unwrap();