1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
<?php
declare(strict_types=1);
/**
* Mail App
*
* @copyright 2022 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Mail\Service;
use OCA\Mail\Account;
use OCA\Mail\Contracts\ILocalMailboxService;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailTransmission;
use OCA\Mail\Db\LocalMessage;
use OCA\Mail\Db\LocalMessageMapper;
use OCA\Mail\Db\Recipient;
use OCA\Mail\Events\OutboxMessageCreatedEvent;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\Attachment\AttachmentService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\AppFramework\Utility\ITimeFactory;
use Psr\Log\LoggerInterface;
use Throwable;
class OutboxService implements ILocalMailboxService {
/** @var IMailTransmission */
private $transmission;
/** @var LocalMessageMapper */
private $mapper;
/** @var AttachmentService */
private $attachmentService;
/** @var IEventDispatcher */
private $eventDispatcher;
/** @var IMAPClientFactory */
private $clientFactory;
/** @var IMailManager */
private $mailManager;
/** @var AccountService */
private $accountService;
/** @var ITimeFactory */
private $timeFactory;
/** @var LoggerInterface */
private $logger;
public function __construct(IMailTransmission $transmission,
LocalMessageMapper $mapper,
AttachmentService $attachmentService,
IEventDispatcher $eventDispatcher,
IMAPClientFactory $clientFactory,
IMailManager $mailManager,
AccountService $accountService,
ITimeFactory $timeFactory,
LoggerInterface $logger) {
$this->transmission = $transmission;
$this->mapper = $mapper;
$this->attachmentService = $attachmentService;
$this->eventDispatcher = $eventDispatcher;
$this->clientFactory = $clientFactory;
$this->mailManager = $mailManager;
$this->timeFactory = $timeFactory;
$this->logger = $logger;
$this->accountService = $accountService;
}
/**
* @param array $recipients
* @param int $type
* @return Recipient[]
*/
private static function convertToRecipient(array $recipients, int $type): array {
return array_map(function ($recipient) use ($type) {
$r = new Recipient();
$r->setType($type);
$r->setLabel($recipient['label'] ?? $recipient['email']);
$r->setEmail($recipient['email']);
return $r;
}, $recipients);
}
/**
* @return LocalMessage[]
*/
public function getMessages(string $userId): array {
return $this->mapper->getAllForUser($userId);
}
/**
* @throws DoesNotExistException
*/
public function getMessage(int $id, string $userId): LocalMessage {
return $this->mapper->findById($id, $userId);
}
public function deleteMessage(string $userId, LocalMessage $message): void {
$this->attachmentService->deleteLocalMessageAttachments($userId, $message->getId());
$this->mapper->deleteWithRecipients($message);
}
public function sendMessage(LocalMessage $message, Account $account): void {
try {
$this->transmission->sendLocalMessage($account, $message);
} catch (ClientException|ServiceException $e) {
// Mark as failed so the message is not sent repeatedly in background
$message->setFailed(true);
$this->mapper->update($message);
throw $e;
}
$this->attachmentService->deleteLocalMessageAttachments($account->getUserId(), $message->getId());
$this->mapper->deleteWithRecipients($message);
}
public function saveMessage(Account $account, LocalMessage $message, array $to, array $cc, array $bcc, array $attachments = []): LocalMessage {
$toRecipients = self::convertToRecipient($to, Recipient::TYPE_TO);
$ccRecipients = self::convertToRecipient($cc, Recipient::TYPE_CC);
$bccRecipients = self::convertToRecipient($bcc, Recipient::TYPE_BCC);
$message = $this->mapper->saveWithRecipients($message, $toRecipients, $ccRecipients, $bccRecipients);
$client = $this->clientFactory->getClient($account);
try {
$attachmentIds = $this->attachmentService->handleAttachments($account, $attachments, $client);
} finally {
$client->logout();
}
$message->setAttachments($this->attachmentService->saveLocalMessageAttachments($account->getUserId(), $message->getId(), $attachmentIds));
return $message;
}
public function updateMessage(Account $account, LocalMessage $message, array $to, array $cc, array $bcc, array $attachments = []): LocalMessage {
$toRecipients = self::convertToRecipient($to, Recipient::TYPE_TO);
$ccRecipients = self::convertToRecipient($cc, Recipient::TYPE_CC);
$bccRecipients = self::convertToRecipient($bcc, Recipient::TYPE_BCC);
$message = $this->mapper->updateWithRecipients($message, $toRecipients, $ccRecipients, $bccRecipients);
$client = $this->clientFactory->getClient($account);
try {
$attachmentIds = $this->attachmentService->handleAttachments($account, $attachments, $client);
} finally {
$client->logout();
}
$message->setAttachments($this->attachmentService->updateLocalMessageAttachments($account->getUserId(), $message, $attachmentIds));
return $message;
}
public function handleDraft(Account $account, int $draftId): void {
$message = $this->mailManager->getMessage($account->getUserId(), $draftId);
$this->eventDispatcher->dispatch(
OutboxMessageCreatedEvent::class,
new OutboxMessageCreatedEvent($account, $message)
);
}
public function flush(): void {
$messages = $this->mapper->findDue(
$this->timeFactory->getTime()
);
if (empty($messages)) {
return;
}
$accountIds = array_unique(array_map(function ($message) {
return $message->getAccountId();
}, $messages));
$accounts = array_combine($accountIds, array_map(function ($accountId) {
try {
return $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
// The message belongs to a deleted account
return null;
}
}, $accountIds));
foreach ($messages as $message) {
try {
$account = $accounts[$message->getAccountId()];
if ($account === null) {
// Ignore message of non-existent account
continue;
}
$this->sendMessage(
$message,
$account,
);
$this->logger->debug('Outbox message {id} sent', [
'id' => $message->getId(),
]);
} catch (Throwable $e) {
// Failure of one message should not stop sending other messages
// Log and continue
$this->logger->warning('Could not send outbox message {id}: ' . $e->getMessage(), [
'id' => $message->getId(),
'exception' => $e,
]);
}
}
}
}
|