Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use std::time::Duration;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use futures_util::StreamExt;
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk::{
config::{SyncSettings, SyncToken},
test_utils::logged_in_client_with_server,
};
use matrix_sdk_test::{
ALICE, BOB, JoinedRoomBuilder, SyncResponseBuilder, async_test, event_factory::EventFactory,
mocks::mock_encryption_state,
Expand Down Expand Up @@ -189,7 +192,8 @@ async fn test_new_focused() {
async fn test_live_aggregations_are_reflected_on_focused_timelines() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let mut sync_response_builder = SyncResponseBuilder::new();
sync_response_builder.add_joined_room(JoinedRoomBuilder::new(room_id));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use futures_util::{
};
use matrix_sdk::{
assert_let_timeout,
config::SyncSettings,
config::{SyncSettings, SyncToken},
event_cache::RoomPaginationStatus,
test_utils::{
logged_in_client_with_server,
Expand Down Expand Up @@ -412,7 +412,8 @@ async fn test_wait_for_token() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;

let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let f = EventFactory::new();
let mut sync_builder = SyncResponseBuilder::new();
Expand Down Expand Up @@ -535,7 +536,8 @@ async fn test_dedup_pagination() {
async fn test_timeline_reset_while_paginating() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let f = EventFactory::new();
let mut sync_builder = SyncResponseBuilder::new();
Expand Down
8 changes: 6 additions & 2 deletions crates/matrix-sdk-ui/tests/integration/timeline/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use std::{sync::Arc, time::Duration};

use assert_matches::assert_matches;
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk::{
config::{SyncSettings, SyncToken},
test_utils::logged_in_client_with_server,
};
use matrix_sdk_common::executor::spawn;
use matrix_sdk_test::{
ALICE, BOB, CAROL, DEFAULT_TEST_ROOM_ID, JoinedRoomBuilder, SyncResponseBuilder, async_test,
Expand All @@ -34,7 +37,8 @@ use crate::mock_sync;
#[async_test]
async fn test_update_sender_profiles() {
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let f = EventFactory::new();
let mut sync_builder = SyncResponseBuilder::new();
Expand Down
12 changes: 9 additions & 3 deletions crates/matrix-sdk-ui/tests/integration/timeline/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use futures_util::StreamExt;
use matrix_sdk::{Error, config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk::{
Error,
config::{SyncSettings, SyncToken},
test_utils::logged_in_client_with_server,
};
use matrix_sdk_base::store::QueueWedgeError;
use matrix_sdk_test::{
ALICE, JoinedRoomBuilder, SyncResponseBuilder, async_test, event_factory::EventFactory,
Expand Down Expand Up @@ -314,7 +318,8 @@ async fn test_reloaded_failed_local_echoes_are_marked_as_failed() {
async fn test_clear_with_echoes() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let f = EventFactory::new();
let mut sync_builder = SyncResponseBuilder::new();
Expand Down Expand Up @@ -397,7 +402,8 @@ async fn test_clear_with_echoes() {
async fn test_no_duplicate_date_divider() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let mut sync_response_builder = SyncResponseBuilder::new();
sync_response_builder.add_joined_room(JoinedRoomBuilder::new(room_id));
Expand Down
17 changes: 12 additions & 5 deletions crates/matrix-sdk-ui/tests/integration/timeline/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use futures_util::StreamExt;
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk::{
config::{SyncSettings, SyncToken},
test_utils::logged_in_client_with_server,
};
use matrix_sdk_common::executor::spawn;
use matrix_sdk_test::{
ALICE, BOB, GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder, async_test,
Expand All @@ -42,7 +45,8 @@ use crate::mock_sync;
async fn test_batched() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let f = EventFactory::new();
let mut sync_builder = SyncResponseBuilder::new();
Expand Down Expand Up @@ -84,7 +88,8 @@ async fn test_batched() {
async fn test_event_filter() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
let f = EventFactory::new();

let mut sync_builder = SyncResponseBuilder::new();
Expand Down Expand Up @@ -174,7 +179,8 @@ async fn test_event_filter() {
async fn test_timeline_is_reset_when_a_user_is_ignored_or_unignored() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);

let mut sync_builder = SyncResponseBuilder::new();
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));
Expand Down Expand Up @@ -289,7 +295,8 @@ async fn test_timeline_is_reset_when_a_user_is_ignored_or_unignored() {
async fn test_profile_updates() {
let room_id = room_id!("!a98sd12bjh:example.org");
let (client, server) = logged_in_client_with_server().await;
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
let sync_settings =
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
let f = EventFactory::new();

let mut sync_builder = SyncResponseBuilder::new();
Expand Down
14 changes: 10 additions & 4 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use crate::{
matrix::MatrixAuth, oauth::OAuth, AuthCtx, AuthData, ReloadSessionCallback,
SaveSessionCallback,
},
config::RequestConfig,
config::{RequestConfig, SyncToken},
deduplicating_handler::DeduplicatingHandler,
error::HttpResult,
event_cache::EventCache,
Expand Down Expand Up @@ -2304,9 +2304,15 @@ impl Client {
error!(error = ?e, "Error while sending outgoing E2EE requests");
}

let token = match sync_settings.token {
SyncToken::Specific(token) => Some(token),
SyncToken::NoToken => None,
SyncToken::ReusePrevious => self.sync_token().await,
};

let request = assign!(sync_events::v3::Request::new(), {
filter: sync_settings.filter.map(|f| *f),
since: sync_settings.token,
since: token,
full_state: sync_settings.full_state,
set_presence: sync_settings.set_presence,
timeout: sync_settings.timeout,
Expand Down Expand Up @@ -2604,8 +2610,8 @@ impl Client {
let mut timeout = None;
let mut last_sync_time: Option<Instant> = None;

if sync_settings.token.is_none() {
sync_settings.token = self.sync_token().await;
if let SyncToken::NoToken = sync_settings.token {
sync_settings.token = SyncToken::from_optional_token(self.sync_token().await);
}

let parent_span = Span::current();
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ mod sync;

pub use matrix_sdk_base::store::StoreConfig;
pub use request::RequestConfig;
pub use sync::SyncSettings;
pub use sync::{SyncSettings, SyncToken};
39 changes: 35 additions & 4 deletions crates/matrix-sdk/src/config/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,45 @@ use ruma::{api::client::sync::sync_events, presence::PresenceState};

const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30);

/// Token to use in [`SyncSettings`]
#[derive(Clone, Default, Debug)]
pub enum SyncToken {
/// Provide a specific token
Specific(String),
/// Provide no token
NoToken,
/// Use previous token
#[default]
ReusePrevious,
}

impl<T> From<T> for SyncToken
where
T: Into<String>,
{
fn from(token: T) -> SyncToken {
SyncToken::Specific(token.into())
}
}

impl SyncToken {
/// Convert a token that may exist into a [`SyncToken`]
pub fn from_optional_token(maybe_token: Option<String>) -> SyncToken {
match maybe_token {
Some(token) => SyncToken::Specific(token),
None => SyncToken::default(),
}
}
}

/// Settings for a sync call.
#[derive(Clone)]
pub struct SyncSettings {
// Filter is pretty big at 1000 bytes, box it to reduce stack size
pub(crate) filter: Option<Box<sync_events::v3::Filter>>,
pub(crate) timeout: Option<Duration>,
pub(crate) ignore_timeout_on_first_sync: bool,
pub(crate) token: Option<String>,
pub(crate) token: SyncToken,
pub(crate) full_state: bool,
pub(crate) set_presence: PresenceState,
}
Expand Down Expand Up @@ -66,7 +97,7 @@ impl SyncSettings {
filter: None,
timeout: Some(DEFAULT_SYNC_TIMEOUT),
ignore_timeout_on_first_sync: false,
token: None,
token: SyncToken::default(),
full_state: false,
set_presence: PresenceState::Online,
}
Expand All @@ -78,8 +109,8 @@ impl SyncSettings {
///
/// * `token` - The sync token that should be used for the sync call.
#[must_use]
pub fn token(mut self, token: impl Into<String>) -> Self {
self.token = Some(token.into());
pub fn token(mut self, token: impl Into<SyncToken>) -> Self {
self.token = token.into();
self
}

Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Client {

match response {
Ok(r) => {
sync_settings.token = Some(r.next_batch.clone());
sync_settings.token = r.next_batch.clone().into();
Ok(r)
}
Err(e) => {
Expand Down
24 changes: 15 additions & 9 deletions crates/matrix-sdk/tests/integration/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use eyeball_im::VectorDiff;
use futures_util::FutureExt;
use matrix_sdk::{
authentication::oauth::{error::OAuthTokenRevocationError, OAuthError},
config::{RequestConfig, StoreConfig, SyncSettings},
config::{RequestConfig, StoreConfig, SyncSettings, SyncToken},
store::RoomLoadSettings,
sync::{RoomUpdate, State},
test_utils::{
Expand Down Expand Up @@ -942,7 +942,8 @@ async fn test_test_ambiguity_changes() {

// Initial sync, adds 2 members.
mock_sync(&server, &*test_json::SYNC, None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
Expand Down Expand Up @@ -1015,7 +1016,8 @@ async fn test_test_ambiguity_changes() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let changes = &response.rooms.joined.get(*DEFAULT_TEST_ROOM_ID).unwrap().ambiguity_changes;
Expand Down Expand Up @@ -1074,7 +1076,8 @@ async fn test_test_ambiguity_changes() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let changes = &response.rooms.joined.get(*DEFAULT_TEST_ROOM_ID).unwrap().ambiguity_changes;
Expand Down Expand Up @@ -1120,7 +1123,8 @@ async fn test_test_ambiguity_changes() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let changes = &response.rooms.joined.get(*DEFAULT_TEST_ROOM_ID).unwrap().ambiguity_changes;
Expand Down Expand Up @@ -1166,7 +1170,8 @@ async fn test_test_ambiguity_changes() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let changes = &response.rooms.joined.get(*DEFAULT_TEST_ROOM_ID).unwrap().ambiguity_changes;
Expand Down Expand Up @@ -1212,7 +1217,8 @@ async fn test_test_ambiguity_changes() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
let response = client.sync_once(SyncSettings::default()).await.unwrap();
let response =
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

// Avatar change does not trigger ambiguity change.
Expand Down Expand Up @@ -1315,7 +1321,7 @@ async fn test_dms_are_processed_in_any_sync_response() {
let json_response = sync_response_builder.build_json_sync_response();

mock_sync(&server, json_response, None).await;
client.sync_once(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let room_1 = client.get_room(room_id_1).unwrap();
Expand All @@ -1327,7 +1333,7 @@ async fn test_dms_are_processed_in_any_sync_response() {
let json_response = sync_response_builder.build_json_sync_response();

mock_sync(&server, json_response, None).await;
client.sync_once(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

let room_2 = client.get_room(room_id_2).unwrap();
Expand Down
9 changes: 6 additions & 3 deletions crates/matrix-sdk/tests/integration/notification.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use assert_matches2::assert_matches;
use matrix_sdk::{config::SyncSettings, sync::Notification};
use matrix_sdk::{
config::{SyncSettings, SyncToken},
sync::Notification,
};
use matrix_sdk_base::deserialized_responses::RawAnySyncOrStrippedTimelineEvent;
use matrix_sdk_test::{
async_test, event_factory::EventFactory, stripped_state_event, sync_state_event, test_json,
Expand Down Expand Up @@ -50,7 +53,7 @@ async fn test_notifications_joined() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
client.sync_once(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();
server.reset().await;

assert_pending!(receiver_stream);
Expand All @@ -72,7 +75,7 @@ async fn test_notifications_joined() {
sync_builder.add_joined_room(joined_room);

mock_sync(&server, sync_builder.build_json_sync_response(), None).await;
client.sync_once(SyncSettings::default()).await.unwrap();
client.sync_once(SyncSettings::default().token(SyncToken::NoToken)).await.unwrap();

let (notif_room_id, notification) = assert_ready!(receiver_stream);
assert_eq!(notif_room_id, room_id);
Expand Down
Loading
Loading