diff --git a/ts/session/sending/PendingMessageCache.ts b/ts/session/sending/PendingMessageCache.ts index 2ae437f6f..b2189af83 100644 --- a/ts/session/sending/PendingMessageCache.ts +++ b/ts/session/sending/PendingMessageCache.ts @@ -1,8 +1,9 @@ import * as Data from '../../../js/modules/data'; import { RawMessage } from '../types/RawMessage'; -import { ChatMessage, ContentMessage } from '../messages/outgoing'; +import { ContentMessage } from '../messages/outgoing'; import * as MessageUtils from '../utils'; 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 @@ -18,11 +19,10 @@ export class PendingMessageCache { constructor() { // Load pending messages from the database - // You must call init() on this class in order to load from DB. - // const pendingMessageCache = new PendingMessageCache(); - // await pendingMessageCache.init() - // >> do stuff + // You should await init() on making a new PendingMessageCache + // if you'd like to have instant access to the cache this.cache = []; + void this.init(); } public async add( @@ -80,7 +80,7 @@ export class PendingMessageCache { } public getDevices(): Array { - // Gets all devices with pending messages + // Gets all unique devices with pending messages const pubkeyStrings = [...new Set(this.cache.map(m => m.device))]; const pubkeys: Array = []; @@ -100,21 +100,33 @@ export class PendingMessageCache { private async getFromStorage(): Promise> { // tslint:disable-next-line: no-backbone-get-set-outside-model - const pendingMessagesData = await Data.getItemById('pendingMessages'); - const pendingMessagesJSON = pendingMessagesData - ? String(pendingMessagesData.value) - : ''; + const data = await Data.getItemById('pendingMessages'); + if (!data || !data.value) { + return []; + } - // tslint:disable-next-line: no-unnecessary-local-variable - const encodedPendingMessages = pendingMessagesJSON - ? JSON.parse(pendingMessagesJSON) - : []; + const barePending = JSON.parse(String(data.value)); - // 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 = Object.values(rawBuffer); + const plainTextBuffer = Uint8Array.from(bufferValues); + + return { + identifier, + plainTextBuffer, + timestamp, + device, + ttl, + encryption, + } as RawMessage; + }); - // TODO: - // Build up Uint8Array from painTextBuffer in JSON - return encodedPendingMessages; + return pending as Array; } private async syncCacheWithDB() { diff --git a/ts/session/types/PubKey.ts b/ts/session/types/PubKey.ts index 9ab308cfb..468b919bb 100644 --- a/ts/session/types/PubKey.ts +++ b/ts/session/types/PubKey.ts @@ -1,22 +1,14 @@ import * as crypto from 'crypto'; -export enum PubKeyCategory { - Primary = 'priamry', - Secondary = 'secondary', - Group = 'group', -} - export class PubKey { private static readonly PUBKEY_LEN = 66; private static readonly regex: string = `^05[0-9a-fA-F]{${PubKey.PUBKEY_LEN - 2}}$`; public readonly key: string; - public type?: PubKeyCategory; - constructor(pubkeyString: string, type?: PubKeyCategory) { + constructor(pubkeyString: string) { PubKey.validate(pubkeyString); this.key = pubkeyString; - this.type = type; } public static from(pubkeyString: string): PubKey | undefined {