Skip to content

Commit 769465c

Browse files
committed
fix: ensure drafts folder exists
Signed-off-by: Daniel Kesselberg <[email protected]>
1 parent 6de0c2a commit 769465c

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

lib/Contracts/IMailManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ public function getMailboxes(Account $account, bool $forceSync = false): array;
4646
/**
4747
* @param Account $account
4848
* @param string $name
49+
* @param string[] $specialUse
4950
*
5051
* @return Mailbox
5152
*
5253
* @throws ServiceException
5354
*/
54-
public function createMailbox(Account $account, string $name): Mailbox;
55+
public function createMailbox(Account $account, string $name, array $specialUse = []): Mailbox;
5556

5657
/**
5758
* @param Mailbox $mailbox

lib/IMAP/FolderMapper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ public function getFolders(Account $account, Horde_Imap_Client_Socket $client,
7171
), $toPersist);
7272
}
7373

74-
public function createFolder(Horde_Imap_Client_Socket $client,
75-
string $name): Folder {
76-
$client->createMailbox($name);
74+
public function createFolder(Horde_Imap_Client_Socket $client, string $name, array $specialUse = []): Folder {
75+
$client->createMailbox($name, [
76+
'special_use' => $specialUse,
77+
]);
7778

7879
$list = $client->listMailboxes($name, Horde_Imap_Client::MBOX_ALL_SUBSCRIBED, [
7980
'delimiter' => true,

lib/Service/MailManager.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,11 @@ public function getMailboxes(Account $account, bool $forceSync = false): array {
142142
return $this->mailboxMapper->findAll($account);
143143
}
144144

145-
/**
146-
* @param Account $account
147-
* @param string $name
148-
*
149-
* @return Mailbox
150-
* @throws ServiceException
151-
*/
152145
#[\Override]
153-
public function createMailbox(Account $account, string $name): Mailbox {
146+
public function createMailbox(Account $account, string $name, array $specialUse = []): Mailbox {
154147
$client = $this->imapClientFactory->getClient($account);
155148
try {
156-
$folder = $this->folderMapper->createFolder($client, $name);
149+
$folder = $this->folderMapper->createFolder($client, $name, $specialUse);
157150
$this->folderMapper->fetchFolderAcls([$folder], $client);
158151
} catch (Horde_Imap_Client_Exception $e) {
159152
throw new ServiceException(

lib/Service/MailTransmission.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use OCA\Mail\Account;
2929
use OCA\Mail\Address;
3030
use OCA\Mail\AddressList;
31+
use OCA\Mail\Contracts\IMailManager;
3132
use OCA\Mail\Contracts\IMailTransmission;
3233
use OCA\Mail\Db\LocalMessage;
3334
use OCA\Mail\Db\Mailbox;
@@ -67,6 +68,7 @@ public function __construct(
6768
private PerformanceLogger $performanceLogger,
6869
private AliasesService $aliasesService,
6970
private TransmissionService $transmissionService,
71+
private IMailManager $mailManager,
7072
) {
7173
}
7274

@@ -231,12 +233,7 @@ public function saveLocalDraft(Account $account, LocalMessage $message): void {
231233
$transport = new Horde_Mail_Transport_Null();
232234
$mail->send($transport, false, false);
233235
$perfLogger->step('create IMAP draft message');
234-
// save the message in the drafts folder
235-
$draftsMailboxId = $account->getMailAccount()->getDraftsMailboxId();
236-
if ($draftsMailboxId === null) {
237-
throw new ClientException('No drafts mailbox configured');
238-
}
239-
$draftsMailbox = $this->mailboxMapper->findById($draftsMailboxId);
236+
$draftsMailbox = $this->findOrCreateDraftsMailbox($account);
240237
$this->messageMapper->save(
241238
$client,
242239
$draftsMailbox,
@@ -258,6 +255,20 @@ public function saveLocalDraft(Account $account, LocalMessage $message): void {
258255
$perfLogger->end();
259256
}
260257

258+
private function findOrCreateDraftsMailbox(Account $account): Mailbox {
259+
$draftsMailboxId = $account->getMailAccount()->getDraftsMailboxId();
260+
261+
if ($draftsMailboxId === null) {
262+
return $this->mailManager->createMailbox(
263+
$account,
264+
'Drafts',
265+
[Horde_Imap_Client::SPECIALUSE_DRAFTS]
266+
);
267+
}
268+
269+
return $this->mailboxMapper->findById($draftsMailboxId);
270+
}
271+
261272
/**
262273
* @param NewMessageData $message
263274
* @param Message|null $previousDraft

0 commit comments

Comments
 (0)