test_receipts.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # Copyright 2016 OpenMarket 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. from typing import Collection, Optional
  15. from synapse.api.constants import ReceiptTypes
  16. from synapse.types import UserID, create_requester
  17. from tests.test_utils.event_injection import create_event
  18. from tests.unittest import HomeserverTestCase
  19. OTHER_USER_ID = "@other:test"
  20. OUR_USER_ID = "@our:test"
  21. class ReceiptTestCase(HomeserverTestCase):
  22. def prepare(self, reactor, clock, homeserver) -> None:
  23. super().prepare(reactor, clock, homeserver)
  24. self.store = homeserver.get_datastores().main
  25. self.room_creator = homeserver.get_room_creation_handler()
  26. self.persist_event_storage_controller = (
  27. self.hs.get_storage_controllers().persistence
  28. )
  29. # Create a test user
  30. self.ourUser = UserID.from_string(OUR_USER_ID)
  31. self.ourRequester = create_requester(self.ourUser)
  32. # Create a second test user
  33. self.otherUser = UserID.from_string(OTHER_USER_ID)
  34. self.otherRequester = create_requester(self.otherUser)
  35. # Create a test room
  36. info, _ = self.get_success(self.room_creator.create_room(self.ourRequester, {}))
  37. self.room_id1 = info["room_id"]
  38. # Create a second test room
  39. info, _ = self.get_success(self.room_creator.create_room(self.ourRequester, {}))
  40. self.room_id2 = info["room_id"]
  41. # Join the second user to the first room
  42. memberEvent, memberEventContext = self.get_success(
  43. create_event(
  44. self.hs,
  45. room_id=self.room_id1,
  46. type="m.room.member",
  47. sender=self.otherRequester.user.to_string(),
  48. state_key=self.otherRequester.user.to_string(),
  49. content={"membership": "join"},
  50. )
  51. )
  52. self.get_success(
  53. self.persist_event_storage_controller.persist_event(
  54. memberEvent, memberEventContext
  55. )
  56. )
  57. # Join the second user to the second room
  58. memberEvent, memberEventContext = self.get_success(
  59. create_event(
  60. self.hs,
  61. room_id=self.room_id2,
  62. type="m.room.member",
  63. sender=self.otherRequester.user.to_string(),
  64. state_key=self.otherRequester.user.to_string(),
  65. content={"membership": "join"},
  66. )
  67. )
  68. self.get_success(
  69. self.persist_event_storage_controller.persist_event(
  70. memberEvent, memberEventContext
  71. )
  72. )
  73. def get_last_unthreaded_receipt(
  74. self, receipt_types: Collection[str], room_id: Optional[str] = None
  75. ) -> Optional[str]:
  76. """
  77. Fetch the event ID for the latest unthreaded receipt in the test room for the test user.
  78. Args:
  79. receipt_types: The receipt types to fetch.
  80. Returns:
  81. The latest receipt, if one exists.
  82. """
  83. result = self.get_success(
  84. self.store.db_pool.runInteraction(
  85. "get_last_receipt_event_id_for_user",
  86. self.store.get_last_unthreaded_receipt_for_user_txn,
  87. OUR_USER_ID,
  88. room_id or self.room_id1,
  89. receipt_types,
  90. )
  91. )
  92. if not result:
  93. return None
  94. event_id, _ = result
  95. return event_id
  96. def test_return_empty_with_no_data(self) -> None:
  97. res = self.get_success(
  98. self.store.get_receipts_for_user(
  99. OUR_USER_ID,
  100. [
  101. ReceiptTypes.READ,
  102. ReceiptTypes.READ_PRIVATE,
  103. ],
  104. )
  105. )
  106. self.assertEqual(res, {})
  107. res = self.get_success(
  108. self.store.get_receipts_for_user_with_orderings(
  109. OUR_USER_ID,
  110. [
  111. ReceiptTypes.READ,
  112. ReceiptTypes.READ_PRIVATE,
  113. ],
  114. )
  115. )
  116. self.assertEqual(res, {})
  117. res = self.get_last_unthreaded_receipt(
  118. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  119. )
  120. self.assertEqual(res, None)
  121. def test_get_receipts_for_user(self) -> None:
  122. # Send some events into the first room
  123. event1_1_id = self.create_and_send_event(
  124. self.room_id1, UserID.from_string(OTHER_USER_ID)
  125. )
  126. event1_2_id = self.create_and_send_event(
  127. self.room_id1, UserID.from_string(OTHER_USER_ID)
  128. )
  129. # Send public read receipt for the first event
  130. self.get_success(
  131. self.store.insert_receipt(
  132. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], None, {}
  133. )
  134. )
  135. # Send private read receipt for the second event
  136. self.get_success(
  137. self.store.insert_receipt(
  138. self.room_id1,
  139. ReceiptTypes.READ_PRIVATE,
  140. OUR_USER_ID,
  141. [event1_2_id],
  142. None,
  143. {},
  144. )
  145. )
  146. # Test we get the latest event when we want both private and public receipts
  147. res = self.get_success(
  148. self.store.get_receipts_for_user(
  149. OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  150. )
  151. )
  152. self.assertEqual(res, {self.room_id1: event1_2_id})
  153. # Test we get the older event when we want only public receipt
  154. res = self.get_success(
  155. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  156. )
  157. self.assertEqual(res, {self.room_id1: event1_1_id})
  158. # Test we get the latest event when we want only the public receipt
  159. res = self.get_success(
  160. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ_PRIVATE])
  161. )
  162. self.assertEqual(res, {self.room_id1: event1_2_id})
  163. # Test receipt updating
  164. self.get_success(
  165. self.store.insert_receipt(
  166. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], None, {}
  167. )
  168. )
  169. res = self.get_success(
  170. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  171. )
  172. self.assertEqual(res, {self.room_id1: event1_2_id})
  173. # Send some events into the second room
  174. event2_1_id = self.create_and_send_event(
  175. self.room_id2, UserID.from_string(OTHER_USER_ID)
  176. )
  177. # Test new room is reflected in what the method returns
  178. self.get_success(
  179. self.store.insert_receipt(
  180. self.room_id2,
  181. ReceiptTypes.READ_PRIVATE,
  182. OUR_USER_ID,
  183. [event2_1_id],
  184. None,
  185. {},
  186. )
  187. )
  188. res = self.get_success(
  189. self.store.get_receipts_for_user(
  190. OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  191. )
  192. )
  193. self.assertEqual(res, {self.room_id1: event1_2_id, self.room_id2: event2_1_id})
  194. def test_get_last_receipt_event_id_for_user(self) -> None:
  195. # Send some events into the first room
  196. event1_1_id = self.create_and_send_event(
  197. self.room_id1, UserID.from_string(OTHER_USER_ID)
  198. )
  199. event1_2_id = self.create_and_send_event(
  200. self.room_id1, UserID.from_string(OTHER_USER_ID)
  201. )
  202. # Send public read receipt for the first event
  203. self.get_success(
  204. self.store.insert_receipt(
  205. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], None, {}
  206. )
  207. )
  208. # Send private read receipt for the second event
  209. self.get_success(
  210. self.store.insert_receipt(
  211. self.room_id1,
  212. ReceiptTypes.READ_PRIVATE,
  213. OUR_USER_ID,
  214. [event1_2_id],
  215. None,
  216. {},
  217. )
  218. )
  219. # Test we get the latest event when we want both private and public receipts
  220. res = self.get_last_unthreaded_receipt(
  221. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  222. )
  223. self.assertEqual(res, event1_2_id)
  224. # Test we get the older event when we want only public receipt
  225. res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
  226. self.assertEqual(res, event1_1_id)
  227. # Test we get the latest event when we want only the private receipt
  228. res = self.get_last_unthreaded_receipt([ReceiptTypes.READ_PRIVATE])
  229. self.assertEqual(res, event1_2_id)
  230. # Test receipt updating
  231. self.get_success(
  232. self.store.insert_receipt(
  233. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], None, {}
  234. )
  235. )
  236. res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
  237. self.assertEqual(res, event1_2_id)
  238. # Send some events into the second room
  239. event2_1_id = self.create_and_send_event(
  240. self.room_id2, UserID.from_string(OTHER_USER_ID)
  241. )
  242. # Test new room is reflected in what the method returns
  243. self.get_success(
  244. self.store.insert_receipt(
  245. self.room_id2,
  246. ReceiptTypes.READ_PRIVATE,
  247. OUR_USER_ID,
  248. [event2_1_id],
  249. None,
  250. {},
  251. )
  252. )
  253. res = self.get_last_unthreaded_receipt(
  254. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE], room_id=self.room_id2
  255. )
  256. self.assertEqual(res, event2_1_id)