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

Commit 5f81c0c

Browse files
authored
Add/Unerase annotations to Module API (#11341)
1 parent 433ee15 commit 5f81c0c

File tree

2 files changed

+135
-23
lines changed

2 files changed

+135
-23
lines changed

changelog.d/11341.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type annotations for some methods and properties in the module API.

synapse/module_api/__init__.py

+134-23
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,44 @@
3535

3636
from synapse.api.errors import SynapseError
3737
from synapse.events import EventBase
38-
from synapse.events.presence_router import PresenceRouter
38+
from synapse.events.presence_router import (
39+
GET_INTERESTED_USERS_CALLBACK,
40+
GET_USERS_FOR_STATES_CALLBACK,
41+
PresenceRouter,
42+
)
43+
from synapse.events.spamcheck import (
44+
CHECK_EVENT_FOR_SPAM_CALLBACK,
45+
CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK,
46+
CHECK_REGISTRATION_FOR_SPAM_CALLBACK,
47+
CHECK_USERNAME_FOR_SPAM_CALLBACK,
48+
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK,
49+
USER_MAY_CREATE_ROOM_CALLBACK,
50+
USER_MAY_CREATE_ROOM_WITH_INVITES_CALLBACK,
51+
USER_MAY_INVITE_CALLBACK,
52+
USER_MAY_JOIN_ROOM_CALLBACK,
53+
USER_MAY_PUBLISH_ROOM_CALLBACK,
54+
USER_MAY_SEND_3PID_INVITE_CALLBACK,
55+
)
56+
from synapse.events.third_party_rules import (
57+
CHECK_EVENT_ALLOWED_CALLBACK,
58+
CHECK_THREEPID_CAN_BE_INVITED_CALLBACK,
59+
CHECK_VISIBILITY_CAN_BE_MODIFIED_CALLBACK,
60+
ON_CREATE_ROOM_CALLBACK,
61+
ON_NEW_EVENT_CALLBACK,
62+
)
63+
from synapse.handlers.account_validity import (
64+
IS_USER_EXPIRED_CALLBACK,
65+
ON_LEGACY_ADMIN_REQUEST,
66+
ON_LEGACY_RENEW_CALLBACK,
67+
ON_LEGACY_SEND_MAIL_CALLBACK,
68+
ON_USER_REGISTRATION_CALLBACK,
69+
)
70+
from synapse.handlers.auth import (
71+
CHECK_3PID_AUTH_CALLBACK,
72+
CHECK_AUTH_CALLBACK,
73+
ON_LOGGED_OUT_CALLBACK,
74+
AuthHandler,
75+
)
3976
from synapse.http.client import SimpleHttpClient
4077
from synapse.http.server import (
4178
DirectServeHtmlResource,
@@ -114,7 +151,7 @@ class ModuleApi:
114151
can register new users etc if necessary.
115152
"""
116153

117-
def __init__(self, hs: "HomeServer", auth_handler):
154+
def __init__(self, hs: "HomeServer", auth_handler: AuthHandler) -> None:
118155
self._hs = hs
119156

120157
# TODO: Fix this type hint once the types for the data stores have been ironed
@@ -156,45 +193,119 @@ def __init__(self, hs: "HomeServer", auth_handler):
156193
#################################################################################
157194
# The following methods should only be called during the module's initialisation.
158195

159-
@property
160-
def register_spam_checker_callbacks(self):
196+
def register_spam_checker_callbacks(
197+
self,
198+
check_event_for_spam: Optional[CHECK_EVENT_FOR_SPAM_CALLBACK] = None,
199+
user_may_join_room: Optional[USER_MAY_JOIN_ROOM_CALLBACK] = None,
200+
user_may_invite: Optional[USER_MAY_INVITE_CALLBACK] = None,
201+
user_may_send_3pid_invite: Optional[USER_MAY_SEND_3PID_INVITE_CALLBACK] = None,
202+
user_may_create_room: Optional[USER_MAY_CREATE_ROOM_CALLBACK] = None,
203+
user_may_create_room_with_invites: Optional[
204+
USER_MAY_CREATE_ROOM_WITH_INVITES_CALLBACK
205+
] = None,
206+
user_may_create_room_alias: Optional[
207+
USER_MAY_CREATE_ROOM_ALIAS_CALLBACK
208+
] = None,
209+
user_may_publish_room: Optional[USER_MAY_PUBLISH_ROOM_CALLBACK] = None,
210+
check_username_for_spam: Optional[CHECK_USERNAME_FOR_SPAM_CALLBACK] = None,
211+
check_registration_for_spam: Optional[
212+
CHECK_REGISTRATION_FOR_SPAM_CALLBACK
213+
] = None,
214+
check_media_file_for_spam: Optional[CHECK_MEDIA_FILE_FOR_SPAM_CALLBACK] = None,
215+
) -> None:
161216
"""Registers callbacks for spam checking capabilities.
162217
163218
Added in Synapse v1.37.0.
164219
"""
165-
return self._spam_checker.register_callbacks
220+
return self._spam_checker.register_callbacks(
221+
check_event_for_spam=check_event_for_spam,
222+
user_may_join_room=user_may_join_room,
223+
user_may_invite=user_may_invite,
224+
user_may_send_3pid_invite=user_may_send_3pid_invite,
225+
user_may_create_room=user_may_create_room,
226+
user_may_create_room_with_invites=user_may_create_room_with_invites,
227+
user_may_create_room_alias=user_may_create_room_alias,
228+
user_may_publish_room=user_may_publish_room,
229+
check_username_for_spam=check_username_for_spam,
230+
check_registration_for_spam=check_registration_for_spam,
231+
check_media_file_for_spam=check_media_file_for_spam,
232+
)
166233

167-
@property
168-
def register_account_validity_callbacks(self):
234+
def register_account_validity_callbacks(
235+
self,
236+
is_user_expired: Optional[IS_USER_EXPIRED_CALLBACK] = None,
237+
on_user_registration: Optional[ON_USER_REGISTRATION_CALLBACK] = None,
238+
on_legacy_send_mail: Optional[ON_LEGACY_SEND_MAIL_CALLBACK] = None,
239+
on_legacy_renew: Optional[ON_LEGACY_RENEW_CALLBACK] = None,
240+
on_legacy_admin_request: Optional[ON_LEGACY_ADMIN_REQUEST] = None,
241+
) -> None:
169242
"""Registers callbacks for account validity capabilities.
170243
171244
Added in Synapse v1.39.0.
172245
"""
173-
return self._account_validity_handler.register_account_validity_callbacks
246+
return self._account_validity_handler.register_account_validity_callbacks(
247+
is_user_expired=is_user_expired,
248+
on_user_registration=on_user_registration,
249+
on_legacy_send_mail=on_legacy_send_mail,
250+
on_legacy_renew=on_legacy_renew,
251+
on_legacy_admin_request=on_legacy_admin_request,
252+
)
174253

175-
@property
176-
def register_third_party_rules_callbacks(self):
254+
def register_third_party_rules_callbacks(
255+
self,
256+
check_event_allowed: Optional[CHECK_EVENT_ALLOWED_CALLBACK] = None,
257+
on_create_room: Optional[ON_CREATE_ROOM_CALLBACK] = None,
258+
check_threepid_can_be_invited: Optional[
259+
CHECK_THREEPID_CAN_BE_INVITED_CALLBACK
260+
] = None,
261+
check_visibility_can_be_modified: Optional[
262+
CHECK_VISIBILITY_CAN_BE_MODIFIED_CALLBACK
263+
] = None,
264+
on_new_event: Optional[ON_NEW_EVENT_CALLBACK] = None,
265+
) -> None:
177266
"""Registers callbacks for third party event rules capabilities.
178267
179268
Added in Synapse v1.39.0.
180269
"""
181-
return self._third_party_event_rules.register_third_party_rules_callbacks
270+
return self._third_party_event_rules.register_third_party_rules_callbacks(
271+
check_event_allowed=check_event_allowed,
272+
on_create_room=on_create_room,
273+
check_threepid_can_be_invited=check_threepid_can_be_invited,
274+
check_visibility_can_be_modified=check_visibility_can_be_modified,
275+
on_new_event=on_new_event,
276+
)
182277

183-
@property
184-
def register_presence_router_callbacks(self):
278+
def register_presence_router_callbacks(
279+
self,
280+
get_users_for_states: Optional[GET_USERS_FOR_STATES_CALLBACK] = None,
281+
get_interested_users: Optional[GET_INTERESTED_USERS_CALLBACK] = None,
282+
) -> None:
185283
"""Registers callbacks for presence router capabilities.
186284
187285
Added in Synapse v1.42.0.
188286
"""
189-
return self._presence_router.register_presence_router_callbacks
287+
return self._presence_router.register_presence_router_callbacks(
288+
get_users_for_states=get_users_for_states,
289+
get_interested_users=get_interested_users,
290+
)
190291

191-
@property
192-
def register_password_auth_provider_callbacks(self):
292+
def register_password_auth_provider_callbacks(
293+
self,
294+
check_3pid_auth: Optional[CHECK_3PID_AUTH_CALLBACK] = None,
295+
on_logged_out: Optional[ON_LOGGED_OUT_CALLBACK] = None,
296+
auth_checkers: Optional[
297+
Dict[Tuple[str, Tuple[str, ...]], CHECK_AUTH_CALLBACK]
298+
] = None,
299+
) -> None:
193300
"""Registers callbacks for password auth provider capabilities.
194301
195302
Added in Synapse v1.46.0.
196303
"""
197-
return self._password_auth_provider.register_password_auth_provider_callbacks
304+
return self._password_auth_provider.register_password_auth_provider_callbacks(
305+
check_3pid_auth=check_3pid_auth,
306+
on_logged_out=on_logged_out,
307+
auth_checkers=auth_checkers,
308+
)
198309

199310
def register_web_resource(self, path: str, resource: Resource):
200311
"""Registers a web resource to be served at the given path.
@@ -216,7 +327,7 @@ def register_web_resource(self, path: str, resource: Resource):
216327
# The following methods can be called by the module at any point in time.
217328

218329
@property
219-
def http_client(self):
330+
def http_client(self) -> SimpleHttpClient:
220331
"""Allows making outbound HTTP requests to remote resources.
221332
222333
An instance of synapse.http.client.SimpleHttpClient
@@ -226,7 +337,7 @@ def http_client(self):
226337
return self._http_client
227338

228339
@property
229-
def public_room_list_manager(self):
340+
def public_room_list_manager(self) -> "PublicRoomListManager":
230341
"""Allows adding to, removing from and checking the status of rooms in the
231342
public room list.
232343
@@ -309,7 +420,7 @@ async def is_user_admin(self, user_id: str) -> bool:
309420
"""
310421
return await self._store.is_server_admin(UserID.from_string(user_id))
311422

312-
def get_qualified_user_id(self, username):
423+
def get_qualified_user_id(self, username: str) -> str:
313424
"""Qualify a user id, if necessary
314425
315426
Takes a user id provided by the user and adds the @ and :domain to
@@ -318,7 +429,7 @@ def get_qualified_user_id(self, username):
318429
Added in Synapse v0.25.0.
319430
320431
Args:
321-
username (str): provided user id
432+
username: provided user id
322433
323434
Returns:
324435
str: qualified @user:id
@@ -357,13 +468,13 @@ async def get_threepids_for_user(self, user_id: str) -> List[Dict[str, str]]:
357468
"""
358469
return await self._store.user_get_threepids(user_id)
359470

360-
def check_user_exists(self, user_id):
471+
def check_user_exists(self, user_id: str):
361472
"""Check if user exists.
362473
363474
Added in Synapse v0.25.0.
364475
365476
Args:
366-
user_id (str): Complete @user:id
477+
user_id: Complete @user:id
367478
368479
Returns:
369480
Deferred[str|None]: Canonical (case-corrected) user_id, or None

0 commit comments

Comments
 (0)