Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Contracts/IAttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public function getAttachment(string $userId, int $id): array;
* @param int $id
*/
public function deleteAttachment(string $userId, int $id);

}
2 changes: 1 addition & 1 deletion lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getImapMessage(Horde_Imap_Client_Socket $client,
*
* @return Message[]
*/
public function getThread(Account $account, string $threadRootId): array;
public function getThread(Account $account, string $threadRootId, string $sortOrder = IMailSearch::ORDER_NEWEST_FIRST): array;

/**
* @param Account $sourceAccount
Expand Down
4 changes: 2 additions & 2 deletions lib/Contracts/IMailSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function findMessage(Account $account,
* @param string|null $userId
* @param string|null $view
*
* @return Message[]
* @return Message[][]
*
* @throws ClientException
* @throws ServiceException
Expand All @@ -59,7 +59,7 @@ public function findMessages(Account $account,
/**
* Run a search through all mailboxes of a user.
*
* @return Message[]
* @return Message[][]
*
* @throws ClientException
* @throws ServiceException
Expand Down
11 changes: 11 additions & 0 deletions lib/Db/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class Message extends Entity implements JsonSerializable {

/** @var bool */
private $fetchAvatarFromClient = false;
/** @var array */
private $attachments = [];

public function __construct() {
$this->from = new AddressList([]);
Expand Down Expand Up @@ -312,6 +314,14 @@ public function getAvatar(): ?Avatar {
return $this->avatar;
}

public function setAttachments(array $attachments): void {
$this->attachments = $attachments;
}

public function getAttachments(): array {
return $this->attachments;
}

#[\Override]
#[ReturnTypeWillChange]
public function jsonSerialize() {
Expand Down Expand Up @@ -359,6 +369,7 @@ static function (Tag $tag) {
'mentionsMe' => $this->getMentionsMe(),
'avatar' => $this->avatar?->jsonSerialize(),
'fetchAvatarFromClient' => $this->fetchAvatarFromClient,
'attachments' => $this->getAttachments(),
];
}
}
96 changes: 92 additions & 4 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,11 @@
/**
* @param Account $account
* @param string $threadRootId
* @param string $sortOrder
*
* @return Message[]
*/
public function findThread(Account $account, string $threadRootId): array {
public function findThread(Account $account, string $threadRootId, string $sortOrder): array {
$qb = $this->db->getQueryBuilder();
$qb->select('messages.*')
->from($this->getTableName(), 'messages')
Expand All @@ -768,7 +769,7 @@
$qb->expr()->eq('mailboxes.account_id', $qb->createNamedParameter($account->getId(), IQueryBuilder::PARAM_INT)),
$qb->expr()->eq('messages.thread_root_id', $qb->createNamedParameter($threadRootId, IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR)
)
->orderBy('messages.sent_at', 'desc');
->orderBy('messages.sent_at', $sortOrder);

return $this->findRelatedData($this->findEntities($qb), $account->getUserId());
}
Expand Down Expand Up @@ -1273,10 +1274,11 @@
* @param Mailbox $mailbox
* @param string $userId
* @param int[] $ids
* @param string $sortOrder
*
* @return Message[]
*/
public function findByMailboxAndIds(Mailbox $mailbox, string $userId, array $ids): array {
public function findByMailboxAndIds(Mailbox $mailbox, string $userId, array $ids, string $sortOrder): array {
if ($ids === []) {
return [];
}
Expand All @@ -1288,7 +1290,7 @@
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId()), IQueryBuilder::PARAM_INT),
$qb->expr()->in('id', $qb->createParameter('ids'))
)
->orderBy('sent_at', 'desc');
->orderBy('sent_at', $sortOrder);

$results = [];
foreach (array_chunk($ids, 1000) as $chunk) {
Expand All @@ -1298,6 +1300,50 @@
return array_merge([], ...$results);
}

/**
* @param Account $account
* @param Mailbox $mailbox
* @param string $userId
* @param int[] $ids
* @param string $sortOrder
* @param bool $threadingEnabled
*
* @return Message[][]

Check failure on line 1311 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Db/MessageMapper.php:1311:13: InvalidReturnType: The declared return type 'array<array-key, array<array-key, OCA\Mail\Db\Message>>' for OCA\Mail\Db\MessageMapper::findMessageListsByMailboxAndIds is incorrect, got 'array<array-key, OCA\Mail\Db\Message|array<array-key, OCA\Mail\Db\Message|list{OCA\Mail\Db\Message}>>' (see https://psalm.dev/011)
*/
public function findMessageListsByMailboxAndIds(Account $account, Mailbox $mailbox, string $userId, array $ids, string $sortOrder, bool $threadingEnabled = false): array {
if ($ids === []) {
return [];
}

$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('mailbox_id', $qb->createNamedParameter($mailbox->getId()), IQueryBuilder::PARAM_INT),
$qb->expr()->in('id', $qb->createParameter('ids'))
)
->orderBy('sent_at', $sortOrder);
$results = [];
foreach (array_chunk($ids, 1000) as $chunk) {
$qb->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
if ($threadingEnabled) {
$res = $qb->executeQuery();
while ($row = $res->fetch()) {
$message = $this->mapRowToEntity($row);
if ($message->getThreadRootId() === null) {
$results[] = [$message];
} else {
$results[] = $this->findThread($account, $message->getThreadRootId(), $sortOrder);

Check failure on line 1336 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Db/MessageMapper.php:1336:48: PossiblyNullArgument: Argument 2 of OCA\Mail\Db\MessageMapper::findThread cannot be null, possibly null value provided (see https://psalm.dev/078)
}
}
$res->closeCursor();
} else {
$results[] = array_map(fn (Message $msg) => [$msg], $this->findRelatedData($this->findEntities($qb), $userId));
}
}
return $threadingEnabled ? $results : array_merge([], ...$results);

Check failure on line 1344 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnStatement

lib/Db/MessageMapper.php:1344:10: InvalidReturnStatement: The inferred type 'array<array-key, OCA\Mail\Db\Message|array<array-key, OCA\Mail\Db\Message|list{OCA\Mail\Db\Message}>>' does not match the declared return type 'array<array-key, array<array-key, OCA\Mail\Db\Message>>' for OCA\Mail\Db\MessageMapper::findMessageListsByMailboxAndIds (see https://psalm.dev/128)
}

/**
* @param string $userId
* @param int[] $ids
Expand Down Expand Up @@ -1325,6 +1371,48 @@
return array_merge([], ...$results);
}


/**
* @param Account $account
* @param string $userId
* @param int[] $ids
* @param string $sortOrder
*
* @return Message[][]

Check failure on line 1381 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/Db/MessageMapper.php:1381:13: InvalidReturnType: The declared return type 'array<array-key, array<array-key, OCA\Mail\Db\Message>>' for OCA\Mail\Db\MessageMapper::findMessageListsByIds is incorrect, got 'array<array-key, OCA\Mail\Db\Message|array<array-key, OCA\Mail\Db\Message|list{OCA\Mail\Db\Message}>>' (see https://psalm.dev/011)
*/
public function findMessageListsByIds(Account $account, string $userId, array $ids, string $sortOrder, bool $threadingEnabled = false): array {
if ($ids === []) {
return [];
}
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->in('id', $qb->createParameter('ids'))
)
->orderBy('sent_at', $sortOrder);

$results = [];
foreach (array_chunk($ids, 1000) as $chunk) {
$qb->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
if ($threadingEnabled) {
$res = $qb->executeQuery();
while ($row = $res->fetch()) {
$message = $this->mapRowToEntity($row);
if ($message->getThreadRootId() === null) {
$results[] = [$message];
} else {
$results[] = $this->findThread($account, $message->getThreadRootId(), $sortOrder);

Check failure on line 1405 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyNullArgument

lib/Db/MessageMapper.php:1405:48: PossiblyNullArgument: Argument 2 of OCA\Mail\Db\MessageMapper::findThread cannot be null, possibly null value provided (see https://psalm.dev/078)
}
}
$res->closeCursor();
} else {
$results[] = array_map(fn (Message $msg) => [$msg], $this->findRelatedData($this->findEntities($qb), $userId));
}
}
return $threadingEnabled ? $results : array_merge([], ...$results);

Check failure on line 1413 in lib/Db/MessageMapper.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnStatement

lib/Db/MessageMapper.php:1413:10: InvalidReturnStatement: The inferred type 'array<array-key, OCA\Mail\Db\Message|array<array-key, OCA\Mail\Db\Message|list{OCA\Mail\Db\Message}>>' does not match the declared return type 'array<array-key, array<array-key, OCA\Mail\Db\Message>>' for OCA\Mail\Db\MessageMapper::findMessageListsByIds (see https://psalm.dev/128)
}

/**
* @param Message[] $messages
*
Expand Down
20 changes: 15 additions & 5 deletions lib/IMAP/PreviewEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Mail\Db\Message;
use OCA\Mail\Db\MessageMapper as DbMapper;
use OCA\Mail\IMAP\MessageMapper as ImapMapper;
use OCA\Mail\Service\Attachment\AttachmentService;
use OCA\Mail\Service\Avatar\Avatar;
use OCA\Mail\Service\AvatarService;
use Psr\Log\LoggerInterface;
Expand All @@ -39,11 +40,14 @@
/** @var AvatarService */
private $avatarService;

public function __construct(IMAPClientFactory $clientFactory,
public function __construct(
IMAPClientFactory $clientFactory,
ImapMapper $imapMapper,
DbMapper $dbMapper,
LoggerInterface $logger,
AvatarService $avatarService) {
AvatarService $avatarService,
private AttachmentService $attachmentService,
) {
$this->clientFactory = $clientFactory;
$this->imapMapper = $imapMapper;
$this->mapper = $dbMapper;
Expand All @@ -52,12 +56,12 @@
}

/**
* @param Message[] $messages
* @param Message[][] $messages
*
* @return Message[]
* @return Message[][]

Check failure on line 61 in lib/IMAP/PreviewEnhancer.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidReturnType

lib/IMAP/PreviewEnhancer.php:61:13: InvalidReturnType: The declared return type 'array<array-key, array<array-key, OCA\Mail\Db\Message>>' for OCA\Mail\IMAP\PreviewEnhancer::process is incorrect, got 'array<array-key, OCA\Mail\Db\Message|array<array-key, OCA\Mail\Db\Message>>' (see https://psalm.dev/011)
*/
public function process(Account $account, Mailbox $mailbox, array $messages, bool $preLoadAvatars = false, ?string $userId = null): array {
$needAnalyze = array_reduce($messages, static function (array $carry, Message $message) {

Check failure on line 64 in lib/IMAP/PreviewEnhancer.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/IMAP/PreviewEnhancer.php:64:73: InvalidArgument: The second param of the closure passed to array_reduce must take array<array-key, OCA\Mail\Db\Message> but only accepts OCA\Mail\Db\Message (see https://psalm.dev/004)
if ($message->getStructureAnalyzed()) {
// Nothing to do
return $carry;
Expand All @@ -65,6 +69,12 @@

return array_merge($carry, [$message->getUid()]);
}, []);
$client = $this->clientFactory->getClient($account);

foreach ($messages as $message) {
$attachments = $this->attachmentService->getAttachmentNames($account, $mailbox, $message, $client);
$message->setAttachments($attachments);
}

if ($preLoadAvatars) {
foreach ($messages as $message) {
Expand All @@ -87,7 +97,7 @@
return $messages;
}

$client = $this->clientFactory->getClient($account);

try {
$data = $this->imapMapper->getBodyStructureData(
$client,
Expand Down
3 changes: 2 additions & 1 deletion lib/Model/IMAPMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public function getSentDate(): Horde_Imap_Client_DateTime {
return $this->imapDate;
}


/**
* @param int $id
*
Expand Down Expand Up @@ -384,7 +385,7 @@ public function setContent(string $content) {
*/
#[\Override]
public function getAttachments(): array {
throw new Exception('not implemented');
return $this->attachments;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions lib/Service/Attachment/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
use OCA\Mail\Db\LocalAttachment;
use OCA\Mail\Db\LocalAttachmentMapper;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\Message;
use OCA\Mail\Exception\AttachmentNotFoundException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Exception\UploadException;
use OCA\Mail\IMAP\MessageMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\ICacheFactory;
use Psr\Log\LoggerInterface;

class AttachmentService implements IAttachmentService {
Expand All @@ -51,6 +55,10 @@ class AttachmentService implements IAttachmentService {
* @var LoggerInterface
*/
private $logger;
/**
* @var ICache
*/
private $cache;

/**
* @param Folder $userFolder
Expand All @@ -60,13 +68,15 @@ public function __construct($userFolder,
AttachmentStorage $storage,
IMailManager $mailManager,
MessageMapper $imapMessageMapper,
ICacheFactory $cacheFactory,
LoggerInterface $logger) {
$this->mapper = $mapper;
$this->storage = $storage;
$this->mailManager = $mailManager;
$this->messageMapper = $imapMessageMapper;
$this->userFolder = $userFolder;
$this->logger = $logger;
$this->cache = $cacheFactory->createLocal('mail.attachment_names');
}

/**
Expand Down Expand Up @@ -249,6 +259,32 @@ public function handleAttachments(Account $account, array $attachments, \Horde_I
return array_values(array_filter($attachmentIds));
}

public function getAttachmentNames(Account $account, Mailbox $mailbox, Message $message, \Horde_Imap_Client_Socket $client): array {
$attachments = [];
$uniqueCacheId = $account->getUserId() . $account->getId() . $mailbox->getId() . $message->getUid();
$cached = $this->cache->get($uniqueCacheId);
if ($cached) {
return $cached;
}
try {
$imapMessage = $this->mailManager->getImapMessage(
$client,
$account,
$mailbox,
$message->getUid(),
true
);
$attachments = $imapMessage->getAttachments();
} catch (ServiceException $e) {
$this->logger->error('Could not get attachment names', ['exception' => $e, 'messageId' => $message->getUid()]);
}
$result = array_map(static function (array $attachment) {
return ['name' => $attachment['fileName'],'mime' => $attachment['mime']];
}, $attachments);
$this->cache->set($uniqueCacheId, $result);
return $result;
}

/**
* Add a message as attachment
*
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCA\Mail\Account;
use OCA\Mail\Attachment;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\Message;
Expand Down Expand Up @@ -236,8 +237,8 @@ public function getImapMessagesForScheduleProcessing(Account $account,
}

#[\Override]
public function getThread(Account $account, string $threadRootId): array {
return $this->dbMessageMapper->findThread($account, $threadRootId);
public function getThread(Account $account, string $threadRootId, string $sortOrder = IMailSearch::ORDER_NEWEST_FIRST): array {
return $this->dbMessageMapper->findThread($account, $threadRootId, $sortOrder);
}

#[\Override]
Expand Down
Loading
Loading