2014-12-02 04:40:22 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket 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.
|
2020-01-29 10:58:01 -07:00
|
|
|
from typing import Optional
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
import attr
|
2020-01-29 10:58:01 -07:00
|
|
|
from nacl.signing import SigningKey
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2019-04-01 03:24:38 -06:00
|
|
|
from synapse.api.constants import MAX_DEPTH
|
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 (
|
2019-01-25 10:19:31 -07:00
|
|
|
KNOWN_EVENT_FORMAT_VERSIONS,
|
|
|
|
KNOWN_ROOM_VERSIONS,
|
2019-01-29 10:26:24 -07:00
|
|
|
EventFormatVersions,
|
2020-01-29 10:55:48 -07:00
|
|
|
RoomVersion,
|
2019-01-25 10:19:31 -07:00
|
|
|
)
|
|
|
|
from synapse.crypto.event_signing import add_hashes_and_signatures
|
2020-02-07 08:30:04 -07:00
|
|
|
from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
|
2020-01-29 10:58:01 -07:00
|
|
|
from synapse.types import EventID, JsonDict
|
|
|
|
from synapse.util import Clock
|
2014-12-02 04:40:22 -07:00
|
|
|
from synapse.util.stringutils import random_string
|
|
|
|
|
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
@attr.s(slots=True, cmp=False, frozen=True)
|
|
|
|
class EventBuilder(object):
|
|
|
|
"""A format independent event builder used to build up the event content
|
|
|
|
before signing the event.
|
2019-01-23 13:21:33 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
(Note that while objects of this class are frozen, the
|
|
|
|
content/unsigned/internal_metadata fields are still mutable)
|
2019-01-23 13:21:33 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
Attributes:
|
2020-01-29 10:55:48 -07:00
|
|
|
room_version: Version of the target room
|
2019-01-25 10:19:31 -07:00
|
|
|
room_id (str)
|
|
|
|
type (str)
|
|
|
|
sender (str)
|
|
|
|
content (dict)
|
|
|
|
unsigned (dict)
|
|
|
|
internal_metadata (_EventInternalMetadata)
|
|
|
|
|
|
|
|
_state (StateHandler)
|
|
|
|
_auth (synapse.api.Auth)
|
|
|
|
_store (DataStore)
|
|
|
|
_clock (Clock)
|
|
|
|
_hostname (str): The hostname of the server creating the event
|
|
|
|
_signing_key: The signing key to use to sign the event as the server
|
2019-01-23 13:21:33 -07:00
|
|
|
"""
|
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
_state = attr.ib()
|
|
|
|
_auth = attr.ib()
|
|
|
|
_store = attr.ib()
|
|
|
|
_clock = attr.ib()
|
|
|
|
_hostname = attr.ib()
|
|
|
|
_signing_key = attr.ib()
|
|
|
|
|
2020-01-29 10:55:48 -07:00
|
|
|
room_version = attr.ib(type=RoomVersion)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
|
|
room_id = attr.ib()
|
|
|
|
type = attr.ib()
|
|
|
|
sender = attr.ib()
|
|
|
|
|
|
|
|
content = attr.ib(default=attr.Factory(dict))
|
|
|
|
unsigned = attr.ib(default=attr.Factory(dict))
|
|
|
|
|
|
|
|
# These only exist on a subset of events, so they raise AttributeError if
|
|
|
|
# someone tries to get them when they don't exist.
|
|
|
|
_state_key = attr.ib(default=None)
|
|
|
|
_redacts = attr.ib(default=None)
|
2019-05-24 07:44:04 -06:00
|
|
|
_origin_server_ts = attr.ib(default=None)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
internal_metadata = attr.ib(
|
|
|
|
default=attr.Factory(lambda: _EventInternalMetadata({}))
|
|
|
|
)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state_key(self):
|
|
|
|
if self._state_key is not None:
|
|
|
|
return self._state_key
|
|
|
|
|
|
|
|
raise AttributeError("state_key")
|
|
|
|
|
|
|
|
def is_state(self):
|
|
|
|
return self._state_key is not None
|
2019-01-23 13:21:33 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def build(self, prev_event_ids):
|
|
|
|
"""Transform into a fully signed and hashed event
|
2014-12-11 06:25:19 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
Args:
|
|
|
|
prev_event_ids (list[str]): The event IDs to use as the prev events
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Deferred[FrozenEvent]
|
|
|
|
"""
|
|
|
|
|
|
|
|
state_ids = yield self._state.get_current_state_ids(
|
2019-06-20 03:32:02 -06:00
|
|
|
self.room_id, prev_event_ids
|
2014-12-03 09:07:21 -07:00
|
|
|
)
|
2019-06-20 03:32:02 -06:00
|
|
|
auth_ids = yield self._auth.compute_auth_events(self, state_ids)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2020-01-29 10:55:48 -07:00
|
|
|
format_version = self.room_version.event_format
|
|
|
|
if format_version == EventFormatVersions.V1:
|
2019-01-29 10:26:24 -07:00
|
|
|
auth_events = yield self._store.add_event_hashes(auth_ids)
|
|
|
|
prev_events = yield self._store.add_event_hashes(prev_event_ids)
|
|
|
|
else:
|
|
|
|
auth_events = auth_ids
|
|
|
|
prev_events = prev_event_ids
|
2017-01-13 06:16:54 -07:00
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
old_depth = yield self._store.get_max_depth_of(prev_event_ids)
|
2019-01-25 10:19:31 -07:00
|
|
|
depth = old_depth + 1
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
# we cap depth of generated events, to ensure that they are not
|
|
|
|
# rejected by other servers (and so that they can be persisted in
|
|
|
|
# the db)
|
|
|
|
depth = min(depth, MAX_DEPTH)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
event_dict = {
|
|
|
|
"auth_events": auth_events,
|
|
|
|
"prev_events": prev_events,
|
|
|
|
"type": self.type,
|
|
|
|
"room_id": self.room_id,
|
|
|
|
"sender": self.sender,
|
|
|
|
"content": self.content,
|
|
|
|
"unsigned": self.unsigned,
|
|
|
|
"depth": depth,
|
|
|
|
"prev_state": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.is_state():
|
|
|
|
event_dict["state_key"] = self._state_key
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
if self._redacts is not None:
|
|
|
|
event_dict["redacts"] = self._redacts
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-05-24 07:44:04 -06:00
|
|
|
if self._origin_server_ts is not None:
|
|
|
|
event_dict["origin_server_ts"] = self._origin_server_ts
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return create_local_event_from_event_dict(
|
|
|
|
clock=self._clock,
|
|
|
|
hostname=self._hostname,
|
|
|
|
signing_key=self._signing_key,
|
2020-01-29 10:58:01 -07:00
|
|
|
room_version=self.room_version,
|
2019-07-23 07:00:55 -06:00
|
|
|
event_dict=event_dict,
|
|
|
|
internal_metadata_dict=self.internal_metadata.get_dict(),
|
2019-01-25 10:19:31 -07:00
|
|
|
)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
class EventBuilderFactory(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.hostname = hs.hostname
|
|
|
|
self.signing_key = hs.config.signing_key[0]
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.state = hs.get_state_handler()
|
|
|
|
self.auth = hs.get_auth()
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
def new(self, room_version, key_values):
|
2019-01-23 13:21:33 -07:00
|
|
|
"""Generate an event builder appropriate for the given room version
|
|
|
|
|
2019-04-01 03:24:38 -06:00
|
|
|
Deprecated: use for_room_version with a RoomVersion object instead
|
|
|
|
|
2019-01-23 13:21:33 -07:00
|
|
|
Args:
|
2019-04-01 03:24:38 -06:00
|
|
|
room_version (str): Version of the room that we're creating an event builder
|
|
|
|
for
|
2019-01-23 13:21:33 -07:00
|
|
|
key_values (dict): Fields used as the basis of the new event
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
EventBuilder
|
|
|
|
"""
|
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-04-01 03:24:38 -06:00
|
|
|
return self.for_room_version(v, key_values)
|
2019-01-23 13:21:33 -07:00
|
|
|
|
2019-04-01 03:24:38 -06:00
|
|
|
def for_room_version(self, room_version, key_values):
|
|
|
|
"""Generate an event builder appropriate for the given room version
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_version (synapse.api.room_versions.RoomVersion):
|
|
|
|
Version of the room that we're creating an event builder for
|
|
|
|
key_values (dict): Fields used as the basis of the new event
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
EventBuilder
|
|
|
|
"""
|
2019-01-25 10:19:31 -07:00
|
|
|
return EventBuilder(
|
|
|
|
store=self.store,
|
|
|
|
state=self.state,
|
|
|
|
auth=self.auth,
|
|
|
|
clock=self.clock,
|
|
|
|
hostname=self.hostname,
|
|
|
|
signing_key=self.signing_key,
|
2020-01-29 10:55:48 -07:00
|
|
|
room_version=room_version,
|
2019-01-25 10:19:31 -07:00
|
|
|
type=key_values["type"],
|
|
|
|
state_key=key_values.get("state_key"),
|
|
|
|
room_id=key_values["room_id"],
|
|
|
|
sender=key_values["sender"],
|
|
|
|
content=key_values.get("content", {}),
|
|
|
|
unsigned=key_values.get("unsigned", {}),
|
|
|
|
redacts=key_values.get("redacts", None),
|
2019-05-24 07:44:04 -06:00
|
|
|
origin_server_ts=key_values.get("origin_server_ts", None),
|
2019-01-25 10:19:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
def create_local_event_from_event_dict(
|
2020-01-29 10:58:01 -07:00
|
|
|
clock: Clock,
|
|
|
|
hostname: str,
|
|
|
|
signing_key: SigningKey,
|
|
|
|
room_version: RoomVersion,
|
|
|
|
event_dict: JsonDict,
|
|
|
|
internal_metadata_dict: Optional[JsonDict] = None,
|
|
|
|
) -> EventBase:
|
2019-01-25 10:19:31 -07:00
|
|
|
"""Takes a fully formed event dict, ensuring that fields like `origin`
|
|
|
|
and `origin_server_ts` have correct values for a locally produced event,
|
|
|
|
then signs and hashes it.
|
|
|
|
"""
|
|
|
|
|
2020-01-29 10:58:01 -07:00
|
|
|
format_version = room_version.event_format
|
2019-01-25 10:19:31 -07:00
|
|
|
if format_version not in KNOWN_EVENT_FORMAT_VERSIONS:
|
2019-06-20 03:32:02 -06:00
|
|
|
raise Exception("No event format defined for version %r" % (format_version,))
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
|
|
if internal_metadata_dict is None:
|
|
|
|
internal_metadata_dict = {}
|
|
|
|
|
|
|
|
time_now = int(clock.time_msec())
|
|
|
|
|
2019-01-29 10:26:24 -07:00
|
|
|
if format_version == EventFormatVersions.V1:
|
|
|
|
event_dict["event_id"] = _create_event_id(clock, hostname)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
|
|
event_dict["origin"] = hostname
|
2019-05-24 07:44:04 -06:00
|
|
|
event_dict.setdefault("origin_server_ts", time_now)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
|
|
event_dict.setdefault("unsigned", {})
|
|
|
|
age = event_dict["unsigned"].pop("age", 0)
|
|
|
|
event_dict["unsigned"].setdefault("age_ts", time_now - age)
|
|
|
|
|
|
|
|
event_dict.setdefault("signatures", {})
|
|
|
|
|
2020-01-29 10:40:33 -07:00
|
|
|
add_hashes_and_signatures(room_version, event_dict, hostname, signing_key)
|
2020-02-07 08:30:04 -07:00
|
|
|
return make_event_from_dict(
|
|
|
|
event_dict, room_version, internal_metadata_dict=internal_metadata_dict
|
2019-01-25 10:19:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# A counter used when generating new event IDs
|
|
|
|
_event_id_counter = 0
|
|
|
|
|
|
|
|
|
|
|
|
def _create_event_id(clock, hostname):
|
|
|
|
"""Create a new event ID
|
|
|
|
|
|
|
|
Args:
|
|
|
|
clock (Clock)
|
|
|
|
hostname (str): The server name for the event ID
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str
|
|
|
|
"""
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
global _event_id_counter
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
i = str(_event_id_counter)
|
|
|
|
_event_id_counter += 1
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
local_part = str(int(clock.time())) + i + random_string(5)
|
2014-12-02 04:40:22 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
e_id = EventID(local_part, hostname)
|
2014-12-10 03:06:12 -07:00
|
|
|
|
2019-01-25 10:19:31 -07:00
|
|
|
return e_id.to_string()
|