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.
pull/10242/head
Andrew Morgan 2021-06-23 15:57:41 +01:00 committed by GitHub
parent e19e3d452d
commit 394673055d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 55 deletions

View File

@ -0,0 +1 @@
Omit empty fields from the `/sync` response. Contributed by @deepbluev7.

View File

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import itertools import itertools
import logging import logging
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple
from synapse.api.constants import Membership, PresenceState from synapse.api.constants import Membership, PresenceState
@ -232,29 +233,51 @@ class SyncRestServlet(RestServlet):
) )
logger.debug("building sync response dict") logger.debug("building sync response dict")
return {
"account_data": {"events": sync_result.account_data}, response: dict = defaultdict(dict)
"to_device": {"events": sync_result.to_device}, response["next_batch"] = await sync_result.next_batch.to_string(self.store)
"device_lists": {
"changed": list(sync_result.device_lists.changed), if sync_result.account_data:
"left": list(sync_result.device_lists.left), response["account_data"] = {"events": sync_result.account_data}
}, if sync_result.presence:
"presence": SyncRestServlet.encode_presence(sync_result.presence, time_now), response["presence"] = SyncRestServlet.encode_presence(
"rooms": { sync_result.presence, time_now
Membership.JOIN: joined, )
Membership.INVITE: invited,
Membership.KNOCK: knocked, if sync_result.to_device:
Membership.LEAVE: archived, response["to_device"] = {"events": sync_result.to_device}
},
"groups": { if sync_result.device_lists.changed:
Membership.JOIN: sync_result.groups.join, response["device_lists"]["changed"] = list(sync_result.device_lists.changed)
Membership.INVITE: sync_result.groups.invite, if sync_result.device_lists.left:
Membership.LEAVE: sync_result.groups.leave, response["device_lists"]["left"] = list(sync_result.device_lists.left)
},
"device_one_time_keys_count": sync_result.device_one_time_keys_count, if sync_result.device_one_time_keys_count:
"org.matrix.msc2732.device_unused_fallback_key_types": sync_result.device_unused_fallback_key_types, response[
"next_batch": await sync_result.next_batch.to_string(self.store), "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"][Membership.JOIN] = joined
if invited:
response["rooms"][Membership.INVITE] = invited
if knocked:
response["rooms"][Membership.KNOCK] = knocked
if archived:
response["rooms"][Membership.LEAVE] = archived
if sync_result.groups.join:
response["groups"][Membership.JOIN] = sync_result.groups.join
if sync_result.groups.invite:
response["groups"][Membership.INVITE] = sync_result.groups.invite
if sync_result.groups.leave:
response["groups"][Membership.LEAVE] = sync_result.groups.leave
return response
@staticmethod @staticmethod
def encode_presence(events, time_now): def encode_presence(events, time_now):

View File

@ -41,35 +41,7 @@ class FilterTestCase(unittest.HomeserverTestCase):
channel = self.make_request("GET", "/sync") channel = self.make_request("GET", "/sync")
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
self.assertTrue( self.assertIn("next_batch", channel.json_body)
{
"next_batch",
"rooms",
"presence",
"account_data",
"to_device",
"device_lists",
}.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",
"rooms",
"account_data",
"to_device",
"device_lists",
}.issubset(set(channel.json_body.keys()))
)
class SyncFilterTestCase(unittest.HomeserverTestCase): class SyncFilterTestCase(unittest.HomeserverTestCase):

View File

@ -306,8 +306,9 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.HomeserverTestCase):
channel = self.make_request("GET", "/sync?timeout=0", access_token=tok) channel = self.make_request("GET", "/sync?timeout=0", access_token=tok)
invites = channel.json_body["rooms"]["invite"] self.assertNotIn(
self.assertEqual(len(invites), 0, invites) "rooms", channel.json_body, "Got invites without server notice"
)
def test_invite_with_notice(self): def test_invite_with_notice(self):
"""Tests that, if the MAU limit is hit, the server notices user invites each user """Tests that, if the MAU limit is hit, the server notices user invites each user
@ -364,6 +365,7 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.HomeserverTestCase):
# We could also pick another user and sync with it, which would return an # 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 # 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. # using so we use the last one because it saves us an extra sync.
if "rooms" in channel.json_body:
invites = channel.json_body["rooms"]["invite"] invites = channel.json_body["rooms"]["invite"]
# Make sure we have an invite to process. # Make sure we have an invite to process.