Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

71 rinda
2.7 KiB

  1. # Copyright 2017 Vector Creations 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. import logging
  15. from typing import TYPE_CHECKING
  16. from synapse.api.constants import ReceiptTypes
  17. from synapse.api.errors import SynapseError
  18. from synapse.util.async_helpers import Linearizer
  19. if TYPE_CHECKING:
  20. from synapse.server import HomeServer
  21. logger = logging.getLogger(__name__)
  22. class ReadMarkerHandler:
  23. def __init__(self, hs: "HomeServer"):
  24. self.store = hs.get_datastores().main
  25. self.account_data_handler = hs.get_account_data_handler()
  26. self.read_marker_linearizer = Linearizer(name="read_marker")
  27. async def received_client_read_marker(
  28. self, room_id: str, user_id: str, event_id: str
  29. ) -> None:
  30. """Updates the read marker for a given user in a given room if the event ID given
  31. is ahead in the stream relative to the current read marker.
  32. This uses a notifier to indicate that account data should be sent down /sync if
  33. the read marker has changed.
  34. """
  35. async with self.read_marker_linearizer.queue((room_id, user_id)):
  36. existing_read_marker = await self.store.get_account_data_for_room_and_type(
  37. user_id, room_id, ReceiptTypes.FULLY_READ
  38. )
  39. should_update = True
  40. # Get event ordering, this also ensures we know about the event
  41. event_ordering = await self.store.get_event_ordering(event_id)
  42. if existing_read_marker:
  43. try:
  44. old_event_ordering = await self.store.get_event_ordering(
  45. existing_read_marker["event_id"]
  46. )
  47. except SynapseError:
  48. # Old event no longer exists, assume new is ahead. This may
  49. # happen if the old event was removed due to retention.
  50. pass
  51. else:
  52. # Only update if the new marker is ahead in the stream
  53. should_update = event_ordering > old_event_ordering
  54. if should_update:
  55. content = {"event_id": event_id}
  56. await self.account_data_handler.add_account_data_to_room(
  57. user_id, room_id, ReceiptTypes.FULLY_READ, content
  58. )