1414
1515import logging
1616import string
17- from typing import Iterable , List , Optional
17+ from typing import TYPE_CHECKING , Iterable , List , Optional
1818
1919from synapse .api .constants import MAX_ALIAS_LENGTH , EventTypes
2020from synapse .api .errors import (
2727 SynapseError ,
2828)
2929from synapse .appservice import ApplicationService
30- from synapse .types import Requester , RoomAlias , UserID , get_domain_from_id
30+ from synapse .storage .databases .main .directory import RoomAliasMapping
31+ from synapse .types import JsonDict , Requester , RoomAlias , UserID , get_domain_from_id
3132
3233from ._base import BaseHandler
3334
35+ if TYPE_CHECKING :
36+ from synapse .server import HomeServer
37+
3438logger = logging .getLogger (__name__ )
3539
3640
3741class DirectoryHandler (BaseHandler ):
38- def __init__ (self , hs ):
42+ def __init__ (self , hs : "HomeServer" ):
3943 super ().__init__ (hs )
4044
4145 self .state = hs .get_state_handler ()
@@ -60,7 +64,7 @@ async def _create_association(
6064 room_id : str ,
6165 servers : Optional [Iterable [str ]] = None ,
6266 creator : Optional [str ] = None ,
63- ):
67+ ) -> None :
6468 # general association creation for both human users and app services
6569
6670 for wchar in string .whitespace :
@@ -104,8 +108,9 @@ async def create_association(
104108 """
105109
106110 user_id = requester .user .to_string ()
111+ room_alias_str = room_alias .to_string ()
107112
108- if len (room_alias . to_string () ) > MAX_ALIAS_LENGTH :
113+ if len (room_alias_str ) > MAX_ALIAS_LENGTH :
109114 raise SynapseError (
110115 400 ,
111116 "Can't create aliases longer than %s characters" % MAX_ALIAS_LENGTH ,
@@ -114,7 +119,7 @@ async def create_association(
114119
115120 service = requester .app_service
116121 if service :
117- if not service .is_interested_in_alias (room_alias . to_string () ):
122+ if not service .is_interested_in_alias (room_alias_str ):
118123 raise SynapseError (
119124 400 ,
120125 "This application service has not reserved this kind of alias." ,
@@ -138,7 +143,7 @@ async def create_association(
138143 raise AuthError (403 , "This user is not permitted to create this alias" )
139144
140145 if not self .config .is_alias_creation_allowed (
141- user_id , room_id , room_alias . to_string ()
146+ user_id , room_id , room_alias_str
142147 ):
143148 # Lets just return a generic message, as there may be all sorts of
144149 # reasons why we said no. TODO: Allow configurable error messages
@@ -211,7 +216,7 @@ async def delete_association(
211216
212217 async def delete_appservice_association (
213218 self , service : ApplicationService , room_alias : RoomAlias
214- ):
219+ ) -> None :
215220 if not service .is_interested_in_alias (room_alias .to_string ()):
216221 raise SynapseError (
217222 400 ,
@@ -220,25 +225,27 @@ async def delete_appservice_association(
220225 )
221226 await self ._delete_association (room_alias )
222227
223- async def _delete_association (self , room_alias : RoomAlias ):
228+ async def _delete_association (self , room_alias : RoomAlias ) -> str :
224229 if not self .hs .is_mine (room_alias ):
225230 raise SynapseError (400 , "Room alias must be local" )
226231
227232 room_id = await self .store .delete_room_alias (room_alias )
228233
229234 return room_id
230235
231- async def get_association (self , room_alias : RoomAlias ):
236+ async def get_association (self , room_alias : RoomAlias ) -> JsonDict :
232237 room_id = None
233238 if self .hs .is_mine (room_alias ):
234- result = await self .get_association_from_room_alias (room_alias )
239+ result = await self .get_association_from_room_alias (
240+ room_alias
241+ ) # type: Optional[RoomAliasMapping]
235242
236243 if result :
237244 room_id = result .room_id
238245 servers = result .servers
239246 else :
240247 try :
241- result = await self .federation .make_query (
248+ fed_result = await self .federation .make_query (
242249 destination = room_alias .domain ,
243250 query_type = "directory" ,
244251 args = {"room_alias" : room_alias .to_string ()},
@@ -248,13 +255,13 @@ async def get_association(self, room_alias: RoomAlias):
248255 except CodeMessageException as e :
249256 logging .warning ("Error retrieving alias" )
250257 if e .code == 404 :
251- result = None
258+ fed_result = None
252259 else :
253260 raise
254261
255- if result and "room_id" in result and "servers" in result :
256- room_id = result ["room_id" ]
257- servers = result ["servers" ]
262+ if fed_result and "room_id" in fed_result and "servers" in fed_result :
263+ room_id = fed_result ["room_id" ]
264+ servers = fed_result ["servers" ]
258265
259266 if not room_id :
260267 raise SynapseError (
@@ -275,7 +282,7 @@ async def get_association(self, room_alias: RoomAlias):
275282
276283 return {"room_id" : room_id , "servers" : servers }
277284
278- async def on_directory_query (self , args ) :
285+ async def on_directory_query (self , args : JsonDict ) -> JsonDict :
279286 room_alias = RoomAlias .from_string (args ["room_alias" ])
280287 if not self .hs .is_mine (room_alias ):
281288 raise SynapseError (400 , "Room Alias is not hosted on this homeserver" )
@@ -293,7 +300,7 @@ async def on_directory_query(self, args):
293300
294301 async def _update_canonical_alias (
295302 self , requester : Requester , user_id : str , room_id : str , room_alias : RoomAlias
296- ):
303+ ) -> None :
297304 """
298305 Send an updated canonical alias event if the removed alias was set as
299306 the canonical alias or listed in the alt_aliases field.
@@ -344,7 +351,9 @@ async def _update_canonical_alias(
344351 ratelimit = False ,
345352 )
346353
347- async def get_association_from_room_alias (self , room_alias : RoomAlias ):
354+ async def get_association_from_room_alias (
355+ self , room_alias : RoomAlias
356+ ) -> Optional [RoomAliasMapping ]:
348357 result = await self .store .get_association_from_room_alias (room_alias )
349358 if not result :
350359 # Query AS to see if it exists
@@ -372,7 +381,7 @@ def can_modify_alias(self, alias: RoomAlias, user_id: Optional[str] = None) -> b
372381 # either no interested services, or no service with an exclusive lock
373382 return True
374383
375- async def _user_can_delete_alias (self , alias : RoomAlias , user_id : str ):
384+ async def _user_can_delete_alias (self , alias : RoomAlias , user_id : str ) -> bool :
376385 """Determine whether a user can delete an alias.
377386
378387 One of the following must be true:
@@ -394,14 +403,13 @@ async def _user_can_delete_alias(self, alias: RoomAlias, user_id: str):
394403 if not room_id :
395404 return False
396405
397- res = await self .auth .check_can_change_room_list (
406+ return await self .auth .check_can_change_room_list (
398407 room_id , UserID .from_string (user_id )
399408 )
400- return res
401409
402410 async def edit_published_room_list (
403411 self , requester : Requester , room_id : str , visibility : str
404- ):
412+ ) -> None :
405413 """Edit the entry of the room in the published room list.
406414
407415 requester
@@ -469,7 +477,7 @@ async def edit_published_room_list(
469477
470478 async def edit_published_appservice_room_list (
471479 self , appservice_id : str , network_id : str , room_id : str , visibility : str
472- ):
480+ ) -> None :
473481 """Add or remove a room from the appservice/network specific public
474482 room list.
475483
@@ -499,5 +507,4 @@ async def get_aliases_for_room(
499507 room_id , requester .user .to_string ()
500508 )
501509
502- aliases = await self .store .get_aliases_for_room (room_id )
503- return aliases
510+ return await self .store .get_aliases_for_room (room_id )
0 commit comments