You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
5.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 matrix.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.api.errors import SynapseError, Codes
  16. from synapse.util.jsonobject import JsonEncodedObject
  17. class SynapseEvent(JsonEncodedObject):
  18. """Base class for Synapse events. These are JSON objects which must abide
  19. by a certain well-defined structure.
  20. """
  21. # Attributes that are currently assumed by the federation side:
  22. # Mandatory:
  23. # - event_id
  24. # - room_id
  25. # - type
  26. # - is_state
  27. #
  28. # Optional:
  29. # - state_key (mandatory when is_state is True)
  30. # - prev_events (these can be filled out by the federation layer itself.)
  31. # - prev_state
  32. valid_keys = [
  33. "event_id",
  34. "type",
  35. "room_id",
  36. "user_id", # sender/initiator
  37. "content", # HTTP body, JSON
  38. ]
  39. internal_keys = [
  40. "is_state",
  41. "state_key",
  42. "prev_events",
  43. "prev_state",
  44. "depth",
  45. "destinations",
  46. "origin",
  47. ]
  48. required_keys = [
  49. "event_id",
  50. "room_id",
  51. "content",
  52. ]
  53. def __init__(self, raises=True, **kwargs):
  54. super(SynapseEvent, self).__init__(**kwargs)
  55. if "content" in kwargs:
  56. self.check_json(self.content, raises=raises)
  57. def get_content_template(self):
  58. """ Retrieve the JSON template for this event as a dict.
  59. The template must be a dict representing the JSON to match. Only
  60. required keys should be present. The values of the keys in the template
  61. are checked via type() to the values of the same keys in the actual
  62. event JSON.
  63. NB: If loading content via json.loads, you MUST define strings as
  64. unicode.
  65. For example:
  66. Content:
  67. {
  68. "name": u"bob",
  69. "age": 18,
  70. "friends": [u"mike", u"jill"]
  71. }
  72. Template:
  73. {
  74. "name": u"string",
  75. "age": 0,
  76. "friends": [u"string"]
  77. }
  78. The values "string" and 0 could be anything, so long as the types
  79. are the same as the content.
  80. """
  81. raise NotImplementedError("get_content_template not implemented.")
  82. def check_json(self, content, raises=True):
  83. """Checks the given JSON content abides by the rules of the template.
  84. Args:
  85. content : A JSON object to check.
  86. raises: True to raise a SynapseError if the check fails.
  87. Returns:
  88. True if the content passes the template. Returns False if the check
  89. fails and raises=False.
  90. Raises:
  91. SynapseError if the check fails and raises=True.
  92. """
  93. # recursively call to inspect each layer
  94. err_msg = self._check_json(content, self.get_content_template())
  95. if err_msg:
  96. if raises:
  97. raise SynapseError(400, err_msg, Codes.BAD_JSON)
  98. else:
  99. return False
  100. else:
  101. return True
  102. def _check_json(self, content, template):
  103. """Check content and template matches.
  104. If the template is a dict, each key in the dict will be validated with
  105. the content, else it will just compare the types of content and
  106. template. This basic type check is required because this function will
  107. be recursively called and could be called with just strs or ints.
  108. Args:
  109. content: The content to validate.
  110. template: The validation template.
  111. Returns:
  112. str: An error message if the validation fails, else None.
  113. """
  114. if type(content) != type(template):
  115. return "Mismatched types: %s" % template
  116. if type(template) == dict:
  117. for key in template:
  118. if key not in content:
  119. return "Missing %s key" % key
  120. if type(content[key]) != type(template[key]):
  121. return "Key %s is of the wrong type." % key
  122. if type(content[key]) == dict:
  123. # we must go deeper
  124. msg = self._check_json(content[key], template[key])
  125. if msg:
  126. return msg
  127. elif type(content[key]) == list:
  128. # make sure each item type in content matches the template
  129. for entry in content[key]:
  130. msg = self._check_json(entry, template[key][0])
  131. if msg:
  132. return msg