From 8bd401b0035face086369e3698e26ffc590bd50a Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 8 Dec 2025 17:45:02 +0200 Subject: [PATCH] change(matrix_sdk::Room): Return the used `EncryptionInfo` when sending `MessageLike` and `RawMessageLike` events --- .../matrix-sdk/src/encryption/identities/users.rs | 2 +- crates/matrix-sdk/src/encryption/mod.rs | 1 + crates/matrix-sdk/src/room/futures.rs | 13 ++++++++++--- crates/matrix-sdk/src/room/mod.rs | 5 +++-- crates/matrix-sdk/src/widget/matrix.rs | 2 +- .../tests/integration/encryption/shared_history.rs | 1 + crates/matrix-sdk/tests/integration/room/joined.rs | 2 +- crates/matrix-sdk/tests/integration/send_queue.rs | 3 ++- .../src/tests/e2ee/mod.rs | 3 ++- .../src/tests/e2ee/shared_history.rs | 4 ++++ .../matrix-sdk-integration-testing/src/tests/nse.rs | 1 + .../src/tests/room.rs | 2 +- .../src/tests/timeline.rs | 7 +++++-- 13 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/matrix-sdk/src/encryption/identities/users.rs b/crates/matrix-sdk/src/encryption/identities/users.rs index 38eb6fc16..c27e73484 100644 --- a/crates/matrix-sdk/src/encryption/identities/users.rs +++ b/crates/matrix-sdk/src/encryption/identities/users.rs @@ -286,7 +286,7 @@ impl UserIdentity { self.client.create_dm(i.user_id()).await? }; - let response = room.send(RoomMessageEventContent::new(content)).await?; + let (response, _) = room.send(RoomMessageEventContent::new(content)).await?; let verification = i.request_verification(room.room_id(), &response.event_id, methods); diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index 429c94991..788051a73 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -617,6 +617,7 @@ impl Client { .send(*content) .with_transaction_id(txn_id) .await + .map(|(response, _)| response) } pub(crate) async fn send_to_device( diff --git a/crates/matrix-sdk/src/room/futures.rs b/crates/matrix-sdk/src/room/futures.rs index e417c8263..5cad34568 100644 --- a/crates/matrix-sdk/src/room/futures.rs +++ b/crates/matrix-sdk/src/room/futures.rs @@ -21,6 +21,7 @@ use std::borrow::Borrow; use std::future::IntoFuture; use eyeball::SharedObservable; +use matrix_sdk_base::deserialized_responses::EncryptionInfo; use matrix_sdk_common::boxed_into_future; use mime::Mime; #[cfg(doc)] @@ -95,7 +96,7 @@ impl<'a> SendMessageLikeEvent<'a> { } impl<'a> IntoFuture for SendMessageLikeEvent<'a> { - type Output = Result; + type Output = Result<(send_message_event::v3::Response, Option)>; boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { @@ -163,7 +164,7 @@ impl<'a> SendRawMessageLikeEvent<'a> { } impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> { - type Output = Result; + type Output = Result<(send_message_event::v3::Response, Option)>; boxed_into_future!(extra_bounds: 'a); fn into_future(self) -> Self::IntoFuture { @@ -186,6 +187,11 @@ impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> { #[cfg(not(feature = "e2e-encryption"))] trace!("Sending plaintext event to room because we don't have encryption support."); + #[cfg(feature = "e2e-encryption")] + let mut encryption_info: Option = None; + #[cfg(not(feature = "e2e-encryption"))] + let encryption_info: Option = None; + #[cfg(feature = "e2e-encryption")] if room.latest_encryption_state().await?.is_encrypted() { Span::current().record("is_room_encrypted", true); @@ -207,6 +213,7 @@ impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> { let result = olm.encrypt_room_event_raw(room.room_id(), event_type, &content).await?; content = result.content.cast(); + encryption_info = Some(result.encryption_info); event_type = "m.room.encrypted"; } } else { @@ -226,7 +233,7 @@ impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> { Span::current().record("event_id", tracing::field::debug(&response.event_id)); info!("Sent event in room"); - Ok(response) + Ok((response, encryption_info)) }; Box::pin(fut.instrument(tracing_span)) diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 5a2303355..5d9a9be6a 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -2643,7 +2643,8 @@ impl Room { if let Some(txn_id) = txn_id { fut = fut.with_transaction_id(txn_id); } - fut.await + + fut.await.map(|(response, _)| response) } /// Creates the inner [`MessageType`] for an already-uploaded media file @@ -3829,7 +3830,7 @@ impl Room { if beacon_info_event.content.is_live() { let content = BeaconEventContent::new(beacon_info_event.event_id, geo_uri, None); - Ok(self.send(content).await?) + Ok(self.send(content).await?.0) } else { Err(BeaconError::NotLive) } diff --git a/crates/matrix-sdk/src/widget/matrix.rs b/crates/matrix-sdk/src/widget/matrix.rs index 50bd8d901..3660fdee2 100644 --- a/crates/matrix-sdk/src/widget/matrix.rs +++ b/crates/matrix-sdk/src/widget/matrix.rs @@ -169,7 +169,7 @@ impl MatrixDriver { Ok(match (state_key, delayed_event_parameters) { (None, None) => SendEventResponse::from_event_id( - self.room.send_raw(&type_str, content).await?.event_id, + self.room.send_raw(&type_str, content).await?.0.event_id, ), (Some(key), None) => SendEventResponse::from_event_id( diff --git a/crates/matrix-sdk/tests/integration/encryption/shared_history.rs b/crates/matrix-sdk/tests/integration/encryption/shared_history.rs index df4eaf9a6..9b0a517f7 100644 --- a/crates/matrix-sdk/tests/integration/encryption/shared_history.rs +++ b/crates/matrix-sdk/tests/integration/encryption/shared_history.rs @@ -85,6 +85,7 @@ async fn test_shared_history_out_of_order() { .send(RoomMessageEventContent::text_plain("It's a secret to everybody")) .await .expect("We should be able to send an initial message") + .0 .event_id; matrix_mock_server diff --git a/crates/matrix-sdk/tests/integration/room/joined.rs b/crates/matrix-sdk/tests/integration/room/joined.rs index a8b2de77d..45e6fcba4 100644 --- a/crates/matrix-sdk/tests/integration/room/joined.rs +++ b/crates/matrix-sdk/tests/integration/room/joined.rs @@ -734,7 +734,7 @@ async fn test_room_message_send() { let content = RoomMessageEventContent::text_plain("Hello world"); let txn_id = TransactionId::new(); - let response = room.send(content).with_transaction_id(txn_id).await.unwrap(); + let response = room.send(content).with_transaction_id(txn_id).await.unwrap().0; assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id) } diff --git a/crates/matrix-sdk/tests/integration/send_queue.rs b/crates/matrix-sdk/tests/integration/send_queue.rs index 2974e7878..b95a9ece7 100644 --- a/crates/matrix-sdk/tests/integration/send_queue.rs +++ b/crates/matrix-sdk/tests/integration/send_queue.rs @@ -398,7 +398,8 @@ async fn test_nothing_sent_when_disabled() { mock.mock_room_state_encryption().plain().mount().await; mock.mock_room_send().ok(event_id).expect(1).mount().await; - let response = room.send(RoomMessageEventContent::text_plain("Hello, World!")).await.unwrap(); + let (response, _) = + room.send(RoomMessageEventContent::text_plain("Hello, World!")).await.unwrap(); assert_eq!(response.event_id, event_id); } diff --git a/testing/matrix-sdk-integration-testing/src/tests/e2ee/mod.rs b/testing/matrix-sdk-integration-testing/src/tests/e2ee/mod.rs index 3cf97bd20..61fb906d2 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/e2ee/mod.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/e2ee/mod.rs @@ -938,7 +938,8 @@ async fn test_secret_gossip_after_interactive_verification() -> Result<()> { let response = room_first_client .send(RoomMessageEventContent::text_plain("It's a secret to everybody")) - .await?; + .await? + .0; let event_id = response.event_id; diff --git a/testing/matrix-sdk-integration-testing/src/tests/e2ee/shared_history.rs b/testing/matrix-sdk-integration-testing/src/tests/e2ee/shared_history.rs index 2d5d951a1..976bbd348 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/e2ee/shared_history.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/e2ee/shared_history.rs @@ -110,6 +110,7 @@ async fn test_history_share_on_invite_helper(exclude_insecure_devices: bool) -> .send(RoomMessageEventContent::text_plain("Hello Bob")) .await .expect("We should be able to send a message to the room") + .0 .event_id; let bundle_stream = bob @@ -271,6 +272,7 @@ async fn test_history_share_on_invite_pin_violation() -> Result<()> { .send(RoomMessageEventContent::text_plain("Hello Bob")) .await .expect("We should be able to send a message to the room") + .0 .event_id; // Let us create some streams to get notified about a received bundle and a @@ -451,6 +453,7 @@ async fn test_transitive_history_share_with_withhelds() -> Result<()> { .instrument(bob_span.clone()) .await .expect("We should be able to send a message to the room") + .0 .event_id; alice @@ -606,6 +609,7 @@ async fn test_history_sharing_session_merging() -> Result<()> { .instrument(bob_span.clone()) .await .expect("We should be able to send a message to the room") + .0 .event_id; alice diff --git a/testing/matrix-sdk-integration-testing/src/tests/nse.rs b/testing/matrix-sdk-integration-testing/src/tests/nse.rs index 9e1534420..368d89ab1 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/nse.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/nse.rs @@ -250,6 +250,7 @@ impl ClientWrapper { room.send(RoomMessageEventContent::text_plain(message.to_owned())) .await .expect("Sending message failed") + .0 .event_id, message.to_owned(), ) diff --git a/testing/matrix-sdk-integration-testing/src/tests/room.rs b/testing/matrix-sdk-integration-testing/src/tests/room.rs index 185100452..4b8c948cf 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/room.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/room.rs @@ -82,7 +82,7 @@ async fn test_event_with_context() -> Result<()> { alice_room.send(RoomMessageEventContent::text_plain(i.to_string())).await?; } - let send_event_response = + let (send_event_response, _) = alice_room.send(RoomMessageEventContent::text_plain("hello there!")).await?; let event_id = send_event_response.event_id; diff --git a/testing/matrix-sdk-integration-testing/src/tests/timeline.rs b/testing/matrix-sdk-integration-testing/src/tests/timeline.rs index 016798220..69e375afe 100644 --- a/testing/matrix-sdk-integration-testing/src/tests/timeline.rs +++ b/testing/matrix-sdk-integration-testing/src/tests/timeline.rs @@ -409,6 +409,7 @@ async fn test_enabling_backups_retries_decryption() { .send(RoomMessageEventContent::text_plain("It's a secret to everybody!")) .await .expect("We should be able to send a message to our new room") + .0 .event_id; alice @@ -619,6 +620,7 @@ async fn test_room_keys_received_on_notification_client_trigger_redecryption() { .send(RoomMessageEventContent::text_plain("It's a secret to everybody!")) .await .expect("We should be able to send a message to our new room") + .0 .event_id; // We don't need Alice anymore. @@ -725,6 +727,7 @@ async fn test_new_users_first_messages_dont_warn_about_insecure_device_if_it_is_ room.send(RoomMessageEventContent::text_plain(message)) .await .expect("We should be able to send a message to our new room") + .0 .event_id } @@ -942,7 +945,7 @@ async fn test_thread_focused_timeline() -> TestResult { }; // Bob sends messages in a thread. - let resp = bob_room.send(RoomMessageEventContent::text_plain("Root message")).await?; + let (resp, _) = bob_room.send(RoomMessageEventContent::text_plain("Root message")).await?; let thread_root = resp.event_id; let thread_reply_event_content = bob_room @@ -955,7 +958,7 @@ async fn test_thread_focused_timeline() -> TestResult { ) .await?; - let thread_reply_event_id = bob_room.send(thread_reply_event_content).await?.event_id; + let thread_reply_event_id = bob_room.send(thread_reply_event_content).await?.0.event_id; // Alice creates a timeline focused on the in-thread event, so this will use // /context, and the thread root will be part of the response.