getFromStorage complete

pull/1159/head
Vincent 5 years ago
parent b8ec9bd995
commit 193573aa67

@ -1,8 +1,9 @@
import * as Data from '../../../js/modules/data'; import * as Data from '../../../js/modules/data';
import { RawMessage } from '../types/RawMessage'; import { RawMessage } from '../types/RawMessage';
import { ChatMessage, ContentMessage } from '../messages/outgoing'; import { ContentMessage } from '../messages/outgoing';
import * as MessageUtils from '../utils'; import * as MessageUtils from '../utils';
import { PubKey } from '../types'; import { PubKey } from '../types';
import logger from 'redux-logger';
// TODO: We should be able to import functions straight from the db here without going through the window object // TODO: We should be able to import functions straight from the db here without going through the window object
@ -18,11 +19,10 @@ export class PendingMessageCache {
constructor() { constructor() {
// Load pending messages from the database // Load pending messages from the database
// You must call init() on this class in order to load from DB. // You should await init() on making a new PendingMessageCache
// const pendingMessageCache = new PendingMessageCache(); // if you'd like to have instant access to the cache
// await pendingMessageCache.init()
// >> do stuff
this.cache = []; this.cache = [];
void this.init();
} }
public async add( public async add(
@ -80,7 +80,7 @@ export class PendingMessageCache {
} }
public getDevices(): Array<PubKey> { public getDevices(): Array<PubKey> {
// Gets all devices with pending messages // Gets all unique devices with pending messages
const pubkeyStrings = [...new Set(this.cache.map(m => m.device))]; const pubkeyStrings = [...new Set(this.cache.map(m => m.device))];
const pubkeys: Array<PubKey> = []; const pubkeys: Array<PubKey> = [];
@ -100,21 +100,33 @@ export class PendingMessageCache {
private async getFromStorage(): Promise<Array<RawMessage>> { private async getFromStorage(): Promise<Array<RawMessage>> {
// tslint:disable-next-line: no-backbone-get-set-outside-model // tslint:disable-next-line: no-backbone-get-set-outside-model
const pendingMessagesData = await Data.getItemById('pendingMessages'); const data = await Data.getItemById('pendingMessages');
const pendingMessagesJSON = pendingMessagesData if (!data || !data.value) {
? String(pendingMessagesData.value) return [];
: ''; }
// tslint:disable-next-line: no-unnecessary-local-variable const barePending = JSON.parse(String(data.value));
const encodedPendingMessages = pendingMessagesJSON
? JSON.parse(pendingMessagesJSON)
: [];
// Set pubkey from string to PubKey.from() // tslint:disable-next-line: no-unnecessary-local-variable
const pending = barePending.map((message: any) => {
const { identifier, timestamp, device, ttl, encryption } = message;
// Recreate buffers
const rawBuffer = message.plainTextBuffer;
const bufferValues: Array<number> = Object.values(rawBuffer);
const plainTextBuffer = Uint8Array.from(bufferValues);
return {
identifier,
plainTextBuffer,
timestamp,
device,
ttl,
encryption,
} as RawMessage;
});
// TODO: return pending as Array<RawMessage>;
// Build up Uint8Array from painTextBuffer in JSON
return encodedPendingMessages;
} }
private async syncCacheWithDB() { private async syncCacheWithDB() {

@ -1,22 +1,14 @@
import * as crypto from 'crypto'; import * as crypto from 'crypto';
export enum PubKeyCategory {
Primary = 'priamry',
Secondary = 'secondary',
Group = 'group',
}
export class PubKey { export class PubKey {
private static readonly PUBKEY_LEN = 66; private static readonly PUBKEY_LEN = 66;
private static readonly regex: string = `^05[0-9a-fA-F]{${PubKey.PUBKEY_LEN - 2}}$`; private static readonly regex: string = `^05[0-9a-fA-F]{${PubKey.PUBKEY_LEN - 2}}$`;
public readonly key: string; public readonly key: string;
public type?: PubKeyCategory;
constructor(pubkeyString: string, type?: PubKeyCategory) { constructor(pubkeyString: string) {
PubKey.validate(pubkeyString); PubKey.validate(pubkeyString);
this.key = pubkeyString; this.key = pubkeyString;
this.type = type;
} }
public static from(pubkeyString: string): PubKey | undefined { public static from(pubkeyString: string): PubKey | undefined {

Loading…
Cancel
Save