95
95
DatabasePool ,
96
96
LoggingDatabaseConnection ,
97
97
LoggingTransaction ,
98
- PostgresEngine ,
99
98
)
100
99
from synapse .storage .databases .main .receipts import ReceiptsWorkerStore
101
100
from synapse .storage .databases .main .stream import StreamWorkerStore
@@ -448,20 +447,6 @@ def _get_thread(thread_id: str) -> NotifCounts:
448
447
(ReceiptTypes .READ , ReceiptTypes .READ_PRIVATE ),
449
448
)
450
449
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
-
465
450
# First we pull the counts from the summary table.
466
451
#
467
452
# We check that `last_receipt_stream_ordering` matches the stream ordering of the
@@ -490,18 +475,20 @@ def _get_thread(thread_id: str) -> NotifCounts:
490
475
WHERE
491
476
user_id = ?
492
477
AND room_id = ?
478
+ AND stream_ordering > ?
493
479
AND { receipt_types_clause }
494
480
GROUP BY thread_id
495
481
) AS receipts USING (thread_id)
496
482
WHERE room_id = ? AND user_id = ?
497
483
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, ?)
500
486
) AND (notif_count != 0 OR COALESCE(unread_count, 0) != 0)
501
487
""" ,
502
488
(
503
489
user_id ,
504
490
room_id ,
491
+ unthreaded_receipt_stream_ordering ,
505
492
* receipts_args ,
506
493
room_id ,
507
494
user_id ,
@@ -526,12 +513,13 @@ def _get_thread(thread_id: str) -> NotifCounts:
526
513
WHERE
527
514
user_id = ?
528
515
AND room_id = ?
516
+ AND stream_ordering > ?
529
517
AND { receipt_types_clause }
530
518
GROUP BY thread_id
531
519
) AS receipts USING (thread_id)
532
520
WHERE user_id = ?
533
521
AND room_id = ?
534
- AND stream_ordering > { receipt_stream_clause }
522
+ AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
535
523
AND highlight = 1
536
524
GROUP BY thread_id
537
525
"""
@@ -540,6 +528,7 @@ def _get_thread(thread_id: str) -> NotifCounts:
540
528
(
541
529
user_id ,
542
530
room_id ,
531
+ unthreaded_receipt_stream_ordering ,
543
532
* receipts_args ,
544
533
user_id ,
545
534
room_id ,
@@ -603,12 +592,13 @@ def _get_thread(thread_id: str) -> NotifCounts:
603
592
WHERE
604
593
user_id = ?
605
594
AND room_id = ?
595
+ AND stream_ordering > ?
606
596
AND { receipt_types_clause }
607
597
GROUP BY thread_id
608
598
) AS receipts USING (thread_id)
609
599
WHERE user_id = ?
610
600
AND room_id = ?
611
- AND stream_ordering > { receipt_stream_clause }
601
+ AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
612
602
AND NOT { thread_id_clause }
613
603
GROUP BY thread_id
614
604
"""
@@ -617,6 +607,7 @@ def _get_thread(thread_id: str) -> NotifCounts:
617
607
(
618
608
user_id ,
619
609
room_id ,
610
+ unthreaded_receipt_stream_ordering ,
620
611
* receipts_args ,
621
612
user_id ,
622
613
room_id ,
0 commit comments