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

Commit 520843b

Browse files
committed
Get push actions in single query using new receipts by room
1 parent 763a65e commit 520843b

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

synapse/storage/databases/main/event_push_actions.py

+50-2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,26 @@ async def get_unread_push_actions_for_user_in_range_for_http(
516516
),
517517
)
518518

519+
def get_push_actions(
520+
txn: LoggingTransaction,
521+
) -> List[Tuple[str, str, int, str, bool]]:
522+
sql = """
523+
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions, ep.highlight
524+
FROM event_push_actions AS ep
525+
WHERE
526+
ep.user_id = ?
527+
AND ep.stream_ordering > ?
528+
AND ep.stream_ordering <= ?
529+
AND ep.notif = 1
530+
ORDER BY ep.stream_ordering ASC LIMIT ?
531+
"""
532+
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
533+
return cast(List[Tuple[str, str, int, str, bool]], txn.fetchall())
534+
535+
push_actions = await self.db_pool.runInteraction(
536+
"get_unread_push_actions_for_user_in_range_http", get_push_actions
537+
)
538+
519539
# find rooms that have a read receipt in them and return the next
520540
# push actions
521541
def get_after_receipt(
@@ -615,7 +635,10 @@ def get_no_receipt(
615635
stream_ordering=row[2],
616636
actions=_deserialize_action(row[3], row[4]),
617637
)
618-
for row in after_read_receipt + no_read_receipt
638+
for row in push_actions
639+
# Only include push actions with a stream ordering after any receipt, or without any
640+
# receipt present (invited to but never read rooms).
641+
if row[2] > receipts_by_room.get(row[1], 0)
619642
]
620643

621644
# Now sort it so it's ordered correctly, since currently it will
@@ -659,6 +682,28 @@ async def get_unread_push_actions_for_user_in_range_for_email(
659682
),
660683
)
661684

685+
def get_push_actions(
686+
txn: LoggingTransaction,
687+
) -> List[Tuple[str, str, int, str, bool, int]]:
688+
sql = """
689+
SELECT ep.event_id, ep.room_id, ep.stream_ordering, ep.actions,
690+
ep.highlight, e.received_ts
691+
FROM event_push_actions AS ep
692+
INNER JOIN events AS e USING (room_id, event_id)
693+
WHERE
694+
ep.user_id = ?
695+
AND ep.stream_ordering > ?
696+
AND ep.stream_ordering <= ?
697+
AND ep.notif = 1
698+
ORDER BY ep.stream_ordering DESC LIMIT ?
699+
"""
700+
txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
701+
return cast(List[Tuple[str, str, int, str, bool, int]], txn.fetchall())
702+
703+
push_actions = await self.db_pool.runInteraction(
704+
"get_unread_push_actions_for_user_in_range_email", get_push_actions
705+
)
706+
662707
# find rooms that have a read receipt in them and return the most recent
663708
# push actions
664709
def get_after_receipt(
@@ -758,7 +803,10 @@ def get_no_receipt(
758803
actions=_deserialize_action(row[3], row[4]),
759804
received_ts=row[5],
760805
)
761-
for row in after_read_receipt + no_read_receipt
806+
for row in push_actions
807+
# Only include push actions with a stream ordering after any receipt, or without any
808+
# receipt present (invited to but never read rooms).
809+
if row[2] > receipts_by_room.get(row[1], 0)
762810
]
763811

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

0 commit comments

Comments
 (0)