Skip to content

Commit

Permalink
Add a cache for IMAP message in the database
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Dec 6, 2019
1 parent e7221b1 commit 1b8a31a
Show file tree
Hide file tree
Showing 54 changed files with 2,226 additions and 805 deletions.
2 changes: 2 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
<repair-steps>
<post-migration>
<step>OCA\Mail\Migration\FixCollectedAddresses</step>
<step>OCA\Mail\Migration\FixAccountSyncs</step>
<step>OCA\Mail\Migration\MigrateProvisioningConfig</step>
<step>OCA\Mail\Migration\ProvisionAccounts</step>
</post-migration>
</repair-steps>
<commands>
<command>OCA\Mail\Command\CreateAccount</command>
<command>OCA\Mail\Command\ExportAccount</command>
<command>OCA\Mail\Command\SyncAccount</command>
</commands>
<settings>
<admin>OCA\Mail\Settings\AdminSettings</admin>
Expand Down
7 changes: 6 additions & 1 deletion lib/Address.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
declare(strict_types=1);

/**
Expand Down Expand Up @@ -30,6 +30,11 @@

class Address implements JsonSerializable {

public const TYPE_FROM = 0;
public const TYPE_TO = 1;
public const TYPE_CC = 2;
public const TYPE_BCC = 3;

/** @var Horde_Mail_Rfc822_Address */
private $wrapped;

Expand Down
8 changes: 7 additions & 1 deletion lib/AddressList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/**
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
Expand Down Expand Up @@ -69,6 +69,12 @@ public static function fromHorde(Horde_Mail_Rfc822_List $hordeList) {
return new AddressList($addresses);
}

public static function fromRow(array $recipient): self {
return new self([
new Address($recipient['label'], $recipient['email'])
]);
}

/**
* Get first element
*
Expand Down
2 changes: 1 addition & 1 deletion lib/AppInfo/BootstrapSingleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
use OCA\Mail\Service\Group\IGroupService;
use OCA\Mail\Service\Group\NextcloudGroupService;
use OCA\Mail\Service\MailManager;
use OCA\Mail\Service\MailSearch;
use OCA\Mail\Service\Search\MailSearch;
use OCA\Mail\Service\MailTransmission;
use OCA\Mail\Service\UserPreferenceSevice;
use OCP\AppFramework\IAppContainer;
Expand Down
78 changes: 78 additions & 0 deletions lib/BackgroundJob/SyncJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\SyncService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\ILogger;

class SyncJob extends TimedJob {

/** @var AccountService */
private $accountService;
/** @var SyncService */
private $syncService;
/** @var ILogger */
private $logger;
/** @var IJobList */
private $jobList;

public function __construct(ITimeFactory $time,
AccountService $accountService,
SyncService $syncService,
ILogger $logger,
IJobList $jobList) {
parent::__construct($time);

$this->accountService = $accountService;
$this->syncService = $syncService;
$this->logger = $logger;

$this->setInterval(3600);
$this->jobList = $jobList;
}

protected function run($argument) {
$accountId = (int)$argument['accountId'];

try {
$account = $this->accountService->findById($accountId);
} catch (DoesNotExistException $e) {
$this->logger->debug('Could not find account <' . $accountId . '> removing from jobs');
$this->jobList->remove(self::class, $argument);
return;
}

try {
$this->syncService->syncAccount($account);
} catch (\Exception $e) {
$this->logger->logException($e);
}
}

}
73 changes: 73 additions & 0 deletions lib/Command/SyncAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Mail\Command;

use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\SyncService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SyncAccount extends Command {

const ARGUMENT_ACCOUNT_ID = 'account-id';
const OPTION_FORCE = 'force';

/** @var AccountService */
private $accountService;

/** @var MailboxMapper */
private $mailboxMapper;

/** @var SyncService */
private $syncService;

public function __construct(AccountService $service,
MailboxMapper $mailboxMapper,
SyncService $syncService) {
parent::__construct();

$this->accountService = $service;
$this->mailboxMapper = $mailboxMapper;
$this->syncService = $syncService;
}

protected function configure() {
$this->setName('mail:account:sync');
$this->setDescription('Synchronize an IMAP account');
$this->addArgument(self::ARGUMENT_ACCOUNT_ID, InputArgument::REQUIRED);
$this->addOption(self::OPTION_FORCE, 'f', InputOption::VALUE_NONE);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$accountId = (int)$input->getArgument(self::ARGUMENT_ACCOUNT_ID);
$force = $input->getOption(self::OPTION_FORCE);

$account = $this->accountService->findById($accountId);
$this->syncService->syncAccount($account, $force);
}
}
11 changes: 0 additions & 11 deletions lib/Contracts/IMailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ public function getFolderStats(Account $account, string $folderId): FolderStats;
*/
public function getMessage(Account $account, string $mailbox, int $id, bool $loadBody = false): IMAPMessage;

/**
* @param Account
* @param SyncRequest $syncRequest
*
* @return SyncResponse
*
* @throws ClientException
* @throws ServiceException
*/
public function syncMessages(Account $account, SyncRequest $syncRequest): SyncResponse;

/**
* @param Account $sourceAccount
* @param string $sourceFolderId
Expand Down
35 changes: 30 additions & 5 deletions lib/Controller/FoldersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

namespace OCA\Mail\Controller;

use Horde_Imap_Client;
use OCA\Mail\Exception\MailboxNotCachedException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Service\SyncService;
use function base64_decode;
use function is_array;
use OCA\Mail\Contracts\IMailManager;
Expand All @@ -47,20 +51,28 @@ class FoldersController extends Controller {
/** @var IMailManager */
private $mailManager;

/** @var SyncService */
private $syncService;

/**
* @param string $appName
* @param IRequest $request
* @param AccountService $accountService
* @param string $UserId
* @param IMailManager $mailManager
*/
public function __construct(string $appName, IRequest $request,
AccountService $accountService, $UserId, IMailManager $mailManager) {
public function __construct(string $appName,
IRequest $request,
AccountService $accountService,
$UserId,
IMailManager $mailManager,
SyncService $syncService) {
parent::__construct($appName, $request);

$this->accountService = $accountService;
$this->currentUserId = $UserId;
$this->mailManager = $mailManager;
$this->syncService = $syncService;
}

/**
Expand Down Expand Up @@ -91,15 +103,28 @@ public function index(int $accountId): JSONResponse {
* @param string $syncToken
* @param int[] $uids
* @return JSONResponse
* @throws ServiceException
*/
public function sync(int $accountId, string $folderId, string $syncToken, array $uids = []): JSONResponse {
public function sync(int $accountId, string $folderId, array $uids): JSONResponse {
$account = $this->accountService->find($this->currentUserId, $accountId);

if (empty($accountId) || empty($folderId) || empty($syncToken) || !is_array($uids)) {
if (empty($accountId) || empty($folderId) || !is_array($uids)) {
return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
}

$syncResponse = $this->mailManager->syncMessages($account, new SyncRequest(base64_decode($folderId), $syncToken, $uids));
try {
$syncResponse = $this->syncService->syncMailbox(
$account,
base64_decode($folderId),
Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
array_map(function($uid) {
return (int) $uid;
}, $uids),
true
);
} catch (MailboxNotCachedException $e) {
return new JSONResponse(null, Http::STATUS_PRECONDITION_REQUIRED);
}

return new JSONResponse($syncResponse);
}
Expand Down
12 changes: 11 additions & 1 deletion lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
namespace OCA\Mail\Controller;

use Exception;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
use OCA\Mail\Exception\ServiceException;
Expand All @@ -40,6 +39,7 @@
use OCA\Mail\Model\IMAPMessage;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\IMailBox;
use OCA\Mail\Service\SyncService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -67,6 +67,9 @@ class MessagesController extends Controller {
/** @var IMailSearch */
private $mailSearch;

/** @var SyncService */
private $syncService;

/** @var string */
private $currentUserId;

Expand Down Expand Up @@ -101,6 +104,7 @@ public function __construct(string $appName,
AccountService $accountService,
IMailManager $mailManager,
IMailSearch $mailSearch,
SyncService $syncService,
string $UserId,
$userFolder,
ILogger $logger,
Expand All @@ -112,6 +116,7 @@ public function __construct(string $appName,
$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->mailSearch = $mailSearch;
$this->syncService = $syncService;
$this->currentUserId = $UserId;
$this->userFolder = $userFolder;
$this->logger = $logger;
Expand Down Expand Up @@ -140,6 +145,11 @@ public function index(int $accountId, string $folderId, int $cursor = null, stri
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
}

$this->syncService->ensurePopulated(
$account,
base64_decode($folderId)
);

$this->logger->debug("loading messages of folder <$folderId>");

return new JSONResponse(
Expand Down
26 changes: 26 additions & 0 deletions lib/Db/MailAccountMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public function find(string $userId, int $accountId): MailAccount {
return $this->findEntity($query);
}

/**
* Finds an mail account by id
*
* @param int $id
*
* @return MailAccount
*/
public function findById(int $id): MailAccount {
$qb = $this->db->getQueryBuilder();
$query = $qb
->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));

return $this->findEntity($query);
}

/**
* Finds all Mail Accounts by user id existing for this user
*
Expand Down Expand Up @@ -118,4 +135,13 @@ public function deleteProvisionedAccounts(): void {
$delete->execute();
}

public function getAllAccounts(): array {
$qb = $this->db->getQueryBuilder();
$query = $qb
->select('*')
->from($this->getTableName());

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

}
Loading

0 comments on commit 1b8a31a

Please sign in to comment.