diff --git a/src/controller.php b/src/controller.php index 1cab399..570cdcd 100644 --- a/src/controller.php +++ b/src/controller.php @@ -100,6 +100,35 @@ class DeleteEmailController extends Controller { } } +class HasNewMessagesController extends Controller { + private $email_ids; + private $address; + private $config_domains; + private $config_blocked_usernames; + + public function __construct($email_ids, $address, $config_domains, array $config_blocked_usernames) { + $this->email_ids = $email_ids; + $this->address = $address; + $this->config_domains = $config_domains; + $this->config_blocked_usernames = $config_blocked_usernames; + } + + function invoke(ImapClient $imapClient) { + $user = User::parseDomain($this->address, $this->config_blocked_usernames); + $this->validate_user($user, $this->config_domains); + $emails = $imapClient->get_emails($user); + + $knownMailIds = explode('|', $this->email_ids); + + $newMailIds = array_map(function ($mail) { + return $mail->id; + }, $emails); + + $onlyNewMailIds = array_diff($newMailIds, $knownMailIds); + $this->viewHandler->new_mail_counter_json(count($onlyNewMailIds)); + } +} + class RedirectToRandomAddressController extends Controller { private $config_domains; diff --git a/src/router.php b/src/router.php index 6896e5b..2b91c50 100644 --- a/src/router.php +++ b/src/router.php @@ -37,6 +37,11 @@ class Router { && isset($this->get_vars['address'])) { return new DeleteEmailController($this->get_vars['email_id'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']); + } elseif ($this->action === "has_new_messages" + && isset($this->get_vars['email_ids']) + && isset($this->get_vars['address'])) { + return new HasNewMessagesController($this->get_vars['email_ids'], $this->get_vars['address'], $this->config['domains'], $this->config['blocked_usernames']); + } elseif ($this->action === 'random') { return new RedirectToRandomAddressController($this->config['domains']); diff --git a/src/view.php b/src/view.php index f1c8b2f..1219206 100644 --- a/src/view.php +++ b/src/view.php @@ -17,6 +17,8 @@ interface ViewHandler { function downloadEmailAsRfc822($full_email, $filename); function invalid_input($config_domains); + + function new_mail_counter_json($counter); } @@ -49,4 +51,9 @@ class ServerRenderViewHandler implements ViewHandler { $address = User::get_random_address($config_domains); $this->newAddress($address); } + + function new_mail_counter_json($counter) { + header('Content-Type: application/json'); + print json_encode($counter); + } }