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 16, 2023
1 parent 9218afb commit bde3b91
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 35 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
119 changes: 115 additions & 4 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,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 {
Expand Down Expand Up @@ -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
Expand All @@ -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());
}

/**
Expand All @@ -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()
);
Expand All @@ -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']);
}
}

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 @@ -621,21 +621,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 @@ -760,6 +762,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 @@ -784,9 +790,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 @@ -494,14 +494,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
5 changes: 2 additions & 3 deletions src/service/ThreadService.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ export async function moveThread(id, destMailboxId) {
}
}

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

try {
return await axios.post(url, { unixTimestamp })
return await axios.post(url, { unixTimestamp, destMailboxId })
} catch (e) {
throw convertAxiosError(e)
}
Expand Down
20 changes: 14 additions & 6 deletions src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,11 @@ export default {
commit('removeMessage', { id })
})
},
// Only adds DB entry, moving the message is done in a separate request
async snoozeMessage({ commit }, { id, unixTimestamp }) {
async snoozeMessage({ commit }, { id, unixTimestamp, destMailboxId }) {
return handleHttpAuthErrors(commit, async () => {
await snoozeMessage(id, unixTimestamp)
await snoozeMessage(id, unixTimestamp, destMailboxId)
commit('removeEnvelope', { id })
commit('removeMessage', { id })
})
},
async fetchActiveSieveScript({ commit }, { accountId }) {
Expand Down Expand Up @@ -1281,10 +1282,17 @@ export default {
}
})
},
// Only adds DB entry, moving the messages is done in a separate request
async snoozeThread({ getters, commit }, { envelope, unixTimestamp }) {
async snoozeThread({ getters, commit }, { envelope, unixTimestamp, destMailboxId }) {
return handleHttpAuthErrors(commit, async () => {
await ThreadService.snoozeThread(envelope.databaseId, unixTimestamp)
try {
await ThreadService.snoozeThread(envelope.databaseId, unixTimestamp, destMailboxId)
console.debug('thread snoozed')
} catch (e) {
commit('addEnvelope', envelope)
console.error('could not snooze thread', e)
throw e
}
commit('removeEnvelope', { id: envelope.databaseId })
})
},

Expand Down

0 comments on commit bde3b91

Please sign in to comment.