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

Commit d693641

Browse files
committed
Port the rest of the spam-checker API to accept Union[Allow, Codes] where relevant.
1 parent f449081 commit d693641

File tree

10 files changed

+454
-172
lines changed

10 files changed

+454
-172
lines changed

changelog.d/12857.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port spam-checker API callbacks `user_may_join_room`, `user_may_invite`, `user_may_send_3pid_invite`, `user_may_create_room`, `user_may_create_room_alias`, `user_may_publish_room`, `check_media_file_for_spam` to more powerful and less ambiguous `Union[Allow, Codes]`.

docs/modules/spam_checker_callbacks.md

+89-63
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The available spam checker callbacks are:
1111
### `check_event_for_spam`
1212

1313
_First introduced in Synapse v1.37.0_
14-
_Signature extended to support Allow and Code in Synapse v1.60.0_
14+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
1515
_Boolean and string return value types deprecated in Synapse v1.60.0_
1616

1717
```python
@@ -38,14 +38,22 @@ will not call any of the subsequent implementations of this callback.
3838
### `user_may_join_room`
3939

4040
_First introduced in Synapse v1.37.0_
41+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
42+
_Boolean return value type deprecated in Synapse v1.60.0_
4143

4244
```python
43-
async def user_may_join_room(user: str, room: str, is_invited: bool) -> bool
45+
async def user_may_join_room(user: str, room: str, is_invited: bool) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
4446
```
4547

46-
Called when a user is trying to join a room. The module must return a `bool` to indicate
47-
whether the user can join the room. Return `False` to prevent the user from joining the
48-
room; otherwise return `True` to permit the joining.
48+
Called when a user is trying to join a room. The callback must return either:
49+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
50+
may still decide to reject it.
51+
- `synapse.api.Codes` to reject the operation with an error code. In case
52+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
53+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
54+
callbacks in expect `True` to allow and others `True` to reject.
55+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
56+
some callbacks in expect `True` to allow and others `True` to reject.
4957

5058
The user is represented by their Matrix user ID (e.g.
5159
`@alice:example.com`) and the room is represented by its Matrix ID (e.g.
@@ -55,46 +63,52 @@ currently has a pending invite in the room.
5563
This callback isn't called if the join is performed by a server administrator, or in the
5664
context of a room creation.
5765

58-
If multiple modules implement this callback, they will be considered in order. If a
59-
callback returns `True`, Synapse falls through to the next one. The value of the first
60-
callback that does not return `True` will be used. If this happens, Synapse will not call
61-
any of the subsequent implementations of this callback.
62-
6366
### `user_may_invite`
6467

6568
_First introduced in Synapse v1.37.0_
69+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
70+
_Boolean return value type deprecated in Synapse v1.60.0_
6671

6772
```python
68-
async def user_may_invite(inviter: str, invitee: str, room_id: str) -> bool
73+
async def user_may_invite(inviter: str, invitee: str, room_id: str) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
6974
```
7075

71-
Called when processing an invitation. The module must return a `bool` indicating whether
72-
the inviter can invite the invitee to the given room. Both inviter and invitee are
73-
represented by their Matrix user ID (e.g. `@alice:example.com`). Return `False` to prevent
74-
the invitation; otherwise return `True` to permit it.
76+
Called when processing an invitation. Called when a user is trying to join a room. The callback must return either:
77+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
78+
may still decide to reject it.
79+
- `synapse.api.Codes` to reject the operation with an error code. In case
80+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
81+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
82+
callbacks in expect `True` to allow and others `True` to reject.
83+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
84+
some callbacks in expect `True` to allow and others `True` to reject.
7585

76-
If multiple modules implement this callback, they will be considered in order. If a
77-
callback returns `True`, Synapse falls through to the next one. The value of the first
78-
callback that does not return `True` will be used. If this happens, Synapse will not call
79-
any of the subsequent implementations of this callback.
8086

8187
### `user_may_send_3pid_invite`
8288

8389
_First introduced in Synapse v1.45.0_
90+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
91+
_Boolean return value type deprecated in Synapse v1.60.0_
8492

8593
```python
8694
async def user_may_send_3pid_invite(
8795
inviter: str,
8896
medium: str,
8997
address: str,
9098
room_id: str,
91-
) -> bool
99+
) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
92100
```
93101

94-
Called when processing an invitation using a third-party identifier (also called a 3PID,
95-
e.g. an email address or a phone number). The module must return a `bool` indicating
96-
whether the inviter can invite the invitee to the given room. Return `False` to prevent
97-
the invitation; otherwise return `True` to permit it.
102+
Called when processing an invitation using a third-party identifier (also called a 3PID,
103+
e.g. an email address or a phone number). The callback must return either:
104+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
105+
may still decide to reject it.
106+
- `synapse.api.Codes` to reject the operation with an error code. In case
107+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
108+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
109+
callbacks in expect `True` to allow and others `True` to reject.
110+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
111+
some callbacks in expect `True` to allow and others `True` to reject.
98112

99113
The inviter is represented by their Matrix user ID (e.g. `@alice:example.com`), and the
100114
invitee is represented by its medium (e.g. "email") and its address
@@ -124,55 +138,62 @@ any of the subsequent implementations of this callback.
124138
### `user_may_create_room`
125139

126140
_First introduced in Synapse v1.37.0_
141+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
142+
_Boolean return value type deprecated in Synapse v1.60.0_
127143

128144
```python
129-
async def user_may_create_room(user: str) -> bool
145+
async def user_may_create_room(user: str) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
130146
```
131147

132-
Called when processing a room creation request. The module must return a `bool` indicating
133-
whether the given user (represented by their Matrix user ID) is allowed to create a room.
134-
Return `False` to prevent room creation; otherwise return `True` to permit it.
135-
136-
If multiple modules implement this callback, they will be considered in order. If a
137-
callback returns `True`, Synapse falls through to the next one. The value of the first
138-
callback that does not return `True` will be used. If this happens, Synapse will not call
139-
any of the subsequent implementations of this callback.
148+
Called when processing a room creation request. The callback must return either:
149+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
150+
may still decide to reject it.
151+
- `synapse.api.Codes` to reject the operation with an error code. In case
152+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
153+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
154+
callbacks in expect `True` to allow and others `True` to reject.
155+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
156+
some callbacks in expect `True` to allow and others `True` to reject.
140157

141158
### `user_may_create_room_alias`
142159

143160
_First introduced in Synapse v1.37.0_
161+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
162+
_Boolean return value type deprecated in Synapse v1.60.0_
144163

145164
```python
146165
async def user_may_create_room_alias(user: str, room_alias: "synapse.types.RoomAlias") -> bool
147166
```
148167

149-
Called when trying to associate an alias with an existing room. The module must return a
150-
`bool` indicating whether the given user (represented by their Matrix user ID) is allowed
151-
to set the given alias. Return `False` to prevent the alias creation; otherwise return
152-
`True` to permit it.
153-
154-
If multiple modules implement this callback, they will be considered in order. If a
155-
callback returns `True`, Synapse falls through to the next one. The value of the first
156-
callback that does not return `True` will be used. If this happens, Synapse will not call
157-
any of the subsequent implementations of this callback.
168+
Called when trying to associate an alias with an existing room. The callback must return either:
169+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
170+
may still decide to reject it.
171+
- `synapse.api.Codes` to reject the operation with an error code. In case
172+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
173+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
174+
callbacks in expect `True` to allow and others `True` to reject.
175+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
176+
some callbacks in expect `True` to allow and others `True` to reject.
158177

159178
### `user_may_publish_room`
160179

161180
_First introduced in Synapse v1.37.0_
181+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
182+
_Boolean return value type deprecated in Synapse v1.60.0_
162183

163184
```python
164-
async def user_may_publish_room(user: str, room_id: str) -> bool
185+
async def user_may_publish_room(user: str, room_id: str) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
165186
```
166187

167-
Called when trying to publish a room to the homeserver's public rooms directory. The
168-
module must return a `bool` indicating whether the given user (represented by their
169-
Matrix user ID) is allowed to publish the given room. Return `False` to prevent the
170-
room from being published; otherwise return `True` to permit its publication.
171-
172-
If multiple modules implement this callback, they will be considered in order. If a
173-
callback returns `True`, Synapse falls through to the next one. The value of the first
174-
callback that does not return `True` will be used. If this happens, Synapse will not call
175-
any of the subsequent implementations of this callback.
188+
Called when trying to publish a room to the homeserver's public rooms directory. The callback must return either:
189+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
190+
may still decide to reject it.
191+
- `synapse.api.Codes` to reject the operation with an error code. In case
192+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
193+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
194+
callbacks in expect `True` to allow and others `True` to reject.
195+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
196+
some callbacks in expect `True` to allow and others `True` to reject.
176197

177198
### `check_username_for_spam`
178199

@@ -239,22 +260,25 @@ this callback.
239260
### `check_media_file_for_spam`
240261

241262
_First introduced in Synapse v1.37.0_
263+
_Signature extended to support Allow and Codes in Synapse v1.60.0_
264+
_Boolean return value type deprecated in Synapse v1.60.0_
242265

243266
```python
244267
async def check_media_file_for_spam(
245268
file_wrapper: "synapse.rest.media.v1.media_storage.ReadableFileWrapper",
246269
file_info: "synapse.rest.media.v1._base.FileInfo",
247-
) -> bool
270+
) -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes", bool]
248271
```
249272

250-
Called when storing a local or remote file. The module must return a `bool` indicating
251-
whether the given file should be excluded from the homeserver's media store. Return
252-
`True` to prevent this file from being stored; otherwise return `False`.
253-
254-
If multiple modules implement this callback, they will be considered in order. If a
255-
callback returns `False`, Synapse falls through to the next one. The value of the first
256-
callback that does not return `False` will be used. If this happens, Synapse will not call
257-
any of the subsequent implementations of this callback.
273+
Called when storing a local or remote file. The callback must return either:
274+
- `synapse.module_api.ALLOW`, to allow the operation. Other callbacks
275+
may still decide to reject it.
276+
- `synapse.api.Codes` to reject the operation with an error code. In case
277+
of doubt, `synapse.api.error.Codes.FORBIDDEN` is a good error code.
278+
- (deprecated) on `True`, behave as `ALLOW`. Deprecated as confusing, as some
279+
callbacks in expect `True` to allow and others `True` to reject.
280+
- (deprecated) on `False`, behave as `synapse.api.error.Codes.FORBIDDEN`. Deprecated as confusing, as
281+
some callbacks in expect `True` to allow and others `True` to reject.
258282

259283
## Example
260284

@@ -299,6 +323,8 @@ class ListSpamChecker:
299323
resource=IsUserEvilResource(config),
300324
)
301325

302-
async def check_event_for_spam(self, event: "synapse.events.EventBase") -> Union[bool, str]:
303-
return event.sender not in self.evil_users
326+
async def check_event_for_spam(self, event: "synapse.events.EventBase") -> Union["synapse.module_api.ALLOW", "synapse.module_api.error.Codes"]:
327+
if event.sender not in self.evil_users:
328+
return synapse.module_api.ALLOW
329+
return synapse.module_api.error.Codes.FORBIDDEN
304330
```

0 commit comments

Comments
 (0)