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

Commit 0b014eb

Browse files
authored
Only send out device list updates for our own users (#12465)
Broke in #12365
1 parent 535a689 commit 0b014eb

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

changelog.d/12465.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enable processing of device list updates asynchronously.

synapse/handlers/device.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,13 @@ async def _handle_new_device_update_async(self) -> None:
649649
return
650650

651651
for user_id, device_id, room_id, stream_id, opentracing_context in rows:
652-
joined_user_ids = await self.store.get_users_in_room(room_id)
653-
hosts = {get_domain_from_id(u) for u in joined_user_ids}
654-
hosts.discard(self.server_name)
652+
hosts = set()
653+
654+
# Ignore any users that aren't ours
655+
if self.hs.is_mine_id(user_id):
656+
joined_user_ids = await self.store.get_users_in_room(room_id)
657+
hosts = {get_domain_from_id(u) for u in joined_user_ids}
658+
hosts.discard(self.server_name)
655659

656660
# Check if we've already sent this update to some hosts
657661
if current_stream_id == stream_id:

synapse/storage/databases/main/devices.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,9 @@ def _add_device_outbound_poke_to_stream_txn(
17031703
next(stream_id_iterator),
17041704
user_id,
17051705
device_id,
1706-
False,
1706+
not self.hs.is_mine_id(
1707+
user_id
1708+
), # We only need to send out update for *our* users
17071709
now,
17081710
encoded_context if whitelisted_homeserver(destination) else "{}",
17091711
)

tests/federation/test_federation_sender.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ class FederationSenderDevicesTestCases(HomeserverTestCase):
162162

163163
def make_homeserver(self, reactor, clock):
164164
return self.setup_test_homeserver(
165-
federation_transport_client=Mock(spec=["send_transaction"]),
165+
federation_transport_client=Mock(
166+
spec=["send_transaction", "query_user_devices"]
167+
),
166168
)
167169

168170
def default_config(self):
@@ -218,6 +220,45 @@ def test_send_device_updates(self):
218220
self.assertEqual(len(self.edus), 1)
219221
self.check_device_update_edu(self.edus.pop(0), u1, "D2", stream_id)
220222

223+
def test_dont_send_device_updates_for_remote_users(self):
224+
"""Check that we don't send device updates for remote users"""
225+
226+
# Send the server a device list EDU for the other user, this will cause
227+
# it to try and resync the device lists.
228+
self.hs.get_federation_transport_client().query_user_devices.return_value = (
229+
defer.succeed(
230+
{
231+
"stream_id": "1",
232+
"user_id": "@user2:host2",
233+
"devices": [{"device_id": "D1"}],
234+
}
235+
)
236+
)
237+
238+
self.get_success(
239+
self.hs.get_device_handler().device_list_updater.incoming_device_list_update(
240+
"host2",
241+
{
242+
"user_id": "@user2:host2",
243+
"device_id": "D1",
244+
"stream_id": "1",
245+
"prev_ids": [],
246+
},
247+
)
248+
)
249+
250+
self.reactor.advance(1)
251+
252+
# We shouldn't see an EDU for that update
253+
self.assertEqual(self.edus, [])
254+
255+
# Check that we did successfully process the inbound EDU (otherwise this
256+
# test would pass if we failed to process the EDU)
257+
devices = self.get_success(
258+
self.hs.get_datastores().main.get_cached_devices_for_user("@user2:host2")
259+
)
260+
self.assertIn("D1", devices)
261+
221262
def test_upload_signatures(self):
222263
"""Uploading signatures on some devices should produce updates for that user"""
223264

tests/storage/test_devices.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_get_device_updates_by_remote(self):
118118
device_ids = ["device_id1", "device_id2"]
119119

120120
# Add two device updates with sequential `stream_id`s
121-
self.add_device_change("user_id", device_ids, "somehost")
121+
self.add_device_change("@user_id:test", device_ids, "somehost")
122122

123123
# Get all device updates ever meant for this remote
124124
now_stream_id, device_updates = self.get_success(
@@ -142,7 +142,7 @@ def test_get_device_updates_by_remote_can_limit_properly(self):
142142
"device_id4",
143143
"device_id5",
144144
]
145-
self.add_device_change("user_id", device_ids, "somehost")
145+
self.add_device_change("@user_id:test", device_ids, "somehost")
146146

147147
# Get device updates meant for this remote
148148
next_stream_id, device_updates = self.get_success(
@@ -162,7 +162,7 @@ def test_get_device_updates_by_remote_can_limit_properly(self):
162162

163163
# Add some more device updates to ensure it still resumes properly
164164
device_ids = ["device_id6", "device_id7"]
165-
self.add_device_change("user_id", device_ids, "somehost")
165+
self.add_device_change("@user_id:test", device_ids, "somehost")
166166

167167
# Get the next batch of device updates
168168
next_stream_id, device_updates = self.get_success(

0 commit comments

Comments
 (0)