Add aggregations API

This commit is contained in:
Erik Johnston 2019-05-14 16:59:21 +01:00
parent 5be34fc3e3
commit a0603523d2
5 changed files with 643 additions and 13 deletions

View file

@ -21,7 +21,7 @@ from frozendict import frozendict
from twisted.internet import defer
from synapse.api.constants import EventTypes
from synapse.api.constants import EventTypes, RelationTypes
from synapse.util.async_helpers import yieldable_gather_results
from . import EventBase
@ -324,8 +324,12 @@ class EventClientSerializer(object):
"""
def __init__(self, hs):
pass
self.store = hs.get_datastore()
self.experimental_msc1849_support_enabled = (
hs.config.experimental_msc1849_support_enabled
)
@defer.inlineCallbacks
def serialize_event(self, event, time_now, **kwargs):
"""Serializes a single event.
@ -337,8 +341,32 @@ class EventClientSerializer(object):
Returns:
Deferred[dict]: The serialized event
"""
# To handle the case of presence events and the like
if not isinstance(event, EventBase):
defer.returnValue(event)
event_id = event.event_id
event = serialize_event(event, time_now, **kwargs)
return defer.succeed(event)
# If MSC1849 is enabled then we need to look if thre are any relations
# we need to bundle in with the event
if self.experimental_msc1849_support_enabled:
annotations = yield self.store.get_aggregation_groups_for_event(
event_id,
)
references = yield self.store.get_relations_for_event(
event_id, RelationTypes.REFERENCES, direction="f",
)
if annotations.chunk:
r = event["unsigned"].setdefault("m.relations", {})
r[RelationTypes.ANNOTATION] = annotations.to_dict()
if references.chunk:
r = event["unsigned"].setdefault("m.relations", {})
r[RelationTypes.REFERENCES] = references.to_dict()
defer.returnValue(event)
def serialize_events(self, events, time_now, **kwargs):
"""Serializes multiple events.