2014-12-02 12:40:22 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-12-02 12:40:22 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2015-02-11 17:52:22 +01:00
|
|
|
from synapse.util.frozenutils import freeze
|
2016-03-23 17:13:05 +01:00
|
|
|
from synapse.util.caches import intern_dict
|
2014-12-03 17:07:21 +01:00
|
|
|
|
2014-12-05 17:20:48 +01:00
|
|
|
|
2015-05-29 13:17:33 +02:00
|
|
|
# Whether we should use frozen_dict in FrozenEvent. Using frozen_dicts prevents
|
|
|
|
# bugs where we accidentally share e.g. signature dicts. However, converting
|
|
|
|
# a dict to frozen_dicts is expensive.
|
|
|
|
USE_FROZEN_DICTS = True
|
|
|
|
|
|
|
|
|
2014-12-02 12:40:22 +01:00
|
|
|
class _EventInternalMetadata(object):
|
|
|
|
def __init__(self, internal_metadata_dict):
|
2015-01-30 11:30:54 +01:00
|
|
|
self.__dict__ = dict(internal_metadata_dict)
|
2014-12-02 12:40:22 +01:00
|
|
|
|
|
|
|
def get_dict(self):
|
|
|
|
return dict(self.__dict__)
|
|
|
|
|
2014-12-10 13:00:05 +01:00
|
|
|
def is_outlier(self):
|
2016-03-30 13:55:02 +02:00
|
|
|
return getattr(self, "outlier", False)
|
2014-12-10 13:00:05 +01:00
|
|
|
|
2016-03-31 16:00:42 +02:00
|
|
|
def is_invite_from_remote(self):
|
|
|
|
return getattr(self, "invite_from_remote", False)
|
|
|
|
|
2017-01-05 12:26:30 +01:00
|
|
|
def get_send_on_behalf_of(self):
|
|
|
|
"""Whether this server should send the event on behalf of another server.
|
|
|
|
This is used by the federation "send_join" API to forward the initial join
|
|
|
|
event for a server in the room.
|
|
|
|
|
|
|
|
returns a str with the name of the server this event is sent on behalf of.
|
|
|
|
"""
|
2017-01-07 05:00:42 +01:00
|
|
|
return getattr(self, "send_on_behalf_of", None)
|
2017-01-05 12:26:30 +01:00
|
|
|
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
def _event_dict_property(key):
|
2014-12-16 16:24:03 +01:00
|
|
|
def getter(self):
|
|
|
|
return self._event_dict[key]
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-16 16:24:03 +01:00
|
|
|
def setter(self, v):
|
|
|
|
self._event_dict[key] = v
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-16 16:24:03 +01:00
|
|
|
def delete(self):
|
|
|
|
del self._event_dict[key]
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-16 16:24:03 +01:00
|
|
|
return property(
|
|
|
|
getter,
|
|
|
|
setter,
|
|
|
|
delete,
|
|
|
|
)
|
2014-12-02 12:40:22 +01:00
|
|
|
|
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
class EventBase(object):
|
|
|
|
def __init__(self, event_dict, signatures={}, unsigned={},
|
2015-03-25 18:26:32 +01:00
|
|
|
internal_metadata_dict={}, rejected_reason=None):
|
2015-01-06 19:51:03 +01:00
|
|
|
self.signatures = signatures
|
|
|
|
self.unsigned = unsigned
|
2015-03-25 18:26:32 +01:00
|
|
|
self.rejected_reason = rejected_reason
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2015-01-06 19:51:03 +01:00
|
|
|
self._event_dict = event_dict
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
self.internal_metadata = _EventInternalMetadata(
|
|
|
|
internal_metadata_dict
|
|
|
|
)
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
auth_events = _event_dict_property("auth_events")
|
2014-12-05 17:20:48 +01:00
|
|
|
depth = _event_dict_property("depth")
|
2014-12-03 17:07:21 +01:00
|
|
|
content = _event_dict_property("content")
|
|
|
|
event_id = _event_dict_property("event_id")
|
|
|
|
hashes = _event_dict_property("hashes")
|
|
|
|
origin = _event_dict_property("origin")
|
2014-12-10 15:49:52 +01:00
|
|
|
origin_server_ts = _event_dict_property("origin_server_ts")
|
2014-12-03 17:07:21 +01:00
|
|
|
prev_events = _event_dict_property("prev_events")
|
|
|
|
prev_state = _event_dict_property("prev_state")
|
2014-12-11 14:25:19 +01:00
|
|
|
redacts = _event_dict_property("redacts")
|
2014-12-03 17:07:21 +01:00
|
|
|
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")
|
2014-12-02 12:40:22 +01:00
|
|
|
|
2014-12-05 17:20:48 +01:00
|
|
|
@property
|
|
|
|
def membership(self):
|
|
|
|
return self.content["membership"]
|
|
|
|
|
2014-12-04 12:27:59 +01:00
|
|
|
def is_state(self):
|
2015-02-04 11:16:51 +01:00
|
|
|
return hasattr(self, "state_key") and self.state_key is not None
|
2014-12-04 12:27:59 +01:00
|
|
|
|
2014-12-02 12:40:22 +01:00
|
|
|
def get_dict(self):
|
2014-12-04 16:50:01 +01:00
|
|
|
d = dict(self._event_dict)
|
2014-12-02 12:40:22 +01:00
|
|
|
d.update({
|
2014-12-04 16:50:01 +01:00
|
|
|
"signatures": self.signatures,
|
2015-08-21 10:36:07 +02:00
|
|
|
"unsigned": dict(self.unsigned),
|
2014-12-02 12:40:22 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
2016-08-26 12:20:59 +02:00
|
|
|
def get(self, key, default=None):
|
2014-12-10 15:02:48 +01:00
|
|
|
return self._event_dict.get(key, default)
|
|
|
|
|
2014-12-02 12:40:22 +01:00
|
|
|
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"]
|
|
|
|
|
2015-08-21 10:36:07 +02:00
|
|
|
# This may be a frozen event
|
|
|
|
pdu_json["unsigned"].pop("redacted_because", None)
|
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
return pdu_json
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
raise AttributeError("Unrecognized attribute %s" % (instance,))
|
|
|
|
|
2016-01-18 11:46:09 +01:00
|
|
|
def __getitem__(self, field):
|
|
|
|
return self._event_dict[field]
|
|
|
|
|
|
|
|
def __contains__(self, field):
|
|
|
|
return field in self._event_dict
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return self._event_dict.items()
|
|
|
|
|
2014-12-03 17:07:21 +01:00
|
|
|
|
|
|
|
class FrozenEvent(EventBase):
|
2015-03-25 18:26:32 +01:00
|
|
|
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
|
2015-01-06 19:51:03 +01:00
|
|
|
event_dict = dict(event_dict)
|
2014-12-03 17:07:21 +01:00
|
|
|
|
2015-01-06 19:51:03 +01:00
|
|
|
# Signatures is a dict of dicts, and this is faster than doing a
|
|
|
|
# copy.deepcopy
|
|
|
|
signatures = {
|
|
|
|
name: {sig_id: sig for sig_id, sig in sigs.items()}
|
|
|
|
for name, sigs in event_dict.pop("signatures", {}).items()
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned = dict(event_dict.pop("unsigned", {}))
|
2014-12-03 17:07:21 +01:00
|
|
|
|
2016-03-23 15:58:08 +01:00
|
|
|
# We intern these strings because they turn up a lot (especially when
|
|
|
|
# caching).
|
2016-03-23 17:13:05 +01:00
|
|
|
event_dict = intern_dict(event_dict)
|
2016-03-23 15:58:08 +01:00
|
|
|
|
2015-05-29 13:17:33 +02:00
|
|
|
if USE_FROZEN_DICTS:
|
|
|
|
frozen_dict = freeze(event_dict)
|
|
|
|
else:
|
2015-05-29 16:02:55 +02:00
|
|
|
frozen_dict = event_dict
|
2014-12-03 17:07:21 +01:00
|
|
|
|
|
|
|
super(FrozenEvent, self).__init__(
|
|
|
|
frozen_dict,
|
|
|
|
signatures=signatures,
|
2014-12-16 14:17:09 +01:00
|
|
|
unsigned=unsigned,
|
|
|
|
internal_metadata_dict=internal_metadata_dict,
|
2015-03-25 18:26:32 +01:00
|
|
|
rejected_reason=rejected_reason,
|
2014-12-03 17:07:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_event(event):
|
|
|
|
e = FrozenEvent(
|
2014-12-04 16:50:01 +01:00
|
|
|
event.get_pdu_json()
|
2014-12-03 17:07:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
e.internal_metadata = event.internal_metadata
|
|
|
|
|
|
|
|
return e
|
2014-12-05 17:20:48 +01:00
|
|
|
|
2014-12-10 12:38:08 +01:00
|
|
|
def __str__(self):
|
2014-12-10 15:02:48 +01:00
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
def __repr__(self):
|
2014-12-10 12:38:08 +01:00
|
|
|
return "<FrozenEvent event_id='%s', type='%s', state_key='%s'>" % (
|
2016-03-18 15:31:44 +01:00
|
|
|
self.get("event_id", None),
|
|
|
|
self.get("type", None),
|
|
|
|
self.get("state_key", None),
|
2014-12-15 17:14:34 +01:00
|
|
|
)
|