Skip to content

Commit

Permalink
fixup! feat: fiter messages by mention
Browse files Browse the repository at this point in the history
Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
  • Loading branch information
hamza221 committed Oct 10, 2024
1 parent 6dd0685 commit 1393dbe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
22 changes: 11 additions & 11 deletions lib/IMAP/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MessageMapper {

public function __construct(LoggerInterface $logger,
SmimeService $smimeService,
ImapMessageFetcherFactory $imapMessageFactory) {
ImapMessageFetcherFactory $imapMessageFactory,) {
$this->logger = $logger;
$this->smimeService = $smimeService;
$this->imapMessageFactory = $imapMessageFactory;
Expand Down Expand Up @@ -858,7 +858,8 @@ private function buildAttachmentsPartsQuery(Horde_Mime_Part $structure, array $a
*/
public function getBodyStructureData(Horde_Imap_Client_Socket $client,
string $mailbox,
array $uids): array {
array $uids,
string $emailAddress): array {
$structureQuery = new Horde_Imap_Client_Fetch_Query();
$structureQuery->structure();
$structureQuery->headerText([
Expand All @@ -870,8 +871,7 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,
$structures = $client->fetch($mailbox, $structureQuery, [
'ids' => new Horde_Imap_Client_Ids($uids),
]);

return array_map(function (Horde_Imap_Client_Data_Fetch $fetchData) use ($mailbox, $client) {
return array_map(function (Horde_Imap_Client_Data_Fetch $fetchData) use ($mailbox, $client,$emailAddress ) {
$hasAttachments = false;
$text = '';
$isImipMessage = false;
Expand Down Expand Up @@ -939,12 +939,14 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,
$structure->setContents($htmlBody);
$htmlBody = $structure->getContents();
}
$mentionsUser = $this->checkLinks($htmlBody,$emailAddress);
$html = new Html2Text($htmlBody, ['do_links' => 'none','alt_image' => 'hide']);
return new MessageStructureData(
$hasAttachments,
trim($html->getText()),
$isImipMessage,
$isEncrypted,
$mentionsUser,
);
}
$textBody = $part->getBodyPart($textBodyId);
Expand All @@ -966,20 +968,18 @@ public function getBodyStructureData(Horde_Imap_Client_Socket $client,
return new MessageStructureData($hasAttachments, $text, $isImipMessage, $isEncrypted);
}, iterator_to_array($structures->getIterator()));
}

private function extractLinks(string $body) : array {
$results = [];
private function checkLinks(string $body, string $mailAddress) : bool {
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($body);
libxml_use_internal_errors();
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$href = $anchor->getAttribute('href');
$results[] = [
'href' => $href,
];
if($href === 'mailto:'.$mailAddress){
return true;
}
}
return $results;
return false;
}
}
9 changes: 8 additions & 1 deletion lib/IMAP/MessageStructureData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ class MessageStructureData {
private $isImipMessage;

private bool $isEncrypted;
private bool $mentionsMe;

public function __construct(bool $hasAttachments,
string $previewText,
bool $isImipMessage,
bool $isEncrypted) {
bool $isEncrypted,
?bool $mentionsMe = false) {
$this->hasAttachments = $hasAttachments;
$this->previewText = $previewText;
$this->isImipMessage = $isImipMessage;
$this->isEncrypted = $isEncrypted;
$this->mentionsMe = $mentionsMe;
}

public function hasAttachments(): bool {
Expand All @@ -46,4 +49,8 @@ public function isImipMessage(): bool {
public function isEncrypted(): bool {
return $this->isEncrypted;
}

public function getMentionsMe(): bool {
return $this->mentionsMe;
}
}
4 changes: 3 additions & 1 deletion lib/IMAP/PreviewEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function process(Account $account, Mailbox $mailbox, array $messages): ar
$data = $this->imapMapper->getBodyStructureData(
$client,
$mailbox->getName(),
$needAnalyze
$needAnalyze,
$account->getEMailAddress()
);
} catch (Horde_Imap_Client_Exception $e) {
// Ignore for now, but log
Expand All @@ -94,6 +95,7 @@ public function process(Account $account, Mailbox $mailbox, array $messages): ar
$message->setStructureAnalyzed(true);
$message->setImipMessage($structureData->isImipMessage());
$message->setEncrypted($structureData->isEncrypted());
$message->setMentionsMe($structureData->getMentionsMe());

return $message;
}, $messages));
Expand Down

0 comments on commit 1393dbe

Please sign in to comment.