Skip to content

Commit 5300d62

Browse files
committed
feat(new-file-menu): customize notifications for new filme menu
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 03696cf commit 5300d62

File tree

4 files changed

+117
-17
lines changed

4 files changed

+117
-17
lines changed

lib/Listener/NewFileMenuTaskFailedListener.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OCA\Assistant\Service\NotificationService;
1111
use OCP\EventDispatcher\Event;
1212
use OCP\EventDispatcher\IEventListener;
13+
use OCP\Files\IRootFolder;
1314
use OCP\TaskProcessing\Events\TaskFailedEvent;
1415

1516
/**
@@ -19,6 +20,7 @@ class NewFileMenuTaskFailedListener implements IEventListener {
1920

2021
public function __construct(
2122
private NotificationService $notificationService,
23+
private IRootFolder $rootFolder,
2224
) {
2325
}
2426

@@ -40,9 +42,12 @@ public function handle(Event $event): void {
4042
return;
4143
}
4244

43-
$notificationTarget = null;
44-
$notificationActionLabel = null;
45-
46-
$this->notificationService->sendNotification($task, $notificationTarget, $notificationActionLabel);
45+
$directoryId = (int)$matches[1];
46+
$userFolder = $this->rootFolder->getUserFolder($task->getUserId());
47+
$directory = $userFolder->getFirstNodeById($directoryId);
48+
$this->notificationService->sendNewImageFileNotification(
49+
$task->getUserId(), $task->getId(),
50+
$directoryId, $directory->getName(), $userFolder->getRelativePath($directory->getPath()),
51+
);
4752
}
4853
}

lib/Listener/NewFileMenuTaskSuccessfulListener.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use OCA\Assistant\Service\NotificationService;
1212
use OCP\EventDispatcher\Event;
1313
use OCP\EventDispatcher\IEventListener;
14-
use OCP\IURLGenerator;
14+
use OCP\Files\IRootFolder;
1515
use OCP\TaskProcessing\Events\TaskSuccessfulEvent;
1616
use Psr\Log\LoggerInterface;
1717

@@ -24,7 +24,7 @@ public function __construct(
2424
private NotificationService $notificationService,
2525
private AssistantService $assistantService,
2626
private LoggerInterface $logger,
27-
private IUrlGenerator $url,
27+
private IRootFolder $rootFolder,
2828
) {
2929
}
3030

@@ -49,16 +49,14 @@ public function handle(Event $event): void {
4949
$directoryId = (int)$matches[1];
5050
$fileId = (int)$task->getOutput()['images'][0];
5151
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-
],
52+
$targetFile = $this->assistantService->saveNewFileMenuActionFile($task->getUserId(), $task->getId(), $fileId, $directoryId);
53+
$userFolder = $this->rootFolder->getUserFolder($task->getUserId());
54+
$directory = $targetFile->getParent();
55+
$this->notificationService->sendNewImageFileNotification(
56+
$task->getUserId(), $task->getId(),
57+
$directoryId, $directory->getName(), $userFolder->getRelativePath($directory->getPath()),
58+
$targetFile->getId(), $targetFile->getName(), $userFolder->getRelativePath($targetFile->getPath()),
6059
);
61-
$this->notificationService->sendNotification($task, $notificationTarget);
6260
} catch (\Exception $e) {
6361
$this->logger->error('TaskSuccessfulListener: Failed to save new file menu action file.', [
6462
'task' => $task->jsonSerialize(),

lib/Notification/Notifier.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function prepare(INotification $notification, string $languageCode): INot
189189
case 'file_action_success':
190190
$subject = $l->t('File action has finished');
191191

192-
$sourceFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['source_file_id']]);
192+
$targetDirLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['source_file_id']]);
193193
$targetFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_file_id']]);
194194
$taskLink = $params['target'];
195195
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
@@ -216,7 +216,7 @@ public function prepare(INotification $notification, string $languageCode): INot
216216
'id' => (string)$params['source_file_id'],
217217
'name' => $params['source_file_name'],
218218
'path' => $params['source_file_path'],
219-
'link' => $sourceFileLink,
219+
'link' => $targetDirLink,
220220
],
221221
'targetFile' => [
222222
'type' => 'file',
@@ -274,6 +274,70 @@ public function prepare(INotification $notification, string $languageCode): INot
274274

275275
return $notification;
276276

277+
case 'new_image_file_success':
278+
$subject = $l->t('New image file has been generated');
279+
280+
$targetDirLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_directory_id']]);
281+
$targetFileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_file_id']]);
282+
$taskLink = $params['target'];
283+
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
284+
285+
$message = $l->t('{targetFile} has been generated in {targetDirectory}');
286+
287+
$notification
288+
->setParsedSubject($subject)
289+
->setRichMessage($message, [
290+
'targetDirectory' => [
291+
'type' => 'file',
292+
'id' => (string)$params['target_directory_id'],
293+
'name' => $params['target_directory_name'],
294+
'path' => $params['target_directory_path'],
295+
'link' => $targetDirLink,
296+
],
297+
'targetFile' => [
298+
'type' => 'file',
299+
'id' => (string)$params['target_file_id'],
300+
'name' => $params['target_file_name'],
301+
'path' => $params['target_file_path'],
302+
'link' => $targetFileLink,
303+
],
304+
])
305+
->setLink($taskLink)
306+
->setIcon($iconUrl);
307+
308+
$actionLabel = $l->t('View results');
309+
$action = $notification->createAction();
310+
$action->setLabel($actionLabel)
311+
->setParsedLabel($actionLabel)
312+
->setLink($taskLink, IAction::TYPE_WEB)
313+
->setPrimary(true);
314+
315+
$notification->addParsedAction($action);
316+
317+
return $notification;
318+
319+
case 'new_image_file_failure':
320+
$subject = $l->t('Image file generation has failed');
321+
322+
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));
323+
324+
$message = $l->t('Generation of a new image file in {targetDirectory} has failed');
325+
326+
$notification
327+
->setParsedSubject($subject)
328+
->setRichMessage($message, [
329+
'targetDirectory' => [
330+
'type' => 'file',
331+
'id' => (string)$params['target_directory_id'],
332+
'name' => $params['target_directory_name'],
333+
'path' => $params['target_directory_path'],
334+
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $params['target_directory_id']]),
335+
],
336+
])
337+
->setIcon($iconUrl);
338+
339+
return $notification;
340+
277341
default:
278342
// Unknown subject => Unknown notification => throw
279343
throw new InvalidArgumentException();

lib/Service/NotificationService.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,37 @@ public function sendFileActionNotification(
100100

101101
$manager->notify($notification);
102102
}
103+
104+
public function sendNewImageFileNotification(
105+
string $userId, int $taskId,
106+
?int $targetDirectoryId = null, ?string $targetDirectoryName = null, ?string $targetDirectoryPath = null,
107+
?int $targetFileId = null, ?string $targetFileName = null, ?string $targetFilePath = null,
108+
): void {
109+
$manager = $this->notificationManager;
110+
$notification = $manager->createNotification();
111+
112+
$params = [
113+
'target_directory_id' => $targetDirectoryId,
114+
'target_directory_name' => $targetDirectoryName,
115+
'target_directory_path' => $targetDirectoryPath,
116+
'target_file_id' => $targetFileId,
117+
'target_file_name' => $targetFileName,
118+
'target_file_path' => $targetFilePath,
119+
'task_id' => $taskId,
120+
'target' => $this->getDefaultTarget($taskId),
121+
];
122+
$taskSuccessful = $targetFileId !== null && $targetFileName !== null;
123+
124+
$subject = $taskSuccessful
125+
? 'new_image_file_success'
126+
: 'new_image_file_failure';
127+
128+
$notification->setApp(Application::APP_ID)
129+
->setUser($userId)
130+
->setDateTime(new DateTime())
131+
->setObject('task', (string)$taskId)
132+
->setSubject($subject, $params);
133+
134+
$manager->notify($notification);
135+
}
103136
}

0 commit comments

Comments
 (0)