Files
emails/api/common/Service/Messages.php
2023-06-12 21:14:07 -04:00

203 lines
7.3 KiB
PHP

<?php
namespace ProVM\Common\Service;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\MailboxInterface;
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\Mailbox\Stateless;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\Message\AttachmentInterface;
use ProVM\Emails\Model\Mailbox;
use ProVM\Emails\Repository\Message;
use Safe\DateTimeImmutable;
class Messages extends Base
{
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, Jobs $jobsService, LoggerInterface $logger)
{
$this->setMailboxes($mailboxes)
->setRepository($repository)
->setRemoteService($remoteService)
->setJobsService($jobsService)
->setLogger($logger);
}
protected Mailboxes $mailboxes;
protected Message $repository;
protected Remote\Messages $remoteService;
protected Jobs $jobsService;
public function getMailboxes(): Mailboxes
{
return $this->mailboxes;
}
public function getRepository(): Message
{
return $this->repository;
}
public function getRemoteService(): Remote\Messages
{
return $this->remoteService;
}
public function getJobsService(): Jobs
{
return $this->jobsService;
}
public function setMailboxes(Mailboxes $mailboxes): Messages
{
$this->mailboxes = $mailboxes;
return $this;
}
public function setRepository(Message $repository): Messages
{
$this->repository = $repository;
return $this;
}
public function setRemoteService(Remote\Messages $service): Messages
{
$this->remoteService = $service;
return $this;
}
public function setJobsService(Jobs $service): Messages
{
$this->jobsService = $service;
return $this;
}
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
{
return $this->getRepository()->fetchByUID($message_uid);
}
public function getRemoteMessage(string $message_uid): MessageInterface
{
$message = $this->getLocalMessage($message_uid);
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($message->getMailbox()->getName());
return $this->getRemoteService()->get($remote_mailbox, $message->getSubject(), $message->getFrom(), $message->getDateTime());
}
public function getAll(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
return $this->getRepository()->fetchByMailbox($mailbox->getId());
}
public function getValid(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
return $this->getRepository()->fetchValidByMailbox($mailbox->getId());
}
public function grab(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
if (!$this->getMailboxes()->validate($mailbox_name)) {
$this->restoreUIDs($mailbox_name);
}
try {
$start = $mailbox->lastPosition() + 1;
} catch (Stateless $e) {
$start = 0;
}
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
$total = $remote_mailbox->count();
$total_amount = $total - $start;
$amount = min(100, $total_amount);
$messages = [];
for ($i = 0; $i < $total_amount; $i += $amount) {
if ($amount + $i > $total_amount) {
$amount = $total_amount - $i;
}
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $start + $i, $amount);
foreach ($remote_messages as $p => $m) {
if ($this->save($mailbox, $m, $p)) {
$messages []= $m->getId();
}
}
}
$this->getMailboxes()->updateState($mailbox, $messages, new DateTimeImmutable());
return $messages;
}
public function restoreUIDs(string $mailbox_name): void
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
$total_amount = $mailbox->lastPosition();
$amount = min(100, $total_amount);
for ($i = 0; $i < $total_amount; $i += $amount) {
if ($amount + $i > $total_amount) {
$amount = $total_amount - $i;
}
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $i, $amount);
foreach ($remote_messages as $m) {
$this->update($mailbox, $m, $i);
}
}
}
public function save(Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
{
$data = [
'mailbox_id' => $mailbox->getId(),
'position' => $position,
'uid' => $remote_message->getId(),
'subject' => $remote_message->getSubject() ?? '',
'from' => $remote_message->getFrom()->getAddress(),
'date_time' => $remote_message->getDate()->format('Y-m-d H:i:s')
];
try {
$message = $this->getRepository()->create($data);
if ($message->getId() === 0) {
if ($remote_message->hasAttachments()) {
$message->doesHaveAttachments();
}
if ($this->validAttachments($remote_message)) {
$message->doesHaveValidAttachments();
}
}
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
$this->getLogger()->error($e);
return false;
}
}
public function update(Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
{
try {
$message = $this->getRepository()->fetchByMailboxSubjectFromAndDate($mailbox->getId(), $remote_message->getSubject(), $remote_message->getFrom()->getAddress(), $remote_message->getDate());
$message->setUID($remote_message->getId());
$message->setPosition($position);
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
return false;
}
}
public function validateAttachment(AttachmentInterface $attachment): bool
{
return str_contains($attachment->getFilename(), '.pdf');
}
public function validAttachments(MessageInterface $remote_message): bool
{
foreach ($remote_message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
return true;
}
}
return false;
}
public function find(string $subject, string $date): array
{
return $this->repository->fetchAllBySubjectAndDate($subject, new DateTimeImmutable($date));
}
public function checkUpdate(): void
{
$registered = $this->getMailboxes()->getRegistered();
foreach ($registered as $mailbox) {
if (!$this->getMailboxes()->isUpdated($mailbox)) {
$this->getJobsService()->queue('messages:grab', [$mailbox->getId()]);
}
}
}
}