add `is_world_readable` state_accessor func, use `self` instead of `services()`

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-06-07 01:16:18 -04:00
parent 1cc7cf54a7
commit bfbb29dded
3 changed files with 25 additions and 50 deletions

View File

@ -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

View File

@ -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<bool, Error> {
})
}
/// Checks if guests are able to view room content without joining
fn world_readable(room_id: &RoomId) -> Result<bool, Error> {
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<OwnedRoomId>), Error> {
Ok(services()

View File

@ -259,20 +259,14 @@ impl Service {
}
pub fn get_name(&self, room_id: &RoomId) -> Result<Option<String>> {
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<ruma::JsOption<RoomAvatarEventContent>> {
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<Option<RoomMemberEventContent>> {
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<bool, Error> {
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))
}
}