Skip to content

Commit

Permalink
dispatch event to let the scheduling app request a notification and s…
Browse files Browse the repository at this point in the history
…et its target

Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Aug 3, 2023
1 parent 2d8414b commit ae4826f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
67 changes: 67 additions & 0 deletions lib/Event/BeforeAssistantNotificationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace OCA\TPAssistant\Event;

use OCP\EventDispatcher\Event;
use OCP\TextProcessing\Task;

/**
* Event to let apps that scheduled a task via the assistant
* decide if they want a notification or not.
* If they want one, they can specify the notification target link
*/
class BeforeAssistantNotificationEvent extends Event {

private bool $wantsNotification = false;
private ?string $notificationTarget = null;

public function __construct(
private Task $task,
) {
parent::__construct();
}

/**
* Get the task that was successful and for which a notification can be produced
*
* @return Task
*/
public function getTask(): Task {
return $this->task;
}

/**
* Does the app that scheduled the task want a notification?
*
* @return bool
*/
public function getWantsNotification(): bool {
return $this->wantsNotification;
}

/**
* @param bool $wantsNotification true means a notification will be produced for this task
* @return void
*/
public function setWantsNotification(bool $wantsNotification): void {
$this->wantsNotification = $wantsNotification;
}

/**
* @return string|null
*/
public function getNotificationTarget(): ?string {
return $this->notificationTarget;
}

/**
* @param string|null $notificationTarget URL that will be used as target for the notification and its main action,
* null means the assistant will take care of rendering the result (in a modal or by setting a dedicated NC page showing the result)
* @return void
*/
public function setNotificationTarget(?string $notificationTarget): void {
$this->notificationTarget = $notificationTarget;
}
}
19 changes: 18 additions & 1 deletion lib/Listener/TaskSuccessfulListener.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\TaskSuccessfulEvent;

class TaskSuccessfulListener implements IEventListener {

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

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

$task = $event->getTask();
$this->assistantService->sendNotification($task);
$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);
error_log('Task successful');
}
}
3 changes: 1 addition & 2 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ public function prepare(INotification $notification, string $languageCode): INot

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 = $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getTaskResultPage', ['taskId' => $params['id']]);
$link = $params['target'] ?? $this->url->linkToRouteAbsolute(Application::APP_ID . '.assistant.getTaskResultPage', ['taskId' => $params['id']]);

$notification
->setParsedSubject($subject)
Expand Down
8 changes: 6 additions & 2 deletions lib/Service/AssistantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ public function __construct(
) {
}

public function sendNotification(Task $task): void {
public function sendNotification(Task $task, ?string $target): void {
$manager = $this->notificationManager;
$notification = $manager->createNotification();

$params = [
'appId' => $task->getAppId(),
'id' => $task->getId(),
'input' => $task->getInput(),
'target' => $target,
];
$status = $task->getStatus();
$subject = $status === Task::STATUS_SUCCESSFUL
? 'success'
: 'failure';
$objectType = ($task->getAppId() === Application::APP_ID || $target === null)
? 'task'
: 'task-with-custom-target';
$notification->setApp(Application::APP_ID)
->setUser($task->getUserId())
->setDateTime(new DateTime())
->setObject('task', $task->getId())
->setObject($objectType, $task->getId())
->setSubject($subject, $params);

$manager->notify($notification);
Expand Down
10 changes: 7 additions & 3 deletions src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ async function scheduleTask({ appId, identifier, taskType, input }) {
}

function handleNotification(event) {
console.debug('aaaaaa -------- handle notification', event)
if (event.notification.app !== 'textprocessing_assistant' || event.action.type !== 'WEB') {
return
}
event.cancelAction = true
showResults(event.notification.objectId)
// Handle the action click only if the task was scheduled by the assistant
// or if the scheduling app didn't give any notification target
// We use the object type to know
if (event.notification.objectType === 'task') {
event.cancelAction = true
showResults(event.notification.objectId)
}
}

async function subscribeToNotifications() {
Expand Down
15 changes: 12 additions & 3 deletions src/components/AssistantForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
label="name"
input-id="task-select"
@input="onTaskInput" />
<span v-if="selectedTaskType"
class="task-description">
{{ selectedTaskType.description }}
</span>
<NcRichContenteditable
:value.sync="myInput"
class="editable-input"
Expand All @@ -25,11 +29,12 @@
:disabled="loading"
:placeholder="t('textprocessing_assistant', 'Result')"
:link-autocomplete="false" />
<NcButton
<NcButton v-if="selectedTaskType"
class="submit-button"
aria-label="plop"
title="Send"
:aria-label="t('textprocessing_assistant', 'Submit assistant task')"
:title="t('textprocessing_assistant', 'Submit')"
@click="onSubmit">
{{ selectedTaskType.name }}
<template #icon>
<CreationIcon />
</template>
Expand Down Expand Up @@ -152,6 +157,10 @@ export default {
width: 250px;
}
.task-description {
align-self: start;
}
.submit-button {
align-self: end;
}
Expand Down

0 comments on commit ae4826f

Please sign in to comment.