25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

123 satır
4.6 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, Sequence, Tuple
  15. import attr
  16. from synapse.handlers.account_data import AccountDataEventSource
  17. from synapse.handlers.presence import PresenceEventSource
  18. from synapse.handlers.receipts import ReceiptEventSource
  19. from synapse.handlers.room import RoomEventSource
  20. from synapse.handlers.typing import TypingNotificationEventSource
  21. from synapse.logging.opentracing import trace
  22. from synapse.streams import EventSource
  23. from synapse.types import MultiWriterStreamToken, StreamKeyType, StreamToken
  24. if TYPE_CHECKING:
  25. from synapse.server import HomeServer
  26. @attr.s(frozen=True, slots=True, auto_attribs=True)
  27. class _EventSourcesInner:
  28. room: RoomEventSource
  29. presence: PresenceEventSource
  30. typing: TypingNotificationEventSource
  31. receipt: ReceiptEventSource
  32. account_data: AccountDataEventSource
  33. def get_sources(self) -> Sequence[Tuple[StreamKeyType, EventSource]]:
  34. return [
  35. (StreamKeyType.ROOM, self.room),
  36. (StreamKeyType.PRESENCE, self.presence),
  37. (StreamKeyType.TYPING, self.typing),
  38. (StreamKeyType.RECEIPT, self.receipt),
  39. (StreamKeyType.ACCOUNT_DATA, self.account_data),
  40. ]
  41. class EventSources:
  42. def __init__(self, hs: "HomeServer"):
  43. self.sources = _EventSourcesInner(
  44. # mypy previously warned that attribute.type is `Optional`, but we know it's
  45. # never `None` here since all the attributes of `_EventSourcesInner` are
  46. # annotated.
  47. # As of the stubs in attrs 22.1.0, `attr.fields()` now returns Any,
  48. # so the call to `attribute.type` is not checked.
  49. *(attribute.type(hs) for attribute in attr.fields(_EventSourcesInner))
  50. )
  51. self.store = hs.get_datastores().main
  52. self._instance_name = hs.get_instance_name()
  53. def get_current_token(self) -> StreamToken:
  54. push_rules_key = self.store.get_max_push_rules_stream_id()
  55. to_device_key = self.store.get_to_device_stream_token()
  56. device_list_key = self.store.get_device_stream_token()
  57. un_partial_stated_rooms_key = self.store.get_un_partial_stated_rooms_token(
  58. self._instance_name
  59. )
  60. token = StreamToken(
  61. room_key=self.sources.room.get_current_key(),
  62. presence_key=self.sources.presence.get_current_key(),
  63. typing_key=self.sources.typing.get_current_key(),
  64. receipt_key=self.sources.receipt.get_current_key(),
  65. account_data_key=self.sources.account_data.get_current_key(),
  66. push_rules_key=push_rules_key,
  67. to_device_key=to_device_key,
  68. device_list_key=device_list_key,
  69. # Groups key is unused.
  70. groups_key=0,
  71. un_partial_stated_rooms_key=un_partial_stated_rooms_key,
  72. )
  73. return token
  74. @trace
  75. async def get_start_token_for_pagination(self, room_id: str) -> StreamToken:
  76. """Get the start token for a given room to be used to paginate
  77. events.
  78. The returned token does not have the current values for fields other
  79. than `room`, since they are not used during pagination.
  80. Returns:
  81. The start token for pagination.
  82. """
  83. return StreamToken.START
  84. @trace
  85. async def get_current_token_for_pagination(self, room_id: str) -> StreamToken:
  86. """Get the current token for a given room to be used to paginate
  87. events.
  88. The returned token does not have the current values for fields other
  89. than `room`, since they are not used during pagination.
  90. Returns:
  91. The current token for pagination.
  92. """
  93. token = StreamToken(
  94. room_key=await self.sources.room.get_current_key_for_room(room_id),
  95. presence_key=0,
  96. typing_key=0,
  97. receipt_key=MultiWriterStreamToken(stream=0),
  98. account_data_key=0,
  99. push_rules_key=0,
  100. to_device_key=0,
  101. device_list_key=0,
  102. groups_key=0,
  103. un_partial_stated_rooms_key=0,
  104. )
  105. return token