1313# limitations under the License.
1414from typing import TYPE_CHECKING , Collection , Optional
1515
16- from synapse .api .constants import EventTypes , JoinRules , Membership
16+ from synapse .api .constants import (
17+ EventTypes ,
18+ JoinRules ,
19+ Membership ,
20+ RestrictedJoinRuleTypes ,
21+ )
1722from synapse .api .errors import AuthError
1823from synapse .api .room_versions import RoomVersion
1924from synapse .events import EventBase
@@ -42,7 +47,7 @@ async def check_restricted_join_rules(
4247 Check whether a user can join a room without an invite due to restricted join rules.
4348
4449 When joining a room with restricted joined rules (as defined in MSC3083),
45- the membership of spaces must be checked during a room join.
50+ the membership of rooms must be checked during a room join.
4651
4752 Args:
4853 state_ids: The state of the room as it currently is.
@@ -67,20 +72,20 @@ async def check_restricted_join_rules(
6772 if not await self .has_restricted_join_rules (state_ids , room_version ):
6873 return
6974
70- # Get the spaces which allow access to this room and check if the user is
75+ # Get the rooms which allow access to this room and check if the user is
7176 # in any of them.
72- allowed_spaces = await self .get_spaces_that_allow_join (state_ids )
73- if not await self .is_user_in_rooms (allowed_spaces , user_id ):
77+ allowed_rooms = await self .get_rooms_that_allow_join (state_ids )
78+ if not await self .is_user_in_rooms (allowed_rooms , user_id ):
7479 raise AuthError (
7580 403 ,
76- "You do not belong to any of the required spaces to join this room." ,
81+ "You do not belong to any of the required rooms to join this room." ,
7782 )
7883
7984 async def has_restricted_join_rules (
8085 self , state_ids : StateMap [str ], room_version : RoomVersion
8186 ) -> bool :
8287 """
83- Return if the room has the proper join rules set for access via spaces .
88+ Return if the room has the proper join rules set for access via rooms .
8489
8590 Args:
8691 state_ids: The state of the room as it currently is.
@@ -102,17 +107,17 @@ async def has_restricted_join_rules(
102107 join_rules_event = await self ._store .get_event (join_rules_event_id )
103108 return join_rules_event .content .get ("join_rule" ) == JoinRules .MSC3083_RESTRICTED
104109
105- async def get_spaces_that_allow_join (
110+ async def get_rooms_that_allow_join (
106111 self , state_ids : StateMap [str ]
107112 ) -> Collection [str ]:
108113 """
109- Generate a list of spaces which allow access to a room.
114+ Generate a list of rooms in which membership allows access to a room.
110115
111116 Args:
112- state_ids: The state of the room as it currently is.
117+ state_ids: The current state of the room the user wishes to join
113118
114119 Returns:
115- A collection of spaces which provide membership to the room.
120+ A collection of room IDs. Membership in any of the rooms in the list grants the ability to join the target room.
116121 """
117122 # If there's no join rule, then it defaults to invite (so this doesn't apply).
118123 join_rules_event_id = state_ids .get ((EventTypes .JoinRules , "" ), None )
@@ -123,21 +128,25 @@ async def get_spaces_that_allow_join(
123128 join_rules_event = await self ._store .get_event (join_rules_event_id )
124129
125130 # If allowed is of the wrong form, then only allow invited users.
126- allowed_spaces = join_rules_event .content .get ("allow" , [])
127- if not isinstance (allowed_spaces , list ):
131+ allow_list = join_rules_event .content .get ("allow" , [])
132+ if not isinstance (allow_list , list ):
128133 return ()
129134
130135 # Pull out the other room IDs, invalid data gets filtered.
131136 result = []
132- for space in allowed_spaces :
133- if not isinstance (space , dict ):
137+ for allow in allow_list :
138+ if not isinstance (allow , dict ):
139+ continue
140+
141+ # If the type is unexpected, skip it.
142+ if allow .get ("type" ) != RestrictedJoinRuleTypes .ROOM_MEMBERSHIP :
134143 continue
135144
136- space_id = space .get ("space " )
137- if not isinstance (space_id , str ):
145+ room_id = allow .get ("room_id " )
146+ if not isinstance (room_id , str ):
138147 continue
139148
140- result .append (space_id )
149+ result .append (room_id )
141150
142151 return result
143152
0 commit comments