From a2ab6a9f23aa16277b392a8ef36f825d0344bbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 7 May 2021 19:11:27 +0200 Subject: [PATCH] base: Get the tags for a room --- matrix_sdk_base/src/rooms/normal.rs | 17 ++++++++++++++++- matrix_sdk_base/src/store/memory_store.rs | 19 +++++++++++++++++++ matrix_sdk_base/src/store/mod.rs | 14 ++++++++++++++ matrix_sdk_base/src/store/sled_store/mod.rs | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/matrix_sdk_base/src/rooms/normal.rs b/matrix_sdk_base/src/rooms/normal.rs index d9dc8862..2587645e 100644 --- a/matrix_sdk_base/src/rooms/normal.rs +++ b/matrix_sdk_base/src/rooms/normal.rs @@ -29,7 +29,8 @@ use matrix_sdk_common::{ guest_access::GuestAccess, history_visibility::HistoryVisibility, join_rules::JoinRule, tombstone::TombstoneEventContent, }, - AnyStateEventContent, AnySyncStateEvent, EventType, + tag::Tags, + AnyBasicEvent, AnyStateEventContent, AnySyncStateEvent, EventType, }, identifiers::{MxcUri, RoomAliasId, RoomId, UserId}, }; @@ -447,6 +448,20 @@ impl Room { display_name_ambiguous: ambiguous, })) } + + /// Get the `Tags` for this room. + pub async fn tags(&self) -> StoreResult> { + if let Some(AnyBasicEvent::Tag(event)) = self + .store + .get_room_account_data_event(self.room_id(), EventType::Tag) + .await? + .and_then(|r| r.deserialize().ok()) + { + Ok(Some(event.content.tags)) + } else { + Ok(None) + } + } } /// The underlying pure data structure for joined and left rooms. diff --git a/matrix_sdk_base/src/store/memory_store.rs b/matrix_sdk_base/src/store/memory_store.rs index b8182cc1..8088d272 100644 --- a/matrix_sdk_base/src/store/memory_store.rs +++ b/matrix_sdk_base/src/store/memory_store.rs @@ -314,6 +314,17 @@ impl MemoryStore { .get(event_type.as_ref()) .map(|e| e.clone())) } + + async fn get_room_account_data_event( + &self, + room_id: &RoomId, + event_type: EventType, + ) -> Result>> { + Ok(self + .room_account_data + .get(room_id) + .and_then(|m| m.get(event_type.as_ref()).map(|e| e.clone()))) + } } #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] @@ -403,4 +414,12 @@ impl StateStore for MemoryStore { ) -> Result>> { self.get_account_data_event(event_type).await } + + async fn get_room_account_data_event( + &self, + room_id: &RoomId, + event_type: EventType, + ) -> Result>> { + self.get_room_account_data_event(room_id, event_type).await + } } diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index 2e017d22..be7245b5 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -196,6 +196,20 @@ pub trait StateStore: AsyncTraitDeps { &self, event_type: EventType, ) -> Result>>; + + /// Get an event out of the room account data store. + /// + /// # Arguments + /// + /// * `room_id` - The id of the room for which the room account data event should + /// be fetched. + /// + /// * `event_type` - The event type of the room account data event. + async fn get_room_account_data_event( + &self, + room_id: &RoomId, + event_type: EventType, + ) -> Result>>; } /// A state store wrapper for the SDK. diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index e14c0a0b..315f2a35 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -597,6 +597,18 @@ impl SledStore { .map(|m| self.deserialize_event(&m)) .transpose()?) } + + pub async fn get_room_account_data_event( + &self, + room_id: &RoomId, + event_type: EventType, + ) -> Result>> { + Ok(self + .room_account_data + .get((room_id.as_str(), event_type.as_str()).encode())? + .map(|m| self.deserialize_event(&m)) + .transpose()?) + } } #[async_trait] @@ -681,6 +693,14 @@ impl StateStore for SledStore { ) -> Result>> { self.get_account_data_event(event_type).await } + + async fn get_room_account_data_event( + &self, + room_id: &RoomId, + event_type: EventType, + ) -> Result>> { + self.get_room_account_data_event(room_id, event_type).await + } } #[cfg(test)]