From 5adca482bdc88ac586f4beb151abdf6a0df55f15 Mon Sep 17 00:00:00 2001 From: warrickct Date: Tue, 8 Mar 2022 15:42:07 +1100 Subject: [PATCH] Fix open groups not being restored when restoring device from recovery phrase. --- ts/receiver/configMessage.ts | 53 +++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/ts/receiver/configMessage.ts b/ts/receiver/configMessage.ts index da70fe075..26aa8c412 100644 --- a/ts/receiver/configMessage.ts +++ b/ts/receiver/configMessage.ts @@ -57,22 +57,18 @@ async function handleGroupsAndContactsFromConfigMessage( ) { const lastConfigUpdate = await getItemById(hasSyncedInitialConfigurationItem); const lastConfigTimestamp = lastConfigUpdate?.timestamp; - const isNewerConfig = lastConfigTimestamp && lastConfigTimestamp < _.toNumber(envelope.timestamp); + const isNewerConfig = + !lastConfigTimestamp || + (lastConfigTimestamp && lastConfigTimestamp < _.toNumber(envelope.timestamp)); - if (isNewerConfig) { - window?.log?.info( - 'Dropping configuration groups change as we already handled one... Only handling contacts ' - ); - if (isNewerConfig) { - if (configMessage.contacts?.length) { - await Promise.all( - configMessage.contacts.map(async c => handleContactReceived(c, envelope)) - ); - } - return; - } + if (!isNewerConfig) { + return; } + window?.log?.info( + 'Dropping configuration groups change as we already handled one... Only handling contacts ' + ); + await createOrUpdateItem({ id: 'hasSyncedInitialConfigurationItem', value: true, @@ -103,12 +99,22 @@ async function handleGroupsAndContactsFromConfigMessage( }) ); - const numberOpenGroup = configMessage.openGroups?.length || 0; + await handleOpenGroupsFromConfig(configMessage.openGroups); + + if (configMessage.contacts?.length) { + await Promise.all(configMessage.contacts.map(async c => handleContactFromConfig(c, envelope))); + } +} - // Trigger a join for all open groups we are not already in. - // Currently, if you left an open group but kept the conversation, you won't rejoin it here. +/** + * Trigger a join for all open groups we are not already in. + * Currently, if you left an open group but kept the conversation, you won't rejoin it here. + * @param openGroups string array of open group urls + */ +const handleOpenGroupsFromConfig = async (openGroups: Array) => { + const numberOpenGroup = openGroups?.length || 0; for (let i = 0; i < numberOpenGroup; i++) { - const currentOpenGroupUrl = configMessage.openGroups[i]; + const currentOpenGroupUrl = openGroups[i]; const parsedRoom = parseOpenGroupV2(currentOpenGroupUrl); if (!parsedRoom) { continue; @@ -121,12 +127,15 @@ async function handleGroupsAndContactsFromConfigMessage( void joinOpenGroupV2WithUIEvents(currentOpenGroupUrl, false, true); } } - if (configMessage.contacts?.length && isNewerConfig) { - await Promise.all(configMessage.contacts.map(async c => handleContactReceived(c, envelope))); - } -} +}; -const handleContactReceived = async ( +/** + * Handles adding of a contact and setting approval/block status + * @param contactReceived Contact to sync + * @param envelope + * @returns + */ +const handleContactFromConfig = async ( contactReceived: SignalService.ConfigurationMessage.IContact, envelope: EnvelopePlus ) => {