From 326d48fd85a2e6cafe6b27f63f0fb48976ae162f Mon Sep 17 00:00:00 2001 From: Johannes Merkel Date: Fri, 11 Aug 2023 15:55:54 +0200 Subject: [PATCH] fix(Snooze): some minor changes Signed-off-by: Johannes Merkel --- lib/Controller/MessagesController.php | 11 +- lib/Controller/ThreadController.php | 11 +- lib/Db/MessageSnoozeMapper.php | 10 +- lib/Service/SnoozeService.php | 159 +++++++++++++++++++++++++- src/components/Envelope.vue | 23 ++-- src/components/MenuEnvelope.vue | 5 - src/components/NavigationMailbox.vue | 2 +- src/service/MessageService.js | 4 +- src/service/ThreadService.js | 5 +- src/store/actions.js | 20 +++- 10 files changed, 207 insertions(+), 43 deletions(-) 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 2c6a3b6d2c..a1dd66fe96 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/Db/MessageSnoozeMapper.php b/lib/Db/MessageSnoozeMapper.php index 08957bf909..ee4a3e0611 100644 --- a/lib/Db/MessageSnoozeMapper.php +++ b/lib/Db/MessageSnoozeMapper.php @@ -41,19 +41,15 @@ public function __construct(IDBConnection $db) { /** * Deletes DB Entry for a waked message * - * @param string $messageId + * @param string[] $messageIds * * @return void */ - public function deleteByMessageId(string $messageId): void { + public function deleteByMessageIds(array $messageIds): void { $qb = $this->db->getQueryBuilder(); $delete = $qb->delete($this->getTableName()) - ->where($qb->expr()->eq( - 'message_id', - $qb->createNamedParameter($messageId, IQueryBuilder::PARAM_STR), - IQueryBuilder::PARAM_STR, - )); + ->where($qb->expr()->in('message_id', $qb->createNamedParameter($messageIds, IQueryBuilder::PARAM_STR_ARRAY))); $delete->executeStatement(); } diff --git a/lib/Service/SnoozeService.php b/lib/Service/SnoozeService.php index e04820240b..f90bd0c9bf 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,7 +41,9 @@ use OCA\Mail\IMAP\IMAPClientFactory; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\DB\Exception; use Psr\Log\LoggerInterface; +use Throwable; class SnoozeService { @@ -81,19 +84,121 @@ 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 Throwable + */ + 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 $e; + } + } + + /** + * @param Message $selectedMessage + * @param int $unixTimestamp + * @param Account $srcAccount + * @param Mailbox $srcMailbox + * @param Account $dstAccount + * @param Mailbox $dstMailbox + * + * @return void + * + * @throws Throwable + */ + 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 $e; + } + + } + /** * Adds a DB entry for the message with a wake timestamp * * @param Message $message * @param int $unixTimestamp + * * @return void + * + * @throws Exception */ - 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) { + if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + $messageId = $message->getMessageId(); + if($messageId === null) { + throw $e; + } + $this->messageSnoozeMapper->deleteByMessageIds(array($messageId)); + $this->messageSnoozeMapper->insert($snooze); + } else { + throw $e; + } + } + } + + /** + * Removes the DB entry for the message + * + * @param Message $message + * @return void + */ + public function unSnoozeMessageDB(Message $message): void { + $messageId = $message->getMessageId(); + if($messageId !== null) { + $this->messageSnoozeMapper->deleteByMessageIds(array($messageId)); + } } /** @@ -101,9 +206,12 @@ public function snoozeMessage(Message $message, int $unixTimestamp): void { * * @param Message $selectedMessage * @param int $unixTimestamp + * * @return void + * + * @throws Exception */ - public function snoozeThread(Message $selectedMessage, int $unixTimestamp): void { + public function snoozeThreadDB(Message $selectedMessage, int $unixTimestamp): void { $messages = $this->threadMapper->findMessageIdsByThreadRoot( $selectedMessage->getThreadRootId() ); @@ -112,7 +220,40 @@ 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) { + if ($e->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + $messageId = $message['messageId']; + if($messageId === null) { + throw $e; + } + $this->messageSnoozeMapper->deleteByMessageIds(array($messageId)); + $this->messageSnoozeMapper->insert($snooze); + } else { + throw $e; + } + } + } + } + + /** + * Removes DB entry for the messages + * + * @param Message $selectedMessage + * @return void + */ + public function unSnoozeThreadDB(Message $selectedMessage): void { + $messages = $this->threadMapper->findMessageIdsByThreadRoot( + $selectedMessage->getThreadRootId() + ); + + $messageIdsToDelete = []; + foreach ($messages as $message) { + $messageIdsToDelete[] = $message['messageId']; + } + if(count($messageIdsToDelete) > 0) { + $this->messageSnoozeMapper->deleteByMessageIds($messageIdsToDelete); } } @@ -144,6 +285,7 @@ private function wakeMessagesByAccount(Account $account): void { $client = $this->clientFactory->getClient($account); try { + $messageIdsToDelete = []; foreach ($messages as $message) { $this->mailManager->moveMessage( $account, @@ -155,7 +297,14 @@ private function wakeMessagesByAccount(Account $account): void { //TODO mark message as unread? - $this->messageSnoozeMapper->deleteByMessageId($message->getMessageId()); + $messageId = $message->getMessageId(); + if($messageId !== null) { + $messageIdsToDelete[] = $messageId; + } + + } + if(count($messageIdsToDelete) > 0) { + $this->messageSnoozeMapper->deleteByMessageIds($messageIdsToDelete); } } finally { $client->logout(); diff --git a/src/components/Envelope.vue b/src/components/Envelope.vue index fb3d7b9f89..51481e6b71 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">