lib/Db/Message.php | 6 +++++- lib/Db/MessageMapper.php | 2 ++ lib/IMAP/MessageMapper.php | 6 ++++-- lib/IMAP/Sync/SimpleMailboxSync.php | 17 +++++++++++++++-- lib/Migration/Version1020Date20191002091035.php | 4 ++++ lib/Model/IMAPMessage.php | 3 ++- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/Db/Message.php b/lib/Db/Message.php index 9ed4391b..c2878433 100644 --- a/lib/Db/Message.php +++ b/lib/Db/Message.php @@ -55,6 +55,8 @@ use function json_encode; * @method bool getFlagJunk() * @method void setFlagNotjunk(bool $notjunk) * @method bool getFlagNotjunk() + * @method void setFlagAttachments(bool $hasAttachments) + * @method bool getFlagAttachments() * @method void setUpdatedAt(int $time) * @method int getUpdatedAt() */ @@ -73,6 +75,7 @@ class Message extends Entity implements JsonSerializable { protected $flagForwarded; protected $flagJunk; protected $flagNotjunk; + protected $flagAttachments; protected $updatedAt; /** @var AddressList */ @@ -103,6 +106,7 @@ class Message extends Entity implements JsonSerializable { $this->addType('flagForwarded', 'bool'); $this->addType('flagJunk', 'bool'); $this->addType('flagNotjunk', 'bool'); + $this->addType('flagAttachments', 'bool'); $this->addType('updatedAt', 'integer'); } @@ -174,7 +178,7 @@ class Message extends Entity implements JsonSerializable { 'deleted' => $this->getFlagDeleted(), 'draft' => $this->getFlagDraft(), 'forwarded' => $this->getFlagForwarded(), - 'hasAttachments' => false, // TODO + 'hasAttachments' => $this->getFlagAttachments(), ], 'from' => $this->getFrom()->jsonSerialize(), 'to' => $this->getTo()->jsonSerialize(), diff --git a/lib/Db/MessageMapper.php b/lib/Db/MessageMapper.php index c3cb8b42..ca63bf9a 100644 --- a/lib/Db/MessageMapper.php +++ b/lib/Db/MessageMapper.php @@ -80,6 +80,7 @@ class MessageMapper extends QBMapper { $qb1->setValue('flag_forwarded', $qb1->createParameter('flag_forwarded')); $qb1->setValue('flag_junk', $qb1->createParameter('flag_junk')); $qb1->setValue('flag_notjunk', $qb1->createParameter('flag_notjunk')); + $qb1->setValue('flag_attachments', $qb1->createParameter('flag_attachments')); $qb2 = $this->db->getQueryBuilder(); $qb2->insert('mail_recipients') @@ -102,6 +103,7 @@ class MessageMapper extends QBMapper { $qb1->setParameter('flag_forwarded', $message->getFlagForwarded(), IQueryBuilder::PARAM_BOOL); $qb1->setParameter('flag_junk', $message->getFlagJunk(), IQueryBuilder::PARAM_BOOL); $qb1->setParameter('flag_notjunk', $message->getFlagNotjunk(), IQueryBuilder::PARAM_BOOL); + $qb1->setParameter('flag_attachments', $message->getFlagAttachments(), IQueryBuilder::PARAM_BOOL); $qb1->execute(); diff --git a/lib/IMAP/MessageMapper.php b/lib/IMAP/MessageMapper.php index d0366912..dcebcb79 100644 --- a/lib/IMAP/MessageMapper.php +++ b/lib/IMAP/MessageMapper.php @@ -81,7 +81,7 @@ class MessageMapper { $client, $mailbox->getMailbox(), array_map( - function(Horde_Imap_Client_Data_Fetch $data) { + function (Horde_Imap_Client_Data_Fetch $data) { return $data->getUid(); }, iterator_to_array($client->fetch( @@ -100,11 +100,13 @@ class MessageMapper { public function findByIds(Horde_Imap_Client_Base $client, string $mailbox, array $ids, - bool $loadBody = false): array { + bool $loadBody = false, + bool $loadStructure = false): array { $query = new Horde_Imap_Client_Fetch_Query(); $query->envelope(); $query->flags(); $query->uid(); + $query->structure(); // For attachments $query->imapDate(); $fetchResults = iterator_to_array($client->fetch($mailbox, $query, [ diff --git a/lib/IMAP/Sync/SimpleMailboxSync.php b/lib/IMAP/Sync/SimpleMailboxSync.php index 919ea736..1a3177f3 100644 --- a/lib/IMAP/Sync/SimpleMailboxSync.php +++ b/lib/IMAP/Sync/SimpleMailboxSync.php @@ -45,28 +45,41 @@ class SimpleMailboxSync implements ISyncStrategy { * @param Horde_Imap_Client_Base $imapClient * @param Request $syncRequest * @param Horde_Imap_Client_Data_Sync $hordeSync + * * @return IMAPMessage[] */ public function getNewMessages(Horde_Imap_Client_Base $imapClient, Request $syncRequest, Horde_Imap_Client_Data_Sync $hordeSync): array { - return $this->messageMapper->findByIds($imapClient, $syncRequest->getMailbox(), $hordeSync->newmsgsuids->ids); + return $this->messageMapper->findByIds( + $imapClient, + $syncRequest->getMailbox(), + $hordeSync->newmsgsuids->ids, + false, + true + ); } /** * @param Horde_Imap_Client_Base $imapClient * @param Request $syncRequest * @param Horde_Imap_Client_Data_Sync $hordeSync + * * @return IMAPMessage[] */ public function getChangedMessages(Horde_Imap_Client_Base $imapClient, Request $syncRequest, Horde_Imap_Client_Data_Sync $hordeSync): array { - return $this->messageMapper->findByIds($imapClient, $syncRequest->getMailbox(), $hordeSync->flagsuids->ids); + return $this->messageMapper->findByIds( + $imapClient, + $syncRequest->getMailbox(), + $hordeSync->flagsuids->ids + ); } /** * @param Horde_Imap_Client_Base $imapClient * @param Request $syncRequest * @param Horde_Imap_Client_Data_Sync $hordeSync + * * @return IMAPMessage[] */ public function getVanishedMessageUids(Horde_Imap_Client_Base $imapClient, diff --git a/lib/Migration/Version1020Date20191002091035.php b/lib/Migration/Version1020Date20191002091035.php index d0cb3b05..dc5922d4 100644 --- a/lib/Migration/Version1020Date20191002091035.php +++ b/lib/Migration/Version1020Date20191002091035.php @@ -87,6 +87,10 @@ class Version1020Date20191002091035 extends SimpleMigrationStep { 'notnull' => true, 'default' => false, ]); + $messagesTable->addColumn('flag_attachments', 'boolean', [ + 'notnull' => true, + 'default' => false, + ]); $messagesTable->addColumn('updated_at', 'integer', [ 'notnull' => false, 'length' => 4, diff --git a/lib/Model/IMAPMessage.php b/lib/Model/IMAPMessage.php index be30a78a..531e15e0 100644 --- a/lib/Model/IMAPMessage.php +++ b/lib/Model/IMAPMessage.php @@ -270,7 +270,7 @@ class IMAPMessage implements IMessage, JsonSerializable { * * @return bool */ - private function hasAttachments($part) { + private function hasAttachments($part): bool { foreach ($part->getParts() as $p) { /** @var Horde_Mime_Part $p */ $filename = $p->getName(); @@ -619,6 +619,7 @@ class IMAPMessage implements IMessage, JsonSerializable { $msg->setFlagForwarded(in_array(Horde_Imap_Client::FLAG_FORWARDED, $flags, true)); $msg->setFlagJunk(in_array(Horde_Imap_Client::FLAG_JUNK, $flags, true)); $msg->setFlagNotjunk(in_array(Horde_Imap_Client::FLAG_NOTJUNK, $flags, true)); + $msg->setFlagAttachments($this->hasAttachments($this->fetch->getStructure())); return $msg; }