From 4b4c0952a24e84f5416c9c3fed14f37c5a446b3e Mon Sep 17 00:00:00 2001
From: strawberry <strawberry@puppygock.gay>
Date: Sat, 8 Jun 2024 20:45:17 -0400
Subject: [PATCH] refactor `guest_can_join` into 1 single function

Signed-off-by: strawberry <strawberry@puppygock.gay>
---
 src/api/client/directory.rs             |  8 +-------
 src/service/rooms/spaces/mod.rs         | 16 +---------------
 src/service/rooms/state_accessor/mod.rs | 15 ++++++++++++---
 3 files changed, 14 insertions(+), 25 deletions(-)

diff --git a/src/api/client/directory.rs b/src/api/client/directory.rs
index 0e5b8afa9..aee4341ff 100644
--- a/src/api/client/directory.rs
+++ b/src/api/client/directory.rs
@@ -13,7 +13,6 @@
 			avatar::RoomAvatarEventContent,
 			canonical_alias::RoomCanonicalAliasEventContent,
 			create::RoomCreateEventContent,
-			guest_access::{GuestAccess, RoomGuestAccessEventContent},
 			join_rules::{JoinRule, RoomJoinRulesEventContent},
 			topic::RoomTopicEventContent,
 		},
@@ -264,12 +263,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
 				guest_can_join: 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."))
-					})?,
+					.guest_can_join(&room_id)?,
 				avatar_url: services()
 					.rooms
 					.state_accessor
diff --git a/src/service/rooms/spaces/mod.rs b/src/service/rooms/spaces/mod.rs
index 6fcaccb9c..9a5dc9d07 100644
--- a/src/service/rooms/spaces/mod.rs
+++ b/src/service/rooms/spaces/mod.rs
@@ -17,7 +17,6 @@
 			avatar::RoomAvatarEventContent,
 			canonical_alias::RoomCanonicalAliasEventContent,
 			create::RoomCreateEventContent,
-			guest_access::{GuestAccess, RoomGuestAccessEventContent},
 			join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
 			topic::RoomTopicEventContent,
 		},
@@ -591,7 +590,7 @@ fn get_room_summary(
 				})
 				.unwrap_or(None),
 			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()
                 .rooms
                 .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
 fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
 	Ok(services()
diff --git a/src/service/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs
index 0681c298f..32ed66ee3 100644
--- a/src/service/rooms/state_accessor/mod.rs
+++ b/src/service/rooms/state_accessor/mod.rs
@@ -10,6 +10,7 @@
 	events::{
 		room::{
 			avatar::RoomAvatarEventContent,
+			guest_access::{GuestAccess, RoomGuestAccessEventContent},
 			history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
 			member::{MembershipState, RoomMemberEventContent},
 			name::RoomNameEventContent,
@@ -304,8 +305,7 @@ pub async fn user_can_invite(
 
 	/// 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, "")?
+		self.room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")?
 			.map_or(Ok(false), |s| {
 				serde_json::from_str(s.content.get())
 					.map(|c: RoomHistoryVisibilityEventContent| {
@@ -319,6 +319,15 @@ pub fn is_world_readable(&self, room_id: &RoomId) -> Result<bool, Error> {
 						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."))
+			})
 	}
 }
-- 
GitLab