diff --git a/bindings/matrix-sdk-ffi/src/encryption.rs b/bindings/matrix-sdk-ffi/src/encryption.rs index d85498758..f9fec9d99 100644 --- a/bindings/matrix-sdk-ffi/src/encryption.rs +++ b/bindings/matrix-sdk-ffi/src/encryption.rs @@ -434,6 +434,10 @@ impl IdentityResetHandle { self.inner.reset(None).await.map_err(|e| ClientError::Generic { msg: e.to_string() }) } } + + pub async fn cancel(&self) { + self.inner.cancel().await; + } } #[derive(uniffi::Enum)] diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index 0ad9f7148..712c90bff 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -57,7 +57,7 @@ use ruma::{ }, DeviceId, OwnedDeviceId, OwnedUserId, TransactionId, UserId, }; -use tokio::sync::RwLockReadGuard; +use tokio::sync::{Mutex, RwLockReadGuard}; use tracing::{debug, error, instrument, trace, warn}; use url::Url; use vodozemac::Curve25519PublicKey; @@ -246,9 +246,26 @@ pub struct CrossSigningResetHandle { upload_request: UploadSigningKeysRequest, signatures_request: UploadSignaturesRequest, auth_type: CrossSigningResetAuthType, + is_cancelled: Mutex, } impl CrossSigningResetHandle { + /// Set up a new `CrossSigningResetHandle`. + pub fn new( + client: Client, + upload_request: UploadSigningKeysRequest, + signatures_request: UploadSignaturesRequest, + auth_type: CrossSigningResetAuthType, + ) -> Self { + Self { + client, + upload_request, + signatures_request, + auth_type, + is_cancelled: Mutex::new(false), + } + } + /// Get the [`CrossSigningResetAuthType`] this cross-signing reset process /// is using. pub fn auth_type(&self) -> &CrossSigningResetAuthType { @@ -264,6 +281,10 @@ impl CrossSigningResetHandle { // TODO: Do we want to put a limit on this infinite loop? 🤷 while let Err(e) = self.client.send(upload_request.clone(), None).await { + if *self.is_cancelled.lock().await { + return Ok(()); + } + if e.client_api_error_kind() != Some(&ErrorKind::Unrecognized) { return Err(e.into()); } @@ -273,6 +294,11 @@ impl CrossSigningResetHandle { Ok(()) } + + /// Cancel the ongoing identity reset process + pub async fn cancel(&self) { + *self.is_cancelled.lock().await = true; + } } /// information about the additional authentication that is required before the @@ -1231,12 +1257,12 @@ impl Encryption { if let Some(auth_type) = CrossSigningResetAuthType::new(&self.client, &error).await? { let client = self.client.clone(); - Ok(Some(CrossSigningResetHandle { + Ok(Some(CrossSigningResetHandle::new( client, - upload_request: upload_signing_keys_req, - signatures_request: upload_signatures_req, + upload_signing_keys_req, + upload_signatures_req, auth_type, - })) + ))) } else { Err(error.into()) } diff --git a/crates/matrix-sdk/src/encryption/recovery/mod.rs b/crates/matrix-sdk/src/encryption/recovery/mod.rs index 3c1d94864..51a4d214c 100644 --- a/crates/matrix-sdk/src/encryption/recovery/mod.rs +++ b/crates/matrix-sdk/src/encryption/recovery/mod.rs @@ -669,4 +669,9 @@ impl IdentityResetHandle { Ok(()) } + + /// Cancel the ongoing identity reset process + pub async fn cancel(&self) { + self.cross_signing_reset_handle.cancel().await; + } }