Skip to content

Commit fa2f58f

Browse files
committed
sdk: no need for named future for history sharing
1 parent 000b630 commit fa2f58f

File tree

3 files changed

+82
-87
lines changed

3 files changed

+82
-87
lines changed

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

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@
1717
1818
#![deny(unreachable_pub)]
1919

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

2222
use eyeball::SharedObservable;
2323
#[cfg(not(target_arch = "wasm32"))]
2424
use eyeball::Subscriber;
2525
use matrix_sdk_common::boxed_into_future;
26-
use ruma::{
27-
events::room::{EncryptedFile, EncryptedFileInit},
28-
OwnedUserId,
29-
};
26+
use ruma::events::room::{EncryptedFile, EncryptedFileInit};
3027

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

3630
/// Future returned by [`Client::upload_encrypted_file`].
3731
#[allow(missing_debug_implementations)]
@@ -127,78 +121,3 @@ where
127121
})
128122
}
129123
}
130-
131-
/// Future returned by [`Room::share_history`].
132-
#[derive(Debug)]
133-
pub struct ShareRoomHistory<'a> {
134-
/// The room whose history should be shared.
135-
room: &'a Room,
136-
137-
/// The recipient of the shared history.
138-
user_id: OwnedUserId,
139-
}
140-
141-
impl<'a> ShareRoomHistory<'a> {
142-
pub(crate) fn new(room: &'a Room, user_id: OwnedUserId) -> Self {
143-
Self { room, user_id }
144-
}
145-
}
146-
147-
impl<'a> IntoFuture for ShareRoomHistory<'a> {
148-
type Output = Result<()>;
149-
boxed_into_future!(extra_bounds: 'a);
150-
151-
fn into_future(self) -> Self::IntoFuture {
152-
let Self { room, user_id } = self;
153-
Box::pin(async move {
154-
tracing::info!("Sharing message history in {} with {}", room.room_id(), user_id);
155-
let client = &room.client;
156-
157-
// 1. Construct the key bundle
158-
let bundle = {
159-
let olm_machine = client.olm_machine().await;
160-
let olm_machine = olm_machine
161-
.as_ref()
162-
.expect("This should only be called once we have an OlmMachine");
163-
olm_machine.store().build_room_key_bundle(room.room_id()).await?
164-
};
165-
166-
if bundle.is_empty() {
167-
tracing::info!("No keys to share");
168-
return Ok(());
169-
}
170-
171-
// 2. Upload to the server as an encrypted file
172-
let json = serde_json::to_vec(&bundle)?;
173-
let upload = room
174-
.client
175-
.upload_encrypted_file(&mime::APPLICATION_JSON, &mut (json.as_slice()))
176-
.await?;
177-
178-
tracing::info!(
179-
media_url = ?upload.url,
180-
shared_keys = bundle.room_keys.len(),
181-
withheld_keys = bundle.withheld.len(),
182-
"Uploaded encrypted key blob"
183-
);
184-
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?;
196-
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-
}
201-
Ok(())
202-
})
203-
}
204-
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ use crate::{
160160
#[cfg(feature = "e2e-encryption")]
161161
use crate::{
162162
crypto::types::events::CryptoContextInfo, encryption::backups::BackupState,
163-
encryption::futures::ShareRoomHistory,
163+
room::shared_room_history::share_room_history,
164164
};
165165

166166
pub mod edit;
@@ -175,6 +175,7 @@ pub mod reply;
175175

176176
/// Contains all the functionality for modifying the privacy settings in a room.
177177
pub mod privacy_settings;
178+
mod shared_room_history;
178179

179180
/// A struct containing methods that are common for Joined, Invited and Left
180181
/// Rooms
@@ -1813,8 +1814,8 @@ impl Room {
18131814
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
18141815
#[cfg(feature = "e2e-encryption")]
18151816
#[instrument(skip_all, fields(room_id = ?self.room_id(), ?user_id))]
1816-
pub fn share_history<'a>(&'a self, user_id: &UserId) -> ShareRoomHistory<'a> {
1817-
ShareRoomHistory::new(self, user_id.to_owned())
1817+
pub async fn share_history<'a>(&'a self, user_id: &UserId) -> Result<()> {
1818+
share_room_history(self, user_id.to_owned()).await
18181819
}
18191820

18201821
/// Wait for the room to be fully synced.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::iter;
16+
17+
use ruma::OwnedUserId;
18+
19+
use crate::{crypto::types::events::room_key_bundle::RoomKeyBundleContent, Result, Room};
20+
21+
/// Share any shareable E2EE history in the given room with the given recipient,
22+
/// as per [MSC4268].
23+
///
24+
/// # Panics
25+
///
26+
/// Panics if the OlmMachine hasn't been set up on the client.
27+
///
28+
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
29+
pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()> {
30+
tracing::info!("Sharing message history in {} with {}", room.room_id(), user_id);
31+
let client = &room.client;
32+
33+
// 1. Construct the key bundle
34+
let bundle = {
35+
let olm_machine = client.olm_machine().await;
36+
let olm_machine =
37+
olm_machine.as_ref().expect("This should only be called once we have an OlmMachine");
38+
olm_machine.store().build_room_key_bundle(room.room_id()).await?
39+
};
40+
41+
if bundle.is_empty() {
42+
tracing::info!("No keys to share");
43+
return Ok(());
44+
}
45+
46+
// 2. Upload to the server as an encrypted file
47+
let json = serde_json::to_vec(&bundle)?;
48+
let upload =
49+
room.client.upload_encrypted_file(&mime::APPLICATION_JSON, &mut (json.as_slice())).await?;
50+
51+
tracing::info!(
52+
media_url = ?upload.url,
53+
shared_keys = bundle.room_keys.len(),
54+
withheld_keys = bundle.withheld.len(),
55+
"Uploaded encrypted key blob"
56+
);
57+
58+
// 3. Establish Olm sessions with all of the recipient's devices.
59+
client.claim_one_time_keys(iter::once(user_id.as_ref())).await?;
60+
61+
// 4. Send to-device messages to the recipient to share the keys.
62+
let requests = client
63+
.base_client()
64+
.share_room_key_bundle_data(
65+
&user_id,
66+
RoomKeyBundleContent { room_id: room.room_id().to_owned(), file: upload },
67+
)
68+
.await?;
69+
70+
for request in requests {
71+
let response = client.send_to_device(&request).await?;
72+
client.mark_request_as_sent(&request.txn_id, &response).await?;
73+
}
74+
Ok(())
75+
}

0 commit comments

Comments
 (0)