Repository: matrix-org/synapse

Issue:
Unread count keeps showing up for some rooms
### Description

A private room I’m in keeps showing a (2) unread message count on client launch, both on Element Web and Android, even after I cleared cache.

### Steps to reproduce

- got some messages in a specific private room
- read them on Element Web (the count disappears)
- left home, started Element Android, the count appeared again for that room but no new message was visible; opening the room here makes the count go away again
- went back home, started Element Web again, the same count was back again.
- read the room again, did a clear cache & reload, the count is back again.

This all happened within the past 24 hours. During that time, I also have had counts appear on direct conversations with no new events while the Web client was running. That is not as reproducible though.

### Homeserver

matrix.org

### Synapse Version

1.61.0

### Installation Method

_No response_

### Platform

:shrug: matrix.org

### Relevant log output

```shell
I don’t have access to that.
```


### Anything else that would be useful to know?

_No response_

Discussion:

Patch:
diff --git a/changelog.d/13223.bugfix b/changelog.d/13223.bugfix
new file mode 100644
index 000000000..6ee3aed91
--- /dev/null
+++ b/changelog.d/13223.bugfix
@@ -0,0 +1,1 @@
+Fix bug where notification counts would get stuck after a highlighted message. Broke in v1.62.0.
diff --git a/synapse/storage/databases/main/event_push_actions.py b/synapse/storage/databases/main/event_push_actions.py
index a3edcbb39..1a951ac02 100644
--- a/synapse/storage/databases/main/event_push_actions.py
+++ b/synapse/storage/databases/main/event_push_actions.py
@@ -1016,9 +1016,14 @@ class EventPushActionsWorkerStore(ReceiptsWorkerStore, StreamWorkerStore, SQLBas
                 upd.stream_ordering
             FROM (
                 SELECT user_id, room_id, count(*) as cnt,
-                    max(stream_ordering) as stream_ordering
-                FROM event_push_actions
-                WHERE ? < stream_ordering AND stream_ordering <= ?
+                    max(ea.stream_ordering) as stream_ordering
+                FROM event_push_actions AS ea
+                LEFT JOIN event_push_summary AS old USING (user_id, room_id)
+                WHERE ? < ea.stream_ordering AND ea.stream_ordering <= ?
+                    AND (
+                        old.last_receipt_stream_ordering IS NULL
+                        OR old.last_receipt_stream_ordering < ea.stream_ordering
+                    )
                     AND %s = 1
                 GROUP BY user_id, room_id
             ) AS upd


Test patch:
diff --git a/tests/storage/test_event_push_actions.py b/tests/storage/test_event_push_actions.py
index e68126777..e8c53f16d 100644
--- a/tests/storage/test_event_push_actions.py
+++ b/tests/storage/test_event_push_actions.py
@@ -196,6 +196,13 @@ class EventPushActionsStoreTestCase(HomeserverTestCase):
         _mark_read(10, 10)
         _assert_counts(0, 0)
 
+        _inject_actions(11, HIGHLIGHT)
+        _assert_counts(1, 1)
+        _mark_read(11, 11)
+        _assert_counts(0, 0)
+        _rotate(11)
+        _assert_counts(0, 0)
+
     def test_find_first_stream_ordering_after_ts(self) -> None:
         def add_event(so: int, ts: int) -> None:
             self.get_success(