Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): Sliding sync rooms aren't restored from the cache #3702

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions crates/matrix-sdk/src/sliding_sync/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use super::{
cache::{format_storage_key_prefix, restore_sliding_sync_state},
sticky_parameters::SlidingSyncStickyManager,
Error, SlidingSync, SlidingSyncInner, SlidingSyncListBuilder, SlidingSyncPositionMarkers,
SlidingSyncRoom,
};
use crate::{sliding_sync::SlidingSyncStickyParameters, Client, Result};

Expand All @@ -38,7 +37,6 @@ pub struct SlidingSyncBuilder {
lists: Vec<SlidingSyncListBuilder>,
extensions: Option<ExtensionsConfig>,
subscriptions: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
rooms: BTreeMap<OwnedRoomId, SlidingSyncRoom>,
poll_timeout: Duration,
network_timeout: Duration,
#[cfg(feature = "e2e-encryption")]
Expand All @@ -60,7 +58,6 @@ impl SlidingSyncBuilder {
lists: Vec::new(),
extensions: None,
subscriptions: BTreeMap::new(),
rooms: BTreeMap::new(),
poll_timeout: Duration::from_secs(30),
network_timeout: Duration::from_secs(30),
#[cfg(feature = "e2e-encryption")]
Expand Down Expand Up @@ -254,23 +251,23 @@ impl SlidingSyncBuilder {
let restored_fields =
restore_sliding_sync_state(&client, &self.storage_key, &lists).await?;

let (delta_token, pos) = if let Some(fields) = restored_fields {
let (delta_token, pos, rooms) = if let Some(fields) = restored_fields {
#[cfg(feature = "e2e-encryption")]
let pos = if self.share_pos { fields.pos } else { None };
#[cfg(not(feature = "e2e-encryption"))]
let pos = None;

(fields.delta_token, pos)
(fields.delta_token, pos, fields.rooms)
} else {
(None, None)
(None, None, BTreeMap::new())
};

#[cfg(feature = "e2e-encryption")]
let share_pos = self.share_pos;
#[cfg(not(feature = "e2e-encryption"))]
let share_pos = false;

let rooms = AsyncRwLock::new(self.rooms);
let rooms = AsyncRwLock::new(rooms);
let lists = AsyncRwLock::new(lists);

// Use the configured sliding sync proxy, or if not set, try to use the one
Expand Down
31 changes: 29 additions & 2 deletions crates/matrix-sdk/src/sliding_sync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,14 @@ mod tests {

use assert_matches::assert_matches;
use matrix_sdk_test::async_test;
use ruma::owned_room_id;

use super::{
super::FrozenSlidingSyncRoom, clean_storage, format_storage_key_for_sliding_sync,
format_storage_key_for_sliding_sync_list, format_storage_key_prefix,
restore_sliding_sync_state, store_sliding_sync_state, SlidingSyncList,
};
use crate::{test_utils::logged_in_client, Result};
use crate::{test_utils::logged_in_client, Result, SlidingSyncRoom};

#[allow(clippy::await_holding_lock)]
#[async_test]
Expand Down Expand Up @@ -330,6 +331,9 @@ mod tests {
.await?
.is_none());

let room_id1 = owned_room_id!("!r1:matrix.org");
let room_id2 = owned_room_id!("!r2:matrix.org");

// Create a new `SlidingSync` instance, and store it.
let storage_key = {
let sync_id = "test-sync-id";
Expand All @@ -353,6 +357,20 @@ mod tests {
list_bar.set_maximum_number_of_rooms(Some(1337));
}

// Add some rooms.
{
let mut rooms = sliding_sync.inner.rooms.write().await;

rooms.insert(
room_id1.clone(),
SlidingSyncRoom::new(client.clone(), room_id1.clone(), None, Vec::new()),
);
rooms.insert(
room_id2.clone(),
SlidingSyncRoom::new(client.clone(), room_id2.clone(), None, Vec::new()),
);
}

let position_guard = sliding_sync.inner.position.lock().await;
assert!(sliding_sync.cache_to_storage(&position_guard).await.is_ok());

Expand Down Expand Up @@ -403,7 +421,7 @@ mod tests {

// Check the list' state.
{
let lists = sliding_sync.inner.lists.write().await;
let lists = sliding_sync.inner.lists.read().await;

// This one was cached.
let list_foo = lists.get("list_foo").unwrap();
Expand All @@ -414,6 +432,15 @@ mod tests {
assert_eq!(list_bar.maximum_number_of_rooms(), None);
}

// Check the rooms.
{
let rooms = sliding_sync.inner.rooms.read().await;

// Rooms were cached.
assert!(rooms.contains_key(&room_id1));
assert!(rooms.contains_key(&room_id2));
}

// The maximum number of rooms reloaded from the cache should have been
// published.
{
Expand Down
Loading