Skip to content

Commit 03696cf

Browse files
committed
feat(new-file-menu): use separate listeners for new file menu tasks
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent e3a6095 commit 03696cf

File tree

4 files changed

+124
-36
lines changed

4 files changed

+124
-36
lines changed

lib/AppInfo/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use OCA\Assistant\Listener\FileActionTaskSuccessfulListener;
1616
use OCA\Assistant\Listener\FreePrompt\FreePromptReferenceListener;
1717
use OCA\Assistant\Listener\LoadAdditionalScriptsListener;
18+
use OCA\Assistant\Listener\NewFileMenuTaskFailedListener;
19+
use OCA\Assistant\Listener\NewFileMenuTaskSuccessfulListener;
1820
use OCA\Assistant\Listener\SpeechToText\SpeechToTextReferenceListener;
1921
use OCA\Assistant\Listener\TaskFailedListener;
2022
use OCA\Assistant\Listener\TaskOutputFileReferenceListener;
@@ -74,7 +76,9 @@ public function register(IRegistrationContext $context): void {
7476
$context->registerEventListener(TaskFailedEvent::class, TaskFailedListener::class);
7577
$context->registerEventListener(TaskSuccessfulEvent::class, ChattyLLMTaskListener::class);
7678
$context->registerEventListener(TaskSuccessfulEvent::class, FileActionTaskSuccessfulListener::class);
79+
$context->registerEventListener(TaskSuccessfulEvent::class, NewFileMenuTaskSuccessfulListener::class);
7780
$context->registerEventListener(TaskFailedEvent::class, FileActionTaskFailedListener::class);
81+
$context->registerEventListener(TaskFailedEvent::class, NewFileMenuTaskFailedListener::class);
7882

7983
$context->registerNotifierService(Notifier::class);
8084

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace OCA\Assistant\Listener;
9+
10+
use OCA\Assistant\Service\NotificationService;
11+
use OCP\EventDispatcher\Event;
12+
use OCP\EventDispatcher\IEventListener;
13+
use OCP\TaskProcessing\Events\TaskFailedEvent;
14+
15+
/**
16+
* @template-implements IEventListener<Event>
17+
*/
18+
class NewFileMenuTaskFailedListener implements IEventListener {
19+
20+
public function __construct(
21+
private NotificationService $notificationService,
22+
) {
23+
}
24+
25+
public function handle(Event $event): void {
26+
if (!$event instanceof TaskFailedEvent) {
27+
return;
28+
}
29+
30+
$task = $event->getTask();
31+
if ($task->getUserId() === null) {
32+
return;
33+
}
34+
35+
$customIdPattern = '/^new-image-file:(\d+)$/';
36+
$isNewImageFileAction = preg_match($customIdPattern, $task->getCustomId(), $matches) === 1;
37+
38+
// For tasks with customId "new-image-file:<directoryIdNumber>" we always send a notification
39+
if (!$isNewImageFileAction) {
40+
return;
41+
}
42+
43+
$notificationTarget = null;
44+
$notificationActionLabel = null;
45+
46+
$this->notificationService->sendNotification($task, $notificationTarget, $notificationActionLabel);
47+
}
48+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace OCA\Assistant\Listener;
9+
10+
use OCA\Assistant\Service\AssistantService;
11+
use OCA\Assistant\Service\NotificationService;
12+
use OCP\EventDispatcher\Event;
13+
use OCP\EventDispatcher\IEventListener;
14+
use OCP\IURLGenerator;
15+
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* @template-implements IEventListener<Event>
20+
*/
21+
class NewFileMenuTaskSuccessfulListener implements IEventListener {
22+
23+
public function __construct(
24+
private NotificationService $notificationService,
25+
private AssistantService $assistantService,
26+
private LoggerInterface $logger,
27+
private IUrlGenerator $url,
28+
) {
29+
}
30+
31+
public function handle(Event $event): void {
32+
if (!$event instanceof TaskSuccessfulEvent) {
33+
return;
34+
}
35+
36+
$task = $event->getTask();
37+
if ($task->getUserId() === null) {
38+
return;
39+
}
40+
41+
$customIdPattern = '/^new-image-file:(\d+)$/';
42+
$isNewImageFileAction = preg_match($customIdPattern, $task->getCustomId(), $matches) === 1;
43+
44+
// For tasks with customId "new-image-file:<directoryIdNumber>" we always send a notification
45+
if (!$isNewImageFileAction) {
46+
return;
47+
}
48+
49+
$directoryId = (int)$matches[1];
50+
$fileId = (int)$task->getOutput()['images'][0];
51+
try {
52+
$file = $this->assistantService->saveNewFileMenuActionFile($task->getUserId(), $task->getId(), $fileId, $directoryId);
53+
$notificationTarget = $this->url->linkToRouteAbsolute(
54+
'files.viewcontroller.showFile',
55+
[
56+
'fileid' => $file->getId(),
57+
'opendetails' => 'true',
58+
'openfile' => 'false',
59+
],
60+
);
61+
$this->notificationService->sendNotification($task, $notificationTarget);
62+
} catch (\Exception $e) {
63+
$this->logger->error('TaskSuccessfulListener: Failed to save new file menu action file.', [
64+
'task' => $task->jsonSerialize(),
65+
'exception' => $e,
66+
]);
67+
}
68+
69+
}
70+
}

lib/Listener/TaskSuccessfulListener.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010
use OCA\Assistant\AppInfo\Application;
1111
use OCA\Assistant\Db\TaskNotificationMapper;
1212
use OCA\Assistant\Event\BeforeAssistantNotificationEvent;
13-
use OCA\Assistant\Service\AssistantService;
1413
use OCA\Assistant\Service\NotificationService;
1514
use OCP\EventDispatcher\Event;
1615
use OCP\EventDispatcher\IEventDispatcher;
1716
use OCP\EventDispatcher\IEventListener;
18-
use OCP\IURLGenerator;
1917
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
20-
use Psr\Log\LoggerInterface;
2118

2219
/**
2320
* @template-implements IEventListener<Event>
@@ -28,9 +25,6 @@ public function __construct(
2825
private TaskNotificationMapper $taskNotificationMapper,
2926
private NotificationService $notificationService,
3027
private IEventDispatcher $eventDispatcher,
31-
private AssistantService $assistantService,
32-
private LoggerInterface $logger,
33-
private IUrlGenerator $url,
3428
) {
3529
}
3630

@@ -44,11 +38,7 @@ public function handle(Event $event): void {
4438
return;
4539
}
4640

47-
$customIdPattern = '/^new-image-file:(\d+)$/';
48-
$hasTargetDirectory = preg_match($customIdPattern, $task->getCustomId(), $matches) === 1;
49-
50-
// For tasks with customId "new-image-file:<directoryIdNumber>" we always send a notification
51-
if ($this->taskNotificationMapper->getByTaskId($task->getId()) === null && !$hasTargetDirectory) {
41+
if ($this->taskNotificationMapper->getByTaskId($task->getId()) === null) {
5242
return;
5343
}
5444

@@ -67,31 +57,7 @@ public function handle(Event $event): void {
6757
$notificationActionLabel = $beforeAssistantNotificationEvent->getNotificationActionLabel();
6858
}
6959

70-
if ($hasTargetDirectory) {
71-
$directoryId = (int)$matches[1];
72-
$fileId = (int)$task->getOutput()['images'][0];
73-
try {
74-
$file = $this->assistantService->saveNewFileMenuActionFile($task->getUserId(), $task->getId(), $fileId, $directoryId);
75-
$notificationTarget = $this->url->linkToRouteAbsolute(
76-
'files.viewcontroller.showFile',
77-
[
78-
'fileid' => $file->getId(),
79-
'opendetails' => 'true',
80-
'openfile' => 'false',
81-
],
82-
);
83-
} catch (\Exception $e) {
84-
$this->logger->error('TaskSuccessfulListener: Failed to save new file menu action file.', [
85-
'task' => $task->jsonSerialize(),
86-
'exception' => $e,
87-
]);
88-
}
89-
}
90-
9160
$this->notificationService->sendNotification($task, $notificationTarget, $notificationActionLabel);
92-
93-
if (!$hasTargetDirectory) {
94-
$this->taskNotificationMapper->deleteByTaskId($task->getId());
95-
}
61+
$this->taskNotificationMapper->deleteByTaskId($task->getId());
9662
}
9763
}

0 commit comments

Comments
 (0)