-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Richard Steinmetz <[email protected]>
- Loading branch information
Showing
22 changed files
with
997 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]> | ||
* | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @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\Account; | ||
use OCA\Mail\Contracts\IMailManager; | ||
use OCA\Mail\Db\MailAccountMapper; | ||
use OCA\Mail\Db\MailboxMapper; | ||
use OCA\Mail\Db\MessageMapper; | ||
use OCA\Mail\Db\MessageRetentionMapper; | ||
use OCA\Mail\Exception\ClientException; | ||
use OCA\Mail\Exception\ServiceException; | ||
use OCA\Mail\IMAP\IMAPClientFactory; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\TimedJob; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class TrashRetentionJob extends TimedJob { | ||
public function __construct( | ||
ITimeFactory $time, | ||
private LoggerInterface $logger, | ||
private IMAPClientFactory $clientFactory, | ||
private MessageMapper $messageMapper, | ||
private MessageRetentionMapper $messageRetentionMapper, | ||
private MailAccountMapper $accountMapper, | ||
private MailboxMapper $mailboxMapper, | ||
private IMailManager $mailManager, | ||
) { | ||
parent::__construct($time); | ||
|
||
$this->setInterval(24 * 3600); | ||
$this->setTimeSensitivity(self::TIME_INSENSITIVE); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function run($argument) { | ||
$accounts = $this->accountMapper->getAllAccounts(); | ||
foreach ($accounts as $account) { | ||
$account = new Account($account); | ||
|
||
$retentionDays = $account->getMailAccount()->getTrashRetentionDays(); | ||
if ($retentionDays === null || $retentionDays <= 0) { | ||
continue; | ||
} | ||
|
||
$retentionSeconds = $retentionDays * 24 * 3600; | ||
|
||
try { | ||
$this->cleanTrash($account, $retentionSeconds); | ||
} catch (ServiceException|ClientException $e) { | ||
$this->logger->error('Could not clean trash mailbox', [ | ||
'exception' => $e, | ||
'userId' => $account->getUserId(), | ||
'accountId' => $account->getId(), | ||
'trashMailboxId' => $account->getMailAccount()->getTrashMailboxId(), | ||
]); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @throws ClientException | ||
* @throws ServiceException | ||
*/ | ||
private function cleanTrash(Account $account, int $retentionSeconds): void { | ||
$trashMailboxId = $account->getMailAccount()->getTrashMailboxId(); | ||
if ($trashMailboxId === null) { | ||
return; | ||
} | ||
|
||
try { | ||
$trashMailbox = $this->mailboxMapper->findById($trashMailboxId); | ||
} catch (DoesNotExistException $e) { | ||
return; | ||
} | ||
|
||
$now = $this->time->getTime(); | ||
$messages = $this->messageMapper->findMessagesKnownSinceBefore( | ||
$trashMailboxId, | ||
$now - $retentionSeconds, | ||
); | ||
|
||
if (count($messages) === 0) { | ||
return; | ||
} | ||
|
||
$processedMessageIds = []; | ||
$client = $this->clientFactory->getClient($account); | ||
try { | ||
foreach ($messages as $message) { | ||
$this->mailManager->deleteMessageWithClient( | ||
$account, | ||
$trashMailbox, | ||
$message->getUid(), | ||
$client, | ||
); | ||
|
||
$messageId = $message->getMessageId(); | ||
if ($messageId !== null) { | ||
$processedMessageIds[] = $messageId; | ||
} | ||
} | ||
} finally { | ||
$client->logout(); | ||
} | ||
|
||
$this->messageRetentionMapper->deleteByMessageIds($processedMessageIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
* @copyright 2019 Christoph Wurst <[email protected]> | ||
* | ||
* @author 2019 Christoph Wurst <[email protected]> | ||
* @author 2023 Richard Steinmetz <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
|
@@ -1414,4 +1415,36 @@ public function getUnanalyzed(int $lastRun, array $mailboxIds): array { | |
|
||
return $this->findEntities($select); | ||
} | ||
|
||
/** | ||
* @param int $mailboxId | ||
* @param int $before UNIX timestamp (seconds) | ||
* | ||
* @return Message[] | ||
*/ | ||
public function findMessagesKnownSinceBefore(int $mailboxId, int $before): array { | ||
$qb = $this->db->getQueryBuilder(); | ||
|
||
$select = $qb->select('m.*') | ||
->from($this->getTableName(), 'm') | ||
->join('m', 'mail_messages_retention', '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.known_since', | ||
$qb->createNamedParameter($before, IQueryBuilder::PARAM_INT), | ||
IQueryBuilder::PARAM_INT, | ||
), | ||
); | ||
|
||
return $this->findEntities($select); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]> | ||
* | ||
* @author Richard Steinmetz <[email protected]> | ||
* | ||
* @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\Db; | ||
|
||
use OCP\AppFramework\Db\Entity; | ||
|
||
/** | ||
* @method void setMessageId(string $messageId) | ||
* @method string getMessageId() | ||
* @method void setKnownSince(int $knownSince) | ||
* @method int getKnownSince() | ||
*/ | ||
class MessageRetention extends Entity { | ||
|
||
/** @var string */ | ||
protected $messageId; | ||
|
||
/** @var int */ | ||
protected $knownSince; | ||
|
||
public function __construct() { | ||
$this->addType('messageId', 'string'); | ||
$this->addType('knownSince', 'integer'); | ||
} | ||
} |
Oops, something went wrong.