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.
 
 
 
 
 
 

348 lines
13 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. from typing import TYPE_CHECKING, Optional, Union
  15. import attr
  16. from frozendict import frozendict
  17. from synapse.appservice import ApplicationService
  18. from synapse.events import EventBase
  19. from synapse.logging.context import make_deferred_yieldable, run_in_background
  20. from synapse.types import StateMap
  21. if TYPE_CHECKING:
  22. from synapse.storage.databases.main import DataStore
  23. @attr.s(slots=True)
  24. class EventContext:
  25. """
  26. Holds information relevant to persisting an event
  27. Attributes:
  28. rejected: A rejection reason if the event was rejected, else False
  29. _state_group: The ID of the state group for this event. Note that state events
  30. are persisted with a state group which includes the new event, so this is
  31. effectively the state *after* the event in question.
  32. For a *rejected* state event, where the state of the rejected event is
  33. ignored, this state_group should never make it into the
  34. event_to_state_groups table. Indeed, inspecting this value for a rejected
  35. state event is almost certainly incorrect.
  36. For an outlier, where we don't have the state at the event, this will be
  37. None.
  38. Note that this is a private attribute: it should be accessed via
  39. the ``state_group`` property.
  40. state_group_before_event: The ID of the state group representing the state
  41. of the room before this event.
  42. If this is a non-state event, this will be the same as ``state_group``. If
  43. it's a state event, it will be the same as ``prev_group``.
  44. If ``state_group`` is None (ie, the event is an outlier),
  45. ``state_group_before_event`` will always also be ``None``.
  46. prev_group: If it is known, ``state_group``'s prev_group. Note that this being
  47. None does not necessarily mean that ``state_group`` does not have
  48. a prev_group!
  49. If the event is a state event, this is normally the same as ``prev_group``.
  50. If ``state_group`` is None (ie, the event is an outlier), ``prev_group``
  51. will always also be ``None``.
  52. Note that this *not* (necessarily) the state group associated with
  53. ``_prev_state_ids``.
  54. delta_ids: If ``prev_group`` is not None, the state delta between ``prev_group``
  55. and ``state_group``.
  56. app_service: If this event is being sent by a (local) application service, that
  57. app service.
  58. _current_state_ids: The room state map, including this event - ie, the state
  59. in ``state_group``.
  60. (type, state_key) -> event_id
  61. FIXME: what is this for an outlier? it seems ill-defined. It seems like
  62. it could be either {}, or the state we were given by the remote
  63. server, depending on $THINGS
  64. Note that this is a private attribute: it should be accessed via
  65. ``get_current_state_ids``. _AsyncEventContext impl calculates this
  66. on-demand: it will be None until that happens.
  67. _prev_state_ids: The room state map, excluding this event - ie, the state
  68. in ``state_group_before_event``. For a non-state
  69. event, this will be the same as _current_state_events.
  70. Note that it is a completely different thing to prev_group!
  71. (type, state_key) -> event_id
  72. FIXME: again, what is this for an outlier?
  73. As with _current_state_ids, this is a private attribute. It should be
  74. accessed via get_prev_state_ids.
  75. """
  76. rejected = attr.ib(default=False, type=Union[bool, str])
  77. _state_group = attr.ib(default=None, type=Optional[int])
  78. state_group_before_event = attr.ib(default=None, type=Optional[int])
  79. prev_group = attr.ib(default=None, type=Optional[int])
  80. delta_ids = attr.ib(default=None, type=Optional[StateMap[str]])
  81. app_service = attr.ib(default=None, type=Optional[ApplicationService])
  82. _current_state_ids = attr.ib(default=None, type=Optional[StateMap[str]])
  83. _prev_state_ids = attr.ib(default=None, type=Optional[StateMap[str]])
  84. @staticmethod
  85. def with_state(
  86. state_group,
  87. state_group_before_event,
  88. current_state_ids,
  89. prev_state_ids,
  90. prev_group=None,
  91. delta_ids=None,
  92. ):
  93. return EventContext(
  94. current_state_ids=current_state_ids,
  95. prev_state_ids=prev_state_ids,
  96. state_group=state_group,
  97. state_group_before_event=state_group_before_event,
  98. prev_group=prev_group,
  99. delta_ids=delta_ids,
  100. )
  101. async def serialize(self, event: EventBase, store: "DataStore") -> dict:
  102. """Converts self to a type that can be serialized as JSON, and then
  103. deserialized by `deserialize`
  104. Args:
  105. event (FrozenEvent): The event that this context relates to
  106. Returns:
  107. dict
  108. """
  109. # We don't serialize the full state dicts, instead they get pulled out
  110. # of the DB on the other side. However, the other side can't figure out
  111. # the prev_state_ids, so if we're a state event we include the event
  112. # id that we replaced in the state.
  113. if event.is_state():
  114. prev_state_ids = await self.get_prev_state_ids()
  115. prev_state_id = prev_state_ids.get((event.type, event.state_key))
  116. else:
  117. prev_state_id = None
  118. return {
  119. "prev_state_id": prev_state_id,
  120. "event_type": event.type,
  121. "event_state_key": event.state_key if event.is_state() else None,
  122. "state_group": self._state_group,
  123. "state_group_before_event": self.state_group_before_event,
  124. "rejected": self.rejected,
  125. "prev_group": self.prev_group,
  126. "delta_ids": _encode_state_dict(self.delta_ids),
  127. "app_service_id": self.app_service.id if self.app_service else None,
  128. }
  129. @staticmethod
  130. def deserialize(storage, input):
  131. """Converts a dict that was produced by `serialize` back into a
  132. EventContext.
  133. Args:
  134. storage (Storage): Used to convert AS ID to AS object and fetch
  135. state.
  136. input (dict): A dict produced by `serialize`
  137. Returns:
  138. EventContext
  139. """
  140. context = _AsyncEventContextImpl(
  141. # We use the state_group and prev_state_id stuff to pull the
  142. # current_state_ids out of the DB and construct prev_state_ids.
  143. storage=storage,
  144. prev_state_id=input["prev_state_id"],
  145. event_type=input["event_type"],
  146. event_state_key=input["event_state_key"],
  147. state_group=input["state_group"],
  148. state_group_before_event=input["state_group_before_event"],
  149. prev_group=input["prev_group"],
  150. delta_ids=_decode_state_dict(input["delta_ids"]),
  151. rejected=input["rejected"],
  152. )
  153. app_service_id = input["app_service_id"]
  154. if app_service_id:
  155. context.app_service = storage.main.get_app_service_by_id(app_service_id)
  156. return context
  157. @property
  158. def state_group(self) -> Optional[int]:
  159. """The ID of the state group for this event.
  160. Note that state events are persisted with a state group which includes the new
  161. event, so this is effectively the state *after* the event in question.
  162. For an outlier, where we don't have the state at the event, this will be None.
  163. It is an error to access this for a rejected event, since rejected state should
  164. not make it into the room state. Accessing this property will raise an exception
  165. if ``rejected`` is set.
  166. """
  167. if self.rejected:
  168. raise RuntimeError("Attempt to access state_group of rejected event")
  169. return self._state_group
  170. async def get_current_state_ids(self) -> Optional[StateMap[str]]:
  171. """
  172. Gets the room state map, including this event - ie, the state in ``state_group``
  173. It is an error to access this for a rejected event, since rejected state should
  174. not make it into the room state. This method will raise an exception if
  175. ``rejected`` is set.
  176. Returns:
  177. Returns None if state_group is None, which happens when the associated
  178. event is an outlier.
  179. Maps a (type, state_key) to the event ID of the state event matching
  180. this tuple.
  181. """
  182. if self.rejected:
  183. raise RuntimeError("Attempt to access state_ids of rejected event")
  184. await self._ensure_fetched()
  185. return self._current_state_ids
  186. async def get_prev_state_ids(self):
  187. """
  188. Gets the room state map, excluding this event.
  189. For a non-state event, this will be the same as get_current_state_ids().
  190. Returns:
  191. dict[(str, str), str]|None: Returns None if state_group
  192. is None, which happens when the associated event is an outlier.
  193. Maps a (type, state_key) to the event ID of the state event matching
  194. this tuple.
  195. """
  196. await self._ensure_fetched()
  197. return self._prev_state_ids
  198. def get_cached_current_state_ids(self):
  199. """Gets the current state IDs if we have them already cached.
  200. It is an error to access this for a rejected event, since rejected state should
  201. not make it into the room state. This method will raise an exception if
  202. ``rejected`` is set.
  203. Returns:
  204. dict[(str, str), str]|None: Returns None if we haven't cached the
  205. state or if state_group is None, which happens when the associated
  206. event is an outlier.
  207. """
  208. if self.rejected:
  209. raise RuntimeError("Attempt to access state_ids of rejected event")
  210. return self._current_state_ids
  211. async def _ensure_fetched(self):
  212. return None
  213. @attr.s(slots=True)
  214. class _AsyncEventContextImpl(EventContext):
  215. """
  216. An implementation of EventContext which fetches _current_state_ids and
  217. _prev_state_ids from the database on demand.
  218. Attributes:
  219. _storage (Storage)
  220. _fetching_state_deferred (Deferred|None): Resolves when *_state_ids have
  221. been calculated. None if we haven't started calculating yet
  222. _event_type (str): The type of the event the context is associated with.
  223. _event_state_key (str): The state_key of the event the context is
  224. associated with.
  225. _prev_state_id (str|None): If the event associated with the context is
  226. a state event, then `_prev_state_id` is the event_id of the state
  227. that was replaced.
  228. """
  229. # This needs to have a default as we're inheriting
  230. _storage = attr.ib(default=None)
  231. _prev_state_id = attr.ib(default=None)
  232. _event_type = attr.ib(default=None)
  233. _event_state_key = attr.ib(default=None)
  234. _fetching_state_deferred = attr.ib(default=None)
  235. async def _ensure_fetched(self):
  236. if not self._fetching_state_deferred:
  237. self._fetching_state_deferred = run_in_background(self._fill_out_state)
  238. return await make_deferred_yieldable(self._fetching_state_deferred)
  239. async def _fill_out_state(self):
  240. """Called to populate the _current_state_ids and _prev_state_ids
  241. attributes by loading from the database.
  242. """
  243. if self.state_group is None:
  244. return
  245. self._current_state_ids = await self._storage.state.get_state_ids_for_group(
  246. self.state_group
  247. )
  248. if self._event_state_key is not None:
  249. self._prev_state_ids = dict(self._current_state_ids)
  250. key = (self._event_type, self._event_state_key)
  251. if self._prev_state_id:
  252. self._prev_state_ids[key] = self._prev_state_id
  253. else:
  254. self._prev_state_ids.pop(key, None)
  255. else:
  256. self._prev_state_ids = self._current_state_ids
  257. def _encode_state_dict(state_dict):
  258. """Since dicts of (type, state_key) -> event_id cannot be serialized in
  259. JSON we need to convert them to a form that can.
  260. """
  261. if state_dict is None:
  262. return None
  263. return [(etype, state_key, v) for (etype, state_key), v in state_dict.items()]
  264. def _decode_state_dict(input):
  265. """Decodes a state dict encoded using `_encode_state_dict` above"""
  266. if input is None:
  267. return None
  268. return frozendict({(etype, state_key): v for etype, state_key, v in input})