summary refs log tree commit diff
path: root/synapse/events/utils.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-05-16 16:54:05 +0100
committerGitHub <noreply@github.com>2019-05-16 16:54:05 +0100
commit5c39d262c048ecb9f7d6aacc10fa26ffef8c32df (patch)
treeb4584277b4404429f290824e37f8807d772cc0b3 /synapse/events/utils.py
parentMerge pull request #5186 from matrix-org/erikj/simple_pagination (diff)
parentMove parsing of tokens out of storage layer (diff)
downloadsynapse-5c39d262c048ecb9f7d6aacc10fa26ffef8c32df.tar.xz
Merge pull request #5192 from matrix-org/erikj/relations_aggregations
Add relation aggregation APIs
Diffstat (limited to 'synapse/events/utils.py')
-rw-r--r--synapse/events/utils.py34
1 files changed, 31 insertions, 3 deletions
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index a5454556cc..16d0c64372 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -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.