Use single WebAPI instance across the app

This commit is contained in:
Fedor Indutny 2021-07-23 10:23:50 -07:00 committed by GitHub
parent 79633a9e7b
commit fdec47d637
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 218 additions and 308 deletions

View file

@ -1,29 +1,35 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { EventEmitter } from 'events';
import { WebAPICredentials } from '../Types.d';
import { StorageInterface } from '../../types/Storage.d';
import Helpers from '../Helpers';
export class User {
constructor(private readonly storage: StorageInterface) {}
export type SetCredentialsOptions = {
uuid?: string;
number: string;
deviceId: number;
deviceName?: string;
password: string;
};
public async setNumberAndDeviceId(
number: string,
deviceId: number,
deviceName?: string
): Promise<void> {
await this.storage.put('number_id', `${number}.${deviceId}`);
if (deviceName) {
await this.storage.put('device_name', deviceName);
}
export class User extends EventEmitter {
constructor(private readonly storage: StorageInterface) {
super();
}
public async setUuidAndDeviceId(
uuid: string,
deviceId: number
): Promise<void> {
return this.storage.put('uuid_id', `${uuid}.${deviceId}`);
await this.storage.put('uuid_id', `${uuid}.${deviceId}`);
window.log.info('storage.user: uuid and device id changed');
this.emit('credentialsChange');
}
public getNumber(): string | undefined {
@ -62,6 +68,41 @@ export class User {
return this.storage.remove('signaling_key');
}
public async setCredentials(
credentials: SetCredentialsOptions
): Promise<void> {
const { uuid, number, deviceId, deviceName, password } = credentials;
await Promise.all([
this.storage.put('number_id', `${number}.${deviceId}`),
this.storage.put('uuid_id', `${uuid}.${deviceId}`),
this.storage.put('password', password),
deviceName
? this.storage.put('device_name', deviceName)
: Promise.resolve(),
]);
window.log.info('storage.user: credentials changed');
this.emit('credentialsChange');
}
public async removeCredentials(): Promise<void> {
await Promise.all([
this.storage.remove('number_id'),
this.storage.remove('uuid_id'),
this.storage.remove('password'),
this.storage.remove('device_name'),
]);
}
public getWebAPICredentials(): WebAPICredentials {
return {
username:
this.storage.get('uuid_id') || this.storage.get('number_id') || '',
password: this.storage.get('password', ''),
};
}
private _getDeviceIdFromUuid(): string | undefined {
const uuid = this.storage.get('uuid_id');
if (uuid === undefined) return undefined;
@ -73,4 +114,25 @@ export class User {
if (numberId === undefined) return undefined;
return Helpers.unencodeNumber(numberId)[1];
}
//
// EventEmitter typing
//
public on(type: 'credentialsChange', callback: () => void): this;
public on(
type: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (...args: Array<any>) => void
): this {
return super.on(type, listener);
}
public emit(type: 'credentialsChange'): boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public emit(type: string | symbol, ...args: Array<any>): boolean {
return super.emit(type, ...args);
}
}