1+ use std:: collections:: BTreeMap ;
12#[ cfg( feature = "e2e-encryption" ) ]
23use std:: ops:: Deref ;
34
4- use ruma:: api:: client:: sync:: sync_events:: {
5- v3:: { self , Ephemeral } ,
6- v4,
7- } ;
85#[ cfg( feature = "e2e-encryption" ) ]
96use ruma:: UserId ;
7+ use ruma:: {
8+ api:: client:: sync:: sync_events:: {
9+ v3:: { self , Ephemeral } ,
10+ v4, DeviceLists ,
11+ } ,
12+ DeviceKeyAlgorithm , UInt ,
13+ } ;
1014use tracing:: { debug, info, instrument} ;
1115
1216use super :: BaseClient ;
@@ -26,7 +30,7 @@ impl BaseClient {
2630 /// * `response` - The response that we received after a successful sliding
2731 /// sync.
2832 #[ instrument( skip_all, level = "trace" ) ]
29- pub async fn process_sliding_sync ( & self , response : v4:: Response ) -> Result < SyncResponse > {
33+ pub async fn process_sliding_sync ( & self , response : & v4:: Response ) -> Result < SyncResponse > {
3034 #[ allow( unused_variables) ]
3135 let v4:: Response {
3236 // FIXME not yet supported by sliding sync. see
@@ -39,6 +43,7 @@ impl BaseClient {
3943 //presence,
4044 ..
4145 } = response;
46+
4247 info ! ( rooms = rooms. len( ) , lists = lists. len( ) , extensions = !extensions. is_empty( ) ) ;
4348
4449 if rooms. is_empty ( ) && extensions. is_empty ( ) {
@@ -49,21 +54,35 @@ impl BaseClient {
4954
5055 let v4:: Extensions { to_device, e2ee, account_data, .. } = extensions;
5156
52- let to_device_events = to_device. map ( |v4| v4. events ) . unwrap_or_default ( ) ;
57+ let to_device_events = to_device. as_ref ( ) . map ( |v4| v4. events . clone ( ) ) . unwrap_or_default ( ) ;
5358
5459 // Destructure the single `None` of the E2EE extension into separate objects
55- // since that's what the OlmMachine API expects. Passing in the default
56- // empty maps and vecs for this is completely fine, since the OlmMachine
60+ // since that's what the ` OlmMachine` API expects. Passing in the default
61+ // empty maps and vecs for this is completely fine, since the ` OlmMachine`
5762 // assumes empty maps/vecs mean no change in the one-time key counts.
63+
64+ // We declare default values that can be referenced hereinbelow. When we try to
65+ // extract values from `e2ee`, that would be unfortunate to clone the
66+ // value just to pass them (to remove them `e2ee`) as a reference later.
67+ let device_one_time_keys_count = BTreeMap :: < DeviceKeyAlgorithm , UInt > :: default ( ) ;
68+ let device_unused_fallback_key_types = None ;
69+
5870 let ( device_lists, device_one_time_keys_count, device_unused_fallback_key_types) = e2ee
71+ . as_ref ( )
5972 . map ( |e2ee| {
6073 (
61- e2ee. device_lists ,
62- e2ee. device_one_time_keys_count ,
63- e2ee. device_unused_fallback_key_types ,
74+ e2ee. device_lists . clone ( ) ,
75+ & e2ee. device_one_time_keys_count ,
76+ & e2ee. device_unused_fallback_key_types ,
6477 )
6578 } )
66- . unwrap_or_default ( ) ;
79+ . unwrap_or_else ( || {
80+ (
81+ DeviceLists :: default ( ) ,
82+ & device_one_time_keys_count,
83+ & device_unused_fallback_key_types,
84+ )
85+ } ) ;
6786
6887 info ! (
6988 to_device_events = to_device_events. len( ) ,
@@ -80,7 +99,7 @@ impl BaseClient {
8099 self . preprocess_to_device_events (
81100 to_device_events,
82101 & device_lists,
83- & device_one_time_keys_count,
102+ device_one_time_keys_count,
84103 device_unused_fallback_key_types. as_deref ( ) ,
85104 )
86105 . await ?
@@ -98,14 +117,14 @@ impl BaseClient {
98117
99118 let mut new_rooms = Rooms :: default ( ) ;
100119
101- for ( room_id, room_data) in rooms. into_iter ( ) {
120+ for ( room_id, room_data) in rooms {
102121 if !room_data. invite_state . is_empty ( ) {
103122 let invite_states = & room_data. invite_state ;
104- let room = store. get_or_create_stripped_room ( & room_id) . await ;
123+ let room = store. get_or_create_stripped_room ( room_id) . await ;
105124 let mut room_info = room. clone_info ( ) ;
106125 room_info. mark_state_partially_synced ( ) ;
107126
108- if let Some ( r) = store. get_room ( & room_id) {
127+ if let Some ( r) = store. get_room ( room_id) {
109128 let mut room_info = r. clone_info ( ) ;
110129 room_info. mark_as_invited ( ) ; // FIXME: this might not be accurate
111130 room_info. mark_state_partially_synced ( ) ;
@@ -119,7 +138,7 @@ impl BaseClient {
119138 v3:: InvitedRoom :: from ( v3:: InviteState :: from ( invite_states. clone ( ) ) ) ,
120139 ) ;
121140 } else {
122- let room = store. get_or_create_room ( & room_id, RoomType :: Joined ) . await ;
141+ let room = store. get_or_create_room ( room_id, RoomType :: Joined ) . await ;
123142 let mut room_info = room. clone_info ( ) ;
124143 room_info. mark_as_joined ( ) ; // FIXME: this might not be accurate
125144 room_info. mark_state_partially_synced ( ) ;
@@ -153,8 +172,8 @@ impl BaseClient {
153172 // }
154173
155174 let room_account_data = if let Some ( inner_account_data) = & account_data {
156- if let Some ( events) = inner_account_data. rooms . get ( & room_id) {
157- self . handle_room_account_data ( & room_id, events, & mut changes) . await ;
175+ if let Some ( events) = inner_account_data. rooms . get ( room_id) {
176+ self . handle_room_account_data ( room_id, events, & mut changes) . await ;
158177 Some ( events. to_vec ( ) )
159178 } else {
160179 None
@@ -171,8 +190,8 @@ impl BaseClient {
171190 . handle_timeline (
172191 & room,
173192 room_data. limited ,
174- room_data. timeline ,
175- room_data. prev_batch ,
193+ room_data. timeline . clone ( ) ,
194+ room_data. prev_batch . clone ( ) ,
176195 & push_rules,
177196 & mut user_ids,
178197 & mut room_info,
@@ -188,8 +207,8 @@ impl BaseClient {
188207 // The room turned on encryption in this sync, we need
189208 // to also get all the existing users and mark them for
190209 // tracking.
191- let joined = store. get_joined_user_ids ( & room_id) . await ?;
192- let invited = store. get_invited_user_ids ( & room_id) . await ?;
210+ let joined = store. get_joined_user_ids ( room_id) . await ?;
211+ let invited = store. get_invited_user_ids ( room_id) . await ?;
193212
194213 let user_ids: Vec < & UserId > =
195214 joined. iter ( ) . chain ( & invited) . map ( Deref :: deref) . collect ( ) ;
@@ -246,15 +265,15 @@ impl BaseClient {
246265 debug ! ( "applied changes" ) ;
247266
248267 let device_one_time_keys_count =
249- device_one_time_keys_count. into_iter ( ) . map ( |( k, v) | ( k, v . into ( ) ) ) . collect ( ) ;
268+ device_one_time_keys_count. iter ( ) . map ( |( k, v) | ( k. clone ( ) , ( * v ) . into ( ) ) ) . collect ( ) ;
250269
251270 Ok ( SyncResponse {
252271 rooms : new_rooms,
253272 ambiguity_changes : AmbiguityChanges { changes : ambiguity_cache. changes } ,
254273 notifications : changes. notifications ,
255274 // FIXME not yet supported by sliding sync.
256275 presence : Default :: default ( ) ,
257- account_data : account_data. map ( |a| a. global ) . unwrap_or_default ( ) ,
276+ account_data : account_data. as_ref ( ) . map ( |a| a. global . clone ( ) ) . unwrap_or_default ( ) ,
258277 to_device_events,
259278 device_lists,
260279 device_one_time_keys_count,
0 commit comments