1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15-
1615import logging
17- from typing import Dict , List , Optional , Union
16+ from typing import TYPE_CHECKING , Dict , List , Optional , Union
1817
1918from prometheus_client import Counter
2019
3433 run_as_background_process ,
3534 wrap_as_background_process ,
3635)
37- from synapse .types import Collection , JsonDict , RoomStreamToken , UserID
36+ from synapse .storage .databases .main .directory import RoomAliasMapping
37+ from synapse .types import Collection , JsonDict , RoomAlias , RoomStreamToken , UserID
3838from synapse .util .metrics import Measure
3939
40+ if TYPE_CHECKING :
41+ from synapse .app .homeserver import HomeServer
42+
4043logger = logging .getLogger (__name__ )
4144
4245events_processed_counter = Counter ("synapse_handlers_appservice_events_processed" , "" )
4346
4447
4548class ApplicationServicesHandler :
46- def __init__ (self , hs ):
49+ def __init__ (self , hs : "HomeServer" ):
4750 self .store = hs .get_datastore ()
4851 self .is_mine_id = hs .is_mine_id
4952 self .appservice_api = hs .get_application_service_api ()
@@ -247,7 +250,9 @@ async def _notify_interested_services_ephemeral(
247250 service , "presence" , new_token
248251 )
249252
250- async def _handle_typing (self , service : ApplicationService , new_token : int ):
253+ async def _handle_typing (
254+ self , service : ApplicationService , new_token : int
255+ ) -> List [JsonDict ]:
251256 typing_source = self .event_sources .sources ["typing" ]
252257 # Get the typing events from just before current
253258 typing , _ = await typing_source .get_new_events_as (
@@ -259,7 +264,7 @@ async def _handle_typing(self, service: ApplicationService, new_token: int):
259264 )
260265 return typing
261266
262- async def _handle_receipts (self , service : ApplicationService ):
267+ async def _handle_receipts (self , service : ApplicationService ) -> List [ JsonDict ] :
263268 from_key = await self .store .get_type_stream_id_for_appservice (
264269 service , "read_receipt"
265270 )
@@ -271,7 +276,7 @@ async def _handle_receipts(self, service: ApplicationService):
271276
272277 async def _handle_presence (
273278 self , service : ApplicationService , users : Collection [Union [str , UserID ]]
274- ):
279+ ) -> List [ JsonDict ] :
275280 events = [] # type: List[JsonDict]
276281 presence_source = self .event_sources .sources ["presence" ]
277282 from_key = await self .store .get_type_stream_id_for_appservice (
@@ -301,11 +306,11 @@ async def _handle_presence(
301306
302307 return events
303308
304- async def query_user_exists (self , user_id ) :
309+ async def query_user_exists (self , user_id : str ) -> bool :
305310 """Check if any application service knows this user_id exists.
306311
307312 Args:
308- user_id(str) : The user to query if they exist on any AS.
313+ user_id: The user to query if they exist on any AS.
309314 Returns:
310315 True if this user exists on at least one application service.
311316 """
@@ -316,11 +321,13 @@ async def query_user_exists(self, user_id):
316321 return True
317322 return False
318323
319- async def query_room_alias_exists (self , room_alias ):
324+ async def query_room_alias_exists (
325+ self , room_alias : RoomAlias
326+ ) -> Optional [RoomAliasMapping ]:
320327 """Check if an application service knows this room alias exists.
321328
322329 Args:
323- room_alias(RoomAlias) : The room alias to query.
330+ room_alias: The room alias to query.
324331 Returns:
325332 namedtuple: with keys "room_id" and "servers" or None if no
326333 association can be found.
@@ -336,10 +343,13 @@ async def query_room_alias_exists(self, room_alias):
336343 )
337344 if is_known_alias :
338345 # the alias exists now so don't query more ASes.
339- result = await self .store .get_association_from_room_alias (room_alias )
340- return result
346+ return await self .store .get_association_from_room_alias (room_alias )
347+
348+ return None
341349
342- async def query_3pe (self , kind , protocol , fields ):
350+ async def query_3pe (
351+ self , kind : str , protocol : str , fields : Dict [bytes , List [bytes ]]
352+ ) -> List [JsonDict ]:
343353 services = self ._get_services_for_3pn (protocol )
344354
345355 results = await make_deferred_yieldable (
@@ -361,7 +371,9 @@ async def query_3pe(self, kind, protocol, fields):
361371
362372 return ret
363373
364- async def get_3pe_protocols (self , only_protocol = None ):
374+ async def get_3pe_protocols (
375+ self , only_protocol : Optional [str ] = None
376+ ) -> Dict [str , JsonDict ]:
365377 services = self .store .get_app_services ()
366378 protocols = {} # type: Dict[str, List[JsonDict]]
367379
@@ -379,7 +391,7 @@ async def get_3pe_protocols(self, only_protocol=None):
379391 if info is not None :
380392 protocols [p ].append (info )
381393
382- def _merge_instances (infos ) :
394+ def _merge_instances (infos : List [ JsonDict ]) -> JsonDict :
383395 if not infos :
384396 return {}
385397
@@ -394,19 +406,17 @@ def _merge_instances(infos):
394406
395407 return combined
396408
397- for p in protocols .keys ():
398- protocols [p ] = _merge_instances (protocols [p ])
409+ return {p : _merge_instances (protocols [p ]) for p in protocols .keys ()}
399410
400- return protocols
401-
402- async def _get_services_for_event ( self , event ) :
411+ async def _get_services_for_event (
412+ self , event : EventBase
413+ ) -> List [ ApplicationService ] :
403414 """Retrieve a list of application services interested in this event.
404415
405416 Args:
406- event(Event) : The event to check. Can be None if alias_list is not.
417+ event: The event to check. Can be None if alias_list is not.
407418 Returns:
408- list<ApplicationService>: A list of services interested in this
409- event based on the service regex.
419+ A list of services interested in this event based on the service regex.
410420 """
411421 services = self .store .get_app_services ()
412422
@@ -420,17 +430,15 @@ async def _get_services_for_event(self, event):
420430
421431 return interested_list
422432
423- def _get_services_for_user (self , user_id ) :
433+ def _get_services_for_user (self , user_id : str ) -> List [ ApplicationService ] :
424434 services = self .store .get_app_services ()
425- interested_list = [s for s in services if (s .is_interested_in_user (user_id ))]
426- return interested_list
435+ return [s for s in services if (s .is_interested_in_user (user_id ))]
427436
428- def _get_services_for_3pn (self , protocol ) :
437+ def _get_services_for_3pn (self , protocol : str ) -> List [ ApplicationService ] :
429438 services = self .store .get_app_services ()
430- interested_list = [s for s in services if s .is_interested_in_protocol (protocol )]
431- return interested_list
439+ return [s for s in services if s .is_interested_in_protocol (protocol )]
432440
433- async def _is_unknown_user (self , user_id ) :
441+ async def _is_unknown_user (self , user_id : str ) -> bool :
434442 if not self .is_mine_id (user_id ):
435443 # we don't know if they are unknown or not since it isn't one of our
436444 # users. We can't poke ASes.
@@ -445,9 +453,8 @@ async def _is_unknown_user(self, user_id):
445453 service_list = [s for s in services if s .sender == user_id ]
446454 return len (service_list ) == 0
447455
448- async def _check_user_exists (self , user_id ) :
456+ async def _check_user_exists (self , user_id : str ) -> bool :
449457 unknown_user = await self ._is_unknown_user (user_id )
450458 if unknown_user :
451- exists = await self .query_user_exists (user_id )
452- return exists
459+ return await self .query_user_exists (user_id )
453460 return True
0 commit comments