utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014, 2015 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. from synapse.api.constants import EventTypes
  16. from . import EventBase
  17. def prune_event(event):
  18. """ Returns a pruned version of the given event, which removes all keys we
  19. don't know about or think could potentially be dodgy.
  20. This is used when we "redact" an event. We want to remove all fields that
  21. the user has specified, but we do want to keep necessary information like
  22. type, state_key etc.
  23. """
  24. event_type = event.type
  25. allowed_keys = [
  26. "event_id",
  27. "sender",
  28. "room_id",
  29. "hashes",
  30. "signatures",
  31. "content",
  32. "type",
  33. "state_key",
  34. "depth",
  35. "prev_events",
  36. "prev_state",
  37. "auth_events",
  38. "origin",
  39. "origin_server_ts",
  40. "membership",
  41. ]
  42. event_dict = event.get_dict()
  43. new_content = {}
  44. def add_fields(*fields):
  45. for field in fields:
  46. if field in event.content:
  47. new_content[field] = event_dict["content"][field]
  48. if event_type == EventTypes.Member:
  49. add_fields("membership")
  50. elif event_type == EventTypes.Create:
  51. add_fields("creator")
  52. elif event_type == EventTypes.JoinRules:
  53. add_fields("join_rule")
  54. elif event_type == EventTypes.PowerLevels:
  55. add_fields(
  56. "users",
  57. "users_default",
  58. "events",
  59. "events_default",
  60. "events_default",
  61. "state_default",
  62. "ban",
  63. "kick",
  64. "redact",
  65. )
  66. elif event_type == EventTypes.Aliases:
  67. add_fields("aliases")
  68. allowed_fields = {
  69. k: v
  70. for k, v in event_dict.items()
  71. if k in allowed_keys
  72. }
  73. allowed_fields["content"] = new_content
  74. allowed_fields["unsigned"] = {}
  75. if "age_ts" in event.unsigned:
  76. allowed_fields["unsigned"]["age_ts"] = event.unsigned["age_ts"]
  77. return type(event)(
  78. allowed_fields,
  79. internal_metadata_dict=event.internal_metadata.get_dict()
  80. )
  81. def format_event_raw(d):
  82. return d
  83. def format_event_for_client_v1(d):
  84. d["user_id"] = d.pop("sender", None)
  85. move_keys = ("age", "redacted_because", "replaces_state", "prev_content")
  86. for key in move_keys:
  87. if key in d["unsigned"]:
  88. d[key] = d["unsigned"][key]
  89. drop_keys = (
  90. "auth_events", "prev_events", "hashes", "signatures", "depth",
  91. "unsigned", "origin", "prev_state"
  92. )
  93. for key in drop_keys:
  94. d.pop(key, None)
  95. return d
  96. def format_event_for_client_v2(d):
  97. drop_keys = (
  98. "auth_events", "prev_events", "hashes", "signatures", "depth",
  99. "origin", "prev_state",
  100. )
  101. for key in drop_keys:
  102. d.pop(key, None)
  103. return d
  104. def format_event_for_client_v2_without_event_id(d):
  105. d = format_event_for_client_v2(d)
  106. d.pop("room_id", None)
  107. d.pop("event_id", None)
  108. return d
  109. def serialize_event(e, time_now_ms, as_client_event=True,
  110. event_format=format_event_for_client_v1,
  111. token_id=None):
  112. # FIXME(erikj): To handle the case of presence events and the like
  113. if not isinstance(e, EventBase):
  114. return e
  115. time_now_ms = int(time_now_ms)
  116. # Should this strip out None's?
  117. d = {k: v for k, v in e.get_dict().items()}
  118. if "age_ts" in d["unsigned"]:
  119. d["unsigned"]["age"] = time_now_ms - d["unsigned"]["age_ts"]
  120. del d["unsigned"]["age_ts"]
  121. if "redacted_because" in e.unsigned:
  122. d["unsigned"]["redacted_because"] = serialize_event(
  123. e.unsigned["redacted_because"], time_now_ms
  124. )
  125. if token_id is not None:
  126. if token_id == getattr(e.internal_metadata, "token_id", None):
  127. txn_id = getattr(e.internal_metadata, "txn_id", None)
  128. if txn_id is not None:
  129. d["unsigned"]["transaction_id"] = txn_id
  130. if as_client_event:
  131. return event_format(d)
  132. else:
  133. return d