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

315 lines
10 KiB
Rust
Raw Normal View History

use std::{
collections::BTreeMap,
fmt::Debug,
sync::{Mutex, RwLock as StdRwLock},
};
2023-03-10 11:09:08 +00:00
use eyeball::unique::Observable;
use ruma::{
api::client::sync::sync_events::v4::{
self, AccountDataConfig, E2EEConfig, ExtensionsConfig, ReceiptsConfig, ToDeviceConfig,
TypingConfig,
},
assign, OwnedRoomId,
};
use tracing::trace;
use url::Url;
use super::{
Error, FrozenSlidingSync, FrozenSlidingSyncList, SlidingSync, SlidingSyncInner,
SlidingSyncList, SlidingSyncListBuilder, SlidingSyncPositionMarkers, SlidingSyncRoom,
};
use crate::{Client, Result};
/// Configuration for a Sliding Sync instance.
///
/// Get a new builder with methods like [`crate::Client::sliding_sync`], or
/// [`crate::SlidingSync::builder`].
2023-02-20 16:09:04 +00:00
#[derive(Clone, Debug)]
pub struct SlidingSyncBuilder {
storage_key: Option<String>,
homeserver: Option<Url>,
2023-02-20 16:09:04 +00:00
client: Option<Client>,
lists: BTreeMap<String, SlidingSyncList>,
extensions: Option<ExtensionsConfig>,
subscriptions: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
}
2023-02-20 16:09:04 +00:00
impl SlidingSyncBuilder {
pub(super) fn new() -> Self {
Self {
storage_key: None,
homeserver: None,
client: None,
lists: BTreeMap::new(),
2023-02-20 16:09:04 +00:00
extensions: None,
subscriptions: BTreeMap::new(),
}
}
2023-02-20 16:09:04 +00:00
/// Set the storage key to keep this cache at and load it from.
pub fn storage_key(mut self, value: Option<String>) -> Self {
self.storage_key = value;
self
}
2023-02-20 16:09:04 +00:00
/// Set the homeserver for sliding sync only.
pub fn homeserver(mut self, value: Url) -> Self {
self.homeserver = Some(value);
self
}
2023-02-20 16:09:04 +00:00
/// Set the client this sliding sync will be using.
pub fn client(mut self, value: Client) -> Self {
self.client = Some(value);
self
}
2023-02-20 16:09:04 +00:00
pub(super) fn subscriptions(
mut self,
value: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
) -> Self {
self.subscriptions = value;
self
}
/// Convenience function to add a full-sync list to the builder
pub fn add_fullsync_list(self) -> Self {
self.add_list(
SlidingSyncListBuilder::default_with_fullsync()
.build()
.expect("Building default full sync list doesn't fail"),
)
}
/// The cold cache key to read from and store the frozen state at
pub fn cold_cache<T: ToString>(mut self, name: T) -> Self {
2023-02-20 16:09:04 +00:00
self.storage_key = Some(name.to_string());
self
}
/// Do not use the cold cache
pub fn no_cold_cache(mut self) -> Self {
self.storage_key = None;
self
}
/// Reset the lists to `None`
pub fn no_lists(mut self) -> Self {
self.lists.clear();
self
}
/// Add the given list to the lists.
///
/// Replace any list with the name.
pub fn add_list(mut self, list: SlidingSyncList) -> Self {
self.lists.insert(list.name.clone(), list);
self
}
/// Activate e2ee, to-device-message and account data extensions if not yet
/// configured.
///
/// Will leave any extension configuration found untouched, so the order
/// does not matter.
pub fn with_common_extensions(mut self) -> Self {
{
2023-02-20 16:09:04 +00:00
let mut cfg = self.extensions.get_or_insert_with(Default::default);
if cfg.to_device.is_none() {
cfg.to_device = Some(assign!(ToDeviceConfig::default(), { enabled: Some(true) }));
}
if cfg.e2ee.is_none() {
cfg.e2ee = Some(assign!(E2EEConfig::default(), { enabled: Some(true) }));
}
if cfg.account_data.is_none() {
cfg.account_data =
Some(assign!(AccountDataConfig::default(), { enabled: Some(true) }));
}
}
self
}
/// Activate e2ee, to-device-message, account data, typing and receipt
/// extensions if not yet configured.
///
/// Will leave any extension configuration found untouched, so the order
/// does not matter.
pub fn with_all_extensions(mut self) -> Self {
{
2023-02-20 16:09:04 +00:00
let mut cfg = self.extensions.get_or_insert_with(Default::default);
if cfg.to_device.is_none() {
cfg.to_device = Some(assign!(ToDeviceConfig::default(), { enabled: Some(true) }));
}
if cfg.e2ee.is_none() {
cfg.e2ee = Some(assign!(E2EEConfig::default(), { enabled: Some(true) }));
}
if cfg.account_data.is_none() {
cfg.account_data =
Some(assign!(AccountDataConfig::default(), { enabled: Some(true) }));
}
if cfg.receipts.is_none() {
cfg.receipts = Some(assign!(ReceiptsConfig::default(), { enabled: Some(true) }));
}
if cfg.typing.is_none() {
cfg.typing = Some(assign!(TypingConfig::default(), { enabled: Some(true) }));
}
}
self
}
/// Set the E2EE extension configuration.
pub fn with_e2ee_extension(mut self, e2ee: E2EEConfig) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).e2ee = Some(e2ee);
self
}
/// Unset the E2EE extension configuration.
pub fn without_e2ee_extension(mut self) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).e2ee = None;
self
}
/// Set the ToDevice extension configuration.
pub fn with_to_device_extension(mut self, to_device: ToDeviceConfig) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).to_device = Some(to_device);
self
}
/// Unset the ToDevice extension configuration.
pub fn without_to_device_extension(mut self) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).to_device = None;
self
}
/// Set the account data extension configuration.
pub fn with_account_data_extension(mut self, account_data: AccountDataConfig) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).account_data = Some(account_data);
self
}
/// Unset the account data extension configuration.
pub fn without_account_data_extension(mut self) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).account_data = None;
self
}
/// Set the Typing extension configuration.
pub fn with_typing_extension(mut self, typing: TypingConfig) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).typing = Some(typing);
self
}
/// Unset the Typing extension configuration.
pub fn without_typing_extension(mut self) -> Self {
2023-02-20 16:09:04 +00:00
self.extensions.get_or_insert_with(Default::default).typing = None;
self
}
/// Set the Receipt extension configuration.
pub fn with_receipt_extension(mut self, receipt: ReceiptsConfig) -> Self {
self.extensions.get_or_insert_with(Default::default).receipts = Some(receipt);
self
}
/// Unset the Receipt extension configuration.
pub fn without_receipt_extension(mut self) -> Self {
self.extensions.get_or_insert_with(Default::default).receipts = None;
self
}
2023-02-20 16:09:04 +00:00
/// Build the Sliding Sync.
///
2023-02-22 17:25:16 +00:00
/// If `self.storage_key` is `Some(_)`, load the cached data from cold
/// storage.
2023-02-20 16:09:04 +00:00
pub async fn build(mut self) -> Result<SlidingSync> {
let client = self.client.ok_or(Error::BuildMissingField("client"))?;
let mut delta_token = None;
2023-02-20 16:09:04 +00:00
let mut rooms_found: BTreeMap<OwnedRoomId, SlidingSyncRoom> = BTreeMap::new();
if let Some(storage_key) = &self.storage_key {
trace!(storage_key, "trying to load from cold");
for (name, list) in &mut self.lists {
if let Some(frozen_list) = client
2023-02-20 16:09:04 +00:00
.store()
.get_custom_value(format!("{storage_key}::{name}").as_bytes())
.await?
.map(|v| serde_json::from_slice::<FrozenSlidingSyncList>(&v))
2023-02-20 16:09:04 +00:00
.transpose()?
{
trace!(name, "frozen for list found");
2023-02-20 16:09:04 +00:00
fix(sdk): Fix, test, and clean up `SlidingSyncList`. This patch should ideally be split into multiple smaller ones, but life is life. This main purpose of this patch is to fix and to test `SlidingSyncListRequestGenerator`. This quest has led me to rename mutiple fields in `SlidingSyncList` and `SlidingSyncListBuilder`, like: * `rooms_count` becomes `maximum_number_of_rooms`, it's not something the _client_ counts, but it's a maximum number given by the server, * `batch_size` becomes `full_sync_batch_size`, so that now, it emphasizes that it's about full-sync only, * `limit` becomes `full_sync_maximum_number_of_rooms_to_fetch`, so that now, it also emphasizes that it's about ful-sync only _and_ what the limit is about! This quest has continued with the renaming of the `SlidingSyncMode` variants. After a discussion with the ElementX team, we've agreed on the following renamings: * `Cold` becomes `NotLoaded`, * `Preload` becomes `Preloaded`, * `CatchingUp` becomes `PartiallyLoaded`, * `Live` becomes `FullyLoaded`. Finally, _le plat de résistance_. In `SlidingSyncListRequestGenerator`, the `make_request_for_ranges` has been renamed to `build_request` and no longer takes a `&mut self` but a simpler `&self`! It didn't make sense to me that something that make/build a request was modifying `Self`. Because the type of `SlidingSyncListRequestGenerator::ranges` has changed, all ranges now have a consistent type (within this module at least). Consequently, this method no longer need to do a type conversion. Still on the same type, the `update_state` method is much more documented, and errors on range bounds (offset by 1) are now all fixed. The creation of new ranges happens in a new dedicated pure function, `create_range`. It returns an `Option` because it's possible to not be able to compute a range (previously, invalid ranges were considered valid). It's used in the `Iterator` implementation. This `Iterator` implementation contains a liiiittle bit more code, but at least now we understand what it does, and it's clear what `range_start` and `desired_size` we calculate. By the way, the `prefetch_request` method has been removed: it's not a prefetch, it's a regular request; it was calculating the range. But now there is `create_range`, and since it's pure, we can unit test it! _Pour le dessert_, this patch adds multiple tests. It is now possible because of the previous refactoring. First off, we test the `create_range` in many configurations. It's pretty clear to understand, and since it's core to `SlidingSyncListRequestGenerator`, I'm pretty happy with how it ends. Second, we test paging-, growing- and selective- mode with a new macro: `assert_request_and_response`, which allows to “send” requests, and to “receive” responses. The design of `SlidingSync` allows to mimic requests and responses, that's great. We don't really care about the responses here, but we care about the requests' `ranges`, and the `SlidingSyncList.state` after a response is received. It also helps to see how ranges behaves when the state is `PartiallyLoaded` or `FullyLoaded`.
2023-03-15 15:57:29 +00:00
let FrozenSlidingSyncList { maximum_number_of_rooms, rooms_list, rooms } =
frozen_list;
list.set_from_cold(maximum_number_of_rooms, rooms_list);
2023-02-20 16:09:04 +00:00
for (key, frozen_room) in rooms.into_iter() {
rooms_found.entry(key).or_insert_with(|| {
SlidingSyncRoom::from_frozen(frozen_room, client.clone())
});
}
} else {
trace!(name, "no frozen state for list found");
2023-02-20 16:09:04 +00:00
}
}
if let Some(FrozenSlidingSync { to_device_since, delta_token: frozen_delta_token }) =
client
.store()
.get_custom_value(storage_key.as_bytes())
.await?
.map(|v| serde_json::from_slice::<FrozenSlidingSync>(&v))
.transpose()?
2023-02-20 16:09:04 +00:00
{
trace!("frozen for generic found");
2023-02-20 16:09:04 +00:00
if let Some(since) = to_device_since {
if let Some(to_device_ext) =
self.extensions.get_or_insert_with(Default::default).to_device.as_mut()
{
to_device_ext.since = Some(since);
}
}
delta_token = frozen_delta_token;
2023-02-20 16:09:04 +00:00
}
2023-02-20 16:09:04 +00:00
trace!("sync unfrozen done");
};
trace!(len = rooms_found.len(), "rooms unfrozen");
let rooms = StdRwLock::new(rooms_found);
let lists = StdRwLock::new(self.lists);
2023-02-20 16:09:04 +00:00
Ok(SlidingSync::new(SlidingSyncInner {
2023-02-20 16:09:04 +00:00
homeserver: self.homeserver,
client,
storage_key: self.storage_key,
lists,
2023-02-20 16:09:04 +00:00
rooms,
extensions: Mutex::new(self.extensions),
reset_counter: Default::default(),
2023-02-20 16:09:04 +00:00
position: StdRwLock::new(SlidingSyncPositionMarkers {
pos: Observable::new(None),
delta_token: Observable::new(delta_token),
}),
subscriptions: StdRwLock::new(self.subscriptions),
2023-02-20 16:09:04 +00:00
unsubscribe: Default::default(),
}))
}
}