relations.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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. import logging
  15. from typing import TYPE_CHECKING, Optional
  16. from synapse.api.errors import SynapseError
  17. from synapse.types import JsonDict, Requester, StreamToken
  18. if TYPE_CHECKING:
  19. from synapse.server import HomeServer
  20. logger = logging.getLogger(__name__)
  21. class RelationsHandler:
  22. def __init__(self, hs: "HomeServer"):
  23. self._main_store = hs.get_datastores().main
  24. self._auth = hs.get_auth()
  25. self._clock = hs.get_clock()
  26. self._event_handler = hs.get_event_handler()
  27. self._event_serializer = hs.get_event_client_serializer()
  28. async def get_relations(
  29. self,
  30. requester: Requester,
  31. event_id: str,
  32. room_id: str,
  33. relation_type: Optional[str] = None,
  34. event_type: Optional[str] = None,
  35. aggregation_key: Optional[str] = None,
  36. limit: int = 5,
  37. direction: str = "b",
  38. from_token: Optional[StreamToken] = None,
  39. to_token: Optional[StreamToken] = None,
  40. ) -> JsonDict:
  41. """Get related events of a event, ordered by topological ordering.
  42. TODO Accept a PaginationConfig instead of individual pagination parameters.
  43. Args:
  44. requester: The user requesting the relations.
  45. event_id: Fetch events that relate to this event ID.
  46. room_id: The room the event belongs to.
  47. relation_type: Only fetch events with this relation type, if given.
  48. event_type: Only fetch events with this event type, if given.
  49. aggregation_key: Only fetch events with this aggregation key, if given.
  50. limit: Only fetch the most recent `limit` events.
  51. direction: Whether to fetch the most recent first (`"b"`) or the
  52. oldest first (`"f"`).
  53. from_token: Fetch rows from the given token, or from the start if None.
  54. to_token: Fetch rows up to the given token, or up to the end if None.
  55. Returns:
  56. The pagination chunk.
  57. """
  58. user_id = requester.user.to_string()
  59. await self._auth.check_user_in_room_or_world_readable(
  60. room_id, user_id, allow_departed_users=True
  61. )
  62. # This gets the original event and checks that a) the event exists and
  63. # b) the user is allowed to view it.
  64. event = await self._event_handler.get_event(requester.user, room_id, event_id)
  65. if event is None:
  66. raise SynapseError(404, "Unknown parent event.")
  67. pagination_chunk = await self._main_store.get_relations_for_event(
  68. event_id=event_id,
  69. event=event,
  70. room_id=room_id,
  71. relation_type=relation_type,
  72. event_type=event_type,
  73. aggregation_key=aggregation_key,
  74. limit=limit,
  75. direction=direction,
  76. from_token=from_token,
  77. to_token=to_token,
  78. )
  79. events = await self._main_store.get_events_as_list(
  80. [c["event_id"] for c in pagination_chunk.chunk]
  81. )
  82. now = self._clock.time_msec()
  83. # Do not bundle aggregations when retrieving the original event because
  84. # we want the content before relations are applied to it.
  85. original_event = self._event_serializer.serialize_event(
  86. event, now, bundle_aggregations=None
  87. )
  88. # The relations returned for the requested event do include their
  89. # bundled aggregations.
  90. aggregations = await self._main_store.get_bundled_aggregations(
  91. events, requester.user.to_string()
  92. )
  93. serialized_events = self._event_serializer.serialize_events(
  94. events, now, bundle_aggregations=aggregations
  95. )
  96. return_value = await pagination_chunk.to_dict(self._main_store)
  97. return_value["chunk"] = serialized_events
  98. return_value["original_event"] = original_event
  99. return return_value