From bf46829595450b69a83da8cada99786470a492b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 26 Jul 2023 08:33:27 +0200 Subject: [PATCH] fix: spaces with restricted rooms --- src/service/rooms/spaces/mod.rs | 63 ++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/src/service/rooms/spaces/mod.rs b/src/service/rooms/spaces/mod.rs index 36fa1fcd..e92fc07b 100644 --- a/src/service/rooms/spaces/mod.rs +++ b/src/service/rooms/spaces/mod.rs @@ -16,7 +16,7 @@ use ruma::{ create::RoomCreateEventContent, guest_access::{GuestAccess, RoomGuestAccessEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, - join_rules::{JoinRule, RoomJoinRulesEventContent}, + join_rules::{self, AllowRule, JoinRule, RoomJoinRulesEventContent}, topic::RoomTopicEventContent, }, StateEventType, @@ -30,7 +30,7 @@ use tracing::{debug, error, warn}; use crate::{services, Error, PduEvent, Result}; pub enum CachedJoinRule { - Simplified(SpaceRoomJoinRule), + //Simplified(SpaceRoomJoinRule), Full(JoinRule), } @@ -84,9 +84,9 @@ impl Service { { if let Some(cached) = cached { let allowed = match &cached.join_rule { - CachedJoinRule::Simplified(s) => { - self.handle_simplified_join_rule(s, sender_user, ¤t_room)? - } + //CachedJoinRule::Simplified(s) => { + //self.handle_simplified_join_rule(s, sender_user, ¤t_room)? + //} CachedJoinRule::Full(f) => { self.handle_join_rule(f, sender_user, ¤t_room)? } @@ -211,11 +211,34 @@ impl Service { .map(|c| c.room_id.clone()) .collect::>(); - if self.handle_simplified_join_rule( - &response.room.join_rule, - sender_user, - ¤t_room, - )? { + let join_rule = match response.room.join_rule { + SpaceRoomJoinRule::Invite => JoinRule::Invite, + SpaceRoomJoinRule::Knock => JoinRule::Knock, + SpaceRoomJoinRule::Private => JoinRule::Private, + SpaceRoomJoinRule::Restricted => { + JoinRule::Restricted(join_rules::Restricted { + allow: response + .room + .allowed_room_ids + .into_iter() + .map(|room| AllowRule::room_membership(room)) + .collect(), + }) + } + SpaceRoomJoinRule::KnockRestricted => { + JoinRule::KnockRestricted(join_rules::Restricted { + allow: response + .room + .allowed_room_ids + .into_iter() + .map(|room| AllowRule::room_membership(room)) + .collect(), + }) + } + SpaceRoomJoinRule::Public => JoinRule::Public, + _ => return Err(Error::BadServerResponse("Unknown join rule")), + }; + if self.handle_join_rule(&join_rule, sender_user, ¤t_room)? { if left_to_skip > 0 { left_to_skip -= 1; } else { @@ -231,7 +254,7 @@ impl Service { Some(CachedSpaceChunk { chunk, children, - join_rule: CachedJoinRule::Simplified(response.room.join_rule), + join_rule: CachedJoinRule::Full(join_rule), }), ); @@ -437,8 +460,22 @@ impl Service { } match join_rule { - JoinRule::Restricted(_) => { - // TODO: Check rules + JoinRule::Restricted(r) => { + for rule in &r.allow { + match rule { + join_rules::AllowRule::RoomMembership(rm) => { + if let Ok(true) = services() + .rooms + .state_cache + .is_joined(sender_user, &rm.room_id) + { + return Ok(true); + } + } + _ => {} + } + } + Ok(false) } JoinRule::KnockRestricted(_) => {