refactor getting room's canonical alias into 1 function

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-06-08 20:52:41 -04:00
parent 4b4c0952a2
commit 88d038ffec
3 changed files with 14 additions and 15 deletions

View file

@ -11,7 +11,6 @@ use ruma::{
events::{ events::{
room::{ room::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent, create::RoomCreateEventContent,
join_rules::{JoinRule, RoomJoinRulesEventContent}, join_rules::{JoinRule, RoomJoinRulesEventContent},
topic::RoomTopicEventContent, topic::RoomTopicEventContent,
@ -229,12 +228,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
canonical_alias: services() canonical_alias: services()
.rooms .rooms
.state_accessor .state_accessor
.room_state_get(&room_id, &StateEventType::RoomCanonicalAlias, "")? .get_canonical_alias(&room_id)?,
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomCanonicalAliasEventContent| c.alias)
.map_err(|_| Error::bad_database("Invalid canonical alias event in database."))
})?,
name: services().rooms.state_accessor.get_name(&room_id)?, name: services().rooms.state_accessor.get_name(&room_id)?,
num_joined_members: services() num_joined_members: services()
.rooms .rooms

View file

@ -15,7 +15,6 @@ use ruma::{
events::{ events::{
room::{ room::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent, create::RoomCreateEventContent,
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership}, join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
topic::RoomTopicEventContent, topic::RoomTopicEventContent,
@ -558,12 +557,7 @@ impl Service {
canonical_alias: services() canonical_alias: services()
.rooms .rooms
.state_accessor .state_accessor
.room_state_get(room_id, &StateEventType::RoomCanonicalAlias, "")? .get_canonical_alias(room_id)?,
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomCanonicalAliasEventContent| c.alias)
.map_err(|_| Error::bad_database("Invalid canonical alias event in database."))
})?,
name: services().rooms.state_accessor.get_name(room_id)?, name: services().rooms.state_accessor.get_name(room_id)?,
num_joined_members: services() num_joined_members: services()
.rooms .rooms

View file

@ -10,6 +10,7 @@ use ruma::{
events::{ events::{
room::{ room::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent}, guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
member::{MembershipState, RoomMemberEventContent}, member::{MembershipState, RoomMemberEventContent},
@ -17,7 +18,7 @@ use ruma::{
}, },
StateEventType, StateEventType,
}, },
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId, EventId, OwnedRoomAliasId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
}; };
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tokio::sync::MutexGuard; use tokio::sync::MutexGuard;
@ -330,4 +331,14 @@ impl Service {
.map_err(|_| Error::bad_database("Invalid room guest access event in database.")) .map_err(|_| Error::bad_database("Invalid room guest access event in database."))
}) })
} }
/// Gets the primary alias from canonical alias event
pub fn get_canonical_alias(&self, room_id: &RoomId) -> Result<Option<OwnedRoomAliasId>, Error> {
self.room_state_get(room_id, &StateEventType::RoomCanonicalAlias, "")?
.map_or(Ok(None), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomCanonicalAliasEventContent| c.alias)
.map_err(|_| Error::bad_database("Invalid canonical alias event in database."))
})
}
} }