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

Commit e9eb354

Browse files
authored
Leave out optional keys from /sync (#9919)
This leaves out all optional keys from /sync. This should be fine for all clients tested against conduit already, but it may break some clients, as such we should check, that at least most of them don't break horribly and maybe back out some of the individual changes. (We can probably always leave out groups for example, while the others may cause more issues.) Signed-off-by: Nicolas Werner <nicolas.werner@hotmail.de>
1 parent a61b13c commit e9eb354

File tree

4 files changed

+51
-50
lines changed

4 files changed

+51
-50
lines changed

changelog.d/9919.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Omit empty fields from the `/sync` response. Contributed by @deepbluev7.

synapse/rest/client/v2_alpha/sync.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import itertools
1616
import logging
17+
from collections import defaultdict
1718
from typing import TYPE_CHECKING, Tuple
1819

1920
from synapse.api.constants import PresenceState
@@ -229,24 +230,49 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter):
229230
)
230231

231232
logger.debug("building sync response dict")
232-
return {
233-
"account_data": {"events": sync_result.account_data},
234-
"to_device": {"events": sync_result.to_device},
235-
"device_lists": {
236-
"changed": list(sync_result.device_lists.changed),
237-
"left": list(sync_result.device_lists.left),
238-
},
239-
"presence": SyncRestServlet.encode_presence(sync_result.presence, time_now),
240-
"rooms": {"join": joined, "invite": invited, "leave": archived},
241-
"groups": {
242-
"join": sync_result.groups.join,
243-
"invite": sync_result.groups.invite,
244-
"leave": sync_result.groups.leave,
245-
},
246-
"device_one_time_keys_count": sync_result.device_one_time_keys_count,
247-
"org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types,
248-
"next_batch": await sync_result.next_batch.to_string(self.store),
249-
}
233+
234+
response: dict = defaultdict(dict)
235+
response["next_batch"] = await sync_result.next_batch.to_string(self.store)
236+
237+
if sync_result.account_data:
238+
response["account_data"] = {"events": sync_result.account_data}
239+
if sync_result.presence:
240+
response["presence"] = SyncRestServlet.encode_presence(
241+
sync_result.presence, time_now
242+
)
243+
244+
if sync_result.to_device:
245+
response["to_device"] = {"events": sync_result.to_device}
246+
247+
if sync_result.device_lists.changed:
248+
response["device_lists"]["changed"] = list(sync_result.device_lists.changed)
249+
if sync_result.device_lists.left:
250+
response["device_lists"]["left"] = list(sync_result.device_lists.left)
251+
252+
if sync_result.device_one_time_keys_count:
253+
response[
254+
"device_one_time_keys_count"
255+
] = sync_result.device_one_time_keys_count
256+
if sync_result.device_unused_fallback_key_types:
257+
response[
258+
"org.matrix.msc2732.device_unused_fallback_key_types"
259+
] = sync_result.device_unused_fallback_key_types
260+
261+
if joined:
262+
response["rooms"]["join"] = joined
263+
if invited:
264+
response["rooms"]["invite"] = invited
265+
if archived:
266+
response["rooms"]["leave"] = archived
267+
268+
if sync_result.groups.join:
269+
response["groups"]["join"] = sync_result.groups.join
270+
if sync_result.groups.invite:
271+
response["groups"]["invite"] = sync_result.groups.invite
272+
if sync_result.groups.leave:
273+
response["groups"]["leave"] = sync_result.groups.leave
274+
275+
return response
250276

251277
@staticmethod
252278
def encode_presence(events, time_now):

tests/rest/client/v2_alpha/test_sync.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,7 @@ def test_sync_argless(self):
3737
channel = self.make_request("GET", "/sync")
3838

3939
self.assertEqual(channel.code, 200)
40-
self.assertTrue(
41-
{
42-
"next_batch",
43-
"rooms",
44-
"presence",
45-
"account_data",
46-
"to_device",
47-
"device_lists",
48-
}.issubset(set(channel.json_body.keys()))
49-
)
50-
51-
def test_sync_presence_disabled(self):
52-
"""
53-
When presence is disabled, the key does not appear in /sync.
54-
"""
55-
self.hs.config.use_presence = False
56-
57-
channel = self.make_request("GET", "/sync")
58-
59-
self.assertEqual(channel.code, 200)
60-
self.assertTrue(
61-
{
62-
"next_batch",
63-
"rooms",
64-
"account_data",
65-
"to_device",
66-
"device_lists",
67-
}.issubset(set(channel.json_body.keys()))
68-
)
40+
self.assertIn("next_batch", channel.json_body)
6941

7042

7143
class SyncFilterTestCase(unittest.HomeserverTestCase):

tests/server_notices/test_resource_limits_server_notices.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ def test_no_invite_without_notice(self):
306306

307307
channel = self.make_request("GET", "/sync?timeout=0", access_token=tok)
308308

309-
invites = channel.json_body["rooms"]["invite"]
310-
self.assertEqual(len(invites), 0, invites)
309+
self.assertNotIn(
310+
"rooms", channel.json_body, "Got invites without server notice"
311+
)
311312

312313
def test_invite_with_notice(self):
313314
"""Tests that, if the MAU limit is hit, the server notices user invites each user
@@ -364,7 +365,8 @@ def _trigger_notice_and_join(self):
364365
# We could also pick another user and sync with it, which would return an
365366
# invite to a system notices room, but it doesn't matter which user we're
366367
# using so we use the last one because it saves us an extra sync.
367-
invites = channel.json_body["rooms"]["invite"]
368+
if "rooms" in channel.json_body:
369+
invites = channel.json_body["rooms"]["invite"]
368370

369371
# Make sure we have an invite to process.
370372
self.assertEqual(len(invites), 1, invites)

0 commit comments

Comments
 (0)