Add PNI endpoints to WebAPI

This commit is contained in:
Fedor Indutny 2021-11-30 20:33:51 +01:00 committed by GitHub
parent 7c1ce3366d
commit 348012ef4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 321 additions and 210 deletions

View file

@ -11,7 +11,8 @@ import * as log from '../../logging/log';
import Helpers from '../Helpers';
export type SetCredentialsOptions = {
uuid?: string;
uuid: string;
pni: string;
number: string;
deviceId: number;
deviceName?: string;
@ -58,22 +59,30 @@ export class User {
return Helpers.unencodeNumber(numberId)[0];
}
public getUuid(): UUID | undefined {
public getUuid(uuidKind = UUIDKind.ACI): UUID | undefined {
if (uuidKind === UUIDKind.PNI) {
const pni = this.storage.get('pni');
if (pni === undefined) return undefined;
return new UUID(pni);
}
strictAssert(
uuidKind === UUIDKind.ACI,
`Unsupported uuid kind: ${uuidKind}`
);
const uuid = this.storage.get('uuid_id');
if (uuid === undefined) return undefined;
return new UUID(Helpers.unencodeNumber(uuid.toLowerCase())[0]);
}
public getCheckedUuid(): UUID {
const uuid = this.getUuid();
public getCheckedUuid(uuidKind?: UUIDKind): UUID {
const uuid = this.getUuid(uuidKind);
strictAssert(uuid !== undefined, 'Must have our own uuid');
return uuid;
}
public getPni(): UUID | undefined {
const pni = this.storage.get('pni');
if (pni === undefined) return undefined;
return new UUID(pni);
public async setPni(pni: string): Promise<void> {
await this.storage.put('pni', UUID.cast(pni));
}
public getOurUuidKind(uuid: UUID): UUIDKind {
@ -83,7 +92,7 @@ export class User {
return UUIDKind.ACI;
}
const pni = this.getPni();
const pni = this.getUuid(UUIDKind.PNI);
if (pni?.toString() === uuid.toString()) {
return UUIDKind.PNI;
}
@ -118,12 +127,13 @@ export class User {
public async setCredentials(
credentials: SetCredentialsOptions
): Promise<void> {
const { uuid, number, deviceId, deviceName, password } = credentials;
const { uuid, pni, 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),
this.setPni(pni),
deviceName
? this.storage.put('device_name', deviceName)
: Promise.resolve(),