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

Commit 4f687b2

Browse files
committed
Handle threads when fetching events for push.
1 parent 041fe7f commit 4f687b2

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
"""
@@ -709,7 +735,7 @@ def f(txn: LoggingTransaction) -> List[str]:
709735

710736
def _get_receipts_by_room_txn(
711737
self, txn: LoggingTransaction, user_id: str
712-
) -> Dict[str, int]:
738+
) -> Dict[str, _RoomReceipt]:
713739
"""
714740
Generate a map of room ID to the latest stream ordering that has been
715741
read by the given user.
@@ -719,7 +745,8 @@ def _get_receipts_by_room_txn(
719745
user_id: The user to fetch receipts for.
720746
721747
Returns:
722-
A map of room ID to stream ordering for all rooms the user has a receipt in.
748+
A map including all rooms the user is in with a receipt. It maps
749+
room IDs to _RoomReceipt instances
723750
"""
724751
receipt_types_clause, args = make_in_list_sql_clause(
725752
self.database_engine,
@@ -728,20 +755,26 @@ def _get_receipts_by_room_txn(
728755
)
729756

730757
sql = f"""
731-
SELECT room_id, MAX(stream_ordering)
758+
SELECT room_id, thread_id, MAX(stream_ordering)
732759
FROM receipts_linearized
733760
INNER JOIN events USING (room_id, event_id)
734761
WHERE {receipt_types_clause}
735762
AND user_id = ?
736-
GROUP BY room_id
763+
GROUP BY room_id, thread_id
737764
"""
738765

739766
args.extend((user_id,))
740767
txn.execute(sql, args)
741-
return {
742-
room_id: latest_stream_ordering
743-
for room_id, latest_stream_ordering in txn.fetchall()
744-
}
768+
769+
result: Dict[str, _RoomReceipt] = {}
770+
for room_id, thread_id, stream_ordering in txn:
771+
room_receipt = result.setdefault(room_id, _RoomReceipt())
772+
if thread_id is None:
773+
room_receipt.unthreaded_stream_ordering = stream_ordering
774+
else:
775+
room_receipt.threaded_stream_ordering[thread_id] = stream_ordering
776+
777+
return result
745778

746779
async def get_unread_push_actions_for_user_in_range_for_http(
747780
self,
@@ -774,9 +807,10 @@ async def get_unread_push_actions_for_user_in_range_for_http(
774807

775808
def get_push_actions_txn(
776809
txn: LoggingTransaction,
777-
) -> List[Tuple[str, str, int, str, bool]]:
810+
) -> List[Tuple[str, str, str, int, str, bool]]:
778811
sql = """
779-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions, ep.highlight
812+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
813+
ep.actions, ep.highlight
780814
FROM event_push_actions AS ep
781815
WHERE
782816
ep.user_id = ?
@@ -786,7 +820,7 @@ def get_push_actions_txn(
786820
ORDER BY ep.stream_ordering ASC LIMIT ?
787821
"""
788822
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
789-
return cast(List[Tuple[str, str, int, str, bool]], txn.fetchall())
823+
return cast(List[Tuple[str, str, str, int, str, bool]], txn.fetchall())
790824

791825
push_actions = await self.db_pool.runInteraction(
792826
"get_unread_push_actions_for_user_in_range_http", get_push_actions_txn
@@ -799,10 +833,10 @@ def get_push_actions_txn(
799833
stream_ordering=stream_ordering,
800834
actions=_deserialize_action(actions, highlight),
801835
)
802-
for event_id, room_id, stream_ordering, actions, highlight in push_actions
803-
# Only include push actions with a stream ordering after any receipt, or without any
804-
# receipt present (invited to but never read rooms).
805-
if stream_ordering > receipts_by_room.get(room_id, 0)
836+
for event_id, room_id, thread_id, stream_ordering, actions, highlight in push_actions
837+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
838+
thread_id, stream_ordering
839+
)
806840
]
807841

808842
# Now sort it so it's ordered correctly, since currently it will
@@ -846,10 +880,10 @@ async def get_unread_push_actions_for_user_in_range_for_email(
846880

847881
def get_push_actions_txn(
848882
txn: LoggingTransaction,
849-
) -> List[Tuple[str, str, int, str, bool, int]]:
883+
) -> List[Tuple[str, str, str, int, str, bool, int]]:
850884
sql = """
851-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions,
852-
ep.highlight, e.received_ts
885+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
886+
ep.actions, ep.highlight, e.received_ts
853887
FROM event_push_actions AS ep
854888
INNER JOIN events AS e USING (room_id, event_id)
855889
WHERE
@@ -860,7 +894,7 @@ def get_push_actions_txn(
860894
ORDER BY ep.stream_ordering DESC LIMIT ?
861895
"""
862896
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
863-
return cast(List[Tuple[str, str, int, str, bool, int]], txn.fetchall())
897+
return cast(List[Tuple[str, str, str, int, str, bool, int]], txn.fetchall())
864898

865899
push_actions = await self.db_pool.runInteraction(
866900
"get_unread_push_actions_for_user_in_range_email", get_push_actions_txn
@@ -875,10 +909,10 @@ def get_push_actions_txn(
875909
actions=_deserialize_action(actions, highlight),
876910
received_ts=received_ts,
877911
)
878-
for event_id, room_id, stream_ordering, actions, highlight, received_ts in push_actions
879-
# Only include push actions with a stream ordering after any receipt, or without any
880-
# receipt present (invited to but never read rooms).
881-
if stream_ordering > receipts_by_room.get(room_id, 0)
912+
for event_id, room_id, thread_id, stream_ordering, actions, highlight, received_ts in push_actions
913+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
914+
thread_id, stream_ordering
915+
)
882916
]
883917

884918
# 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)