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

Commit 3946730

Browse files
Re-introduce "Leave out optional keys from /sync" change (#10214)
Required some fixes due to merge conflicts with #6739, but nothing too hairy. The first commit is the same as the original (after merge conflict resolution) then two more for compatibility with the latest sync code.
1 parent e19e3d4 commit 3946730

File tree

4 files changed

+53
-55
lines changed

4 files changed

+53
-55
lines changed

changelog.d/10214.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: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import itertools
1515
import logging
16+
from collections import defaultdict
1617
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple
1718

1819
from synapse.api.constants import Membership, PresenceState
@@ -232,29 +233,51 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter):
232233
)
233234

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

259282
@staticmethod
260283
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
@@ -41,35 +41,7 @@ def test_sync_argless(self):
4141
channel = self.make_request("GET", "/sync")
4242

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

7446

7547
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)