simplify space join rules related

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-04-04 01:05:43 +00:00
parent d6cc447add
commit 500faa8d7f
5 changed files with 113 additions and 128 deletions

22
Cargo.lock generated
View file

@ -3654,7 +3654,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.10.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"assign",
"js_int",
@ -3674,7 +3674,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.10.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"js_int",
"ruma-common",
@ -3686,7 +3686,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.18.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"as_variant",
"assign",
@ -3709,7 +3709,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"as_variant",
"base64 0.22.1",
@ -3741,7 +3741,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.28.1"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"as_variant",
"indexmap 2.8.0",
@ -3766,7 +3766,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"bytes",
"headers",
@ -3788,7 +3788,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.9.5"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"js_int",
"thiserror 2.0.12",
@ -3797,7 +3797,7 @@ dependencies = [
[[package]]
name = "ruma-identity-service-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"js_int",
"ruma-common",
@ -3807,7 +3807,7 @@ dependencies = [
[[package]]
name = "ruma-macros"
version = "0.13.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"cfg-if",
"proc-macro-crate",
@ -3822,7 +3822,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.9.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"js_int",
"ruma-common",
@ -3834,7 +3834,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.15.0"
source = "git+https://github.com/girlbossceo/ruwuma?rev=edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef#edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
source = "git+https://github.com/girlbossceo/ruwuma?rev=920148dca1076454ca0ca5d43b5ce1aa708381d4#920148dca1076454ca0ca5d43b5ce1aa708381d4"
dependencies = [
"base64 0.22.1",
"ed25519-dalek",

View file

@ -350,7 +350,7 @@ version = "0.1.2"
[workspace.dependencies.ruma]
git = "https://github.com/girlbossceo/ruwuma"
#branch = "conduwuit-changes"
rev = "edbdc79e560d01d9e4a76f7421e70ea4fd4c54ef"
rev = "920148dca1076454ca0ca5d43b5ce1aa708381d4"
features = [
"compat",
"rand",

View file

@ -4,9 +4,13 @@ use conduwuit::{
Err, Result, debug_warn,
utils::{IterStream, future::TryExtExt},
};
use futures::{FutureExt, StreamExt, future::join3, stream::FuturesUnordered};
use futures::{
FutureExt, StreamExt,
future::{OptionFuture, join3},
stream::FuturesUnordered,
};
use ruma::{
OwnedRoomId, OwnedServerName, RoomId, UserId,
OwnedServerName, RoomId, UserId,
api::{
client::room::get_summary,
federation::space::{SpaceHierarchyParentSummary, get_hierarchy},
@ -91,13 +95,9 @@ async fn room_summary_response(
join_rule: room.join_rule,
room_type: room.room_type,
room_version: room.room_version,
membership: if sender_user.is_none() {
None
} else {
Some(MembershipState::Leave)
},
encryption: room.encryption,
allowed_room_ids: room.allowed_room_ids,
membership: sender_user.is_some().then_some(MembershipState::Leave),
})
}
@ -106,20 +106,22 @@ async fn local_room_summary_response(
room_id: &RoomId,
sender_user: Option<&UserId>,
) -> Result<get_summary::msc3266::Response> {
let join_rule = services.rooms.state_accessor.get_space_join_rule(room_id);
let join_rule = services.rooms.state_accessor.get_join_rules(room_id);
let world_readable = services.rooms.state_accessor.is_world_readable(room_id);
let guest_can_join = services.rooms.state_accessor.guest_can_join(room_id);
let ((join_rule, allowed_room_ids), world_readable, guest_can_join) =
let (join_rule, world_readable, guest_can_join) =
join3(join_rule, world_readable, guest_can_join).await;
user_can_see_summary(
services,
room_id,
&join_rule,
&join_rule.clone().into(),
guest_can_join,
world_readable,
&allowed_room_ids,
join_rule.allowed_rooms(),
sender_user,
)
.await?;
@ -129,26 +131,43 @@ async fn local_room_summary_response(
.state_accessor
.get_canonical_alias(room_id)
.ok();
let name = services.rooms.state_accessor.get_name(room_id).ok();
let topic = services.rooms.state_accessor.get_room_topic(room_id).ok();
let room_type = services.rooms.state_accessor.get_room_type(room_id).ok();
let avatar_url = services
.rooms
.state_accessor
.get_avatar(room_id)
.map(|res| res.into_option().unwrap_or_default().url);
let room_version = services.rooms.state.get_room_version(room_id).ok();
let encryption = services
.rooms
.state_accessor
.get_room_encryption(room_id)
.ok();
let num_joined_members = services
.rooms
.state_cache
.room_joined_count(room_id)
.unwrap_or(0);
let membership: OptionFuture<_> = sender_user
.map(|sender_user| {
services
.rooms
.state_accessor
.get_member(room_id, sender_user)
.map_ok_or(MembershipState::Leave, |content| content.membership)
})
.into();
let (
canonical_alias,
name,
@ -158,6 +177,7 @@ async fn local_room_summary_response(
room_type,
room_version,
encryption,
membership,
) = futures::join!(
canonical_alias,
name,
@ -167,6 +187,7 @@ async fn local_room_summary_response(
room_type,
room_version,
encryption,
membership,
);
Ok(get_summary::msc3266::Response {
@ -178,21 +199,12 @@ async fn local_room_summary_response(
num_joined_members: num_joined_members.try_into().unwrap_or_default(),
topic,
world_readable,
join_rule,
room_type,
room_version,
membership: if let Some(sender_user) = sender_user {
services
.rooms
.state_accessor
.get_member(room_id, sender_user)
.await
.map_or(Some(MembershipState::Leave), |content| Some(content.membership))
} else {
None
},
encryption,
allowed_room_ids,
membership,
allowed_room_ids: join_rule.allowed_rooms().map(Into::into).collect(),
join_rule: join_rule.into(),
})
}
@ -241,7 +253,7 @@ async fn remote_room_summary_hierarchy_response(
&room.join_rule,
room.guest_can_join,
room.world_readable,
&room.allowed_room_ids,
room.allowed_room_ids.iter().map(AsRef::as_ref),
sender_user,
)
.await
@ -254,15 +266,18 @@ async fn remote_room_summary_hierarchy_response(
)))
}
async fn user_can_see_summary(
async fn user_can_see_summary<'a, I>(
services: &Services,
room_id: &RoomId,
join_rule: &SpaceRoomJoinRule,
guest_can_join: bool,
world_readable: bool,
allowed_room_ids: &[OwnedRoomId],
allowed_room_ids: I,
sender_user: Option<&UserId>,
) -> Result {
) -> Result
where
I: Iterator<Item = &'a RoomId> + Send,
{
match sender_user {
| Some(sender_user) => {
let user_can_see_state_events = services
@ -271,7 +286,6 @@ async fn user_can_see_summary(
.user_can_see_state_events(sender_user, room_id);
let is_guest = services.users.is_deactivated(sender_user).unwrap_or(false);
let user_in_allowed_restricted_room = allowed_room_ids
.iter()
.stream()
.any(|room| services.rooms.state_cache.is_joined(sender_user, room));

View file

@ -121,21 +121,22 @@ pub async fn get_summary_and_children_local(
| None => (), // cache miss
| Some(None) => return Ok(None),
| Some(Some(cached)) => {
return Ok(Some(
if self
.is_accessible_child(
let allowed_rooms = cached.summary.allowed_room_ids.iter().map(AsRef::as_ref);
let is_accessible_child = self.is_accessible_child(
current_room,
&cached.summary.join_rule,
identifier,
&cached.summary.allowed_room_ids,
)
.await
{
allowed_rooms,
);
let accessibility = if is_accessible_child.await {
SummaryAccessibility::Accessible(cached.summary.clone())
} else {
SummaryAccessibility::Inaccessible
},
));
};
return Ok(Some(accessibility));
},
}
@ -145,12 +146,11 @@ pub async fn get_summary_and_children_local(
.collect()
.await;
let summary = self
let Ok(summary) = self
.get_room_summary(current_room, children_pdus, identifier)
.boxed()
.await;
let Ok(summary) = summary else {
.await
else {
return Ok(None);
};
@ -217,20 +217,19 @@ async fn get_summary_and_children_federation(
.await;
let identifier = Identifier::UserId(user_id);
let allowed_room_ids = summary.allowed_room_ids.iter().map(AsRef::as_ref);
let is_accessible_child = self
.is_accessible_child(
current_room,
&summary.join_rule,
&identifier,
&summary.allowed_room_ids,
)
.is_accessible_child(current_room, &summary.join_rule, &identifier, allowed_room_ids)
.await;
if is_accessible_child {
return Ok(Some(SummaryAccessibility::Accessible(summary)));
}
let accessibility = if is_accessible_child {
SummaryAccessibility::Accessible(summary)
} else {
SummaryAccessibility::Inaccessible
};
Ok(Some(SummaryAccessibility::Inaccessible))
Ok(Some(accessibility))
}
/// Simply returns the stripped m.space.child events of a room
@ -305,14 +304,15 @@ async fn get_room_summary(
children_state: Vec<Raw<HierarchySpaceChildEvent>>,
identifier: &Identifier<'_>,
) -> Result<SpaceHierarchyParentSummary, Error> {
let (join_rule, allowed_room_ids) = self
.services
.state_accessor
.get_space_join_rule(room_id)
.await;
let join_rule = self.services.state_accessor.get_join_rules(room_id).await;
let is_accessible_child = self
.is_accessible_child(room_id, &join_rule, identifier, &allowed_room_ids)
.is_accessible_child(
room_id,
&join_rule.clone().into(),
identifier,
join_rule.allowed_rooms(),
)
.await;
if !is_accessible_child {
@ -379,7 +379,7 @@ async fn get_room_summary(
encryption,
);
Ok(SpaceHierarchyParentSummary {
let summary = SpaceHierarchyParentSummary {
canonical_alias,
name,
topic,
@ -388,24 +388,29 @@ async fn get_room_summary(
avatar_url,
room_type,
children_state,
allowed_room_ids,
join_rule,
room_id: room_id.to_owned(),
num_joined_members: num_joined_members.try_into().unwrap_or_default(),
encryption,
room_version,
})
room_id: room_id.to_owned(),
num_joined_members: num_joined_members.try_into().unwrap_or_default(),
allowed_room_ids: join_rule.allowed_rooms().map(Into::into).collect(),
join_rule: join_rule.clone().into(),
};
Ok(summary)
}
/// With the given identifier, checks if a room is accessable
#[implement(Service)]
async fn is_accessible_child(
async fn is_accessible_child<'a, I>(
&self,
current_room: &RoomId,
join_rule: &SpaceRoomJoinRule,
identifier: &Identifier<'_>,
allowed_room_ids: &[OwnedRoomId],
) -> bool {
allowed_rooms: I,
) -> bool
where
I: Iterator<Item = &'a RoomId> + Send,
{
if let Identifier::ServerName(server_name) = identifier {
// Checks if ACLs allow for the server to participate
if self
@ -430,21 +435,18 @@ async fn is_accessible_child(
}
}
match join_rule {
match *join_rule {
| SpaceRoomJoinRule::Public
| SpaceRoomJoinRule::Knock
| SpaceRoomJoinRule::KnockRestricted => true,
| SpaceRoomJoinRule::Restricted =>
allowed_room_ids
.iter()
allowed_rooms
.stream()
.any(|room| async {
match identifier {
.any(async |room| match identifier {
| Identifier::UserId(user) =>
self.services.state_cache.is_joined(user, room).await,
| Identifier::ServerName(server) =>
self.services.state_cache.server_in_room(server, room).await,
}
})
.await,

View file

@ -9,7 +9,7 @@ use async_trait::async_trait;
use conduwuit::{Result, err};
use database::Map;
use ruma::{
EventEncryptionAlgorithm, JsOption, OwnedRoomAliasId, OwnedRoomId, RoomId, UserId,
EventEncryptionAlgorithm, JsOption, OwnedRoomAliasId, RoomId, UserId,
events::{
StateEventType,
room::{
@ -19,14 +19,13 @@ use ruma::{
encryption::RoomEncryptionEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::RoomMemberEventContent,
name::RoomNameEventContent,
topic::RoomTopicEventContent,
},
},
room::RoomType,
space::SpaceRoomJoinRule,
};
use crate::{Dep, rooms};
@ -129,42 +128,12 @@ impl Service {
.map(|c: RoomTopicEventContent| c.topic)
}
/// Returns the space join rule (`SpaceRoomJoinRule`) for a given room and
/// any allowed room IDs if available. Will default to Invite and empty vec
/// if doesnt exist or invalid,
pub async fn get_space_join_rule(
&self,
room_id: &RoomId,
) -> (SpaceRoomJoinRule, Vec<OwnedRoomId>) {
self.room_state_get_content(room_id, &StateEventType::RoomJoinRules, "")
.await
.map_or_else(
|_| (SpaceRoomJoinRule::Invite, vec![]),
|c: RoomJoinRulesEventContent| {
(c.join_rule.clone().into(), self.allowed_room_ids(c.join_rule))
},
)
}
/// Returns the join rules for a given room (`JoinRule` type). Will default
/// to Invite if doesnt exist or invalid
pub async fn get_join_rules(&self, room_id: &RoomId) -> JoinRule {
self.room_state_get_content(room_id, &StateEventType::RoomJoinRules, "")
.await
.map_or_else(|_| JoinRule::Invite, |c: RoomJoinRulesEventContent| (c.join_rule))
}
/// Returns an empty vec if not a restricted room
pub fn allowed_room_ids(&self, join_rule: JoinRule) -> Vec<OwnedRoomId> {
let mut room_ids = Vec::with_capacity(1); // restricted rooms generally only have 1 allowed room ID
if let JoinRule::Restricted(r) | JoinRule::KnockRestricted(r) = join_rule {
for rule in r.allow {
if let AllowRule::RoomMembership(RoomMembership { room_id: membership }) = rule {
room_ids.push(membership.clone());
}
}
}
room_ids
.map_or(JoinRule::Invite, |c: RoomJoinRulesEventContent| c.join_rule)
}
pub async fn get_room_type(&self, room_id: &RoomId) -> Result<RoomType> {