Skip to content

Commit

Permalink
Merge pull request #6230 from nextcloud/fix/attachments-not-sent
Browse files Browse the repository at this point in the history
Fix message attachments not sending
  • Loading branch information
miaulalala authored Apr 13, 2022
2 parents 1282378 + 32d9f12 commit 80f38ed
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
29 changes: 20 additions & 9 deletions lib/Db/LocalMessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\DB\Exception as DBException;
use Throwable;
use function array_filter;
use function array_map;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
Expand Down Expand Up @@ -133,19 +132,31 @@ public function findDue(int $time): array {
$qb->expr()->lte('send_at', $qb->createNamedParameter($time, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
);
$messages = $this->findEntities($select);

if (empty($messages)) {
return [];
}

$ids = array_map(function (LocalMessage $message) {
return $message->getId();
}, $messages);

$attachments = $this->attachmentMapper->findByLocalMessageIds($ids);
$recipients = $this->recipientMapper->findByLocalMessageIds($ids);
return array_map(static function ($message) use ($attachments, $recipients) {
$message->setAttachments(array_filter($attachments, function (LocalAttachment $attachment) use ($message) {
return $attachment->getLocalMessageId() === $message->getId();
}));
$message->setRecipients(array_filter($recipients, function (Recipient $recipient) use ($message) {
return $recipient->getLocalMessageId() === $message->getId();
}));
return $message;

$recipientMap = [];
foreach ($recipients as $r) {
$recipientMap[$r->getLocalMessageId()][] = $r;
}
$attachmentMap = [];
foreach ($attachments as $a) {
$attachmentMap[$a->getLocalMessageId()][] = $a;
}

return array_map(static function ($localMessage) use ($attachmentMap, $recipientMap) {
$localMessage->setAttachments($attachmentMap[$localMessage->getId()] ?? []);
$localMessage->setRecipients($recipientMap[$localMessage->getId()] ?? []);
return $localMessage;
}, $messages);
}

Expand Down
9 changes: 7 additions & 2 deletions lib/Service/Attachment/AttachmentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

namespace OCA\Mail\Service\Attachment;

use Exception;
use OCA\Mail\Exception\AttachmentNotFoundException;
use OCA\Mail\Exception\UploadException;
use OCP\Files\IAppData;
Expand Down Expand Up @@ -127,6 +126,12 @@ public function retrieve(string $userId, int $attachmentId) {
}

public function delete(string $userId, int $attachmentId): void {
throw new Exception('not implemented');
$folder = $this->getAttachmentFolder($userId);
try {
$file = $folder->getFile((string)$attachmentId);
} catch (NotFoundException $e) {
return;
}
$file->delete();
}
}
15 changes: 8 additions & 7 deletions lib/Service/MailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,21 @@ public function sendLocalMessage(Account $account, LocalMessage $message): void
})
)
);
$attachments = array_map(function (LocalAttachment $attachment) {
// Convert to the untyped nested array used in \OCA\Mail\Controller\AccountsController::send
return [
'type' => 'local',
'id' => $attachment->getId(),
];
}, $message->getAttachments());
$messageData = new NewMessageData(
$account,
$to,
$cc,
$bcc,
$message->getSubject(),
$message->getBody(),
array_map(function (LocalAttachment $attachment) {
// Convert to the untyped nested array used in \OCA\Mail\Controller\AccountsController::send
return [
'type' => 'local',
'id' => $attachment->getId(),
];
}, $message->getAttachments()),
$attachments,
$message->isHtml()
);

Expand Down
5 changes: 4 additions & 1 deletion lib/Service/OutboxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ public function flush(): void {
$this->timeFactory->getTime()
);

if (empty($messages)) {
return;
}

$accountIds = array_unique(array_map(function ($message) {
return $message->getAccountId();
}, $messages));
Expand All @@ -187,7 +191,6 @@ public function flush(): void {

foreach ($messages as $message) {
try {
// TODO: memoize accounts
$this->sendMessage(
$message,
$accounts[$message->getAccountId()],
Expand Down

0 comments on commit 80f38ed

Please sign in to comment.