Skip to content

Commit

Permalink
fixup! add sorting capabilities to mail
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Sep 28, 2023
1 parent d9e8fe6 commit 0ca20de
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,9 @@ 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)
$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->from($this->getTableName(), 'm')
Expand Down Expand Up @@ -807,15 +809,27 @@ public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, string $sor
}

if (!empty($query->getStart())) {
$select->andWhere(
$qb->expr()->gte('m.sent_at', $qb->createNamedParameter($query->getStart()), IQueryBuilder::PARAM_INT)
);
if ($sortOrder === IMailSearch::ORDER_NEWEST_FIRST) {
$select->andWhere(
$qb->expr()->gte('m.sent_at', $qb->createNamedParameter($query->getStart()), IQueryBuilder::PARAM_INT)
);
} else {
$select->andWhere(
$qb->expr()->lte('m.sent_at', $qb->createNamedParameter($query->getStart()), IQueryBuilder::PARAM_INT)
);
}
}

if (!empty($query->getEnd())) {
$select->andWhere(
$qb->expr()->lte('m.sent_at', $qb->createNamedParameter($query->getEnd()), IQueryBuilder::PARAM_INT)
);
if ($sortOrder === IMailSearch::ORDER_NEWEST_FIRST) {
$select->andWhere(
$qb->expr()->lte('m.sent_at', $qb->createNamedParameter($query->getEnd()), IQueryBuilder::PARAM_INT)
);
} else {
$select->andWhere(
$qb->expr()->gte('m.sent_at', $qb->createNamedParameter($query->getEnd()), IQueryBuilder::PARAM_INT)
);
}
}


Expand All @@ -825,12 +839,11 @@ public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, string $sor
);
}

if ($query->getCursor() !== null && $sortOrder === IMailSearch::ORDER_NEWEST_FIRST) {
if ($query->getCursor() !== null && $sortOrder === IMailSearch::ORDER_NEWEST_FIRST) {
$select->andWhere(
$qb->expr()->lt('m.sent_at', $qb->createNamedParameter($query->getCursor(), IQueryBuilder::PARAM_INT))
);
}
elseif ($query->getCursor() !== null && $sortOrder === IMailSearch::ORDER_OLDEST_FIRST){
} elseif ($query->getCursor() !== null && $sortOrder === IMailSearch::ORDER_OLDEST_FIRST){
$select->andWhere(
$qb->expr()->gt('m.sent_at', $qb->createNamedParameter($query->getCursor(), IQueryBuilder::PARAM_INT))
);
Expand Down Expand Up @@ -881,15 +894,15 @@ public function findIdsByQuery(Mailbox $mailbox, SearchQuery $query, string $sor

public function findLastMessageTimestamp(Account $account, Mailbox $mailbox, ?SearchQuery $query, string $sortOrder): ?int {
if ($query !== null) {
$lastMessageId = $this->findIdsByQuery($mailbox,$query,IMailSearch::ORDER_NEWEST_FIRST, 1, null, )[0] ?? null;
$lastMessageId = $this->findIdsByQuery($mailbox,$query,$sortOrder, 1)[0] ?? null;
} else {
$lastMessageId = $this->findAllIds($mailbox, 1, IMailSearch::ORDER_NEWEST_FIRST)[0] ?? null;
$lastMessageId = $this->findAllIds($mailbox)[0] ?? null;
}
if ($lastMessageId === null) {
return null;
}

$lastMessages = $this->findByIds($account->getUserId(), [$lastMessageId],$sortOrder);
$lastMessages = $this->findByIds($account->getUserId(), [$lastMessageId], $sortOrder);
if (empty($lastMessages)) {
// Should never happen
return null;
Expand Down Expand Up @@ -1244,7 +1257,9 @@ public function findNewIds(Mailbox $mailbox, array $ids, ?int $lastMessageTimest
$subSelect = $this->db->getQueryBuilder();

$subSelect
->select($subSelect->func()->min('sent_at'))
->select($sortOrder === IMailSearch::ORDER_NEWEST_FIRST ?
$subSelect->func()->min('sent_at') :
$subSelect->func()->max('sent_at'))
->from($this->getTableName())
->where(
$subSelect->expr()->eq('mailbox_id', $select->createNamedParameter($mailbox->getId(), IQueryBuilder::PARAM_INT)),
Expand All @@ -1256,7 +1271,9 @@ 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),
$select->expr()->lt('m.sent_at', 'm2.sent_at', 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)
);
$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)),
Expand All @@ -1279,7 +1296,7 @@ public function findNewIds(Mailbox $mailbox, array $ids, ?int $lastMessageTimest
->from($this->getTableName(), 'm')
->leftJoin('m', $this->getTableName(), 'm2', $selfJoin)
->where(...$wheres)
->orderBy('m.sent_at', 'desc');
->orderBy('m.sent_at', $sortOrder === IMailSearch::ORDER_NEWEST_FIRST ? 'desc' : 'asc');

$results = [];
foreach (array_chunk($ids, 1000) as $chunk) {
Expand Down

0 comments on commit 0ca20de

Please sign in to comment.