utils.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  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. import re
  16. from six import string_types
  17. from frozendict import frozendict
  18. from synapse.api.constants import EventTypes
  19. from . import EventBase
  20. # Split strings on "." but not "\." This uses a negative lookbehind assertion for '\'
  21. # (?<!stuff) matches if the current position in the string is not preceded
  22. # by a match for 'stuff'.
  23. # TODO: This is fast, but fails to handle "foo\\.bar" which should be treated as
  24. # the literal fields "foo\" and "bar" but will instead be treated as "foo\\.bar"
  25. SPLIT_FIELD_REGEX = re.compile(r'(?<!\\)\.')
  26. def prune_event(event):
  27. """ Returns a pruned version of the given event, which removes all keys we
  28. don't know about or think could potentially be dodgy.
  29. This is used when we "redact" an event. We want to remove all fields that
  30. the user has specified, but we do want to keep necessary information like
  31. type, state_key etc.
  32. """
  33. event_type = event.type
  34. allowed_keys = [
  35. "event_id",
  36. "sender",
  37. "room_id",
  38. "hashes",
  39. "signatures",
  40. "content",
  41. "type",
  42. "state_key",
  43. "depth",
  44. "prev_events",
  45. "prev_state",
  46. "auth_events",
  47. "origin",
  48. "origin_server_ts",
  49. "membership",
  50. ]
  51. event_dict = event.get_dict()
  52. new_content = {}
  53. def add_fields(*fields):
  54. for field in fields:
  55. if field in event.content:
  56. new_content[field] = event_dict["content"][field]
  57. if event_type == EventTypes.Member:
  58. add_fields("membership")
  59. elif event_type == EventTypes.Create:
  60. add_fields("creator")
  61. elif event_type == EventTypes.JoinRules:
  62. add_fields("join_rule")
  63. elif event_type == EventTypes.PowerLevels:
  64. add_fields(
  65. "users",
  66. "users_default",
  67. "events",
  68. "events_default",
  69. "state_default",
  70. "ban",
  71. "kick",
  72. "redact",
  73. )
  74. elif event_type == EventTypes.Aliases:
  75. add_fields("aliases")
  76. elif event_type == EventTypes.RoomHistoryVisibility:
  77. add_fields("history_visibility")
  78. allowed_fields = {
  79. k: v
  80. for k, v in event_dict.items()
  81. if k in allowed_keys
  82. }
  83. allowed_fields["content"] = new_content
  84. allowed_fields["unsigned"] = {}
  85. if "age_ts" in event.unsigned:
  86. allowed_fields["unsigned"]["age_ts"] = event.unsigned["age_ts"]
  87. if "replaces_state" in event.unsigned:
  88. allowed_fields["unsigned"]["replaces_state"] = event.unsigned["replaces_state"]
  89. return type(event)(
  90. allowed_fields,
  91. internal_metadata_dict=event.internal_metadata.get_dict()
  92. )
  93. def _copy_field(src, dst, field):
  94. """Copy the field in 'src' to 'dst'.
  95. For example, if src={"foo":{"bar":5}} and dst={}, and field=["foo","bar"]
  96. then dst={"foo":{"bar":5}}.
  97. Args:
  98. src(dict): The dict to read from.
  99. dst(dict): The dict to modify.
  100. field(list<str>): List of keys to drill down to in 'src'.
  101. """
  102. if len(field) == 0: # this should be impossible
  103. return
  104. if len(field) == 1: # common case e.g. 'origin_server_ts'
  105. if field[0] in src:
  106. dst[field[0]] = src[field[0]]
  107. return
  108. # Else is a nested field e.g. 'content.body'
  109. # Pop the last field as that's the key to move across and we need the
  110. # parent dict in order to access the data. Drill down to the right dict.
  111. key_to_move = field.pop(-1)
  112. sub_dict = src
  113. for sub_field in field: # e.g. sub_field => "content"
  114. if sub_field in sub_dict and type(sub_dict[sub_field]) in [dict, frozendict]:
  115. sub_dict = sub_dict[sub_field]
  116. else:
  117. return
  118. if key_to_move not in sub_dict:
  119. return
  120. # Insert the key into the output dictionary, creating nested objects
  121. # as required. We couldn't do this any earlier or else we'd need to delete
  122. # the empty objects if the key didn't exist.
  123. sub_out_dict = dst
  124. for sub_field in field:
  125. sub_out_dict = sub_out_dict.setdefault(sub_field, {})
  126. sub_out_dict[key_to_move] = sub_dict[key_to_move]
  127. def only_fields(dictionary, fields):
  128. """Return a new dict with only the fields in 'dictionary' which are present
  129. in 'fields'.
  130. If there are no event fields specified then all fields are included.
  131. The entries may include '.' charaters to indicate sub-fields.
  132. So ['content.body'] will include the 'body' field of the 'content' object.
  133. A literal '.' character in a field name may be escaped using a '\'.
  134. Args:
  135. dictionary(dict): The dictionary to read from.
  136. fields(list<str>): A list of fields to copy over. Only shallow refs are
  137. taken.
  138. Returns:
  139. dict: A new dictionary with only the given fields. If fields was empty,
  140. the same dictionary is returned.
  141. """
  142. if len(fields) == 0:
  143. return dictionary
  144. # for each field, convert it:
  145. # ["content.body.thing\.with\.dots"] => [["content", "body", "thing\.with\.dots"]]
  146. split_fields = [SPLIT_FIELD_REGEX.split(f) for f in fields]
  147. # for each element of the output array of arrays:
  148. # remove escaping so we can use the right key names.
  149. split_fields[:] = [
  150. [f.replace(r'\.', r'.') for f in field_array] for field_array in split_fields
  151. ]
  152. output = {}
  153. for field_array in split_fields:
  154. _copy_field(dictionary, output, field_array)
  155. return output
  156. def format_event_raw(d):
  157. return d
  158. def format_event_for_client_v1(d):
  159. d = format_event_for_client_v2(d)
  160. sender = d.get("sender")
  161. if sender is not None:
  162. d["user_id"] = sender
  163. copy_keys = (
  164. "age", "redacted_because", "replaces_state", "prev_content",
  165. "invite_room_state",
  166. )
  167. for key in copy_keys:
  168. if key in d["unsigned"]:
  169. d[key] = d["unsigned"][key]
  170. return d
  171. def format_event_for_client_v2(d):
  172. drop_keys = (
  173. "auth_events", "prev_events", "hashes", "signatures", "depth",
  174. "origin", "prev_state",
  175. )
  176. for key in drop_keys:
  177. d.pop(key, None)
  178. return d
  179. def format_event_for_client_v2_without_room_id(d):
  180. d = format_event_for_client_v2(d)
  181. d.pop("room_id", None)
  182. return d
  183. def serialize_event(e, time_now_ms, as_client_event=True,
  184. event_format=format_event_for_client_v1,
  185. token_id=None, only_event_fields=None, is_invite=False):
  186. """Serialize event for clients
  187. Args:
  188. e (EventBase)
  189. time_now_ms (int)
  190. as_client_event (bool)
  191. event_format
  192. token_id
  193. only_event_fields
  194. is_invite (bool): Whether this is an invite that is being sent to the
  195. invitee
  196. Returns:
  197. dict
  198. """
  199. # FIXME(erikj): To handle the case of presence events and the like
  200. if not isinstance(e, EventBase):
  201. return e
  202. time_now_ms = int(time_now_ms)
  203. # Should this strip out None's?
  204. d = {k: v for k, v in e.get_dict().items()}
  205. if "age_ts" in d["unsigned"]:
  206. d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"]
  207. del d["unsigned"]["age_ts"]
  208. if "redacted_because" in e.unsigned:
  209. d["unsigned"]["redacted_because"] = serialize_event(
  210. e.unsigned["redacted_because"], time_now_ms,
  211. event_format=event_format
  212. )
  213. if token_id is not None:
  214. if token_id == getattr(e.internal_metadata, "token_id", None):
  215. txn_id = getattr(e.internal_metadata, "txn_id", None)
  216. if txn_id is not None:
  217. d["unsigned"]["transaction_id"] = txn_id
  218. # If this is an invite for somebody else, then we don't care about the
  219. # invite_room_state as that's meant solely for the invitee. Other clients
  220. # will already have the state since they're in the room.
  221. if not is_invite:
  222. d["unsigned"].pop("invite_room_state", None)
  223. if as_client_event:
  224. d = event_format(d)
  225. if only_event_fields:
  226. if (not isinstance(only_event_fields, list) or
  227. not all(isinstance(f, string_types) for f in only_event_fields)):
  228. raise TypeError("only_event_fields must be a list of strings")
  229. d = only_fields(d, only_event_fields)
  230. return d