32
32
from synapse .spam_checker_api import RegistrationBehaviour
33
33
from synapse .types import RoomAlias , UserProfile
34
34
from synapse .util .async_helpers import delay_cancellation , maybe_awaitable
35
+ from synapse .util .async_helpers import maybe_awaitable
36
+ from synapse .util .metrics import Measure
35
37
36
38
if TYPE_CHECKING :
37
39
import synapse .events
@@ -162,7 +164,10 @@ def run(*args: Any, **kwargs: Any) -> Awaitable:
162
164
163
165
164
166
class SpamChecker :
165
- def __init__ (self ) -> None :
167
+ def __init__ (self , hs : "synapse.server.HomeServer" ) -> None :
168
+ self .hs = hs
169
+ self .clock = hs .get_clock ()
170
+
166
171
self ._check_event_for_spam_callbacks : List [CHECK_EVENT_FOR_SPAM_CALLBACK ] = []
167
172
self ._user_may_join_room_callbacks : List [USER_MAY_JOIN_ROOM_CALLBACK ] = []
168
173
self ._user_may_invite_callbacks : List [USER_MAY_INVITE_CALLBACK ] = []
@@ -255,7 +260,10 @@ async def check_event_for_spam(
255
260
will be used as the error message returned to the user.
256
261
"""
257
262
for callback in self ._check_event_for_spam_callbacks :
258
- res : Union [bool , str ] = await delay_cancellation (callback (event ))
263
+ with Measure (
264
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
265
+ ):
266
+ res : Union [bool , str ] = await delay_cancellation (callback (event ))
259
267
if res :
260
268
return res
261
269
@@ -276,9 +284,12 @@ async def user_may_join_room(
276
284
Whether the user may join the room
277
285
"""
278
286
for callback in self ._user_may_join_room_callbacks :
279
- may_join_room = await delay_cancellation (
280
- callback (user_id , room_id , is_invited )
281
- )
287
+ with Measure (
288
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
289
+ ):
290
+ may_join_room = await delay_cancellation (
291
+ callback (user_id , room_id , is_invited )
292
+ )
282
293
if may_join_room is False :
283
294
return False
284
295
@@ -300,9 +311,12 @@ async def user_may_invite(
300
311
True if the user may send an invite, otherwise False
301
312
"""
302
313
for callback in self ._user_may_invite_callbacks :
303
- may_invite = await delay_cancellation (
304
- callback (inviter_userid , invitee_userid , room_id )
305
- )
314
+ with Measure (
315
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
316
+ ):
317
+ may_invite = await delay_cancellation (
318
+ callback (inviter_userid , invitee_userid , room_id )
319
+ )
306
320
if may_invite is False :
307
321
return False
308
322
@@ -328,9 +342,12 @@ async def user_may_send_3pid_invite(
328
342
True if the user may send the invite, otherwise False
329
343
"""
330
344
for callback in self ._user_may_send_3pid_invite_callbacks :
331
- may_send_3pid_invite = await delay_cancellation (
332
- callback (inviter_userid , medium , address , room_id )
333
- )
345
+ with Measure (
346
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
347
+ ):
348
+ may_send_3pid_invite = await delay_cancellation (
349
+ callback (inviter_userid , medium , address , room_id )
350
+ )
334
351
if may_send_3pid_invite is False :
335
352
return False
336
353
@@ -348,7 +365,10 @@ async def user_may_create_room(self, userid: str) -> bool:
348
365
True if the user may create a room, otherwise False
349
366
"""
350
367
for callback in self ._user_may_create_room_callbacks :
351
- may_create_room = await delay_cancellation (callback (userid ))
368
+ with Measure (
369
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
370
+ ):
371
+ may_create_room = await delay_cancellation (callback (userid ))
352
372
if may_create_room is False :
353
373
return False
354
374
@@ -369,9 +389,12 @@ async def user_may_create_room_alias(
369
389
True if the user may create a room alias, otherwise False
370
390
"""
371
391
for callback in self ._user_may_create_room_alias_callbacks :
372
- may_create_room_alias = await delay_cancellation (
373
- callback (userid , room_alias )
374
- )
392
+ with Measure (
393
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
394
+ ):
395
+ may_create_room_alias = await delay_cancellation (
396
+ callback (userid , room_alias )
397
+ )
375
398
if may_create_room_alias is False :
376
399
return False
377
400
@@ -390,7 +413,10 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
390
413
True if the user may publish the room, otherwise False
391
414
"""
392
415
for callback in self ._user_may_publish_room_callbacks :
393
- may_publish_room = await delay_cancellation (callback (userid , room_id ))
416
+ with Measure (
417
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
418
+ ):
419
+ may_publish_room = await delay_cancellation (callback (userid , room_id ))
394
420
if may_publish_room is False :
395
421
return False
396
422
@@ -412,9 +438,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
412
438
True if the user is spammy.
413
439
"""
414
440
for callback in self ._check_username_for_spam_callbacks :
415
- # Make a copy of the user profile object to ensure the spam checker cannot
416
- # modify it.
417
- if await delay_cancellation (callback (user_profile .copy ())):
441
+ with Measure (
442
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
443
+ ):
444
+ # Make a copy of the user profile object to ensure the spam checker cannot
445
+ # modify it.
446
+ res = await delay_cancellation (callback (user_profile .copy ()))
447
+ if res :
418
448
return True
419
449
420
450
return False
@@ -442,9 +472,12 @@ async def check_registration_for_spam(
442
472
"""
443
473
444
474
for callback in self ._check_registration_for_spam_callbacks :
445
- behaviour = await delay_cancellation (
446
- callback (email_threepid , username , request_info , auth_provider_id )
447
- )
475
+ with Measure (
476
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
477
+ ):
478
+ behaviour = await delay_cancellation (
479
+ callback (email_threepid , username , request_info , auth_provider_id )
480
+ )
448
481
assert isinstance (behaviour , RegistrationBehaviour )
449
482
if behaviour != RegistrationBehaviour .ALLOW :
450
483
return behaviour
@@ -486,7 +519,10 @@ async def check_media_file_for_spam(
486
519
"""
487
520
488
521
for callback in self ._check_media_file_for_spam_callbacks :
489
- spam = await delay_cancellation (callback (file_wrapper , file_info ))
522
+ with Measure (
523
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
524
+ ):
525
+ spam = await delay_cancellation (callback (file_wrapper , file_info ))
490
526
if spam :
491
527
return True
492
528
0 commit comments