Skip to content

Commit 75cde02

Browse files
authored
Merge pull request #4946 from matrix-org/rav/history_sharing/share_on_invite
sdk: share room history when we send an invite, subject to an experimental feature flag.
2 parents 59ecb1e + 6e96391 commit 75cde02

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ uniffi = ["dep:uniffi", "matrix-sdk-base/uniffi", "dep:matrix-sdk-ffi-macros"]
4848
experimental-widgets = ["dep:uuid"]
4949

5050
docsrs = ["e2e-encryption", "sqlite", "indexeddb", "sso-login", "qrcode"]
51+
experimental-share-history-on-invite = []
5152

5253
[dependencies]
5354
anyhow = { workspace = true, optional = true }

crates/matrix-sdk/src/room/mod.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ use crate::{
158158
BaseRoom, Client, Error, HttpResult, Result, RoomState, TransmissionProgress,
159159
};
160160
#[cfg(feature = "e2e-encryption")]
161-
use crate::{
162-
crypto::types::events::CryptoContextInfo, encryption::backups::BackupState,
163-
room::shared_room_history::share_room_history,
164-
};
161+
use crate::{crypto::types::events::CryptoContextInfo, encryption::backups::BackupState};
165162

166163
pub mod edit;
167164
pub mod futures;
@@ -1480,6 +1477,11 @@ impl Room {
14801477
/// * `user_id` - The `UserId` of the user to invite to the room.
14811478
#[instrument(skip_all)]
14821479
pub async fn invite_user_by_id(&self, user_id: &UserId) -> Result<()> {
1480+
#[cfg(all(feature = "experimental-share-history-on-invite", feature = "e2e-encryption"))]
1481+
{
1482+
shared_room_history::share_room_history(self, user_id.to_owned()).await?;
1483+
}
1484+
14831485
let recipient = InvitationRecipient::UserId { user_id: user_id.to_owned() };
14841486
let request = invite_user::v3::Request::new(self.room_id().to_owned(), recipient);
14851487
self.client.send(request).await?;
@@ -1806,21 +1808,6 @@ impl Room {
18061808
Ok(())
18071809
}
18081810

1809-
/// Share any shareable E2EE history in this room with the given recipient,
1810-
/// as per [MSC4268].
1811-
///
1812-
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1813-
///
1814-
/// This is temporarily exposed for integration testing as part of
1815-
/// experimental work on history sharing. In future, it will be combined
1816-
/// with sending an invite.
1817-
#[cfg(feature = "e2e-encryption")]
1818-
#[doc(hidden)]
1819-
#[instrument(skip_all, fields(room_id = ?self.room_id(), ?user_id))]
1820-
pub async fn share_history<'a>(&'a self, user_id: &UserId) -> Result<()> {
1821-
share_room_history(self, user_id.to_owned()).await
1822-
}
1823-
18241811
/// Wait for the room to be fully synced.
18251812
///
18261813
/// This method makes sure the room that was returned when joining a room

crates/matrix-sdk/src/room/shared_room_history.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@
1515
use std::iter;
1616

1717
use ruma::OwnedUserId;
18+
use tracing::{info, instrument, warn};
1819

1920
use crate::{crypto::types::events::room_key_bundle::RoomKeyBundleContent, Error, Result, Room};
2021

2122
/// Share any shareable E2EE history in the given room with the given recipient,
2223
/// as per [MSC4268].
2324
///
2425
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
26+
#[instrument(skip(room), fields(room_id = ?room.room_id()))]
2527
pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()> {
26-
tracing::info!("Sharing message history in {} with {}", room.room_id(), user_id);
2728
let client = &room.client;
2829

30+
// 0. We can only share room history if our user has set up cross signing
31+
let own_identity = match client.user_id() {
32+
Some(own_user) => client.encryption().get_user_identity(own_user).await?,
33+
None => None,
34+
};
35+
if own_identity.is_none() {
36+
warn!("Not sharing message history as cross-signing is not set up");
37+
return Ok(());
38+
}
39+
40+
info!("Sharing message history");
41+
2942
// 1. Construct the key bundle
3043
let bundle = {
3144
let olm_machine = client.olm_machine().await;
@@ -34,7 +47,7 @@ pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()>
3447
};
3548

3649
if bundle.is_empty() {
37-
tracing::info!("No keys to share");
50+
info!("No keys to share");
3851
return Ok(());
3952
}
4053

@@ -43,7 +56,7 @@ pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()>
4356
let upload =
4457
client.upload_encrypted_file(&mime::APPLICATION_JSON, &mut (json.as_slice())).await?;
4558

46-
tracing::info!(
59+
info!(
4760
media_url = ?upload.url,
4861
shared_keys = bundle.room_keys.len(),
4962
withheld_keys = bundle.withheld.len(),

labs/multiverse/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ crossterm = "0.28.1"
1616
futures-util = { workspace = true }
1717
imbl = { workspace = true }
1818
itertools = { workspace = true }
19-
matrix-sdk = { path = "../../crates/matrix-sdk", features = ["sso-login"] }
19+
matrix-sdk = { path = "../../crates/matrix-sdk", features = ["sso-login", "experimental-share-history-on-invite"] }
2020
matrix-sdk-base = { path = "../../crates/matrix-sdk-base" }
2121
matrix-sdk-common = { path = "../../crates/matrix-sdk-common" }
2222
matrix-sdk-ui = { path = "../../crates/matrix-sdk-ui" }

testing/matrix-sdk-integration-testing/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ futures = { version = "0.3.29", features = ["executor"] }
1717
futures-core = { workspace = true }
1818
futures-util = { workspace = true }
1919
http = { workspace = true }
20-
matrix-sdk = {workspace = true, default-features = true, features = ["testing", "qrcode"] }
20+
matrix-sdk = {workspace = true, default-features = true, features = ["testing", "qrcode", "experimental-share-history-on-invite"] }
2121
matrix-sdk-base = { workspace = true, default-features = true, features = ["testing", "qrcode"] }
2222
matrix-sdk-ui = { workspace = true, default-features = true }
2323
matrix-sdk-test = { workspace = true }

testing/matrix-sdk-integration-testing/src/tests/e2ee.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,7 @@ async fn test_history_share_on_invite() -> Result<()> {
12891289
.expect("We should be able to send a message to the room");
12901290

12911291
// Alice invites Bob to the room
1292-
// TODO: invite Bob rather than just call `share_history`
1293-
alice_room.share_history(bob.user_id().unwrap()).await?;
1292+
alice_room.invite_user_by_id(bob.user_id().unwrap()).await?;
12941293

12951294
let bob_response = bob.sync_once().instrument(bob_span.clone()).await?;
12961295

0 commit comments

Comments
 (0)