Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit dcced5a

Browse files
authored
Use threaded receipts when fetching events for push. (#13878)
Update the HTTP and email pushers to consider threaded read receipts when fetching unread events.
1 parent c3b0e5e commit dcced5a

File tree

3 files changed

+97
-41
lines changed

3 files changed

+97
-41
lines changed

changelog.d/13878.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Experimental support for thread-specific receipts ([MSC3771](https://github.com/matrix-org/matrix-spec-proposals/pull/3771)).

synapse/storage/databases/main/event_push_actions.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@
119119
]
120120

121121

122+
@attr.s(slots=True, auto_attribs=True)
123+
class _RoomReceipt:
124+
"""
125+
HttpPushAction instances include the information used to generate HTTP
126+
requests to a push gateway.
127+
"""
128+
129+
unthreaded_stream_ordering: int = 0
130+
# threaded_stream_ordering includes the main pseudo-thread.
131+
threaded_stream_ordering: Dict[str, int] = attr.Factory(dict)
132+
133+
def is_unread(self, thread_id: str, stream_ordering: int) -> bool:
134+
"""Returns True if the stream ordering is unread according to the receipt information."""
135+
136+
# Only include push actions with a stream ordering after both the unthreaded
137+
# and threaded receipt. Properly handles a user without any receipts present.
138+
return (
139+
self.unthreaded_stream_ordering < stream_ordering
140+
and self.threaded_stream_ordering.get(thread_id, 0) < stream_ordering
141+
)
142+
143+
144+
# A _RoomReceipt with no receipts in it.
145+
MISSING_ROOM_RECEIPT = _RoomReceipt()
146+
147+
122148
@attr.s(slots=True, frozen=True, auto_attribs=True)
123149
class HttpPushAction:
124150
"""
@@ -716,7 +742,7 @@ def f(txn: LoggingTransaction) -> List[str]:
716742

717743
def _get_receipts_by_room_txn(
718744
self, txn: LoggingTransaction, user_id: str
719-
) -> Dict[str, int]:
745+
) -> Dict[str, _RoomReceipt]:
720746
"""
721747
Generate a map of room ID to the latest stream ordering that has been
722748
read by the given user.
@@ -726,7 +752,8 @@ def _get_receipts_by_room_txn(
726752
user_id: The user to fetch receipts for.
727753
728754
Returns:
729-
A map of room ID to stream ordering for all rooms the user has a receipt in.
755+
A map including all rooms the user is in with a receipt. It maps
756+
room IDs to _RoomReceipt instances
730757
"""
731758
receipt_types_clause, args = make_in_list_sql_clause(
732759
self.database_engine,
@@ -735,20 +762,26 @@ def _get_receipts_by_room_txn(
735762
)
736763

737764
sql = f"""
738-
SELECT room_id, MAX(stream_ordering)
765+
SELECT room_id, thread_id, MAX(stream_ordering)
739766
FROM receipts_linearized
740767
INNER JOIN events USING (room_id, event_id)
741768
WHERE {receipt_types_clause}
742769
AND user_id = ?
743-
GROUP BY room_id
770+
GROUP BY room_id, thread_id
744771
"""
745772

746773
args.extend((user_id,))
747774
txn.execute(sql, args)
748-
return {
749-
room_id: latest_stream_ordering
750-
for room_id, latest_stream_ordering in txn.fetchall()
751-
}
775+
776+
result: Dict[str, _RoomReceipt] = {}
777+
for room_id, thread_id, stream_ordering in txn:
778+
room_receipt = result.setdefault(room_id, _RoomReceipt())
779+
if thread_id is None:
780+
room_receipt.unthreaded_stream_ordering = stream_ordering
781+
else:
782+
room_receipt.threaded_stream_ordering[thread_id] = stream_ordering
783+
784+
return result
752785

753786
async def get_unread_push_actions_for_user_in_range_for_http(
754787
self,
@@ -781,9 +814,10 @@ async def get_unread_push_actions_for_user_in_range_for_http(
781814

782815
def get_push_actions_txn(
783816
txn: LoggingTransaction,
784-
) -> List[Tuple[str, str, int, str, bool]]:
817+
) -> List[Tuple[str, str, str, int, str, bool]]:
785818
sql = """
786-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions, ep.highlight
819+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
820+
ep.actions, ep.highlight
787821
FROM event_push_actions AS ep
788822
WHERE
789823
ep.user_id = ?
@@ -793,7 +827,7 @@ def get_push_actions_txn(
793827
ORDER BY ep.stream_ordering ASC LIMIT ?
794828
"""
795829
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
796-
return cast(List[Tuple[str, str, int, str, bool]], txn.fetchall())
830+
return cast(List[Tuple[str, str, str, int, str, bool]], txn.fetchall())
797831

798832
push_actions = await self.db_pool.runInteraction(
799833
"get_unread_push_actions_for_user_in_range_http", get_push_actions_txn
@@ -806,10 +840,10 @@ def get_push_actions_txn(
806840
stream_ordering=stream_ordering,
807841
actions=_deserialize_action(actions, highlight),
808842
)
809-
for event_id, room_id, stream_ordering, actions, highlight in push_actions
810-
# Only include push actions with a stream ordering after any receipt, or without any
811-
# receipt present (invited to but never read rooms).
812-
if stream_ordering > receipts_by_room.get(room_id, 0)
843+
for event_id, room_id, thread_id, stream_ordering, actions, highlight in push_actions
844+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
845+
thread_id, stream_ordering
846+
)
813847
]
814848

815849
# Now sort it so it's ordered correctly, since currently it will
@@ -853,10 +887,10 @@ async def get_unread_push_actions_for_user_in_range_for_email(
853887

854888
def get_push_actions_txn(
855889
txn: LoggingTransaction,
856-
) -> List[Tuple[str, str, int, str, bool, int]]:
890+
) -> List[Tuple[str, str, str, int, str, bool, int]]:
857891
sql = """
858-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions,
859-
ep.highlight, e.received_ts
892+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
893+
ep.actions, ep.highlight, e.received_ts
860894
FROM event_push_actions AS ep
861895
INNER JOIN events AS e USING (room_id, event_id)
862896
WHERE
@@ -867,7 +901,7 @@ def get_push_actions_txn(
867901
ORDER BY ep.stream_ordering DESC LIMIT ?
868902
"""
869903
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
870-
return cast(List[Tuple[str, str, int, str, bool, int]], txn.fetchall())
904+
return cast(List[Tuple[str, str, str, int, str, bool, int]], txn.fetchall())
871905

872906
push_actions = await self.db_pool.runInteraction(
873907
"get_unread_push_actions_for_user_in_range_email", get_push_actions_txn
@@ -882,10 +916,10 @@ def get_push_actions_txn(
882916
actions=_deserialize_action(actions, highlight),
883917
received_ts=received_ts,
884918
)
885-
for event_id, room_id, stream_ordering, actions, highlight, received_ts in push_actions
886-
# Only include push actions with a stream ordering after any receipt, or without any
887-
# receipt present (invited to but never read rooms).
888-
if stream_ordering > receipts_by_room.get(room_id, 0)
919+
for event_id, room_id, thread_id, stream_ordering, actions, highlight, received_ts in push_actions
920+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
921+
thread_id, stream_ordering
922+
)
889923
]
890924

891925
# Now sort it so it's ordered correctly, since currently it will

tests/storage/test_event_push_actions.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from twisted.test.proto_helpers import MemoryReactor
1818

19-
from synapse.api.constants import MAIN_TIMELINE
19+
from synapse.api.constants import MAIN_TIMELINE, RelationTypes
2020
from synapse.rest import admin
2121
from synapse.rest.client import login, room
2222
from synapse.server import HomeServer
@@ -66,16 +66,23 @@ def test_get_unread_push_actions_for_user_in_range(self) -> None:
6666
user_id, token, _, other_token, room_id = self._create_users_and_room()
6767

6868
# Create two events, one of which is a highlight.
69-
self.helper.send_event(
69+
first_event_id = self.helper.send_event(
7070
room_id,
7171
type="m.room.message",
7272
content={"msgtype": "m.text", "body": "msg"},
7373
tok=other_token,
74-
)
75-
event_id = self.helper.send_event(
74+
)["event_id"]
75+
second_event_id = self.helper.send_event(
7676
room_id,
7777
type="m.room.message",
78-
content={"msgtype": "m.text", "body": user_id},
78+
content={
79+
"msgtype": "m.text",
80+
"body": user_id,
81+
"m.relates_to": {
82+
"rel_type": RelationTypes.THREAD,
83+
"event_id": first_event_id,
84+
},
85+
},
7986
tok=other_token,
8087
)["event_id"]
8188

@@ -95,13 +102,13 @@ def test_get_unread_push_actions_for_user_in_range(self) -> None:
95102
)
96103
self.assertEqual(2, len(email_actions))
97104

98-
# Send a receipt, which should clear any actions.
105+
# Send a receipt, which should clear the first action.
99106
self.get_success(
100107
self.store.insert_receipt(
101108
room_id,
102109
"m.read",
103110
user_id=user_id,
104-
event_ids=[event_id],
111+
event_ids=[first_event_id],
105112
thread_id=None,
106113
data={},
107114
)
@@ -111,6 +118,30 @@ def test_get_unread_push_actions_for_user_in_range(self) -> None:
111118
user_id, 0, 1000, 20
112119
)
113120
)
121+
self.assertEqual(1, len(http_actions))
122+
email_actions = self.get_success(
123+
self.store.get_unread_push_actions_for_user_in_range_for_email(
124+
user_id, 0, 1000, 20
125+
)
126+
)
127+
self.assertEqual(1, len(email_actions))
128+
129+
# Send a thread receipt to clear the thread action.
130+
self.get_success(
131+
self.store.insert_receipt(
132+
room_id,
133+
"m.read",
134+
user_id=user_id,
135+
event_ids=[second_event_id],
136+
thread_id=first_event_id,
137+
data={},
138+
)
139+
)
140+
http_actions = self.get_success(
141+
self.store.get_unread_push_actions_for_user_in_range_for_http(
142+
user_id, 0, 1000, 20
143+
)
144+
)
114145
self.assertEqual([], http_actions)
115146
email_actions = self.get_success(
116147
self.store.get_unread_push_actions_for_user_in_range_for_email(
@@ -417,17 +448,7 @@ def test_count_aggregation_mixed(self) -> None:
417448
sends both unthreaded and threaded receipts.
418449
"""
419450

420-
# Create a user to receive notifications and send receipts.
421-
user_id = self.register_user("user1235", "pass")
422-
token = self.login("user1235", "pass")
423-
424-
# And another users to send events.
425-
other_id = self.register_user("other", "pass")
426-
other_token = self.login("other", "pass")
427-
428-
# Create a room and put both users in it.
429-
room_id = self.helper.create_room_as(user_id, tok=token)
430-
self.helper.join(room_id, other_id, tok=other_token)
451+
user_id, token, _, other_token, room_id = self._create_users_and_room()
431452
thread_id: str
432453

433454
last_event_id: str

0 commit comments

Comments
 (0)