added HasNewMessagesController

This commit is contained in:
Synox 2018-06-14 18:53:11 +02:00 committed by Aravindo Wingeier
parent 1aa35ba523
commit cac61f6ede
3 changed files with 41 additions and 0 deletions

View file

@ -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;

View file

@ -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']);

View file

@ -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);
}
}