diff --git a/native/backup/backup-handler.js b/native/backup/backup-handler.js --- a/native/backup/backup-handler.js +++ b/native/backup/backup-handler.js @@ -4,7 +4,10 @@ import * as React from 'react'; import { createUserKeysBackupActionTypes } from 'lib/actions/backup-actions.js'; -import { useCurrentIdentityUserState } from 'lib/hooks/peer-list-hooks.js'; +import { + useCurrentIdentityUserState, + type CurrentIdentityUserState, +} from 'lib/hooks/peer-list-hooks.js'; import { useCheckIfPrimaryDevice } from 'lib/hooks/primary-device-hooks.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; @@ -39,6 +42,7 @@ const identityContext = React.useContext(IdentityClientContext); invariant(identityContext, 'Identity context should be set'); + const { getAuthMetadata } = identityContext; const getCurrentIdentityUserState = useCurrentIdentityUserState(); const migrateToNewFlow = useMigrationToNewFlow(); @@ -100,19 +104,16 @@ void (async () => { backupUploadInProgress.current = true; - const isPrimaryDevice = await checkIfPrimaryDevice(); - const { getAuthMetadata } = identityContext; - const { userID, deviceID } = await getAuthMetadata(); - let currentIdentityUserState, deviceListIsSigned; + const [isPrimaryDevice, { userID, deviceID }] = await Promise.all([ + checkIfPrimaryDevice(), + getAuthMetadata(), + ]); + + // CurrentIdentityUserState is required to check if migration to + // new flow is needed. + let currentIdentityUserState: ?CurrentIdentityUserState = null; try { currentIdentityUserState = await getCurrentIdentityUserState(); - - deviceListIsSigned = - !!currentIdentityUserState.currentDeviceList.curPrimarySignature; - if (!isPrimaryDevice && deviceListIsSigned) { - backupUploadInProgress.current = false; - return; - } } catch (err) { const message = getMessageForException(err) ?? 'unknown error'; showAlertToStaff('Error fetching current device list:', message); @@ -121,6 +122,19 @@ return; } + const deviceListIsSigned = + currentIdentityUserState.currentDeviceList.curPrimarySignature; + + // Early return is safe: + // - in the case of non-primary device, the attempt to upload + // a backup is not needed + // - in the case of a signed device list there is no need + // to perform the migration. + if (!isPrimaryDevice && deviceListIsSigned) { + backupUploadInProgress.current = false; + return; + } + const shouldDoMigration = usingRestoreFlow && (!latestBackupInfo || !deviceListIsSigned); if (!shouldDoMigration && !isPrimaryDevice) { @@ -130,6 +144,10 @@ try { const promise = (async () => { if (shouldDoMigration && !deviceListIsSigned) { + if (!currentIdentityUserState) { + throw new Error('Missing currentIdentityUserState'); + } + return await migrateToNewFlow( userID, deviceID, @@ -160,9 +178,9 @@ checkIfPrimaryDevice, createUserKeysBackup, dispatchActionPromise, + getAuthMetadata, getCurrentIdentityUserState, handlerStarted, - identityContext, latestBackupInfo, migrateToNewFlow, showAlertToStaff,