15
15
# limitations under the License.
16
16
17
17
import inspect
18
- from typing import TYPE_CHECKING , Any , Dict , List , Optional , Tuple
18
+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Tuple , Union
19
19
20
20
from synapse .spam_checker_api import RegistrationBehaviour
21
21
from synapse .types import Collection
22
+ from synapse .util .async_helpers import maybe_awaitable
22
23
23
24
if TYPE_CHECKING :
24
25
import synapse .events
@@ -39,7 +40,9 @@ def __init__(self, hs: "synapse.server.HomeServer"):
39
40
else :
40
41
self .spam_checkers .append (module (config = config ))
41
42
42
- def check_event_for_spam (self , event : "synapse.events.EventBase" ) -> bool :
43
+ async def check_event_for_spam (
44
+ self , event : "synapse.events.EventBase"
45
+ ) -> Union [bool , str ]:
43
46
"""Checks if a given event is considered "spammy" by this server.
44
47
45
48
If the server considers an event spammy, then it will be rejected if
@@ -50,15 +53,16 @@ def check_event_for_spam(self, event: "synapse.events.EventBase") -> bool:
50
53
event: the event to be checked
51
54
52
55
Returns:
53
- True if the event is spammy.
56
+ True or a string if the event is spammy. If a string is returned it
57
+ will be used as the error message returned to the user.
54
58
"""
55
59
for spam_checker in self .spam_checkers :
56
- if spam_checker .check_event_for_spam (event ):
60
+ if await maybe_awaitable ( spam_checker .check_event_for_spam (event ) ):
57
61
return True
58
62
59
63
return False
60
64
61
- def user_may_invite (
65
+ async def user_may_invite (
62
66
self , inviter_userid : str , invitee_userid : str , room_id : str
63
67
) -> bool :
64
68
"""Checks if a given user may send an invite
@@ -75,14 +79,18 @@ def user_may_invite(
75
79
"""
76
80
for spam_checker in self .spam_checkers :
77
81
if (
78
- spam_checker .user_may_invite (inviter_userid , invitee_userid , room_id )
82
+ await maybe_awaitable (
83
+ spam_checker .user_may_invite (
84
+ inviter_userid , invitee_userid , room_id
85
+ )
86
+ )
79
87
is False
80
88
):
81
89
return False
82
90
83
91
return True
84
92
85
- def user_may_create_room (self , userid : str ) -> bool :
93
+ async def user_may_create_room (self , userid : str ) -> bool :
86
94
"""Checks if a given user may create a room
87
95
88
96
If this method returns false, the creation request will be rejected.
@@ -94,12 +102,15 @@ def user_may_create_room(self, userid: str) -> bool:
94
102
True if the user may create a room, otherwise False
95
103
"""
96
104
for spam_checker in self .spam_checkers :
97
- if spam_checker .user_may_create_room (userid ) is False :
105
+ if (
106
+ await maybe_awaitable (spam_checker .user_may_create_room (userid ))
107
+ is False
108
+ ):
98
109
return False
99
110
100
111
return True
101
112
102
- def user_may_create_room_alias (self , userid : str , room_alias : str ) -> bool :
113
+ async def user_may_create_room_alias (self , userid : str , room_alias : str ) -> bool :
103
114
"""Checks if a given user may create a room alias
104
115
105
116
If this method returns false, the association request will be rejected.
@@ -112,12 +123,17 @@ def user_may_create_room_alias(self, userid: str, room_alias: str) -> bool:
112
123
True if the user may create a room alias, otherwise False
113
124
"""
114
125
for spam_checker in self .spam_checkers :
115
- if spam_checker .user_may_create_room_alias (userid , room_alias ) is False :
126
+ if (
127
+ await maybe_awaitable (
128
+ spam_checker .user_may_create_room_alias (userid , room_alias )
129
+ )
130
+ is False
131
+ ):
116
132
return False
117
133
118
134
return True
119
135
120
- def user_may_publish_room (self , userid : str , room_id : str ) -> bool :
136
+ async def user_may_publish_room (self , userid : str , room_id : str ) -> bool :
121
137
"""Checks if a given user may publish a room to the directory
122
138
123
139
If this method returns false, the publish request will be rejected.
@@ -130,12 +146,17 @@ def user_may_publish_room(self, userid: str, room_id: str) -> bool:
130
146
True if the user may publish the room, otherwise False
131
147
"""
132
148
for spam_checker in self .spam_checkers :
133
- if spam_checker .user_may_publish_room (userid , room_id ) is False :
149
+ if (
150
+ await maybe_awaitable (
151
+ spam_checker .user_may_publish_room (userid , room_id )
152
+ )
153
+ is False
154
+ ):
134
155
return False
135
156
136
157
return True
137
158
138
- def check_username_for_spam (self , user_profile : Dict [str , str ]) -> bool :
159
+ async def check_username_for_spam (self , user_profile : Dict [str , str ]) -> bool :
139
160
"""Checks if a user ID or display name are considered "spammy" by this server.
140
161
141
162
If the server considers a username spammy, then it will not be included in
@@ -157,12 +178,12 @@ def check_username_for_spam(self, user_profile: Dict[str, str]) -> bool:
157
178
if checker :
158
179
# Make a copy of the user profile object to ensure the spam checker
159
180
# cannot modify it.
160
- if checker (user_profile .copy ()):
181
+ if await maybe_awaitable ( checker (user_profile .copy () )):
161
182
return True
162
183
163
184
return False
164
185
165
- def check_registration_for_spam (
186
+ async def check_registration_for_spam (
166
187
self ,
167
188
email_threepid : Optional [dict ],
168
189
username : Optional [str ],
@@ -185,7 +206,9 @@ def check_registration_for_spam(
185
206
# spam checker
186
207
checker = getattr (spam_checker , "check_registration_for_spam" , None )
187
208
if checker :
188
- behaviour = checker (email_threepid , username , request_info )
209
+ behaviour = await maybe_awaitable (
210
+ checker (email_threepid , username , request_info )
211
+ )
189
212
assert isinstance (behaviour , RegistrationBehaviour )
190
213
if behaviour != RegistrationBehaviour .ALLOW :
191
214
return behaviour
0 commit comments