import { DecryptionFailureCode } from 'matrix-js-sdk/lib/crypto-api'; /** * [Gitea #159] What to tell the user about an undecryptable message, and the * one thing that fixes it (if anything does). Raw codes never reach the UI * text — they go in the tooltip for support. */ export type DecryptionAction = 'setup-backup' | 'retry' | 'verify-session' | 'none'; export type DecryptionReason = { text: string; action: DecryptionAction; actionLabel?: string; }; export const describeDecryptionFailure = ( code: DecryptionFailureCode | string | null | undefined, ): DecryptionReason => { switch (code) { case DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP: // No backup exists on the server at all: this key is gone for this // session (another signed-in device may still share it), and the fix is // for the future. return { text: 'Sent before you signed in here, and no key backup exists — set one up so your next session can read history.', action: 'setup-backup', actionLabel: 'Set up key backup', }; case DecryptionFailureCode.HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED: // A backup exists but this session can't open it (needs the recovery key // / verification). return { text: "Sent before you signed in here. Your key backup exists but this session can't open it yet.", action: 'verify-session', actionLabel: 'Unlock key backup', }; case DecryptionFailureCode.HISTORICAL_MESSAGE_WORKING_BACKUP: return { text: 'Waiting for the key from your backup…', action: 'retry', actionLabel: 'Retry', }; case DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED: return { text: 'Sent before you joined this room.', action: 'none' }; case DecryptionFailureCode.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE: return { text: "The sender's device won't share keys with unverified sessions.", action: 'verify-session', actionLabel: 'Verify this session', }; case DecryptionFailureCode.MEGOLM_KEY_WITHHELD: return { text: 'The sender chose not to share the key for this message.', action: 'none' }; case DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID: case DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX: // rust-crypto re-requests the key from our other devices by itself // (there is no public re-request API any more); a retry re-runs the // decryption in case it has arrived since. return { text: "This session doesn't have the key yet — it is being requested from your other devices.", action: 'retry', actionLabel: 'Retry', }; case DecryptionFailureCode.SENDER_IDENTITY_PREVIOUSLY_VERIFIED: return { text: "The sender's identity changed since you verified them — re-verify to read this.", action: 'none', }; case DecryptionFailureCode.UNSIGNED_SENDER_DEVICE: return { text: "Sent from a device the sender hasn't verified.", action: 'none' }; case DecryptionFailureCode.UNKNOWN_SENDER_DEVICE: return { text: "Sent from a device we don't know about.", action: 'none' }; default: return { text: 'Unable to decrypt this message.', action: 'none' }; } };