Enforce stronger types for ArrayBuffers and storage

This commit is contained in:
Fedor Indutny 2021-06-14 17:09:37 -07:00 committed by GitHub
parent 61ac79e9ae
commit 8f5086227a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 748 additions and 675 deletions

View file

@ -0,0 +1,98 @@
// Copyright 2016-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { without } from 'lodash';
import { StorageInterface } from '../../types/Storage.d';
const BLOCKED_NUMBERS_ID = 'blocked';
const BLOCKED_UUIDS_ID = 'blocked-uuids';
const BLOCKED_GROUPS_ID = 'blocked-groups';
export class Blocked {
constructor(private readonly storage: StorageInterface) {}
public getBlockedNumbers(): Array<string> {
return this.storage.get(BLOCKED_NUMBERS_ID, new Array<string>());
}
public isBlocked(number: string): boolean {
return this.getBlockedNumbers().includes(number);
}
public async addBlockedNumber(number: string): Promise<void> {
const numbers = this.getBlockedNumbers();
if (numbers.includes(number)) {
return;
}
window.log.info('adding', number, 'to blocked list');
await this.storage.put(BLOCKED_NUMBERS_ID, numbers.concat(number));
}
public async removeBlockedNumber(number: string): Promise<void> {
const numbers = this.getBlockedNumbers();
if (!numbers.includes(number)) {
return;
}
window.log.info('removing', number, 'from blocked list');
await this.storage.put(BLOCKED_NUMBERS_ID, without(numbers, number));
}
public getBlockedUuids(): Array<string> {
return this.storage.get(BLOCKED_UUIDS_ID, new Array<string>());
}
public isUuidBlocked(uuid: string): boolean {
return this.getBlockedUuids().includes(uuid);
}
public async addBlockedUuid(uuid: string): Promise<void> {
const uuids = this.getBlockedUuids();
if (uuids.includes(uuid)) {
return;
}
window.log.info('adding', uuid, 'to blocked list');
await this.storage.put(BLOCKED_UUIDS_ID, uuids.concat(uuid));
}
public async removeBlockedUuid(uuid: string): Promise<void> {
const numbers = this.getBlockedUuids();
if (!numbers.includes(uuid)) {
return;
}
window.log.info('removing', uuid, 'from blocked list');
await this.storage.put(BLOCKED_UUIDS_ID, without(numbers, uuid));
}
public getBlockedGroups(): Array<string> {
return this.storage.get(BLOCKED_GROUPS_ID, new Array<string>());
}
public isGroupBlocked(groupId: string): boolean {
return this.getBlockedGroups().includes(groupId);
}
public async addBlockedGroup(groupId: string): Promise<void> {
const groupIds = this.getBlockedGroups();
if (groupIds.includes(groupId)) {
return;
}
window.log.info(`adding group(${groupId}) to blocked list`);
await this.storage.put(BLOCKED_GROUPS_ID, groupIds.concat(groupId));
}
public async removeBlockedGroup(groupId: string): Promise<void> {
const groupIds = this.getBlockedGroups();
if (!groupIds.includes(groupId)) {
return;
}
window.log.info(`removing group(${groupId} from blocked list`);
await this.storage.put(BLOCKED_GROUPS_ID, without(groupIds, groupId));
}
}

View file

@ -0,0 +1,76 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { StorageInterface } from '../../types/Storage.d';
import Helpers from '../Helpers';
export class User {
constructor(private readonly storage: StorageInterface) {}
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);
}
}
public async setUuidAndDeviceId(
uuid: string,
deviceId: number
): Promise<void> {
return this.storage.put('uuid_id', `${uuid}.${deviceId}`);
}
public getNumber(): string | undefined {
const numberId = this.storage.get('number_id');
if (numberId === undefined) return undefined;
return Helpers.unencodeNumber(numberId)[0];
}
public getUuid(): string | undefined {
const uuid = this.storage.get('uuid_id');
if (uuid === undefined) return undefined;
return Helpers.unencodeNumber(uuid.toLowerCase())[0];
}
public getDeviceId(): number | undefined {
const value = this._getDeviceIdFromUuid() || this._getDeviceIdFromNumber();
if (value === undefined) {
return undefined;
}
return parseInt(value, 10);
}
public getDeviceName(): string | undefined {
return this.storage.get('device_name');
}
public async setDeviceNameEncrypted(): Promise<void> {
return this.storage.put('deviceNameEncrypted', true);
}
public getDeviceNameEncrypted(): boolean | undefined {
return this.storage.get('deviceNameEncrypted');
}
public getSignalingKey(): ArrayBuffer | undefined {
return this.storage.get('signaling_key');
}
private _getDeviceIdFromUuid(): string | undefined {
const uuid = this.storage.get('uuid_id');
if (uuid === undefined) return undefined;
return Helpers.unencodeNumber(uuid)[1];
}
private _getDeviceIdFromNumber(): string | undefined {
const numberId = this.storage.get('number_id');
if (numberId === undefined) return undefined;
return Helpers.unencodeNumber(numberId)[1];
}
}