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

Commit 1a209ef

Browse files
Update get_users_in_room mis-use to get hosts with dedicated get_current_hosts_in_room (#13605)
See #13575 (comment)
1 parent d58615c commit 1a209ef

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

changelog.d/13605.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor `get_users_in_room(room_id)` mis-use with dedicated `get_current_hosts_in_room(room_id)` function.

synapse/handlers/device.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def __init__(self, hs: "HomeServer"):
310310
super().__init__(hs)
311311

312312
self.federation_sender = hs.get_federation_sender()
313+
self._storage_controllers = hs.get_storage_controllers()
313314

314315
self.device_list_updater = DeviceListUpdater(hs, self)
315316

@@ -694,8 +695,11 @@ async def _handle_new_device_update_async(self) -> None:
694695

695696
# Ignore any users that aren't ours
696697
if self.hs.is_mine_id(user_id):
697-
joined_user_ids = await self.store.get_users_in_room(room_id)
698-
hosts = {get_domain_from_id(u) for u in joined_user_ids}
698+
hosts = set(
699+
await self._storage_controllers.state.get_current_hosts_in_room(
700+
room_id
701+
)
702+
)
699703
hosts.discard(self.server_name)
700704

701705
# Check if we've already sent this update to some hosts

synapse/handlers/directory.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from synapse.appservice import ApplicationService
3131
from synapse.module_api import NOT_SPAM
3232
from synapse.storage.databases.main.directory import RoomAliasMapping
33-
from synapse.types import JsonDict, Requester, RoomAlias, get_domain_from_id
33+
from synapse.types import JsonDict, Requester, RoomAlias
3434

3535
if TYPE_CHECKING:
3636
from synapse.server import HomeServer
@@ -83,8 +83,9 @@ async def _create_association(
8383
# TODO(erikj): Add transactions.
8484
# TODO(erikj): Check if there is a current association.
8585
if not servers:
86-
users = await self.store.get_users_in_room(room_id)
87-
servers = {get_domain_from_id(u) for u in users}
86+
servers = await self._storage_controllers.state.get_current_hosts_in_room(
87+
room_id
88+
)
8889

8990
if not servers:
9091
raise SynapseError(400, "Failed to get server list")
@@ -287,8 +288,9 @@ async def get_association(self, room_alias: RoomAlias) -> JsonDict:
287288
Codes.NOT_FOUND,
288289
)
289290

290-
users = await self.store.get_users_in_room(room_id)
291-
extra_servers = {get_domain_from_id(u) for u in users}
291+
extra_servers = await self._storage_controllers.state.get_current_hosts_in_room(
292+
room_id
293+
)
292294
servers_set = set(extra_servers) | set(servers)
293295

294296
# If this server is in the list of servers, return it first.

synapse/handlers/presence.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2051,8 +2051,7 @@ async def get_interested_remotes(
20512051
)
20522052

20532053
for room_id, states in room_ids_to_states.items():
2054-
user_ids = await store.get_users_in_room(room_id)
2055-
hosts = {get_domain_from_id(user_id) for user_id in user_ids}
2054+
hosts = await store.get_current_hosts_in_room(room_id)
20562055
for host in hosts:
20572056
hosts_and_states.setdefault(host, set()).update(states)
20582057

synapse/handlers/typing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from synapse.replication.tcp.streams import TypingStream
2828
from synapse.streams import EventSource
29-
from synapse.types import JsonDict, Requester, StreamKeyType, UserID, get_domain_from_id
29+
from synapse.types import JsonDict, Requester, StreamKeyType, UserID
3030
from synapse.util.caches.stream_change_cache import StreamChangeCache
3131
from synapse.util.metrics import Measure
3232
from synapse.util.wheel_timer import WheelTimer
@@ -362,8 +362,9 @@ async def _recv_edu(self, origin: str, content: JsonDict) -> None:
362362
)
363363
return
364364

365-
users = await self.store.get_users_in_room(room_id)
366-
domains = {get_domain_from_id(u) for u in users}
365+
domains = await self._storage_controllers.state.get_current_hosts_in_room(
366+
room_id
367+
)
367368

368369
if self.server_name in domains:
369370
logger.info("Got typing update from %s: %r", user_id, content)

tests/federation/test_federation_sender.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,24 @@ def default_config(self):
173173
return c
174174

175175
def prepare(self, reactor, clock, hs):
176-
# stub out `get_rooms_for_user` and `get_users_in_room` so that the
176+
test_room_id = "!room:host1"
177+
178+
# stub out `get_rooms_for_user` and `get_current_hosts_in_room` so that the
177179
# server thinks the user shares a room with `@user2:host2`
178180
def get_rooms_for_user(user_id):
179-
return defer.succeed({"!room:host1"})
181+
return defer.succeed({test_room_id})
180182

181183
hs.get_datastores().main.get_rooms_for_user = get_rooms_for_user
182184

183-
def get_users_in_room(room_id):
184-
return defer.succeed({"@user2:host2"})
185+
async def get_current_hosts_in_room(room_id):
186+
if room_id == test_room_id:
187+
return ["host2"]
188+
189+
# TODO: We should fail the test when we encounter an unxpected room ID.
190+
# We can't just use `self.fail(...)` here because the app code is greedy
191+
# with `Exception` and will catch it before the test can see it.
185192

186-
hs.get_datastores().main.get_users_in_room = get_users_in_room
193+
hs.get_datastores().main.get_current_hosts_in_room = get_current_hosts_in_room
187194

188195
# whenever send_transaction is called, record the edu data
189196
self.edus = []

0 commit comments

Comments
 (0)