dedupe some state accessors in various places

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-07-03 12:53:51 -04:00
parent b0eec60724
commit 0832c28a40
4 changed files with 30 additions and 74 deletions

View file

@ -10,11 +10,7 @@ use ruma::{
},
directory::{Filter, PublicRoomJoinRule, PublicRoomsChunk, RoomNetwork},
events::{
room::{
avatar::RoomAvatarEventContent,
create::RoomCreateEventContent,
join_rules::{JoinRule, RoomJoinRulesEventContent},
},
room::join_rules::{JoinRule, RoomJoinRulesEventContent},
StateEventType,
},
uint, ServerName, UInt,
@ -256,15 +252,10 @@ pub(crate) async fn get_public_rooms_filtered_helper(
avatar_url: services()
.rooms
.state_accessor
.room_state_get(&room_id, &StateEventType::RoomAvatar, "")?
.map(|s| {
serde_json::from_str(s.content.get())
.map(|c: RoomAvatarEventContent| c.url)
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))
})
.transpose()?
// url is now an Option<String> so we must flatten
.flatten(),
.get_avatar(&room_id)?
.into_option()
.unwrap_or_default()
.url,
join_rule: services()
.rooms
.state_accessor
@ -287,15 +278,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
room_type: services()
.rooms
.state_accessor
.room_state_get(&room_id, &StateEventType::RoomCreate, "")?
.map(|s| {
serde_json::from_str::<RoomCreateEventContent>(s.content.get()).map_err(|e| {
error!("Invalid room create event in database: {}", e);
Error::BadDatabase("Invalid room create event in database.")
})
})
.transpose()?
.and_then(|e| e.room_type),
.get_room_type(&room_id)?,
room_id,
};
Ok(chunk)

View file

@ -106,7 +106,8 @@ pub(crate) async fn get_room_summary(
canonical_alias: services()
.rooms
.state_accessor
.get_canonical_alias(&room_id)?,
.get_canonical_alias(&room_id)
.unwrap_or(None),
avatar_url: services()
.rooms
.state_accessor

View file

@ -19,13 +19,7 @@ use ruma::{
},
},
events::{
room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
join_rules::{JoinRule, RoomJoinRulesEventContent},
topic::RoomTopicEventContent,
},
room::join_rules::{JoinRule, RoomJoinRulesEventContent},
space::child::{HierarchySpaceChildEvent, SpaceChildEventContent},
StateEventType,
},
@ -409,19 +403,20 @@ impl Service {
canonical_alias: services()
.rooms
.state_accessor
.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."))
})?,
name: services().rooms.state_accessor.get_name(room_id)?,
.get_canonical_alias(room_id)
.unwrap_or(None),
name: services()
.rooms
.state_accessor
.get_name(room_id)
.unwrap_or(None),
num_joined_members: services()
.rooms
.state_cache
.room_joined_count(room_id)?
.room_joined_count(room_id)
.unwrap_or_default()
.unwrap_or_else(|| {
warn!("Room {} has no member count", room_id);
warn!("Room {room_id} has no member count");
0
})
.try_into()
@ -430,42 +425,19 @@ 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)?,
avatar_url: services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomAvatar, "")?
.map(|s| {
serde_json::from_str(s.content.get())
.map(|c: RoomAvatarEventContent| c.url)
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))
})
.transpose()?
// url is now an Option<String> so we must flatten
.flatten(),
.get_avatar(room_id)?
.into_option()
.unwrap_or_default()
.url,
join_rule,
room_type: services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomCreate, "")?
.map(|s| {
serde_json::from_str::<RoomCreateEventContent>(s.content.get()).map_err(|e| {
error!("Invalid room create event in database: {}", e);
Error::BadDatabase("Invalid room create event in database.")
})
})
.transpose()?
.and_then(|e| e.room_type),
room_type: services().rooms.state_accessor.get_room_type(room_id)?,
children_state,
allowed_room_ids,
})

View file

@ -422,10 +422,10 @@ impl Service {
)
}
/// Returns the join rule for a given room
pub fn get_join_rule(&self, current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
/// Returns the join rule (`SpaceRoomJoinRule`) for a given room
pub fn get_join_rule(&self, room_id: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
Ok(self
.room_state_get(current_room, &StateEventType::RoomJoinRules, "")?
.room_state_get(room_id, &StateEventType::RoomJoinRules, "")?
.map(|s| {
serde_json::from_str(s.content.get())
.map(|c: RoomJoinRulesEventContent| {