Fix up types for the typing handler. (#9638)

By splitting this to two separate methods the callers know
what methods they can expect on the handler.
This commit is contained in:
Patrick Cloke 2021-03-17 11:30:21 -04:00 committed by GitHub
parent 73dbce5523
commit cc324d53fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 14 deletions

1
changelog.d/9638.misc Normal file
View File

@ -0,0 +1 @@
Add additional type hints to the Homeserver object.

View File

@ -33,7 +33,7 @@ import attr
from synapse.replication.http.streams import ReplicationGetStreamUpdates from synapse.replication.http.streams import ReplicationGetStreamUpdates
if TYPE_CHECKING: if TYPE_CHECKING:
import synapse.server from synapse.app.homeserver import HomeServer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -299,20 +299,23 @@ class TypingStream(Stream):
NAME = "typing" NAME = "typing"
ROW_TYPE = TypingStreamRow ROW_TYPE = TypingStreamRow
def __init__(self, hs): def __init__(self, hs: "HomeServer"):
typing_handler = hs.get_typing_handler()
writer_instance = hs.config.worker.writers.typing writer_instance = hs.config.worker.writers.typing
if writer_instance == hs.get_instance_name(): if writer_instance == hs.get_instance_name():
# On the writer, query the typing handler # On the writer, query the typing handler
update_function = typing_handler.get_all_typing_updates typing_writer_handler = hs.get_typing_writer_handler()
update_function = (
typing_writer_handler.get_all_typing_updates
) # type: Callable[[str, int, int, int], Awaitable[Tuple[List[Tuple[int, Any]], int, bool]]]
current_token_function = typing_writer_handler.get_current_token
else: else:
# Query the typing writer process # Query the typing writer process
update_function = make_http_update_function(hs, self.NAME) update_function = make_http_update_function(hs, self.NAME)
current_token_function = hs.get_typing_handler().get_current_token
super().__init__( super().__init__(
hs.get_instance_name(), hs.get_instance_name(),
current_token_without_instance(typing_handler.get_current_token), current_token_without_instance(current_token_function),
update_function, update_function,
) )
@ -509,7 +512,7 @@ class AccountDataStream(Stream):
NAME = "account_data" NAME = "account_data"
ROW_TYPE = AccountDataStreamRow ROW_TYPE = AccountDataStreamRow
def __init__(self, hs: "synapse.server.HomeServer"): def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastore() self.store = hs.get_datastore()
super().__init__( super().__init__(
hs.get_instance_name(), hs.get_instance_name(),

View File

@ -49,7 +49,7 @@ from synapse.util import json_decoder
from synapse.util.stringutils import parse_and_validate_server_name, random_string from synapse.util.stringutils import parse_and_validate_server_name, random_string
if TYPE_CHECKING: if TYPE_CHECKING:
import synapse.server from synapse.app.homeserver import HomeServer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -846,10 +846,10 @@ class RoomTypingRestServlet(RestServlet):
"/rooms/(?P<room_id>[^/]*)/typing/(?P<user_id>[^/]*)$", v1=True "/rooms/(?P<room_id>[^/]*)/typing/(?P<user_id>[^/]*)$", v1=True
) )
def __init__(self, hs): def __init__(self, hs: "HomeServer"):
super().__init__() super().__init__()
self.hs = hs
self.presence_handler = hs.get_presence_handler() self.presence_handler = hs.get_presence_handler()
self.typing_handler = hs.get_typing_handler()
self.auth = hs.get_auth() self.auth = hs.get_auth()
# If we're not on the typing writer instance we should scream if we get # If we're not on the typing writer instance we should scream if we get
@ -874,16 +874,19 @@ class RoomTypingRestServlet(RestServlet):
# Limit timeout to stop people from setting silly typing timeouts. # Limit timeout to stop people from setting silly typing timeouts.
timeout = min(content.get("timeout", 30000), 120000) timeout = min(content.get("timeout", 30000), 120000)
# Defer getting the typing handler since it will raise on workers.
typing_handler = self.hs.get_typing_writer_handler()
try: try:
if content["typing"]: if content["typing"]:
await self.typing_handler.started_typing( await typing_handler.started_typing(
target_user=target_user, target_user=target_user,
requester=requester, requester=requester,
room_id=room_id, room_id=room_id,
timeout=timeout, timeout=timeout,
) )
else: else:
await self.typing_handler.stopped_typing( await typing_handler.stopped_typing(
target_user=target_user, requester=requester, room_id=room_id target_user=target_user, requester=requester, room_id=room_id
) )
except ShadowBanError: except ShadowBanError:
@ -901,7 +904,7 @@ class RoomAliasListServlet(RestServlet):
), ),
] ]
def __init__(self, hs: "synapse.server.HomeServer"): def __init__(self, hs: "HomeServer"):
super().__init__() super().__init__()
self.auth = hs.get_auth() self.auth = hs.get_auth()
self.directory_handler = hs.get_directory_handler() self.directory_handler = hs.get_directory_handler()

View File

@ -417,9 +417,18 @@ class HomeServer(metaclass=abc.ABCMeta):
return PresenceHandler(self) return PresenceHandler(self)
@cache_in_self @cache_in_self
def get_typing_handler(self): def get_typing_writer_handler(self) -> TypingWriterHandler:
if self.config.worker.writers.typing == self.get_instance_name(): if self.config.worker.writers.typing == self.get_instance_name():
return TypingWriterHandler(self) return TypingWriterHandler(self)
else:
raise Exception("Workers cannot write typing")
@cache_in_self
def get_typing_handler(self) -> FollowerTypingHandler:
if self.config.worker.writers.typing == self.get_instance_name():
# Use get_typing_writer_handler to ensure that we use the same
# cached version.
return self.get_typing_writer_handler()
else: else:
return FollowerTypingHandler(self) return FollowerTypingHandler(self)