Skip to content

feat: add snooze mvp #8694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Positive:

Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud.com/blog/nextcloud-ethical-ai-rating/).
]]></description>
<version>3.4.0-beta.1</version>
<version>3.4.0-beta.2</version>
<licence>agpl</licence>
<author>Greta Doçi</author>
<author homepage="https://github.com/nextcloud/groupware">Nextcloud Groupware Team</author>
Expand All @@ -48,6 +48,7 @@ Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud
<job>OCA\Mail\BackgroundJob\IMipMessageJob</job>
<job>OCA\Mail\BackgroundJob\DraftsJob</job>
<job>OCA\Mail\BackgroundJob\TrashRetentionJob</job>
<job>OCA\Mail\BackgroundJob\WakeJob</job>
</background-jobs>
<repair-steps>
<post-migration>
Expand Down
10 changes: 10 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@
'url' => '/api/messages/{id}/move',
'verb' => 'POST'
],
[
'name' => 'messages#snooze',
'url' => '/api/messages/{id}/snooze',
'verb' => 'POST'
],
[
'name' => 'messages#mdn',
'url' => '/api/messages/{id}/mdn',
Expand Down Expand Up @@ -365,6 +370,11 @@
'url' => '/api/thread/{id}',
'verb' => 'POST'
],
[
'name' => 'thread#snooze',
'url' => '/api/thread/{id}/snooze',
'verb' => 'POST'
],
[
'name' => 'thread#summarize',
'url' => '/api/thread/{id}/summary',
Expand Down
50 changes: 50 additions & 0 deletions lib/BackgroundJob/WakeJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Johannes Merkel <mail@johannesgge.de>
*
* @author Johannes Merkel <mail@johannesgge.de>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Service\SnoozeService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;

class WakeJob extends TimedJob {
public function __construct(
ITimeFactory $time,
private SnoozeService $snoozeService,
) {
parent::__construct($time);

$this->setInterval(60);
$this->setTimeSensitivity(self::TIME_SENSITIVE);
}

/**
* @inheritDoc
*/
public function run($argument): void {
$this->snoozeService->wakeMessages();
}
}
6 changes: 6 additions & 0 deletions lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function update(int $id,
* @param int|null $sentMailboxId
* @param int|null $trashMailboxId
* @param int|null $archiveMailboxId
* @param int|null $snoozeMailboxId
* @param bool|null $signatureAboveQuote
*
* @return JSONResponse
Expand All @@ -241,6 +242,7 @@ public function patchAccount(int $id,
int $sentMailboxId = null,
int $trashMailboxId = null,
int $archiveMailboxId = null,
int $snoozeMailboxId = null,
bool $signatureAboveQuote = null,
int $trashRetentionDays = null,
int $junkMailboxId = null,
Expand All @@ -265,6 +267,10 @@ public function patchAccount(int $id,
$this->mailManager->getMailbox($this->currentUserId, $archiveMailboxId);
$dbAccount->setarchiveMailboxId($archiveMailboxId);
}
if ($snoozeMailboxId !== null) {
$this->mailManager->getMailbox($this->currentUserId, $snoozeMailboxId);
$dbAccount->setSnoozeMailboxId($snoozeMailboxId);
}
if ($editorMode !== null) {
$dbAccount->setEditorMode($editorMode);
}
Expand Down
30 changes: 29 additions & 1 deletion lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\ItineraryService;
use OCA\Mail\Service\SmimeService;
use OCA\Mail\Service\SnoozeService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -86,6 +87,7 @@ class MessagesController extends Controller {
private SmimeService $smimeService;
private IMAPClientFactory $clientFactory;
private IDkimService $dkimService;
private SnoozeService $snoozeService;

public function __construct(string $appName,
IRequest $request,
Expand All @@ -104,7 +106,8 @@ public function __construct(string $appName,
IMailTransmission $mailTransmission,
SmimeService $smimeService,
IMAPClientFactory $clientFactory,
IDkimService $dkimService) {
IDkimService $dkimService,
SnoozeService $snoozeService) {
parent::__construct($appName, $request);
$this->accountService = $accountService;
$this->mailManager = $mailManager;
Expand All @@ -122,6 +125,7 @@ public function __construct(string $appName,
$this->smimeService = $smimeService;
$this->clientFactory = $clientFactory;
$this->dkimService = $dkimService;
$this->snoozeService = $snoozeService;
}

/**
Expand Down Expand Up @@ -378,6 +382,30 @@ public function move(int $id, int $destFolderId): JSONResponse {
return new JSONResponse();
}

/**
* Adds a DB Entry for the message with a wake timestamp
* Moving the message is done in a separate request
*
* @NoAdminRequired
*
* @param int $id
* @param int $unixTimestamp
*
* @return JSONResponse
*/
#[TrapError]
public function snooze(int $id, int $unixTimestamp): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$this->snoozeService->snoozeMessage($message, $unixTimestamp);

return new JSONResponse();
}

/**
* @NoAdminRequired
*
Expand Down
49 changes: 38 additions & 11 deletions lib/Controller/ThreadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AiIntegrationsService;
use OCA\Mail\Service\SnoozeService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand All @@ -40,6 +41,7 @@ class ThreadController extends Controller {
private string $currentUserId;
private AccountService $accountService;
private IMailManager $mailManager;
private SnoozeService $snoozeService;
private AiIntegrationsService $aiIntergrationsService;
private LoggerInterface $logger;

Expand All @@ -49,12 +51,14 @@ public function __construct(string $appName,
string $UserId,
AccountService $accountService,
IMailManager $mailManager,
SnoozeService $snoozeService,
AiIntegrationsService $aiIntergrationsService,
LoggerInterface $logger) {
parent::__construct($appName, $request);
$this->currentUserId = $UserId;
$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->snoozeService = $snoozeService;
$this->aiIntergrationsService = $aiIntergrationsService;
$this->logger = $logger;
}
Expand Down Expand Up @@ -93,29 +97,25 @@ public function move(int $id, int $destMailboxId): JSONResponse {
}

/**
* Adds a DB Entry for the messages with a wake timestamp
* Moving the messages is done in a separate request
*
* @NoAdminRequired
*
* @param int $id
* @param int $unixTimestamp
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function delete(int $id): JSONResponse {
public function snooze(int $id, int $unixTimestamp): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
$selectedMessage = $this->mailManager->getMessage($this->currentUserId, $id);
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$this->mailManager->deleteThread(
$account,
$mailbox,
$message->getThreadRootId()
);
$this->snoozeService->snoozeThread($selectedMessage, $unixTimestamp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty cool that a whole thread can be snoozed as well :)


return new JSONResponse();
}
Expand Down Expand Up @@ -155,4 +155,31 @@ public function summarize(int $id): JSONResponse {
return new JSONResponse(['data' => $summary]);
}

/**
* @NoAdminRequired
*
* @param int $id
*
* @return JSONResponse
* @throws ClientException
* @throws ServiceException
*/
#[TrapError]
public function delete(int $id): JSONResponse {
try {
$message = $this->mailManager->getMessage($this->currentUserId, $id);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$this->mailManager->deleteThread(
$account,
$mailbox,
$message->getThreadRootId()
);

return new JSONResponse();
}
}
7 changes: 7 additions & 0 deletions lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
* @method int|null getTrashMailboxId()
* @method void setArchiveMailboxId(?int $id)
* @method int|null getArchiveMailboxId()
* @method void setSnoozeMailboxId(?int $id)
* @method int|null getSnoozeMailboxId()
* @method bool|null isSieveEnabled()
* @method void setSieveEnabled(bool $sieveEnabled)
* @method string|null getSieveHost()
Expand Down Expand Up @@ -155,6 +157,9 @@ class MailAccount extends Entity {
/** @var int|null */
protected $archiveMailboxId;

/** @var int|null */
protected $snoozeMailboxId;

/** @var bool */
protected $sieveEnabled = false;
/** @var string|null */
Expand Down Expand Up @@ -254,6 +259,7 @@ public function __construct(array $params = []) {
$this->addType('sentMailboxId', 'integer');
$this->addType('trashMailboxId', 'integer');
$this->addType('archiveMailboxId', 'integer');
$this->addType('snoozeMailboxId', 'integer');
$this->addType('sieveEnabled', 'boolean');
$this->addType('sievePort', 'integer');
$this->addType('signatureAboveQuote', 'boolean');
Expand Down Expand Up @@ -288,6 +294,7 @@ public function toJson() {
'sentMailboxId' => $this->getSentMailboxId(),
'trashMailboxId' => $this->getTrashMailboxId(),
'archiveMailboxId' => $this->getArchiveMailboxId(),
'snoozeMailboxId' => $this->getSnoozeMailboxId(),
'sieveEnabled' => ($this->isSieveEnabled() === true),
'signatureAboveQuote' => ($this->isSignatureAboveQuote() === true),
'signatureMode' => $this->getSignatureMode(),
Expand Down
34 changes: 34 additions & 0 deletions lib/Db/MessageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1447,4 +1447,38 @@ public function findMessagesKnownSinceBefore(int $mailboxId, int $before): array

return $this->findEntities($select);
}

/**
* Finds snoozed messages that are ready to wake since $time
*
* @param int $mailboxId
* @param int $time UNIX timestamp (seconds)
*
* @return Message[]
*/
public function findMessagesToUnSnooze(int $mailboxId, int $time): array {
$qb = $this->db->getQueryBuilder();

$select = $qb->select('m.*')
->from($this->getTableName(), 'm')
->join('m', 'mail_messages_snoozed', 'mr', $qb->expr()->eq(
'm.message_id',
'mr.message_id',
IQueryBuilder::PARAM_STR,
))
->where(
$qb->expr()->eq(
'm.mailbox_id',
$qb->createNamedParameter($mailboxId, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
),
$qb->expr()->lt(
'mr.snoozed_until',
$qb->createNamedParameter($time, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
),
);

return $this->findEntities($select);
}
}
Loading