@@ -16,18 +16,19 @@ use std::iter;
16
16
17
17
use ruma:: OwnedUserId ;
18
18
19
- use crate :: { Error , Result , Room } ;
19
+ use crate :: { crypto :: types :: events :: room_key_bundle :: RoomKeyBundleContent , Error , Result , Room } ;
20
20
21
21
/// Share any shareable E2EE history in the given room with the given recipient,
22
22
/// as per [MSC4268].
23
23
///
24
24
/// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
25
25
pub async fn share_room_history ( room : & Room , user_id : OwnedUserId ) -> Result < ( ) > {
26
26
tracing:: info!( "Sharing message history in {} with {}" , room. room_id( ) , user_id) ;
27
+ let client = & room. client ;
27
28
28
29
// 1. Construct the key bundle
29
30
let bundle = {
30
- let olm_machine = room . client . olm_machine ( ) . await ;
31
+ let olm_machine = client. olm_machine ( ) . await ;
31
32
let olm_machine = olm_machine. as_ref ( ) . ok_or ( Error :: NoOlmMachine ) ?;
32
33
olm_machine. store ( ) . build_room_key_bundle ( room. room_id ( ) ) . await ?
33
34
} ;
@@ -40,7 +41,7 @@ pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()>
40
41
// 2. Upload to the server as an encrypted file
41
42
let json = serde_json:: to_vec ( & bundle) ?;
42
43
let upload =
43
- room . client . upload_encrypted_file ( & mime:: APPLICATION_JSON , & mut ( json. as_slice ( ) ) ) . await ?;
44
+ client. upload_encrypted_file ( & mime:: APPLICATION_JSON , & mut ( json. as_slice ( ) ) ) . await ?;
44
45
45
46
tracing:: info!(
46
47
media_url = ?upload. url,
@@ -49,8 +50,26 @@ pub async fn share_room_history(room: &Room, user_id: OwnedUserId) -> Result<()>
49
50
"Uploaded encrypted key blob"
50
51
) ;
51
52
52
- // 3. Send to-device messages to the recipient to share the keys .
53
- // TODO
53
+ // 3. Establish Olm sessions with all of the recipient's devices .
54
+ client . claim_one_time_keys ( iter :: once ( user_id . as_ref ( ) ) ) . await ? ;
54
55
56
+ // 4. Send to-device messages to the recipient to share the keys.
57
+ let content = RoomKeyBundleContent { room_id : room. room_id ( ) . to_owned ( ) , file : upload } ;
58
+ let requests = {
59
+ let olm_machine = client. olm_machine ( ) . await ;
60
+ let olm_machine = olm_machine. as_ref ( ) . ok_or ( Error :: NoOlmMachine ) ?;
61
+ olm_machine
62
+ . share_room_key_bundle_data (
63
+ & user_id,
64
+ & client. base_client ( ) . room_key_recipient_strategy ,
65
+ content,
66
+ )
67
+ . await ?
68
+ } ;
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
+ }
55
74
Ok ( ( ) )
56
75
}
0 commit comments