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.
 
 
 
 
 
 

350 lines
13 KiB

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