Skip to content

Commit 03d1aa3

Browse files
committed
sdk: send out the to-device requests created by Room::share_history
1 parent d7688a6 commit 03d1aa3

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

crates/matrix-sdk-base/src/client.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ use eyeball_im::{Vector, VectorDiff};
2626
use futures_util::Stream;
2727
#[cfg(feature = "e2e-encryption")]
2828
use matrix_sdk_crypto::{
29-
store::DynCryptoStore, types::requests::ToDeviceRequest, CollectStrategy, EncryptionSettings,
30-
OlmError, OlmMachine, TrustRequirement,
29+
store::DynCryptoStore, types::events::room_key_bundle::RoomKeyBundleContent,
30+
types::requests::ToDeviceRequest, CollectStrategy, EncryptionSettings, OlmError, OlmMachine,
31+
TrustRequirement,
3132
};
3233
#[cfg(feature = "e2e-encryption")]
3334
use ruma::events::room::{history_visibility::HistoryVisibility, member::MembershipState};
@@ -43,7 +44,7 @@ use ruma::{
4344
push::{Action, Ruleset},
4445
serde::Raw,
4546
time::Instant,
46-
OwnedRoomId, OwnedUserId, RoomId,
47+
OwnedRoomId, OwnedUserId, RoomId, UserId,
4748
};
4849
use tokio::sync::{broadcast, Mutex};
4950
#[cfg(feature = "e2e-encryption")]
@@ -1131,6 +1132,22 @@ impl BaseClient {
11311132
}
11321133
}
11331134

1135+
/// Get to-device requests that will share the details of a room key history
1136+
/// bundle with a user.
1137+
#[cfg(feature = "e2e-encryption")]
1138+
pub async fn share_room_key_bundle_data(
1139+
&self,
1140+
user_id: &UserId,
1141+
bundle_data: RoomKeyBundleContent,
1142+
) -> Result<Vec<ToDeviceRequest>> {
1143+
let olm = self.olm_machine().await;
1144+
let olm = olm.as_ref().expect("Olm machine wasn't started");
1145+
1146+
Ok(olm
1147+
.share_room_key_bundle_data(user_id, &self.room_key_recipient_strategy, bundle_data)
1148+
.await?)
1149+
}
1150+
11341151
/// Get the room with the given room id.
11351152
///
11361153
/// # Arguments

crates/matrix-sdk/src/encryption/futures.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
#![deny(unreachable_pub)]
1919

20-
use std::{future::IntoFuture, io::Read};
20+
use std::{future::IntoFuture, io::Read, iter};
2121

2222
use eyeball::SharedObservable;
2323
#[cfg(not(target_arch = "wasm32"))]
@@ -28,7 +28,10 @@ use ruma::{
2828
OwnedUserId,
2929
};
3030

31-
use crate::{config::RequestConfig, Client, Media, Result, Room, TransmissionProgress};
31+
use crate::{
32+
config::RequestConfig, crypto::types::events::room_key_bundle::RoomKeyBundleContent, Client,
33+
Media, Result, Room, TransmissionProgress,
34+
};
3235

3336
/// Future returned by [`Client::upload_encrypted_file`].
3437
#[allow(missing_debug_implementations)]
@@ -149,10 +152,11 @@ impl<'a> IntoFuture for ShareRoomHistory<'a> {
149152
let Self { room, user_id } = self;
150153
Box::pin(async move {
151154
tracing::info!("Sharing message history in {} with {}", room.room_id(), user_id);
155+
let client = &room.client;
152156

153157
// 1. Construct the key bundle
154158
let bundle = {
155-
let olm_machine = room.client.olm_machine().await;
159+
let olm_machine = client.olm_machine().await;
156160
let olm_machine = olm_machine
157161
.as_ref()
158162
.expect("This should only be called once we have an OlmMachine");
@@ -178,9 +182,22 @@ impl<'a> IntoFuture for ShareRoomHistory<'a> {
178182
"Uploaded encrypted key blob"
179183
);
180184

181-
// 3. Send to-device messages to the recipient to share the keys.
182-
// TODO
185+
// 3. Establish Olm sessions with all of the recipient's devices.
186+
client.claim_one_time_keys(iter::once(user_id.as_ref())).await?;
187+
188+
// 4. Send to-device messages to the recipient to share the keys.
189+
let requests = client
190+
.base_client()
191+
.share_room_key_bundle_data(
192+
&user_id,
193+
RoomKeyBundleContent { room_id: room.room_id().to_owned(), file: upload },
194+
)
195+
.await?;
183196

197+
for request in requests {
198+
let response = client.send_to_device(&request).await?;
199+
client.mark_request_as_sent(&request.txn_id, &response).await?;
200+
}
184201
Ok(())
185202
})
186203
}

0 commit comments

Comments
 (0)