From fcdf1463ef4e3c8da70b88d6e8bf36bc8e6e10cf Mon Sep 17 00:00:00 2001 From: strawberry Date: Sat, 8 Jun 2024 20:56:51 -0400 Subject: [PATCH] refactor `get_room_topic` into 1 single function Signed-off-by: strawberry --- src/api/client/directory.rs | 11 +---------- src/service/rooms/spaces/mod.rs | 11 +---------- src/service/rooms/state_accessor/mod.rs | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/api/client/directory.rs b/src/api/client/directory.rs index 9ce4ffd7..ef475a68 100644 --- a/src/api/client/directory.rs +++ b/src/api/client/directory.rs @@ -13,7 +13,6 @@ use ruma::{ avatar::RoomAvatarEventContent, create::RoomCreateEventContent, join_rules::{JoinRule, RoomJoinRulesEventContent}, - topic::RoomTopicEventContent, }, StateEventType, }, @@ -243,15 +242,7 @@ pub(crate) async fn get_public_rooms_filtered_helper( topic: services() .rooms .state_accessor - .room_state_get(&room_id, &StateEventType::RoomTopic, "")? - .map_or(Ok(None), |s| { - serde_json::from_str(s.content.get()) - .map(|c: RoomTopicEventContent| Some(c.topic)) - .map_err(|e| { - error!("Invalid room topic event in database for room {room_id}: {e}"); - Error::bad_database("Invalid room topic event in database.") - }) - }) + .get_room_topic(&room_id) .unwrap_or(None), world_readable: services().rooms.state_accessor.is_world_readable(&room_id)?, guest_can_join: services() diff --git a/src/service/rooms/spaces/mod.rs b/src/service/rooms/spaces/mod.rs index c96ab7a0..ffe1935d 100644 --- a/src/service/rooms/spaces/mod.rs +++ b/src/service/rooms/spaces/mod.rs @@ -17,7 +17,6 @@ use ruma::{ avatar::RoomAvatarEventContent, create::RoomCreateEventContent, join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership}, - topic::RoomTopicEventContent, }, space::child::{HierarchySpaceChildEvent, SpaceChildEventContent}, StateEventType, @@ -573,15 +572,7 @@ impl Service { topic: services() .rooms .state_accessor - .room_state_get(room_id, &StateEventType::RoomTopic, "")? - .map_or(Ok(None), |s| { - serde_json::from_str(s.content.get()) - .map(|c: RoomTopicEventContent| Some(c.topic)) - .map_err(|_| { - error!("Invalid room topic event in database for room {}", room_id); - Error::bad_database("Invalid room topic event in database.") - }) - }) + .get_room_topic(room_id) .unwrap_or(None), world_readable: services().rooms.state_accessor.is_world_readable(room_id)?, guest_can_join: services().rooms.state_accessor.guest_can_join(room_id)?, diff --git a/src/service/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs index 62c8c224..d3dc92ef 100644 --- a/src/service/rooms/state_accessor/mod.rs +++ b/src/service/rooms/state_accessor/mod.rs @@ -15,6 +15,7 @@ use ruma::{ history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, member::{MembershipState, RoomMemberEventContent}, name::RoomNameEventContent, + topic::RoomTopicEventContent, }, StateEventType, }, @@ -341,4 +342,17 @@ impl Service { .map_err(|_| Error::bad_database("Invalid canonical alias event in database.")) }) } + + /// Gets the room topic + pub fn get_room_topic(&self, room_id: &RoomId) -> Result, Error> { + self.room_state_get(room_id, &StateEventType::RoomTopic, "")? + .map_or(Ok(None), |s| { + serde_json::from_str(s.content.get()) + .map(|c: RoomTopicEventContent| Some(c.topic)) + .map_err(|e| { + error!("Invalid room topic event in database for room {room_id}: {e}"); + Error::bad_database("Invalid room topic event in database.") + }) + }) + } }