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 .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 delay_cancellation (callback (event ))
262
+ with Measure (
263
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
264
+ ):
265
+ res : Union [bool , str ] = await delay_cancellation (callback (event ))
259
266
if res :
260
267
return res
261
268
@@ -276,9 +283,12 @@ 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
- may_join_room = await delay_cancellation (
280
- callback (user_id , room_id , is_invited )
281
- )
286
+ with Measure (
287
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
288
+ ):
289
+ may_join_room = await delay_cancellation (
290
+ callback (user_id , room_id , is_invited )
291
+ )
282
292
if may_join_room is False :
283
293
return False
284
294
@@ -300,9 +310,12 @@ async def user_may_invite(
300
310
True if the user may send an invite, otherwise False
301
311
"""
302
312
for callback in self ._user_may_invite_callbacks :
303
- may_invite = await delay_cancellation (
304
- callback (inviter_userid , invitee_userid , room_id )
305
- )
313
+ with Measure (
314
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
315
+ ):
316
+ may_invite = await delay_cancellation (
317
+ callback (inviter_userid , invitee_userid , room_id )
318
+ )
306
319
if may_invite is False :
307
320
return False
308
321
@@ -328,9 +341,12 @@ async def user_may_send_3pid_invite(
328
341
True if the user may send the invite, otherwise False
329
342
"""
330
343
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
- )
344
+ with Measure (
345
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
346
+ ):
347
+ may_send_3pid_invite = await delay_cancellation (
348
+ callback (inviter_userid , medium , address , room_id )
349
+ )
334
350
if may_send_3pid_invite is False :
335
351
return False
336
352
@@ -348,7 +364,10 @@ async def user_may_create_room(self, userid: str) -> bool:
348
364
True if the user may create a room, otherwise False
349
365
"""
350
366
for callback in self ._user_may_create_room_callbacks :
351
- may_create_room = await delay_cancellation (callback (userid ))
367
+ with Measure (
368
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
369
+ ):
370
+ may_create_room = await delay_cancellation (callback (userid ))
352
371
if may_create_room is False :
353
372
return False
354
373
@@ -369,9 +388,12 @@ async def user_may_create_room_alias(
369
388
True if the user may create a room alias, otherwise False
370
389
"""
371
390
for callback in self ._user_may_create_room_alias_callbacks :
372
- may_create_room_alias = await delay_cancellation (
373
- callback (userid , room_alias )
374
- )
391
+ with Measure (
392
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
393
+ ):
394
+ may_create_room_alias = await delay_cancellation (
395
+ callback (userid , room_alias )
396
+ )
375
397
if may_create_room_alias is False :
376
398
return False
377
399
@@ -390,7 +412,10 @@ async def user_may_publish_room(self, userid: str, room_id: str) -> bool:
390
412
True if the user may publish the room, otherwise False
391
413
"""
392
414
for callback in self ._user_may_publish_room_callbacks :
393
- may_publish_room = await delay_cancellation (callback (userid , room_id ))
415
+ with Measure (
416
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
417
+ ):
418
+ may_publish_room = await delay_cancellation (callback (userid , room_id ))
394
419
if may_publish_room is False :
395
420
return False
396
421
@@ -412,9 +437,13 @@ async def check_username_for_spam(self, user_profile: UserProfile) -> bool:
412
437
True if the user is spammy.
413
438
"""
414
439
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 ())):
440
+ with Measure (
441
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
442
+ ):
443
+ # Make a copy of the user profile object to ensure the spam checker cannot
444
+ # modify it.
445
+ res = await delay_cancellation (callback (user_profile .copy ()))
446
+ if res :
418
447
return True
419
448
420
449
return False
@@ -442,9 +471,12 @@ async def check_registration_for_spam(
442
471
"""
443
472
444
473
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
- )
474
+ with Measure (
475
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
476
+ ):
477
+ behaviour = await delay_cancellation (
478
+ callback (email_threepid , username , request_info , auth_provider_id )
479
+ )
448
480
assert isinstance (behaviour , RegistrationBehaviour )
449
481
if behaviour != RegistrationBehaviour .ALLOW :
450
482
return behaviour
@@ -486,7 +518,10 @@ async def check_media_file_for_spam(
486
518
"""
487
519
488
520
for callback in self ._check_media_file_for_spam_callbacks :
489
- spam = await delay_cancellation (callback (file_wrapper , file_info ))
521
+ with Measure (
522
+ self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
523
+ ):
524
+ spam = await delay_cancellation (callback (file_wrapper , file_info ))
490
525
if spam :
491
526
return True
492
527
0 commit comments