summary refs log tree commit diff
path: root/synapse/events/utils.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-05-09 13:21:57 +0100
committerErik Johnston <erik@matrix.org>2019-05-14 11:58:01 +0100
commitb54b03f9e1abc1964fe5f00115a165a2b8e10df5 (patch)
treebb2a357af902fbdfa935da416a6beaedbd5c8a02 /synapse/events/utils.py
parentMigrate all tests to use the dict-based config format instead of hanging item... (diff)
downloadsynapse-b54b03f9e1abc1964fe5f00115a165a2b8e10df5.tar.xz
Allow client event serialization to be async
Diffstat (limited to 'synapse/events/utils.py')
-rw-r--r--synapse/events/utils.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index 07fccdd8f9..a5454556cc 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -19,7 +19,10 @@ from six import string_types
 
 from frozendict import frozendict
 
+from twisted.internet import defer
+
 from synapse.api.constants import EventTypes
+from synapse.util.async_helpers import yieldable_gather_results
 
 from . import EventBase
 
@@ -311,3 +314,44 @@ def serialize_event(e, time_now_ms, as_client_event=True,
         d = only_fields(d, only_event_fields)
 
     return d
+
+
+class EventClientSerializer(object):
+    """Serializes events that are to be sent to clients.
+
+    This is used for bundling extra information with any events to be sent to
+    clients.
+    """
+
+    def __init__(self, hs):
+        pass
+
+    def serialize_event(self, event, time_now, **kwargs):
+        """Serializes a single event.
+
+        Args:
+            event (EventBase)
+            time_now (int): The current time in milliseconds
+            **kwargs: Arguments to pass to `serialize_event`
+
+        Returns:
+            Deferred[dict]: The serialized event
+        """
+        event = serialize_event(event, time_now, **kwargs)
+        return defer.succeed(event)
+
+    def serialize_events(self, events, time_now, **kwargs):
+        """Serializes multiple events.
+
+        Args:
+            event (iter[EventBase])
+            time_now (int): The current time in milliseconds
+            **kwargs: Arguments to pass to `serialize_event`
+
+        Returns:
+            Deferred[list[dict]]: The list of serialized events
+        """
+        return yieldable_gather_results(
+            self.serialize_event, events,
+            time_now=time_now, **kwargs
+        )