Skip to content

Commit

Permalink
[native] improve conditions about performing migration
Browse files Browse the repository at this point in the history
Summary:
[ENG-10094](https://linear.app/comm/issue/ENG-10094/migration-to-v2-is-not-working-on-non-primary-device).

Added more descriptive conditions to make this code easier to understand.

Depends on D14213

Test Plan: Tested later in the stack

Reviewers: bartek, tomek

Reviewed By: bartek, tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D14214
  • Loading branch information
xsanm committed Jan 20, 2025
1 parent 325cb59 commit 72769c2
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions native/backup/backup-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,41 @@ function BackupHandler(): null {
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;
}
// In case of unsigned device list migration and backup upload is needed.
// Uploading backup in this case is handled by `migrateToNewFlow`.
const shouldDoMigration = usingRestoreFlow && !deviceListIsSigned;

const shouldDoMigration =
usingRestoreFlow && (!latestBackupInfo || !deviceListIsSigned);
if (!shouldDoMigration && !isPrimaryDevice) {
// When this is a primary device and there is no latest backup it
// needs to be updated. This handles cases after restoration
// or after registration.
const shouldCreateUserKeysBackup = isPrimaryDevice && !latestBackupInfo;

if (!shouldDoMigration && !shouldCreateUserKeysBackup) {
backupUploadInProgress.current = false;
return;
}

try {
const promise = (async () => {
if (shouldDoMigration && !deviceListIsSigned) {
if (shouldDoMigration) {
if (!currentIdentityUserState) {
throw new Error('Missing currentIdentityUserState');
}

// Early return without checking `shouldCreateUserKeysBackup`
// is safe because migration is uploading User Keys backup.
return await migrateToNewFlow(
userID,
deviceID,
currentIdentityUserState,
);
} else {
const backupID = await createUserKeysBackup();
return {
backupID,
timestamp: Date.now(),
};
}

const backupID = await createUserKeysBackup();
return {
backupID,
timestamp: Date.now(),
};
})();
void dispatchActionPromise(createUserKeysBackupActionTypes, promise);
await promise;
Expand Down

0 comments on commit 72769c2

Please sign in to comment.