-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SebastianKrupinski <[email protected]>
- Loading branch information
1 parent
9d2deda
commit 7f85f0d
Showing
4 changed files
with
68 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,19 @@ | |
namespace OCA\Mail\Provider\Command; | ||
|
||
use OCA\Mail\Db\LocalMessage; | ||
use OCA\Mail\Exception\ClientException; | ||
use OCA\Mail\Exception\UploadException; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\Attachment\AttachmentService; | ||
use OCA\Mail\Service\OutboxService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\IConfig; | ||
use OCP\Mail\Provider\Exception\SendException; | ||
use OCP\Mail\Provider\IAddress; | ||
use OCP\Mail\Provider\IMessage; | ||
|
||
class MessageSend { | ||
|
||
public function __construct( | ||
protected IConfig $config, | ||
protected ITimeFactory $time, | ||
protected AccountService $accountService, | ||
protected OutboxService $outboxService, | ||
|
@@ -66,7 +66,7 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar | |
// retrieve user mail account details | ||
try { | ||
$account = $this->accountService->find($userId, (int)$serviceId); | ||
} catch (\Throwable $e) { | ||
} catch (ClientException $e) { | ||
throw new SendException('Error: occurred while retrieving mail account details', 0, $e); | ||
} | ||
// convert mail provider message to mail app message | ||
|
@@ -79,29 +79,26 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar | |
//$localMessage->setEditorBody($message->getBody()); | ||
$localMessage->setHtml(true); | ||
$localMessage->setSendAt($this->time->getTime()); | ||
// convert all mail provider attachments to local attachments | ||
// convert mail provider addresses to recipient addresses | ||
$to = $this->convertAddressArray($message->getTo()); | ||
$cc = $this->convertAddressArray($message->getCc()); | ||
$bcc = $this->convertAddressArray($message->getBcc()); | ||
// save attachments | ||
$attachments = []; | ||
if (count($message->getAttachments()) > 0) { | ||
// iterate attachments and save them | ||
try { | ||
foreach ($message->getAttachments() as $entry) { | ||
// convert mail provider attachment to mail app attachment | ||
try { | ||
$attachments[] = $this->attachmentService->addFileFromString( | ||
$userId, | ||
(string)$entry->getName(), | ||
(string)$entry->getType(), | ||
(string)$entry->getContents() | ||
)->jsonSerialize(); | ||
} catch (\Throwable $e) { | ||
throw new SendException('Error: occurred while saving mail message attachment', 0, $e); | ||
} | ||
$attachments[] = $this->attachmentService->addFileFromString( | ||
$userId, | ||
(string)$entry->getName(), | ||
(string)$entry->getType(), | ||
(string)$entry->getContents() | ||
)->jsonSerialize(); | ||
} | ||
} catch (UploadException $e) { | ||
$this->purgeSavedAttachments($attachments); | ||
throw new SendException('Error: occurred while saving mail message attachment', 0, $e); | ||
} | ||
// convert recipient addresses | ||
$to = $this->convertAddressArray($message->getTo()); | ||
$cc = $this->convertAddressArray($message->getCc()); | ||
$bcc = $this->convertAddressArray($message->getBcc()); | ||
// save message for sending | ||
// save message | ||
try { | ||
$localMessage = $this->outboxService->saveMessage( | ||
$account, | ||
|
@@ -112,6 +109,7 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar | |
$attachments | ||
); | ||
} catch (\Throwable $e) { | ||
$this->purgeSavedAttachments($attachments); | ||
throw new SendException('Error: occurred while saving mail message', 0, $e); | ||
} | ||
// send message | ||
|
@@ -131,7 +129,7 @@ public function perform(string $userId, string $serviceId, IMessage $message, ar | |
* | ||
* @param array<int,IAddress> $addresses collection of IAddress objects | ||
* | ||
* @return array collection of addresses and labels e.g. [[email => '[email protected]', 'User One']] | ||
* @return array<int, array{email: string, label?: string}> collection of addresses and labels | ||
Check failure on line 132 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterMoreSpecificReturnType
|
||
*/ | ||
protected function convertAddressArray(array $addresses): array { | ||
return array_map(static function (IAddress $address) { | ||
Check failure on line 135 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterLessSpecificReturnStatement
|
||
|
@@ -141,4 +139,19 @@ protected function convertAddressArray(array $addresses): array { | |
}, $addresses); | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @since 4.0.0 | ||
* | ||
* @param array | ||
* | ||
* @return array | ||
Check failure on line 149 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterMismatchingDocblockReturnType
Check failure on line 149 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterInvalidReturnType
|
||
*/ | ||
protected function purgeSavedAttachments(array $attachments): void { | ||
Check failure on line 151 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterInvalidDocblock
|
||
foreach ($attachments as $attachment) { | ||
$this->attachmentService->deleteAttachment($attachment->getUserId(), $attachment->getId()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,16 +76,12 @@ public function testHasServices(): void { | |
'smtpUser' => '', | ||
])); | ||
// define account services find | ||
$this->accountService | ||
->expects($this->any()) | ||
->method('findByUserId') | ||
$this->accountService->expects($this->any())->method('findByUserId') | ||
->will( | ||
$this->returnValueMap( | ||
[ | ||
['user0', []], | ||
['user1', [100 => $mailAccount]] | ||
] | ||
) | ||
$this->returnValueMap([ | ||
['user0', []], | ||
['user1', [100 => $mailAccount]] | ||
]) | ||
); | ||
// construct mail provider | ||
$mailProvider = new MailProvider($this->containerInterface, $this->accountService, $this->logger, $this->l10n); | ||
|
@@ -121,16 +117,12 @@ public function testListServices(): void { | |
new MailAddress('[email protected]', 'User One') | ||
); | ||
// define account services find | ||
$this->accountService | ||
->expects($this->any()) | ||
->method('findByUserId') | ||
$this->accountService->expects($this->any())->method('findByUserId') | ||
->will( | ||
$this->returnValueMap( | ||
[ | ||
['user0', []], | ||
['user1', [$mailAccount]] | ||
] | ||
) | ||
$this->returnValueMap([ | ||
['user0', []], | ||
['user1', [$mailAccount]] | ||
]) | ||
); | ||
// construct mail provider | ||
$mailProvider = new MailProvider($this->containerInterface, $this->accountService, $this->logger, $this->l10n); | ||
|
@@ -166,16 +158,12 @@ public function testFindServiceById(): void { | |
new MailAddress('[email protected]', 'User One') | ||
); | ||
// define account services find | ||
$this->accountService | ||
->expects($this->any()) | ||
->method('find') | ||
->will( | ||
$this->returnValueMap( | ||
[ | ||
['user0', 100, $this->throwException(new ClientException())], | ||
['user1', 100, $mailAccount] | ||
] | ||
) | ||
$this->accountService->expects($this->any())->method('find') | ||
->willReturnCallback( | ||
fn (string $userId, int $serviceId) => match (true) { | ||
$userId === 'user1' && $serviceId === 100 => $mailAccount, | ||
default => throw new ClientException() | ||
} | ||
); | ||
// construct mail provider | ||
$mailProvider = new MailProvider($this->containerInterface, $this->accountService, $this->logger, $this->l10n); | ||
|
@@ -211,16 +199,12 @@ public function testFindServiceByAddress(): void { | |
new MailAddress('[email protected]', 'User One') | ||
); | ||
// define account services find | ||
$this->accountService | ||
->expects($this->any()) | ||
->method('findByUserIdAndAddress') | ||
$this->accountService->expects($this->any())->method('findByUserIdAndAddress') | ||
->will( | ||
$this->returnValueMap( | ||
[ | ||
['user0', '[email protected]', $this->throwException(new ClientException())], | ||
['user1', '[email protected]', [$mailAccount]] | ||
] | ||
) | ||
$this->returnValueMap([ | ||
['user0', '[email protected]', []], | ||
['user1', '[email protected]', [$mailAccount]] | ||
]) | ||
); | ||
// construct mail provider | ||
$mailProvider = new MailProvider($this->containerInterface, $this->accountService, $this->logger, $this->l10n); | ||
|