From 1bfa02a96e6fe6cbb4ae324c2053c9d98c4f0111 Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Tue, 19 Jul 2022 23:46:57 +0200 Subject: [PATCH] fixup! Add imip processing --- appinfo/info.xml | 2 +- lib/Db/MessageMapper.php | 5 +++-- lib/Service/IMipService.php | 16 ++++++++-------- lib/Service/MailManager.php | 23 +++++++++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 2be5fadc8d..5001ab9e78 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -12,7 +12,7 @@ - **🙈 We’re not reinventing the wheel!** Based on the great [Horde](https://horde.org) libraries. - **📬 Want to host your own mail server?** We do not have to reimplement this as you could set up [Mail-in-a-Box](https://mailinabox.email)! ]]> - 1.14.0-alpha.4 + 1.14.0-alpha.5 agpl Greta Doçi Nextcloud Groupware Team diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index b182ada9f4..e29d4aeaff 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -1232,15 +1232,16 @@ public function resetInReplyTo(): int { * @return Message[] */ public function findIMipMessages(): array { + $time = $this->timeFactory->getTime() - 60 * 60 * 24 * 14; $qb = $this->db->getQueryBuilder(); $select = $qb->select('*') ->from($this->getTableName()) - ->andWhere( + ->where( $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)), + $qb->expr()->gt('sent_at', $qb->createNamedParameter($time, IQueryBuilder::PARAM_INT)), ); return $this->findEntities($select); diff --git a/lib/Service/IMipService.php b/lib/Service/IMipService.php index 59f46ed9d6..b25032e3db 100644 --- a/lib/Service/IMipService.php +++ b/lib/Service/IMipService.php @@ -60,7 +60,7 @@ public function __construct( $this->logger = $logger; } - public function process() { + public function process(): void { $messages = $this->messageMapper->findIMipMessages(); // Collect all mailboxes in memory @@ -106,28 +106,28 @@ public function process() { try { - $imapMessage = $this->mailManager->getImapMessage($account, $mailbox, $message->getUid()); + $imapMessage = $this->mailManager->getImapMessageForScheduleProcessing($account, $mailbox, $message->getUid()); } catch (ServiceException $e) { $message->setImipError(true); $processedMessages[$account->getId()][] = $message; continue; } - if (empty($imapMessage->scheduling)) { + if (empty($imapMessage->scheduling[0])) { // No scheduling info, maybe the DB is wrong $message->setImipError(true); $processedMessages[$account->getId()][] = $message; continue; } - $principalUri = ''; + $principalUri = 'principals/users/' . $account->getUserId(); $sender = $imapMessage->getFrom()->first()->getEmail(); $recipient = $account->getEmail(); $processed = false; - if ($imapMessage->scheduling['method'] === 'REPLY') { - $processed = $this->calendarManager->handleIMipReply($principalUri, $sender, $recipient, $imapMessage->scheduling['content']); + if ($imapMessage->scheduling[0]['method'] === 'REPLY') { + $processed = $this->calendarManager->handleIMipReply($principalUri, $sender, $recipient, $imapMessage->scheduling[0]['contents']); } elseif ($imapMessage->scheduling['method'] === 'CANCEL') { - $processed = $this->calendarManager->handleIMipCancel($principalUri, $sender, $recipient, $imapMessage->scheduling['content']); + $processed = $this->calendarManager->handleIMipCancel($principalUri, $sender, $recipient, $imapMessage->scheduling[0]['contents']); } $message->setImipProcessed($processed); $message->setImipError(!$processed); @@ -139,7 +139,7 @@ public function process() { continue; } try { - $this->messageMapper->updateBulk($accounts[$accountId], false, $processedMessages[$accountId]); + $this->messageMapper->updateBulk($accounts[$accountId], false, ...$processedMessages[$accountId]); } catch (\Throwable $e) { $this->logger->error('Could not update iMip messages for account ' . $accountId, ['exception' => $e]); } diff --git a/lib/Service/MailManager.php b/lib/Service/MailManager.php index a769013688..c5fc02b793 100644 --- a/lib/Service/MailManager.php +++ b/lib/Service/MailManager.php @@ -194,6 +194,29 @@ public function getImapMessage(Account $account, } } + public function getImapMessageForScheduleProcessing(Account $account, + Mailbox $mailbox, + int $uid, + bool $loadBody = false): IMAPMessage { + $client = $this->imapClientFactory->getClient($account); + try { + return $this->imapMessageMapper->find( + $client, + $mailbox->getName(), + $uid, + true + ); + } catch (Horde_Imap_Client_Exception|DoesNotExistException $e) { + throw new ServiceException( + 'Could not load message', + (int)$e->getCode(), + $e + ); + } finally { + $client->logout(); + } + } + public function getThread(Account $account, string $threadRootId): array { return $this->dbMessageMapper->findThread($account, $threadRootId); }