summary refs log tree commit diff
path: root/synapse/events
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-12-05 16:20:48 +0000
committerErik Johnston <erik@matrix.org>2014-12-05 16:20:48 +0000
commit6630e1b5795667fd947cc5b0d5d2b00da97325e3 (patch)
treece141a61defe211cac454a5953106f6e351fe260 /synapse/events
parentMerge branch 'develop' of github.com:matrix-org/synapse into events_refactor (diff)
downloadsynapse-6630e1b5795667fd947cc5b0d5d2b00da97325e3.tar.xz
Start making more things use EventContext rather than event.*
Diffstat (limited to 'synapse/events')
-rw-r--r--synapse/events/__init__.py35
-rw-r--r--synapse/events/utils.py16
2 files changed, 49 insertions, 2 deletions
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py
index 58edf2bc8f..e81b995d39 100644
--- a/synapse/events/__init__.py
+++ b/synapse/events/__init__.py
@@ -17,8 +17,8 @@ from frozendict import frozendict
 
 
 def _freeze(o):
-    if isinstance(o, dict):
-        return frozendict({k: _freeze(v) for k,v in o.items()})
+    if isinstance(o, dict) or isinstance(o, frozendict):
+        return frozendict({k: _freeze(v) for k, v in o.items()})
 
     if isinstance(o, basestring):
         return o
@@ -31,6 +31,21 @@ def _freeze(o):
     return o
 
 
+def _unfreeze(o):
+    if isinstance(o, frozendict) or isinstance(o, dict):
+        return dict({k: _unfreeze(v) for k, v in o.items()})
+
+    if isinstance(o, basestring):
+        return o
+
+    try:
+        return [_unfreeze(i) for i in o]
+    except TypeError:
+        pass
+
+    return o
+
+
 class _EventInternalMetadata(object):
     def __init__(self, internal_metadata_dict):
         self.__dict__ = internal_metadata_dict
@@ -69,6 +84,7 @@ class EventBase(object):
         )
 
     auth_events = _event_dict_property("auth_events")
+    depth = _event_dict_property("depth")
     content = _event_dict_property("content")
     event_id = _event_dict_property("event_id")
     hashes = _event_dict_property("hashes")
@@ -81,6 +97,10 @@ class EventBase(object):
     type = _event_dict_property("type")
     user_id = _event_dict_property("sender")
 
+    @property
+    def membership(self):
+        return self.content["membership"]
+
     def is_state(self):
         return hasattr(self, "state_key")
 
@@ -134,3 +154,14 @@ class FrozenEvent(EventBase):
         e.internal_metadata = event.internal_metadata
 
         return e
+
+    def get_dict(self):
+        # We need to unfreeze what we return
+
+        d = _unfreeze(self._event_dict)
+        d.update({
+            "signatures": self.signatures,
+            "unsigned": self.unsigned,
+        })
+
+        return d
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
index 412f690f08..1b05ee0a95 100644
--- a/synapse/events/utils.py
+++ b/synapse/events/utils.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 
 from synapse.api.constants import EventTypes
+from . import EventBase
 
 
 def prune_event(event):
@@ -80,3 +81,18 @@ def prune_event(event):
     allowed_fields["content"] = new_content
 
     return type(event)(allowed_fields)
+
+
+def serialize_event(hs, e):
+    # FIXME(erikj): To handle the case of presence events and the like
+    if not isinstance(e, EventBase):
+        return e
+
+    # Should this strip out None's?
+    d = {k: v for k, v in e.get_dict().items()}
+    if "age_ts" in d["unsigned"]:
+        now = int(hs.get_clock().time_msec())
+        d["unsigned"]["age"] = now - d["unsigned"]["age_ts"]
+        del d["unsigned"]["age_ts"]
+
+    return d
\ No newline at end of file