email refacctoring

This commit is contained in:
Yuri Kuznetsov
2022-01-10 14:02:47 +02:00
parent 8eb7f932fc
commit 93f2f962e2
15 changed files with 347 additions and 89 deletions
+16 -15
View File
@@ -29,12 +29,12 @@
namespace Espo\Core\Mail\Account;
use Laminas\Mail\Storage;
use Espo\Core\Exceptions\Error;
use Espo\Core\Mail\Account\Storage\Flag;
use Espo\Core\Mail\Account\Storage;
use Espo\Core\Mail\Importer;
use Espo\Core\Mail\Importer\Data as ImporterData;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Mail\ParserFactory;
use Espo\Core\Mail\MessageWrapper;
use Espo\Core\Mail\Account\Hook\BeforeFetch as BeforeFetchHook;
@@ -44,6 +44,7 @@ use Espo\Core\Mail\Account\Hook\BeforeFetchResult as BeforeFetchHookResult;
use Espo\Core\Utils\DateTime as DateTimeUtil;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Log;
use Espo\Core\Field\DateTime as DateTimeField;
use Espo\Entities\EmailFilter;
use Espo\Entities\Email;
@@ -121,7 +122,7 @@ class Fetcher
private function fetchFolder(
Account $account,
string $folderOriginal,
Imap $storage,
Storage $storage,
Collection $filterList
): void {
@@ -130,7 +131,7 @@ class Fetcher
$folder = mb_convert_encoding($folderOriginal, 'UTF7-IMAP', 'UTF-8');
try {
$storage->selectFolder($folder);
$storage->selectFolder($folderOriginal);
}
catch (Throwable $e) {
$this->log->error(
@@ -211,7 +212,7 @@ class Fetcher
if ($forceByDate) {
if ($previousLastUID) {
$idList = $storage->getIdsFromUID($previousLastUID);
$idList = $storage->getIdsFromUniqueId($previousLastUID);
if (count($idList)) {
$uid1 = $storage->getUniqueId($idList[0]);
@@ -234,14 +235,14 @@ class Fetcher
*/
private function getIdList(
Account $account,
Imap $storage,
Storage $storage,
?string $lastUID,
?string $lastDate,
bool $forceByDate
): array {
if (!empty($lastUID) && !$forceByDate) {
return $storage->getIdsFromUID($lastUID);
return $storage->getIdsFromUniqueId($lastUID);
}
if ($lastDate) {
@@ -255,8 +256,8 @@ class Fetcher
$fetchSince = $account->getFetchSince()->getString();
}
return $storage->getIdsFromDate(
(new DateTime($fetchSince))->format('d-M-Y')
return $storage->getIdsSinceDate(
DateTimeField::fromString($fetchSince)
);
}
@@ -265,7 +266,7 @@ class Fetcher
*/
private function fetchEmail(
Account $account,
Imap $storage,
Storage $storage,
int $id,
Collection $filterList
): ?Email {
@@ -297,7 +298,7 @@ class Fetcher
->withAssignedUserId($assignedUserId);
try {
$message = new MessageWrapper($storage, (string) $id, $parser);
$message = new MessageWrapper($id, $storage, $parser);
$hookResult = null;
@@ -318,9 +319,9 @@ class Fetcher
if (
$account->keepFetchedEmailsUnread() &&
is_array($flags) &&
empty($flags[Storage::FLAG_SEEN])
empty($flags[Flag::SEEN])
) {
unset($flags[Storage::FLAG_RECENT]);
unset($flags[Flag::RECENT]);
$storage->setFlags($id, $flags);
}
@@ -395,7 +396,7 @@ class Fetcher
->find();
}
private function checkFetchOnlyHeader(Imap $storage, int $id): bool
private function checkFetchOnlyHeader(Storage $storage, int $id): bool
{
$maxSize = $this->config->get('emailMessageMaxSize');
@@ -39,8 +39,6 @@ use Espo\Core\Mail\Account\StorageFactory;
use Espo\Core\Utils\Crypt;
use RecursiveIteratorIterator;
class Service
{
private Fetcher $fetcher;
@@ -90,18 +88,7 @@ class Service
$storage = $this->storageFactory->createWithParams($params);
$folderIterator = new RecursiveIteratorIterator(
$storage->getFolders(),
RecursiveIteratorIterator::SELF_FIRST
);
$list = [];
foreach ($folderIterator as $folder) {
$list[] = mb_convert_encoding($folder->getGlobalName(), 'UTF-8', 'UTF7-IMAP');
}
return $list;
return $storage->getFolderNames();
}
public function testConnection(Params $params): void
@@ -118,7 +105,7 @@ class Service
$storage = $this->storageFactory->createWithParams($params);
$storage->getFolders();
$storage->getFolderNames();
}
/**
@@ -32,7 +32,7 @@ namespace Espo\Core\Mail\Account\GroupAccount;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\Account;
use Espo\Core\Mail\Account\StorageFactory as StorageFactoryInterface;
use Espo\Core\Mail\Account\Storage\LaminasStorage;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Utils\Crypt;
@@ -49,8 +49,6 @@ class StorageFactory implements StorageFactoryInterface
private InjectableFactory $injectableFactory;
private string $storageClassName = Imap::class;
public function __construct(Crypt $crypt, Log $log, InjectableFactory $injectableFactory)
{
$this->crypt = $crypt;
@@ -58,7 +56,7 @@ class StorageFactory implements StorageFactoryInterface
$this->injectableFactory = $injectableFactory;
}
public function create(Account $account): Imap
public function create(Account $account): LaminasStorage
{
$params = Params::createBuilder()
->setHost($account->getHost())
@@ -75,7 +73,7 @@ class StorageFactory implements StorageFactoryInterface
return $this->createWithParams($params);
}
public function createWithParams(Params $params): Imap
public function createWithParams(Params $params): LaminasStorage
{
$rawParams = [
'host' => $params->getHost(),
@@ -127,6 +125,8 @@ class StorageFactory implements StorageFactoryInterface
}
}
return new $this->storageClassName($imapParams);
return new LaminasStorage(
new Imap($imapParams)
);
}
}
@@ -42,8 +42,6 @@ use Espo\Core\Utils\Crypt;
use Espo\Entities\User;
use RecursiveIteratorIterator;
class Service
{
private Fetcher $fetcher;
@@ -107,18 +105,7 @@ class Service
$storage = $this->storageFactory->createWithParams($params);
$folderIterator = new RecursiveIteratorIterator(
$storage->getFolders(),
RecursiveIteratorIterator::SELF_FIRST
);
$list = [];
foreach ($folderIterator as $folder) {
$list[] = mb_convert_encoding($folder->getGlobalName(), 'UTF-8', 'UTF7-IMAP');
}
return $list;
return $storage->getFolderNames();
}
public function testConnection(Params $params): void
@@ -156,7 +143,7 @@ class Service
$storage = $this->storageFactory->createWithParams($params);
$storage->getFolders();
$storage->getFolderNames();
}
/**
@@ -34,6 +34,7 @@ use Espo\Core\Mail\Account\StorageFactory as StorageFactoryInterface;
use Espo\Core\Mail\Account\Account;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Mail\Account\Storage\LaminasStorage;
use Espo\Core\Utils\Crypt;
use Espo\Core\Utils\Log;
@@ -56,8 +57,6 @@ class StorageFactory implements StorageFactoryInterface
private EntityManager $entityManager;
private string $storageClassName = Imap::class;
public function __construct(
Crypt $crypt,
Log $log,
@@ -70,7 +69,7 @@ class StorageFactory implements StorageFactoryInterface
$this->entityManager = $entityManager;
}
public function create(Account $account): Imap
public function create(Account $account): LaminasStorage
{
$params = Params::createBuilder()
->setHost($account->getHost())
@@ -89,7 +88,7 @@ class StorageFactory implements StorageFactoryInterface
return $this->createWithParams($params);
}
public function createWithParams(Params $params): Imap
public function createWithParams(Params $params): LaminasStorage
{
$rawParams = [
'host' => $params->getHost(),
@@ -172,7 +171,9 @@ class StorageFactory implements StorageFactoryInterface
}
}
return new $this->storageClassName($imapParams);
return new LaminasStorage(
new Imap($imapParams)
);
}
private function getUserDataRepository(): UserDataRepository
@@ -0,0 +1,98 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Mail\Account;
use Espo\Core\Field\DateTime;
interface Storage
{
/**
* Set message flags.
*
* @param string[] $flags
*/
public function setFlags(int $id, array $flags): void;
/**
* Get a message size.
*/
public function getSize(int $id): int;
/**
* Get message raw content.
*/
public function getRawContent(int $id): string;
/**
* Get a message unique ID.
*/
public function getUniqueId(int $id): string;
/**
* Get IDs from unique ID.
*
* @return int[]
*/
public function getIdsFromUniqueId(string $uniqueId): array;
/**
* Get IDs since a specific date.
*
* @return int[]
*/
public function getIdsSinceDate(DateTime $since): array;
/**
* Get only header and flags. Won't fetch the whole email.
*
* @return array{header: string, flags: string[]}
*/
public function getHeaderAndFlags(int $id): array;
/**
* Close the resource.
*/
public function close(): void;
/**
* @retrun string[]
*/
public function getFolderNames(): array;
/**
* Select a folder.
*/
public function selectFolder(string $name): void;
/**
* Store a message.
*/
public function appendMessage(string $content, ?string $folder = null): void;
}
@@ -0,0 +1,49 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Mail\Account\Storage;
class Flag
{
public const PASSED = 'Passed';
public const SEEN = '\Seen';
public const UNSEEN = '\Unseen';
public const ANSWERED = '\Answered';
public const FLAGGED = '\Flagged';
public const DELETED = '\Deleted';
public const DRAFT = '\Draft';
public const RECENT = '\Recent';
}
@@ -0,0 +1,131 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Mail\Account\Storage;
use Espo\Core\Mail\Account\Storage;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Field\DateTime;
use RecursiveIteratorIterator;
class LaminasStorage implements Storage
{
private Imap $imap;
public function __construct(Imap $imap)
{
$this->imap = $imap;
}
/**
* @param string[] $flags
*/
public function setFlags(int $id, array $flags): void
{
$this->imap->setFlags($id, $flags);
}
public function getSize(int $id): int
{
return $this->imap->getSize($id);
}
public function getRawContent(int $id): string
{
return $this->imap->getRawContent($id);
}
public function getUniqueId(int $id): string
{
return $this->imap->getUniqueId($id);
}
/**
* @return int[]
*/
public function getIdsFromUniqueId(string $uniqueId): array
{
return $this->imap->getIdsFromUniqueId($uniqueId);
}
/**
* @return int[]
*/
public function getIdsSinceDate(DateTime $since): array
{
return $this->imap->getIdsSinceDate(
$since->getDateTime()->format('d-M-Y')
);
}
/**
* @return array{header: string, flags: string[]}
*/
public function getHeaderAndFlags(int $id): array
{
return $this->imap->getHeaderAndFlags($id);
}
public function close(): void
{
$this->imap->close();
}
/**
* @retrun string[]
*/
public function getFolderNames(): array
{
$folderIterator = new RecursiveIteratorIterator(
$this->imap->getFolders(),
RecursiveIteratorIterator::SELF_FIRST
);
$list = [];
foreach ($folderIterator as $folder) {
$list[] = mb_convert_encoding($folder->getGlobalName(), 'UTF-8', 'UTF7-IMAP');
}
return $list;
}
public function selectFolder(string $name): void
{
$nameConverted = mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8');
$this->imap->selectFolder($nameConverted);
}
public function appendMessage(string $content, ?string $folder = null): void
{
$this->imap->appendMessage($content, $folder);
}
}
@@ -29,12 +29,12 @@
namespace Espo\Core\Mail\Account;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Mail\Account\Storage\Params;
use Espo\Core\Mail\Account\Storage;
interface StorageFactory
{
public function create(Account $account): Imap;
public function create(Account $account): Storage;
public function createWithParams(Params $params): Imap;
public function createWithParams(Params $params): Storage;
}
+3 -1
View File
@@ -102,7 +102,9 @@ class Importer
$folderData = $data->getFolderData();
$parser = $message instanceof MessageWrapper ?
$message->getParser() :
(
$message->getParser() ?? $this->parserFactory->create()
) :
$this->parserFactory->create();
/** @var Email $email */
@@ -31,31 +31,43 @@ namespace Espo\Core\Mail\Mail\Storage;
class Imap extends \Laminas\Mail\Storage\Imap
{
public function getIdsFromUID($uid)
/**
* @return int[]
*/
public function getIdsFromUniqueId(string $uid): array
{
$uid = intval($uid) + 1;
return $this->protocol->search(['UID ' . $uid . ':*']);
$nextUid = strval(intval($uid) + 1);
return $this->protocol->search(['UID ' . $nextUid . ':*']);
}
public function getIdsFromDate($date)
/**
* @return int[]
*/
public function getIdsSinceDate(string $date): array
{
return $this->protocol->search(['SINCE "' . $date . '"']);
}
public function getHeaderAndFlags($id, $part = null)
/**
* @param int $id
* @return array{header: string, flags: string[]}
*/
public function getHeaderAndFlags(int $id): array
{
$data = $this->protocol->fetch(['FLAGS', 'RFC822.HEADER'], $id);
$header = $data['RFC822.HEADER'];
$flags = [];
foreach ($data['FLAGS'] as $flag) {
$flags[] = isset(static::$knownFlags[$flag]) ? static::$knownFlags[$flag] : $flag;
}
return [
'flags' => $flags,
'header' => $header
'header' => $header,
];
}
}
-5
View File
@@ -56,11 +56,6 @@ interface Message
*/
public function getFullRawContent(): string;
/**
* Set a full raw message.
*/
public function setFullRawContent(string $content): void;
/**
* Get flags.
*
+11 -11
View File
@@ -29,16 +29,16 @@
namespace Espo\Core\Mail;
use Espo\Core\Mail\Mail\Storage\Imap;
use Espo\Core\Mail\Account\Storage;
class MessageWrapper implements Message
{
private int $id;
private $parser;
private $storage;
private $id;
private $rawHeader = null;
private $rawContent = null;
@@ -47,8 +47,12 @@ class MessageWrapper implements Message
private $flagList = null;
public function __construct(?Imap $storage = null, ?string $id = null, ?Parser $parser = null)
{
public function __construct(
int $id,
?Storage $storage = null,
?Parser $parser = null,
?string $fullRawContent = null
) {
if ($storage) {
$data = $storage->getHeaderAndFlags($id);
@@ -59,11 +63,7 @@ class MessageWrapper implements Message
$this->id = $id;
$this->storage = $storage;
$this->parser = $parser;
}
public function setFullRawContent(string $content): void
{
$this->fullRawContent = $content;
$this->fullRawContent = $fullRawContent;
}
public function getRawHeader(): string
@@ -89,7 +89,7 @@ class MessageWrapper implements Message
public function getRawContent(): string
{
if (is_null($this->rawContent)) {
$this->rawContent = $this->storage->getRawContent((int) $this->id);
$this->rawContent = $this->storage->getRawContent($this->id);
}
return $this->rawContent ?? '';
@@ -29,13 +29,10 @@
namespace tests\unit\Espo\Core\Mail;
use Espo\Core\{
Mail\ImporterData,
};
use Espo\Core\Mail\Importer\Data as ImporterData;
use Espo\Entities\EmailFilter;
class ImporterDataTest extends \PHPUnit\Framework\TestCase
{
+1 -3
View File
@@ -165,9 +165,7 @@ class ImporterTest extends \PHPUnit\Framework\TestCase
$this->linkMultipleSaver
);
$message = new MessageWrapper();
$message->setFullRawContent($contents);
$message = new MessageWrapper(0, null, null, $contents);
$data = ImporterData
::create()