diff --git a/src/api/client/directory.rs b/src/api/client/directory.rs index 7cfb3392..0e5b8afa 100644 --- a/src/api/client/directory.rs +++ b/src/api/client/directory.rs @@ -14,7 +14,6 @@ use ruma::{ canonical_alias::RoomCanonicalAliasEventContent, create::RoomCreateEventContent, guest_access::{GuestAccess, RoomGuestAccessEventContent}, - history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, join_rules::{JoinRule, RoomJoinRulesEventContent}, topic::RoomTopicEventContent, }, @@ -261,21 +260,7 @@ pub(crate) async fn get_public_rooms_filtered_helper( }) }) .unwrap_or(None), - world_readable: services() - .rooms - .state_accessor - .room_state_get(&room_id, &StateEventType::RoomHistoryVisibility, "")? - .map_or(Ok(false), |s| { - serde_json::from_str(s.content.get()) - .map(|c: RoomHistoryVisibilityEventContent| { - c.history_visibility == HistoryVisibility::WorldReadable - }) - .map_err(|e| { - error!( - "Invalid room history visibility event in database for room {room_id}, assuming is \"shared\": {e}", - ); - Error::bad_database("Invalid room history visibility event in database.") - })}).unwrap_or(false), + world_readable: services().rooms.state_accessor.is_world_readable(&room_id)?, guest_can_join: services() .rooms .state_accessor diff --git a/src/service/rooms/spaces/mod.rs b/src/service/rooms/spaces/mod.rs index 219b8c39..72ebaf4a 100644 --- a/src/service/rooms/spaces/mod.rs +++ b/src/service/rooms/spaces/mod.rs @@ -18,7 +18,6 @@ use ruma::{ canonical_alias::RoomCanonicalAliasEventContent, create::RoomCreateEventContent, guest_access::{GuestAccess, RoomGuestAccessEventContent}, - history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership}, topic::RoomTopicEventContent, }, @@ -591,7 +590,7 @@ impl Service { }) }) .unwrap_or(None), - world_readable: world_readable(room_id)?, + world_readable: services().rooms.state_accessor.is_world_readable(room_id)?, guest_can_join: guest_can_join(room_id)?, avatar_url: services() .rooms @@ -858,26 +857,6 @@ fn guest_can_join(room_id: &RoomId) -> Result { }) } -/// Checks if guests are able to view room content without joining -fn world_readable(room_id: &RoomId) -> Result { - Ok(services() - .rooms - .state_accessor - .room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")? - .map_or(Ok(false), |s| { - serde_json::from_str(s.content.get()) - .map(|c: RoomHistoryVisibilityEventContent| c.history_visibility == HistoryVisibility::WorldReadable) - .map_err(|e| { - error!( - "Invalid room history visibility event in database for room {room_id}, assuming is \ - \"shared\": {e} " - ); - Error::bad_database("Invalid room history visibility event in database.") - }) - }) - .unwrap_or(false)) -} - /// Returns the join rule for a given room fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec), Error> { Ok(services() diff --git a/src/service/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs index a05ba719..0681c298 100644 --- a/src/service/rooms/state_accessor/mod.rs +++ b/src/service/rooms/state_accessor/mod.rs @@ -259,20 +259,14 @@ impl Service { } pub fn get_name(&self, room_id: &RoomId) -> Result> { - services() - .rooms - .state_accessor - .room_state_get(room_id, &StateEventType::RoomName, "")? + self.room_state_get(room_id, &StateEventType::RoomName, "")? .map_or(Ok(None), |s| { Ok(serde_json::from_str(s.content.get()).map_or_else(|_| None, |c: RoomNameEventContent| Some(c.name))) }) } pub fn get_avatar(&self, room_id: &RoomId) -> Result> { - services() - .rooms - .state_accessor - .room_state_get(room_id, &StateEventType::RoomAvatar, "")? + self.room_state_get(room_id, &StateEventType::RoomAvatar, "")? .map_or(Ok(ruma::JsOption::Undefined), |s| { serde_json::from_str(s.content.get()) .map_err(|_| Error::bad_database("Invalid room avatar event in database.")) @@ -280,10 +274,7 @@ impl Service { } pub fn get_member(&self, room_id: &RoomId, user_id: &UserId) -> Result> { - services() - .rooms - .state_accessor - .room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())? + self.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())? .map_or(Ok(None), |s| { serde_json::from_str(s.content.get()) .map_err(|_| Error::bad_database("Invalid room member event in database.")) @@ -310,4 +301,24 @@ impl Service { .create_hash_and_sign_event(new_event, sender, room_id, state_lock) .is_ok()) } + + /// Checks if guests are able to view room content without joining + pub fn is_world_readable(&self, room_id: &RoomId) -> Result { + Ok(self + .room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")? + .map_or(Ok(false), |s| { + serde_json::from_str(s.content.get()) + .map(|c: RoomHistoryVisibilityEventContent| { + c.history_visibility == HistoryVisibility::WorldReadable + }) + .map_err(|e| { + error!( + "Invalid room history visibility event in database for room {room_id}, assuming not world \ + readable: {e} " + ); + Error::bad_database("Invalid room history visibility event in database.") + }) + }) + .unwrap_or(false)) + } }