Skip to content

Commit

Permalink
handle failed task (notification + display in form + display in page)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Aug 5, 2023
1 parent 7edf0db commit 4fa1e85
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
25 changes: 23 additions & 2 deletions lib/Listener/TaskFailedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace OCA\TPAssistant\Listener;

use OCA\TPAssistant\AppInfo\Application;
use OCA\TPAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\TPAssistant\Service\AssistantService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\TextProcessing\Events\TaskFailedEvent;

class TaskFailedListener implements IEventListener {

public function __construct(
private AssistantService $assistantService,
private IEventDispatcher $eventDispatcher,
) {
}

Expand All @@ -20,7 +24,24 @@ public function handle(Event $event): void {
}

$task = $event->getTask();
$this->assistantService->sendNotification($task);
error_log('Task failed');
error_log('Task failed ' . $task->getId());
if ($task->getUserId() === null) {
return;
}

$notificationTarget = null;

// we dispatch an event to ask the app that scheduled the task if it wants a notification
// and what the target should be
if ($task->getAppId() !== Application::APP_ID) {
$beforeAssistantNotificationEvent = new BeforeAssistantNotificationEvent($task);
$this->eventDispatcher->dispatchTyped($beforeAssistantNotificationEvent);
if (!$beforeAssistantNotificationEvent->getWantsNotification()) {
return;
}
$notificationTarget = $beforeAssistantNotificationEvent->getNotificationTarget();
}

$this->assistantService->sendNotification($task, $notificationTarget);
}
}
6 changes: 5 additions & 1 deletion lib/Listener/TaskSuccessfulListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function handle(Event $event): void {
}

$task = $event->getTask();
error_log('Task successful ' . $task->getId());
if ($task->getUserId() === null) {
return;
}

$notificationTarget = null;

// we dispatch an event to ask the app that scheduled the task if it wants a notification
Expand All @@ -38,6 +43,5 @@ public function handle(Event $event): void {
}

$this->assistantService->sendNotification($task, $notificationTarget);
error_log('Task successful');
}
}
22 changes: 21 additions & 1 deletion lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function prepare(INotification $notification, string $languageCode): INot
$l = $this->factory->get(Application::APP_ID, $languageCode);

$params = $notification->getSubjectParameters();
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));

switch ($notification->getSubject()) {
case 'success':
$subject = $l->t('Assistant Task for app %1$s has finished', [$params['appId']]);
$content = $l->t('The input was: %1$s', [$params['input']]);
$link = $params['target'] ?? $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getTaskResultPage', ['taskId' => $params['id']]);
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));

$notification
->setParsedSubject($subject)
Expand All @@ -84,6 +84,26 @@ public function prepare(INotification $notification, string $languageCode): INot
return $notification;

case 'failure':
$subject = $l->t('Assistant Task for app %1$s has failed', [$params['appId']]);
$content = $l->t('The input was: %1$s', [$params['input']]);
$link = $params['target'] ?? $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getTaskResultPage', ['taskId' => $params['id']]);
$iconUrl = $this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/error.svg'));

$notification
->setParsedSubject($subject)
->setParsedMessage($content)
->setLink($link)
->setIcon($iconUrl);

$actionLabel = $l->t('View task');
$action = $notification->createAction();
$action->setLabel($actionLabel)
->setParsedLabel($actionLabel)
->setLink($notification->getLink(), IAction::TYPE_WEB)
->setPrimary(true);

$notification->addParsedAction($action);

return $notification;

default:
Expand Down
3 changes: 2 additions & 1 deletion src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async function showResults(taskId) {
const { generateOcsUrl } = await import(/* webpackChunkName: "router-lazy" */'@nextcloud/router')
const url = generateOcsUrl('textprocessing/task/{taskId}', { taskId })
axios.get(url).then(response => {
console.debug('showing results for task', response.data.ocs.data.task)
openAssistantResult(response.data.ocs.data.task)
}).catch(error => {
console.error(error)
Expand Down Expand Up @@ -137,7 +138,7 @@ async function openAssistantResult(task) {
propsData: {
// isInsideViewer,
input: task.input,
output: task.output,
output: task.output ?? '',
selectedTaskTypeId: task.type,
},
}).$mount(modalElement)
Expand Down
4 changes: 2 additions & 2 deletions src/views/TaskResultPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssistantForm
class="form"
:input="task.input"
:output="task.output"
:output="task.output ?? ''"
:selected-task-type-id="task.type"
@submit="onSubmit" />
</div>
Expand Down Expand Up @@ -65,7 +65,7 @@ export default {
.assistant-wrapper {
display: flex;
justify-content: center;
margin: 16px 0;
margin: 24px 0 16px 0;
.form {
width: 400px;
}
Expand Down

0 comments on commit 4fa1e85

Please sign in to comment.