2023-02-20 09:43:05 +00:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
|
|
|
fmt::Debug,
|
2023-03-08 14:05:04 +00:00
|
|
|
sync::{Mutex, RwLock as StdRwLock},
|
2023-02-20 09:43:05 +00:00
|
|
|
};
|
|
|
|
|
2023-03-10 11:09:08 +00:00
|
|
|
use eyeball::unique::Observable;
|
2023-02-20 09:43:05 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::sync::sync_events::v4::{
|
2023-03-08 12:53:23 +00:00
|
|
|
self, AccountDataConfig, E2EEConfig, ExtensionsConfig, ReceiptsConfig, ToDeviceConfig,
|
2023-02-20 09:43:05 +00:00
|
|
|
TypingConfig,
|
|
|
|
},
|
|
|
|
assign, OwnedRoomId,
|
|
|
|
};
|
|
|
|
use tracing::trace;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use super::{
|
2023-03-08 11:12:58 +00:00
|
|
|
Error, FrozenSlidingSync, FrozenSlidingSyncList, SlidingSync, SlidingSyncInner,
|
2023-03-09 15:40:26 +00:00
|
|
|
SlidingSyncList, SlidingSyncListBuilder, SlidingSyncPositionMarkers, SlidingSyncRoom,
|
2023-02-20 09:43:05 +00:00
|
|
|
};
|
|
|
|
use crate::{Client, Result};
|
|
|
|
|
2023-02-22 16:29:40 +00:00
|
|
|
/// 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 {
|
2023-02-20 09:43:05 +00:00
|
|
|
storage_key: Option<String>,
|
|
|
|
homeserver: Option<Url>,
|
2023-02-20 16:09:04 +00:00
|
|
|
client: Option<Client>,
|
2023-03-01 13:18:40 +00:00
|
|
|
lists: BTreeMap<String, SlidingSyncList>,
|
2023-02-20 09:43:05 +00:00
|
|
|
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,
|
2023-03-01 13:18:40 +00:00
|
|
|
lists: BTreeMap::new(),
|
2023-02-20 16:09:04 +00:00
|
|
|
extensions: None,
|
|
|
|
subscriptions: BTreeMap::new(),
|
|
|
|
}
|
|
|
|
}
|
2023-02-20 09:43:05 +00:00
|
|
|
|
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 09:43:05 +00:00
|
|
|
|
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 09:43:05 +00:00
|
|
|
|
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 09:43:05 +00:00
|
|
|
|
2023-02-20 16:09:04 +00:00
|
|
|
pub(super) fn subscriptions(
|
|
|
|
mut self,
|
|
|
|
value: BTreeMap<OwnedRoomId, v4::RoomSubscription>,
|
|
|
|
) -> Self {
|
|
|
|
self.subscriptions = value;
|
|
|
|
self
|
2023-02-20 09:43:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 13:18:40 +00:00
|
|
|
/// Convenience function to add a full-sync list to the builder
|
|
|
|
pub fn add_fullsync_list(self) -> Self {
|
|
|
|
self.add_list(
|
2023-03-01 11:09:34 +00:00
|
|
|
SlidingSyncListBuilder::default_with_fullsync()
|
2023-02-20 09:43:05 +00:00
|
|
|
.build()
|
2023-03-01 13:18:40 +00:00
|
|
|
.expect("Building default full sync list doesn't fail"),
|
2023-02-20 09:43:05 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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());
|
2023-02-20 09:43:05 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do not use the cold cache
|
|
|
|
pub fn no_cold_cache(mut self) -> Self {
|
|
|
|
self.storage_key = None;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:18:40 +00:00
|
|
|
/// Reset the lists to `None`
|
|
|
|
pub fn no_lists(mut self) -> Self {
|
|
|
|
self.lists.clear();
|
2023-02-20 09:43:05 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:18:40 +00:00
|
|
|
/// Add the given list to the lists.
|
2023-02-20 09:43:05 +00:00
|
|
|
///
|
2023-03-01 13:18:40 +00:00
|
|
|
/// Replace any list with the name.
|
|
|
|
pub fn add_list(mut self, list: SlidingSyncList) -> Self {
|
|
|
|
self.lists.insert(list.name.clone(), list);
|
|
|
|
|
2023-02-20 09:43:05 +00:00
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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) }));
|
|
|
|
}
|
|
|
|
|
2023-03-08 12:53:23 +00:00
|
|
|
if cfg.receipts.is_none() {
|
|
|
|
cfg.receipts = Some(assign!(ReceiptsConfig::default(), { enabled: Some(true) }));
|
2023-02-20 09:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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;
|
2023-02-20 09:43:05 +00:00
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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;
|
2023-02-20 09:43:05 +00:00
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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;
|
2023-02-20 09:43:05 +00:00
|
|
|
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);
|
2023-02-20 09:43:05 +00:00
|
|
|
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;
|
2023-02-20 09:43:05 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the Receipt extension configuration.
|
2023-03-08 12:53:23 +00:00
|
|
|
pub fn with_receipt_extension(mut self, receipt: ReceiptsConfig) -> Self {
|
|
|
|
self.extensions.get_or_insert_with(Default::default).receipts = Some(receipt);
|
2023-02-20 09:43:05 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unset the Receipt extension configuration.
|
|
|
|
pub fn without_receipt_extension(mut self) -> Self {
|
2023-03-08 12:53:23 +00:00
|
|
|
self.extensions.get_or_insert_with(Default::default).receipts = None;
|
2023-02-20 09:43:05 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:09:04 +00:00
|
|
|
/// Build the Sliding Sync.
|
2023-02-20 09:43:05 +00:00
|
|
|
///
|
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"))?;
|
|
|
|
|
2023-03-09 15:40:26 +00:00
|
|
|
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");
|
|
|
|
|
2023-03-01 13:18:40 +00:00
|
|
|
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?
|
2023-03-01 11:09:34 +00:00
|
|
|
.map(|v| serde_json::from_slice::<FrozenSlidingSyncList>(&v))
|
2023-02-20 16:09:04 +00:00
|
|
|
.transpose()?
|
|
|
|
{
|
2023-03-01 13:18:40 +00:00
|
|
|
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-22 16:29:40 +00:00
|
|
|
|
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 {
|
2023-03-01 13:18:40 +00:00
|
|
|
trace!(name, "no frozen state for list found");
|
2023-02-20 16:09:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:40:26 +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-22 16:29:40 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 16:29:40 +00:00
|
|
|
|
2023-03-09 15:40:26 +00:00
|
|
|
delta_token = frozen_delta_token;
|
2023-02-20 16:09:04 +00:00
|
|
|
}
|
2023-02-22 16:29:40 +00:00
|
|
|
|
2023-02-20 16:09:04 +00:00
|
|
|
trace!("sync unfrozen done");
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!(len = rooms_found.len(), "rooms unfrozen");
|
2023-02-22 16:29:40 +00:00
|
|
|
|
2023-03-08 11:12:58 +00:00
|
|
|
let rooms = StdRwLock::new(rooms_found);
|
|
|
|
let lists = StdRwLock::new(self.lists);
|
2023-02-20 16:09:04 +00:00
|
|
|
|
2023-03-08 14:05:04 +00:00
|
|
|
Ok(SlidingSync::new(SlidingSyncInner {
|
2023-02-20 16:09:04 +00:00
|
|
|
homeserver: self.homeserver,
|
|
|
|
client,
|
|
|
|
storage_key: self.storage_key,
|
|
|
|
|
2023-03-01 13:18:40 +00:00
|
|
|
lists,
|
2023-02-20 16:09:04 +00:00
|
|
|
rooms,
|
|
|
|
|
2023-03-08 14:05:04 +00:00
|
|
|
extensions: Mutex::new(self.extensions),
|
2023-02-27 11:09:14 +00:00
|
|
|
reset_counter: Default::default(),
|
2023-02-20 16:09:04 +00:00
|
|
|
|
2023-03-09 15:40:26 +00:00
|
|
|
position: StdRwLock::new(SlidingSyncPositionMarkers {
|
|
|
|
pos: Observable::new(None),
|
|
|
|
delta_token: Observable::new(delta_token),
|
|
|
|
}),
|
|
|
|
|
2023-03-08 14:05:04 +00:00
|
|
|
subscriptions: StdRwLock::new(self.subscriptions),
|
2023-02-20 16:09:04 +00:00
|
|
|
unsubscribe: Default::default(),
|
2023-03-08 14:05:04 +00:00
|
|
|
}))
|
2023-02-20 09:43:05 +00:00
|
|
|
}
|
|
|
|
}
|