-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9567 from nextcloud/feat/follow-up-reminder
feat: follow up reminders
- Loading branch information
Showing
32 changed files
with
1,495 additions
and
38 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,106 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
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 OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\QueuedJob; | ||
use OCP\DB\Exception; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class FollowUpClassifierJob extends QueuedJob { | ||
|
||
public const PARAM_MESSAGE_ID = 'messageId'; | ||
public const PARAM_MAILBOX_ID = 'mailboxId'; | ||
public const PARAM_USER_ID = 'userId'; | ||
|
||
public function __construct( | ||
ITimeFactory $time, | ||
private LoggerInterface $logger, | ||
private AccountService $accountService, | ||
private IMailManager $mailManager, | ||
private AiIntegrationsService $aiService, | ||
private ThreadMapper $threadMapper, | ||
) { | ||
parent::__construct($time); | ||
} | ||
|
||
public function run($argument): void { | ||
$messageId = $argument[self::PARAM_MESSAGE_ID]; | ||
$mailboxId = $argument[self::PARAM_MAILBOX_ID]; | ||
$userId = $argument[self::PARAM_USER_ID]; | ||
|
||
if (!$this->aiService->isLlmProcessingEnabled()) { | ||
return; | ||
} | ||
|
||
try { | ||
$mailbox = $this->mailManager->getMailbox($userId, $mailboxId); | ||
$account = $this->accountService->find($userId, $mailbox->getAccountId()); | ||
} catch (ClientException $e) { | ||
return; | ||
} | ||
|
||
$messages = $this->mailManager->getByMessageId($account, $messageId); | ||
$messages = array_filter( | ||
$messages, | ||
static fn (Message $message) => $message->getMailboxId() === $mailboxId, | ||
); | ||
if (count($messages) === 0) { | ||
return; | ||
} | ||
|
||
if (count($messages) > 1) { | ||
$this->logger->warning('Trying to analyze multiple messages with the same message id for follow-ups'); | ||
} | ||
$message = $messages[0]; | ||
|
||
try { | ||
$newerMessages = $this->threadMapper->findNewerMessageIdsInThread( | ||
$mailbox->getAccountId(), | ||
$message, | ||
); | ||
} catch (Exception $e) { | ||
$this->logger->error( | ||
'Failed to check if a message needs a follow-up: ' . $e->getMessage(), | ||
[ 'exception' => $e ], | ||
); | ||
return; | ||
} | ||
if (count($newerMessages) > 0) { | ||
return; | ||
} | ||
|
||
$requiresFollowup = $this->aiService->requiresFollowUp( | ||
$account, | ||
$mailbox, | ||
$message, | ||
$userId, | ||
); | ||
if (!$requiresFollowup) { | ||
return; | ||
} | ||
|
||
$this->logger->debug('Message requires follow-up: ' . $message->getId()); | ||
$tag = $this->mailManager->createTag('Follow up', '#d77000', $userId); | ||
$this->mailManager->tagMessage( | ||
$account, | ||
$mailbox->getName(), | ||
$message, | ||
$tag, | ||
true, | ||
); | ||
} | ||
} |
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Controller; | ||
|
||
use OCA\Mail\Db\MailboxMapper; | ||
use OCA\Mail\Db\MessageMapper; | ||
use OCA\Mail\Db\ThreadMapper; | ||
use OCA\Mail\Http\JsonResponse; | ||
use OCA\Mail\Http\TrapError; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\IRequest; | ||
|
||
class FollowUpController extends Controller { | ||
|
||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private ?string $userId, | ||
private ThreadMapper $threadMapper, | ||
private MessageMapper $messageMapper, | ||
private MailboxMapper $mailboxMapper, | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
/** | ||
* @param int[] $messageIds | ||
*/ | ||
#[TrapError] | ||
#[NoAdminRequired] | ||
public function checkMessageIds(array $messageIds): JsonResponse { | ||
$userId = $this->userId; | ||
if ($userId === null) { | ||
return JsonResponse::fail([], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$mailboxes = []; | ||
|
||
$wasFollowedUp = []; | ||
$messages = $this->messageMapper->findByIds($userId, $messageIds, 'ASC'); | ||
foreach ($messages as $message) { | ||
$mailboxId = $message->getMailboxId(); | ||
if (!isset($mailboxes[$mailboxId])) { | ||
try { | ||
$mailboxes[$mailboxId] = $this->mailboxMapper->findByUid($mailboxId, $userId); | ||
} catch (DoesNotExistException $e) { | ||
continue; | ||
} | ||
} | ||
|
||
$newerMessageIds = $this->threadMapper->findNewerMessageIdsInThread( | ||
$mailboxes[$mailboxId]->getAccountId(), | ||
$message, | ||
); | ||
if (!empty($newerMessageIds)) { | ||
$wasFollowedUp[] = $message->getId(); | ||
} | ||
} | ||
|
||
return JsonResponse::success([ | ||
'wasFollowedUp' => $wasFollowedUp, | ||
]); | ||
} | ||
|
||
} |
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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Listener; | ||
|
||
use DateInterval; | ||
use DateTimeImmutable; | ||
use OCA\Mail\BackgroundJob\FollowUpClassifierJob; | ||
use OCA\Mail\Events\NewMessagesSynchronized; | ||
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\TextProcessing\FreePromptTaskType; | ||
|
||
/** | ||
* @template-implements IEventListener<Event|NewMessagesSynchronized> | ||
*/ | ||
class FollowUpClassifierListener implements IEventListener { | ||
|
||
public function __construct( | ||
private IJobList $jobList, | ||
private AiIntegrationsService $aiService, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof NewMessagesSynchronized)) { | ||
return; | ||
} | ||
|
||
if (!$event->getMailbox()->isSpecialUse('sent') | ||
&& $event->getAccount()->getMailAccount()->getSentMailboxId() !== $event->getMailbox()->getId() | ||
) { | ||
return; | ||
} | ||
|
||
if (!$this->aiService->isLlmProcessingEnabled()) { | ||
return; | ||
} | ||
|
||
if (!$this->aiService->isLlmAvailable(FreePromptTaskType::class)) { | ||
return; | ||
} | ||
|
||
// Do not process emails older than 14D to save some processing power | ||
$notBefore = (new DateTimeImmutable('now')) | ||
->sub(new DateInterval('P14D')); | ||
$userId = $event->getAccount()->getUserId(); | ||
foreach ($event->getMessages() as $message) { | ||
if ($message->getSentAt() < $notBefore->getTimestamp()) { | ||
continue; | ||
} | ||
|
||
$isTagged = false; | ||
foreach ($message->getTags() as $tag) { | ||
if ($tag->getImapLabel() === '$follow_up') { | ||
$isTagged = true; | ||
break; | ||
} | ||
} | ||
if ($isTagged) { | ||
continue; | ||
} | ||
|
||
$jobArguments = [ | ||
FollowUpClassifierJob::PARAM_MESSAGE_ID => $message->getMessageId(), | ||
FollowUpClassifierJob::PARAM_MAILBOX_ID => $message->getMailboxId(), | ||
FollowUpClassifierJob::PARAM_USER_ID => $userId, | ||
]; | ||
// TODO: only use scheduleAfter() once we support >= 28.0.0 | ||
if (method_exists(IJobList::class, 'scheduleAfter')) { | ||
// Delay job a bit because there might be some replies until then and we might be able | ||
// to skip the expensive LLM task | ||
$timestamp = (new DateTimeImmutable('@' . $message->getSentAt())) | ||
->add(new DateInterval('P3DT12H')) | ||
->getTimestamp(); | ||
$this->jobList->scheduleAfter( | ||
FollowUpClassifierJob::class, | ||
$timestamp, | ||
$jobArguments, | ||
); | ||
} else { | ||
$this->jobList->add(FollowUpClassifierJob::class, $jobArguments); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.