25
25
from synapse .api .filtering import DEFAULT_FILTER_COLLECTION , FilterCollection
26
26
from synapse .events .utils import (
27
27
format_event_for_client_v2_without_room_id ,
28
+ format_event_raw ,
28
29
serialize_event ,
29
30
)
30
31
from synapse .handlers .presence import format_user_presence_state
@@ -175,17 +176,28 @@ def on_GET(self, request):
175
176
176
177
@staticmethod
177
178
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
+
178
186
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 ,
180
190
)
181
191
182
192
invited = SyncRestServlet .encode_invited (
183
193
sync_result .invited , time_now , access_token_id ,
194
+ event_formatter ,
184
195
)
185
196
186
197
archived = SyncRestServlet .encode_archived (
187
198
sync_result .archived , time_now , access_token_id ,
188
199
filter .event_fields ,
200
+ event_formatter ,
189
201
)
190
202
191
203
return {
@@ -228,7 +240,7 @@ def encode_presence(events, time_now):
228
240
}
229
241
230
242
@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 ):
232
244
"""
233
245
Encode the joined rooms in a sync result
234
246
@@ -240,21 +252,24 @@ def encode_joined(rooms, time_now, token_id, event_fields):
240
252
token_id(int): ID of the user's auth token - used for namespacing
241
253
of transaction IDs
242
254
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
244
258
Returns:
245
259
dict[str, dict[str, object]]: the joined rooms list, in our
246
260
response format
247
261
"""
248
262
joined = {}
249
263
for room in rooms :
250
264
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 ,
252
267
)
253
268
254
269
return joined
255
270
256
271
@staticmethod
257
- def encode_invited (rooms , time_now , token_id ):
272
+ def encode_invited (rooms , time_now , token_id , event_formatter ):
258
273
"""
259
274
Encode the invited rooms in a sync result
260
275
@@ -264,7 +279,9 @@ def encode_invited(rooms, time_now, token_id):
264
279
time_now(int): current time - used as a baseline for age
265
280
calculations
266
281
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
268
285
269
286
Returns:
270
287
dict[str, dict[str, object]]: the invited rooms list, in our
@@ -274,7 +291,7 @@ def encode_invited(rooms, time_now, token_id):
274
291
for room in rooms :
275
292
invite = serialize_event (
276
293
room .invite , time_now , token_id = token_id ,
277
- event_format = format_event_for_client_v2_without_room_id ,
294
+ event_format = event_formatter ,
278
295
is_invite = True ,
279
296
)
280
297
unsigned = dict (invite .get ("unsigned" , {}))
@@ -288,7 +305,7 @@ def encode_invited(rooms, time_now, token_id):
288
305
return invited
289
306
290
307
@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 ):
292
309
"""
293
310
Encode the archived rooms in a sync result
294
311
@@ -300,21 +317,28 @@ def encode_archived(rooms, time_now, token_id, event_fields):
300
317
token_id(int): ID of the user's auth token - used for namespacing
301
318
of transaction IDs
302
319
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
304
323
Returns:
305
324
dict[str, dict[str, object]]: The invited rooms list, in our
306
325
response format
307
326
"""
308
327
joined = {}
309
328
for room in rooms :
310
329
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 ,
312
333
)
313
334
314
335
return joined
315
336
316
337
@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
+ ):
318
342
"""
319
343
Args:
320
344
room (JoinedSyncResult|ArchivedSyncResult): sync result for a
@@ -326,14 +350,15 @@ def encode_room(room, time_now, token_id, joined=True, only_fields=None):
326
350
joined (bool): True if the user is joined to this room - will mean
327
351
we handle ephemeral events
328
352
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
329
355
Returns:
330
356
dict[str, object]: the room, encoded in our response format
331
357
"""
332
358
def serialize (event ):
333
- # TODO(mjark): Respect formatting requirements in the filter.
334
359
return serialize_event (
335
360
event , time_now , token_id = token_id ,
336
- event_format = format_event_for_client_v2_without_room_id ,
361
+ event_format = event_formatter ,
337
362
only_event_fields = only_fields ,
338
363
)
339
364
0 commit comments