Skip to content

Commit 1201be4

Browse files
fix!(sdk): Client::sync_once defaults to reuse previous token
Introduces a new `SyncToken` enum for the `SyncSettings::token` field. The enum has 3 variants: ReusePrevious (default), NoToken, Specific(String). Some tests were changed to use the old default (NoToken).
1 parent 1ffc014 commit 1201be4

File tree

16 files changed

+140
-58
lines changed

16 files changed

+140
-58
lines changed

crates/matrix-sdk-ui/tests/integration/timeline/focus_event.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ use std::time::Duration;
1919
use assert_matches2::assert_let;
2020
use eyeball_im::VectorDiff;
2121
use futures_util::StreamExt;
22-
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
22+
use matrix_sdk::{
23+
config::{SyncSettings, SyncToken},
24+
test_utils::logged_in_client_with_server,
25+
};
2326
use matrix_sdk_test::{
2427
ALICE, BOB, JoinedRoomBuilder, SyncResponseBuilder, async_test, event_factory::EventFactory,
2528
mocks::mock_encryption_state,
@@ -189,7 +192,8 @@ async fn test_new_focused() {
189192
async fn test_live_aggregations_are_reflected_on_focused_timelines() {
190193
let room_id = room_id!("!a98sd12bjh:example.org");
191194
let (client, server) = logged_in_client_with_server().await;
192-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
195+
let sync_settings =
196+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
193197

194198
let mut sync_response_builder = SyncResponseBuilder::new();
195199
sync_response_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

crates/matrix-sdk-ui/tests/integration/timeline/pagination.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use futures_util::{
2323
};
2424
use matrix_sdk::{
2525
assert_let_timeout,
26-
config::SyncSettings,
26+
config::{SyncSettings, SyncToken},
2727
event_cache::RoomPaginationStatus,
2828
test_utils::{
2929
logged_in_client_with_server,
@@ -412,7 +412,8 @@ async fn test_wait_for_token() {
412412
let room_id = room_id!("!a98sd12bjh:example.org");
413413
let (client, server) = logged_in_client_with_server().await;
414414

415-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
415+
let sync_settings =
416+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
416417

417418
let f = EventFactory::new();
418419
let mut sync_builder = SyncResponseBuilder::new();
@@ -535,7 +536,8 @@ async fn test_dedup_pagination() {
535536
async fn test_timeline_reset_while_paginating() {
536537
let room_id = room_id!("!a98sd12bjh:example.org");
537538
let (client, server) = logged_in_client_with_server().await;
538-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
539+
let sync_settings =
540+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
539541

540542
let f = EventFactory::new();
541543
let mut sync_builder = SyncResponseBuilder::new();

crates/matrix-sdk-ui/tests/integration/timeline/profiles.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
use std::{sync::Arc, time::Duration};
1616

1717
use assert_matches::assert_matches;
18-
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
18+
use matrix_sdk::{
19+
config::{SyncSettings, SyncToken},
20+
test_utils::logged_in_client_with_server,
21+
};
1922
use matrix_sdk_common::executor::spawn;
2023
use matrix_sdk_test::{
2124
ALICE, BOB, CAROL, DEFAULT_TEST_ROOM_ID, JoinedRoomBuilder, SyncResponseBuilder, async_test,
@@ -34,7 +37,8 @@ use crate::mock_sync;
3437
#[async_test]
3538
async fn test_update_sender_profiles() {
3639
let (client, server) = logged_in_client_with_server().await;
37-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
40+
let sync_settings =
41+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
3842

3943
let f = EventFactory::new();
4044
let mut sync_builder = SyncResponseBuilder::new();

crates/matrix-sdk-ui/tests/integration/timeline/queue.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ use assert_matches::assert_matches;
1818
use assert_matches2::assert_let;
1919
use eyeball_im::VectorDiff;
2020
use futures_util::StreamExt;
21-
use matrix_sdk::{Error, config::SyncSettings, test_utils::logged_in_client_with_server};
21+
use matrix_sdk::{
22+
Error,
23+
config::{SyncSettings, SyncToken},
24+
test_utils::logged_in_client_with_server,
25+
};
2226
use matrix_sdk_base::store::QueueWedgeError;
2327
use matrix_sdk_test::{
2428
ALICE, JoinedRoomBuilder, SyncResponseBuilder, async_test, event_factory::EventFactory,
@@ -314,7 +318,8 @@ async fn test_reloaded_failed_local_echoes_are_marked_as_failed() {
314318
async fn test_clear_with_echoes() {
315319
let room_id = room_id!("!a98sd12bjh:example.org");
316320
let (client, server) = logged_in_client_with_server().await;
317-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
321+
let sync_settings =
322+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
318323

319324
let f = EventFactory::new();
320325
let mut sync_builder = SyncResponseBuilder::new();
@@ -403,7 +408,8 @@ async fn test_clear_with_echoes() {
403408
async fn test_no_duplicate_date_divider() {
404409
let room_id = room_id!("!a98sd12bjh:example.org");
405410
let (client, server) = logged_in_client_with_server().await;
406-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
411+
let sync_settings =
412+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
407413

408414
let mut sync_response_builder = SyncResponseBuilder::new();
409415
sync_response_builder.add_joined_room(JoinedRoomBuilder::new(room_id));

crates/matrix-sdk-ui/tests/integration/timeline/subscribe.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use assert_matches::assert_matches;
1818
use assert_matches2::assert_let;
1919
use eyeball_im::VectorDiff;
2020
use futures_util::StreamExt;
21-
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
21+
use matrix_sdk::{
22+
config::{SyncSettings, SyncToken},
23+
test_utils::logged_in_client_with_server,
24+
};
2225
use matrix_sdk_common::executor::spawn;
2326
use matrix_sdk_test::{
2427
ALICE, BOB, GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder, async_test,
@@ -42,7 +45,8 @@ use crate::mock_sync;
4245
async fn test_batched() {
4346
let room_id = room_id!("!a98sd12bjh:example.org");
4447
let (client, server) = logged_in_client_with_server().await;
45-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
48+
let sync_settings =
49+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
4650

4751
let f = EventFactory::new();
4852
let mut sync_builder = SyncResponseBuilder::new();
@@ -84,7 +88,8 @@ async fn test_batched() {
8488
async fn test_event_filter() {
8589
let room_id = room_id!("!a98sd12bjh:example.org");
8690
let (client, server) = logged_in_client_with_server().await;
87-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
91+
let sync_settings =
92+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
8893
let f = EventFactory::new();
8994

9095
let mut sync_builder = SyncResponseBuilder::new();
@@ -174,7 +179,8 @@ async fn test_event_filter() {
174179
async fn test_timeline_is_reset_when_a_user_is_ignored_or_unignored() {
175180
let room_id = room_id!("!a98sd12bjh:example.org");
176181
let (client, server) = logged_in_client_with_server().await;
177-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
182+
let sync_settings =
183+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
178184

179185
let mut sync_builder = SyncResponseBuilder::new();
180186
sync_builder.add_joined_room(JoinedRoomBuilder::new(room_id));
@@ -289,7 +295,8 @@ async fn test_timeline_is_reset_when_a_user_is_ignored_or_unignored() {
289295
async fn test_profile_updates() {
290296
let room_id = room_id!("!a98sd12bjh:example.org");
291297
let (client, server) = logged_in_client_with_server().await;
292-
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));
298+
let sync_settings =
299+
SyncSettings::new().timeout(Duration::from_millis(3000)).token(SyncToken::NoToken);
293300
let f = EventFactory::new();
294301

295302
let mut sync_builder = SyncResponseBuilder::new();

crates/matrix-sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ All notable changes to this project will be documented in this file.
3838

3939
### Refactor
4040

41+
- [**breaking**] `SyncSettings` token is now `SyncToken` enum type which has default behaviour of `SyncToken::ReusePrevious` token. This breaks `Client::sync_once`.
42+
For old behaviour, set the token to `SyncToken::NoToken` with the usual `SyncSettings::token` setter.
4143
- [**breaking**] Change the upload_encrypted_file and make it clone the client instead of owning it. The lifetime of the `UploadEncryptedFile` request returned by `Client::upload_encrypted_file()` only depends on the request lifetime now.
4244
- [**breaking**] Add an `IsPrefix = False` bound to the `account_data()` and
4345
`fetch_account_data_static()` methods of `Account`. These methods only worked

crates/matrix-sdk/src/client/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use crate::{
8585
matrix::MatrixAuth, oauth::OAuth, AuthCtx, AuthData, ReloadSessionCallback,
8686
SaveSessionCallback,
8787
},
88-
config::RequestConfig,
88+
config::{RequestConfig, SyncToken},
8989
deduplicating_handler::DeduplicatingHandler,
9090
error::HttpResult,
9191
event_cache::EventCache,
@@ -2349,9 +2349,15 @@ impl Client {
23492349
error!(error = ?e, "Error while sending outgoing E2EE requests");
23502350
}
23512351

2352+
let token = match sync_settings.token {
2353+
SyncToken::Specific(token) => Some(token),
2354+
SyncToken::NoToken => None,
2355+
SyncToken::ReusePrevious => self.sync_token().await,
2356+
};
2357+
23522358
let request = assign!(sync_events::v3::Request::new(), {
23532359
filter: sync_settings.filter.map(|f| *f),
2354-
since: sync_settings.token,
2360+
since: token,
23552361
full_state: sync_settings.full_state,
23562362
set_presence: sync_settings.set_presence,
23572363
timeout: sync_settings.timeout,
@@ -2649,10 +2655,6 @@ impl Client {
26492655
let mut timeout = None;
26502656
let mut last_sync_time: Option<Instant> = None;
26512657

2652-
if sync_settings.token.is_none() {
2653-
sync_settings.token = self.sync_token().await;
2654-
}
2655-
26562658
let parent_span = Span::current();
26572659

26582660
async_stream::stream!({

crates/matrix-sdk/src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ mod sync;
1919

2020
pub use matrix_sdk_base::store::StoreConfig;
2121
pub use request::RequestConfig;
22-
pub use sync::SyncSettings;
22+
pub use sync::{SyncSettings, SyncToken};

crates/matrix-sdk/src/config/sync.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,48 @@ use ruma::{api::client::sync::sync_events, presence::PresenceState};
1919

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

22+
/// Token to be used in the next sync request.
23+
#[derive(Clone, Default, Debug)]
24+
pub enum SyncToken {
25+
/// Provide a specific token.
26+
Specific(String),
27+
/// Enforce no tokens at all.
28+
NoToken,
29+
/// Use a previous token if the client saw one in the past, and none
30+
/// otherwise.
31+
///
32+
/// This is the default value.
33+
#[default]
34+
ReusePrevious,
35+
}
36+
37+
impl<T> From<T> for SyncToken
38+
where
39+
T: Into<String>,
40+
{
41+
fn from(token: T) -> SyncToken {
42+
SyncToken::Specific(token.into())
43+
}
44+
}
45+
46+
impl SyncToken {
47+
/// Convert a token that may exist into a [`SyncToken`]
48+
pub fn from_optional_token(maybe_token: Option<String>) -> SyncToken {
49+
match maybe_token {
50+
Some(token) => SyncToken::Specific(token),
51+
None => SyncToken::default(),
52+
}
53+
}
54+
}
55+
2256
/// Settings for a sync call.
2357
#[derive(Clone)]
2458
pub struct SyncSettings {
2559
// Filter is pretty big at 1000 bytes, box it to reduce stack size
2660
pub(crate) filter: Option<Box<sync_events::v3::Filter>>,
2761
pub(crate) timeout: Option<Duration>,
2862
pub(crate) ignore_timeout_on_first_sync: bool,
29-
pub(crate) token: Option<String>,
63+
pub(crate) token: SyncToken,
3064
pub(crate) full_state: bool,
3165
pub(crate) set_presence: PresenceState,
3266
}
@@ -66,7 +100,7 @@ impl SyncSettings {
66100
filter: None,
67101
timeout: Some(DEFAULT_SYNC_TIMEOUT),
68102
ignore_timeout_on_first_sync: false,
69-
token: None,
103+
token: SyncToken::default(),
70104
full_state: false,
71105
set_presence: PresenceState::Online,
72106
}
@@ -78,8 +112,8 @@ impl SyncSettings {
78112
///
79113
/// * `token` - The sync token that should be used for the sync call.
80114
#[must_use]
81-
pub fn token(mut self, token: impl Into<String>) -> Self {
82-
self.token = Some(token.into());
115+
pub fn token(mut self, token: impl Into<SyncToken>) -> Self {
116+
self.token = token.into();
83117
self
84118
}
85119

crates/matrix-sdk/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl Client {
315315

316316
match response {
317317
Ok(r) => {
318-
sync_settings.token = Some(r.next_batch.clone());
318+
sync_settings.token = r.next_batch.clone().into();
319319
Ok(r)
320320
}
321321
Err(e) => {

0 commit comments

Comments
 (0)