2014-12-02 04:40:22 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2019-01-29 10:26:24 -07:00
|
|
|
# Copyright 2019 New Vector Ltd
|
2014-12-02 04:40:22 -07: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.
|
|
|
|
|
2018-10-02 06:53:47 -06:00
|
|
|
import os
|
|
|
|
from distutils.util import strtobool
|
|
|
|
|
2018-09-13 08:33:16 -06:00
|
|
|
import six
|
|
|
|
|
2019-01-29 10:26:24 -07:00
|
|
|
from unpaddedbase64 import encode_base64
|
|
|
|
|
2019-05-21 06:47:25 -06:00
|
|
|
from synapse.api.errors import UnsupportedRoomVersionError
|
2019-04-01 03:24:38 -06:00
|
|
|
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, EventFormatVersions
|
2016-03-23 10:13:05 -06:00
|
|
|
from synapse.util.caches import intern_dict
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.util.frozenutils import freeze
|
2014-12-05 09:20:48 -07:00
|
|
|
|
2015-05-29 05:17:33 -06:00
|
|
|
# Whether we should use frozen_dict in FrozenEvent. Using frozen_dicts prevents
|
2018-10-02 06:53:47 -06:00
|
|
|
# bugs where we accidentally share e.g. signature dicts. However, converting a
|
|
|
|
# dict to frozen_dicts is expensive.
|
|
|
|
#
|
|
|
|
# NOTE: This is overridden by the configuration by the Synapse worker apps, but
|
|
|
|
# for the sake of tests, it is set here while it cannot be configured on the
|
|
|
|
# homeserver object itself.
|
|
|
|
USE_FROZEN_DICTS = strtobool(os.environ.get("SYNAPSE_USE_FROZEN_DICTS", "0"))
|
2015-05-29 05:17:33 -06:00
|
|
|
|
|
|
|
|
2014-12-02 04:40:22 -07:00
|
|
|
class _EventInternalMetadata(object):
|
|
|
|
def __init__(self, internal_metadata_dict):
|
2015-01-30 03:30:54 -07:00
|
|
|
self.__dict__ = dict(internal_metadata_dict)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
|
|
|
def get_dict(self):
|
|
|
|
return dict(self.__dict__)
|
|
|
|
|
2014-12-10 05:00:05 -07:00
|
|
|
def is_outlier(self):
|
2016-03-30 05:55:02 -06:00
|
|
|
return getattr(self, "outlier", False)
|
2014-12-10 05:00:05 -07:00
|
|
|
|
2019-01-24 10:33:19 -07:00
|
|
|
def is_out_of_band_membership(self):
|
|
|
|
"""Whether this is an out of band membership, like an invite or an invite
|
2019-01-23 13:05:44 -07:00
|
|
|
rejection. This is needed as those events are marked as outliers, but
|
2019-01-24 10:33:19 -07:00
|
|
|
they still need to be processed as if they're new events (e.g. updating
|
|
|
|
invite state in the database, relaying to clients, etc).
|
2019-01-23 13:05:44 -07:00
|
|
|
"""
|
2019-01-24 10:33:19 -07:00
|
|
|
return getattr(self, "out_of_band_membership", False)
|
2016-03-31 08:00:42 -06:00
|
|
|
|
2017-01-05 04:26:30 -07: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-06 21:00:42 -07:00
|
|
|
return getattr(self, "send_on_behalf_of", None)
|
2017-01-05 04:26:30 -07:00
|
|
|
|
2019-01-28 14:09:45 -07:00
|
|
|
def need_to_check_redaction(self):
|
2019-01-29 14:22:47 -07:00
|
|
|
"""Whether the redaction event needs to be rechecked when fetching
|
|
|
|
from the database.
|
|
|
|
|
|
|
|
Starting in room v3 redaction events are accepted up front, and later
|
|
|
|
checked to see if the redacter and redactee's domains match.
|
|
|
|
|
2019-01-29 15:55:29 -07:00
|
|
|
If the sender of the redaction event is allowed to redact any event
|
|
|
|
due to auth rules, then this will always return false.
|
2019-01-29 14:22:47 -07:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool
|
|
|
|
"""
|
2019-01-28 14:09:45 -07:00
|
|
|
return getattr(self, "recheck_redaction", False)
|
|
|
|
|
2019-02-12 03:31:21 -07:00
|
|
|
def is_soft_failed(self):
|
|
|
|
"""Whether the event has been soft failed.
|
|
|
|
|
|
|
|
Soft failed events should be handled as usual, except:
|
|
|
|
1. They should not go down sync or event streams, or generally
|
|
|
|
sent to clients.
|
|
|
|
2. They should not be added to the forward extremities (and
|
|
|
|
therefore not to current state).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool
|
|
|
|
"""
|
|
|
|
return getattr(self, "soft_failed", False)
|
|
|
|
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
def _event_dict_property(key):
|
2018-04-30 01:49:10 -06:00
|
|
|
# We want to be able to use hasattr with the event dict properties.
|
|
|
|
# However, (on python3) hasattr expects AttributeError to be raised. Hence,
|
|
|
|
# we need to transform the KeyError into an AttributeError
|
2014-12-16 08:24:03 -07:00
|
|
|
def getter(self):
|
2018-04-15 09:24:25 -06:00
|
|
|
try:
|
|
|
|
return self._event_dict[key]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(key)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-16 08:24:03 -07:00
|
|
|
def setter(self, v):
|
2018-04-15 09:24:25 -06:00
|
|
|
try:
|
|
|
|
self._event_dict[key] = v
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(key)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-16 08:24:03 -07:00
|
|
|
def delete(self):
|
2018-04-15 09:24:25 -06:00
|
|
|
try:
|
|
|
|
del self._event_dict[key]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(key)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-16 08:24:03 -07:00
|
|
|
return property(
|
|
|
|
getter,
|
|
|
|
setter,
|
|
|
|
delete,
|
|
|
|
)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
class EventBase(object):
|
|
|
|
def __init__(self, event_dict, signatures={}, unsigned={},
|
2015-03-25 11:26:32 -06:00
|
|
|
internal_metadata_dict={}, rejected_reason=None):
|
2015-01-06 11:51:03 -07:00
|
|
|
self.signatures = signatures
|
|
|
|
self.unsigned = unsigned
|
2015-03-25 11:26:32 -06:00
|
|
|
self.rejected_reason = rejected_reason
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2015-01-06 11:51:03 -07:00
|
|
|
self._event_dict = event_dict
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
self.internal_metadata = _EventInternalMetadata(
|
|
|
|
internal_metadata_dict
|
|
|
|
)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
auth_events = _event_dict_property("auth_events")
|
2014-12-05 09:20:48 -07:00
|
|
|
depth = _event_dict_property("depth")
|
2014-12-03 09:07:21 -07:00
|
|
|
content = _event_dict_property("content")
|
|
|
|
hashes = _event_dict_property("hashes")
|
|
|
|
origin = _event_dict_property("origin")
|
2014-12-10 07:49:52 -07:00
|
|
|
origin_server_ts = _event_dict_property("origin_server_ts")
|
2014-12-03 09:07:21 -07:00
|
|
|
prev_events = _event_dict_property("prev_events")
|
2014-12-11 06:25:19 -07:00
|
|
|
redacts = _event_dict_property("redacts")
|
2014-12-03 09:07:21 -07:00
|
|
|
room_id = _event_dict_property("room_id")
|
|
|
|
sender = _event_dict_property("sender")
|
|
|
|
user_id = _event_dict_property("sender")
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2014-12-05 09:20:48 -07:00
|
|
|
@property
|
|
|
|
def membership(self):
|
|
|
|
return self.content["membership"]
|
|
|
|
|
2014-12-04 04:27:59 -07:00
|
|
|
def is_state(self):
|
2015-02-04 03:16:51 -07:00
|
|
|
return hasattr(self, "state_key") and self.state_key is not None
|
2014-12-04 04:27:59 -07:00
|
|
|
|
2014-12-02 04:40:22 -07:00
|
|
|
def get_dict(self):
|
2014-12-04 08:50:01 -07:00
|
|
|
d = dict(self._event_dict)
|
2014-12-02 04:40:22 -07:00
|
|
|
d.update({
|
2014-12-04 08:50:01 -07:00
|
|
|
"signatures": self.signatures,
|
2015-08-21 02:36:07 -06:00
|
|
|
"unsigned": dict(self.unsigned),
|
2014-12-02 04:40:22 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
return d
|
|
|
|
|
2016-08-26 04:20:59 -06:00
|
|
|
def get(self, key, default=None):
|
2014-12-10 07:02:48 -07:00
|
|
|
return self._event_dict.get(key, default)
|
|
|
|
|
2014-12-02 04:40:22 -07: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 02:36:07 -06:00
|
|
|
# This may be a frozen event
|
|
|
|
pdu_json["unsigned"].pop("redacted_because", None)
|
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
return pdu_json
|
|
|
|
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
raise AttributeError("Unrecognized attribute %s" % (instance,))
|
|
|
|
|
2016-01-18 03:46:09 -07:00
|
|
|
def __getitem__(self, field):
|
|
|
|
return self._event_dict[field]
|
|
|
|
|
|
|
|
def __contains__(self, field):
|
|
|
|
return field in self._event_dict
|
|
|
|
|
|
|
|
def items(self):
|
2018-05-31 03:03:47 -06:00
|
|
|
return list(self._event_dict.items())
|
2016-01-18 03:46:09 -07:00
|
|
|
|
2018-09-13 08:33:16 -06:00
|
|
|
def keys(self):
|
|
|
|
return six.iterkeys(self._event_dict)
|
|
|
|
|
2018-11-05 06:35:15 -07:00
|
|
|
def prev_event_ids(self):
|
|
|
|
"""Returns the list of prev event IDs. The order matches the order
|
|
|
|
specified in the event, though there is no meaning to it.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The list of event IDs of this event's prev_events
|
|
|
|
"""
|
|
|
|
return [e for e, _ in self.prev_events]
|
|
|
|
|
|
|
|
def auth_event_ids(self):
|
|
|
|
"""Returns the list of auth event IDs. The order matches the order
|
|
|
|
specified in the event, though there is no meaning to it.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The list of event IDs of this event's auth_events
|
|
|
|
"""
|
|
|
|
return [e for e, _ in self.auth_events]
|
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
|
|
|
|
class FrozenEvent(EventBase):
|
2019-01-23 04:11:52 -07:00
|
|
|
format_version = EventFormatVersions.V1 # All events of this type are V1
|
|
|
|
|
2015-03-25 11:26:32 -06:00
|
|
|
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
|
2015-01-06 11:51:03 -07:00
|
|
|
event_dict = dict(event_dict)
|
2014-12-03 09:07:21 -07:00
|
|
|
|
2015-01-06 11:51:03 -07: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 09:07:21 -07:00
|
|
|
|
2016-03-23 08:58:08 -06:00
|
|
|
# We intern these strings because they turn up a lot (especially when
|
|
|
|
# caching).
|
2016-03-23 10:13:05 -06:00
|
|
|
event_dict = intern_dict(event_dict)
|
2016-03-23 08:58:08 -06:00
|
|
|
|
2015-05-29 05:17:33 -06:00
|
|
|
if USE_FROZEN_DICTS:
|
|
|
|
frozen_dict = freeze(event_dict)
|
|
|
|
else:
|
2015-05-29 08:02:55 -06:00
|
|
|
frozen_dict = event_dict
|
2014-12-03 09:07:21 -07:00
|
|
|
|
2017-01-13 06:16:54 -07:00
|
|
|
self.event_id = event_dict["event_id"]
|
|
|
|
self.type = event_dict["type"]
|
|
|
|
if "state_key" in event_dict:
|
|
|
|
self.state_key = event_dict["state_key"]
|
|
|
|
|
2014-12-03 09:07:21 -07:00
|
|
|
super(FrozenEvent, self).__init__(
|
|
|
|
frozen_dict,
|
|
|
|
signatures=signatures,
|
2014-12-16 06:17:09 -07:00
|
|
|
unsigned=unsigned,
|
|
|
|
internal_metadata_dict=internal_metadata_dict,
|
2015-03-25 11:26:32 -06:00
|
|
|
rejected_reason=rejected_reason,
|
2014-12-03 09:07:21 -07:00
|
|
|
)
|
|
|
|
|
2019-01-29 10:26:24 -07:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<FrozenEvent event_id='%s', type='%s', state_key='%s'>" % (
|
|
|
|
self.get("event_id", None),
|
|
|
|
self.get("type", None),
|
|
|
|
self.get("state_key", None),
|
2014-12-03 09:07:21 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-01-29 10:26:24 -07:00
|
|
|
class FrozenEventV2(EventBase):
|
|
|
|
format_version = EventFormatVersions.V2 # All events of this type are V2
|
|
|
|
|
|
|
|
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
|
|
|
|
event_dict = dict(event_dict)
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert "event_id" not in event_dict
|
|
|
|
|
|
|
|
unsigned = dict(event_dict.pop("unsigned", {}))
|
|
|
|
|
|
|
|
# We intern these strings because they turn up a lot (especially when
|
|
|
|
# caching).
|
|
|
|
event_dict = intern_dict(event_dict)
|
|
|
|
|
|
|
|
if USE_FROZEN_DICTS:
|
|
|
|
frozen_dict = freeze(event_dict)
|
|
|
|
else:
|
|
|
|
frozen_dict = event_dict
|
|
|
|
|
|
|
|
self._event_id = None
|
|
|
|
self.type = event_dict["type"]
|
|
|
|
if "state_key" in event_dict:
|
|
|
|
self.state_key = event_dict["state_key"]
|
|
|
|
|
|
|
|
super(FrozenEventV2, self).__init__(
|
|
|
|
frozen_dict,
|
|
|
|
signatures=signatures,
|
|
|
|
unsigned=unsigned,
|
|
|
|
internal_metadata_dict=internal_metadata_dict,
|
|
|
|
rejected_reason=rejected_reason,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def event_id(self):
|
|
|
|
# We have to import this here as otherwise we get an import loop which
|
|
|
|
# is hard to break.
|
|
|
|
from synapse.crypto.event_signing import compute_event_reference_hash
|
|
|
|
|
|
|
|
if self._event_id:
|
|
|
|
return self._event_id
|
|
|
|
self._event_id = "$" + encode_base64(compute_event_reference_hash(self)[1])
|
|
|
|
return self._event_id
|
|
|
|
|
|
|
|
def prev_event_ids(self):
|
|
|
|
"""Returns the list of prev event IDs. The order matches the order
|
|
|
|
specified in the event, though there is no meaning to it.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The list of event IDs of this event's prev_events
|
|
|
|
"""
|
|
|
|
return self.prev_events
|
|
|
|
|
|
|
|
def auth_event_ids(self):
|
|
|
|
"""Returns the list of auth event IDs. The order matches the order
|
|
|
|
specified in the event, though there is no meaning to it.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: The list of event IDs of this event's auth_events
|
|
|
|
"""
|
|
|
|
return self.auth_events
|
2014-12-05 09:20:48 -07:00
|
|
|
|
2014-12-10 04:38:08 -07:00
|
|
|
def __str__(self):
|
2014-12-10 07:02:48 -07:00
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
def __repr__(self):
|
2019-05-20 08:54:42 -06:00
|
|
|
return "<%s event_id='%s', type='%s', state_key='%s'>" % (
|
|
|
|
self.__class__.__name__,
|
2019-01-29 10:26:24 -07:00
|
|
|
self.event_id,
|
2016-03-18 08:31:44 -06:00
|
|
|
self.get("type", None),
|
|
|
|
self.get("state_key", None),
|
2014-12-15 09:14:34 -07:00
|
|
|
)
|
2019-01-23 09:50:06 -07:00
|
|
|
|
|
|
|
|
2019-05-20 08:54:42 -06:00
|
|
|
class FrozenEventV3(FrozenEventV2):
|
|
|
|
"""FrozenEventV3, which differs from FrozenEventV2 only in the event_id format"""
|
|
|
|
format_version = EventFormatVersions.V3 # All events of this type are V3
|
|
|
|
|
|
|
|
@property
|
|
|
|
def event_id(self):
|
|
|
|
# We have to import this here as otherwise we get an import loop which
|
|
|
|
# is hard to break.
|
|
|
|
from synapse.crypto.event_signing import compute_event_reference_hash
|
|
|
|
|
|
|
|
if self._event_id:
|
|
|
|
return self._event_id
|
|
|
|
self._event_id = "$" + encode_base64(
|
|
|
|
compute_event_reference_hash(self)[1], urlsafe=True
|
|
|
|
)
|
|
|
|
return self._event_id
|
|
|
|
|
|
|
|
|
2019-01-23 09:50:06 -07:00
|
|
|
def room_version_to_event_format(room_version):
|
|
|
|
"""Converts a room version string to the event format
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_version (str)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
2019-05-21 06:47:25 -06:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
UnsupportedRoomVersionError if the room version is unknown
|
2019-01-23 09:50:06 -07:00
|
|
|
"""
|
2019-04-01 03:24:38 -06:00
|
|
|
v = KNOWN_ROOM_VERSIONS.get(room_version)
|
|
|
|
|
|
|
|
if not v:
|
2019-05-21 06:47:25 -06:00
|
|
|
# this can happen if support is withdrawn for a room version
|
|
|
|
raise UnsupportedRoomVersionError()
|
2019-01-23 09:50:06 -07:00
|
|
|
|
2019-04-01 03:24:38 -06:00
|
|
|
return v.event_format
|
2019-01-23 13:21:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
def event_type_from_format_version(format_version):
|
|
|
|
"""Returns the python type to use to construct an Event object for the
|
|
|
|
given event format version.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
format_version (int): The event format version
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
type: A type that can be initialized as per the initializer of
|
|
|
|
`FrozenEvent`
|
|
|
|
"""
|
2019-01-29 10:26:24 -07:00
|
|
|
|
|
|
|
if format_version == EventFormatVersions.V1:
|
|
|
|
return FrozenEvent
|
|
|
|
elif format_version == EventFormatVersions.V2:
|
|
|
|
return FrozenEventV2
|
2019-05-20 08:54:42 -06:00
|
|
|
elif format_version == EventFormatVersions.V3:
|
|
|
|
return FrozenEventV3
|
2019-01-29 10:26:24 -07:00
|
|
|
else:
|
2019-01-23 13:21:33 -07:00
|
|
|
raise Exception(
|
|
|
|
"No event format %r" % (format_version,)
|
|
|
|
)
|