diff --git a/src/persistent_state.rs b/src/persistent_state.rs index cf92392f..e2fed6ea 100644 --- a/src/persistent_state.rs +++ b/src/persistent_state.rs @@ -7,7 +7,7 @@ use makepad_widgets::{ use matrix_sdk::{ authentication::matrix::MatrixSession, ruma::{OwnedUserId, UserId}, - sliding_sync::VersionBuilder, + sliding_sync, Client, }; use serde::{Deserialize, Serialize}; @@ -48,6 +48,40 @@ pub struct FullSessionPersisted { /// again. #[serde(skip_serializing_if = "Option::is_none")] pub sync_token: Option, + + /// The sliding sync version to use for this client session. + /// + /// This determines the sync protocol used by the Matrix client: + /// - `Native`: Uses the server's native sliding sync implementation for efficient syncing + /// - `None`: Falls back to standard Matrix sync (without sliding sync optimizations) + /// + /// The value is restored and applied to the client via `client.set_sliding_sync_version()` + /// when rebuilding the session from persistent storage. + pub sliding_sync_version: SlidingSyncVersion, +} +/// The serializable version of the sliding_sync::Version +#[derive(Debug, Serialize, Deserialize)] +pub enum SlidingSyncVersion { + None, + Native, +} + +impl From for sliding_sync::Version { + fn from(version: SlidingSyncVersion) -> Self { + match version { + SlidingSyncVersion::None => sliding_sync::Version::None, + SlidingSyncVersion::Native => sliding_sync::Version::Native, + } + } +} + +impl From for SlidingSyncVersion { + fn from(version: sliding_sync::Version) -> Self { + match version { + sliding_sync::Version::None => SlidingSyncVersion::None, + sliding_sync::Version::Native => SlidingSyncVersion::Native, + } + } } fn user_id_to_file_name(user_id: &UserId) -> String { @@ -118,7 +152,7 @@ pub async fn restore_session( // The session was serialized as JSON in a file. let serialized_session = fs::read_to_string(session_file).await?; - let FullSessionPersisted { client_session, user_session, sync_token } = + let FullSessionPersisted { client_session, user_session, sync_token, sliding_sync_version } = serde_json::from_str(&serialized_session)?; let status_str = format!( @@ -130,16 +164,15 @@ pub async fn restore_session( title: "Connecting to homeserver".into(), status: status_str, }); - // Build the client with the previous settings from the session. let client = Client::builder() - .server_name_or_homeserver_url(client_session.homeserver) + .homeserver_url(client_session.homeserver) .sqlite_store(client_session.db_path, Some(&client_session.passphrase)) - .sliding_sync_version_builder(VersionBuilder::DiscoverNative) .handle_refresh_tokens() .build() .await?; - + let sliding_sync_version = sliding_sync_version.into(); + client.set_sliding_sync_version(sliding_sync_version); let status_str = format!("Authenticating previous login session for {}...", user_session.meta.user_id); log!("{status_str}"); Cx::post_action(LoginAction::Status { @@ -170,13 +203,14 @@ pub async fn save_session( .ok_or_else(|| anyhow!("A logged-in client should have a session"))?; save_latest_user_id(&user_session.meta.user_id).await?; - + let sliding_sync_version = client.sliding_sync_version().into(); // Save that user's session. let session_file = session_file_path(&user_session.meta.user_id); let serialized_session = serde_json::to_string(&FullSessionPersisted { client_session, user_session, sync_token: None, + sliding_sync_version })?; if let Some(parent) = session_file.parent() { fs::create_dir_all(parent).await?; diff --git a/src/sliding_sync.rs b/src/sliding_sync.rs index a4350782..31075e3d 100644 --- a/src/sliding_sync.rs +++ b/src/sliding_sync.rs @@ -128,10 +128,11 @@ async fn build_client( ); let client = builder.build().await?; + let homeserver_url = client.homeserver().to_string(); Ok(( client, ClientSessionPersisted { - homeserver: homeserver_url.to_string(), + homeserver: homeserver_url, db_path, passphrase, }, @@ -1487,6 +1488,8 @@ async fn async_main_loop( // enqueue_popup_notification(status.clone()); enqueue_rooms_list_update(RoomsListUpdate::Status { status }); + client.event_cache().subscribe().expect("BUG: CLIENT's event cache unable to subscribe"); + CLIENT.set(client.clone()).expect("BUG: CLIENT already set!"); add_verification_event_handlers_and_sync_client(client.clone()); @@ -1495,6 +1498,7 @@ async fn async_main_loop( handle_ignore_user_list_subscriber(client.clone()); let sync_service = SyncService::builder(client.clone()) + .with_offline_mode() .build() .await?;