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__.py136
-rw-r--r--synapse/events/builder.py69
-rw-r--r--synapse/events/snapshot.py63
-rw-r--r--synapse/events/utils.py82
-rw-r--r--synapse/events/validator.py58
5 files changed, 408 insertions, 0 deletions
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py
new file mode 100644
index 0000000000..58edf2bc8f
--- /dev/null
+++ b/synapse/events/__init__.py
@@ -0,0 +1,136 @@
+# -*- 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 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
+
+    def get_dict(self):
+        return dict(self.__dict__)
+
+
+def _event_dict_property(key):
+        def getter(self):
+            return self._event_dict[key]
+
+        def setter(self, v):
+            self._event_dict[key] = v
+
+        def delete(self):
+            del self._event_dict[key]
+
+        return property(
+            getter,
+            setter,
+            delete,
+        )
+
+
+class EventBase(object):
+    def __init__(self, event_dict, signatures={}, unsigned={},
+                 internal_metadata_dict={}):
+        self.signatures = signatures
+        self.unsigned = unsigned
+
+        self._event_dict = event_dict
+
+        self.internal_metadata = _EventInternalMetadata(
+            internal_metadata_dict
+        )
+
+    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 is_state(self):
+        return hasattr(self, "state_key")
+
+    def get_dict(self):
+        d = dict(self._event_dict)
+        d.update({
+            "signatures": self.signatures,
+            "unsigned": self.unsigned,
+        })
+
+        return d
+
+    def get_internal_metadata_dict(self):
+        return self.internal_metadata.get_dict()
+
+    def get_pdu_json(self, time_now=None):
+        pdu_json = self.get_dict()
+
+        if time_now is not None and "age_ts" in pdu_json["unsigned"]:
+            age = time_now - pdu_json["unsigned"]["age_ts"]
+            pdu_json.setdefault("unsigned", {})["age"] = int(age)
+            del pdu_json["unsigned"]["age_ts"]
+
+        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.get_pdu_json()
+        )
+
+        e.internal_metadata = event.internal_metadata
+
+        return e
diff --git a/synapse/events/builder.py b/synapse/events/builder.py
new file mode 100644
index 0000000000..0b8caf9318
--- /dev/null
+++ b/synapse/events/builder.py
@@ -0,0 +1,69 @@
+# -*- 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 . import EventBase, FrozenEvent
+
+from synapse.types import EventID
+
+from synapse.util.stringutils import random_string
+
+
+class EventBuilder(EventBase):
+    def __init__(self, key_values={}):
+        super(EventBuilder, self).__init__(
+            key_values,
+        )
+
+    def update_event_key(self, key, value):
+        self._event_dict[key] = value
+
+    def update_event_keys(self, other_dict):
+        self._event_dict.update(other_dict)
+
+    def build(self):
+        return FrozenEvent.from_event(self)
+
+
+class EventBuilderFactory(object):
+    def __init__(self, clock, hostname):
+        self.clock = clock
+        self.hostname = hostname
+
+        self.event_id_count = 0
+
+    def create_event_id(self):
+        i = str(self.event_id_count)
+        self.event_id_count += 1
+
+        local_part = str(int(self.clock.time())) + i + random_string(5)
+
+        e_id = EventID.create(local_part, self.hostname)
+
+        return e_id.to_string()
+
+    def new(self, key_values={}):
+        if "event_id" not in key_values:
+            key_values["event_id"] = self.create_event_id()
+
+        time_now = self.clock.time_msec()
+
+        key_values.setdefault("origin", self.hostname)
+        key_values.setdefault("origin_server_ts", time_now)
+
+        if "unsigned" in key_values:
+            age = key_values["unsigned"].pop("age", 0)
+            key_values["unsigned"].setdefault("age_ts", time_now - age)
+
+        return EventBuilder(key_values=key_values,)
\ No newline at end of file
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