matrix_sdk_ui/room_list_service/mod.rs
1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for that specific language governing permissions and
13// limitations under the License.
14
15//! `RoomListService` API.
16//!
17//! The `RoomListService` is a UI API dedicated to present a list of Matrix
18//! rooms to the user. The syncing is handled by [`SlidingSync`]. The idea is to
19//! expose a simple API to handle most of the client app use cases, like:
20//! Showing and updating a list of rooms, filtering a list of rooms, handling
21//! particular updates of a range of rooms (the ones the client app is showing
22//! to the view, i.e. the rooms present in the viewport) etc.
23//!
24//! As such, the `RoomListService` works as an opinionated state machine. The
25//! states are defined by [`State`]. Actions are attached to the each state
26//! transition.
27//!
28//! The API is purposely small. Sliding Sync is versatile. `RoomListService` is
29//! _one_ specific usage of Sliding Sync.
30//!
31//! # Basic principle
32//!
33//! `RoomListService` works with 1 Sliding Sync List:
34//!
35//! * `all_rooms` (referred by the constant [`ALL_ROOMS_LIST_NAME`]) is the only
36//! list. Its goal is to load all the user' rooms. It starts with a
37//! [`SlidingSyncMode::Selective`] sync-mode with a small range (i.e. a small
38//! set of rooms) to load the first rooms quickly, and then updates to a
39//! [`SlidingSyncMode::Growing`] sync-mode to load the remaining rooms “in the
40//! background”: it will sync the existing rooms and will fetch new rooms, by
41//! a certain batch size.
42//!
43//! This behavior has proven to be empirically satisfying to provide a fast and
44//! fluid user experience for a Matrix client.
45//!
46//! [`RoomListService::all_rooms`] provides a way to get a [`RoomList`] for all
47//! the rooms. From that, calling [`RoomList::entries_with_dynamic_adapters`]
48//! provides a way to get a stream of rooms. This stream is sorted, can be
49//! filtered, and the filter can be changed over time.
50//!
51//! [`RoomListService::state`] provides a way to get a stream of the state
52//! machine's state, which can be pretty helpful for the client app.
53
54pub mod filters;
55mod room_list;
56pub mod sorters;
57mod state;
58
59use std::{sync::Arc, time::Duration};
60
61use async_stream::stream;
62use eyeball::Subscriber;
63use futures_util::{Stream, StreamExt, pin_mut};
64use matrix_sdk::{
65 Client, Error as SlidingSyncError, Room, SlidingSync, SlidingSyncList, SlidingSyncMode,
66 event_cache::EventCacheError, sliding_sync::PollTimeout, timeout::timeout,
67};
68pub use room_list::*;
69use ruma::{
70 OwnedRoomId, RoomId, UInt, api::client::sync::sync_events::v5 as http, assign,
71 events::StateEventType,
72};
73pub use state::*;
74use thiserror::Error;
75use tracing::{debug, error, warn};
76
77/// The default `required_state` constant value for sliding sync lists and
78/// sliding sync room subscriptions.
79const DEFAULT_REQUIRED_STATE: &[(StateEventType, &str)] = &[
80 (StateEventType::RoomName, ""),
81 (StateEventType::RoomEncryption, ""),
82 (StateEventType::RoomMember, "$LAZY"),
83 (StateEventType::RoomMember, "$ME"),
84 (StateEventType::RoomTopic, ""),
85 // Temporary workaround for https://github.com/matrix-org/matrix-rust-sdk/issues/5285
86 (StateEventType::RoomAvatar, ""),
87 (StateEventType::RoomCanonicalAlias, ""),
88 (StateEventType::RoomPowerLevels, ""),
89 (StateEventType::CallMember, "*"),
90 (StateEventType::RoomJoinRules, ""),
91 (StateEventType::RoomTombstone, ""),
92 // Those two events are required to properly compute room previews.
93 // `StateEventType::RoomCreate` is also necessary to compute the room
94 // version, and thus handling the tombstoned room correctly.
95 (StateEventType::RoomCreate, ""),
96 (StateEventType::RoomHistoryVisibility, ""),
97 // Required to correctly calculate the room display name.
98 (StateEventType::MemberHints, ""),
99 (StateEventType::SpaceParent, "*"),
100 (StateEventType::SpaceChild, "*"),
101 // Required for live location sharing to work - beacon events reference this state.
102 (StateEventType::BeaconInfo, "*"),
103];
104
105/// The default `required_state` constant value for sliding sync room
106/// subscriptions that must be added to `DEFAULT_REQUIRED_STATE`.
107const DEFAULT_ROOM_SUBSCRIPTION_EXTRA_REQUIRED_STATE: &[(StateEventType, &str)] =
108 &[(StateEventType::RoomPinnedEvents, "")];
109
110/// The default Sliding Sync connection ID for the room list service.
111pub(crate) const DEFAULT_CONNECTION_ID: &str = "room-list";
112
113/// The default timeline limit for the room list service.
114pub(crate) const DEFAULT_LIST_TIMELINE_LIMIT: u32 = 1;
115
116/// The default `timeline_limit` value when used with room subscriptions.
117const DEFAULT_ROOM_SUBSCRIPTION_TIMELINE_LIMIT: u32 = 20;
118
119/// The [`RoomListService`] type. See the module's documentation to learn more.
120#[derive(Debug)]
121pub struct RoomListService {
122 /// Client that has created this [`RoomListService`].
123 client: Client,
124
125 /// The Sliding Sync instance.
126 sliding_sync: Arc<SlidingSync>,
127
128 /// The current state of the `RoomListService`.
129 ///
130 /// `RoomListService` is a simple state-machine.
131 state_machine: StateMachine,
132}
133
134impl RoomListService {
135 /// Create a new `RoomList`.
136 ///
137 /// A [`matrix_sdk::SlidingSync`] client will be created, with a cached list
138 /// already pre-configured.
139 ///
140 /// This won't start an encryption sync, and it's the user's responsibility
141 /// to create one in this case using
142 /// [`EncryptionSyncService`][crate::encryption_sync_service::EncryptionSyncService].
143 pub async fn new(client: Client) -> Result<Self, Error> {
144 Self::new_with(client, true, DEFAULT_CONNECTION_ID, DEFAULT_LIST_TIMELINE_LIMIT).await
145 }
146
147 /// Like [`RoomListService::new`] but with additional configuration options.
148 ///
149 /// - `share_pos`: toggles [`SlidingSyncBuilder::share_pos`] for
150 /// cross-process position sharing.
151 /// - `connection_id`: the Sliding Sync connection ID
152 /// - `timeline_limit`: the timeline limit
153 ///
154 /// [`SlidingSyncBuilder::share_pos`]: matrix_sdk::sliding_sync::SlidingSyncBuilder::share_pos
155 pub async fn new_with(
156 client: Client,
157 share_pos: bool,
158 connection_id: &str,
159 timeline_limit: u32,
160 ) -> Result<Self, Error> {
161 let mut builder = client
162 .sliding_sync(connection_id)
163 .map_err(Error::SlidingSync)?
164 .with_account_data_extension(
165 assign!(http::request::AccountData::default(), { enabled: Some(true) }),
166 )
167 .with_receipt_extension(assign!(http::request::Receipts::default(), {
168 enabled: Some(true),
169 rooms: Some(vec![http::request::ExtensionRoomConfig::AllSubscribed])
170 }))
171 .with_typing_extension(assign!(http::request::Typing::default(), {
172 enabled: Some(true),
173 }));
174
175 match client.enabled_thread_subscriptions().await {
176 Ok(true) => {
177 debug!("Client requested thread subscriptions extension");
178
179 builder = builder.with_thread_subscriptions_extension(
180 assign!(http::request::ThreadSubscriptions::default(), {
181 enabled: Some(true),
182 limit: Some(ruma::uint!(10))
183 }),
184 );
185 }
186
187 Ok(false) => {
188 debug!(
189 "Thread subscriptions extension either not requested on the client, or the server doesn't advertise support for it: not enabling."
190 );
191 }
192
193 Err(error) => {
194 warn!(
195 ?error,
196 "Failed to check whether the client requested thread subscriptions extension: not enabling."
197 );
198 }
199 }
200
201 if share_pos {
202 // The e2ee extensions aren't enabled in this sliding sync instance, and this is
203 // the only one that could be used from a different process. So it's
204 // fine to enable position sharing (i.e. reloading it from disk),
205 // since it's always exclusively owned by the current process.
206 debug!("Enabling `share_pos` for the room list sliding sync");
207 builder = builder.share_pos();
208 }
209
210 let state_machine = StateMachine::new();
211 let observable_state = state_machine.cloned_state();
212
213 let sliding_sync = builder
214 .add_cached_list(
215 SlidingSyncList::builder(ALL_ROOMS_LIST_NAME)
216 .sync_mode(
217 SlidingSyncMode::new_selective()
218 .add_range(ALL_ROOMS_DEFAULT_SELECTIVE_RANGE),
219 )
220 .timeline_limit(timeline_limit)
221 .required_state(
222 DEFAULT_REQUIRED_STATE
223 .iter()
224 .map(|(state_event, value)| (state_event.clone(), (*value).to_owned()))
225 .collect(),
226 )
227 .filters(Some(assign!(http::request::ListFilters::default(), {
228 // As defined in the [SlidingSync MSC](https://github.com/matrix-org/matrix-spec-proposals/blob/9450ced7fb9cf5ea9077d029b3adf36aebfa8709/proposals/3575-sync.md?plain=1#L444)
229 // If unset, both invited and joined rooms are returned. If false, no invited rooms are
230 // returned. If true, only invited rooms are returned.
231 is_invite: None,
232 })))
233 .requires_timeout(move |request_generator| {
234 // We want Sliding Sync to apply the poll + network timeout —i.e. to do the
235 // long-polling— in some particular cases. Let's define them.
236 match observable_state.get() {
237 // These are the states where we want an immediate response from the
238 // server, with no long-polling.
239 State::Init
240 | State::SettingUp
241 | State::Recovering
242 | State::Error { .. }
243 | State::Terminated { .. } => PollTimeout::Some(0),
244
245 // Otherwise we want long-polling if the list is fully-loaded.
246 State::Running => {
247 if request_generator.is_fully_loaded() {
248 // Long-polling.
249 PollTimeout::Default
250 } else {
251 // No long-polling yet.
252 PollTimeout::Some(0)
253 }
254 }
255 }
256 }),
257 )
258 .await
259 .map_err(Error::SlidingSync)?
260 .build()
261 .await
262 .map(Arc::new)
263 .map_err(Error::SlidingSync)?;
264
265 // Eagerly subscribe the event cache to sync responses.
266 client.event_cache().subscribe()?;
267
268 Ok(Self { client, sliding_sync, state_machine })
269 }
270
271 /// Start to sync the room list.
272 ///
273 /// It's the main method of this entire API. Calling `sync` allows to
274 /// receive updates on the room list: new rooms, rooms updates etc. Those
275 /// updates can be read with `RoomList::entries` for example. This method
276 /// returns a [`Stream`] where produced items only hold an empty value
277 /// in case of a sync success, otherwise an error.
278 ///
279 /// The `RoomListService`' state machine is run by this method.
280 ///
281 /// Stopping the [`Stream`] (i.e. by calling [`Self::stop_sync`]), and
282 /// calling [`Self::sync`] again will resume from the previous state of
283 /// the state machine.
284 ///
285 /// This should be used only for testing. In practice, most users should be
286 /// using the [`SyncService`](crate::sync_service::SyncService) instead.
287 #[doc(hidden)]
288 pub fn sync(&self) -> impl Stream<Item = Result<(), Error>> + '_ {
289 stream! {
290 let sync = self.sliding_sync.sync();
291 pin_mut!(sync);
292
293 // This is a state machine implementation.
294 // Things happen in this order:
295 //
296 // 1. The next state is calculated,
297 // 2. The actions associated to the next state are run,
298 // 3. A sync is done,
299 // 4. The next state is stored.
300 loop {
301 debug!("Run a sync iteration");
302
303 // Calculate the next state, and run the associated actions.
304 let next_state = self.state_machine.next(&self.sliding_sync).await?;
305
306 // Do the sync.
307 match sync.next().await {
308 // Got a successful result while syncing.
309 Some(Ok(_update_summary)) => {
310 debug!(state = ?next_state, "New state");
311
312 // Update the state.
313 self.state_machine.set(next_state);
314
315 yield Ok(());
316 }
317
318 // Got an error while syncing.
319 Some(Err(error)) => {
320 debug!(expected_state = ?next_state, "New state is an error");
321
322 let next_state = State::Error { from: Box::new(next_state) };
323 self.state_machine.set(next_state);
324
325 yield Err(Error::SlidingSync(error));
326
327 break;
328 }
329
330 // Sync loop has terminated.
331 None => {
332 debug!(expected_state = ?next_state, "New state is a termination");
333
334 let next_state = State::Terminated { from: Box::new(next_state) };
335 self.state_machine.set(next_state);
336
337 break;
338 }
339 }
340 }
341 }
342 }
343
344 /// Force to stop the sync of the `RoomListService` started by
345 /// [`Self::sync`].
346 ///
347 /// It's of utter importance to call this method rather than stop polling
348 /// the `Stream` returned by [`Self::sync`] because it will force the
349 /// cancellation and exit the sync loop, i.e. it will cancel any
350 /// in-flight HTTP requests, cancel any pending futures etc. and put the
351 /// service into a termination state.
352 ///
353 /// Ideally, one wants to consume the `Stream` returned by [`Self::sync`]
354 /// until it returns `None`, because of [`Self::stop_sync`], so that it
355 /// ensures the states are correctly placed.
356 ///
357 /// Stopping the sync of the room list via this method will put the
358 /// state-machine into the [`State::Terminated`] state.
359 ///
360 /// This should be used only for testing. In practice, most users should be
361 /// using the [`SyncService`](crate::sync_service::SyncService) instead.
362 #[doc(hidden)]
363 pub fn stop_sync(&self) -> Result<(), Error> {
364 self.sliding_sync.stop_sync().map_err(Error::SlidingSync)
365 }
366
367 /// Force the sliding sync session to expire.
368 ///
369 /// This is used by [`SyncService`](crate::sync_service::SyncService).
370 ///
371 /// **Warning**: This method **must not** be called while the sync loop is
372 /// running!
373 pub(crate) async fn expire_sync_session(&self) {
374 self.sliding_sync.expire_session().await;
375
376 // Usually, when the session expires, it leads the state to be `Error`,
377 // thus some actions (like refreshing the lists) are executed. However,
378 // if the sync loop has been stopped manually, the state is `Terminated`, and
379 // when the session is forced to expire, the state remains `Terminated`, thus
380 // the actions aren't executed as expected. Consequently, let's update the
381 // state.
382 if let State::Terminated { from } = self.state_machine.get() {
383 self.state_machine.set(State::Error { from });
384 }
385 }
386
387 /// Get a [`Stream`] of [`SyncIndicator`].
388 ///
389 /// Read the documentation of [`SyncIndicator`] to learn more about it.
390 pub fn sync_indicator(
391 &self,
392 delay_before_showing: Duration,
393 delay_before_hiding: Duration,
394 ) -> impl Stream<Item = SyncIndicator> + use<> {
395 let mut state = self.state();
396
397 stream! {
398 // Ensure the `SyncIndicator` is always hidden to start with.
399 yield SyncIndicator::Hide;
400
401 // Let's not wait for an update to happen. The `SyncIndicator` must be
402 // computed as fast as possible.
403 let mut current_state = state.next_now();
404
405 loop {
406 let (sync_indicator, yield_delay) = match current_state {
407 State::SettingUp | State::Error { .. } => {
408 (SyncIndicator::Show, delay_before_showing)
409 }
410
411 State::Init | State::Recovering | State::Running | State::Terminated { .. } => {
412 (SyncIndicator::Hide, delay_before_hiding)
413 }
414 };
415
416 // `state.next().await` has a maximum of `yield_delay` time to execute…
417 let next_state = match timeout(state.next(), yield_delay).await {
418 // A new state has been received before `yield_delay` time. The new
419 // `sync_indicator` value won't be yielded.
420 Ok(next_state) => next_state,
421
422 // No new state has been received before `yield_delay` time. The
423 // `sync_indicator` value can be yielded.
424 Err(_) => {
425 yield sync_indicator;
426
427 // Now that `sync_indicator` has been yielded, let's wait on
428 // the next state again.
429 state.next().await
430 }
431 };
432
433 if let Some(next_state) = next_state {
434 // Update the `current_state`.
435 current_state = next_state;
436 } else {
437 // Something is broken with the state. Let's stop this stream too.
438 break;
439 }
440 }
441 }
442 }
443
444 /// Get the [`Client`] that has been used to create [`Self`].
445 pub fn client(&self) -> &Client {
446 &self.client
447 }
448
449 /// Get a subscriber to the state.
450 pub fn state(&self) -> Subscriber<State> {
451 self.state_machine.subscribe()
452 }
453
454 async fn list_for(&self, sliding_sync_list_name: &str) -> Result<RoomList, Error> {
455 RoomList::new(&self.client, &self.sliding_sync, sliding_sync_list_name, self.state()).await
456 }
457
458 /// Get a [`RoomList`] for all rooms.
459 pub async fn all_rooms(&self) -> Result<RoomList, Error> {
460 self.list_for(ALL_ROOMS_LIST_NAME).await
461 }
462
463 /// Get a [`Room`] if it exists.
464 pub fn room(&self, room_id: &RoomId) -> Result<Room, Error> {
465 self.client.get_room(room_id).ok_or_else(|| Error::RoomNotFound(room_id.to_owned()))
466 }
467
468 /// Subscribe to rooms.
469 ///
470 /// It means that all events from these rooms will be received every time,
471 /// no matter how the `RoomList` is configured.
472 ///
473 /// [`LatestEvents::listen_to_room`][listen_to_room] will be called for each
474 /// room in `room_ids`, so that the [`LatestEventValue`] will automatically
475 /// be calculated and updated for these rooms, for free.
476 ///
477 /// All previous room subscriptions will be forgotten.
478 ///
479 /// [listen_to_room]: matrix_sdk::latest_events::LatestEvents::listen_to_room
480 /// [`LatestEventValue`]: matrix_sdk::latest_events::LatestEventValue
481 pub async fn subscribe_to_rooms(&self, room_ids: &[&RoomId]) {
482 // Calculate the settings for the room subscriptions.
483 let settings = assign!(http::request::RoomSubscription::default(), {
484 required_state: DEFAULT_REQUIRED_STATE.iter().map(|(state_event, value)| {
485 (state_event.clone(), (*value).to_owned())
486 })
487 .chain(
488 DEFAULT_ROOM_SUBSCRIPTION_EXTRA_REQUIRED_STATE.iter().map(|(state_event, value)| {
489 (state_event.clone(), (*value).to_owned())
490 })
491 )
492 .collect(),
493 timeline_limit: UInt::from(DEFAULT_ROOM_SUBSCRIPTION_TIMELINE_LIMIT),
494 });
495
496 // Decide whether the in-flight request (if any) should be cancelled if needed.
497 let cancel_in_flight_request = match self.state_machine.get() {
498 State::Init | State::Recovering | State::Error { .. } | State::Terminated { .. } => {
499 false
500 }
501 State::SettingUp | State::Running => true,
502 };
503
504 // Before subscribing, let's listen these rooms to calculate their latest
505 // events.
506 if self.client.event_cache().has_subscribed() {
507 let latest_events = self.client.latest_events().await;
508
509 for room_id in room_ids {
510 if let Err(error) = latest_events.listen_to_room(room_id).await {
511 // Let's not fail the room subscription. Instead, emit a log because it's very
512 // unlikely to happen.
513 error!(?error, ?room_id, "Failed to listen to the latest event for this room");
514 }
515 }
516 }
517
518 // Subscribe to the rooms.
519 self.sliding_sync.clear_and_subscribe_to_rooms(
520 room_ids,
521 Some(settings),
522 cancel_in_flight_request,
523 )
524 }
525
526 #[cfg(test)]
527 pub fn sliding_sync(&self) -> &SlidingSync {
528 &self.sliding_sync
529 }
530}
531
532/// [`RoomList`]'s errors.
533#[derive(Debug, Error)]
534pub enum Error {
535 /// Error from [`matrix_sdk::SlidingSync`].
536 #[error(transparent)]
537 SlidingSync(SlidingSyncError),
538
539 /// An operation has been requested on an unknown list.
540 #[error("Unknown list `{0}`")]
541 UnknownList(String),
542
543 /// The requested room doesn't exist.
544 #[error("Room `{0}` not found")]
545 RoomNotFound(OwnedRoomId),
546
547 #[error(transparent)]
548 EventCache(#[from] EventCacheError),
549}
550
551/// An hint whether a _sync spinner/loader/toaster_ should be prompted to the
552/// user, indicating that the [`RoomListService`] is syncing.
553///
554/// This is entirely arbitrary and optinionated. Of course, once
555/// [`RoomListService::sync`] has been called, it's going to be constantly
556/// syncing, until [`RoomListService::stop_sync`] is called, or until an error
557/// happened. But in some cases, it's better for the user experience to prompt
558/// to the user that a sync is happening. It's usually the first sync, or the
559/// recovering sync. However, the sync indicator must be prompted if the
560/// aforementioned sync is “slow”, otherwise the indicator is likely to “blink”
561/// pretty fast, which can be very confusing. It's also common to indicate to
562/// the user that a syncing is happening in case of a network error, that
563/// something is catching up etc.
564#[derive(Debug, Eq, PartialEq)]
565pub enum SyncIndicator {
566 /// Show the sync indicator.
567 Show,
568
569 /// Hide the sync indicator.
570 Hide,
571}
572
573#[cfg(test)]
574mod tests {
575 use std::future::ready;
576
577 use futures_util::{StreamExt, pin_mut};
578 use matrix_sdk::{SlidingSyncMode, test_utils::mocks::MatrixMockServer};
579 use matrix_sdk_test::{TestError, async_test};
580 use ruma::{api::client::sync::sync_events::v5, assign, uint};
581
582 use super::{ALL_ROOMS_LIST_NAME, Error, RoomListService, State};
583
584 #[async_test]
585 async fn test_all_rooms_are_declared() -> Result<(), TestError> {
586 let server = MatrixMockServer::new().await;
587 let client = server.client_builder().build().await;
588 let room_list = RoomListService::new(client).await?;
589
590 let sliding_sync = room_list.sliding_sync();
591
592 // List is present, in Selective mode.
593 assert_eq!(
594 sliding_sync
595 .on_list(ALL_ROOMS_LIST_NAME, |list| ready(matches!(
596 list.sync_mode(),
597 SlidingSyncMode::Selective { ranges } if ranges == vec![0..=19]
598 )))
599 .await,
600 Some(true)
601 );
602
603 Ok(())
604 }
605
606 #[async_test]
607 async fn test_expire_sliding_sync_session_manually() -> Result<(), Error> {
608 let server = MatrixMockServer::new().await;
609 let client = server.client_builder().build().await;
610
611 let room_list = RoomListService::new(client).await?;
612
613 let sync = room_list.sync();
614 pin_mut!(sync);
615
616 // Run a first sync.
617 {
618 let _mock_guard = server
619 .mock_sliding_sync()
620 .ok({
621 let mut response = v5::Response::new("0".to_owned());
622 response.lists.insert(
623 ALL_ROOMS_LIST_NAME.to_owned(),
624 assign!(v5::response::List::default(), { count: uint!(0) }),
625 );
626 response
627 })
628 .mount_as_scoped()
629 .await;
630
631 let _ = sync.next().await;
632 }
633
634 assert_eq!(room_list.state().get(), State::SettingUp);
635
636 // Stop the sync.
637 room_list.stop_sync()?;
638
639 // Do another sync.
640 let _ = sync.next().await;
641
642 // State is `Terminated`, as expected!
643 assert_eq!(
644 room_list.state_machine.get(),
645 State::Terminated { from: Box::new(State::Running) }
646 );
647
648 // Now, let's make the sliding sync session to expire.
649 room_list.expire_sync_session().await;
650
651 // State is `Error`, as a regular session expiration would generate!
652 assert_eq!(room_list.state_machine.get(), State::Error { from: Box::new(State::Running) });
653
654 Ok(())
655 }
656}