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 19, 2022
1 parent f113610 commit 1bfa02a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
]]></description>
<version>1.14.0-alpha.4</version>
<version>1.14.0-alpha.5</version>
<licence>agpl</licence>
<author>Greta Doçi</author>
<author homepage="https://github.com/nextcloud/groupware">Nextcloud Groupware Team</author>
Expand Down
5 changes: 3 additions & 2 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions lib/Service/IMipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 1bfa02a

Please sign in to comment.