From d920737e0bb4ca2797ed1430c4020e61631eb01d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 2 May 2021 15:52:22 +0200 Subject: [PATCH 01/10] Prepare stripping keys from /sync Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 45 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index 95ee3f1b84f3..f5d5a2b1008e 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -14,6 +14,7 @@ import itertools import logging +from collections import defaultdict from typing import TYPE_CHECKING, Tuple from synapse.api.constants import PresenceState @@ -229,24 +230,32 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): ) logger.debug("building sync response dict") - return { - "account_data": {"events": sync_result.account_data}, - "to_device": {"events": sync_result.to_device}, - "device_lists": { - "changed": list(sync_result.device_lists.changed), - "left": list(sync_result.device_lists.left), - }, - "presence": SyncRestServlet.encode_presence(sync_result.presence, time_now), - "rooms": {"join": joined, "invite": invited, "leave": archived}, - "groups": { - "join": sync_result.groups.join, - "invite": sync_result.groups.invite, - "leave": sync_result.groups.leave, - }, - "device_one_time_keys_count": sync_result.device_one_time_keys_count, - "org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, - "next_batch": await sync_result.next_batch.to_string(self.store), - } + + response: dict = defaultdict(dict) + response["next_batch"] = await sync_result.next_batch.to_string(self.store) + response.update( + { + "account_data": {"events": sync_result.account_data}, + "to_device": {"events": sync_result.to_device}, + "device_lists": { + "changed": list(sync_result.device_lists.changed), + "left": list(sync_result.device_lists.left), + }, + "presence": SyncRestServlet.encode_presence( + sync_result.presence, time_now + ), + "rooms": {"join": joined, "invite": invited, "leave": archived}, + "groups": { + "join": sync_result.groups.join, + "invite": sync_result.groups.invite, + "leave": sync_result.groups.leave, + }, + "device_one_time_keys_count": sync_result.device_one_time_keys_count, + "org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, + } + ) + + return response @staticmethod def encode_presence(events, time_now): From df28b9c8550ca8eb9970976f62303dc08605592d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 18:40:26 +0200 Subject: [PATCH 02/10] Remove empty presence, account_data, to_device from /sync Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index f5d5a2b1008e..c04e7885d51f 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -235,15 +235,10 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): response["next_batch"] = await sync_result.next_batch.to_string(self.store) response.update( { - "account_data": {"events": sync_result.account_data}, - "to_device": {"events": sync_result.to_device}, "device_lists": { "changed": list(sync_result.device_lists.changed), "left": list(sync_result.device_lists.left), }, - "presence": SyncRestServlet.encode_presence( - sync_result.presence, time_now - ), "rooms": {"join": joined, "invite": invited, "leave": archived}, "groups": { "join": sync_result.groups.join, @@ -255,6 +250,16 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): } ) + if sync_result.account_data: + response["account_data"] = {"events": sync_result.account_data} + if sync_result.presence: + response["presence"] = SyncRestServlet.encode_presence( + sync_result.presence, time_now + ) + + if sync_result.to_device: + response["to_device"] = {"events": sync_result.to_device} + return response @staticmethod From f43807f535dd6fd63df6de37064395156c6e493b Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 19:31:25 +0200 Subject: [PATCH 03/10] Strip empty room key from /sync response Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index c04e7885d51f..fd9f86bc137e 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -239,7 +239,6 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): "changed": list(sync_result.device_lists.changed), "left": list(sync_result.device_lists.left), }, - "rooms": {"join": joined, "invite": invited, "leave": archived}, "groups": { "join": sync_result.groups.join, "invite": sync_result.groups.invite, @@ -260,6 +259,13 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): if sync_result.to_device: response["to_device"] = {"events": sync_result.to_device} + if joined: + response["rooms"]["join"] = joined + if invited: + response["rooms"]["invite"] = invited + if archived: + response["rooms"]["leave"] = archived + return response @staticmethod From bd8bd2434a43a3a709672d7d812239041569491c Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 19:42:02 +0200 Subject: [PATCH 04/10] Leave out groups key from /sync if empty Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index fd9f86bc137e..027ab5daede5 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -239,11 +239,6 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): "changed": list(sync_result.device_lists.changed), "left": list(sync_result.device_lists.left), }, - "groups": { - "join": sync_result.groups.join, - "invite": sync_result.groups.invite, - "leave": sync_result.groups.leave, - }, "device_one_time_keys_count": sync_result.device_one_time_keys_count, "org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, } @@ -266,6 +261,13 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): if archived: response["rooms"]["leave"] = archived + if sync_result.groups.join: + response["groups"]["join"] = sync_result.groups.join + if sync_result.groups.invite: + response["groups"]["invite"] = sync_result.groups.invite + if sync_result.groups.leave: + response["groups"]["leave"] = sync_result.groups.leave + return response @staticmethod From c07ee94e72550d13d4f658592c0e0ce1cad8391c Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 20:01:44 +0200 Subject: [PATCH 05/10] Leave out device_lists key from /sync if empty Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index 027ab5daede5..624388171def 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -235,10 +235,6 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): response["next_batch"] = await sync_result.next_batch.to_string(self.store) response.update( { - "device_lists": { - "changed": list(sync_result.device_lists.changed), - "left": list(sync_result.device_lists.left), - }, "device_one_time_keys_count": sync_result.device_one_time_keys_count, "org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, } @@ -254,6 +250,11 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): if sync_result.to_device: response["to_device"] = {"events": sync_result.to_device} + if sync_result.device_lists.changed: + response["device_lists"]["changed"] = list(sync_result.device_lists.changed) + if sync_result.device_lists.left: + response["device_lists"]["left"] = list(sync_result.device_lists.left) + if joined: response["rooms"]["join"] = joined if invited: From 0dc3c4a6232b310f95b1784dcfcf4a343695b872 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 20:16:09 +0200 Subject: [PATCH 06/10] Leave out key counts from /sync if empty Signed-off-by: Nicolas Werner --- synapse/rest/client/v2_alpha/sync.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/synapse/rest/client/v2_alpha/sync.py b/synapse/rest/client/v2_alpha/sync.py index 624388171def..5f8565333068 100644 --- a/synapse/rest/client/v2_alpha/sync.py +++ b/synapse/rest/client/v2_alpha/sync.py @@ -233,12 +233,6 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): response: dict = defaultdict(dict) response["next_batch"] = await sync_result.next_batch.to_string(self.store) - response.update( - { - "device_one_time_keys_count": sync_result.device_one_time_keys_count, - "org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, - } - ) if sync_result.account_data: response["account_data"] = {"events": sync_result.account_data} @@ -255,6 +249,15 @@ async def encode_response(self, time_now, sync_result, access_token_id, filter): if sync_result.device_lists.left: response["device_lists"]["left"] = list(sync_result.device_lists.left) + if sync_result.device_one_time_keys_count: + response[ + "device_one_time_keys_count" + ] = sync_result.device_one_time_keys_count + if sync_result.device_unused_fallback_key_types: + response[ + "org.matrix.msc2732.device_unused_fallback_key_types" + ] = sync_result.device_unused_fallback_key_types + if joined: response["rooms"]["join"] = joined if invited: From 3f10d9b0ed94999c786901ac4be1511f16489cd2 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sat, 1 May 2021 20:40:22 +0200 Subject: [PATCH 07/10] Add news entry --- changelog.d/9919.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/9919.feature diff --git a/changelog.d/9919.feature b/changelog.d/9919.feature new file mode 100644 index 000000000000..620eef5f9d5b --- /dev/null +++ b/changelog.d/9919.feature @@ -0,0 +1 @@ +Strip empty objects and list from the `/sync` response. From 8499004a9770cf48776be28400fab029bccf23b0 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 5 May 2021 14:35:49 +0200 Subject: [PATCH 08/10] Fix integration tests with left out keys in /sync Signed-off-by: Nicolas Werner --- tests/rest/client/v2_alpha/test_sync.py | 9 --------- .../test_resource_limits_server_notices.py | 8 +++++--- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/rest/client/v2_alpha/test_sync.py b/tests/rest/client/v2_alpha/test_sync.py index dbcbdf159a1a..14ea45a01fe3 100644 --- a/tests/rest/client/v2_alpha/test_sync.py +++ b/tests/rest/client/v2_alpha/test_sync.py @@ -40,11 +40,6 @@ def test_sync_argless(self): self.assertTrue( { "next_batch", - "rooms", - "presence", - "account_data", - "to_device", - "device_lists", }.issubset(set(channel.json_body.keys())) ) @@ -60,10 +55,6 @@ def test_sync_presence_disabled(self): self.assertTrue( { "next_batch", - "rooms", - "account_data", - "to_device", - "device_lists", }.issubset(set(channel.json_body.keys())) ) diff --git a/tests/server_notices/test_resource_limits_server_notices.py b/tests/server_notices/test_resource_limits_server_notices.py index d46521ccdc0f..d12c542d19eb 100644 --- a/tests/server_notices/test_resource_limits_server_notices.py +++ b/tests/server_notices/test_resource_limits_server_notices.py @@ -306,8 +306,9 @@ def test_no_invite_without_notice(self): channel = self.make_request("GET", "/sync?timeout=0", access_token=tok) - invites = channel.json_body["rooms"]["invite"] - self.assertEqual(len(invites), 0, invites) + self.assertFalse( + "rooms" in channel.json_body, "Got invites without server notice" + ) def test_invite_with_notice(self): """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): # We could also pick another user and sync with it, which would return an # invite to a system notices room, but it doesn't matter which user we're # using so we use the last one because it saves us an extra sync. - invites = channel.json_body["rooms"]["invite"] + if "rooms" in channel.json_body: + invites = channel.json_body["rooms"]["invite"] # Make sure we have an invite to process. self.assertEqual(len(invites), 1, invites) From 59a9155b29a952a0a3d8f6226e859f53ac5e3df4 Mon Sep 17 00:00:00 2001 From: "DeepBlueV7.X" Date: Wed, 5 May 2021 13:08:14 +0000 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> --- changelog.d/9919.feature | 2 +- tests/server_notices/test_resource_limits_server_notices.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog.d/9919.feature b/changelog.d/9919.feature index 620eef5f9d5b..07747505d243 100644 --- a/changelog.d/9919.feature +++ b/changelog.d/9919.feature @@ -1 +1 @@ -Strip empty objects and list from the `/sync` response. +Omit empty fields from the `/sync` response. Contributed by @deepbluev7. diff --git a/tests/server_notices/test_resource_limits_server_notices.py b/tests/server_notices/test_resource_limits_server_notices.py index d12c542d19eb..3245aa91ca6e 100644 --- a/tests/server_notices/test_resource_limits_server_notices.py +++ b/tests/server_notices/test_resource_limits_server_notices.py @@ -306,8 +306,8 @@ def test_no_invite_without_notice(self): channel = self.make_request("GET", "/sync?timeout=0", access_token=tok) - self.assertFalse( - "rooms" in channel.json_body, "Got invites without server notice" + self.assertNotIn( + "rooms", channel.json_body, "Got invites without server notice" ) def test_invite_with_notice(self): From 58ef25fcdc09bc60bf1b93c627cda4a0d22dfdd4 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 5 May 2021 15:10:03 +0200 Subject: [PATCH 10/10] Clean up argless sync test cases Signed-off-by: Nicolas Werner --- tests/rest/client/v2_alpha/test_sync.py | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/tests/rest/client/v2_alpha/test_sync.py b/tests/rest/client/v2_alpha/test_sync.py index 14ea45a01fe3..74be5176d054 100644 --- a/tests/rest/client/v2_alpha/test_sync.py +++ b/tests/rest/client/v2_alpha/test_sync.py @@ -37,26 +37,7 @@ def test_sync_argless(self): channel = self.make_request("GET", "/sync") self.assertEqual(channel.code, 200) - self.assertTrue( - { - "next_batch", - }.issubset(set(channel.json_body.keys())) - ) - - def test_sync_presence_disabled(self): - """ - When presence is disabled, the key does not appear in /sync. - """ - self.hs.config.use_presence = False - - channel = self.make_request("GET", "/sync") - - self.assertEqual(channel.code, 200) - self.assertTrue( - { - "next_batch", - }.issubset(set(channel.json_body.keys())) - ) + self.assertIn("next_batch", channel.json_body) class SyncFilterTestCase(unittest.HomeserverTestCase):