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

Commit

Permalink
Glue only_event_fields into the sync rest servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 22, 2016
1 parent 0a8b0ee commit cea4e4e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def __init__(self, filter_json):
self.include_leave = filter_json.get("room", {}).get(
"include_leave", False
)
self.event_fields = filter_json.get("event_fields", [])

def __repr__(self):
return "<FilterCollection %s>" % (json.dumps(self._filter_json),)
Expand Down
2 changes: 1 addition & 1 deletion synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def serialize_event(e, time_now_ms, as_client_event=True,
if as_client_event:
d = event_format(d)

if (isinstance(only_event_fields, list) and
if (only_event_fields and isinstance(only_event_fields, list) and
all(isinstance(f, basestring) for f in only_event_fields)):
d = only_fields(d, only_event_fields)

Expand Down
23 changes: 13 additions & 10 deletions synapse/rest/client/v2_alpha/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ def on_GET(self, request):
time_now = self.clock.time_msec()

joined = self.encode_joined(
sync_result.joined, time_now, requester.access_token_id
sync_result.joined, time_now, requester.access_token_id, filter.event_fields
)

invited = self.encode_invited(
sync_result.invited, time_now, requester.access_token_id
)

archived = self.encode_archived(
sync_result.archived, time_now, requester.access_token_id
sync_result.archived, time_now, requester.access_token_id, filter.event_fields
)

response_content = {
Expand All @@ -197,7 +197,7 @@ def encode_presence(self, events, time_now):
formatted.append(event)
return {"events": formatted}

def encode_joined(self, rooms, time_now, token_id):
def encode_joined(self, rooms, time_now, token_id, event_fields):
"""
Encode the joined rooms in a sync result
Expand All @@ -208,15 +208,16 @@ def encode_joined(self, rooms, time_now, token_id):
calculations
token_id(int): ID of the user's auth token - used for namespacing
of transaction IDs
event_fields(list<str>): List of event fields to include. If empty,
all fields will be returned.
Returns:
dict[str, dict[str, object]]: the joined rooms list, in our
response format
"""
joined = {}
for room in rooms:
joined[room.room_id] = self.encode_room(
room, time_now, token_id
room, time_now, token_id, only_fields=event_fields
)

return joined
Expand Down Expand Up @@ -253,7 +254,7 @@ def encode_invited(self, rooms, time_now, token_id):

return invited

def encode_archived(self, rooms, time_now, token_id):
def encode_archived(self, rooms, time_now, token_id, event_fields):
"""
Encode the archived rooms in a sync result
Expand All @@ -264,21 +265,22 @@ def encode_archived(self, rooms, time_now, token_id):
calculations
token_id(int): ID of the user's auth token - used for namespacing
of transaction IDs
event_fields(list<str>): List of event fields to include. If empty,
all fields will be returned.
Returns:
dict[str, dict[str, object]]: The invited rooms list, in our
response format
"""
joined = {}
for room in rooms:
joined[room.room_id] = self.encode_room(
room, time_now, token_id, joined=False
room, time_now, token_id, joined=False, only_fields=event_fields
)

return joined

@staticmethod
def encode_room(room, time_now, token_id, joined=True):
def encode_room(room, time_now, token_id, joined=True, only_fields=None):
"""
Args:
room (JoinedSyncResult|ArchivedSyncResult): sync result for a
Expand All @@ -289,7 +291,7 @@ def encode_room(room, time_now, token_id, joined=True):
of transaction IDs
joined (bool): True if the user is joined to this room - will mean
we handle ephemeral events
only_fields(list<str>): Optional. The list of event fields to include.
Returns:
dict[str, object]: the room, encoded in our response format
"""
Expand All @@ -298,6 +300,7 @@ def serialize(event):
return serialize_event(
event, time_now, token_id=token_id,
event_format=format_event_for_client_v2_without_room_id,
only_event_fields=only_fields,
)

state_dict = room.state
Expand Down

0 comments on commit cea4e4e

Please sign in to comment.