Skip to content

Commit 185eb96

Browse files
committed
enh: provide attachement names for messages
Signed-off-by: Hamza <[email protected]>
1 parent 6a1679a commit 185eb96

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

lib/Contracts/IAttachmentService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ public function getAttachment(string $userId, int $id): array;
3434
* @param int $id
3535
*/
3636
public function deleteAttachment(string $userId, int $id);
37+
3738
}

lib/Db/Message.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class Message extends Entity implements JsonSerializable {
142142

143143
/** @var bool */
144144
private $fetchAvatarFromClient = false;
145+
/** @var array */
146+
private $attachments = [];
145147

146148
public function __construct() {
147149
$this->from = new AddressList([]);
@@ -312,6 +314,14 @@ public function getAvatar(): ?Avatar {
312314
return $this->avatar;
313315
}
314316

317+
public function setAttachments(array $attachments): void {
318+
$this->attachments = $attachments;
319+
}
320+
321+
public function getAttachments(): array {
322+
return $this->attachments;
323+
}
324+
315325
#[\Override]
316326
#[ReturnTypeWillChange]
317327
public function jsonSerialize() {
@@ -359,6 +369,7 @@ static function (Tag $tag) {
359369
'mentionsMe' => $this->getMentionsMe(),
360370
'avatar' => $this->avatar?->jsonSerialize(),
361371
'fetchAvatarFromClient' => $this->fetchAvatarFromClient,
372+
'attachments' => $this->getAttachments(),
362373
];
363374
}
364375
}

lib/IMAP/PreviewEnhancer.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Mail\Db\Message;
1616
use OCA\Mail\Db\MessageMapper as DbMapper;
1717
use OCA\Mail\IMAP\MessageMapper as ImapMapper;
18+
use OCA\Mail\Service\Attachment\AttachmentService;
1819
use OCA\Mail\Service\Avatar\Avatar;
1920
use OCA\Mail\Service\AvatarService;
2021
use Psr\Log\LoggerInterface;
@@ -39,11 +40,14 @@ class PreviewEnhancer {
3940
/** @var AvatarService */
4041
private $avatarService;
4142

42-
public function __construct(IMAPClientFactory $clientFactory,
43+
public function __construct(
44+
IMAPClientFactory $clientFactory,
4345
ImapMapper $imapMapper,
4446
DbMapper $dbMapper,
4547
LoggerInterface $logger,
46-
AvatarService $avatarService) {
48+
AvatarService $avatarService,
49+
private AttachmentService $attachmentService,
50+
) {
4751
$this->clientFactory = $clientFactory;
4852
$this->imapMapper = $imapMapper;
4953
$this->mapper = $dbMapper;
@@ -65,6 +69,12 @@ public function process(Account $account, Mailbox $mailbox, array $messages, boo
6569

6670
return array_merge($carry, [$message->getUid()]);
6771
}, []);
72+
$client = $this->clientFactory->getClient($account);
73+
74+
foreach ($messages as $message) {
75+
$attachments = $this->attachmentService->getAttachmentNames($account, $mailbox, $message, $client);
76+
$message->setAttachments($attachments);
77+
}
6878

6979
if ($preLoadAvatars) {
7080
foreach ($messages as $message) {
@@ -87,7 +97,7 @@ public function process(Account $account, Mailbox $mailbox, array $messages, boo
8797
return $messages;
8898
}
8999

90-
$client = $this->clientFactory->getClient($account);
100+
91101
try {
92102
$data = $this->imapMapper->getBodyStructureData(
93103
$client,

lib/Model/IMAPMessage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public function getSentDate(): Horde_Imap_Client_DateTime {
283283
return $this->imapDate;
284284
}
285285

286+
286287
/**
287288
* @param int $id
288289
*
@@ -384,7 +385,7 @@ public function setContent(string $content) {
384385
*/
385386
#[\Override]
386387
public function getAttachments(): array {
387-
throw new Exception('not implemented');
388+
return $this->attachments;
388389
}
389390

390391
/**

lib/Service/Attachment/AttachmentService.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818
use OCA\Mail\Db\LocalAttachment;
1919
use OCA\Mail\Db\LocalAttachmentMapper;
2020
use OCA\Mail\Db\LocalMessage;
21+
use OCA\Mail\Db\Mailbox;
22+
use OCA\Mail\Db\Message;
2123
use OCA\Mail\Exception\AttachmentNotFoundException;
24+
use OCA\Mail\Exception\ServiceException;
2225
use OCA\Mail\Exception\UploadException;
2326
use OCA\Mail\IMAP\MessageMapper;
2427
use OCP\AppFramework\Db\DoesNotExistException;
2528
use OCP\Files\File;
2629
use OCP\Files\Folder;
2730
use OCP\Files\NotFoundException;
2831
use OCP\Files\NotPermittedException;
32+
use OCP\ICacheFactory;
2933
use Psr\Log\LoggerInterface;
3034

3135
class AttachmentService implements IAttachmentService {
@@ -51,6 +55,10 @@ class AttachmentService implements IAttachmentService {
5155
* @var LoggerInterface
5256
*/
5357
private $logger;
58+
/**
59+
* @var ICache
60+
*/
61+
private $cache;
5462

5563
/**
5664
* @param Folder $userFolder
@@ -60,13 +68,15 @@ public function __construct($userFolder,
6068
AttachmentStorage $storage,
6169
IMailManager $mailManager,
6270
MessageMapper $imapMessageMapper,
71+
ICacheFactory $cacheFactory,
6372
LoggerInterface $logger) {
6473
$this->mapper = $mapper;
6574
$this->storage = $storage;
6675
$this->mailManager = $mailManager;
6776
$this->messageMapper = $imapMessageMapper;
6877
$this->userFolder = $userFolder;
6978
$this->logger = $logger;
79+
$this->cache = $cacheFactory->createLocal('mail.attachment_names');
7080
}
7181

7282
/**
@@ -249,6 +259,31 @@ public function handleAttachments(Account $account, array $attachments, \Horde_I
249259
return array_values(array_filter($attachmentIds));
250260
}
251261

262+
public function getAttachmentNames(Account $account, Mailbox $mailbox, Message $message, \Horde_Imap_Client_Socket $client): array {
263+
$attachments = [];
264+
$cached = $this->cache->get($message->getUid());
265+
if ($cached) {
266+
return $cached;
267+
}
268+
try {
269+
$imapMessage = $this->mailManager->getImapMessage(
270+
$client,
271+
$account,
272+
$mailbox,
273+
$message->getUid(),
274+
true
275+
);
276+
$attachments = $imapMessage->getAttachments();
277+
} catch (ServiceException $e) {
278+
$this->logger->error('Could not get attachment names', ['exception' => $e, 'messageId' => $message->getUid()]);
279+
}
280+
$result = array_map(static function (array $attachment) {
281+
return $attachment['fileName'];
282+
}, $attachments);
283+
$this->cache->set($message->getUid(), $result);
284+
return $result;
285+
}
286+
252287
/**
253288
* Add a message as attachment
254289
*

0 commit comments

Comments
 (0)