refactor guest_can_join into 1 single function

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-06-08 20:45:17 -04:00
parent 016270b33b
commit 4b4c0952a2
3 changed files with 14 additions and 25 deletions

View file

@ -13,7 +13,6 @@ use ruma::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent, canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent, create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent}, join_rules::{JoinRule, RoomJoinRulesEventContent},
topic::RoomTopicEventContent, topic::RoomTopicEventContent,
}, },
@ -264,12 +263,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
guest_can_join: services() guest_can_join: services()
.rooms .rooms
.state_accessor .state_accessor
.room_state_get(&room_id, &StateEventType::RoomGuestAccess, "")? .guest_can_join(&room_id)?,
.map_or(Ok(false), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomGuestAccessEventContent| c.guest_access == GuestAccess::CanJoin)
.map_err(|_| Error::bad_database("Invalid room guest access event in database."))
})?,
avatar_url: services() avatar_url: services()
.rooms .rooms
.state_accessor .state_accessor

View file

@ -17,7 +17,6 @@ use ruma::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent, canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent, create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership}, join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
topic::RoomTopicEventContent, topic::RoomTopicEventContent,
}, },
@ -591,7 +590,7 @@ impl Service {
}) })
.unwrap_or(None), .unwrap_or(None),
world_readable: services().rooms.state_accessor.is_world_readable(room_id)?, world_readable: services().rooms.state_accessor.is_world_readable(room_id)?,
guest_can_join: guest_can_join(room_id)?, guest_can_join: services().rooms.state_accessor.guest_can_join(room_id)?,
avatar_url: services() avatar_url: services()
.rooms .rooms
.state_accessor .state_accessor
@ -847,19 +846,6 @@ fn is_accessable_child_recurse(
} }
} }
/// Checks if guests are able to join a given room
fn guest_can_join(room_id: &RoomId) -> Result<bool, Error> {
services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomGuestAccess, "")?
.map_or(Ok(false), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomGuestAccessEventContent| c.guest_access == GuestAccess::CanJoin)
.map_err(|_| Error::bad_database("Invalid room guest access event in database."))
})
}
/// Returns the join rule for a given room /// Returns the join rule for a given room
fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> { fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
Ok(services() Ok(services()

View file

@ -10,6 +10,7 @@ use ruma::{
events::{ events::{
room::{ room::{
avatar::RoomAvatarEventContent, avatar::RoomAvatarEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
member::{MembershipState, RoomMemberEventContent}, member::{MembershipState, RoomMemberEventContent},
name::RoomNameEventContent, name::RoomNameEventContent,
@ -304,8 +305,7 @@ impl Service {
/// Checks if guests are able to view room content without joining /// Checks if guests are able to view room content without joining
pub fn is_world_readable(&self, room_id: &RoomId) -> Result<bool, Error> { pub fn is_world_readable(&self, room_id: &RoomId) -> Result<bool, Error> {
Ok(self self.room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")?
.room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")?
.map_or(Ok(false), |s| { .map_or(Ok(false), |s| {
serde_json::from_str(s.content.get()) serde_json::from_str(s.content.get())
.map(|c: RoomHistoryVisibilityEventContent| { .map(|c: RoomHistoryVisibilityEventContent| {
@ -319,6 +319,15 @@ impl Service {
Error::bad_database("Invalid room history visibility event in database.") Error::bad_database("Invalid room history visibility event in database.")
}) })
}) })
.unwrap_or(false)) }
/// Checks if guests are able to join a given room
pub fn guest_can_join(&self, room_id: &RoomId) -> Result<bool, Error> {
self.room_state_get(room_id, &StateEventType::RoomGuestAccess, "")?
.map_or(Ok(false), |s| {
serde_json::from_str(s.content.get())
.map(|c: RoomGuestAccessEventContent| c.guest_access == GuestAccess::CanJoin)
.map_err(|_| Error::bad_database("Invalid room guest access event in database."))
})
} }
} }