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

Commit dfba1d8

Browse files
authored
Merge pull request #3790 from matrix-org/rav/respect_event_format_in_filter
Implement 'event_format' filter param in /sync
2 parents 2d2828d + c91bd29 commit dfba1d8

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

changelog.d/3790.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement `event_format` filter param in `/sync`

synapse/api/filtering.py

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ def __init__(self, filter_json):
251251
"include_leave", False
252252
)
253253
self.event_fields = filter_json.get("event_fields", [])
254+
self.event_format = filter_json.get("event_format", "client")
254255

255256
def __repr__(self):
256257
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)

synapse/rest/client/v2_alpha/sync.py

+38-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from synapse.api.filtering import DEFAULT_FILTER_COLLECTION, FilterCollection
2626
from synapse.events.utils import (
2727
format_event_for_client_v2_without_room_id,
28+
format_event_raw,
2829
serialize_event,
2930
)
3031
from synapse.handlers.presence import format_user_presence_state
@@ -175,17 +176,28 @@ def on_GET(self, request):
175176

176177
@staticmethod
177178
def encode_response(time_now, sync_result, access_token_id, filter):
179+
if filter.event_format == 'client':
180+
event_formatter = format_event_for_client_v2_without_room_id
181+
elif filter.event_format == 'federation':
182+
event_formatter = format_event_raw
183+
else:
184+
raise Exception("Unknown event format %s" % (filter.event_format, ))
185+
178186
joined = SyncRestServlet.encode_joined(
179-
sync_result.joined, time_now, access_token_id, filter.event_fields
187+
sync_result.joined, time_now, access_token_id,
188+
filter.event_fields,
189+
event_formatter,
180190
)
181191

182192
invited = SyncRestServlet.encode_invited(
183193
sync_result.invited, time_now, access_token_id,
194+
event_formatter,
184195
)
185196

186197
archived = SyncRestServlet.encode_archived(
187198
sync_result.archived, time_now, access_token_id,
188199
filter.event_fields,
200+
event_formatter,
189201
)
190202

191203
return {
@@ -228,7 +240,7 @@ def encode_presence(events, time_now):
228240
}
229241

230242
@staticmethod
231-
def encode_joined(rooms, time_now, token_id, event_fields):
243+
def encode_joined(rooms, time_now, token_id, event_fields, event_formatter):
232244
"""
233245
Encode the joined rooms in a sync result
234246
@@ -240,21 +252,24 @@ def encode_joined(rooms, time_now, token_id, event_fields):
240252
token_id(int): ID of the user's auth token - used for namespacing
241253
of transaction IDs
242254
event_fields(list<str>): List of event fields to include. If empty,
243-
all fields will be returned.
255+
all fields will be returned.
256+
event_formatter (func[dict]): function to convert from federation format
257+
to client format
244258
Returns:
245259
dict[str, dict[str, object]]: the joined rooms list, in our
246260
response format
247261
"""
248262
joined = {}
249263
for room in rooms:
250264
joined[room.room_id] = SyncRestServlet.encode_room(
251-
room, time_now, token_id, only_fields=event_fields
265+
room, time_now, token_id, joined=True, only_fields=event_fields,
266+
event_formatter=event_formatter,
252267
)
253268

254269
return joined
255270

256271
@staticmethod
257-
def encode_invited(rooms, time_now, token_id):
272+
def encode_invited(rooms, time_now, token_id, event_formatter):
258273
"""
259274
Encode the invited rooms in a sync result
260275
@@ -264,7 +279,9 @@ def encode_invited(rooms, time_now, token_id):
264279
time_now(int): current time - used as a baseline for age
265280
calculations
266281
token_id(int): ID of the user's auth token - used for namespacing
267-
of transaction IDs
282+
of transaction IDs
283+
event_formatter (func[dict]): function to convert from federation format
284+
to client format
268285
269286
Returns:
270287
dict[str, dict[str, object]]: the invited rooms list, in our
@@ -274,7 +291,7 @@ def encode_invited(rooms, time_now, token_id):
274291
for room in rooms:
275292
invite = serialize_event(
276293
room.invite, time_now, token_id=token_id,
277-
event_format=format_event_for_client_v2_without_room_id,
294+
event_format=event_formatter,
278295
is_invite=True,
279296
)
280297
unsigned = dict(invite.get("unsigned", {}))
@@ -288,7 +305,7 @@ def encode_invited(rooms, time_now, token_id):
288305
return invited
289306

290307
@staticmethod
291-
def encode_archived(rooms, time_now, token_id, event_fields):
308+
def encode_archived(rooms, time_now, token_id, event_fields, event_formatter):
292309
"""
293310
Encode the archived rooms in a sync result
294311
@@ -300,21 +317,28 @@ def encode_archived(rooms, time_now, token_id, event_fields):
300317
token_id(int): ID of the user's auth token - used for namespacing
301318
of transaction IDs
302319
event_fields(list<str>): List of event fields to include. If empty,
303-
all fields will be returned.
320+
all fields will be returned.
321+
event_formatter (func[dict]): function to convert from federation format
322+
to client format
304323
Returns:
305324
dict[str, dict[str, object]]: The invited rooms list, in our
306325
response format
307326
"""
308327
joined = {}
309328
for room in rooms:
310329
joined[room.room_id] = SyncRestServlet.encode_room(
311-
room, time_now, token_id, joined=False, only_fields=event_fields
330+
room, time_now, token_id, joined=False,
331+
only_fields=event_fields,
332+
event_formatter=event_formatter,
312333
)
313334

314335
return joined
315336

316337
@staticmethod
317-
def encode_room(room, time_now, token_id, joined=True, only_fields=None):
338+
def encode_room(
339+
room, time_now, token_id, joined,
340+
only_fields, event_formatter,
341+
):
318342
"""
319343
Args:
320344
room (JoinedSyncResult|ArchivedSyncResult): sync result for a
@@ -326,14 +350,15 @@ def encode_room(room, time_now, token_id, joined=True, only_fields=None):
326350
joined (bool): True if the user is joined to this room - will mean
327351
we handle ephemeral events
328352
only_fields(list<str>): Optional. The list of event fields to include.
353+
event_formatter (func[dict]): function to convert from federation format
354+
to client format
329355
Returns:
330356
dict[str, object]: the room, encoded in our response format
331357
"""
332358
def serialize(event):
333-
# TODO(mjark): Respect formatting requirements in the filter.
334359
return serialize_event(
335360
event, time_now, token_id=token_id,
336-
event_format=format_event_for_client_v2_without_room_id,
361+
event_format=event_formatter,
337362
only_event_fields=only_fields,
338363
)
339364

0 commit comments

Comments
 (0)