-
Notifications
You must be signed in to change notification settings - Fork 263
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
feat: follow up reminders #9567
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) { | ||
st3iny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we discussed before but there is no way to localize this because the label value is equal to it's displayed text, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily. The
createTag()
method generates a label's value from its display text. Currently, there is no way to specify display name and label value at the same time using this method. I could adjust the API though, e.g. another optional parameter with the label value/id.The label is localized in the frontend (if the user didn't rename it manually).