From c8deecb4aa2054581ba0c756ce6ee27de90b9849 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Fri, 30 Dec 2022 19:47:40 +0100 Subject: [PATCH] Speedup MessageListByRequestIndexOrUids() by using the new FetchIterate() --- .../MailSo/Imap/Commands/Messages.php | 11 ++++- .../app/libraries/MailSo/Mail/MailClient.php | 40 +++++++++---------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Imap/Commands/Messages.php b/snappymail/v/0.0.0/app/libraries/MailSo/Imap/Commands/Messages.php index e238e947f..ffd1aeb37 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Imap/Commands/Messages.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Imap/Commands/Messages.php @@ -32,7 +32,7 @@ trait Messages * @throws \MailSo\Net\Exceptions\* * @throws \MailSo\Imap\Exceptions\* */ - public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array + public function FetchIterate(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : iterable { if (!\strlen(\trim($sIndexRange))) { $this->writeLogException(new \InvalidArgumentException, \LOG_ERR); @@ -112,7 +112,7 @@ trait Messages foreach ($this->yieldUntaggedResponses() as $oResponse) { if (FetchResponse::isValidImapResponse($oResponse)) { if (FetchResponse::hasUidAndSize($oResponse)) { - $aReturn[] = new FetchResponse($oResponse); + yield new FetchResponse($oResponse); } else if ($this->oLogger) { $this->oLogger->Write('Skipped Imap Response! ['.$oResponse.']', \LOG_NOTICE); } @@ -121,7 +121,14 @@ trait Messages } finally { $this->aFetchCallbacks = array(); } + } + public function Fetch(array $aInputFetchItems, string $sIndexRange, bool $bIndexIsUid) : array + { + $aReturn = array(); + foreach ($this->FetchIterate($aInputFetchItems, $sIndexRange, $bIndexIsUid) as $oFetchResponse) { + $aReturn[] = $oFetchResponse; + } return $aReturn; } diff --git a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php index ee07ff2ed..3053aaa82 100644 --- a/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php +++ b/snappymail/v/0.0.0/app/libraries/MailSo/Mail/MailClient.php @@ -479,7 +479,7 @@ class MailClient protected function MessageListByRequestIndexOrUids(MessageCollection $oMessageCollection, SequenceSet $oRange, array &$aAllThreads = []) : void { if (\count($oRange)) { - $aFetchResponse = $this->oImapClient->Fetch(array( + $aFetchIterator = $this->oImapClient->FetchIterate(array( FetchType::UID, FetchType::RFC822_SIZE, FetchType::INTERNALDATE, @@ -487,31 +487,29 @@ class MailClient FetchType::BODYSTRUCTURE, $this->getEnvelopeOrHeadersRequestString() ), (string) $oRange, $oRange->UID); - - if (\count($aFetchResponse)) { - $aCollection = \array_fill_keys($oRange->getArrayCopy(), null); - foreach ($aFetchResponse as /* @var $oFetchResponseItem \MailSo\Imap\FetchResponse */ $oFetchResponseItem) { - $id = $oRange->UID - ? $oFetchResponseItem->GetFetchValue(FetchType::UID) - : $oFetchResponseItem->oImapResponse->ResponseList[1]; - $oMessage = Message::fromFetchResponse($oMessageCollection->FolderName, $oFetchResponseItem); - if ($oMessage) { - if ($aAllThreads) { - $iUid = $oMessage->Uid; - // Find thread and set it. - // Used by GUI to delete/move the whole thread or other features - foreach ($aAllThreads as $aMap) { - if (\in_array($iUid, $aMap)) { - $oMessage->SetThreads($aMap); - break; - } + // FETCH does not respond in the id order of the SequenceSet, so we prefill $aCollection for the right sort order. + $aCollection = \array_fill_keys($oRange->getArrayCopy(), null); + foreach ($aFetchIterator as /* @var $oFetchResponseItem \MailSo\Imap\FetchResponse */ $oFetchResponseItem) { + $id = $oRange->UID + ? $oFetchResponseItem->GetFetchValue(FetchType::UID) + : $oFetchResponseItem->oImapResponse->ResponseList[1]; + $oMessage = Message::fromFetchResponse($oMessageCollection->FolderName, $oFetchResponseItem); + if ($oMessage) { + if ($aAllThreads) { + $iUid = $oMessage->Uid; + // Find thread and set it. + // Used by GUI to delete/move the whole thread or other features + foreach ($aAllThreads as $aMap) { + if (\in_array($iUid, $aMap)) { + $oMessage->SetThreads($aMap); + break; } } - $aCollection[$id] = $oMessage; } + $aCollection[$id] = $oMessage; } - $oMessageCollection->exchangeArray(\array_values(\array_filter($aCollection))); } + $oMessageCollection->exchangeArray(\array_values(\array_filter($aCollection))); } }