From 6d28f343c945686f12252dcee69f7d0d66916656 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Thu, 11 Feb 2021 10:23:21 +1100 Subject: [PATCH] try to decrypt unprocessed message when we get a new encryptionkeypair --- ts/receiver/cache.ts | 56 +++++++++++++++++++++++++++++++++++-- ts/receiver/closedGroups.ts | 6 +++- ts/receiver/receiver.ts | 14 +++++++++- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/ts/receiver/cache.ts b/ts/receiver/cache.ts index 45264d404..02a0c2be9 100644 --- a/ts/receiver/cache.ts +++ b/ts/receiver/cache.ts @@ -31,9 +31,7 @@ export async function addToCache( return window.textsecure.storage.unprocessed.add(data); } -export async function getAllFromCache() { - window.log.info('getAllFromCache'); - +async function fetchAllFromCache(): Promise> { const { textsecure } = window; const count = await textsecure.storage.unprocessed.getCount(); @@ -47,7 +45,59 @@ export async function getAllFromCache() { } const items = await textsecure.storage.unprocessed.getAll(); + return items; +} + +export async function getAllFromCache() { + window.log.info('getAllFromCache'); + const items = await fetchAllFromCache(); + window.log.info('getAllFromCache loaded', items.length, 'saved envelopes'); + const { textsecure } = window; + + return Promise.all( + _.map(items, async (item: any) => { + const attempts = _.toNumber(item.attempts || 0) + 1; + + try { + if (attempts >= 10) { + window.log.warn( + 'getAllFromCache final attempt for envelope', + item.id + ); + await textsecure.storage.unprocessed.remove(item.id); + } else { + await textsecure.storage.unprocessed.updateAttempts( + item.id, + attempts + ); + } + } catch (error) { + window.log.error( + 'getAllFromCache error updating item after load:', + error && error.stack ? error.stack : error + ); + } + + return item; + }) + ); +} + +export async function getAllFromCacheForSource(source: string) { + const items = await fetchAllFromCache(); + + // keep items without source too (for old message already added to the cache) + const itemsFromSource = items.filter( + item => !!item.senderIdentity || item.senderIdentity === source + ); + + window.log.info( + 'getAllFromCacheForSource loaded', + itemsFromSource.length, + 'saved envelopes' + ); + const { textsecure } = window; return Promise.all( _.map(items, async (item: any) => { diff --git a/ts/receiver/closedGroups.ts b/ts/receiver/closedGroups.ts index 56708b2ef..43a43e063 100644 --- a/ts/receiver/closedGroups.ts +++ b/ts/receiver/closedGroups.ts @@ -29,8 +29,8 @@ import { ConversationModel } from '../../js/models/conversations'; import _ from 'lodash'; import { forceSyncConfigurationNowIfNeeded } from '../session/utils/syncUtils'; import { MessageController } from '../session/messages'; -import { ClosedGroupEncryptionPairMessage } from '../session/messages/outgoing'; import { ClosedGroupEncryptionPairReplyMessage } from '../session/messages/outgoing/content/data/group'; +import { queueAllCachedFromSource } from './receiver'; export async function handleClosedGroupControlMessage( envelope: EnvelopePlus, @@ -250,6 +250,8 @@ export async function handleNewClosedGroup( window.SwarmPolling.addGroupId(PubKey.cast(groupId)); await removeFromCache(envelope); + // trigger decrypting of all this group messages we did not decrypt successfully yet. + await queueAllCachedFromSource(groupId); } async function handleUpdateClosedGroup( @@ -465,6 +467,8 @@ async function handleClosedGroupEncryptionKeyPair( await addClosedGroupEncryptionKeyPair(groupPublicKey, keyPair.toHexKeyPair()); await removeFromCache(envelope); + // trigger decrypting of all this group messages we did not decrypt successfully yet. + await queueAllCachedFromSource(groupPublicKey); } async function performIfValid( diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index 02140be0e..84f414ac7 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -3,7 +3,12 @@ import { EnvelopePlus } from './types'; export { downloadAttachment } from './attachments'; -import { addToCache, getAllFromCache, removeFromCache } from './cache'; +import { + addToCache, + getAllFromCache, + getAllFromCacheForSource, + removeFromCache, +} from './cache'; import { processMessage } from '../session/snode_api/swarmPolling'; import { onError } from './errors'; @@ -189,6 +194,13 @@ export async function queueAllCached() { }); } +export async function queueAllCachedFromSource(source: string) { + const items = await getAllFromCacheForSource(source); + items.forEach(async item => { + await queueCached(item); + }); +} + async function queueCached(item: any) { const { textsecure } = window;