-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathFollowUpClassifierJob.php
More file actions
107 lines (92 loc) · 2.6 KB
/
FollowUpClassifierJob.php
File metadata and controls
107 lines (92 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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);
}
#[\Override]
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 ($messages === []) {
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 ($newerMessages !== []) {
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,
);
}
}