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

Fix up types for the typing handler. #9638

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/9638.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add additional type hints to the Homeserver object.
17 changes: 10 additions & 7 deletions synapse/replication/tcp/streams/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from synapse.replication.http.streams import ReplicationGetStreamUpdates

if TYPE_CHECKING:
import synapse.server
from synapse.app.homeserver import HomeServer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -299,20 +299,23 @@ class TypingStream(Stream):
NAME = "typing"
ROW_TYPE = TypingStreamRow

def __init__(self, hs):
typing_handler = hs.get_typing_handler()

def __init__(self, hs: "HomeServer"):
writer_instance = hs.config.worker.writers.typing
if writer_instance == hs.get_instance_name():
# 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:
# Query the typing writer process
update_function = make_http_update_function(hs, self.NAME)
current_token_function = hs.get_typing_handler().get_current_token

super().__init__(
hs.get_instance_name(),
current_token_without_instance(typing_handler.get_current_token),
current_token_without_instance(current_token_function),
update_function,
)

Expand Down Expand Up @@ -509,7 +512,7 @@ class AccountDataStream(Stream):
NAME = "account_data"
ROW_TYPE = AccountDataStreamRow

def __init__(self, hs: "synapse.server.HomeServer"):
def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastore()
super().__init__(
hs.get_instance_name(),
Expand Down
15 changes: 9 additions & 6 deletions synapse/rest/client/v1/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from synapse.util.stringutils import parse_and_validate_server_name, random_string

if TYPE_CHECKING:
import synapse.server
from synapse.app.homeserver import HomeServer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -846,10 +846,10 @@ class RoomTypingRestServlet(RestServlet):
"/rooms/(?P<room_id>[^/]*)/typing/(?P<user_id>[^/]*)$", v1=True
)

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
super().__init__()
self.hs = hs
self.presence_handler = hs.get_presence_handler()
self.typing_handler = hs.get_typing_handler()
self.auth = hs.get_auth()

# If we're not on the typing writer instance we should scream if we get
Expand All @@ -874,16 +874,19 @@ async def on_PUT(self, request, room_id, user_id):
# Limit timeout to stop people from setting silly typing timeouts.
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()
Comment on lines +877 to +878
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annoying, but the other option I came up with was to not register the endpoint on other works which people said was more confusing.


try:
if content["typing"]:
await self.typing_handler.started_typing(
await typing_handler.started_typing(
target_user=target_user,
requester=requester,
room_id=room_id,
timeout=timeout,
)
else:
await self.typing_handler.stopped_typing(
await typing_handler.stopped_typing(
target_user=target_user, requester=requester, room_id=room_id
)
except ShadowBanError:
Expand All @@ -901,7 +904,7 @@ class RoomAliasListServlet(RestServlet):
),
]

def __init__(self, hs: "synapse.server.HomeServer"):
def __init__(self, hs: "HomeServer"):
super().__init__()
self.auth = hs.get_auth()
self.directory_handler = hs.get_directory_handler()
Expand Down
11 changes: 10 additions & 1 deletion synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,18 @@ def get_presence_handler(self) -> PresenceHandler:
return PresenceHandler(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():
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:
return FollowerTypingHandler(self)

Expand Down