Skip to content

Commit

Permalink
Implemented search
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
PeeHaa committed Apr 25, 2018
1 parent c85438a commit b4558a4
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 18 deletions.
11 changes: 11 additions & 0 deletions assets/js/Interface/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import Mail from './NavBar/Mail';
export default class NavBar {
constructor() {
this.mails = {};

document.querySelector('#messages .search input').addEventListener('keyup', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('keypress', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('paste', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('input', this.search.bind(this));
}

addMails(mails) {
Expand Down Expand Up @@ -35,4 +40,10 @@ export default class NavBar {
this.mails[id].deactivate();
});
}

search(e) {
Object.keys(this.mails).forEach((key)=> {
this.mails[key].filter(e.target.value.toLowerCase());
});
}
}
20 changes: 20 additions & 0 deletions assets/js/Interface/NavBar/Mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default class Mail {
this.addSubject(mail.subject);
this.addTimestamp(mail.timestamp);
this.setReadStatus(mail.read);

this.searchableContent = mail.searchableContent;
}

addToDom() {
Expand Down Expand Up @@ -63,4 +65,22 @@ export default class Mail {
markAsRead() {
this.element.classList.remove('new');
}

filter(search) {
if (this.searchableContent.indexOf(search)=== -1) {
this.hide();

return;
}

this.show();
}

hide() {
this.element.classList.add('filtered');
}

show() {
this.element.classList.remove('filtered');
}
}
5 changes: 4 additions & 1 deletion assets/scss/components/messages.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ nav#messages {
}

button {
cursor: pointer;
color: #ffffff;
}
}
Expand All @@ -41,6 +40,10 @@ nav#messages {
color: #ffffff;
}

&.filtered {
display: none;
}

time {
font-size: 11px;
padding: 10px 10px 10px 15px;
Expand Down
32 changes: 32 additions & 0 deletions examples/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace PeeHaa\MailGrab\Examples;

use PHPMailer\PHPMailer\PHPMailer;

require_once __DIR__ . '/../vendor/autoload.php';

$mail = new PHPMailer();

$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 9025;
$mail->SMTPDebug = true;

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->Subject = 'PHPMailer';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->isHTML(true);

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
3 changes: 2 additions & 1 deletion public/css/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion public/css/bundle.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions public/js/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17105,6 +17105,11 @@ var NavBar = function () {
_classCallCheck(this, NavBar);

this.mails = {};

document.querySelector('#messages .search input').addEventListener('keyup', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('keypress', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('paste', this.search.bind(this));
document.querySelector('#messages .search input').addEventListener('input', this.search.bind(this));
}

_createClass(NavBar, [{
Expand Down Expand Up @@ -17149,6 +17154,15 @@ var NavBar = function () {
_this3.mails[id].deactivate();
});
}
}, {
key: 'search',
value: function search(e) {
var _this4 = this;

Object.keys(this.mails).forEach(function (key) {
_this4.mails[key].filter(e.target.value.toLowerCase());
});
}
}]);

return NavBar;
Expand Down Expand Up @@ -17185,6 +17199,8 @@ var Mail = function () {
this.addSubject(mail.subject);
this.addTimestamp(mail.timestamp);
this.setReadStatus(mail.read);

this.searchableContent = mail.searchableContent;
}

_createClass(Mail, [{
Expand Down Expand Up @@ -17249,6 +17265,27 @@ var Mail = function () {
value: function markAsRead() {
this.element.classList.remove('new');
}
}, {
key: 'filter',
value: function filter(search) {
if (this.searchableContent.indexOf(search) === -1) {
this.hide();

return;
}

this.show();
}
}, {
key: 'hide',
value: function hide() {
this.element.classList.add('filtered');
}
}, {
key: 'show',
value: function show() {
this.element.classList.remove('filtered');
}
}]);

return Mail;
Expand Down
2 changes: 1 addition & 1 deletion public/js/bundle.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/bundle.min.js

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/Http/Command/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ private function buildList(): array
/** @var Mail $mail */
foreach ($this->storage as $mail) {
$list[] = [
'id' => $mail->getId(),
'subject' => $mail->getSubject(),
'timestamp' => $mail->getTimestamp()->format(\DateTime::RFC3339_EXTENDED),
'read' => $mail->isRead(),
'project' => $mail->getProject(),
'id' => $mail->getId(),
'subject' => $mail->getSubject(),
'searchableContent' => $mail->getSearchableContent(),
'timestamp' => $mail->getTimestamp()->format(\DateTime::RFC3339_EXTENDED),
'read' => $mail->isRead(),
'project' => $mail->getProject(),
];
}

Expand Down
13 changes: 7 additions & 6 deletions src/Http/Command/NewMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ private function buildList(string $id): array

return [
[
'id' => $mail->getId(),
'subject' => $mail->getSubject(),
'timestamp' => $mail->getTimestamp()->format(\DateTime::RFC3339_EXTENDED),
'read' => $mail->isRead(),
'deleted' => false,
'project' => $mail->getProject(),
'id' => $mail->getId(),
'subject' => $mail->getSubject(),
'searchableContent' => $mail->getSearchableContent(),
'timestamp' => $mail->getTimestamp()->format(\DateTime::RFC3339_EXTENDED),
'read' => $mail->isRead(),
'deleted' => false,
'project' => $mail->getProject(),
],
];
}
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Entity/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,37 @@ public function getSource(): string
return $this->rawMessage;
}

public function getSearchableContent(): string
{
$searchableContent = $this->getSubject();

if ($this->getText()) {
$searchableContent .= ' ' . $this->getText();
}

if ($this->getHtml()) {
$internalErrors = libxml_use_internal_errors(true);
$dom = new \DOMDocument();
$dom->loadHTML($this->getHtml());
libxml_use_internal_errors($internalErrors);

$xpath = new \DOMXPath($dom);
$textNodes = $xpath->query('//*[not(self::script or self::style)]/text()');

/** @var \DOMText $textNode */
foreach ($textNodes as $textNode) {
$searchableContent .= ' ' . $textNode->textContent;
}
}

$searchableContent = trim($searchableContent);
$searchableContent = str_replace(["\r", "\n"], ' ', $searchableContent);
$searchableContent = mb_strtolower($searchableContent);
$searchableContent = preg_replace('~\s+~', ' ', $searchableContent);

return $searchableContent;
}

public function getAttachments(): array
{
$attachments = [];
Expand Down
13 changes: 13 additions & 0 deletions tests/Data/raw-message-for-search-without-html.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Date: Wed, 25 Apr 2018 11:17:15 +0000
To: Joe User <joe@example.net>, ellen@example.com
From: Mailer <from@example.com>
Cc: cc@example.com
Reply-To: Information <info@example.com>
Subject: PHPMailer
Message-ID: <OVhuBTY99dkKuu9hS0cwCe61FCanC1muNDwFvjlloo@MSIGL72M7RDX>
X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1

This is the body in plain text for non-HTML mail clients

13 changes: 13 additions & 0 deletions tests/Data/raw-message-for-search-without-text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Date: Wed, 25 Apr 2018 11:19:17 +0000
To: Joe User <joe@example.net>, ellen@example.com
From: Mailer <from@example.com>
Cc: cc@example.com
Reply-To: Information <info@example.com>
Subject: PHPMailer
Message-ID: <hdAeYRzbi244B3xgZoWqHfUX4j4ASbsXy64Me59GMQw@MSIGL72M7RDX>
X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/html; charset=iso-8859-1

This is the HTML message body <b>in bold!</b>

27 changes: 27 additions & 0 deletions tests/Data/raw-message-for-search.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Date: Wed, 25 Apr 2018 11:03:06 +0000
To: Joe User <joe@example.net>, ellen@example.com
From: Mailer <from@example.com>
Cc: cc@example.com
Reply-To: Information <info@example.com>
Subject: PHPMailer
Message-ID: <nCtDvEpXde2mSWDlbWR7CesiB3a9w8iG45OTrAWdLMo@MSIGL72M7RDX>
X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_nCtDvEpXde2mSWDlbWR7CesiB3a9w8iG45OTrAWdLMo"
Content-Transfer-Encoding: 8bit

This is a multi-part message in MIME format.
--b1_nCtDvEpXde2mSWDlbWR7CesiB3a9w8iG45OTrAWdLMo
Content-Type: text/plain; charset=us-ascii

This is the body in plain text for non-HTML mail clients

--b1_nCtDvEpXde2mSWDlbWR7CesiB3a9w8iG45OTrAWdLMo
Content-Type: text/html; charset=us-ascii

This is the HTML message body <b>in bold!</b>


--b1_nCtDvEpXde2mSWDlbWR7CesiB3a9w8iG45OTrAWdLMo--

2 changes: 1 addition & 1 deletion tests/Unit/Http/Command/InitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ public function testExecuteReturnsResponse()

$result = wait($init->execute($this->inputMock));

$this->assertSame('{"success":true,"payload":{"command":"init","mails":[{"id":"ID","subject":"SUBJECT","timestamp":"TIMESTAMP","read":false,"project":"PROJECT"}],"config":{"hostname":"HOSTNAME","smtpport":"SMTPPORT"}}}', (string) $result);
$this->assertSame('{"success":true,"payload":{"command":"init","mails":[{"id":"ID","subject":"SUBJECT","searchableContent":"","timestamp":"TIMESTAMP","read":false,"project":"PROJECT"}],"config":{"hostname":"HOSTNAME","smtpport":"SMTPPORT"}}}', (string) $result);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Http/Command/NewMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ public function testExecuteReturnsResponseWhenMailIsAvailable()
$result = wait($newMail->execute($this->inputMock));

$this->assertInstanceOf(Success::class, $result);
$this->assertSame('{"success":true,"payload":{"command":"newMail","mails":[{"id":"ID","subject":"SUBJECT","timestamp":"TIMESTAMP","read":false,"deleted":false,"project":"PROJECT"}]}}', (string) $result);
$this->assertSame('{"success":true,"payload":{"command":"newMail","mails":[{"id":"ID","subject":"SUBJECT","searchableContent":"","timestamp":"TIMESTAMP","read":false,"deleted":false,"project":"PROJECT"}]}}', (string) $result);
}
}
Loading

0 comments on commit b4558a4

Please sign in to comment.