Skip to content
Draft
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
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
'verb' => 'GET'
],
[
'name' => 'messages#getSource',
'name' => 'messages#getRawMessage',
'url' => '/api/messages/{id}/source',
'verb' => 'GET'
],
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"optimize-autoloader": true,
"autoloader-suffix": "Mail"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/sebastiankrupinski/jmap-client-php"
}
],
"require": {
"php": ">=8.1 <=8.4",
"ext-openssl": "*",
Expand Down
2 changes: 2 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use OCA\Mail\Contracts\IAvatarService;
use OCA\Mail\Contracts\IDkimService;
use OCA\Mail\Contracts\IDkimValidator;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Contracts\IMailSearch;
use OCA\Mail\Contracts\IMailTransmission;
use OCA\Mail\Contracts\ITrustedSenderService;
Expand Down Expand Up @@ -62,7 +61,6 @@
use OCA\Mail\Service\AvatarService;
use OCA\Mail\Service\DkimService;
use OCA\Mail\Service\DkimValidator;
use OCA\Mail\Service\MailManager;
use OCA\Mail\Service\MailTransmission;
use OCA\Mail\Service\Search\MailSearch;
use OCA\Mail\Service\TrustedSenderService;
Expand Down Expand Up @@ -120,7 +118,6 @@ public function register(IRegistrationContext $context): void {

$context->registerServiceAlias(IAvatarService::class, AvatarService::class);
$context->registerServiceAlias(IAttachmentService::class, AttachmentService::class);
$context->registerServiceAlias(IMailManager::class, MailManager::class);
$context->registerServiceAlias(IMailSearch::class, MailSearch::class);
$context->registerServiceAlias(IMailTransmission::class, MailTransmission::class);
$context->registerServiceAlias(ITrustedSenderService::class, TrustedSenderService::class);
Expand Down
74 changes: 30 additions & 44 deletions lib/BackgroundJob/ContextChat/SubmitContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Exception\SmimeDecryptException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\ContextChat\TaskService;
use OCA\Mail\Service\MailManager;
Expand All @@ -36,7 +35,6 @@ public function __construct(
private AccountService $accountService,
private MailManager $mailManager,
private MessageMapper $messageMapper,
private IMAPClientFactory $clientFactory,
private ContextChatProvider $contextChatProvider,
private IContentManager $contentManager,
private LoggerInterface $logger,
Expand Down Expand Up @@ -109,53 +107,41 @@ protected function run($argument): void {
}


$client = $this->clientFactory->getClient($account);
$items = [];

try {
$startTime = $this->time->getTime();
foreach ($messages as $message) {
if ($this->time->getTime() - $startTime > ContextChatProvider::CONTEXT_CHAT_JOB_INTERVAL) {
break;
}
try {
$imapMessage = $this->mailManager->getImapMessage($client, $account, $mailbox, $message->getUid(), true);
} catch (ServiceException $e) {
// couldn't load message, let's skip it. Retrying would be too costly
continue;
} catch (SmimeDecryptException $e) {
// encryption problem, skip this message
continue;
}


// Skip encrypted messages
if ($imapMessage->isEncrypted()) {
continue;
}


$fullMessage = $imapMessage->getFullMessage($imapMessage->getUid(), true);


$items[] = new ContentItem(
"{$mailbox->getId()}:{$message->getId()}",
$this->contextChatProvider->getId(),
$imapMessage->getSubject(),
$fullMessage['body'] ?? '',
'E-Mail',
$imapMessage->getSentDate(),
[$account->getUserId()],
);
$startTime = $this->time->getTime();
foreach ($messages as $message) {
if ($this->time->getTime() - $startTime > ContextChatProvider::CONTEXT_CHAT_JOB_INTERVAL) {
break;
}
} catch (\Throwable $e) {
$this->logger->warning('Exception occurred when trying to fetch messages for context chat', ['exception' => $e]);
} finally {
try {
$client->close();
} catch (\Horde_Imap_Client_Exception $e) {
$this->logger->debug('Failed to close IMAP client', ['exception' => $e]);
$imapMessage = $this->mailManager->getImapMessage($account, $mailbox, $message, true);
} catch (ServiceException $e) {
// couldn't load message, let's skip it. Retrying would be too costly
continue;
} catch (SmimeDecryptException $e) {
// encryption problem, skip this message
continue;
}


// Skip encrypted messages
if ($imapMessage->isEncrypted()) {
continue;
}


$fullMessage = $imapMessage->getFullMessage($imapMessage->getUid(), true);

$items[] = new ContentItem(
"{$mailbox->getId()}:{$message->getId()}",
$this->contextChatProvider->getId(),
$imapMessage->getSubject(),
$fullMessage['body'] ?? '',
'E-Mail',
$imapMessage->getSentDate(),
[$account->getUserId()],
);
}

if (count($items) > 0) {
Expand Down
12 changes: 6 additions & 6 deletions lib/BackgroundJob/FollowUpClassifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\Message;
use OCA\Mail\Db\ThreadMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\DB\Exception;
Expand All @@ -30,7 +30,7 @@ public function __construct(
ITimeFactory $time,
private LoggerInterface $logger,
private AccountService $accountService,
private IMailManager $mailManager,
private MailManager $mailManager,
private AiIntegrationsService $aiService,
private ThreadMapper $threadMapper,
) {
Expand All @@ -54,7 +54,7 @@ public function run($argument): void {
return;
}

$messages = $this->mailManager->getByMessageId($account, $messageId);
$messages = $this->mailManager->getMessagesByMessageId($account, $messageId);
$messages = array_filter(
$messages,
static fn (Message $message) => $message->getMailboxId() === $mailboxId,
Expand Down Expand Up @@ -96,12 +96,12 @@ public function run($argument): void {

$this->logger->debug("Message requires follow-up: {$message->getId()}");
$tag = $this->mailManager->createTag('Follow up', '#d77000', $userId);
$this->mailManager->tagMessage(
$this->mailManager->tagMessages(
$account,
$mailbox->getName(),
$message,
$mailbox,
$tag,
true,
$message,
);
}
}
12 changes: 6 additions & 6 deletions lib/BackgroundJob/MigrateImportantJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use OCA\Mail\Db\MailboxMapper;

use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Migration\MigrateImportantFromImapAndDb;
use OCA\Mail\Protocol\ProtocolFactory;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -28,23 +28,23 @@ class MigrateImportantJob extends QueuedJob {
private MailManager $mailManager;
private MigrateImportantFromImapAndDb $migration;
private LoggerInterface $logger;
private IMAPClientFactory $imapClientFactory;
private ProtocolFactory $protocolFactory;

public function __construct(MailboxMapper $mailboxMapper,
MailAccountMapper $mailAccountMapper,
MailManager $mailManager,
MigrateImportantFromImapAndDb $migration,
LoggerInterface $logger,
ITimeFactory $timeFactory,
IMAPClientFactory $imapClientFactory,
ProtocolFactory $protocolFactory,
) {
parent::__construct($timeFactory);
$this->mailboxMapper = $mailboxMapper;
$this->mailAccountMapper = $mailAccountMapper;
$this->mailManager = $mailManager;
$this->migration = $migration;
$this->logger = $logger;
$this->imapClientFactory = $imapClientFactory;
$this->protocolFactory = $protocolFactory;
}

/**
Expand All @@ -71,10 +71,10 @@ public function run($argument) {
}

$account = new Account($mailAccount);
$client = $this->imapClientFactory->getClient($account);
$client = $this->protocolFactory->imapClient($account);

try {
if ($this->mailManager->isPermflagsEnabled($client, $account, $mailbox->getName()) === false) {
if ($this->mailManager->isPermflagsEnabled($account, $mailbox) === false) {
$this->logger->debug("Permflags not enabled for <{$accountId}>");
return;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/BackgroundJob/QuotaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
Expand All @@ -22,15 +22,15 @@
class QuotaJob extends TimedJob {
private IUserManager $userManager;
private AccountService $accountService;
private IMailManager $mailManager;
private MailManager $mailManager;
private LoggerInterface $logger;
private IJobList $jobList;
private IManager $notificationManager;

public function __construct(ITimeFactory $time,
IUserManager $userManager,
AccountService $accountService,
IMailManager $mailManager,
MailManager $mailManager,
IManager $notificationManager,
LoggerInterface $logger,
IJobList $jobList) {
Expand Down
16 changes: 16 additions & 0 deletions lib/BackgroundJob/RepairSyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace OCA\Mail\BackgroundJob;

use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Events\SynchronizationEvent;
use OCA\Mail\Protocol\ProtocolFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Sync\SyncService;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -25,6 +27,7 @@ class RepairSyncJob extends TimedJob {
public function __construct(
ITimeFactory $time,
private SyncService $syncService,
private ProtocolFactory $protocolFactory,
private AccountService $accountService,
private IUserManager $userManager,
private MailboxMapper $mailboxMapper,
Expand Down Expand Up @@ -65,6 +68,19 @@ protected function run($argument): void {
return;
}

$this->protocolFactory
->mailboxConnector($account)
->syncAll($account, true);

if ($account->getMailAccount()->getProtocol() !== MailAccount::PROTOCOL_IMAP) {
$this->logger->debug(sprintf(
'Account %d uses %s, skipping IMAP repair sync after mailbox refresh',
$account->getId(),
$account->getMailAccount()->getProtocol(),
));
return;
}

$rebuildThreads = false;
$trashMailboxId = $account->getMailAccount()->getTrashMailboxId();
$snoozeMailboxId = $account->getMailAccount()->getSnoozeMailboxId();
Expand Down
22 changes: 11 additions & 11 deletions lib/BackgroundJob/SyncJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

use Horde_Imap_Client_Exception;
use OCA\Mail\AppInfo\Application;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Exception\IncompleteSyncException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\MailboxSync;
use OCA\Mail\Protocol\ProtocolFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Sync\ImapToDbSynchronizer;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
Expand All @@ -31,8 +31,6 @@ class SyncJob extends TimedJob {

private IUserManager $userManager;
private AccountService $accountService;
private ImapToDbSynchronizer $syncService;
private MailboxSync $mailboxSync;
private LoggerInterface $logger;
private IJobList $jobList;
private readonly bool $forcedSyncInterval;
Expand All @@ -41,8 +39,7 @@ public function __construct(
ITimeFactory $time,
IUserManager $userManager,
AccountService $accountService,
MailboxSync $mailboxSync,
ImapToDbSynchronizer $syncService,
private ProtocolFactory $protocolFactory,
LoggerInterface $logger,
IJobList $jobList,
private readonly IConfig $config,
Expand All @@ -51,8 +48,6 @@ public function __construct(

$this->userManager = $userManager;
$this->accountService = $accountService;
$this->syncService = $syncService;
$this->mailboxSync = $mailboxSync;
$this->logger = $logger;
$this->jobList = $jobList;

Expand Down Expand Up @@ -83,7 +78,8 @@ protected function run($argument) {
return;
}

if (!$account->getMailAccount()->canAuthenticateImap()) {
if ($account->getMailAccount()->getProtocol() === MailAccount::PROTOCOL_IMAP
&& !$account->getMailAccount()->canAuthenticateImap()) {
$this->logger->debug('No authentication on IMAP possible, skipping background sync job');
return;
}
Expand Down Expand Up @@ -124,8 +120,12 @@ protected function run($argument) {
}

try {
$this->mailboxSync->sync($account, $this->logger, true);
$this->syncService->syncAccount($account, $this->logger);
$this->protocolFactory
->mailboxConnector($account)
->syncAll($account, true);
$this->protocolFactory
->messageConnector($account)
->syncAll($account, true);
} catch (IncompleteSyncException $e) {
$this->logger->warning($e->getMessage(), [
'exception' => $e,
Expand Down
Loading
Loading