25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 lines
2.9 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ Defines the JSON structure of the protocol units used by the server to
  15. server protocol.
  16. """
  17. import logging
  18. from typing import List, Optional
  19. import attr
  20. from synapse.types import JsonDict
  21. logger = logging.getLogger(__name__)
  22. @attr.s(slots=True, frozen=True, auto_attribs=True)
  23. class Edu:
  24. """An Edu represents a piece of data sent from one homeserver to another.
  25. In comparison to Pdus, Edus are not persisted for a long time on disk, are
  26. not meaningful beyond a given pair of homeservers, and don't have an
  27. internal ID or previous references graph.
  28. """
  29. edu_type: str
  30. content: dict
  31. origin: str
  32. destination: str
  33. def get_dict(self) -> JsonDict:
  34. return {
  35. "edu_type": self.edu_type,
  36. "content": self.content,
  37. }
  38. def get_internal_dict(self) -> JsonDict:
  39. return {
  40. "edu_type": self.edu_type,
  41. "content": self.content,
  42. "origin": self.origin,
  43. "destination": self.destination,
  44. }
  45. def get_context(self) -> str:
  46. return getattr(self, "content", {}).get("org.matrix.opentracing_context", "{}")
  47. def strip_context(self) -> None:
  48. getattr(self, "content", {})["org.matrix.opentracing_context"] = "{}"
  49. def _none_to_list(edus: Optional[List[JsonDict]]) -> List[JsonDict]:
  50. if edus is None:
  51. return []
  52. return edus
  53. @attr.s(slots=True, frozen=True, auto_attribs=True)
  54. class Transaction:
  55. """A transaction is a list of Pdus and Edus to be sent to a remote home
  56. server with some extra metadata.
  57. Example transaction::
  58. {
  59. "origin": "foo",
  60. "prev_ids": ["abc", "def"],
  61. "pdus": [
  62. ...
  63. ],
  64. }
  65. """
  66. # Required keys.
  67. transaction_id: str
  68. origin: str
  69. destination: str
  70. origin_server_ts: int
  71. pdus: List[JsonDict] = attr.ib(factory=list, converter=_none_to_list)
  72. edus: List[JsonDict] = attr.ib(factory=list, converter=_none_to_list)
  73. def get_dict(self) -> JsonDict:
  74. """A JSON-ready dictionary of valid keys which aren't internal."""
  75. result = {
  76. "origin": self.origin,
  77. "origin_server_ts": self.origin_server_ts,
  78. "pdus": self.pdus,
  79. }
  80. if self.edus:
  81. result["edus"] = self.edus
  82. return result