read_marker.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Vector Creations 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 ._base import BaseHandler
  16. from twisted.internet import defer
  17. from synapse.util.async import Linearizer
  18. import logging
  19. logger = logging.getLogger(__name__)
  20. class ReadMarkerHandler(BaseHandler):
  21. def __init__(self, hs):
  22. super(ReadMarkerHandler, self).__init__(hs)
  23. self.server_name = hs.config.server_name
  24. self.store = hs.get_datastore()
  25. self.read_marker_linearizer = Linearizer(name="read_marker")
  26. self.notifier = hs.get_notifier()
  27. @defer.inlineCallbacks
  28. def received_client_read_marker(self, room_id, user_id, event_id):
  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. with (yield self.read_marker_linearizer.queue((room_id, user_id))):
  35. existing_read_marker = yield 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 = yield self.store.is_event_after(
  42. event_id,
  43. existing_read_marker['event_id']
  44. )
  45. if should_update:
  46. content = {
  47. "event_id": event_id
  48. }
  49. max_id = yield self.store.add_account_data_to_room(
  50. user_id, room_id, "m.fully_read", content
  51. )
  52. self.notifier.on_new_event("account_data_key", max_id, users=[user_id])