2014-08-12 10:10:52 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2014-08-12 10:10:52 -04:00
|
|
|
#
|
|
|
|
#
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
""" Defines the JSON structure of the protocol units used by the server to
|
|
|
|
server protocol.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2021-08-06 09:39:59 -04:00
|
|
|
from typing import List, Optional
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
import attr
|
|
|
|
|
|
|
|
from synapse.types import JsonDict
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class Edu:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""An Edu represents a piece of data sent from one homeserver to another.
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
edu_type: str
|
|
|
|
content: dict
|
|
|
|
origin: str
|
|
|
|
destination: str
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-02-07 08:56:38 -05:00
|
|
|
def get_dict(self) -> JsonDict:
|
|
|
|
return {
|
|
|
|
"edu_type": self.edu_type,
|
|
|
|
"content": self.content,
|
|
|
|
}
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-02-07 08:56:38 -05: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 11:44:27 -04:00
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
def get_context(self) -> str:
|
2019-08-22 13:21:10 -04:00
|
|
|
return getattr(self, "content", {}).get("org.matrix.opentracing_context", "{}")
|
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
def strip_context(self) -> None:
|
2019-09-05 09:41:04 -04:00
|
|
|
getattr(self, "content", {})["org.matrix.opentracing_context"] = "{}"
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
def _none_to_list(edus: Optional[List[JsonDict]]) -> List[JsonDict]:
|
|
|
|
if edus is None:
|
|
|
|
return []
|
|
|
|
return edus
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class Transaction:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""A transaction is a list of Pdus and Edus to be sent to a remote home
|
|
|
|
server with some extra metadata.
|
|
|
|
|
|
|
|
Example transaction::
|
|
|
|
|
|
|
|
{
|
|
|
|
"origin": "foo",
|
|
|
|
"prev_ids": ["abc", "def"],
|
|
|
|
"pdus": [
|
|
|
|
...
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-08-06 09:39:59 -04:00
|
|
|
# Required keys.
|
|
|
|
transaction_id: str
|
|
|
|
origin: str
|
|
|
|
destination: str
|
|
|
|
origin_server_ts: int
|
|
|
|
pdus: List[JsonDict] = attr.ib(factory=list, converter=_none_to_list)
|
|
|
|
edus: List[JsonDict] = attr.ib(factory=list, converter=_none_to_list)
|
|
|
|
|
|
|
|
def get_dict(self) -> JsonDict:
|
|
|
|
"""A JSON-ready dictionary of valid keys which aren't internal."""
|
|
|
|
result = {
|
|
|
|
"origin": self.origin,
|
|
|
|
"origin_server_ts": self.origin_server_ts,
|
|
|
|
"pdus": self.pdus,
|
|
|
|
}
|
|
|
|
if self.edus:
|
|
|
|
result["edus"] = self.edus
|
|
|
|
return result
|