31
31
from synapse .rest .media .v1 .media_storage import ReadableFileWrapper
32
32
from synapse .spam_checker_api import RegistrationBehaviour
33
33
from synapse .types import RoomAlias , UserProfile
34
- from synapse .util .async_helpers import maybe_awaitable
34
+ from synapse .util .async_helpers import delay_cancellation , maybe_awaitable
35
35
36
36
if TYPE_CHECKING :
37
37
import synapse .events
@@ -255,7 +255,7 @@ async def check_event_for_spam(
255
255
will be used as the error message returned to the user.
256
256
"""
257
257
for callback in self ._check_event_for_spam_callbacks :
258
- res : Union [bool , str ] = await callback (event )
258
+ res : Union [bool , str ] = await delay_cancellation ( callback (event ) )
259
259
if res :
260
260
return res
261
261
@@ -276,7 +276,10 @@ async def user_may_join_room(
276
276
Whether the user may join the room
277
277
"""
278
278
for callback in self ._user_may_join_room_callbacks :
279
- if await callback (user_id , room_id , is_invited ) is False :
279
+ may_join_room = await delay_cancellation (
280
+ callback (user_id , room_id , is_invited )
281
+ )
282
+ if may_join_room is False :
280
283
return False
281
284
282
285
return True
@@ -297,7 +300,10 @@ async def user_may_invite(
297
300
True if the user may send an invite, otherwise False
298
301
"""
299
302
for callback in self ._user_may_invite_callbacks :
300
- if await callback (inviter_userid , invitee_userid , room_id ) is False :
303
+ may_invite = await delay_cancellation (
304
+ callback (inviter_userid , invitee_userid , room_id )
305
+ )
306
+ if may_invite is False :
301
307
return False
302
308
303
309
return True
@@ -322,7 +328,10 @@ async def user_may_send_3pid_invite(
322
328
True if the user may send the invite, otherwise False
323
329
"""
324
330
for callback in self ._user_may_send_3pid_invite_callbacks :
325
- if await callback (inviter_userid , medium , address , room_id ) is False :
331
+ may_send_3pid_invite = await delay_cancellation (
332
+ callback (inviter_userid , medium , address , room_id )
333
+ )
334
+ if may_send_3pid_invite is False :
326
335
return False
327
336
328
337
return True
@@ -339,7 +348,8 @@ async def user_may_create_room(self, userid: str) -> bool:
339
348
True if the user may create a room, otherwise False
340
349
"""
341
350
for callback in self ._user_may_create_room_callbacks :
342
- if await callback (userid ) is False :
351
+ may_create_room = await delay_cancellation (callback (userid ))
352
+ if may_create_room is False :
343
353
return False
344
354
345
355
return True
@@ -359,7 +369,10 @@ async def user_may_create_room_alias(
359
369
True if the user may create a room alias, otherwise False
360
370
"""
361
371
for callback in self ._user_may_create_room_alias_callbacks :
362
- if await callback (userid , room_alias ) is False :
372
+ may_create_room_alias = await delay_cancellation (
373
+ callback (userid , room_alias )
374
+ )
375
+ if may_create_room_alias is False :
363
376
return False
364
377
365
378
return True
@@ -377,7 +390,8 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
377
390
True if the user may publish the room, otherwise False
378
391
"""
379
392
for callback in self ._user_may_publish_room_callbacks :
380
- if await callback (userid , room_id ) is False :
393
+ may_publish_room = await delay_cancellation (callback (userid , room_id ))
394
+ if may_publish_room is False :
381
395
return False
382
396
383
397
return True
@@ -400,7 +414,7 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
400
414
for callback in self ._check_username_for_spam_callbacks :
401
415
# Make a copy of the user profile object to ensure the spam checker cannot
402
416
# modify it.
403
- if await callback (user_profile .copy ()):
417
+ if await delay_cancellation ( callback (user_profile .copy () )):
404
418
return True
405
419
406
420
return False
@@ -428,7 +442,7 @@ async def check_registration_for_spam(
428
442
"""
429
443
430
444
for callback in self ._check_registration_for_spam_callbacks :
431
- behaviour = await (
445
+ behaviour = await delay_cancellation (
432
446
callback (email_threepid , username , request_info , auth_provider_id )
433
447
)
434
448
assert isinstance (behaviour , RegistrationBehaviour )
@@ -472,7 +486,7 @@ async def check_media_file_for_spam(
472
486
"""
473
487
474
488
for callback in self ._check_media_file_for_spam_callbacks :
475
- spam = await callback (file_wrapper , file_info )
489
+ spam = await delay_cancellation ( callback (file_wrapper , file_info ) )
476
490
if spam :
477
491
return True
478
492
0 commit comments