test_event_push_actions.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # Copyright 2016-2021 The Matrix.org Foundation C.I.C.
  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 unittest.mock import Mock
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.server import HomeServer
  17. from synapse.storage.databases.main.event_push_actions import NotifCounts
  18. from synapse.util import Clock
  19. from tests.unittest import HomeserverTestCase
  20. USER_ID = "@user:example.com"
  21. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  22. HIGHLIGHT = [
  23. "notify",
  24. {"set_tweak": "sound", "value": "default"},
  25. {"set_tweak": "highlight"},
  26. ]
  27. class EventPushActionsStoreTestCase(HomeserverTestCase):
  28. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  29. self.store = hs.get_datastores().main
  30. persist_events_store = hs.get_datastores().persist_events
  31. assert persist_events_store is not None
  32. self.persist_events_store = persist_events_store
  33. def test_get_unread_push_actions_for_user_in_range_for_http(self) -> None:
  34. self.get_success(
  35. self.store.get_unread_push_actions_for_user_in_range_for_http(
  36. USER_ID, 0, 1000, 20
  37. )
  38. )
  39. def test_get_unread_push_actions_for_user_in_range_for_email(self) -> None:
  40. self.get_success(
  41. self.store.get_unread_push_actions_for_user_in_range_for_email(
  42. USER_ID, 0, 1000, 20
  43. )
  44. )
  45. def test_count_aggregation(self) -> None:
  46. room_id = "!foo:example.com"
  47. user_id = "@user1235:test"
  48. last_read_stream_ordering = [0]
  49. def _assert_counts(noitf_count: int, highlight_count: int) -> None:
  50. counts = self.get_success(
  51. self.store.db_pool.runInteraction(
  52. "",
  53. self.store._get_unread_counts_by_pos_txn,
  54. room_id,
  55. user_id,
  56. last_read_stream_ordering[0],
  57. )
  58. )
  59. self.assertEqual(
  60. counts,
  61. NotifCounts(
  62. notify_count=noitf_count,
  63. unread_count=0, # Unread counts are tested in the sync tests.
  64. highlight_count=highlight_count,
  65. ),
  66. )
  67. def _inject_actions(stream: int, action: list) -> None:
  68. event = Mock()
  69. event.room_id = room_id
  70. event.event_id = f"$test{stream}:example.com"
  71. event.internal_metadata.stream_ordering = stream
  72. event.internal_metadata.is_outlier.return_value = False
  73. event.depth = stream
  74. self.get_success(
  75. self.store.db_pool.simple_insert(
  76. table="events",
  77. values={
  78. "stream_ordering": stream,
  79. "topological_ordering": stream,
  80. "type": "m.room.message",
  81. "room_id": room_id,
  82. "processed": True,
  83. "outlier": False,
  84. "event_id": event.event_id,
  85. },
  86. )
  87. )
  88. self.get_success(
  89. self.store.add_push_actions_to_staging(
  90. event.event_id,
  91. {user_id: action},
  92. False,
  93. )
  94. )
  95. self.get_success(
  96. self.store.db_pool.runInteraction(
  97. "",
  98. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  99. [(event, None)],
  100. [(event, None)],
  101. )
  102. )
  103. def _rotate(stream: int) -> None:
  104. self.get_success(
  105. self.store.db_pool.runInteraction(
  106. "rotate-receipts", self.store._handle_new_receipts_for_notifs_txn
  107. )
  108. )
  109. self.get_success(
  110. self.store.db_pool.runInteraction(
  111. "rotate-notifs", self.store._rotate_notifs_before_txn, stream
  112. )
  113. )
  114. def _mark_read(stream: int, depth: int) -> None:
  115. last_read_stream_ordering[0] = stream
  116. self.get_success(
  117. self.store.db_pool.runInteraction(
  118. "",
  119. self.store._insert_linearized_receipt_txn,
  120. room_id,
  121. "m.read",
  122. user_id,
  123. f"$test{stream}:example.com",
  124. {},
  125. stream,
  126. )
  127. )
  128. _assert_counts(0, 0)
  129. _inject_actions(1, PlAIN_NOTIF)
  130. _assert_counts(1, 0)
  131. _rotate(2)
  132. _assert_counts(1, 0)
  133. _inject_actions(3, PlAIN_NOTIF)
  134. _assert_counts(2, 0)
  135. _rotate(4)
  136. _assert_counts(2, 0)
  137. _inject_actions(5, PlAIN_NOTIF)
  138. _mark_read(3, 3)
  139. _assert_counts(1, 0)
  140. _mark_read(5, 5)
  141. _assert_counts(0, 0)
  142. _inject_actions(6, PlAIN_NOTIF)
  143. _rotate(7)
  144. self.get_success(
  145. self.store.db_pool.simple_delete(
  146. table="event_push_actions", keyvalues={"1": 1}, desc=""
  147. )
  148. )
  149. _assert_counts(1, 0)
  150. _mark_read(6, 6)
  151. _assert_counts(0, 0)
  152. _inject_actions(8, HIGHLIGHT)
  153. _assert_counts(1, 1)
  154. _rotate(9)
  155. _assert_counts(1, 1)
  156. # Check that adding another notification and rotating after highlight
  157. # works.
  158. _inject_actions(10, PlAIN_NOTIF)
  159. _rotate(11)
  160. _assert_counts(2, 1)
  161. # Check that sending read receipts at different points results in the
  162. # right counts.
  163. _mark_read(8, 8)
  164. _assert_counts(1, 0)
  165. _mark_read(10, 10)
  166. _assert_counts(0, 0)
  167. def test_find_first_stream_ordering_after_ts(self) -> None:
  168. def add_event(so: int, ts: int) -> None:
  169. self.get_success(
  170. self.store.db_pool.simple_insert(
  171. "events",
  172. {
  173. "stream_ordering": so,
  174. "received_ts": ts,
  175. "event_id": "event%i" % so,
  176. "type": "",
  177. "room_id": "",
  178. "content": "",
  179. "processed": True,
  180. "outlier": False,
  181. "topological_ordering": 0,
  182. "depth": 0,
  183. },
  184. )
  185. )
  186. # start with the base case where there are no events in the table
  187. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  188. self.assertEqual(r, 0)
  189. # now with one event
  190. add_event(2, 10)
  191. r = self.get_success(self.store.find_first_stream_ordering_after_ts(9))
  192. self.assertEqual(r, 2)
  193. r = self.get_success(self.store.find_first_stream_ordering_after_ts(10))
  194. self.assertEqual(r, 2)
  195. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  196. self.assertEqual(r, 3)
  197. # add a bunch of dummy events to the events table
  198. for (stream_ordering, ts) in (
  199. (3, 110),
  200. (4, 120),
  201. (5, 120),
  202. (10, 130),
  203. (20, 140),
  204. ):
  205. add_event(stream_ordering, ts)
  206. r = self.get_success(self.store.find_first_stream_ordering_after_ts(110))
  207. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  208. # 4 and 5 are both after 120: we want 4 rather than 5
  209. r = self.get_success(self.store.find_first_stream_ordering_after_ts(120))
  210. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  211. r = self.get_success(self.store.find_first_stream_ordering_after_ts(129))
  212. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  213. # check we can get the last event
  214. r = self.get_success(self.store.find_first_stream_ordering_after_ts(140))
  215. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  216. # off the end
  217. r = self.get_success(self.store.find_first_stream_ordering_after_ts(160))
  218. self.assertEqual(r, 21)
  219. # check we can find an event at ordering zero
  220. add_event(0, 5)
  221. r = self.get_success(self.store.find_first_stream_ordering_after_ts(1))
  222. self.assertEqual(r, 0)