diff --git a/ts/receiver/closedGroups.ts b/ts/receiver/closedGroups.ts index 2c8d6cc74..9dab0f735 100644 --- a/ts/receiver/closedGroups.ts +++ b/ts/receiver/closedGroups.ts @@ -193,10 +193,8 @@ export async function handleNewClosedGroup( const maybeConvo = ConversationController.getInstance().get(groupId); - const groupExists = !!maybeConvo; - - if (groupExists) { - if (maybeConvo && (maybeConvo.get('isKickedFromGroup') || maybeConvo.get('left'))) { + if (maybeConvo) { + if (maybeConvo.get('isKickedFromGroup') || maybeConvo.get('left')) { // TODO: indicate that we've been re-invited // to the group if that is the case @@ -205,10 +203,30 @@ export async function handleNewClosedGroup( maybeConvo.set('left', false); maybeConvo.set('lastJoinedTimestamp', _.toNumber(envelope.timestamp)); } else { - window?.log?.warn( - 'Ignoring a closed group message of type NEW: the conversation already exists' + const ecKeyPairAlreadyExistingConvo = new ECKeyPair( + // tslint:disable: no-non-null-assertion + encryptionKeyPair!.publicKey, + encryptionKeyPair!.privateKey ); + const isKeyPairAlreadyHere = await isKeyPairAlreadySaved( + groupId, + ecKeyPairAlreadyExistingConvo.toHexKeyPair() + ); + + if (isKeyPairAlreadyHere) { + await getAllEncryptionKeyPairsForGroup(groupId); + window.log.info('Dropping already saved keypair for group', groupId); + await removeFromCache(envelope); + return; + } + + window.log.info(`Received the encryptionKeyPair for new group ${groupId}`); + + await addClosedGroupEncryptionKeyPair(groupId, ecKeyPairAlreadyExistingConvo.toHexKeyPair()); await removeFromCache(envelope); + window.log.warn( + 'Closed group message of type NEW: the conversation already exists, but we saved the new encryption keypair' + ); return; } }