From 883bd3adb859da139530d95e5ae13941bf6b1f02 Mon Sep 17 00:00:00 2001 From: Andy Balaam Date: Mon, 30 Mar 2026 15:15:32 +0100 Subject: [PATCH] Remodel DeviceListener ifs to be more uniform We enter this chain of `if`s if at lease one of the boolean flags is false. This change attempts to make the logic clearer by ensuring that each branch of the if matches exactly one of those booleans. Justifying this change: if `secretStorageStatus.defaultKeyId === null` then `recoveryKeyIsOk` will be false, except in the strange case where recovery is disabled, in which case we should not be dealing with problems with recovery anyway, so if we previously entered this branch it would have been a bug. All existing tests pass so it looks like we didn't consider this case. --- .../DeviceListenerCurrentDevice.ts | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/web/src/device-listener/DeviceListenerCurrentDevice.ts b/apps/web/src/device-listener/DeviceListenerCurrentDevice.ts index 6bed9182e5..7f9145aac3 100644 --- a/apps/web/src/device-listener/DeviceListenerCurrentDevice.ts +++ b/apps/web/src/device-listener/DeviceListenerCurrentDevice.ts @@ -214,28 +214,30 @@ export class DeviceListenerCurrentDevice { } else if (!keyBackupUploadIsOk) { logSpan.info("Key backup upload is unexpectedly turned off: setting state to TURN_ON_KEY_STORAGE"); await this.setDeviceState("turn_on_key_storage", logSpan); - } else if (secretStorageStatus.defaultKeyId === null) { - // The user just hasn't set up 4S yet: if they have key - // backup, prompt them to turn on recovery too. (If not, they - // have explicitly opted out, so don't hassle them.) - if (recoveryDisabled) { - logSpan.info("Recovery disabled: no toast needed"); - await this.setDeviceState("ok", logSpan); - } else if (keyBackupUploadActive) { - logSpan.info("No default 4S key: setting state to SET_UP_RECOVERY"); - await this.setDeviceState("set_up_recovery", logSpan); - } else { - logSpan.info("No default 4S key but backup disabled: no toast needed"); - await this.setDeviceState("ok", logSpan); - } } else if (!recoveryIsOk) { - logSpan.warn("4S is missing secrets: setting state to KEY_STORAGE_OUT_OF_SYNC", { - secretStorageStatus, - allCrossSigningSecretsCached, - isCurrentDeviceTrusted, - keyBackupDownloadIsOk, - }); - await this.setDeviceState("key_storage_out_of_sync", logSpan); + if (secretStorageStatus.defaultKeyId === null) { + // The user just hasn't set up 4S yet: if they have key + // backup, prompt them to turn on recovery too. (If not, they + // have explicitly opted out, so don't hassle them.) + if (recoveryDisabled) { + logSpan.info("Recovery disabled: no toast needed"); + await this.setDeviceState("ok", logSpan); + } else if (keyBackupUploadActive) { + logSpan.info("No default 4S key: setting state to SET_UP_RECOVERY"); + await this.setDeviceState("set_up_recovery", logSpan); + } else { + logSpan.info("No default 4S key but backup disabled: no toast needed"); + await this.setDeviceState("ok", logSpan); + } + } else { + logSpan.warn("4S is missing secrets: setting state to KEY_STORAGE_OUT_OF_SYNC", { + secretStorageStatus, + allCrossSigningSecretsCached, + isCurrentDeviceTrusted, + keyBackupDownloadIsOk, + }); + await this.setDeviceState("key_storage_out_of_sync", logSpan); + } } else if (!keyBackupDownloadIsOk) { logSpan.warn("Backup key is not cached locally: setting state to KEY_STORAGE_OUT_OF_SYNC", { secretStorageStatus,