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

Commit 2a03087

Browse files
committed
Handle threads when fetching events for push.
1 parent 2b3357c commit 2a03087

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
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: 53 additions & 16 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
"""
@@ -589,7 +615,7 @@ def f(txn: LoggingTransaction) -> List[str]:
589615

590616
def _get_receipts_by_room_txn(
591617
self, txn: LoggingTransaction, user_id: str
592-
) -> Dict[str, int]:
618+
) -> Dict[str, _RoomReceipt]:
593619
"""
594620
Generate a map of room ID to the latest stream ordering that has been
595621
read by the given user.
@@ -599,7 +625,8 @@ def _get_receipts_by_room_txn(
599625
user_id: The user to fetch receipts for.
600626
601627
Returns:
602-
A map of room ID to stream ordering for all rooms the user has a receipt in.
628+
A map including all rooms the user is in with a receipt. It maps
629+
room IDs to _RoomReceipt instances
603630
"""
604631
receipt_types_clause, args = make_in_list_sql_clause(
605632
self.database_engine,
@@ -611,17 +638,26 @@ def _get_receipts_by_room_txn(
611638
)
612639

613640
sql = f"""
614-
SELECT room_id, MAX(stream_ordering)
641+
SELECT room_id, thread_id, MAX(stream_ordering)
615642
FROM receipts_linearized
616643
INNER JOIN events USING (room_id, event_id)
617644
WHERE {receipt_types_clause}
618645
AND user_id = ?
619-
GROUP BY room_id
646+
GROUP BY room_id, thread_id
620647
"""
621648

622649
args.extend((user_id,))
623650
txn.execute(sql, args)
624-
return dict(cast(List[Tuple[str, int]], txn.fetchall()))
651+
652+
result = {}
653+
for room_id, thread_id, stream_ordering in txn:
654+
room_receipt = result.setdefault(room_id, _RoomReceipt())
655+
if thread_id is None:
656+
room_receipt.unthreaded_stream_ordering = stream_ordering
657+
else:
658+
room_receipt.threaded_stream_ordering[thread_id] = stream_ordering
659+
660+
return result
625661

626662
async def get_unread_push_actions_for_user_in_range_for_http(
627663
self,
@@ -656,7 +692,8 @@ def get_push_actions_txn(
656692
txn: LoggingTransaction,
657693
) -> List[Tuple[str, str, int, str, bool]]:
658694
sql = """
659-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions, ep.highlight
695+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
696+
ep.actions, ep.highlight
660697
FROM event_push_actions AS ep
661698
WHERE
662699
ep.user_id = ?
@@ -679,10 +716,10 @@ def get_push_actions_txn(
679716
stream_ordering=stream_ordering,
680717
actions=_deserialize_action(actions, highlight),
681718
)
682-
for event_id, room_id, stream_ordering, actions, highlight in push_actions
683-
# Only include push actions with a stream ordering after any receipt, or without any
684-
# receipt present (invited to but never read rooms).
685-
if stream_ordering > receipts_by_room.get(room_id, 0)
719+
for event_id, room_id, thread_id, stream_ordering, actions, highlight in push_actions
720+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
721+
thread_id, stream_ordering
722+
)
686723
]
687724

688725
# Now sort it so it's ordered correctly, since currently it will
@@ -728,8 +765,8 @@ def get_push_actions_txn(
728765
txn: LoggingTransaction,
729766
) -> List[Tuple[str, str, int, str, bool, int]]:
730767
sql = """
731-
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions,
732-
ep.highlight, e.received_ts
768+
SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
769+
ep.actions, ep.highlight, e.received_ts
733770
FROM event_push_actions AS ep
734771
INNER JOIN events AS e USING (room_id, event_id)
735772
WHERE
@@ -755,10 +792,10 @@ def get_push_actions_txn(
755792
actions=_deserialize_action(actions, highlight),
756793
received_ts=received_ts,
757794
)
758-
for event_id, room_id, stream_ordering, actions, highlight, received_ts in push_actions
759-
# Only include push actions with a stream ordering after any receipt, or without any
760-
# receipt present (invited to but never read rooms).
761-
if stream_ordering > receipts_by_room.get(room_id, 0)
795+
for event_id, room_id, thread_id, stream_ordering, actions, highlight, received_ts in push_actions
796+
if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
797+
thread_id, stream_ordering
798+
)
762799
]
763800

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

0 commit comments

Comments
 (0)