1
0

test_receipts.py 10 KB

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