diff options
author | Mark Haines <mjark@negativecurvature.net> | 2014-12-16 13:53:43 +0000 |
---|---|---|
committer | Mark Haines <mjark@negativecurvature.net> | 2014-12-16 13:53:43 +0000 |
commit | 2af40cfa14ba579c156349ae3457a55bc93f8548 (patch) | |
tree | 2b94b53c72d51fc170549213ed8cb88556c2b501 /synapse/events | |
parent | Add a script for talking matrix federation adding X-Matrix Authorization (diff) | |
parent | Fix pyflakes (diff) | |
download | synapse-2af40cfa14ba579c156349ae3457a55bc93f8548.tar.xz |
Merge pull request #25 from matrix-org/events_refactor
Event refactor
Diffstat (limited to '')
-rw-r--r-- | synapse/events/__init__.py | 149 | ||||
-rw-r--r-- | synapse/events/builder.py | 77 | ||||
-rw-r--r-- | synapse/events/snapshot.py (renamed from tests/events/__init__.py) | 7 | ||||
-rw-r--r-- | synapse/events/utils.py (renamed from synapse/api/events/utils.py) | 69 | ||||
-rw-r--r-- | synapse/events/validator.py | 92 |
5 files changed, 382 insertions, 12 deletions
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py new file mode 100644 index 0000000000..984f14fce4 --- /dev/null +++ b/synapse/events/__init__.py @@ -0,0 +1,149 @@ +# -*- 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.util.frozenutils import freeze, unfreeze + +import copy + + +class _EventInternalMetadata(object): + def __init__(self, internal_metadata_dict): + self.__dict__ = copy.deepcopy(internal_metadata_dict) + + def get_dict(self): + return dict(self.__dict__) + + def is_outlier(self): + return hasattr(self, "outlier") and self.outlier + + +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 = copy.deepcopy(signatures) + self.unsigned = copy.deepcopy(unsigned) + + self._event_dict = copy.deepcopy(event_dict) + + self.internal_metadata = _EventInternalMetadata( + internal_metadata_dict + ) + + 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") + origin = _event_dict_property("origin") + origin_server_ts = _event_dict_property("origin_server_ts") + prev_events = _event_dict_property("prev_events") + prev_state = _event_dict_property("prev_state") + redacts = _event_dict_property("redacts") + 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") + + @property + def membership(self): + return self.content["membership"] + + 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(self, key, default): + return self._event_dict.get(key, default) + + 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, internal_metadata_dict={}): + event_dict = copy.deepcopy(event_dict) + + signatures = copy.deepcopy(event_dict.pop("signatures", {})) + unsigned = copy.deepcopy(event_dict.pop("unsigned", {})) + + frozen_dict = freeze(event_dict) + + super(FrozenEvent, self).__init__( + frozen_dict, + signatures=signatures, + unsigned=unsigned, + internal_metadata_dict=internal_metadata_dict, + ) + + @staticmethod + def from_event(event): + e = FrozenEvent( + event.get_pdu_json() + ) + + e.internal_metadata = event.internal_metadata + + return e + + def get_dict(self): + # We need to unfreeze what we return + return unfreeze(super(FrozenEvent, self).get_dict()) + + def __str__(self): + return self.__repr__() + + def __repr__(self): + return "<FrozenEvent event_id='%s', type='%s', state_key='%s'>" % ( + self.event_id, self.type, self.get("state_key", None), + ) diff --git a/synapse/events/builder.py b/synapse/events/builder.py new file mode 100644 index 0000000000..9579b1fe8b --- /dev/null +++ b/synapse/events/builder.py @@ -0,0 +1,77 @@ +# -*- 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 + +import copy + + +class EventBuilder(EventBase): + def __init__(self, key_values={}): + signatures = copy.deepcopy(key_values.pop("signatures", {})) + unsigned = copy.deepcopy(key_values.pop("unsigned", {})) + + super(EventBuilder, self).__init__( + key_values, + signatures=signatures, + unsigned=unsigned + ) + + 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={}): + key_values["event_id"] = self.create_event_id() + + time_now = int(self.clock.time_msec()) + + key_values.setdefault("origin", self.hostname) + key_values.setdefault("origin_server_ts", time_now) + + key_values.setdefault("unsigned", {}) + age = key_values["unsigned"].pop("age", 0) + key_values["unsigned"].setdefault("age_ts", time_now - age) + + key_values["signatures"] = {} + + return EventBuilder(key_values=key_values,) diff --git a/tests/events/__init__.py b/synapse/events/snapshot.py index 9bff9ec169..b9fb29be01 100644 --- a/tests/events/__init__.py +++ b/synapse/events/snapshot.py @@ -13,3 +13,10 @@ # See the License for the specific language governing permissions and # limitations under the License. + +class EventContext(object): + + def __init__(self, current_state=None, auth_events=None): + self.current_state = current_state + self.auth_events = auth_events + self.state_group = None diff --git a/synapse/api/events/utils.py b/synapse/events/utils.py index d6019d56eb..94f3f15f52 100644 --- a/synapse/api/events/utils.py +++ b/synapse/events/utils.py @@ -13,10 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .room import ( - RoomMemberEvent, RoomJoinRulesEvent, RoomPowerLevelsEvent, - RoomAliasesEvent, RoomCreateEvent, -) +from synapse.api.constants import EventTypes +from . import EventBase def prune_event(event): @@ -31,7 +29,7 @@ def prune_event(event): allowed_keys = [ "event_id", - "user_id", + "sender", "room_id", "hashes", "signatures", @@ -44,6 +42,7 @@ def prune_event(event): "auth_events", "origin", "origin_server_ts", + "membership", ] new_content = {} @@ -53,13 +52,13 @@ def prune_event(event): if field in event.content: new_content[field] = event.content[field] - if event_type == RoomMemberEvent.TYPE: + if event_type == EventTypes.Member: add_fields("membership") - elif event_type == RoomCreateEvent.TYPE: + elif event_type == EventTypes.Create: add_fields("creator") - elif event_type == RoomJoinRulesEvent.TYPE: + elif event_type == EventTypes.JoinRules: add_fields("join_rule") - elif event_type == RoomPowerLevelsEvent.TYPE: + elif event_type == EventTypes.PowerLevels: add_fields( "users", "users_default", @@ -71,15 +70,61 @@ def prune_event(event): "kick", "redact", ) - elif event_type == RoomAliasesEvent.TYPE: + elif event_type == EventTypes.Aliases: add_fields("aliases") allowed_fields = { k: v - for k, v in event.get_full_dict().items() + for k, v in event.get_dict().items() if k in allowed_keys } allowed_fields["content"] = new_content - return type(event)(**allowed_fields) + allowed_fields["unsigned"] = {} + + if "age_ts" in event.unsigned: + allowed_fields["unsigned"]["age_ts"] = event.unsigned["age_ts"] + + 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"] + + d["user_id"] = d.pop("sender", None) + + if "redacted_because" in e.unsigned: + d["redacted_because"] = serialize_event( + hs, e.unsigned["redacted_because"] + ) + + del d["unsigned"]["redacted_because"] + + if "redacted_by" in e.unsigned: + d["redacted_by"] = e.unsigned["redacted_by"] + del d["unsigned"]["redacted_by"] + + if "replaces_state" in e.unsigned: + d["replaces_state"] = e.unsigned["replaces_state"] + del d["unsigned"]["replaces_state"] + + if "prev_content" in e.unsigned: + d["prev_content"] = e.unsigned["prev_content"] + del d["unsigned"]["prev_content"] + + del d["auth_events"] + del d["prev_events"] + del d["hashes"] + del d["signatures"] + + return d diff --git a/synapse/events/validator.py b/synapse/events/validator.py new file mode 100644 index 0000000000..ebc6c30e62 --- /dev/null +++ b/synapse/events/validator.py @@ -0,0 +1,92 @@ +# -*- 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 +from synapse.api.constants import EventTypes, Membership + + +class EventValidator(object): + + def validate(self, event): + EventID.from_string(event.event_id) + RoomID.from_string(event.room_id) + + required = [ + # "auth_events", + "content", + # "hashes", + "origin", + # "prev_events", + "sender", + "type", + ] + + for k in required: + if not hasattr(event, k): + raise SynapseError(400, "Event does not have key %s" % (k,)) + + # 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,)) + + if event.type == EventTypes.Member: + if "membership" not in event.content: + raise SynapseError(400, "Content has not membership key") + + if event.content["membership"] not in Membership.LIST: + raise SynapseError(400, "Invalid membership key") + + # 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) + + if event.type == EventTypes.Message: + strings = [ + "body", + "msgtype", + ] + + self._ensure_strings(event.content, strings) + + elif event.type == EventTypes.Topic: + self._ensure_strings(event.content, ["topic"]) + + elif event.type == EventTypes.Name: + self._ensure_strings(event.content, ["name"]) + + def _ensure_strings(self, d, keys): + for s in keys: + if s not in d: + raise SynapseError(400, "'%s' not in content" % (s,)) + if not isinstance(d[s], basestring): + raise SynapseError(400, "Not '%s' a string type" % (s,)) |