read_marker.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.util.async_helpers import Linearizer
  17. if TYPE_CHECKING:
  18. from synapse.server import HomeServer
  19. logger = logging.getLogger(__name__)
  20. class ReadMarkerHandler:
  21. def __init__(self, hs: "HomeServer"):
  22. self.server_name = hs.config.server.server_name
  23. self.store = hs.get_datastores().main
  24. self.account_data_handler = hs.get_account_data_handler()
  25. self.read_marker_linearizer = Linearizer(name="read_marker")
  26. async def received_client_read_marker(
  27. self, room_id: str, user_id: str, event_id: str
  28. ) -> None:
  29. """Updates the read marker for a given user in a given room if the event ID given
  30. is ahead in the stream relative to the current read marker.
  31. This uses a notifier to indicate that account data should be sent down /sync if
  32. the read marker has changed.
  33. """
  34. async with self.read_marker_linearizer.queue((room_id, user_id)):
  35. existing_read_marker = await self.store.get_account_data_for_room_and_type(
  36. user_id, room_id, "m.fully_read"
  37. )
  38. should_update = True
  39. if existing_read_marker:
  40. # Only update if the new marker is ahead in the stream
  41. should_update = await self.store.is_event_after(
  42. event_id, existing_read_marker["event_id"]
  43. )
  44. if should_update:
  45. content = {"event_id": event_id}
  46. await self.account_data_handler.add_account_data_to_room(
  47. user_id, room_id, "m.fully_read", content
  48. )