Skip to content

Commit

Permalink
email filter move to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Oct 12, 2022
1 parent a7d3073 commit 4e31fc8
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 299 deletions.
25 changes: 17 additions & 8 deletions application/Espo/Classes/Acl/EmailFilter/OwnershipChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

namespace Espo\Classes\Acl\EmailFilter;

use Espo\Entities\EmailAccount;
use Espo\Entities\User;
use Espo\Entities\EmailFilter;

use Espo\ORM\Entity;

Expand All @@ -39,41 +41,48 @@
};

/**
* @implements OwnershipOwnChecker<\Espo\Entities\EmailFilter>
* @implements OwnershipOwnChecker<EmailFilter>
*/
class OwnershipChecker implements OwnershipOwnChecker
{
private $entityManager;
private EntityManager $entityManager;

public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}

/**
* @param EmailFilter $entity
*/
public function checkOwn(User $user, Entity $entity): bool
{
if (!$entity->has('parentId') || !$entity->has('parentType')) {
if ($entity->isGlobal()) {
return false;
}

$parentType = $entity->get('parentType');
$parentId = $entity->get('parentId');
$parentType = $entity->getParentType();
$parentId = $entity->getParentId();

if (!$parentType || !$parentId) {
return false;
}

$parent = $this->entityManager->getEntity($parentType, $parentId);
$parent = $this->entityManager->getEntityById($parentType, $parentId);

if (!$parent) {
return false;
}

if ($parent->getEntityType() === 'User') {
if ($parent->getEntityType() === User::ENTITY_TYPE) {
return $parent->getId() === $user->getId();
}

if ($parent->has('assignedUserId') && $parent->get('assignedUserId') === $user->getId()) {
if (
$parent instanceof EmailAccount &&
$parent->has('assignedUserId') &&
$parent->get('assignedUserId') === $user->getId()
) {
return true;
}

Expand Down
31 changes: 26 additions & 5 deletions application/Espo/Core/Mail/Account/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@
use Espo\Entities\EmailFilter;
use Espo\Entities\Email;

use Espo\Entities\InboundEmail;
use Espo\ORM\Collection;
use Espo\ORM\EntityManager;

use Espo\ORM\Query\Part\Expression;
use Espo\ORM\Query\Part\Order;
use Throwable;
use DateTime;

Expand Down Expand Up @@ -392,22 +395,40 @@ private function processBeforeFetchHook(Account $account, MessageWrapper $messag
*/
private function getFilterList(Account $account): Collection
{
/** @var Collection<EmailFilter> */
return $this->entityManager
$actionList = [EmailFilter::ACTION_SKIP];

if ($account->getEntityType() === InboundEmail::ENTITY_TYPE) {
$actionList[] = EmailFilter::ACTION_MOVE_TO_GROUP_FOLDER;
}

$builder = $this->entityManager
->getRDBRepository(EmailFilter::ENTITY_TYPE)
->where([
'action' => 'Skip',
'action' => $actionList,
'OR' => [
[
'parentType' => $account->getEntityType(),
'parentId' => $account->getId(),
'action' => $actionList,
],
[
'parentId' => null,
'action' => EmailFilter::ACTION_SKIP,
],
]
])
->find();
]);

if (count($actionList) > 1) {
$builder->order(
Order::createByPositionInList(
Expression::column('action'),
$actionList
)
);
}

/** @var Collection<EmailFilter> */
return $builder->find();
}

private function checkFetchOnlyHeader(Storage $storage, int $id): bool
Expand Down
21 changes: 19 additions & 2 deletions application/Espo/Core/Mail/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use Espo\Core\Mail\Importer\DuplicateFinder;
use Espo\Entities\Email;
use Espo\Entities\EmailFilter;
use Espo\Entities\Job;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
Expand Down Expand Up @@ -181,10 +182,18 @@ public function import(Message $message, Data $data): ?Email
$email->setLinkMultipleColumn('users', 'folderId', $uId, $folderId);
}

if ($this->filtersMatcher->findMatch($email, $filterList, true)) {
$matchedFilter = $this->filtersMatcher->findMatch($email, $filterList, true);

if ($matchedFilter && $matchedFilter->getAction() === EmailFilter::ACTION_SKIP) {
return null;
}

if ($matchedFilter && $matchedFilter->getAction() === EmailFilter::ACTION_MOVE_TO_GROUP_FOLDER) {
$groupEmailFolderId = $matchedFilter->getGroupEmailFolderId();

$email->set('groupFolderId', $groupEmailFolderId);
}

if (
$parser->hasHeader($message, 'message-Id') &&
$parser->getHeader($message, 'message-Id')
Expand Down Expand Up @@ -265,9 +274,17 @@ public function import(Message $message, Data $data): ?Email
if (!$data->fetchOnlyHeader()) {
$inlineAttachmentList = $parser->getInlineAttachmentList($message, $email);

if ($this->filtersMatcher->findMatch($email, $filterList)) {
$matchedFilter = $this->filtersMatcher->findMatch($email, $filterList);

if ($matchedFilter && $matchedFilter->getAction() === EmailFilter::ACTION_SKIP) {
return null;
}

if ($matchedFilter && $matchedFilter->getAction() === EmailFilter::ACTION_MOVE_TO_GROUP_FOLDER) {
$groupEmailFolderId = $matchedFilter->getGroupEmailFolderId();

$email->set('groupFolderId', $groupEmailFolderId);
}
}
else {
$email->set('body', 'Not fetched. The email size exceeds the limit.');
Expand Down
21 changes: 21 additions & 0 deletions application/Espo/Entities/EmailFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class EmailFilter extends \Espo\Core\ORM\Entity

public const ACTION_SKIP = 'Skip';
public const ACTION_MOVE_TO_FOLDER = 'Move to Folder';
public const ACTION_MOVE_TO_GROUP_FOLDER = 'Move to Group Folder';

/**
* @return self::ACTION_*|null
Expand All @@ -49,6 +50,26 @@ public function getEmailFolderId(): ?string
return $this->get('emailFolderId');
}

public function getGroupEmailFolderId(): ?string
{
return $this->get('groupEmailFolderId');
}

public function isGlobal(): bool
{
return (bool) $this->get('isGlobal');
}

public function getParentType(): ?string
{
return $this->get('parentType');
}

public function getParentId(): ?string
{
return $this->get('parentId');
}

public function getFrom(): ?string
{
return $this->get('from');
Expand Down
10 changes: 8 additions & 2 deletions application/Espo/Resources/i18n/en_US/EmailFilter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"bodyContains": "Body Contains",
"action": "Action",
"isGlobal": "Is Global",
"emailFolder": "Folder"
"emailFolder": "Folder",
"groupEmailFolder": "Group Email Folder"
},
"links": {
"emailFolder": "Folder",
"groupEmailFolder": "Group Email Folder"
},
"labels": {
"Create EmailFilter": "Create Email Filter",
Expand All @@ -15,7 +20,8 @@
"options": {
"action": {
"Skip": "Ignore",
"Move to Folder": "Put in Folder"
"Move to Folder": "Put in Folder",
"Move to Group Folder": "Put in Group Folder"
}
},
"tooltips": {
Expand Down
6 changes: 4 additions & 2 deletions application/Espo/Resources/layouts/EmailFilter/detail.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
[
false, {"name": "emailFolder"}
],
[
false, {"name": "groupEmailFolder"}
],
[
{"name": "subject", "fullWidth": true}
],
Expand All @@ -21,5 +24,4 @@
]
]
}

]
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
[
{"name": "emailFolder"}
],
[
{"name": "groupEmailFolder"}
],
[
{"name": "from"}
],
Expand All @@ -27,5 +30,4 @@
]
]
}

]
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"action",
"createdBy",
"createdAt"
]
]
Loading

0 comments on commit 4e31fc8

Please sign in to comment.