summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorPatrick Cloke <patrickc@matrix.org>2022-06-16 09:49:35 -0400
committerPatrick Cloke <patrickc@matrix.org>2022-08-05 08:18:08 -0400
commitdfd921d42128ff6a5bc795b17ccbae1eafa187de (patch)
tree32c4b60d664ce7f3c73f1a344adf854d0dced266 /tests
parentExtract the thread ID when processing push rules. (diff)
downloadsynapse-dfd921d42128ff6a5bc795b17ccbae1eafa187de.tar.xz
Return thread notification counts down sync.
Diffstat (limited to 'tests')
-rw-r--r--tests/replication/slave/storage/test_events.py6
-rw-r--r--tests/storage/test_event_push_actions.py175
2 files changed, 177 insertions, 4 deletions
diff --git a/tests/replication/slave/storage/test_events.py b/tests/replication/slave/storage/test_events.py
index f16554cd5c..1ac3260984 100644
--- a/tests/replication/slave/storage/test_events.py
+++ b/tests/replication/slave/storage/test_events.py
@@ -178,7 +178,7 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
         self.check(
             "get_unread_event_push_actions_by_room_for_user",
             [ROOM_ID, USER_ID_2],
-            NotifCounts(highlight_count=0, unread_count=0, notify_count=0),
+            (NotifCounts(highlight_count=0, unread_count=0, notify_count=0), {}),
         )
 
         self.persist(
@@ -191,7 +191,7 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
         self.check(
             "get_unread_event_push_actions_by_room_for_user",
             [ROOM_ID, USER_ID_2],
-            NotifCounts(highlight_count=0, unread_count=0, notify_count=1),
+            (NotifCounts(highlight_count=0, unread_count=0, notify_count=1), {}),
         )
 
         self.persist(
@@ -206,7 +206,7 @@ class SlavedEventStoreTestCase(BaseSlavedStoreTestCase):
         self.check(
             "get_unread_event_push_actions_by_room_for_user",
             [ROOM_ID, USER_ID_2],
-            NotifCounts(highlight_count=1, unread_count=0, notify_count=2),
+            (NotifCounts(highlight_count=1, unread_count=0, notify_count=2), {}),
         )
 
     def test_get_rooms_for_user_with_stream_ordering(self):
diff --git a/tests/storage/test_event_push_actions.py b/tests/storage/test_event_push_actions.py
index ba40124c8a..d1c9035fa8 100644
--- a/tests/storage/test_event_push_actions.py
+++ b/tests/storage/test_event_push_actions.py
@@ -12,12 +12,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from typing import Optional
+
 from twisted.test.proto_helpers import MemoryReactor
 
 from synapse.rest import admin
 from synapse.rest.client import login, room
 from synapse.server import HomeServer
 from synapse.storage.databases.main.event_push_actions import NotifCounts
+from synapse.types import JsonDict
 from synapse.util import Clock
 
 from tests.unittest import HomeserverTestCase
@@ -70,7 +73,7 @@ class EventPushActionsStoreTestCase(HomeserverTestCase):
         def _assert_counts(
             noitf_count: int, unread_count: int, highlight_count: int
         ) -> None:
-            counts = self.get_success(
+            counts, thread_counts = self.get_success(
                 self.store.db_pool.runInteraction(
                     "get-unread-counts",
                     self.store._get_unread_counts_by_receipt_txn,
@@ -86,6 +89,7 @@ class EventPushActionsStoreTestCase(HomeserverTestCase):
                     highlight_count=highlight_count,
                 ),
             )
+            self.assertEqual(thread_counts, {})
 
         def _create_event(highlight: bool = False) -> str:
             result = self.helper.send_event(
@@ -131,6 +135,7 @@ class EventPushActionsStoreTestCase(HomeserverTestCase):
         _assert_counts(0, 0, 0)
 
         _create_event()
+        _assert_counts(1, 1, 0)
         _rotate()
         _assert_counts(1, 1, 0)
 
@@ -166,6 +171,174 @@ class EventPushActionsStoreTestCase(HomeserverTestCase):
         _rotate()
         _assert_counts(0, 0, 0)
 
+    def test_count_aggregation_threads(self) -> None:
+        # Create a user to receive notifications and send receipts.
+        user_id = self.register_user("user1235", "pass")
+        token = self.login("user1235", "pass")
+
+        # And another users to send events.
+        other_id = self.register_user("other", "pass")
+        other_token = self.login("other", "pass")
+
+        # Create a room and put both users in it.
+        room_id = self.helper.create_room_as(user_id, tok=token)
+        self.helper.join(room_id, other_id, tok=other_token)
+        thread_id: str
+
+        last_event_id: str
+
+        def _assert_counts(
+            noitf_count: int,
+            unread_count: int,
+            highlight_count: int,
+            thread_notif_count: int,
+            thread_unread_count: int,
+            thread_highlight_count: int,
+        ) -> None:
+            counts, thread_counts = self.get_success(
+                self.store.db_pool.runInteraction(
+                    "get-unread-counts",
+                    self.store._get_unread_counts_by_receipt_txn,
+                    room_id,
+                    user_id,
+                )
+            )
+            self.assertEqual(
+                counts,
+                NotifCounts(
+                    notify_count=noitf_count,
+                    unread_count=unread_count,
+                    highlight_count=highlight_count,
+                ),
+            )
+            if thread_notif_count or thread_unread_count or thread_highlight_count:
+                self.assertEqual(
+                    thread_counts,
+                    {
+                        thread_id: NotifCounts(
+                            notify_count=thread_notif_count,
+                            unread_count=thread_unread_count,
+                            highlight_count=thread_highlight_count,
+                        ),
+                    },
+                )
+            else:
+                self.assertEqual(thread_counts, {})
+
+        def _create_event(
+            highlight: bool = False, thread_id: Optional[str] = None
+        ) -> str:
+            content: JsonDict = {
+                "msgtype": "m.text",
+                "body": user_id if highlight else "",
+            }
+            if thread_id:
+                content["m.relates_to"] = {
+                    "rel_type": "m.thread",
+                    "event_id": thread_id,
+                }
+
+            result = self.helper.send_event(
+                room_id,
+                type="m.room.message",
+                content=content,
+                tok=other_token,
+            )
+            nonlocal last_event_id
+            last_event_id = result["event_id"]
+            return last_event_id
+
+        def _rotate() -> None:
+            self.get_success(self.store._rotate_notifs())
+
+        def _mark_read(event_id: str) -> None:
+            self.get_success(
+                self.store.insert_receipt(
+                    room_id,
+                    "m.read",
+                    user_id=user_id,
+                    event_ids=[event_id],
+                    data={},
+                )
+            )
+
+        _assert_counts(0, 0, 0, 0, 0, 0)
+        thread_id = _create_event()
+        _assert_counts(1, 0, 0, 0, 0, 0)
+        _rotate()
+        _assert_counts(1, 0, 0, 0, 0, 0)
+
+        _create_event(thread_id=thread_id)
+        _assert_counts(1, 0, 0, 1, 0, 0)
+        _rotate()
+        _assert_counts(1, 0, 0, 1, 0, 0)
+
+        _create_event()
+        _assert_counts(2, 0, 0, 1, 0, 0)
+        _rotate()
+        _assert_counts(2, 0, 0, 1, 0, 0)
+
+        event_id = _create_event(thread_id=thread_id)
+        _assert_counts(2, 0, 0, 2, 0, 0)
+        _rotate()
+        _assert_counts(2, 0, 0, 2, 0, 0)
+
+        _create_event()
+        _create_event(thread_id=thread_id)
+        _mark_read(event_id)
+        _assert_counts(1, 0, 0, 1, 0, 0)
+
+        _mark_read(last_event_id)
+        _assert_counts(0, 0, 0, 0, 0, 0)
+
+        _create_event()
+        _create_event(thread_id=thread_id)
+        _assert_counts(1, 0, 0, 1, 0, 0)
+        _rotate()
+        _assert_counts(1, 0, 0, 1, 0, 0)
+
+        # Delete old event push actions, this should not affect the (summarised) count.
+        self.get_success(self.store._remove_old_push_actions_that_have_rotated())
+        _assert_counts(1, 0, 0, 1, 0, 0)
+
+        _mark_read(last_event_id)
+        _assert_counts(0, 0, 0, 0, 0, 0)
+
+        _create_event(True)
+        _assert_counts(1, 1, 1, 0, 0, 0)
+        _rotate()
+        _assert_counts(1, 1, 1, 0, 0, 0)
+
+        event_id = _create_event(True, thread_id)
+        _assert_counts(1, 1, 1, 1, 1, 1)
+        _rotate()
+        _assert_counts(1, 1, 1, 1, 1, 1)
+
+        # Check that adding another notification and rotating after highlight
+        # works.
+        _create_event()
+        _rotate()
+        _assert_counts(2, 0, 1, 1, 1, 1)
+
+        _create_event(thread_id=thread_id)
+        _rotate()
+        _assert_counts(2, 0, 1, 2, 0, 1)
+
+        # Check that sending read receipts at different points results in the
+        # right counts.
+        _mark_read(event_id)
+        _assert_counts(1, 0, 0, 1, 0, 0)
+        _mark_read(last_event_id)
+        _assert_counts(0, 0, 0, 0, 0, 0)
+
+        _create_event(True)
+        _create_event(True, thread_id)
+        _assert_counts(1, 1, 1, 1, 1, 1)
+        _mark_read(last_event_id)
+        _assert_counts(0, 0, 0, 0, 0, 0)
+        _rotate()
+        _assert_counts(0, 0, 0, 0, 0, 0)
+
     def test_find_first_stream_ordering_after_ts(self) -> None:
         def add_event(so: int, ts: int) -> None:
             self.get_success(