1
0

test_receipts.py 10 KB

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