12
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
-
16
15
import logging
17
16
import operator
18
17
19
- from synapse .api .constants import AccountDataTypes , EventTypes , Membership
18
+ from synapse .api .constants import (
19
+ AccountDataTypes ,
20
+ EventTypes ,
21
+ HistoryVisibility ,
22
+ Membership ,
23
+ )
20
24
from synapse .events .utils import prune_event
21
25
from synapse .storage import Storage
22
26
from synapse .storage .state import StateFilter
25
29
logger = logging .getLogger (__name__ )
26
30
27
31
28
- VISIBILITY_PRIORITY = ("world_readable" , "shared" , "invited" , "joined" )
32
+ VISIBILITY_PRIORITY = (
33
+ HistoryVisibility .WORLD_READABLE ,
34
+ HistoryVisibility .SHARED ,
35
+ HistoryVisibility .INVITED ,
36
+ HistoryVisibility .JOINED ,
37
+ )
29
38
30
39
31
40
MEMBERSHIP_PRIORITY = (
@@ -150,12 +159,14 @@ def allowed(event):
150
159
# get the room_visibility at the time of the event.
151
160
visibility_event = state .get ((EventTypes .RoomHistoryVisibility , "" ), None )
152
161
if visibility_event :
153
- visibility = visibility_event .content .get ("history_visibility" , "shared" )
162
+ visibility = visibility_event .content .get (
163
+ "history_visibility" , HistoryVisibility .SHARED
164
+ )
154
165
else :
155
- visibility = "shared"
166
+ visibility = HistoryVisibility . SHARED
156
167
157
168
if visibility not in VISIBILITY_PRIORITY :
158
- visibility = "shared"
169
+ visibility = HistoryVisibility . SHARED
159
170
160
171
# Always allow history visibility events on boundaries. This is done
161
172
# by setting the effective visibility to the least restrictive
@@ -165,7 +176,7 @@ def allowed(event):
165
176
prev_visibility = prev_content .get ("history_visibility" , None )
166
177
167
178
if prev_visibility not in VISIBILITY_PRIORITY :
168
- prev_visibility = "shared"
179
+ prev_visibility = HistoryVisibility . SHARED
169
180
170
181
new_priority = VISIBILITY_PRIORITY .index (visibility )
171
182
old_priority = VISIBILITY_PRIORITY .index (prev_visibility )
@@ -210,17 +221,17 @@ def allowed(event):
210
221
211
222
# otherwise, it depends on the room visibility.
212
223
213
- if visibility == "joined" :
224
+ if visibility == HistoryVisibility . JOINED :
214
225
# we weren't a member at the time of the event, so we can't
215
226
# see this event.
216
227
return None
217
228
218
- elif visibility == "invited" :
229
+ elif visibility == HistoryVisibility . INVITED :
219
230
# user can also see the event if they were *invited* at the time
220
231
# of the event.
221
232
return event if membership == Membership .INVITE else None
222
233
223
- elif visibility == "shared" and is_peeking :
234
+ elif visibility == HistoryVisibility . SHARED and is_peeking :
224
235
# if the visibility is shared, users cannot see the event unless
225
236
# they have *subequently* joined the room (or were members at the
226
237
# time, of course)
@@ -284,8 +295,10 @@ def is_sender_erased(event, erased_senders):
284
295
def check_event_is_visible (event , state ):
285
296
history = state .get ((EventTypes .RoomHistoryVisibility , "" ), None )
286
297
if history :
287
- visibility = history .content .get ("history_visibility" , "shared" )
288
- if visibility in ["invited" , "joined" ]:
298
+ visibility = history .content .get (
299
+ "history_visibility" , HistoryVisibility .SHARED
300
+ )
301
+ if visibility in [HistoryVisibility .INVITED , HistoryVisibility .JOINED ]:
289
302
# We now loop through all state events looking for
290
303
# membership states for the requesting server to determine
291
304
# if the server is either in the room or has been invited
@@ -305,7 +318,7 @@ def check_event_is_visible(event, state):
305
318
if memtype == Membership .JOIN :
306
319
return True
307
320
elif memtype == Membership .INVITE :
308
- if visibility == "invited" :
321
+ if visibility == HistoryVisibility . INVITED :
309
322
return True
310
323
else :
311
324
# server has no users in the room: redact
@@ -336,7 +349,8 @@ def check_event_is_visible(event, state):
336
349
else :
337
350
event_map = await storage .main .get_events (visibility_ids )
338
351
all_open = all (
339
- e .content .get ("history_visibility" ) in (None , "shared" , "world_readable" )
352
+ e .content .get ("history_visibility" )
353
+ in (None , HistoryVisibility .SHARED , HistoryVisibility .WORLD_READABLE )
340
354
for e in event_map .values ()
341
355
)
342
356
0 commit comments