2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 16:10:52 +02: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.
|
2014-08-13 04:14:34 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
""" Defines the JSON structure of the protocol units used by the server to
|
|
|
|
server protocol.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2021-04-08 23:38:54 +02:00
|
|
|
from typing import Optional
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2020-02-07 14:56:38 +01:00
|
|
|
import attr
|
|
|
|
|
|
|
|
from synapse.types import JsonDict
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.util.jsonobject import JsonEncodedObject
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-02-07 14:56:38 +01:00
|
|
|
@attr.s(slots=True)
|
2014-08-12 16:10:52 +02:00
|
|
|
class Edu(JsonEncodedObject):
|
2021-02-16 23:32:34 +01:00
|
|
|
"""An Edu represents a piece of data sent from one homeserver to another.
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
In comparison to Pdus, Edus are not persisted for a long time on disk, are
|
|
|
|
not meaningful beyond a given pair of homeservers, and don't have an
|
|
|
|
internal ID or previous references graph.
|
|
|
|
"""
|
|
|
|
|
2020-02-07 14:56:38 +01:00
|
|
|
edu_type = attr.ib(type=str)
|
|
|
|
content = attr.ib(type=dict)
|
|
|
|
origin = attr.ib(type=str)
|
|
|
|
destination = attr.ib(type=str)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2020-02-07 14:56:38 +01:00
|
|
|
def get_dict(self) -> JsonDict:
|
|
|
|
return {
|
|
|
|
"edu_type": self.edu_type,
|
|
|
|
"content": self.content,
|
|
|
|
}
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2020-02-07 14:56:38 +01:00
|
|
|
def get_internal_dict(self) -> JsonDict:
|
|
|
|
return {
|
|
|
|
"edu_type": self.edu_type,
|
|
|
|
"content": self.content,
|
|
|
|
"origin": self.origin,
|
|
|
|
"destination": self.destination,
|
|
|
|
}
|
2014-10-14 17:44:27 +02:00
|
|
|
|
2019-08-22 19:21:10 +02:00
|
|
|
def get_context(self):
|
|
|
|
return getattr(self, "content", {}).get("org.matrix.opentracing_context", "{}")
|
|
|
|
|
2019-09-05 15:41:04 +02:00
|
|
|
def strip_context(self):
|
|
|
|
getattr(self, "content", {})["org.matrix.opentracing_context"] = "{}"
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
class Transaction(JsonEncodedObject):
|
2021-02-16 23:32:34 +01:00
|
|
|
"""A transaction is a list of Pdus and Edus to be sent to a remote home
|
2014-08-12 16:10:52 +02:00
|
|
|
server with some extra metadata.
|
|
|
|
|
|
|
|
Example transaction::
|
|
|
|
|
|
|
|
{
|
|
|
|
"origin": "foo",
|
|
|
|
"prev_ids": ["abc", "def"],
|
|
|
|
"pdus": [
|
|
|
|
...
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
valid_keys = [
|
|
|
|
"transaction_id",
|
|
|
|
"origin",
|
|
|
|
"destination",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-08-12 16:10:52 +02:00
|
|
|
"previous_ids",
|
|
|
|
"pdus",
|
|
|
|
"edus",
|
|
|
|
]
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
internal_keys = ["transaction_id", "destination"]
|
2014-10-13 12:49:40 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
required_keys = [
|
|
|
|
"transaction_id",
|
|
|
|
"origin",
|
|
|
|
"destination",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-08-12 16:10:52 +02:00
|
|
|
"pdus",
|
|
|
|
]
|
|
|
|
|
2021-04-08 23:38:54 +02:00
|
|
|
def __init__(self, transaction_id=None, pdus: Optional[list] = None, **kwargs):
|
2021-02-16 23:32:34 +01:00
|
|
|
"""If we include a list of pdus then we decode then as PDU's
|
2014-08-12 16:10:52 +02:00
|
|
|
automatically.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# If there's no EDUs then remove the arg
|
|
|
|
if "edus" in kwargs and not kwargs["edus"]:
|
|
|
|
del kwargs["edus"]
|
|
|
|
|
2021-04-08 23:38:54 +02:00
|
|
|
super().__init__(transaction_id=transaction_id, pdus=pdus or [], **kwargs)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_new(pdus, **kwargs):
|
2021-02-16 23:32:34 +01:00
|
|
|
"""Used to create a new transaction. Will auto fill out
|
2014-10-17 18:12:25 +02:00
|
|
|
transaction_id and origin_server_ts keys.
|
2014-08-12 16:10:52 +02:00
|
|
|
"""
|
2014-10-17 18:12:25 +02:00
|
|
|
if "origin_server_ts" not in kwargs:
|
2019-06-20 11:32:02 +02:00
|
|
|
raise KeyError("Require 'origin_server_ts' to construct a Transaction")
|
2014-08-12 16:10:52 +02:00
|
|
|
if "transaction_id" not in kwargs:
|
2019-06-20 11:32:02 +02:00
|
|
|
raise KeyError("Require 'transaction_id' to construct a Transaction")
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-11-14 22:25:02 +01:00
|
|
|
kwargs["pdus"] = [p.get_pdu_json() for p in pdus]
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
return Transaction(**kwargs)
|