mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-10 08:53:00 -04:00
refactor(sdk): Remove SlidingSyncRoom \o/.
This patch FINALLY removes `SlidingSyncRoom`, youhou!
This commit is contained in:
@@ -83,7 +83,7 @@ pub use room::Room;
|
||||
pub use ruma::{IdParseError, OwnedServerName, ServerName};
|
||||
pub use sliding_sync::{
|
||||
SlidingSync, SlidingSyncBuilder, SlidingSyncList, SlidingSyncListBuilder,
|
||||
SlidingSyncListLoadingState, SlidingSyncMode, SlidingSyncRoom, UpdateSummary,
|
||||
SlidingSyncListLoadingState, SlidingSyncMode, UpdateSummary,
|
||||
};
|
||||
|
||||
#[cfg(feature = "uniffi")]
|
||||
|
||||
@@ -124,20 +124,13 @@ Next to the room list, the details for rooms are the next important aspect.
|
||||
Each [list](#lists) only references the [`OwnedRoomId`] of the room at the given
|
||||
position. The details (`required_state`s and timeline items) requested by all
|
||||
lists are bundled, together with the common details (e.g. whether it is a `dm`
|
||||
or its calculated name) and made available on the Sliding Sync session struct as
|
||||
a [reactive](#reactive-api) through [`.get_all_rooms`](SlidingSync::get_all_rooms),
|
||||
[`get_room`](SlidingSync::get_room) and [`get_rooms`](SlidingSync::get_rooms)
|
||||
APIs.
|
||||
or its calculated name). Use the `Room` API to get these updated data.
|
||||
|
||||
Notably, this map only knows about the rooms that have come down [Sliding
|
||||
Sync protocol][MSC] and if the given room isn't in any active list range, it
|
||||
may be stale. Additionally to selecting the room data via the room lists,
|
||||
the [Sliding Sync protocol][MSC] allows to subscribe to specific rooms via
|
||||
the [`subscribe_to_rooms()`](SlidingSync::subscribe_to_rooms). Any room subscribed
|
||||
to will receive updates (with the given settings) regardless of whether they are
|
||||
visible in any list. The most common case for using this API is when the user
|
||||
enters a room - as we want to receive the incoming new messages regardless of
|
||||
whether the room is pushed out of the lists room list.
|
||||
It is possible [`subscribe_to_rooms()`](SlidingSync::subscribe_to_rooms): any
|
||||
room subscribed to will receive updates (with the given settings) regardless of
|
||||
whether they are visible in any list. The most common case for using this API
|
||||
is when the user enters a room - as we want to receive the incoming new messages
|
||||
regardless of whether the room is pushed out of the lists room list.
|
||||
|
||||
## Extensions
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ mod cache;
|
||||
mod client;
|
||||
mod error;
|
||||
mod list;
|
||||
mod room;
|
||||
mod sticky_parameters;
|
||||
mod utils;
|
||||
|
||||
@@ -50,7 +49,7 @@ use tracing::{debug, error, info, instrument, trace, warn, Instrument, Span};
|
||||
|
||||
#[cfg(feature = "e2e-encryption")]
|
||||
use self::utils::JoinHandleExt as _;
|
||||
pub use self::{builder::*, client::VersionBuilderError, error::*, list::*, room::*};
|
||||
pub use self::{builder::*, client::VersionBuilderError, error::*, list::*};
|
||||
use self::{
|
||||
cache::restore_sliding_sync_state,
|
||||
client::SlidingSyncResponseProcessor,
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
use ruma::{OwnedRoomId, RoomId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The state of a [`SlidingSyncRoom`].
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq)]
|
||||
pub enum SlidingSyncRoomState {
|
||||
/// The room is not loaded, i.e. not updates have been received yet.
|
||||
#[default]
|
||||
NotLoaded,
|
||||
|
||||
/// The room has been preloaded, i.e. its values come from the cache, but no
|
||||
/// updates have been received yet.
|
||||
Preloaded,
|
||||
|
||||
/// The room has received updates.
|
||||
Loaded,
|
||||
}
|
||||
|
||||
/// A Sliding Sync Room.
|
||||
///
|
||||
/// It contains some information about a specific room, along with a queue of
|
||||
/// events for the timeline.
|
||||
///
|
||||
/// It is OK to clone this type as much as you need: cloning it is cheap, and
|
||||
/// shallow. All clones of the same value are sharing the same state.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SlidingSyncRoom {
|
||||
inner: Arc<SlidingSyncRoomInner>,
|
||||
}
|
||||
|
||||
impl SlidingSyncRoom {
|
||||
/// Create a new `SlidingSyncRoom`.
|
||||
pub fn new(room_id: OwnedRoomId) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(SlidingSyncRoomInner {
|
||||
room_id,
|
||||
state: RwLock::new(SlidingSyncRoomState::NotLoaded),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the room ID of this `SlidingSyncRoom`.
|
||||
pub fn room_id(&self) -> &RoomId {
|
||||
&self.inner.room_id
|
||||
}
|
||||
|
||||
pub(super) fn update_state(&mut self) {
|
||||
let mut state = self.inner.state.write().unwrap();
|
||||
*state = SlidingSyncRoomState::Loaded;
|
||||
}
|
||||
|
||||
pub(super) fn from_frozen(frozen_room: FrozenSlidingSyncRoom) -> Self {
|
||||
let FrozenSlidingSyncRoom { room_id } = frozen_room;
|
||||
|
||||
Self {
|
||||
inner: Arc::new(SlidingSyncRoomInner {
|
||||
room_id,
|
||||
state: RwLock::new(SlidingSyncRoomState::Preloaded),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl SlidingSyncRoom {
|
||||
fn state(&self) -> SlidingSyncRoomState {
|
||||
*self.inner.state.read().unwrap()
|
||||
}
|
||||
|
||||
fn set_state(&mut self, state: SlidingSyncRoomState) {
|
||||
*self.inner.state.write().unwrap() = state;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SlidingSyncRoomInner {
|
||||
/// The room ID.
|
||||
room_id: OwnedRoomId,
|
||||
|
||||
/// Internal state of `Self`.
|
||||
state: RwLock<SlidingSyncRoomState>,
|
||||
}
|
||||
|
||||
/// A “frozen” [`SlidingSyncRoom`], i.e. that can be written into, or read from
|
||||
/// a store.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(super) struct FrozenSlidingSyncRoom {
|
||||
pub(super) room_id: OwnedRoomId,
|
||||
}
|
||||
|
||||
impl From<&SlidingSyncRoom> for FrozenSlidingSyncRoom {
|
||||
fn from(value: &SlidingSyncRoom) -> Self {
|
||||
Self { room_id: value.inner.room_id.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use matrix_sdk_test::async_test;
|
||||
use ruma::room_id;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::sliding_sync::{FrozenSlidingSyncRoom, SlidingSyncRoom, SlidingSyncRoomState};
|
||||
|
||||
#[async_test]
|
||||
async fn test_state_from_not_loaded() {
|
||||
let mut room = SlidingSyncRoom::new(room_id!("!foo:bar.org").to_owned());
|
||||
|
||||
assert_eq!(room.state(), SlidingSyncRoomState::NotLoaded);
|
||||
|
||||
// Update with an empty response, but it doesn't matter.
|
||||
room.update_state();
|
||||
|
||||
assert_eq!(room.state(), SlidingSyncRoomState::Loaded);
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn test_state_from_preloaded() {
|
||||
let mut room = SlidingSyncRoom::new(room_id!("!foo:bar.org").to_owned());
|
||||
|
||||
room.set_state(SlidingSyncRoomState::Preloaded);
|
||||
|
||||
// Update with an empty response, but it doesn't matter.
|
||||
room.update_state();
|
||||
|
||||
assert_eq!(room.state(), SlidingSyncRoomState::Loaded);
|
||||
}
|
||||
|
||||
#[async_test]
|
||||
async fn test_room_room_id() {
|
||||
let room_id = room_id!("!foo:bar.org");
|
||||
let room = SlidingSyncRoom::new(room_id.to_owned());
|
||||
|
||||
assert_eq!(room.room_id(), room_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_frozen_sliding_sync_room_serialization() {
|
||||
let frozen_room =
|
||||
FrozenSlidingSyncRoom { room_id: room_id!("!29fhd83h92h0:example.com").to_owned() };
|
||||
|
||||
let serialized = serde_json::to_value(&frozen_room).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
serialized,
|
||||
json!({
|
||||
"room_id": "!29fhd83h92h0:example.com",
|
||||
})
|
||||
);
|
||||
|
||||
let deserialized = serde_json::from_value::<FrozenSlidingSyncRoom>(serialized).unwrap();
|
||||
|
||||
assert_eq!(deserialized.room_id, frozen_room.room_id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user