Skip to content

Commit 9d99e95

Browse files
committed
feat(file-actions): address review comments
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 2b29602 commit 9d99e95

File tree

7 files changed

+53
-24
lines changed

7 files changed

+53
-24
lines changed

lib/Listener/FileActionTaskFailedListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCA\Assistant\Listener;
1111

12-
use OCA\Assistant\AppInfo\Application;
1312
use OCA\Assistant\Service\NotificationService;
1413
use OCA\Assistant\Service\TaskProcessingService;
1514
use OCP\EventDispatcher\Event;
@@ -44,7 +43,7 @@ public function handle(Event $event): void {
4443
return;
4544
}
4645

47-
if (!$this->taskProcessingService->isFileActionTaskTypeAuthorized($taskTypeId)) {
46+
if (!$this->taskProcessingService->isFileActionTaskTypeSupported($taskTypeId)) {
4847
return;
4948
}
5049

lib/Listener/FileActionTaskSuccessfulListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCA\Assistant\Listener;
1111

12-
use OCA\Assistant\AppInfo\Application;
1312
use OCA\Assistant\Service\NotificationService;
1413
use OCA\Assistant\Service\TaskProcessingService;
1514
use OCP\EventDispatcher\Event;
@@ -45,7 +44,7 @@ public function handle(Event $event): void {
4544
return;
4645
}
4746

48-
if (!$this->taskProcessingService->isFileActionTaskTypeAuthorized($taskTypeId)) {
47+
if (!$this->taskProcessingService->isFileActionTaskTypeSupported($taskTypeId)) {
4948
return;
5049
}
5150

lib/Service/NotificationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function sendFileActionNotification(
9595
$notification->setApp(Application::APP_ID)
9696
->setUser($userId)
9797
->setDateTime(new DateTime())
98-
->setObject('task', $taskId)
98+
->setObject('task', (string)$taskId)
9999
->setSubject($subject, $params);
100100

101101
$manager->notify($notification);

lib/Service/TaskProcessingService.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
use OCP\TaskProcessing\Task;
2424
use OCP\TaskProcessing\TaskTypes\AudioToText;
2525
use OCP\TaskProcessing\TaskTypes\TextToTextSummary;
26+
use Psr\Log\LoggerInterface;
2627
use RuntimeException;
2728

2829
class TaskProcessingService {
2930

3031
public function __construct(
3132
private IManager $taskProcessingManager,
3233
private IRootFolder $rootFolder,
34+
private LoggerInterface $logger,
3335
) {
3436
}
3537

@@ -68,12 +70,20 @@ public function getOutputFile(int $fileId): File {
6870
return $node;
6971
}
7072

73+
/**
74+
* @param int $fileId
75+
* @return string
76+
* @throws GenericFileException
77+
* @throws LockedException
78+
* @throws NotFoundException
79+
* @throws NotPermittedException
80+
*/
7181
public function getOutputFileContent(int $fileId): string {
7282
$file = $this->getOutputFile($fileId);
7383
return $file->getContent();
7484
}
7585

76-
public function isFileActionTaskTypeAuthorized(string $taskTypeId): bool {
86+
public function isFileActionTaskTypeSupported(string $taskTypeId): bool {
7787
$authorizedTaskTypes = [AudioToText::ID, TextToTextSummary::ID];
7888
if (class_exists('OCP\\TaskProcessing\\TaskTypes\\TextToSpeech')) {
7989
$authorizedTaskTypes[] = \OCP\TaskProcessing\TaskTypes\TextToSpeech::ID;
@@ -89,35 +99,44 @@ public function isFileActionTaskTypeAuthorized(string $taskTypeId): bool {
8999
* @param string $taskTypeId
90100
* @return int The scheduled task ID
91101
* @throws Exception
92-
* @throws GenericFileException
93-
* @throws LockedException
94102
* @throws NotFoundException
95-
* @throws NotPermittedException
96-
* @throws PreConditionNotMetException
97-
* @throws UnauthorizedException
98-
* @throws ValidationException
99-
* @throws NoUserException
100103
*/
101104
public function runFileAction(string $userId, int $fileId, string $taskTypeId): int {
102-
if (!$this->isFileActionTaskTypeAuthorized($taskTypeId)) {
103-
throw new PreConditionNotMetException();
105+
if (!$this->isFileActionTaskTypeSupported($taskTypeId)) {
106+
throw new Exception('Invalid task type for file action');
107+
}
108+
try {
109+
$userFolder = $this->rootFolder->getUserFolder($userId);
110+
} catch (NoUserException|NotPermittedException $e) {
111+
$this->logger->warning('Assistant runFileAction, the user folder could not be obtained', ['exception' => $e]);
112+
throw new Exception('The user folder could not be obtained');
104113
}
105-
$userFolder = $this->rootFolder->getUserFolder($userId);
106114
$file = $userFolder->getFirstNodeById($fileId);
107115
if (!$file instanceof File) {
116+
$this->logger->warning('Assistant runFileAction, the input file is not a file', ['file_id' => $fileId]);
108117
throw new NotFoundException('File is not a file');
109118
}
110-
$input = $taskTypeId === AudioToText::ID
111-
? ['input' => $fileId]
112-
: ['input' => $file->getContent()];
119+
try {
120+
$input = $taskTypeId === AudioToText::ID
121+
? ['input' => $fileId]
122+
: ['input' => $file->getContent()];
123+
} catch (NotPermittedException|GenericFileException|LockedException $e) {
124+
$this->logger->warning('Assistant runFileAction, impossible to read the file action input file', ['exception' => $e]);
125+
throw new Exception('Impossible to read the file action input file');
126+
}
113127
$task = new Task(
114128
$taskTypeId,
115129
$input,
116130
Application::APP_ID,
117131
$userId,
118132
'file-action:' . $fileId,
119133
);
120-
$this->taskProcessingManager->scheduleTask($task);
134+
try {
135+
$this->taskProcessingManager->scheduleTask($task);
136+
} catch (PreConditionNotMetException|ValidationException|Exception|UnauthorizedException $e) {
137+
$this->logger->warning('Assistant runFileAction, impossible to schedule the task', ['exception' => $e]);
138+
throw new Exception('Impossible to schedule the task');
139+
}
121140
$taskId = $task->getId();
122141
if ($taskId === null) {
123142
throw new Exception('The task could not be scheduled');

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</issueHandlers>
5454
<stubs>
5555
<file name="tests/stubs/oc_core_command_base.php" />
56-
<file name="tests/stubs/oc_hooks_emitter.php" />
56+
<file name="tests/stubs/oc_hooks_oca_files.php" />
5757
<file name="tests/stubs/oc_transcription.php" />
5858
</stubs>
5959
</psalm>

src/files/fileActions.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ function registerSummarizeAction() {
6262
try {
6363
const response = await axios.post(url)
6464
console.debug('taskId', response.data.ocs.data.taskId)
65-
showSuccess(t('assistant', 'The result file will be created soon'))
65+
showSuccess(
66+
t('assistant', 'Summarization task submitted successfully.') + ' '
67+
+ t('assistant', 'You will be notified when it is ready.') + ' '
68+
+ t('assistant', 'It can also be checked in the Assistant in the "Work with text -> Summarize" menu.'),
69+
)
6670
} catch (error) {
6771
console.error(error)
6872
showError(t('assistant', 'Failed to launch the AI file action'))
@@ -97,7 +101,11 @@ function registerTtsAction() {
97101
try {
98102
const response = await axios.post(url)
99103
console.debug('taskId', response.data.ocs.data.taskId)
100-
showSuccess(t('assistant', 'The result file will be created soon'))
104+
showSuccess(
105+
t('assistant', 'Text-to-speech task submitted successfully.') + ' '
106+
+ t('assistant', 'You will be notified when it is ready.') + ' '
107+
+ t('assistant', 'It can also be checked in the Assistant in the "Work with audio -> Generate speech" menu.'),
108+
)
101109
} catch (error) {
102110
console.error(error)
103111
showError(t('assistant', 'Failed to launch the AI file action'))
@@ -132,7 +140,11 @@ function registerSttAction() {
132140
try {
133141
const response = await axios.post(url)
134142
console.debug('taskId', response.data.ocs.data.taskId)
135-
showSuccess(t('assistant', 'The result file will be created soon'))
143+
showSuccess(
144+
t('assistant', 'Transcription task submitted successfully.') + ' '
145+
+ t('assistant', 'You will be notified when it is ready.') + ' '
146+
+ t('assistant', 'It can also be checked in the Assistant in the "Work with audio -> Transcribe audio" menu.'),
147+
)
136148
} catch (error) {
137149
console.error(error)
138150
showError(t('assistant', 'Failed to launch the AI file action'))
File renamed without changes.

0 commit comments

Comments
 (0)