receipts.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. from ._base import BaseHandler
  16. from twisted.internet import defer
  17. from synapse.util.logcontext import PreserveLoggingContext
  18. import logging
  19. logger = logging.getLogger(__name__)
  20. class ReceiptsHandler(BaseHandler):
  21. def __init__(self, hs):
  22. super(ReceiptsHandler, self).__init__(hs)
  23. self.hs = hs
  24. self.federation = hs.get_replication_layer()
  25. self.federation.register_edu_handler(
  26. "m.receipt", self._received_remote_receipt
  27. )
  28. self.clock = self.hs.get_clock()
  29. @defer.inlineCallbacks
  30. def received_client_receipt(self, room_id, receipt_type, user_id,
  31. event_id):
  32. """Called when a client tells us a local user has read up to the given
  33. event_id in the room.
  34. """
  35. receipt = {
  36. "room_id": room_id,
  37. "receipt_type": receipt_type,
  38. "user_id": user_id,
  39. "event_ids": [event_id],
  40. "data": {
  41. "ts": int(self.clock.time_msec()),
  42. }
  43. }
  44. is_new = yield self._handle_new_receipts([receipt])
  45. if is_new:
  46. self._push_remotes([receipt])
  47. @defer.inlineCallbacks
  48. def _received_remote_receipt(self, origin, content):
  49. """Called when we receive an EDU of type m.receipt from a remote HS.
  50. """
  51. receipts = [
  52. {
  53. "room_id": room_id,
  54. "receipt_type": receipt_type,
  55. "user_id": user_id,
  56. "event_ids": user_values["event_ids"],
  57. "data": user_values.get("data", {}),
  58. }
  59. for room_id, room_values in content.items()
  60. for receipt_type, users in room_values.items()
  61. for user_id, user_values in users.items()
  62. ]
  63. yield self._handle_new_receipts(receipts)
  64. @defer.inlineCallbacks
  65. def _handle_new_receipts(self, receipts):
  66. """Takes a list of receipts, stores them and informs the notifier.
  67. """
  68. min_batch_id = None
  69. max_batch_id = None
  70. for receipt in receipts:
  71. room_id = receipt["room_id"]
  72. receipt_type = receipt["receipt_type"]
  73. user_id = receipt["user_id"]
  74. event_ids = receipt["event_ids"]
  75. data = receipt["data"]
  76. res = yield self.store.insert_receipt(
  77. room_id, receipt_type, user_id, event_ids, data
  78. )
  79. if not res:
  80. # res will be None if this read receipt is 'old'
  81. defer.returnValue(False)
  82. stream_id, max_persisted_id = res
  83. if min_batch_id is None or stream_id < min_batch_id:
  84. min_batch_id = stream_id
  85. if max_batch_id is None or max_persisted_id > max_batch_id:
  86. max_batch_id = max_persisted_id
  87. affected_room_ids = list(set([r["room_id"] for r in receipts]))
  88. with PreserveLoggingContext():
  89. self.notifier.on_new_event(
  90. "receipt_key", max_batch_id, rooms=affected_room_ids
  91. )
  92. # Note that the min here shouldn't be relied upon to be accurate.
  93. self.hs.get_pusherpool().on_new_receipts(
  94. min_batch_id, max_batch_id, affected_room_ids
  95. )
  96. defer.returnValue(True)
  97. @defer.inlineCallbacks
  98. def _push_remotes(self, receipts):
  99. """Given a list of receipts, works out which remote servers should be
  100. poked and pokes them.
  101. """
  102. # TODO: Some of this stuff should be coallesced.
  103. for receipt in receipts:
  104. room_id = receipt["room_id"]
  105. receipt_type = receipt["receipt_type"]
  106. user_id = receipt["user_id"]
  107. event_ids = receipt["event_ids"]
  108. data = receipt["data"]
  109. remotedomains = set()
  110. rm_handler = self.hs.get_handlers().room_member_handler
  111. yield rm_handler.fetch_room_distributions_into(
  112. room_id, localusers=None, remotedomains=remotedomains
  113. )
  114. logger.debug("Sending receipt to: %r", remotedomains)
  115. for domain in remotedomains:
  116. self.federation.send_edu(
  117. destination=domain,
  118. edu_type="m.receipt",
  119. content={
  120. room_id: {
  121. receipt_type: {
  122. user_id: {
  123. "event_ids": event_ids,
  124. "data": data,
  125. }
  126. }
  127. },
  128. },
  129. )
  130. @defer.inlineCallbacks
  131. def get_receipts_for_room(self, room_id, to_key):
  132. """Gets all receipts for a room, upto the given key.
  133. """
  134. result = yield self.store.get_linearized_receipts_for_room(
  135. room_id,
  136. to_key=to_key,
  137. )
  138. if not result:
  139. defer.returnValue([])
  140. defer.returnValue(result)
  141. class ReceiptEventSource(object):
  142. def __init__(self, hs):
  143. self.store = hs.get_datastore()
  144. @defer.inlineCallbacks
  145. def get_new_events(self, from_key, room_ids, **kwargs):
  146. from_key = int(from_key)
  147. to_key = yield self.get_current_key()
  148. if from_key == to_key:
  149. defer.returnValue(([], to_key))
  150. events = yield self.store.get_linearized_receipts_for_rooms(
  151. room_ids,
  152. from_key=from_key,
  153. to_key=to_key,
  154. )
  155. defer.returnValue((events, to_key))
  156. def get_current_key(self, direction='f'):
  157. return self.store.get_max_receipt_stream_id()
  158. @defer.inlineCallbacks
  159. def get_pagination_rows(self, user, config, key):
  160. to_key = int(config.from_key)
  161. if config.to_key:
  162. from_key = int(config.to_key)
  163. else:
  164. from_key = None
  165. rooms = yield self.store.get_rooms_for_user(user.to_string())
  166. rooms = [room.room_id for room in rooms]
  167. events = yield self.store.get_linearized_receipts_for_rooms(
  168. rooms,
  169. from_key=from_key,
  170. to_key=to_key,
  171. )
  172. defer.returnValue((events, to_key))