mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-11 17:33:51 -04:00
feat(sdk): SlidingSync::get_rooms? are now async.
This commit is contained in:
@@ -701,12 +701,16 @@ impl SlidingSync {
|
||||
}
|
||||
|
||||
pub fn get_room(&self, room_id: String) -> Result<Option<Arc<SlidingSyncRoom>>, ClientError> {
|
||||
Ok(self.inner.get_room(<&RoomId>::try_from(room_id.as_str())?).map(|inner| {
|
||||
Arc::new(SlidingSyncRoom {
|
||||
inner,
|
||||
sliding_sync: self.inner.clone(),
|
||||
client: self.client.clone(),
|
||||
timeline: Default::default(),
|
||||
let room_id = <&RoomId>::try_from(room_id.as_str())?;
|
||||
|
||||
Ok(RUNTIME.block_on(async move {
|
||||
self.inner.get_room(room_id).await.map(|inner| {
|
||||
Arc::new(SlidingSyncRoom {
|
||||
inner,
|
||||
sliding_sync: self.inner.clone(),
|
||||
client: self.client.clone(),
|
||||
timeline: Default::default(),
|
||||
})
|
||||
})
|
||||
}))
|
||||
}
|
||||
@@ -719,21 +723,24 @@ impl SlidingSync {
|
||||
.into_iter()
|
||||
.map(OwnedRoomId::try_from)
|
||||
.collect::<Result<Vec<OwnedRoomId>, IdParseError>>()?;
|
||||
Ok(self
|
||||
.inner
|
||||
.get_rooms(actual_ids.into_iter())
|
||||
.into_iter()
|
||||
.map(|o| {
|
||||
o.map(|inner| {
|
||||
Arc::new(SlidingSyncRoom {
|
||||
inner,
|
||||
sliding_sync: self.inner.clone(),
|
||||
client: self.client.clone(),
|
||||
timeline: Default::default(),
|
||||
|
||||
Ok(RUNTIME.block_on(async move {
|
||||
self.inner
|
||||
.get_rooms(actual_ids.into_iter())
|
||||
.await
|
||||
.into_iter()
|
||||
.map(|o| {
|
||||
o.map(|inner| {
|
||||
Arc::new(SlidingSyncRoom {
|
||||
inner,
|
||||
sliding_sync: self.inner.clone(),
|
||||
client: self.client.clone(),
|
||||
timeline: Default::default(),
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
.collect()
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn add_list(&self, list_builder: Arc<SlidingSyncListBuilder>) -> Arc<TaskHandle> {
|
||||
|
||||
@@ -60,7 +60,7 @@
|
||||
//! [`RoomList::state_stream`] provides a way to get a stream of the state
|
||||
//! machine's state, which can be pretty helpful for the client app.
|
||||
|
||||
use std::future::ready;
|
||||
use std::{future::ready, sync::Arc};
|
||||
|
||||
use async_stream::stream;
|
||||
use async_trait::async_trait;
|
||||
@@ -71,9 +71,10 @@ use imbl::Vector;
|
||||
pub use matrix_sdk::RoomListEntry;
|
||||
use matrix_sdk::{
|
||||
sliding_sync::Ranges, Client, Error as SlidingSyncError, SlidingSync, SlidingSyncList,
|
||||
SlidingSyncMode,
|
||||
SlidingSyncMode, SlidingSyncRoom,
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use ruma::RoomId;
|
||||
use thiserror::Error;
|
||||
|
||||
pub const ALL_ROOMS_LIST_NAME: &str = "all_rooms";
|
||||
@@ -221,12 +222,35 @@ impl RoomList {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
|
||||
self.sliding_sync.get_room(room_id).map(Room::new)
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "testing"))]
|
||||
pub fn sliding_sync(&self) -> &SlidingSync {
|
||||
&self.sliding_sync
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Room {
|
||||
inner: Arc<RoomInner>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct RoomInner {
|
||||
sliding_sync_room: SlidingSyncRoom,
|
||||
room: Option<matrix_sdk::room::Room>,
|
||||
}
|
||||
|
||||
impl Room {
|
||||
fn new(sliding_sync_room: SlidingSyncRoom) -> Self {
|
||||
let room = sliding_sync_room.client().get_room(sliding_sync_room.room_id());
|
||||
|
||||
Self { inner: Arc::new(RoomInner { sliding_sync_room, room }) }
|
||||
}
|
||||
}
|
||||
|
||||
/// [`RoomList`]'s errors.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -187,7 +187,7 @@ async fn create_one_room(
|
||||
|
||||
assert!(update.rooms.contains(&room_id.to_owned()));
|
||||
|
||||
let room = sliding_sync.get_room(room_id).context("`get_room`")?;
|
||||
let room = sliding_sync.get_room(room_id).await.context("`get_room`")?;
|
||||
assert_eq!(room.name(), Some(room_name.clone()));
|
||||
|
||||
Ok(())
|
||||
@@ -199,6 +199,7 @@ async fn timeline(
|
||||
) -> Result<(Vector<Arc<TimelineItem>>, impl Stream<Item = VectorDiff<Arc<TimelineItem>>>)> {
|
||||
Ok(sliding_sync
|
||||
.get_room(room_id)
|
||||
.await
|
||||
.unwrap()
|
||||
.timeline()
|
||||
.await
|
||||
|
||||
@@ -164,8 +164,8 @@ impl SlidingSync {
|
||||
}
|
||||
|
||||
/// Lookup a specific room
|
||||
pub fn get_room(&self, room_id: &RoomId) -> Option<SlidingSyncRoom> {
|
||||
self.inner.rooms.blocking_read().get(room_id).cloned()
|
||||
pub async fn get_room(&self, room_id: &RoomId) -> Option<SlidingSyncRoom> {
|
||||
self.inner.rooms.read().await.get(room_id).cloned()
|
||||
}
|
||||
|
||||
/// Check the number of rooms.
|
||||
@@ -247,18 +247,18 @@ impl SlidingSync {
|
||||
}
|
||||
|
||||
/// Lookup a set of rooms
|
||||
pub fn get_rooms<I: Iterator<Item = OwnedRoomId>>(
|
||||
pub async fn get_rooms<I: Iterator<Item = OwnedRoomId>>(
|
||||
&self,
|
||||
room_ids: I,
|
||||
) -> Vec<Option<SlidingSyncRoom>> {
|
||||
let rooms = self.inner.rooms.blocking_read();
|
||||
let rooms = self.inner.rooms.read().await;
|
||||
|
||||
room_ids.map(|room_id| rooms.get(&room_id).cloned()).collect()
|
||||
}
|
||||
|
||||
/// Get all rooms.
|
||||
pub fn get_all_rooms(&self) -> Vec<SlidingSyncRoom> {
|
||||
self.inner.rooms.blocking_read().values().cloned().collect()
|
||||
pub async fn get_all_rooms(&self) -> Vec<SlidingSyncRoom> {
|
||||
self.inner.rooms.read().await.values().cloned().collect()
|
||||
}
|
||||
|
||||
fn prepare_extension_config(&self, pos: Option<&str>) -> ExtensionsConfig {
|
||||
|
||||
Reference in New Issue
Block a user