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

Re-introduce "Leave out optional keys from /sync" change #10214

Merged
merged 6 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10214.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Omit empty fields from the `/sync` response. Contributed by @deepbluev7.
1 change: 1 addition & 0 deletions changelog.d/9919.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Omit empty fields from the `/sync` response. Contributed by @deepbluev7.
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
69 changes: 46 additions & 23 deletions synapse/rest/client/v2_alpha/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import itertools
import logging
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple

from synapse.api.constants import Membership, PresenceState
Expand Down Expand Up @@ -232,29 +233,51 @@ 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": {
Membership.JOIN: joined,
Membership.INVITE: invited,
Membership.KNOCK: knocked,
Membership.LEAVE: archived,
},
"groups": {
Membership.JOIN: sync_result.groups.join,
Membership.INVITE: sync_result.groups.invite,
Membership.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)

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}

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 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"][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
def encode_presence(events, time_now):
Expand Down
41 changes: 9 additions & 32 deletions tests/rest/client/v2_alpha/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import json

import synapse.rest.admin
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
from synapse.api.constants import (
EventContentFields,
EventTypes,
Membership,
RelationTypes,
)
from synapse.rest.client.v1 import login, room
from synapse.rest.client.v2_alpha import knock, read_marker, sync

Expand All @@ -41,35 +46,7 @@ def test_sync_argless(self):
channel = self.make_request("GET", "/sync")

self.assertEqual(channel.code, 200)
self.assertTrue(
{
"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()))
)
self.assertIn("next_batch", channel.json_body)


class SyncFilterTestCase(unittest.HomeserverTestCase):
Expand Down Expand Up @@ -371,7 +348,7 @@ def test_knock_room_state(self):

# We expect to see the knock event in the stripped room state later
self.expected_room_state[EventTypes.Member] = {
"content": {"membership": "knock", "displayname": "knocker"},
"content": {"membership": Membership.KNOCK, "displayname": "knocker"},
"state_key": "@knocker:test",
}

Expand All @@ -384,7 +361,7 @@ def test_knock_room_state(self):
self.assertEqual(channel.code, 200, channel.json_body)

# Extract the stripped room state events from /sync
knock_entry = channel.json_body["rooms"]["knock"]
knock_entry = channel.json_body["rooms"][Membership.KNOCK]
room_state_events = knock_entry[self.room_id]["knock_state"]["events"]

# Validate that the knock membership event came last
Expand Down
19 changes: 14 additions & 5 deletions tests/server_notices/test_resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

from twisted.internet import defer

from synapse.api.constants import EventTypes, LimitBlockingTypes, ServerNoticeMsgType
from synapse.api.constants import (
EventTypes,
LimitBlockingTypes,
Membership,
ServerNoticeMsgType,
)
from synapse.api.errors import ResourceLimitError
from synapse.rest import admin
from synapse.rest.client.v1 import login, room
Expand Down Expand Up @@ -306,8 +311,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.assertNotIn(
"rooms", 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
Expand All @@ -321,7 +327,9 @@ def test_invite_with_notice(self):

# Scan the events in the room to search for a message from the server notices
# user.
events = channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
events = channel.json_body["rooms"][Membership.JOIN][room_id]["timeline"][
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
"events"
]
notice_in_room = False
for event in events:
if (
Expand Down Expand Up @@ -364,7 +372,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"][Membership.INVITE]

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