Skip to content

Commit 5b0fd1a

Browse files
committed
feat(sdk) Use the types from http instead of ruma directly.
1 parent ee963d2 commit 5b0fd1a

File tree

10 files changed

+172
-173
lines changed

10 files changed

+172
-173
lines changed

crates/matrix-sdk-base/src/sliding_sync/http.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub mod ss {
3232
// Request types.
3333
pub use super::v4::{
3434
AccountDataConfig as RequestAccountData, E2EEConfig as RequestE2EE,
35-
ReceiptsConfig as RequestReceipts, ToDeviceConfig as RequestToDevice,
36-
TypingConfig as RequestTyping,
35+
ReceiptsConfig as RequestReceipts, RoomReceiptConfig as RequestReceiptsRoom,
36+
ToDeviceConfig as RequestToDevice, TypingConfig as RequestTyping,
3737
};
3838

3939
// Response types.

crates/matrix-sdk-base/src/sliding_sync/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ impl BaseClient {
6464

6565
let http::ss::ResponseExtensions { to_device, e2ee, .. } = extensions;
6666

67-
let to_device_events = to_device.as_ref().map(|v4| v4.events.clone()).unwrap_or_default();
67+
let to_device_events =
68+
to_device.as_ref().map(|to_device| to_device.events.clone()).unwrap_or_default();
6869

6970
trace!(
7071
to_device_events = to_device_events.len(),
@@ -713,7 +714,7 @@ fn process_room_properties(
713714
// Handle the room's avatar.
714715
//
715716
// It can be updated via the state events, or via the
716-
// [`v4::SlidingSyncRoom::avatar`] field. This part of the code handles the
717+
// [`http::ss::ResponseRoom::avatar`] field. This part of the code handles the
717718
// latter case. The former case is handled by [`BaseClient::handle_state`].
718719
match &room_data.avatar {
719720
// A new avatar!
@@ -726,7 +727,7 @@ fn process_room_properties(
726727

727728
// Sliding sync doesn't have a room summary, nevertheless it contains the joined
728729
// and invited member counts, in addition to the heroes if it's been configured
729-
// to return them (see the [`v4::RoomSubscription::include_heroes`]).
730+
// to return them (see the [`http::ss::RequestRoomSubscription::include_heroes`]).
730731
if let Some(count) = room_data.joined_count {
731732
room_info.update_joined_member_count(count.into());
732733
}

crates/matrix-sdk/src/sliding_sync/README.md

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ After the general configuration, one typically wants to add a list via the
5858
## Lists
5959

6060
A list defines a subset of matching rooms one wants to filter for, and be
61-
kept up about. The [`v4::SyncRequestListFilters`][] allows for a granular
61+
kept up about. The [`http::ss::RequestListFilters`] allows for a granular
6262
specification of the exact rooms one wants the server to select and the way
6363
one wants them to be ordered before receiving. Secondly each list has a set
6464
of `ranges`: the subset of indexes of the entire list one is interested in
@@ -73,12 +73,13 @@ are **inclusive**) like so:
7373

7474
```rust
7575
# use matrix_sdk::sliding_sync::{SlidingSyncList, SlidingSyncMode};
76-
use ruma::{assign, api::client::sync::sync_events::v4};
76+
use matrix_sdk_base::sliding_sync::http;
77+
use ruma::assign;
7778

7879
let list_builder = SlidingSyncList::builder("main_list")
7980
.sync_mode(SlidingSyncMode::new_selective().add_range(0..=9))
8081
.filters(Some(assign!(
81-
v4::SyncRequestListFilters::default(), { is_dm: Some(true)}
82+
http::ss::RequestListFilters::default(), { is_dm: Some(true)}
8283
)))
8384
.sort(vec!["by_recency".to_owned()]);
8485
```
@@ -161,15 +162,15 @@ typing- and presence-information and account-data, but can be extended by
161162
any implementation as they please. Handling of the data of the e2ee,
162163
to-device and typing-extensions takes place transparently within the SDK.
163164

164-
By default [`SlidingSync`][] doesn't activate _any_ extensions to save on
165+
By default [`SlidingSync`] doesn't activate _any_ extensions to save on
165166
bandwidth, but we generally recommend to use the `with_XXX_extensions` family
166167
of methods when building sliding sync to enable e2ee, to-device-messages and
167168
account-data-extensions.
168169

169170
## Timeline events
170171

171172
Both the list configuration as well as the [room subscription
172-
settings](`v4::RoomSubscription`) allow to specify a `timeline_limit` to
173+
settings](`http::ss::RequestRoomSubscription`) allow to specify a `timeline_limit` to
173174
receive timeline events. If that is unset or set to 0, no events are sent by
174175
the server (which is the default), if multiple limits are found, the highest
175176
takes precedence. Any positive number indicates that on the first request a
@@ -229,14 +230,8 @@ In full, this typically looks like this:
229230

230231
```rust,no_run
231232
# use futures_util::{pin_mut, StreamExt};
232-
# use matrix_sdk::{
233-
# sliding_sync::{SlidingSyncMode, SlidingSyncListBuilder},
234-
# Client,
235-
# };
236-
# use ruma::{
237-
# api::client::sync::sync_events::v4, assign,
238-
# };
239-
# use tracing::{debug, error, info, warn};
233+
# use matrix_sdk::Client;
234+
# use tracing::{error, info};
240235
# use url::Url;
241236
# async {
242237
# let homeserver = Url::parse("http://example.com")?;
@@ -288,7 +283,7 @@ will return immediately — with a proper response though. One just needs to
288283
make sure to not call that stream any further. Additionally, as both
289284
requests are sent with the same positional argument, the server might
290285
respond with data, the client has already processed. This isn't a problem,
291-
the [`SlidingSync`][] will only process new data and skip the processing
286+
the [`SlidingSync`] will only process new data and skip the processing
292287
even across restarts.
293288

294289
To support this, in practice, one can spawn a `Future` that runs
@@ -341,32 +336,12 @@ be sent in their first request than if they were loaded from a cold cache.
341336
Only the latest 10 timeline items of each room are cached and they are reset
342337
whenever a new set of timeline items is received by the server.
343338

344-
## Bot mode
345-
346-
_Note_: This is not yet exposed via the API. See [#1475](https://github.com/matrix-org/matrix-rust-sdk/issues/1475)
347-
348-
Sliding Sync is modeled for faster and more efficient user-facing client
349-
applications, but offers significant speed ups even for bot cases through
350-
its filtering mechanism. The sort-order and specific subsets, however, are
351-
usually not of interest for bots. For that use case the
352-
[`v4::SyncRequestList`][] offers the
353-
[`slow_get_all_rooms`](`v4::SyncRequestList::slow_get_all_rooms`) flag.
354-
355-
Once switched on, this mode will not trigger any updates on "list
356-
movements", ranges and sorting are ignored and all rooms matching the filter
357-
will be returned with the given room details settings. Depending on the data
358-
that is requested this will still be significantly faster as the response
359-
only returns the matching rooms and states as per settings.
360-
361-
Think about a bot that only interacts in `is_dm = true` and doesn't need
362-
room topic, room avatar and all the other state. It will be a lot faster to
363-
start up and retrieve only the data needed to actually run.
364-
365339
# Full example
366340

367341
```rust,no_run
368342
use matrix_sdk::{Client, sliding_sync::{SlidingSyncList, SlidingSyncMode}};
369-
use ruma::{assign, api::client::sync::sync_events::v4, events::StateEventType};
343+
use matrix_sdk_base::sliding_sync::http;
344+
use ruma::{assign, events::StateEventType};
370345
use tracing::{warn, error, info, debug};
371346
use futures_util::{pin_mut, StreamExt};
372347
use url::Url;
@@ -380,11 +355,11 @@ let sliding_sync_builder = client
380355
.sliding_sync("main-sync")?
381356
.sliding_sync_proxy(Url::parse("http://sliding-sync.example.org")?) // our proxy server
382357
.with_account_data_extension(
383-
assign!(v4::AccountDataConfig::default(), { enabled: Some(true) }),
358+
assign!(http::ss::extensions::RequestAccountData::default(), { enabled: Some(true) }),
384359
) // we enable the account-data extension
385-
.with_e2ee_extension(assign!(v4::E2EEConfig::default(), { enabled: Some(true) })) // and the e2ee extension
360+
.with_e2ee_extension(assign!(http::ss::extensions::RequestE2EE::default(), { enabled: Some(true) })) // and the e2ee extension
386361
.with_to_device_extension(
387-
assign!(v4::ToDeviceConfig::default(), { enabled: Some(true) }),
362+
assign!(http::ss::extensions::RequestToDevice::default(), { enabled: Some(true) }),
388363
); // and the to-device extension
389364
390365
let full_sync_list = SlidingSyncList::builder(&full_sync_list_name)

crates/matrix-sdk/src/sliding_sync/builder.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ use std::{
66
time::Duration,
77
};
88

9+
use matrix_sdk_base::sliding_sync::http;
910
use matrix_sdk_common::{ring_buffer::RingBuffer, timer};
10-
use ruma::{
11-
api::client::sync::sync_events::v4::{
12-
self, AccountDataConfig, E2EEConfig, ExtensionsConfig, ReceiptsConfig, ToDeviceConfig,
13-
TypingConfig,
14-
},
15-
OwnedRoomId,
16-
};
11+
12+
use ruma::OwnedRoomId;
1713
use tokio::sync::{broadcast::channel, Mutex as AsyncMutex, RwLock as AsyncRwLock};
1814
use url::Url;
1915

@@ -36,8 +32,8 @@ pub struct SlidingSyncBuilder {
3632
sliding_sync_proxy: Option<Url>,
3733
client: Client,
3834
lists: Vec<SlidingSyncListBuilder>,
39-
extensions: Option<ExtensionsConfig>,
40-
subscriptions: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
35+
extensions: Option<http::ss::RequestExtensions>,
36+
subscriptions: BTreeMap<OwnedRoomId, http::ss::RequestRoomSubscription>,
4137
rooms: BTreeMap<OwnedRoomId, SlidingSyncRoom>,
4238
poll_timeout: Duration,
4339
network_timeout: Duration,
@@ -135,63 +131,76 @@ impl SlidingSyncBuilder {
135131
}
136132

137133
/// Set the E2EE extension configuration.
138-
pub fn with_e2ee_extension(mut self, e2ee: E2EEConfig) -> Self {
134+
pub fn with_e2ee_extension(mut self, e2ee: http::ss::extensions::RequestE2EE) -> Self {
139135
self.extensions.get_or_insert_with(Default::default).e2ee = e2ee;
140136
self
141137
}
142138

143139
/// Unset the E2EE extension configuration.
144140
pub fn without_e2ee_extension(mut self) -> Self {
145-
self.extensions.get_or_insert_with(Default::default).e2ee = E2EEConfig::default();
141+
self.extensions.get_or_insert_with(Default::default).e2ee =
142+
http::ss::extensions::RequestE2EE::default();
146143
self
147144
}
148145

149146
/// Set the ToDevice extension configuration.
150-
pub fn with_to_device_extension(mut self, to_device: ToDeviceConfig) -> Self {
147+
pub fn with_to_device_extension(
148+
mut self,
149+
to_device: http::ss::extensions::RequestToDevice,
150+
) -> Self {
151151
self.extensions.get_or_insert_with(Default::default).to_device = to_device;
152152
self
153153
}
154154

155155
/// Unset the ToDevice extension configuration.
156156
pub fn without_to_device_extension(mut self) -> Self {
157-
self.extensions.get_or_insert_with(Default::default).to_device = ToDeviceConfig::default();
157+
self.extensions.get_or_insert_with(Default::default).to_device =
158+
http::ss::extensions::RequestToDevice::default();
158159
self
159160
}
160161

161162
/// Set the account data extension configuration.
162-
pub fn with_account_data_extension(mut self, account_data: AccountDataConfig) -> Self {
163+
pub fn with_account_data_extension(
164+
mut self,
165+
account_data: http::ss::extensions::RequestAccountData,
166+
) -> Self {
163167
self.extensions.get_or_insert_with(Default::default).account_data = account_data;
164168
self
165169
}
166170

167171
/// Unset the account data extension configuration.
168172
pub fn without_account_data_extension(mut self) -> Self {
169173
self.extensions.get_or_insert_with(Default::default).account_data =
170-
AccountDataConfig::default();
174+
http::ss::extensions::RequestAccountData::default();
171175
self
172176
}
173177

174178
/// Set the Typing extension configuration.
175-
pub fn with_typing_extension(mut self, typing: TypingConfig) -> Self {
179+
pub fn with_typing_extension(mut self, typing: http::ss::extensions::RequestTyping) -> Self {
176180
self.extensions.get_or_insert_with(Default::default).typing = typing;
177181
self
178182
}
179183

180184
/// Unset the Typing extension configuration.
181185
pub fn without_typing_extension(mut self) -> Self {
182-
self.extensions.get_or_insert_with(Default::default).typing = TypingConfig::default();
186+
self.extensions.get_or_insert_with(Default::default).typing =
187+
http::ss::extensions::RequestTyping::default();
183188
self
184189
}
185190

186191
/// Set the Receipt extension configuration.
187-
pub fn with_receipt_extension(mut self, receipt: ReceiptsConfig) -> Self {
192+
pub fn with_receipt_extension(
193+
mut self,
194+
receipt: http::ss::extensions::RequestReceipts,
195+
) -> Self {
188196
self.extensions.get_or_insert_with(Default::default).receipts = receipt;
189197
self
190198
}
191199

192200
/// Unset the Receipt extension configuration.
193201
pub fn without_receipt_extension(mut self) -> Self {
194-
self.extensions.get_or_insert_with(Default::default).receipts = ReceiptsConfig::default();
202+
self.extensions.get_or_insert_with(Default::default).receipts =
203+
http::ss::extensions::RequestReceipts::default();
195204
self
196205
}
197206

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::BTreeMap;
22

33
use imbl::Vector;
4-
use matrix_sdk_base::{sync::SyncResponse, PreviousEventsProvider};
5-
use ruma::{api::client::sync::sync_events::v4, events::AnyToDeviceEvent, serde::Raw, OwnedRoomId};
4+
use matrix_sdk_base::{sliding_sync::http, sync::SyncResponse, PreviousEventsProvider};
5+
use ruma::{events::AnyToDeviceEvent, serde::Raw, OwnedRoomId};
66

77
use super::{SlidingSync, SlidingSyncBuilder};
88
use crate::{Client, Result, SlidingSyncRoom};
@@ -25,7 +25,7 @@ impl Client {
2525
#[tracing::instrument(skip(self, response))]
2626
pub async fn process_sliding_sync_test_helper(
2727
&self,
28-
response: &v4::Response,
28+
response: &http::ss::Response,
2929
) -> Result<SyncResponse> {
3030
let response = self.base_client().process_sliding_sync(response, &()).await?;
3131

@@ -66,7 +66,10 @@ impl<'a> SlidingSyncResponseProcessor<'a> {
6666
}
6767

6868
#[cfg(feature = "e2e-encryption")]
69-
pub async fn handle_encryption(&mut self, extensions: &v4::Extensions) -> Result<()> {
69+
pub async fn handle_encryption(
70+
&mut self,
71+
extensions: &http::ss::ResponseExtensions,
72+
) -> Result<()> {
7073
// This is an internal API misuse if this is triggered (calling
7174
// handle_room_response before this function), so panic is fine.
7275
assert!(self.response.is_none());
@@ -80,7 +83,7 @@ impl<'a> SlidingSyncResponseProcessor<'a> {
8083
Ok(())
8184
}
8285

83-
pub async fn handle_room_response(&mut self, response: &v4::Response) -> Result<()> {
86+
pub async fn handle_room_response(&mut self, response: &http::ss::Response) -> Result<()> {
8487
self.response = Some(
8588
self.client
8689
.base_client()

crates/matrix-sdk/src/sliding_sync/list/builder.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use std::{
77
};
88

99
use eyeball::{Observable, SharedObservable};
10-
use ruma::{
11-
api::client::sync::sync_events::v4,
12-
events::{StateEventType, TimelineEventType},
13-
};
10+
use matrix_sdk_base::sliding_sync::http;
11+
use ruma::events::{StateEventType, TimelineEventType};
1412
use tokio::sync::broadcast::Sender;
1513

1614
use super::{
@@ -39,7 +37,7 @@ pub struct SlidingSyncListBuilder {
3937
sort: Vec<String>,
4038
required_state: Vec<(StateEventType, String)>,
4139
include_heroes: Option<bool>,
42-
filters: Option<v4::SyncRequestListFilters>,
40+
filters: Option<http::ss::RequestListFilters>,
4341
timeline_limit: Option<Bound>,
4442
pub(crate) name: String,
4543

@@ -131,7 +129,7 @@ impl SlidingSyncListBuilder {
131129
}
132130

133131
/// Any filters to apply to the query.
134-
pub fn filters(mut self, value: Option<v4::SyncRequestListFilters>) -> Self {
132+
pub fn filters(mut self, value: Option<http::ss::RequestListFilters>) -> Self {
135133
self.filters = value;
136134
self
137135
}

crates/matrix-sdk/src/sliding_sync/list/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use std::{
1111

1212
use eyeball::{Observable, SharedObservable, Subscriber};
1313
use futures_core::Stream;
14-
use ruma::{api::client::sync::sync_events::v4, assign, TransactionId};
14+
use matrix_sdk_base::sliding_sync::http;
15+
use ruma::{assign, TransactionId};
1516
use serde::{Deserialize, Serialize};
1617
use tokio::sync::broadcast::Sender;
1718
use tracing::{instrument, warn};
@@ -147,7 +148,7 @@ impl SlidingSyncList {
147148
pub(super) fn next_request(
148149
&self,
149150
txn_id: &mut LazyTransactionId,
150-
) -> Result<v4::SyncRequestList, Error> {
151+
) -> Result<http::ss::RequestList, Error> {
151152
self.inner.next_request(txn_id)
152153
}
153154

@@ -274,7 +275,7 @@ impl SlidingSyncListInner {
274275
}
275276

276277
/// Update the state to the next request, and return it.
277-
fn next_request(&self, txn_id: &mut LazyTransactionId) -> Result<v4::SyncRequestList, Error> {
278+
fn next_request(&self, txn_id: &mut LazyTransactionId) -> Result<http::ss::RequestList, Error> {
278279
let ranges = {
279280
// Use a dedicated scope to ensure the lock is released before continuing.
280281
let mut request_generator = self.request_generator.write().unwrap();
@@ -285,16 +286,16 @@ impl SlidingSyncListInner {
285286
Ok(self.request(ranges, txn_id))
286287
}
287288

288-
/// Build a [`SyncRequestList`][v4::SyncRequestList] based on the current
289+
/// Build a [`SyncRequestList`][http::ss::RequestList] based on the current
289290
/// state of the request generator.
290291
#[instrument(skip(self), fields(name = self.name))]
291-
fn request(&self, ranges: Ranges, txn_id: &mut LazyTransactionId) -> v4::SyncRequestList {
292+
fn request(&self, ranges: Ranges, txn_id: &mut LazyTransactionId) -> http::ss::RequestList {
292293
use ruma::UInt;
293294

294295
let ranges =
295296
ranges.into_iter().map(|r| (UInt::from(*r.start()), UInt::from(*r.end()))).collect();
296297

297-
let mut request = assign!(v4::SyncRequestList::default(), { ranges });
298+
let mut request = assign!(http::ss::RequestList::default(), { ranges });
298299
{
299300
let mut sticky = self.sticky.write().unwrap();
300301
sticky.maybe_apply(&mut request, txn_id);

0 commit comments

Comments
 (0)