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

Commit 041fe7f

Browse files
committed
Only attempt to find threaded receipts newer than the latest unthreaded receipt.
1 parent 6b2384d commit 041fe7f

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

synapse/storage/databases/main/event_push_actions.py

+10-19
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
DatabasePool,
9696
LoggingDatabaseConnection,
9797
LoggingTransaction,
98-
PostgresEngine,
9998
)
10099
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
101100
from synapse.storage.databases.main.stream import StreamWorkerStore
@@ -448,20 +447,6 @@ def _get_thread(thread_id: str) -> NotifCounts:
448447
(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
449448
)
450449

451-
# A clause to get the latest receipt stream ordering taking into account
452-
# both unthreaded and threaded receipts. This takes a single parameter:
453-
# receipt_stream_ordering.
454-
#
455-
# PostgreSQL and SQLite differ in comparing scalar numerics.
456-
if isinstance(self.database_engine, PostgresEngine):
457-
# GREATEST ignores NULLs.
458-
receipt_stream_clause = "GREATEST(threaded_receipt_stream_ordering, ?)"
459-
else:
460-
# MAX returns NULL if any are NULL, so COALESCE to 0 first.
461-
receipt_stream_clause = (
462-
"MAX(COALESCE(threaded_receipt_stream_ordering, 0), ?)"
463-
)
464-
465450
# First we pull the counts from the summary table.
466451
#
467452
# We check that `last_receipt_stream_ordering` matches the stream ordering of the
@@ -490,18 +475,20 @@ def _get_thread(thread_id: str) -> NotifCounts:
490475
WHERE
491476
user_id = ?
492477
AND room_id = ?
478+
AND stream_ordering > ?
493479
AND {receipt_types_clause}
494480
GROUP BY thread_id
495481
) AS receipts USING (thread_id)
496482
WHERE room_id = ? AND user_id = ?
497483
AND (
498-
(last_receipt_stream_ordering IS NULL AND stream_ordering > {receipt_stream_clause})
499-
OR last_receipt_stream_ordering = {receipt_stream_clause}
484+
(last_receipt_stream_ordering IS NULL AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?))
485+
OR last_receipt_stream_ordering = COALESCE(threaded_receipt_stream_ordering, ?)
500486
) AND (notif_count != 0 OR COALESCE(unread_count, 0) != 0)
501487
""",
502488
(
503489
user_id,
504490
room_id,
491+
unthreaded_receipt_stream_ordering,
505492
*receipts_args,
506493
room_id,
507494
user_id,
@@ -526,12 +513,13 @@ def _get_thread(thread_id: str) -> NotifCounts:
526513
WHERE
527514
user_id = ?
528515
AND room_id = ?
516+
AND stream_ordering > ?
529517
AND {receipt_types_clause}
530518
GROUP BY thread_id
531519
) AS receipts USING (thread_id)
532520
WHERE user_id = ?
533521
AND room_id = ?
534-
AND stream_ordering > {receipt_stream_clause}
522+
AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
535523
AND highlight = 1
536524
GROUP BY thread_id
537525
"""
@@ -540,6 +528,7 @@ def _get_thread(thread_id: str) -> NotifCounts:
540528
(
541529
user_id,
542530
room_id,
531+
unthreaded_receipt_stream_ordering,
543532
*receipts_args,
544533
user_id,
545534
room_id,
@@ -603,12 +592,13 @@ def _get_thread(thread_id: str) -> NotifCounts:
603592
WHERE
604593
user_id = ?
605594
AND room_id = ?
595+
AND stream_ordering > ?
606596
AND {receipt_types_clause}
607597
GROUP BY thread_id
608598
) AS receipts USING (thread_id)
609599
WHERE user_id = ?
610600
AND room_id = ?
611-
AND stream_ordering > {receipt_stream_clause}
601+
AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
612602
AND NOT {thread_id_clause}
613603
GROUP BY thread_id
614604
"""
@@ -617,6 +607,7 @@ def _get_thread(thread_id: str) -> NotifCounts:
617607
(
618608
user_id,
619609
room_id,
610+
unthreaded_receipt_stream_ordering,
620611
*receipts_args,
621612
user_id,
622613
room_id,

0 commit comments

Comments
 (0)