base: Get the tags for a room

master
Kévin Commaille 2021-05-07 19:11:27 +02:00
parent 1bda3659ce
commit a2ab6a9f23
No known key found for this signature in database
GPG Key ID: 296D60AE1E61661C
4 changed files with 69 additions and 1 deletions

View File

@ -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<Option<Tags>> {
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.

View File

@ -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<Option<Raw<AnyBasicEvent>>> {
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<Option<Raw<AnyBasicEvent>>> {
self.get_account_data_event(event_type).await
}
async fn get_room_account_data_event(
&self,
room_id: &RoomId,
event_type: EventType,
) -> Result<Option<Raw<AnyBasicEvent>>> {
self.get_room_account_data_event(room_id, event_type).await
}
}

View File

@ -196,6 +196,20 @@ pub trait StateStore: AsyncTraitDeps {
&self,
event_type: EventType,
) -> Result<Option<Raw<AnyBasicEvent>>>;
/// 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<Option<Raw<AnyBasicEvent>>>;
}
/// A state store wrapper for the SDK.

View File

@ -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<Option<Raw<AnyBasicEvent>>> {
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<Option<Raw<AnyBasicEvent>>> {
self.get_account_data_event(event_type).await
}
async fn get_room_account_data_event(
&self,
room_id: &RoomId,
event_type: EventType,
) -> Result<Option<Raw<AnyBasicEvent>>> {
self.get_room_account_data_event(room_id, event_type).await
}
}
#[cfg(test)]