-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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 OCA\Mail\Contracts\IMailManager; | ||
use OCA\Mail\Events\NewMessagesSynchronized; | ||
use OCA\Mail\Exception\ServiceException; | ||
use OCA\Mail\IMAP\IMAPClientFactory; | ||
use OCA\Mail\Service\AiIntegrations\AiIntegrationsService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @template-implements IEventListener<Event|NewMessagesSummarizeListener> | ||
*/ | ||
class NewMessagesSummarizeListener implements IEventListener { | ||
Check failure on line 24 in lib/Listener/NewMessagesSummarizeListener.php GitHub Actions / static-psalm-analysis dev-masterInvalidTemplateParam
|
||
|
||
public function __construct( | ||
protected LoggerInterface $logger, | ||
protected IMAPClientFactory $imapFactory, | ||
protected AiIntegrationsService $aiService, | ||
protected IMailManager $mailManager | ||
) { } | ||
|
||
public function handle(Event $event): void { | ||
Check failure on line 33 in lib/Listener/NewMessagesSummarizeListener.php GitHub Actions / static-psalm-analysis dev-masterMoreSpecificImplementedParamType
|
||
|
||
if (!($event instanceof NewMessagesSynchronized)) { | ||
return; | ||
} | ||
|
||
try { | ||
$this->aiService->summarizeMessages( | ||
$event->getAccount(), | ||
$event->getMessages(), | ||
); | ||
} catch (ServiceException $e) { | ||
$this->logger->error('Could not classify incoming message importance: ' . $e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?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 OCA\Mail\AppInfo\Application; | ||
use OCA\Mail\Db\MessageMapper; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\TaskProcessing\Events\TaskSuccessfulEvent; | ||
use OCP\TaskProcessing\TaskTypes\TextToTextSummary; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @template-implements IEventListener<Event|NewMessagesSummarizeListener> | ||
*/ | ||
class TaskProcessingListener implements IEventListener { | ||
Check failure on line 23 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterInvalidTemplateParam
|
||
|
||
public function __construct( | ||
protected LoggerInterface $logger, | ||
protected MessageMapper $messageStore, | ||
) { } | ||
|
||
public function handle(Event $event): void { | ||
Check failure on line 30 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterMoreSpecificImplementedParamType
|
||
|
||
if (!($event instanceof TaskSuccessfulEvent)) { | ||
return; | ||
} | ||
|
||
$task = $event->getTask(); | ||
|
||
if ($task->getAppId() !== Application::APP_ID) { | ||
return; | ||
} | ||
|
||
if ($task->getTaskTypeId() !== TextToTextSummary::ID) { | ||
return; | ||
} | ||
|
||
list($type, $id) = explode(':', $task->getCustomId()); | ||
Check failure on line 46 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
|
||
$userId = $task->getUserId(); | ||
$summary = $task->getOutput()['output']; | ||
Check failure on line 48 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArrayAccess
|
||
|
||
match ($type) { | ||
'message' => $this->handleMessageSummary($userId, (int)$id, $summary), | ||
Check failure on line 51 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterPossiblyNullArgument
Check failure on line 51 in lib/Listener/TaskProcessingListener.php GitHub Actions / static-psalm-analysis dev-masterPossiblyInvalidArgument
|
||
}; | ||
|
||
} | ||
|
||
protected function handleMessageSummary(string $userId, int $id, string $summary) { | ||
$messages = $this->messageStore->findByIds($userId, [$id], ''); | ||
|
||
if (count($messages) !== 1) { | ||
return; | ||
} | ||
|
||
$message = $messages[0]; | ||
$message->setSummary($summary); | ||
$this->messageStore->update($message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Mail\Migration; | ||
|
||
use Closure; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\Types; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
|
||
class Version4100Date20241209000000 extends SimpleMigrationStep { | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure(): ISchemaWrapper $schemaClosure | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
$schema = $schemaClosure(); | ||
|
||
$outboxTable = $schema->getTable('mail_messages'); | ||
if (!$outboxTable->hasColumn('summary')) { | ||
$outboxTable->addColumn('summary', Types::STRING, [ | ||
'length' => 1024, | ||
'notnull' => false, | ||
]); | ||
} | ||
return $schema; | ||
} | ||
} |