Skip to content

Commit

Permalink
fixup! Add imip processing
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Jul 12, 2022
1 parent 0ae76a9 commit 51f1c8f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
5 changes: 2 additions & 3 deletions lib/BackgroundJob/IMipMessageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Service\IMipService;
use OCA\Mail\Service\OutboxService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use function defined;
Expand All @@ -41,8 +40,8 @@ public function __construct(ITimeFactory $time,
IMipService $iMipService) {
parent::__construct($time);

// Run once per five minutes
$this->setInterval(5 * 60);
// Run once per hour
$this->setInterval(60*60);
/**
* @todo remove checks with 24+
*/
Expand Down
6 changes: 6 additions & 0 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,11 @@ public function resetInReplyTo(): int {
return $update->execute();
}

/**
* Get all iMIP messages from the last two weeks
* that haven't been processed yet
* @return Message[]
*/
public function findIMipMessages(): array {
$qb = $this->db->getQueryBuilder();

Expand All @@ -1234,6 +1239,7 @@ public function findIMipMessages(): array {
$qb->expr()->eq('imip_message', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
$qb->expr()->eq('imip_processed', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
$qb->expr()->eq('imip_error', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
$qb->expr()->gt('sent_at', ($this->timeFactory->getTime() - 60*60*24*14), $qb->createNamedParameter(false, IQueryBuilder::PARAM_INT)),
);

return $this->findEntities($select);
Expand Down
56 changes: 32 additions & 24 deletions lib/Service/IMipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,80 @@
namespace OCA\Mail\Service;

use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\MessageMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Calendar\IManager;

class IMipService {

private MessageMapper $mapper;
private MessageMapper $messageMapper;
private MailboxMapper $mailboxMapper;
private AccountService $accountService;
private MailManager $mailManager;

public function __construct(
MessageMapper $mapper,
MessageMapper $messageMapper,
MailboxMapper $mailboxMapper,
AccountService $accountService,
MailManager $mailManager,
IManager $manager,
) {
$this->mapper = $mapper;
$this->messageMapper = $messageMapper;
$this->mailboxMapper = $mailboxMapper;
$this->accountService = $accountService;
$this->mailManager = $mailManager;
}

public function process() {
$messages = $this->mapper->findIMipMessages();
$messages = $this->messageMapper->findIMipMessages();

$accountIds = array_unique(array_map(function ($message) {
return $message->getAccountId();
$mailboxIds = array_unique(array_map(function (Message $message) {
return $message->getMailboxId();
}, $messages));

$accounts = array_combine($accountIds, array_map(function ($accountId) {
$mailboxes = array_combine($mailboxIds, array_map(function (int $mailboxId) {
try {
return $this->accountService->findById($accountId);
$mailbox = $this->mailboxMapper->findById($mailboxId);
if ($mailbox->getSpecialUse() === '["sent"]' || $mailbox->getSpecialUse() === '["drafts"]') {
return null;
}
} catch (DoesNotExistException $e) {
// The message belongs to a deleted account
return null;
}
}, $accountIds));
}, $mailboxIds));

$mailboxIds = array_unique(array_map(function ($message) {
return $message->mailboxId();
}, $messages));
$accountIds = array_unique(array_map(function (Mailbox $mailbox) {
return $mailbox->getAccountId();
}, $mailboxes));

$mailboxes = array_combine($mailboxIds, array_map(function ($mailboxId) {
$accounts = array_combine($accountIds, array_map(function (int $accountId) {
try {
return $this->mailboxMapper->findById($mailboxId);
return $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
// The message belongs to a deleted account
return null;
}
}, $mailboxIds));
}, $accountIds));


foreach ($messages as $message) {

foreach ($messages as $message) {
$imapMessage = $this->mailManager->getImapMessage($accounts[$message->getAccountId()], $mailboxes[$message->getMailboxId()], $message->getUid());

if(empty($imapMessage->scheduling)) {
// No scheduling info, maybe the DB is wrong
$message->setImipError(true);
$this->messageMapper->update($message);
continue;
}
if($imapMessage->scheduling['method'] === 'REPLY') {

if($imapMessage->scheduling['method'] === 'REPLY' || $imapMessage->scheduling['method'] == 'CANCEL') {
// automatically process this
}
if($imapMessage->scheduling['method'] === 'REQUEST' ) {
// should we write requests to the calendar too??
// would allow answering from our calendar
// we could update the partstat from mail too with the ICAL that is sent when clicking accept or decline

$message->setImipProcessed(true);
$this->messageMapper->update($message);
}
}

Expand Down

0 comments on commit 51f1c8f

Please sign in to comment.