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 maybe_awaitable
35
+ from synapse .util .metrics import Measure
35
36
36
37
if TYPE_CHECKING :
37
38
import synapse .events
@@ -162,7 +163,10 @@ def run(*args: Any, **kwargs: Any) -> Awaitable:
162
163
163
164
164
165
class SpamChecker :
165
- def __init__ (self ) -> None :
166
+ def __init__ (self , hs : "synapse.server.HomeServer" ) -> None :
167
+ self .hs = hs
168
+ self .clock = hs .get_clock ()
169
+
166
170
self ._check_event_for_spam_callbacks : List [CHECK_EVENT_FOR_SPAM_CALLBACK ] = []
167
171
self ._user_may_join_room_callbacks : List [USER_MAY_JOIN_ROOM_CALLBACK ] = []
168
172
self ._user_may_invite_callbacks : List [USER_MAY_INVITE_CALLBACK ] = []
@@ -255,7 +259,10 @@ async def check_event_for_spam(
255
259
will be used as the error message returned to the user.
256
260
"""
257
261
for callback in self ._check_event_for_spam_callbacks :
258
- res : Union [bool , str ] = await callback (event )
262
+ with Measure (
263
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
264
+ ):
265
+ res : Union [bool , str ] = await callback (event )
259
266
if res :
260
267
return res
261
268
@@ -276,7 +283,11 @@ async def user_may_join_room(
276
283
Whether the user may join the room
277
284
"""
278
285
for callback in self ._user_may_join_room_callbacks :
279
- if await callback (user_id , room_id , is_invited ) is False :
286
+ with Measure (
287
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
288
+ ):
289
+ res = await callback (user_id , room_id , is_invited )
290
+ if res is False :
280
291
return False
281
292
282
293
return True
@@ -297,7 +308,11 @@ async def user_may_invite(
297
308
True if the user may send an invite, otherwise False
298
309
"""
299
310
for callback in self ._user_may_invite_callbacks :
300
- if await callback (inviter_userid , invitee_userid , room_id ) is False :
311
+ with Measure (
312
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
313
+ ):
314
+ res = await callback (inviter_userid , invitee_userid , room_id )
315
+ if res is False :
301
316
return False
302
317
303
318
return True
@@ -322,7 +337,11 @@ async def user_may_send_3pid_invite(
322
337
True if the user may send the invite, otherwise False
323
338
"""
324
339
for callback in self ._user_may_send_3pid_invite_callbacks :
325
- if await callback (inviter_userid , medium , address , room_id ) is False :
340
+ with Measure (
341
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
342
+ ):
343
+ res = await callback (inviter_userid , medium , address , room_id )
344
+ if res is False :
326
345
return False
327
346
328
347
return True
@@ -339,7 +358,11 @@ async def user_may_create_room(self, userid: str) -> bool:
339
358
True if the user may create a room, otherwise False
340
359
"""
341
360
for callback in self ._user_may_create_room_callbacks :
342
- if await callback (userid ) is False :
361
+ with Measure (
362
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
363
+ ):
364
+ res = await callback (userid )
365
+ if res is False :
343
366
return False
344
367
345
368
return True
@@ -359,7 +382,11 @@ async def user_may_create_room_alias(
359
382
True if the user may create a room alias, otherwise False
360
383
"""
361
384
for callback in self ._user_may_create_room_alias_callbacks :
362
- if await callback (userid , room_alias ) is False :
385
+ with Measure (
386
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
387
+ ):
388
+ res = await callback (userid , room_alias )
389
+ if res is False :
363
390
return False
364
391
365
392
return True
@@ -377,7 +404,11 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
377
404
True if the user may publish the room, otherwise False
378
405
"""
379
406
for callback in self ._user_may_publish_room_callbacks :
380
- if await callback (userid , room_id ) is False :
407
+ with Measure (
408
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
409
+ ):
410
+ res = await callback (userid , room_id )
411
+ if res is False :
381
412
return False
382
413
383
414
return True
@@ -398,9 +429,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
398
429
True if the user is spammy.
399
430
"""
400
431
for callback in self ._check_username_for_spam_callbacks :
401
- # Make a copy of the user profile object to ensure the spam checker cannot
402
- # modify it.
403
- if await callback (user_profile .copy ()):
432
+ with Measure (
433
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
434
+ ):
435
+ # Make a copy of the user profile object to ensure the spam checker cannot
436
+ # modify it.
437
+ res = await callback (user_profile .copy ())
438
+ if res :
404
439
return True
405
440
406
441
return False
@@ -428,9 +463,12 @@ async def check_registration_for_spam(
428
463
"""
429
464
430
465
for callback in self ._check_registration_for_spam_callbacks :
431
- behaviour = await (
432
- callback (email_threepid , username , request_info , auth_provider_id )
433
- )
466
+ with Measure (
467
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
468
+ ):
469
+ behaviour = await (
470
+ callback (email_threepid , username , request_info , auth_provider_id )
471
+ )
434
472
assert isinstance (behaviour , RegistrationBehaviour )
435
473
if behaviour != RegistrationBehaviour .ALLOW :
436
474
return behaviour
@@ -472,7 +510,10 @@ async def check_media_file_for_spam(
472
510
"""
473
511
474
512
for callback in self ._check_media_file_for_spam_callbacks :
475
- spam = await callback (file_wrapper , file_info )
513
+ with Measure (
514
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
515
+ ):
516
+ spam = await callback (file_wrapper , file_info )
476
517
if spam :
477
518
return True
478
519
0 commit comments