summary refs log tree commit diff
path: root/synapse/events
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/events')
-rw-r--r--synapse/events/__init__.py141
-rw-r--r--synapse/events/builder.py20
-rw-r--r--synapse/events/snapshot.py63
-rw-r--r--synapse/events/utils.py82
-rw-r--r--synapse/events/validator.py58
5 files changed, 286 insertions, 78 deletions
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py
index eefc9d3b30..6748198917 100644
--- a/synapse/events/__init__.py
+++ b/synapse/events/__init__.py
@@ -16,6 +16,21 @@
 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, basestring):
+        return o
+
+    try:
+        return tuple([_freeze(i) for i in o])
+    except TypeError:
+        pass
+
+    return o
+
+
 class _EventInternalMetadata(object):
     def __init__(self, internal_metadata_dict):
         self.__dict__ = internal_metadata_dict
@@ -24,78 +39,47 @@ class _EventInternalMetadata(object):
         return dict(self.__dict__)
 
 
-class Event(object):
-    def __init__(self, event_dict, internal_metadata_dict={}):
-        self._signatures = event_dict.get("signatures", {})
-        self._unsigned = event_dict.get("unsigned", {})
+def _event_dict_property(key):
+        def getter(self):
+            return self._event_dict[key]
 
-        self._original = {
-            k: v
-            for k, v in event_dict.items()
-            if k not in ["signatures", "unsigned"]
-        }
+        def setter(self, v):
+            self._event_dict[key] = v
 
-        self._event_dict = frozendict(self._original)
+        def delete(self):
+            del self._event_dict[key]
 
-        self.internal_metadata = _EventInternalMetadata(
-            internal_metadata_dict
+        return property(
+            getter,
+            setter,
+            delete,
         )
 
-    @property
-    def auth_events(self):
-        return self._event_dict["auth_events"]
-
-    @property
-    def content(self):
-        return self._event_dict["content"]
-
-    @property
-    def event_id(self):
-        return self._event_dict["event_id"]
-
-    @property
-    def hashes(self):
-        return self._event_dict["hashes"]
-
-    @property
-    def origin(self):
-        return self._event_dict["origin"]
-
-    @property
-    def prev_events(self):
-        return self._event_dict["prev_events"]
-
-    @property
-    def prev_state(self):
-        return self._event_dict["prev_state"]
-
-    @property
-    def room_id(self):
-        return self._event_dict["room_id"]
-
-    @property
-    def signatures(self):
-        return self._signatures
 
-    @property
-    def state_key(self):
-        return self._event_dict["state_key"]
+class EventBase(object):
+    def __init__(self, event_dict, signatures={}, unsigned={},
+                 internal_metadata_dict={}):
+        self.signatures = signatures
+        self.unsigned = unsigned
 
-    @property
-    def type(self):
-        return self._event_dict["type"]
+        self._event_dict = event_dict
 
-    @property
-    def unsigned(self):
-        return self._unsigned
-
-    @property
-    def user_id(self):
-        return self._event_dict["sender"]
+        self.internal_metadata = _EventInternalMetadata(
+            internal_metadata_dict
+        )
 
-    @property
-    def sender(self):
-        return self._event_dict["sender"]
+    auth_events = _event_dict_property("auth_events")
+    content = _event_dict_property("content")
+    event_id = _event_dict_property("event_id")
+    hashes = _event_dict_property("hashes")
+    origin = _event_dict_property("origin")
+    prev_events = _event_dict_property("prev_events")
+    prev_state = _event_dict_property("prev_state")
+    room_id = _event_dict_property("room_id")
+    sender = _event_dict_property("sender")
+    state_key = _event_dict_property("state_key")
+    type = _event_dict_property("type")
+    user_id = _event_dict_property("sender")
 
     def get_dict(self):
         d = dict(self._original)
@@ -117,4 +101,33 @@ class Event(object):
             pdu_json.setdefault("unsigned", {})["age"] = int(age)
             del pdu_json["unsigned"]["age_ts"]
 
-        return pdu_json
\ No newline at end of file
+        return pdu_json
+
+    def __set__(self, instance, value):
+        raise AttributeError("Unrecognized attribute %s" % (instance,))
+
+
+class FrozenEvent(EventBase):
+    def __init__(self, event_dict, signatures={}, unsigned={}):
+        event_dict = dict(event_dict)
+
+        signatures.update(event_dict.pop("signatures", {}))
+        unsigned.update(event_dict.pop("unsigned", {}))
+
+        frozen_dict = _freeze(event_dict)
+
+        super(FrozenEvent, self).__init__(
+            frozen_dict,
+            signatures=signatures,
+            unsigned=unsigned
+        )
+
+    @staticmethod
+    def from_event(event):
+        e = FrozenEvent(
+            event.event_dict()
+        )
+
+        e.internal_metadata = event.internal_metadata
+
+        return e
diff --git a/synapse/events/builder.py b/synapse/events/builder.py
index d741795bc5..39b4d2a2ab 100644
--- a/synapse/events/builder.py
+++ b/synapse/events/builder.py
@@ -13,32 +13,24 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import Event
+from . import EventBase, FrozenEvent
 
 from synapse.types import EventID
 
 from synapse.util.stringutils import random_string
 
 
-class EventBuilder(object):
+class EventBuilder(EventBase):
     def __init__(self, key_values={}):
-        self._event_dict = dict(key_values)
-        self._metadata = {}
-
-    def update_event_key(self, key, value):
-        self._event_dict[key] = value
+        super(FrozenEvent, self).__init__(
+            key_values,
+        )
 
     def update_event_keys(self, other_dict):
         self._event_dict.update(other_dict)
 
-    def update_internal_key(self, key, value):
-        self._metadata[key] = value
-
     def build(self):
-        return Event(
-            self._event_dict,
-            self._metadata,
-        )
+        return FrozenEvent.from_event(self)
 
 
 class EventBuilderFactory(object):
diff --git a/synapse/events/snapshot.py b/synapse/events/snapshot.py
new file mode 100644
index 0000000000..ca15ec09ae
--- /dev/null
+++ b/synapse/events/snapshot.py
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+# Copyright 2014 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from twisted.internet import defer
+
+
+class EventSnapshot(object):
+    def __init__(self, prev_events, depth, current_state,
+                 current_state_group):
+        self._prev_events = prev_events
+        self._depth = depth
+        self._current_state = current_state
+        self._current_state_group = current_state_group
+
+
+class EventCache(object):
+    def __init__(self, store):
+        self._store = store
+
+        self._cache = {}
+
+    @defer.inlineCallbacks
+    def load_event(self, event_id):
+        event = self._cache.get(event_id, None)
+
+        if not event:
+            event = yield self._store.get_event(
+                event_id,
+                allow_none=True
+            )
+
+            if event:
+                self._cache[event_id] = event
+
+        defer.returnValue(event)
+
+    def load_event_from_cache(self, event_id):
+        return self._cache.get(event_id, None)
+
+    def add_to_cache(self, *events):
+        self._cache.update({
+            event.event_id: event
+            for event in events
+        })
+
+
+class EventContext(object):
+
+    def __init__(self, current_state, auth_events):
+        self.current_state = current_state
+        self.auth_events = auth_events
diff --git a/synapse/events/utils.py b/synapse/events/utils.py
new file mode 100644
index 0000000000..412f690f08
--- /dev/null
+++ b/synapse/events/utils.py
@@ -0,0 +1,82 @@
+# -*- coding: utf-8 -*-
+# Copyright 2014 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from synapse.api.constants import EventTypes
+
+
+def prune_event(event):
+    """ Returns a pruned version of the given event, which removes all keys we
+    don't know about or think could potentially be dodgy.
+
+    This is used when we "redact" an event. We want to remove all fields that
+    the user has specified, but we do want to keep necessary information like
+    type, state_key etc.
+    """
+    event_type = event.type
+
+    allowed_keys = [
+        "event_id",
+        "sender",
+        "room_id",
+        "hashes",
+        "signatures",
+        "content",
+        "type",
+        "state_key",
+        "depth",
+        "prev_events",
+        "prev_state",
+        "auth_events",
+        "origin",
+        "origin_server_ts",
+    ]
+
+    new_content = {}
+
+    def add_fields(*fields):
+        for field in fields:
+            if field in event.content:
+                new_content[field] = event.content[field]
+
+    if event_type == EventTypes.Member:
+        add_fields("membership")
+    elif event_type == EventTypes.Create:
+        add_fields("creator")
+    elif event_type == EventTypes.JoinRules:
+        add_fields("join_rule")
+    elif event_type == EventTypes.PowerLevels:
+        add_fields(
+            "users",
+            "users_default",
+            "events",
+            "events_default",
+            "events_default",
+            "state_default",
+            "ban",
+            "kick",
+            "redact",
+        )
+    elif event_type == EventTypes.Aliases:
+        add_fields("aliases")
+
+    allowed_fields = {
+        k: v
+        for k, v in event.get_dict().items()
+        if k in allowed_keys
+    }
+
+    allowed_fields["content"] = new_content
+
+    return type(event)(allowed_fields)
diff --git a/synapse/events/validator.py b/synapse/events/validator.py
new file mode 100644
index 0000000000..7dc9506ec4
--- /dev/null
+++ b/synapse/events/validator.py
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+# Copyright 2014 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from synapse.types import EventID, RoomID, UserID
+from synapse.api.errors import SynapseError
+
+
+class EventValidator(object):
+
+    def validate(self, event):
+        EventID.from_string(event.event_id)
+        RoomID.from_string(event.room_id)
+
+        hasattr(event, "auth_events")
+        hasattr(event, "content")
+        hasattr(event, "hashes")
+        hasattr(event, "origin")
+        hasattr(event, "prev_events")
+        hasattr(event, "prev_events")
+        hasattr(event, "sender")
+        hasattr(event, "type")
+
+        # Check that the following keys have string values
+        strings = [
+            "origin",
+            "sender",
+            "type",
+        ]
+
+        if hasattr(event, "state_key"):
+            strings.append("state_key")
+
+        for s in strings:
+            if not isinstance(getattr(event, s), basestring):
+                raise SynapseError(400, "Not '%s' a string type" % (s,))
+
+        # Check that the following keys have dictionary values
+        # TODO
+
+        # Check that the following keys have the correct format for DAGs
+        # TODO
+
+    def validate_new(self, event):
+        self.validate(event)
+
+        UserID.from_string(event.sender)
\ No newline at end of file