Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
WIP: Applied feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoric committed May 23, 2022
1 parent f39d568 commit 92b87d5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/modules/spam_checker_callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ _Signature extended to support Allow and Code in Synapse v1.60.0_
_Boolean and string return value types deprecated in Synapse v1.60.0_

```python
async def check_event_for_spam(event: "synapse.module_api.EventBase") -> Union["synapse.module_api.Allow", "synapse.module_api.Codes", str, bool]
async def check_event_for_spam(event: "synapse.module_api.EventBase") -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", str, bool]
```

Called when receiving an event from a client or via federation. The callback must return either:
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
may still decide to reject it.
- `synapse.api.Codes` to reject the operation with an error code. In case
of doubt, `synapse.api.Codes.FORBIDDEN` is a good error code.
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
- (deprecated) a `str` to reject the operation and specify an error message. Note that clients
typically will not localize the error message to the user's preferred locale.
- (deprecated) on `False`, behave as `ALLOW`. Deprecated as confusing, as some
callbacks in expect `True` to allow and others `True` to reject.
- (deprecated) on `True`, behave as `synapse.api.Codes.FORBIDDEN`. Deprecated as confusing, as
- (deprecated) on `True`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
some callbacks in expect `True` to allow and others `True` to reject.

If multiple modules implement this callback, they will be considered in order. If a
Expand Down
6 changes: 3 additions & 3 deletions synapse/events/spamcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ async def check_event_for_spam(
with Measure(
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
):
res: Union[
Decision, DEPRECATED_STR, DEPRECATED_BOOL
] = await delay_cancellation(callback(event))
res: Union[Decision, str, bool] = await delay_cancellation(
callback(event)
)
if res is False or res is Allow.ALLOW:
# This spam-checker accepts the event.
# Other spam-checkers may reject it, though.
Expand Down
3 changes: 1 addition & 2 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from twisted.web.resource import Resource

from synapse import spam_checker_api
from synapse.api.errors import Codes, SynapseError
from synapse.api.errors import SynapseError
from synapse.events import EventBase
from synapse.events.presence_router import (
GET_INTERESTED_USERS_CALLBACK,
Expand Down Expand Up @@ -151,7 +151,6 @@
"run_in_background",
"cached",
"Allow",
"Codes",
"UserID",
"DatabasePool",
"LoggingTransaction",
Expand Down
2 changes: 2 additions & 0 deletions synapse/module_api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Exception types which are exposed as part of the stable module API"""

from synapse.api.errors import (
Codes,
InvalidClientCredentialsError,
RedirectException,
SynapseError,
Expand All @@ -24,6 +25,7 @@
from synapse.storage.push_rule import RuleNotFoundException

__all__ = [
"Codes",
"InvalidClientCredentialsError",
"RedirectException",
"SynapseError",
Expand Down
4 changes: 4 additions & 0 deletions synapse/spam_checker_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class RegistrationBehaviour(Enum):
DENY = "deny"


# We define the following singleton enum rather than a string to be able to
# write `Union[Allow, ..., str]` in some of the callbacks for the spam-checker
# API, where the `str` is required to maintain backwards compatibility with
# previous versions of the API.
class Allow(Enum):
"""
Singleton to allow events to pass through in SpamChecker APIs.
Expand Down

0 comments on commit 92b87d5

Please sign in to comment.