diff --git a/crates/matrix-sdk/src/widget/machine/driver_req.rs b/crates/matrix-sdk/src/widget/machine/driver_req.rs index 9188077f409..88c61910901 100644 --- a/crates/matrix-sdk/src/widget/machine/driver_req.rs +++ b/crates/matrix-sdk/src/widget/machine/driver_req.rs @@ -273,6 +273,7 @@ impl FromMatrixDriverResponse for SendEventResponse { /// Ask the client to send a to-device message that corresponds to the given /// description. +/// see [MSC4140](https://github.com/matrix-org/matrix-spec-proposals/pull/4140)) as a response. #[derive(Clone, Debug, Deserialize)] pub(crate) struct SendToDeviceRequest { /// The type of the to-device message. diff --git a/crates/matrix-sdk/src/widget/matrix.rs b/crates/matrix-sdk/src/widget/matrix.rs index 86744c0406b..ddc419eb82e 100644 --- a/crates/matrix-sdk/src/widget/matrix.rs +++ b/crates/matrix-sdk/src/widget/matrix.rs @@ -17,13 +17,14 @@ use std::collections::BTreeMap; +use futures_util::future::join_all; use matrix_sdk_base::deserialized_responses::{EncryptionInfo, RawAnySyncOrStrippedState}; use ruma::{ api::client::{ account::request_openid_token::v3::{Request as OpenIdRequest, Response as OpenIdResponse}, delayed_events::{self, update_delayed_event::unstable::UpdateAction}, filter::RoomEventFilter, - to_device::send_event_to_device::{self, v3::Request as RumaToDeviceRequest}, + to_device::send_event_to_device::{self}, }, assign, events::{ @@ -197,10 +198,11 @@ impl MatrixDriver { async {} }); let drop_guard_msg_like = self.room.client().event_handler_drop_guard(handle_msg_like); - + let _room_id = room_id; + let _tx = tx; // Get only all state events from the state section of the sync. let handle_state = self.room.add_event_handler(move |raw: Raw| { - let _ = tx.send(attach_room_id(raw.cast_ref(), &room_id)); + let _ = _tx.send(attach_room_id(raw.cast_ref(), &_room_id)); async {} }); let drop_guard_state = self.room.client().event_handler_drop_guard(handle_state); @@ -224,8 +226,13 @@ impl MatrixDriver { // EncryptionInfo. The widgetAPI expects a boolean `encrypted` to be added // (!) to the raw content to know if the to-device message was encrypted or // not (as per MSC3819). - move |raw: Raw, _: Option| { - let _ = tx.send(raw); + move |raw: Raw, encryption_info: Option| { + // Only sent encrypted to-device events + // TODO only if the room is enxrypted + // self.room. + if encryption_info.is_some() { + let _ = tx.send(raw); + } async {} }, ); @@ -247,17 +254,35 @@ impl MatrixDriver { ) -> Result { let client = self.room.client(); + // TODO: always encrypt based on the room encryption event + // Temporarly just log that the encryption flag is deprecated. + // TODO make encrypted a Option let request = if encrypted { - return Err(Error::UnknownError( - "Sending encrypted to-device events is not supported by the widget driver.".into(), - )); + // We first want to get all missing session before we start any to device + // sending! + // TODO transform this into an array of devicesLists per content. + + let responses: BTreeMap< + OwnedUserId, + BTreeMap>, + > = join_all(messages.into_iter().map(|(user_id, device_content_map)| { + // TODO maybe group by content. + let event_type = event_type.clone(); + async move { + client.encryption().send_raw_to_device(devices, event_type, content); + } + })) + .await + .into_iter() + .collect(); + //TODO do sth with the responsesn + response.map_err(Into::into) } else { - RumaToDeviceRequest::new_raw(event_type, TransactionId::new(), messages) - }; - - let response = client.send(request).await; + RumaToDeviceRequest::new_raw(event_type, TransactionId::new(), messages); + let response = client.send(request).await; - response.map_err(Into::into) + response.map_err(Into::into) + }; } } @@ -283,10 +308,39 @@ fn attach_room_id(raw_ev: &Raw, room_id: &RoomId) -> Raw(); + let room_id = room_id!("!my_id:example.org"); + let new = attach_room_id(&raw, room_id); + assert_eq!( + serde_json::to_value(new).unwrap(), + json!({ + "encrypted": true, + "room_id": "!my_id:example.org", + "type": "m.room.message", + "content": { + "body": "Hello world" + } + }) + ); + } #[test] fn test_add_room_id_to_raw() { diff --git a/crates/matrix-sdk/tests/integration/widget.rs b/crates/matrix-sdk/tests/integration/widget.rs index b9d2d6e1ab4..0a0665d3d57 100644 --- a/crates/matrix-sdk/tests/integration/widget.rs +++ b/crates/matrix-sdk/tests/integration/widget.rs @@ -27,6 +27,7 @@ use matrix_sdk::{ use matrix_sdk_common::{executor::spawn, timeout::timeout}; use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder, ALICE, BOB}; use once_cell::sync::Lazy; +use rand::rngs::mock; use ruma::{ event_id, events::{room::member::MembershipState, MessageLikeEventType, StateEventType}, @@ -967,7 +968,7 @@ async fn test_send_encrypted_to_device_event() { }, } }), - json! {{"error":{"message":"Sending encrypted to-device events is not supported by the widget driver."}}}, + json! {{}}, 0, ) .await;