2727 Union ,
2828)
2929
30+ from synapse .api .errors import Codes
3031from synapse .rest .media .v1 ._base import FileInfo
3132from synapse .rest .media .v1 .media_storage import ReadableFileWrapper
32- from synapse .spam_checker_api import RegistrationBehaviour
33+ from synapse .spam_checker_api import ALLOW , Allow , Decision , RegistrationBehaviour
3334from synapse .types import RoomAlias , UserProfile
3435from synapse .util .async_helpers import delay_cancellation , maybe_awaitable
3536from synapse .util .metrics import Measure
4041
4142logger = logging .getLogger (__name__ )
4243
44+
45+ # A boolean returned value, kept for backwards compatibility but deprecated.
46+ DEPRECATED_BOOL = bool
47+
48+ # A string returned value, kept for backwards compatibility but deprecated.
49+ DEPRECATED_STR = str
50+
4351CHECK_EVENT_FOR_SPAM_CALLBACK = Callable [
4452 ["synapse.events.EventBase" ],
45- Awaitable [Union [bool , str ]],
53+ Awaitable [Union [Allow , Codes , DEPRECATED_BOOL , DEPRECATED_STR ]],
4654]
4755USER_MAY_JOIN_ROOM_CALLBACK = Callable [[str , str , bool ], Awaitable [bool ]]
4856USER_MAY_INVITE_CALLBACK = Callable [[str , str , str ], Awaitable [bool ]]
@@ -244,7 +252,7 @@ def register_callbacks(
244252
245253 async def check_event_for_spam (
246254 self , event : "synapse.events.EventBase"
247- ) -> Union [bool , str ]:
255+ ) -> Union [Decision , str ]:
248256 """Checks if a given event is considered "spammy" by this server.
249257
250258 If the server considers an event spammy, then it will be rejected if
@@ -255,18 +263,36 @@ async def check_event_for_spam(
255263 event: the event to be checked
256264
257265 Returns:
258- True or a string if the event is spammy. If a string is returned it
259- will be used as the error message returned to the user.
266+ - on `ALLOW`, the event is considered good (non-spammy) and should
267+ be let through. Other spamcheck filters may still reject it.
268+ - on `Code`, the event is considered spammy and is rejected with a specific
269+ error message/code.
270+ - on `str`, the event is considered spammy and the string is used as error
271+ message. This usage is generally discouraged as it doesn't support
272+ internationalization.
260273 """
261274 for callback in self ._check_event_for_spam_callbacks :
262275 with Measure (
263276 self .clock , "{}.{}" .format (callback .__module__ , callback .__qualname__ )
264277 ):
265- res : Union [bool , str ] = await delay_cancellation (callback (event ))
266- if res :
267- return res
268-
269- return False
278+ res : Union [
279+ Decision , DEPRECATED_STR , DEPRECATED_BOOL
280+ ] = await delay_cancellation (callback (event ))
281+ if res is False or res is ALLOW :
282+ # This spam-checker accepts the event.
283+ # Other spam-checkers may reject it, though.
284+ continue
285+ elif res is True :
286+ # This spam-checker rejects the event with deprecated
287+ # return value `True`
288+ return Codes .FORBIDDEN
289+ else :
290+ # This spam-checker rejects the event either with a `str`
291+ # or with a `Codes`. In either case, we stop here.
292+ return res
293+
294+ # No spam-checker has rejected the event, let it pass.
295+ return ALLOW
270296
271297 async def user_may_join_room (
272298 self , user_id : str , room_id : str , is_invited : bool
0 commit comments