From 341b655467ac672ec2463769ca3df284801f9a87 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Thu, 19 Sep 2024 12:42:44 +0200 Subject: [PATCH] fix(threading): Handle threads with duplicate send times Signed-off-by: Christoph Wurst --- lib/Db/MessageMapper.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index 47cf21292a..fc84f44a9a 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -792,7 +792,13 @@ public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, string $sor $selfJoin = $select->expr()->andX( $select->expr()->eq('m.mailbox_id', 'm2.mailbox_id', IQueryBuilder::PARAM_INT), $select->expr()->eq('m.thread_root_id', 'm2.thread_root_id', IQueryBuilder::PARAM_INT), - $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT) + $select->expr()->orX( + $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->andX( + $select->expr()->eq('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->lt('m.message_id', 'm2.message_id', IQueryBuilder::PARAM_STR), + ), + ), ); $select->from($this->getTableName(), 'm') @@ -1016,7 +1022,13 @@ public function findIdsGloballyByQuery(IUser $user, SearchQuery $query, ?int $li $selfJoin = $select->expr()->andX( $select->expr()->eq('m.mailbox_id', 'm2.mailbox_id', IQueryBuilder::PARAM_INT), $select->expr()->eq('m.thread_root_id', 'm2.thread_root_id', IQueryBuilder::PARAM_INT), - $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT) + $select->expr()->orX( + $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->andX( + $select->expr()->eq('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->lt('m.message_id', 'm2.message_id', IQueryBuilder::PARAM_STR), + ), + ), ); $select->from($this->getTableName(), 'm') @@ -1373,9 +1385,15 @@ public function findNewIds(Mailbox $mailbox, array $ids, ?int $lastMessageTimest $selfJoin = $select->expr()->andX( $select->expr()->eq('m.mailbox_id', 'm2.mailbox_id', IQueryBuilder::PARAM_INT), $select->expr()->eq('m.thread_root_id', 'm2.thread_root_id', IQueryBuilder::PARAM_INT), - $sortOrder === IMailSearch::ORDER_NEWEST_FIRST ? - $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT) : - $select->expr()->gt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT) + $select->expr()->orX( + $sortOrder === IMailSearch::ORDER_NEWEST_FIRST ? + $select->expr()->lt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT) : + $select->expr()->gt('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->andX( + $select->expr()->eq('m.sent_at', 'm2.sent_at', IQueryBuilder::PARAM_INT), + $select->expr()->lt('m.message_id', 'm2.message_id', IQueryBuilder::PARAM_STR), + ), + ), ); $wheres = [$select->expr()->eq('m.mailbox_id', $select->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT)), $select->expr()->andX($subSelect->expr()->notIn('m.id', $select->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)),