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

Commit 92b87d5

Browse files
committed
WIP: Applied feedback
1 parent f39d568 commit 92b87d5

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

docs/modules/spam_checker_callbacks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ _Signature extended to support Allow and Code in Synapse v1.60.0_
1515
_Boolean and string return value types deprecated in Synapse v1.60.0_
1616

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

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

3333
If multiple modules implement this callback, they will be considered in order. If a

synapse/events/spamcheck.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ async def check_event_for_spam(
275275
with Measure(
276276
self.clock, "{}.{}".format(callback.__module__, callback.__qualname__)
277277
):
278-
res: Union[
279-
Decision, DEPRECATED_STR, DEPRECATED_BOOL
280-
] = await delay_cancellation(callback(event))
278+
res: Union[Decision, str, bool] = await delay_cancellation(
279+
callback(event)
280+
)
281281
if res is False or res is Allow.ALLOW:
282282
# This spam-checker accepts the event.
283283
# Other spam-checkers may reject it, though.

synapse/module_api/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from twisted.web.resource import Resource
3737

3838
from synapse import spam_checker_api
39-
from synapse.api.errors import Codes, SynapseError
39+
from synapse.api.errors import SynapseError
4040
from synapse.events import EventBase
4141
from synapse.events.presence_router import (
4242
GET_INTERESTED_USERS_CALLBACK,
@@ -151,7 +151,6 @@
151151
"run_in_background",
152152
"cached",
153153
"Allow",
154-
"Codes",
155154
"UserID",
156155
"DatabasePool",
157156
"LoggingTransaction",

synapse/module_api/errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Exception types which are exposed as part of the stable module API"""
1616

1717
from synapse.api.errors import (
18+
Codes,
1819
InvalidClientCredentialsError,
1920
RedirectException,
2021
SynapseError,
@@ -24,6 +25,7 @@
2425
from synapse.storage.push_rule import RuleNotFoundException
2526

2627
__all__ = [
28+
"Codes",
2729
"InvalidClientCredentialsError",
2830
"RedirectException",
2931
"SynapseError",

synapse/spam_checker_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class RegistrationBehaviour(Enum):
2727
DENY = "deny"
2828

2929

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

0 commit comments

Comments
 (0)