13
13
# limitations under the License.
14
14
from typing import TYPE_CHECKING , Collection , Optional
15
15
16
- from synapse .api .constants import EventTypes , JoinRules , Membership
16
+ from synapse .api .constants import (
17
+ EventTypes ,
18
+ JoinRules ,
19
+ Membership ,
20
+ RestrictedJoinRuleTypes ,
21
+ )
17
22
from synapse .api .errors import AuthError
18
23
from synapse .api .room_versions import RoomVersion
19
24
from synapse .events import EventBase
@@ -42,7 +47,7 @@ async def check_restricted_join_rules(
42
47
Check whether a user can join a room without an invite due to restricted join rules.
43
48
44
49
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.
46
51
47
52
Args:
48
53
state_ids: The state of the room as it currently is.
@@ -67,20 +72,20 @@ async def check_restricted_join_rules(
67
72
if not await self .has_restricted_join_rules (state_ids , room_version ):
68
73
return
69
74
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
71
76
# 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 ):
74
79
raise AuthError (
75
80
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." ,
77
82
)
78
83
79
84
async def has_restricted_join_rules (
80
85
self , state_ids : StateMap [str ], room_version : RoomVersion
81
86
) -> bool :
82
87
"""
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 .
84
89
85
90
Args:
86
91
state_ids: The state of the room as it currently is.
@@ -102,17 +107,17 @@ async def has_restricted_join_rules(
102
107
join_rules_event = await self ._store .get_event (join_rules_event_id )
103
108
return join_rules_event .content .get ("join_rule" ) == JoinRules .MSC3083_RESTRICTED
104
109
105
- async def get_spaces_that_allow_join (
110
+ async def get_rooms_that_allow_join (
106
111
self , state_ids : StateMap [str ]
107
112
) -> Collection [str ]:
108
113
"""
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.
110
115
111
116
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
113
118
114
119
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.
116
121
"""
117
122
# If there's no join rule, then it defaults to invite (so this doesn't apply).
118
123
join_rules_event_id = state_ids .get ((EventTypes .JoinRules , "" ), None )
@@ -123,21 +128,25 @@ async def get_spaces_that_allow_join(
123
128
join_rules_event = await self ._store .get_event (join_rules_event_id )
124
129
125
130
# 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 ):
128
133
return ()
129
134
130
135
# Pull out the other room IDs, invalid data gets filtered.
131
136
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 :
134
143
continue
135
144
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 ):
138
147
continue
139
148
140
- result .append (space_id )
149
+ result .append (room_id )
141
150
142
151
return result
143
152
0 commit comments