From 4cc7237db37eaeec8eb084d4529d97c806c9cd78 Mon Sep 17 00:00:00 2001 From: Fisher Darling Date: Thu, 17 Jun 2021 23:10:31 -0600 Subject: [PATCH] remove getting every state event and add getters to room --- matrix_sdk/src/room/common.rs | 23 +++++++++++++++++++++ matrix_sdk_base/src/store/memory_store.rs | 22 +++----------------- matrix_sdk_base/src/store/mod.rs | 13 +++--------- matrix_sdk_base/src/store/sled_store/mod.rs | 18 +++------------- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/matrix_sdk/src/room/common.rs b/matrix_sdk/src/room/common.rs index cb425425..2e4aeebf 100644 --- a/matrix_sdk/src/room/common.rs +++ b/matrix_sdk/src/room/common.rs @@ -8,6 +8,8 @@ use ruma::{ membership::{get_member_events, join_room_by_id, leave_room}, message::get_message_events, }, + events::{AnySyncStateEvent, EventType}, + serde::Raw, UserId, }; @@ -324,4 +326,25 @@ impl Common { .map(|member| RoomMember::new(self.client.clone(), member)) .collect()) } + + /// Get all state events of a given type in this room. + pub async fn get_state_events( + &self, + event_type: EventType, + ) -> Result>> { + self.client.store().get_state_events(self.room_id(), event_type).await.map_err(Into::into) + } + + /// Get a specific state event in this room. + pub async fn get_state_event( + &self, + event_type: EventType, + state_key: &str, + ) -> Result>> { + self.client + .store() + .get_state_event(self.room_id(), event_type, state_key) + .await + .map_err(Into::into) + } } diff --git a/matrix_sdk_base/src/store/memory_store.rs b/matrix_sdk_base/src/store/memory_store.rs index cb474676..fb65d995 100644 --- a/matrix_sdk_base/src/store/memory_store.rs +++ b/matrix_sdk_base/src/store/memory_store.rs @@ -293,19 +293,7 @@ impl MemoryStore { })) } - async fn get_state_events(&self, room_id: &RoomId) -> Result>> { - #[allow(clippy::map_clone)] - Ok(self - .room_state - .get(room_id) - .map(|r| { - r.iter().flat_map(|t| t.clone().into_iter().map(|(_, e)| e.clone())).collect() - // e.get(event_type.as_ref()).map(|s| s.iter().map(|e| e.clone()).collect::>()) - }) - .unwrap_or_default()) - } - - async fn get_state_events_by_type( + async fn get_state_events( &self, room_id: &RoomId, event_type: EventType, @@ -485,16 +473,12 @@ impl StateStore for MemoryStore { self.get_state_event(room_id, event_type, state_key).await } - async fn get_state_events(&self, room_id: &RoomId) -> Result>> { - self.get_state_events(room_id).await - } - - async fn get_state_events_by_type( + async fn get_state_events( &self, room_id: &RoomId, event_type: EventType, ) -> Result>> { - self.get_state_events_by_type(room_id, event_type).await + self.get_state_events(room_id, event_type).await } async fn get_profile( diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index d3828251..17c77c1a 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -133,21 +133,14 @@ pub trait StateStore: AsyncTraitDeps { state_key: &str, ) -> Result>>; - /// Get all synced state events for a room. - /// - /// # Arguments - /// - /// * `room_id` - The id of the room to get state events for. - async fn get_state_events(&self, room_id: &RoomId) -> Result>>; - - /// Get a list of state events for a given `EventType`. + /// Get a list of state events for a given room and `EventType`. /// /// # Arguments /// /// * `room_id` - The id of the room to find events for. /// - /// * `event_type` - The event type to find. - async fn get_state_events_by_type( + /// * `event_type` - The event type. + async fn get_state_events( &self, room_id: &RoomId, event_type: EventType, diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index 274ea72d..c99a4c4c 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -580,15 +580,7 @@ impl SledStore { .transpose()?) } - pub async fn get_state_events(&self, room_id: &RoomId) -> Result>> { - Ok(self - .room_state - .scan_prefix((room_id.as_str()).encode()) - .flat_map(|e| e.map(|(_, e)| self.deserialize_event(&e))) - .collect::>()?) - } - - pub async fn get_state_events_by_type( + pub async fn get_state_events( &self, room_id: &RoomId, event_type: EventType, @@ -821,16 +813,12 @@ impl StateStore for SledStore { self.get_state_event(room_id, event_type, state_key).await } - async fn get_state_events(&self, room_id: &RoomId) -> Result>> { - self.get_state_events(room_id).await - } - - async fn get_state_events_by_type( + async fn get_state_events( &self, room_id: &RoomId, event_type: EventType, ) -> Result>> { - self.get_state_events_by_type(room_id, event_type).await + self.get_state_events(room_id, event_type).await } async fn get_profile(