Skip to content

Commit

Permalink
fix(Snooze): some minor changes
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Merkel <[email protected]>
  • Loading branch information
JohannesGGE committed Aug 17, 2023
1 parent 76ae438 commit 9c87de5
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 43 deletions.
11 changes: 9 additions & 2 deletions lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
11 changes: 9 additions & 2 deletions lib/Controller/ThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 3 additions & 7 deletions lib/Db/MessageSnoozeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
145 changes: 140 additions & 5 deletions lib/Service/SnoozeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -81,29 +84,127 @@ 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) {
$this->messageSnoozeMapper->deleteByMessageIds(array($message->getMessageId()));

Check failure on line 179 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/Service/SnoozeService.php:179:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)

Check failure on line 179 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

InvalidArgument

lib/Service/SnoozeService.php:179:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)

Check failure on line 179 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

InvalidArgument

lib/Service/SnoozeService.php:179:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)
$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 {
$this->messageSnoozeMapper->deleteByMessageIds(array($message->getMessageId()));

Check failure on line 194 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/Service/SnoozeService.php:194:50: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)

Check failure on line 194 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

InvalidArgument

lib/Service/SnoozeService.php:194:50: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)

Check failure on line 194 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

InvalidArgument

lib/Service/SnoozeService.php:194:50: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but list{null|string} provided (see https://psalm.dev/004)
}

/**
* Adds a DB entry for the messages with a wake timestamp
*
* @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()
);
Expand All @@ -112,7 +213,36 @@ 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) {
$this->messageSnoozeMapper->deleteByMessageIds([$message['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);
}
}

Expand Down Expand Up @@ -144,6 +274,7 @@ private function wakeMessagesByAccount(Account $account): void {

$client = $this->clientFactory->getClient($account);
try {
$messageIdsToDelete = array();
foreach ($messages as $message) {
$this->mailManager->moveMessage(
$account,
Expand All @@ -155,7 +286,11 @@ private function wakeMessagesByAccount(Account $account): void {

//TODO mark message as unread?
$this->messageSnoozeMapper->deleteByMessageId($message->getMessageId());
$messageIdsToDelete[] = $message->getMessageId();

}
if(count($messageIdsToDelete) > 0) {
$this->messageSnoozeMapper->deleteByMessageIds($messageIdsToDelete);

Check failure on line 293 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-master

InvalidArgument

lib/Service/SnoozeService.php:293:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but non-empty-list<null|string> provided (see https://psalm.dev/004)

Check failure on line 293 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable27

InvalidArgument

lib/Service/SnoozeService.php:293:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but non-empty-list<null|string> provided (see https://psalm.dev/004)

Check failure on line 293 in lib/Service/SnoozeService.php

View workflow job for this annotation

GitHub Actions / Nextcloud dev-stable26

InvalidArgument

lib/Service/SnoozeService.php:293:52: InvalidArgument: Argument 1 of OCA\Mail\Db\MessageSnoozeMapper::deleteByMessageIds expects array<array-key, string>, but non-empty-list<null|string> provided (see https://psalm.dev/004)
}
} finally {
$client->logout();
Expand Down
23 changes: 13 additions & 10 deletions src/components/Envelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
:details="formatted()"
@click="onClick"
@click.ctrl="toggleSelected"
@update:menuOpen="moreActionsOpen=false">
@update:menuOpen="closeMoreAndSnoozeOptions">
<template #icon>
<Star
v-if="data.flags.flagged"
Expand Down Expand Up @@ -625,21 +625,23 @@ export default {
reminderOptions() {
const currentDateTime = moment()
// Same day 18:00 PM (or hidden)
const laterTodayTime = (currentDateTime.hour() < 18)
// Same day 18:00 PM (hidden if after 17:00 PM now)
const laterTodayTime = (currentDateTime.hour() < 17)
? moment().hour(18)
: null
// Tomorrow 08:00 AM
const tomorrowTime = moment().add(1, 'days').hour(8)
// Saturday 08:00 AM (or hidden)
const thisWeekendTime = (currentDateTime.day() !== 6 && currentDateTime.day() !== 0)
// Saturday 08:00 AM (hidden if Friday, Saturday or Sunday now)
const thisWeekendTime = (currentDateTime.day() > 0 && currentDateTime.day() < 5)
? moment().day(6).hour(8)
: null
// Next Monday 08:00 AM
const nextWeekTime = moment().add(1, 'weeks').day(1).hour(8)
// Next Monday 08:00 AM (hidden if Sunday now)
const nextWeekTime = (currentDateTime.day() !== 0)
? moment().add(1, 'weeks').day(1).hour(8)
: null
return [
{
Expand Down Expand Up @@ -764,6 +766,10 @@ export default {
this.snoozeOptions = true
this.moreActionsOpen = false
},
closeMoreAndSnoozeOptions() {
this.snoozeOptions = false
this.moreActionsOpen = false
},
async onArchive() {
// Remove from selection first
this.setSelected(false)
Expand All @@ -788,9 +794,6 @@ export default {
await this.$store.dispatch('snoozeThread', {
envelope: this.data,
unixTimestamp: timestamp / 1000,
})
await this.$store.dispatch('moveThread', {
envelope: this.data,
destMailboxId: this.account.snoozeMailboxId,
})
showSuccess(t('mail', 'Thread was snoozed'))
Expand Down
5 changes: 0 additions & 5 deletions src/components/MenuEnvelope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,9 @@ export default {
logger.info(`snoozing message ${this.envelope.databaseId}`)
try {
// Adds DB entry
await this.$store.dispatch('snoozeMessage', {
id: this.envelope.databaseId,
unixTimestamp: timestamp / 1000,
})
// Moves message
await this.$store.dispatch('moveMessage', {
id: this.envelope.databaseId,
destMailboxId: this.account.snoozeMailboxId,
})
showSuccess(t('mail', 'Message was snoozed'))
Expand Down
2 changes: 1 addition & 1 deletion src/components/NavigationMailbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
:size="20" />
<IconJunk v-else-if="mailbox.databaseId === account.junkMailboxId"
:size="20" />
<AlarmIcon v-else-if="mailbox.name === 'Snoozed'"
<AlarmIcon v-else-if="mailbox.databaseId === account.snoozeMailboxId"
:size="20" />
<IconFolderShared v-else-if="mailbox.shared"
:size="20" />
Expand Down
4 changes: 2 additions & 2 deletions src/service/MessageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ export function moveMessage(id, destFolderId) {
})
}

// Only adds DB entry, moving the message is done in a separate request
export function snoozeMessage(id, unixTimestamp) {
export function snoozeMessage(id, unixTimestamp, destMailboxId) {
const url = generateUrl('/apps/mail/api/messages/{id}/snooze', {
id,
})

return axios.post(url, {
unixTimestamp,
destMailboxId,
})
}

Expand Down
Loading

0 comments on commit 9c87de5

Please sign in to comment.