Skip to content

Commit

Permalink
Make delay before recover configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul committed Sep 19, 2024
1 parent 005e9c4 commit e3d754b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 112 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ growable-bloom-filter = "2.1.0"
http = "1.1.0"
imbl = "3.0.0"
itertools = "0.12.0"
mock_instant = "0.5.1"
once_cell = "1.16.0"
pin-project-lite = "0.2.9"
rand = "0.8.5"
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl From<matrix_sdk_ui::room_list_service::State> for RoomListServiceState {
S::Init => Self::Initial,
S::SettingUp => Self::SettingUp,
S::Recovering => Self::Recovering,
S::Running { .. } => Self::Running,
S::Running => Self::Running,
S::Error { .. } => Self::Error,
S::Terminated { .. } => Self::Terminated,
}
Expand Down
8 changes: 0 additions & 8 deletions crates/matrix-sdk-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ rustls-tls = ["matrix-sdk/rustls-tls"]

uniffi = ["dep:uniffi", "matrix-sdk/uniffi", "matrix-sdk-base/uniffi"]

testing = ["dep:mock_instant", "matrix-sdk/testing", "matrix-sdk-base/testing"]

# Add support for encrypted extensible events.
unstable-msc3956 = ["ruma/unstable-msc3956"]

Expand All @@ -40,7 +38,6 @@ itertools = { workspace = true }
matrix-sdk = { workspace = true, features = ["experimental-sliding-sync", "e2e-encryption"] }
matrix-sdk-base = { workspace = true }
mime = "0.3.16"
mock_instant = { workspace = true, optional = true }
once_cell = { workspace = true }
pin-project-lite = { workspace = true }
ruma = { workspace = true, features = ["html", "unstable-msc3381"] }
Expand All @@ -60,14 +57,9 @@ assert_matches2 = { workspace = true }
eyeball-im-util = { workspace = true }
matrix-sdk = { workspace = true, features = ["testing", "sqlite"] }
matrix-sdk-test = { workspace = true }
mock_instant = { workspace = true }
stream_assert = { workspace = true }
tempfile = "3.3.0"
wiremock = { workspace = true }

[[test]]
name = "integration"
required-features = ["testing"]

[lints]
workspace = true
54 changes: 34 additions & 20 deletions crates/matrix-sdk-ui/src/room_list_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub struct RoomListService {
///
/// `RoomListService` is a simple state-machine.
state: SharedObservable<State>,

state_machine: StateMachine,
}

impl RoomListService {
Expand All @@ -100,19 +102,29 @@ impl RoomListService {
///
/// This won't start an encryption sync, and it's the user's responsibility
/// to create one in this case using `EncryptionSync`.
pub async fn new(client: Client) -> Result<Self, Error> {
Self::new_internal(client, false).await
pub async fn new(
client: Client,
delay_before_recover: Option<Duration>,
) -> Result<Self, Error> {
Self::new_internal(client, false, delay_before_recover).await
}

/// Create a new `RoomList` that enables encryption.
///
/// This will include syncing the encryption information, so there must not
/// be any instance of `EncryptionSync` running in the background.
pub async fn new_with_encryption(client: Client) -> Result<Self, Error> {
Self::new_internal(client, true).await
pub async fn new_with_encryption(
client: Client,
delay_before_recover: Option<Duration>,
) -> Result<Self, Error> {
Self::new_internal(client, true, delay_before_recover).await
}

async fn new_internal(client: Client, with_encryption: bool) -> Result<Self, Error> {
async fn new_internal(
client: Client,
with_encryption: bool,
delay_before_recover: Option<Duration>,
) -> Result<Self, Error> {
let mut builder = client
.sliding_sync("room-list")
.map_err(Error::SlidingSync)?
Expand Down Expand Up @@ -172,7 +184,12 @@ impl RoomListService {
// Eagerly subscribe the event cache to sync responses.
client.event_cache().subscribe()?;

Ok(Self { client, sliding_sync, state: SharedObservable::new(State::Init) })
Ok(Self {
client,
sliding_sync,
state: SharedObservable::new(State::Init),
state_machine: StateMachine::new(delay_before_recover),
})
}

/// Start to sync the room list.
Expand Down Expand Up @@ -207,8 +224,9 @@ impl RoomListService {
loop {
debug!("Run a sync iteration");

let current_state = self.state.get();
// Calculate the next state, and run the associated actions.
let next_state = self.state.get().next(&self.sliding_sync).await?;
let next_state = self.state_machine.next(current_state, &self.sliding_sync).await?;

// Do the sync.
match sync.next().await {
Expand Down Expand Up @@ -315,7 +333,7 @@ impl RoomListService {
(SyncIndicator::Show, delay_before_showing)
}

State::SettingUp | State::Recovering | State::Running {..} | State::Terminated { .. } => {
State::SettingUp | State::Recovering | State::Running | State::Terminated { .. } => {
(SyncIndicator::Hide, delay_before_hiding)
}
};
Expand Down Expand Up @@ -505,7 +523,7 @@ mod tests {
pub(super) async fn new_room_list() -> Result<RoomListService, Error> {
let (client, _) = new_client().await;

RoomListService::new(client).await
RoomListService::new(client, None).await
}

struct SlidingSyncMatcher;
Expand All @@ -522,15 +540,15 @@ mod tests {
let (client, _) = new_client().await;

{
let room_list = RoomListService::new(client.clone()).await?;
let room_list = RoomListService::new(client.clone(), None).await?;
assert_matches!(room_list.sliding_sync().version(), SlidingSyncVersion::Native);
}

{
let url = Url::parse("https://foo.matrix/").unwrap();
client.set_sliding_sync_version(SlidingSyncVersion::Proxy { url: url.clone() });

let room_list = RoomListService::new(client.clone()).await?;
let room_list = RoomListService::new(client.clone(), None).await?;
assert_matches!(
room_list.sliding_sync().version(),
SlidingSyncVersion::Proxy { url: given_url } => {
Expand Down Expand Up @@ -565,12 +583,12 @@ mod tests {
async fn test_no_to_device_and_e2ee_if_not_explicitly_set() -> Result<(), Error> {
let (client, _) = new_client().await;

let no_encryption = RoomListService::new(client.clone()).await?;
let no_encryption = RoomListService::new(client.clone(), None).await?;
let extensions = no_encryption.sliding_sync.extensions_config();
assert_eq!(extensions.e2ee.enabled, None);
assert_eq!(extensions.to_device.enabled, None);

let with_encryption = RoomListService::new_with_encryption(client).await?;
let with_encryption = RoomListService::new_with_encryption(client, None).await?;
let extensions = with_encryption.sliding_sync.extensions_config();
assert_eq!(extensions.e2ee.enabled, Some(true));
assert_eq!(extensions.to_device.enabled, Some(true));
Expand All @@ -582,7 +600,7 @@ mod tests {
async fn test_expire_sliding_sync_session_manually() -> Result<(), Error> {
let (client, server) = new_client().await;

let room_list = RoomListService::new(client).await?;
let room_list = RoomListService::new(client, None).await?;

let sync = room_list.sync();
pin_mut!(sync);
Expand Down Expand Up @@ -617,17 +635,13 @@ mod tests {
let _ = sync.next().await;

// State is `Terminated`, as expected!
assert_matches!(room_list.state.get(), State::Terminated { from } => {
assert_matches!(from.as_ref(), State::Running { .. });
});
assert_eq!(room_list.state.get(), State::Terminated { from: Box::new(State::Running) });

// Now, let's make the sliding sync session to expire.
room_list.expire_sync_session().await;

// State is `Error`, as a regular session expiration would generate!
assert_matches!(room_list.state.get(), State::Error { from } => {
assert_matches!(from.as_ref(), State::Running { .. });
});
assert_eq!(room_list.state.get(), State::Error { from: Box::new(State::Running) });

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/room_list_service/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl RoomList {

match state {
Terminated { .. } | Error { .. } | Init => (),
SettingUp | Recovering | Running { .. } => break,
SettingUp | Recovering | Running => break,
}
}

Expand Down
Loading

0 comments on commit e3d754b

Please sign in to comment.