Made IMAP THREAD command iterable

This commit is contained in:
the-djmaze 2022-05-31 10:08:31 +02:00
parent 1c267a0030
commit 1ef7ea3f27
4 changed files with 13 additions and 18 deletions

View file

@ -402,12 +402,12 @@ trait Messages
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception
*/ */
public function MessageSimpleThread(string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array public function MessageSimpleThread(string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : iterable
{ {
$oThread = new \MailSo\Imap\Requests\THREAD($this); $oThread = new \MailSo\Imap\Requests\THREAD($this);
$oThread->sCriterias = $sSearchCriterias; $oThread->sCriterias = $sSearchCriterias;
$oThread->bUid = $bReturnUid; $oThread->bUid = $bReturnUid;
return $oThread->SendRequestGetResponse(); yield from $oThread->SendRequestIterateResponse();
} }
private function getSimpleESearchOrESortResult(bool $bReturnUid) : array private function getSimpleESearchOrESortResult(bool $bReturnUid) : array

View file

@ -18,8 +18,8 @@ namespace MailSo\Imap\Enumerations;
*/ */
abstract class StoreAction abstract class StoreAction
{ {
const SET_FLAGS = 'FLAGS'; // const SET_FLAGS = 'FLAGS';
const SET_FLAGS_SILENT = 'FLAGS.SILENT'; // const SET_FLAGS_SILENT = 'FLAGS.SILENT';
const ADD_FLAGS = '+FLAGS'; const ADD_FLAGS = '+FLAGS';
const ADD_FLAGS_SILENT = '+FLAGS.SILENT'; const ADD_FLAGS_SILENT = '+FLAGS.SILENT';
const REMOVE_FLAGS = '-FLAGS'; const REMOVE_FLAGS = '-FLAGS';

View file

@ -41,7 +41,7 @@ class THREAD extends Request
parent::__construct($oImapClient); parent::__construct($oImapClient);
} }
public function SendRequestGetResponse() : array public function SendRequestIterateResponse() : iterable
{ {
if (!$this->oImapClient->IsSupported(\strtoupper("THREAD={$this->sAlgorithm}"))) { if (!$this->oImapClient->IsSupported(\strtoupper("THREAD={$this->sAlgorithm}"))) {
$this->oImapClient->writeLogException( $this->oImapClient->writeLogException(
@ -58,7 +58,6 @@ class THREAD extends Request
) )
); );
$aReturn = array();
foreach ($this->oImapClient->yieldUntaggedResponses() as $oResponse) { foreach ($this->oImapClient->yieldUntaggedResponses() as $oResponse) {
$iOffset = ($this->bUid && 'UID' === $oResponse->StatusOrIndex && !empty($oResponse->ResponseList[2]) && 'THREAD' === $oResponse->ResponseList[2]) ? 1 : 0; $iOffset = ($this->bUid && 'UID' === $oResponse->StatusOrIndex && !empty($oResponse->ResponseList[2]) && 'THREAD' === $oResponse->ResponseList[2]) ? 1 : 0;
if (('THREAD' === $oResponse->StatusOrIndex || $iOffset) if (('THREAD' === $oResponse->StatusOrIndex || $iOffset)
@ -69,12 +68,11 @@ class THREAD extends Request
for ($iIndex = 2 + $iOffset; $iIndex < $iLen; ++$iIndex) { for ($iIndex = 2 + $iOffset; $iIndex < $iLen; ++$iIndex) {
$aNewValue = $this->validateThreadItem($oResponse->ResponseList[$iIndex]); $aNewValue = $this->validateThreadItem($oResponse->ResponseList[$iIndex]);
if (\is_array($aNewValue)) { if (\is_array($aNewValue)) {
$aReturn[] = $aNewValue; yield $aNewValue;
} }
} }
} }
} }
return $aReturn;
} }
/** /**

View file

@ -618,10 +618,15 @@ class MailClient
$this->oImapClient->FolderExamine($sFolderName); $this->oImapClient->FolderExamine($sFolderName);
$aThreadUids = array(); $aResult = array();
try try
{ {
$aThreadUids = $this->oImapClient->MessageSimpleThread($sSearchHash); foreach ($this->oImapClient->MessageSimpleThread($sSearchHash) as $mItem) {
// Flatten to single level
$aMap = [];
\array_walk_recursive($mItem, function($a) use (&$aMap) { $aMap[] = $a; });
$aResult[] = $aMap;
}
} }
catch (\MailSo\Imap\Exceptions\RuntimeException $oException) catch (\MailSo\Imap\Exceptions\RuntimeException $oException)
{ {
@ -629,14 +634,6 @@ class MailClient
unset($oException); unset($oException);
} }
// Flatten to single levels
$aResult = array();
foreach ($aThreadUids as $mItem) {
$aMap = [];
\array_walk_recursive($mItem, function($a) use (&$aMap) { $aMap[] = $a; });
$aResult[] = $aMap;
}
if ($oCacher && $oCacher->IsInited() && !empty($sSerializedHashKey)) if ($oCacher && $oCacher->IsInited() && !empty($sSerializedHashKey))
{ {
$oCacher->Set($sSerializedHashKey, \json_encode(array( $oCacher->Set($sSerializedHashKey, \json_encode(array(