2022-05-13 06:49:25 +08:00
|
|
|
<?php
|
|
|
|
|
2022-05-17 23:15:20 +08:00
|
|
|
class ImapContactsSuggestions implements \RainLoop\Providers\Suggestions\ISuggestions
|
2022-05-13 06:49:25 +08:00
|
|
|
{
|
|
|
|
// TODO: make setting
|
2022-05-17 23:15:20 +08:00
|
|
|
public $sFolderName = 'INBOX';
|
2022-05-13 06:49:25 +08:00
|
|
|
|
|
|
|
public function Process(\RainLoop\Model\Account $oAccount, string $sQuery, int $iLimit = 20): array
|
|
|
|
{
|
|
|
|
$sQuery = \trim($sQuery);
|
|
|
|
if (2 > \strlen($sQuery)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$oActions = \RainLoop\Api::Actions();
|
|
|
|
$oMailClient = $oActions->MailClient();
|
|
|
|
if (!$oMailClient->IsLoggined()) {
|
|
|
|
$oAccount = $oActions->getAccountFromToken();
|
2022-12-16 16:45:18 +08:00
|
|
|
$oAccount->ImapConnectAndLoginHelper($oActions->Plugins(), $oMailClient->ImapClient(), $oActions->Config());
|
2022-05-13 06:49:25 +08:00
|
|
|
}
|
|
|
|
$oImapClient = $oMailClient->ImapClient();
|
|
|
|
|
|
|
|
$oImapClient->FolderSelect($this->sFolderName);
|
|
|
|
|
|
|
|
$sQuery = \MailSo\Imap\SearchCriterias::escapeSearchString($oImapClient, $sQuery);
|
|
|
|
$aUids = \array_slice(
|
|
|
|
$oImapClient->MessageSimpleSearch("FROM {$sQuery}"),
|
|
|
|
0, $iLimit
|
|
|
|
);
|
|
|
|
|
|
|
|
$aResult = [];
|
2022-05-29 05:38:41 +08:00
|
|
|
if ($aUids) {
|
|
|
|
foreach ($oImapClient->Fetch(['BODY.PEEK[HEADER.FIELDS (FROM)]'], \implode(',', $aUids), true) as $oFetchResponse) {
|
|
|
|
$oHeaders = new \MailSo\Mime\HeaderCollection($oFetchResponse->GetHeaderFieldsValue());
|
|
|
|
$oFrom = $oHeaders->GetAsEmailCollection(\MailSo\Mime\Enumerations\Header::FROM_, true);
|
|
|
|
foreach ($oFrom as $oMail) {
|
|
|
|
$aResult[] = [$oMail->GetEmail(), $oMail->GetDisplayName()];
|
|
|
|
}
|
2022-05-13 06:49:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $aResult;
|
|
|
|
}
|
|
|
|
}
|