diff --git a/src/device.rs b/src/device.rs index 35d9989e8..52e964a17 100644 --- a/src/device.rs +++ b/src/device.rs @@ -7,9 +7,7 @@ use crate::{ encryption::EncryptionAlgorithm, future::future_to_promise, identifiers::{self, DeviceId, UserId}, - impl_from_to_inner, - js::try_array_to_vec, - requests, types, verification, vodozemac, + impl_from_to_inner, requests, types, verification, vodozemac, }; /// A device represents a E2EE capable client of an user. @@ -28,10 +26,12 @@ impl Device { /// Returns a Promise for a 2-element array `[VerificationRequest, /// ToDeviceRequest]`. #[wasm_bindgen(js_name = "requestVerification")] - pub fn request_verification(&self, methods: Option) -> Result { - let methods = - methods.map(try_array_to_vec::).transpose()?; + pub fn request_verification( + &self, + methods: Option>, + ) -> Result { let me = self.inner.clone(); + let methods = methods.map(|methods| methods.iter().map(Into::into).collect()); Ok(future_to_promise(async move { let tuple = Array::new(); diff --git a/src/identities.rs b/src/identities.rs index d2db7d790..e98956d70 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -3,10 +3,7 @@ use js_sys::{Array, Promise}; use wasm_bindgen::prelude::*; -use crate::{ - future::future_to_promise, identifiers, impl_from_to_inner, js::try_array_to_vec, requests, - verification, -}; +use crate::{future::future_to_promise, identifiers, impl_from_to_inner, requests, verification}; pub(crate) struct UserIdentities { inner: matrix_sdk_crypto::UserIdentities, @@ -60,9 +57,11 @@ impl OwnUserIdentity { /// Send a verification request to our other devices. #[wasm_bindgen(js_name = "requestVerification")] - pub fn request_verification(&self, methods: Option) -> Result { - let methods = - methods.map(try_array_to_vec::).transpose()?; + pub fn request_verification( + &self, + methods: Option>, + ) -> Result { + let methods = methods.map(|methods| methods.iter().map(Into::into).collect()); let me = self.inner.clone(); Ok(future_to_promise(async move { @@ -164,13 +163,12 @@ impl UserIdentity { &self, room_id: &identifiers::RoomId, request_event_id: &identifiers::EventId, - methods: Option, + methods: Option>, ) -> Result { let me = self.inner.clone(); let room_id = room_id.inner.clone(); let request_event_id = request_event_id.inner.clone(); - let methods = - methods.map(try_array_to_vec::).transpose()?; + let methods = methods.map(|methods| methods.iter().map(Into::into).collect()); Ok(future_to_promise::<_, verification::VerificationRequest>(async move { Ok(me @@ -188,10 +186,12 @@ impl UserIdentity { /// After the content has been sent out a VerificationRequest can be started /// with the `request_verification` method. #[wasm_bindgen(js_name = "verificationRequestContent")] - pub fn verification_request_content(&self, methods: Option) -> Result { + pub fn verification_request_content( + &self, + methods: Option>, + ) -> Result { let me = self.inner.clone(); - let methods = - methods.map(try_array_to_vec::).transpose()?; + let methods = methods.map(|methods| methods.iter().map(Into::into).collect()); Ok(future_to_promise(async move { Ok(serde_json::to_string(&me.verification_request_content(methods).await)?) diff --git a/src/js.rs b/src/js.rs index 743708d32..4bfc9a3c4 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1,4 +1,4 @@ -use js_sys::{Array, Object, Reflect}; +use js_sys::{Object, Reflect}; use wasm_bindgen::{convert::RefFromWasmAbi, prelude::*}; /// A really hacky and dirty code to downcast a `JsValue` to `T: @@ -27,14 +27,3 @@ where ))) } } - -/// Transform a value `JS` from JavaScript to a Rust wrapper, to a -/// Rust wrapped type. -pub(crate) fn try_array_to_vec( - array: Array, -) -> Result, >::Error> -where - JS: TryFrom + Into, -{ - array.iter().map(|item| JS::try_from(item).map(Into::into)).collect() -} diff --git a/src/verification.rs b/src/verification.rs index 3ab40b3bd..8d49d1054 100644 --- a/src/verification.rs +++ b/src/verification.rs @@ -17,7 +17,6 @@ use crate::{ future::future_to_promise, identifiers::{DeviceId, RoomId, UserId}, impl_from_to_inner, - js::try_array_to_vec, machine::promise_result_to_future, requests, }; @@ -63,6 +62,19 @@ impl TryFrom for VerificationMethod { } } +impl From<&VerificationMethod> for RumaVerificationMethod { + fn from(value: &VerificationMethod) -> Self { + use VerificationMethod::*; + + match value { + SasV1 => Self::SasV1, + QrCodeScanV1 => Self::QrCodeScanV1, + QrCodeShowV1 => Self::QrCodeShowV1, + ReciprocateV1 => Self::ReciprocateV1, + } + } +} + impl From for RumaVerificationMethod { fn from(value: VerificationMethod) -> Self { use VerificationMethod::*; @@ -785,9 +797,9 @@ impl VerificationRequest { own_user_id: &UserId, own_device_id: &DeviceId, other_user_id: &UserId, - methods: Option, + methods: Option>, ) -> Result { - let methods = methods.map(try_array_to_vec::).transpose()?; + let methods = methods.map(|methods| methods.iter().map(Into::into).collect()); Ok(serde_json::to_string(&matrix_sdk_crypto::VerificationRequest::request( &own_user_id.inner, @@ -974,8 +986,11 @@ impl VerificationRequest { /// It returns either a `ToDeviceRequest`, a `RoomMessageRequest` /// or `undefined`. #[wasm_bindgen(js_name = "acceptWithMethods")] - pub fn accept_with_methods(&self, methods: Array) -> Result { - let methods = try_array_to_vec::(methods)?; + pub fn accept_with_methods( + &self, + methods: Vec, + ) -> Result { + let methods = methods.iter().map(Into::into).collect(); self.inner .accept_with_methods(methods)