@@ -8,12 +8,12 @@ use std::collections::BTreeMap;
8
8
9
9
use matrix_sdk_base:: { StateStore , StoreError } ;
10
10
use matrix_sdk_common:: timer;
11
- use ruma:: UserId ;
11
+ use ruma:: { OwnedRoomId , UserId } ;
12
12
use tracing:: { trace, warn} ;
13
13
14
14
use super :: {
15
15
FrozenSlidingSync , FrozenSlidingSyncList , SlidingSync , SlidingSyncList ,
16
- SlidingSyncPositionMarkers ,
16
+ SlidingSyncPositionMarkers , SlidingSyncRoom ,
17
17
} ;
18
18
#[ cfg( feature = "e2e-encryption" ) ]
19
19
use crate :: sliding_sync:: FrozenSlidingSyncPos ;
@@ -88,7 +88,10 @@ pub(super) async fn store_sliding_sync_state(
88
88
storage
89
89
. set_custom_value (
90
90
instance_storage_key. as_bytes ( ) ,
91
- serde_json:: to_vec ( & FrozenSlidingSync :: new ( position) ) ?,
91
+ serde_json:: to_vec ( & FrozenSlidingSync :: new (
92
+ position,
93
+ & * sliding_sync. inner . rooms . read ( ) . await ,
94
+ ) ) ?,
92
95
)
93
96
. await ?;
94
97
@@ -106,8 +109,6 @@ pub(super) async fn store_sliding_sync_state(
106
109
107
110
// Write every `SlidingSyncList` that's configured for caching into the store.
108
111
let frozen_lists = {
109
- let rooms_lock = sliding_sync. inner . rooms . read ( ) . await ;
110
-
111
112
sliding_sync
112
113
. inner
113
114
. lists
@@ -118,7 +119,7 @@ pub(super) async fn store_sliding_sync_state(
118
119
. map ( |( list_name, list) | {
119
120
Ok ( (
120
121
format_storage_key_for_sliding_sync_list ( storage_key, list_name) ,
121
- serde_json:: to_vec ( & FrozenSlidingSyncList :: freeze ( list, & rooms_lock ) ) ?,
122
+ serde_json:: to_vec ( & FrozenSlidingSyncList :: freeze ( list) ) ?,
122
123
) )
123
124
} )
124
125
. collect :: < Result < Vec < _ > , crate :: Error > > ( ) ?
@@ -163,9 +164,9 @@ pub(super) async fn restore_sliding_sync_list(
163
164
// error, we remove the entry from the cache and keep the list in its initial
164
165
// state.
165
166
warn ! (
166
- list_name,
167
- "failed to deserialize the list from the cache, it is obsolete; removing the cache entry!"
168
- ) ;
167
+ list_name,
168
+ "failed to deserialize the list from the cache, it is obsolete; removing the cache entry!"
169
+ ) ;
169
170
// Let's clear the list and stop here.
170
171
invalidate_cached_list ( storage, storage_key, list_name) . await ;
171
172
}
@@ -186,6 +187,7 @@ pub(super) struct RestoredFields {
186
187
pub delta_token : Option < String > ,
187
188
pub to_device_token : Option < String > ,
188
189
pub pos : Option < String > ,
190
+ pub rooms : BTreeMap < OwnedRoomId , SlidingSyncRoom > ,
189
191
}
190
192
191
193
/// Restore the `SlidingSync`'s state from what is stored in the storage.
@@ -221,7 +223,11 @@ pub(super) async fn restore_sliding_sync_state(
221
223
. map ( |custom_value| serde_json:: from_slice :: < FrozenSlidingSync > ( & custom_value) )
222
224
{
223
225
// `SlidingSync` has been found and successfully deserialized.
224
- Some ( Ok ( FrozenSlidingSync { to_device_since, delta_token : frozen_delta_token } ) ) => {
226
+ Some ( Ok ( FrozenSlidingSync {
227
+ to_device_since,
228
+ delta_token : frozen_delta_token,
229
+ rooms : frozen_rooms,
230
+ } ) ) => {
225
231
trace ! ( "Successfully read the `SlidingSync` from the cache" ) ;
226
232
// Only update the to-device token if we failed to read it from the crypto store
227
233
// above.
@@ -246,6 +252,16 @@ pub(super) async fn restore_sliding_sync_state(
246
252
}
247
253
}
248
254
}
255
+
256
+ restored_fields. rooms = frozen_rooms
257
+ . into_iter ( )
258
+ . map ( |frozen_room| {
259
+ (
260
+ frozen_room. room_id . clone ( ) ,
261
+ SlidingSyncRoom :: from_frozen ( frozen_room, client. clone ( ) ) ,
262
+ )
263
+ } )
264
+ . collect ( ) ;
249
265
}
250
266
251
267
// `SlidingSync` has been found, but it wasn't possible to deserialize it. It's
@@ -281,11 +297,11 @@ mod tests {
281
297
use matrix_sdk_test:: async_test;
282
298
283
299
use super :: {
284
- clean_storage, format_storage_key_for_sliding_sync,
300
+ super :: FrozenSlidingSyncRoom , clean_storage, format_storage_key_for_sliding_sync,
285
301
format_storage_key_for_sliding_sync_list, format_storage_key_prefix,
286
- restore_sliding_sync_state, store_sliding_sync_state,
302
+ restore_sliding_sync_state, store_sliding_sync_state, SlidingSyncList ,
287
303
} ;
288
- use crate :: { test_utils:: logged_in_client, Result , SlidingSyncList } ;
304
+ use crate :: { test_utils:: logged_in_client, Result } ;
289
305
290
306
#[ allow( clippy:: await_holding_lock) ]
291
307
#[ async_test]
@@ -440,6 +456,9 @@ mod tests {
440
456
#[ cfg( feature = "e2e-encryption" ) ]
441
457
#[ async_test]
442
458
async fn test_sliding_sync_high_level_cache_and_restore ( ) -> Result < ( ) > {
459
+ use imbl:: Vector ;
460
+ use ruma:: owned_room_id;
461
+
443
462
use crate :: sliding_sync:: FrozenSlidingSync ;
444
463
445
464
let client = logged_in_client ( Some ( "https://foo.bar" . to_owned ( ) ) ) . await ;
@@ -513,6 +532,11 @@ mod tests {
513
532
serde_json:: to_vec ( & FrozenSlidingSync {
514
533
to_device_since : Some ( to_device_token. clone ( ) ) ,
515
534
delta_token : Some ( delta_token. clone ( ) ) ,
535
+ rooms : vec ! [ FrozenSlidingSyncRoom {
536
+ room_id: owned_room_id!( "!r0:matrix.org" ) ,
537
+ prev_batch: Some ( "t0ken" . to_owned( ) ) ,
538
+ timeline_queue: Vector :: new( ) ,
539
+ } ] ,
516
540
} ) ?,
517
541
)
518
542
. await ?;
@@ -521,11 +545,12 @@ mod tests {
521
545
. await ?
522
546
. expect ( "must have restored fields" ) ;
523
547
524
- // After restoring, the delta token, the to-device since token, and stream
525
- // position could be read from the state store.
548
+ // After restoring, the delta token, the to-device since token, stream
549
+ // position and rooms could be read from the state store.
526
550
assert_eq ! ( restored_fields. delta_token. unwrap( ) , delta_token) ;
527
551
assert_eq ! ( restored_fields. to_device_token. unwrap( ) , to_device_token) ;
528
552
assert_eq ! ( restored_fields. pos. unwrap( ) , pos) ;
553
+ assert_eq ! ( restored_fields. rooms. len( ) , 1 ) ;
529
554
530
555
Ok ( ( ) )
531
556
}
0 commit comments