receipts.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket 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. import logging
  16. from twisted.internet import defer
  17. from synapse.handlers._base import BaseHandler
  18. from synapse.types import ReadReceipt
  19. logger = logging.getLogger(__name__)
  20. class ReceiptsHandler(BaseHandler):
  21. def __init__(self, hs):
  22. super(ReceiptsHandler, self).__init__(hs)
  23. self.server_name = hs.config.server_name
  24. self.store = hs.get_datastore()
  25. self.hs = hs
  26. self.federation = hs.get_federation_sender()
  27. hs.get_federation_registry().register_edu_handler(
  28. "m.receipt", self._received_remote_receipt
  29. )
  30. self.clock = self.hs.get_clock()
  31. self.state = hs.get_state_handler()
  32. @defer.inlineCallbacks
  33. def _received_remote_receipt(self, origin, content):
  34. """Called when we receive an EDU of type m.receipt from a remote HS.
  35. """
  36. receipts = [
  37. ReadReceipt(
  38. room_id=room_id,
  39. receipt_type=receipt_type,
  40. user_id=user_id,
  41. event_ids=user_values["event_ids"],
  42. data=user_values.get("data", {}),
  43. )
  44. for room_id, room_values in content.items()
  45. for receipt_type, users in room_values.items()
  46. for user_id, user_values in users.items()
  47. ]
  48. yield self._handle_new_receipts(receipts)
  49. @defer.inlineCallbacks
  50. def _handle_new_receipts(self, receipts):
  51. """Takes a list of receipts, stores them and informs the notifier.
  52. """
  53. min_batch_id = None
  54. max_batch_id = None
  55. for receipt in receipts:
  56. res = yield self.store.insert_receipt(
  57. receipt.room_id,
  58. receipt.receipt_type,
  59. receipt.user_id,
  60. receipt.event_ids,
  61. receipt.data,
  62. )
  63. if not res:
  64. # res will be None if this read receipt is 'old'
  65. continue
  66. stream_id, max_persisted_id = res
  67. if min_batch_id is None or stream_id < min_batch_id:
  68. min_batch_id = stream_id
  69. if max_batch_id is None or max_persisted_id > max_batch_id:
  70. max_batch_id = max_persisted_id
  71. if min_batch_id is None:
  72. # no new receipts
  73. return False
  74. affected_room_ids = list(set([r.room_id for r in receipts]))
  75. self.notifier.on_new_event("receipt_key", max_batch_id, rooms=affected_room_ids)
  76. # Note that the min here shouldn't be relied upon to be accurate.
  77. yield self.hs.get_pusherpool().on_new_receipts(
  78. min_batch_id, max_batch_id, affected_room_ids
  79. )
  80. return True
  81. @defer.inlineCallbacks
  82. def received_client_receipt(self, room_id, receipt_type, user_id, event_id):
  83. """Called when a client tells us a local user has read up to the given
  84. event_id in the room.
  85. """
  86. receipt = ReadReceipt(
  87. room_id=room_id,
  88. receipt_type=receipt_type,
  89. user_id=user_id,
  90. event_ids=[event_id],
  91. data={"ts": int(self.clock.time_msec())},
  92. )
  93. is_new = yield self._handle_new_receipts([receipt])
  94. if not is_new:
  95. return
  96. yield self.federation.send_read_receipt(receipt)
  97. @defer.inlineCallbacks
  98. def get_receipts_for_room(self, room_id, to_key):
  99. """Gets all receipts for a room, upto the given key.
  100. """
  101. result = yield self.store.get_linearized_receipts_for_room(
  102. room_id, to_key=to_key
  103. )
  104. if not result:
  105. return []
  106. return result
  107. class ReceiptEventSource(object):
  108. def __init__(self, hs):
  109. self.store = hs.get_datastore()
  110. @defer.inlineCallbacks
  111. def get_new_events(self, from_key, room_ids, **kwargs):
  112. from_key = int(from_key)
  113. to_key = yield self.get_current_key()
  114. if from_key == to_key:
  115. return ([], to_key)
  116. events = yield self.store.get_linearized_receipts_for_rooms(
  117. room_ids, from_key=from_key, to_key=to_key
  118. )
  119. return (events, to_key)
  120. def get_current_key(self, direction="f"):
  121. return self.store.get_max_receipt_stream_id()
  122. @defer.inlineCallbacks
  123. def get_pagination_rows(self, user, config, key):
  124. to_key = int(config.from_key)
  125. if config.to_key:
  126. from_key = int(config.to_key)
  127. else:
  128. from_key = None
  129. room_ids = yield self.store.get_rooms_for_user(user.to_string())
  130. events = yield self.store.get_linearized_receipts_for_rooms(
  131. room_ids, from_key=from_key, to_key=to_key
  132. )
  133. return (events, to_key)