diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index a04408b514..15d2a2de71 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -390,18 +390,25 @@ public function move(int $id, int $destFolderId): JSONResponse { * * @param int $id * @param int $unixTimestamp + * @param int $destMailboxId * * @return JSONResponse + * @throws ClientException + * @throws ServiceException */ #[TrapError] - public function snooze(int $id, int $unixTimestamp): JSONResponse { + public function snooze(int $id, int $unixTimestamp, int $destMailboxId): JSONResponse { try { $message = $this->mailManager->getMessage($this->currentUserId, $id); + $srcMailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId()); + $dstMailbox = $this->mailManager->getMailbox($this->currentUserId, $destMailboxId); + $srcAccount = $this->accountService->find($this->currentUserId, $srcMailbox->getAccountId()); + $dstAccount = $this->accountService->find($this->currentUserId, $dstMailbox->getAccountId()); } catch (DoesNotExistException $e) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } - $this->snoozeService->snoozeMessage($message, $unixTimestamp); + $this->snoozeService->snoozeMessage($message, $unixTimestamp, $srcAccount, $srcMailbox, $dstAccount, $dstMailbox); return new JSONResponse(); } diff --git a/lib/Controller/ThreadController.php b/lib/Controller/ThreadController.php index 0ffb66cdd8..7876510fc5 100755 --- a/lib/Controller/ThreadController.php +++ b/lib/Controller/ThreadController.php @@ -104,18 +104,25 @@ public function move(int $id, int $destMailboxId): JSONResponse { * * @param int $id * @param int $unixTimestamp + * @param int $destMailboxId * * @return JSONResponse + * @throws ClientException + * @throws ServiceException */ #[TrapError] - public function snooze(int $id, int $unixTimestamp): JSONResponse { + public function snooze(int $id, int $unixTimestamp, int $destMailboxId): JSONResponse { try { $selectedMessage = $this->mailManager->getMessage($this->currentUserId, $id); + $srcMailbox = $this->mailManager->getMailbox($this->currentUserId, $selectedMessage->getMailboxId()); + $srcAccount = $this->accountService->find($this->currentUserId, $srcMailbox->getAccountId()); + $dstMailbox = $this->mailManager->getMailbox($this->currentUserId, $destMailboxId); + $dstAccount = $this->accountService->find($this->currentUserId, $dstMailbox->getAccountId()); } catch (DoesNotExistException $e) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } - $this->snoozeService->snoozeThread($selectedMessage, $unixTimestamp); + $this->snoozeService->snoozeThread($selectedMessage, $unixTimestamp, $srcAccount, $srcMailbox, $dstAccount, $dstMailbox); return new JSONResponse(); } diff --git a/lib/Service/SnoozeService.php b/lib/Service/SnoozeService.php index e04820240b..39340c3a1b 100644 --- a/lib/Service/SnoozeService.php +++ b/lib/Service/SnoozeService.php @@ -29,6 +29,7 @@ use OCA\Mail\Account; use OCA\Mail\Contracts\IMailManager; use OCA\Mail\Db\MailAccountMapper; +use OCA\Mail\Db\Mailbox; use OCA\Mail\Db\MailboxMapper; use OCA\Mail\Db\Message; use OCA\Mail\Db\MessageMapper; @@ -40,6 +41,7 @@ use OCA\Mail\IMAP\IMAPClientFactory; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\DB\Exception; use Psr\Log\LoggerInterface; class SnoozeService { @@ -81,6 +83,79 @@ public function wakeMessages(): void { } } + /** + * @param Message $message + * @param int $unixTimestamp + * @param Account $srcAccount + * @param Mailbox $srcMailbox + * @param Account $dstAccount + * @param Mailbox $dstMailbox + * + * @return void + * + * @throws ServiceException + */ + public function snoozeMessage( + Message $message, + int $unixTimestamp, + Account $srcAccount, + Mailbox $srcMailbox, + Account $dstAccount, + Mailbox $dstMailbox + ): void { + $this->snoozeMessageDB($message, $unixTimestamp); + + try { + $this->mailManager->moveMessage( + $srcAccount, + $srcMailbox->getName(), + $message->getUid(), + $dstAccount, + $dstMailbox->getName() + ); + } catch (\Throwable $e) { + $this->unSnoozeMessageDB($message); + throw new ServiceException($e->getMessage()); + } + } + + /** + * @param Message $selectedMessage + * @param int $unixTimestamp + * @param Account $srcAccount + * @param Mailbox $srcMailbox + * @param Account $dstAccount + * @param Mailbox $dstMailbox + * + * @return void + * + * @throws ServiceException + */ + public function snoozeThread( + Message $selectedMessage, + int $unixTimestamp, + Account $srcAccount, + Mailbox $srcMailbox, + Account $dstAccount, + Mailbox $dstMailbox + ):void { + $this->snoozeThreadDB($selectedMessage, $unixTimestamp); + + try { + $this->mailManager->moveThread( + $srcAccount, + $srcMailbox, + $dstAccount, + $dstMailbox, + $selectedMessage->getThreadRootId() + ); + } catch (\Throwable $e) { + $this->unSnoozeThreadDB($selectedMessage); + throw new ServiceException($e->getMessage()); + } + + } + /** * Adds a DB entry for the message with a wake timestamp @@ -89,11 +164,26 @@ public function wakeMessages(): void { * @param int $unixTimestamp * @return void */ - public function snoozeMessage(Message $message, int $unixTimestamp): void { + public function snoozeMessageDB(Message $message, int $unixTimestamp): void { $snooze = new MessageSnooze(); $snooze->setMessageId($message->getMessageId()); $snooze->setSnoozedUntil($unixTimestamp); - $this->messageSnoozeMapper->insert($snooze); + try { + $this->messageSnoozeMapper->insert($snooze); + } catch(Exception $e) { + $this->messageSnoozeMapper->deleteByMessageId($message->getMessageId()); + $this->messageSnoozeMapper->insert($snooze); + } + } + + /** + * Removes the DB entry for the message + * + * @param Message $message + * @return void + */ + public function unSnoozeMessageDB(Message $message): void { + $this->messageSnoozeMapper->deleteByMessageId($message->getMessageId()); } /** @@ -103,7 +193,7 @@ public function snoozeMessage(Message $message, int $unixTimestamp): void { * @param int $unixTimestamp * @return void */ - public function snoozeThread(Message $selectedMessage, int $unixTimestamp): void { + public function snoozeThreadDB(Message $selectedMessage, int $unixTimestamp): void { $messages = $this->threadMapper->findMessageIdsByThreadRoot( $selectedMessage->getThreadRootId() ); @@ -112,7 +202,28 @@ public function snoozeThread(Message $selectedMessage, int $unixTimestamp): void $snooze = new MessageSnooze(); $snooze->setMessageId($message['messageId']); $snooze->setSnoozedUntil($unixTimestamp); - $this->messageSnoozeMapper->insert($snooze); + try { + $this->messageSnoozeMapper->insert($snooze); + } catch(Exception $e) { + $this->messageSnoozeMapper->deleteByMessageId($message['messageId']); + $this->messageSnoozeMapper->insert($snooze); + } + } + } + + /** + * Removes DB entry for the messages + * + * @param Message $selectedMessage + * @return void + */ + public function unSnoozeThreadDB(Message $selectedMessage): void { + $messages = $this->threadMapper->findMessageIdsByThreadRoot( + $selectedMessage->getThreadRootId() + ); + + foreach ($messages as $message) { + $this->messageSnoozeMapper->deleteByMessageId($message['messageId']); } } diff --git a/src/components/Envelope.vue b/src/components/Envelope.vue index bc560fd1e7..75a2d54721 100644 --- a/src/components/Envelope.vue +++ b/src/components/Envelope.vue @@ -17,7 +17,7 @@ :details="formatted()" @click="onClick" @click.ctrl="toggleSelected" - @update:menuOpen="moreActionsOpen=false"> + @update:menuOpen="closeMoreAndSnoozeOptions">