Skip to content

Commit

Permalink
plop
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 94e499c commit f1204ff
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function handle(Event $event): void {
return;
}

Util::addScript(Application::APP_ID, Application::APP_ID . '-assistant');
Util::addScript(Application::APP_ID, Application::APP_ID . '-main');
}
}
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');
}
}
19 changes: 19 additions & 0 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ 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']]);

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

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

$notification->addParsedAction($action);

return $notification;

default:
Expand Down
31 changes: 6 additions & 25 deletions src/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ __webpack_public_path__ = linkTo('textprocessing_assistant', 'js/') // eslint-di
* @return {Promise<unknown>}
*/
export async function openAssistantForm({ appId, identifier = '', taskType = null, input = '', isInsideViewer = undefined }) {
const { default: Vue } = await import(/* webpackChunkName: "vue-lazy" */'vue')
const { default: Vue } = await import(/* webpackChunkName: "vue-lazy1" */'vue')
const { default: AssistantModal } = await import(/* webpackChunkName: "assistant-modal-lazy" */'./components/AssistantModal.vue')
Vue.mixin({ methods: { t, n } })

Expand Down Expand Up @@ -68,7 +68,7 @@ export async function openAssistantForm({ appId, identifier = '', taskType = nul
* @param {string} input the task input text
* @return {Promise<*>}
*/
async function scheduleTask(appId, identifier, taskType, input) {
export async function scheduleTask(appId, identifier, taskType, input) {
const { default: axios } = await import(/* webpackChunkName: "axios-lazy" */'@nextcloud/axios')
const { generateOcsUrl } = await import(/* webpackChunkName: "router-lazy" */'@nextcloud/router')
const url = generateOcsUrl('textprocessing/schedule', 2)
Expand Down Expand Up @@ -104,7 +104,7 @@ function handleNotification(event) {
*
* @return {Promise<void>}
*/
async function subscribeToNotifications() {
export async function subscribeToNotifications() {
const { subscribe } = await import(/* webpackChunkName: "router-lazy" */'@nextcloud/event-bus')
subscribe('notifications:action:execute', handleNotification)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ async function showResults(taskId) {
* @return {Promise<void>}
*/
async function openAssistantResult(task) {
const { default: Vue } = await import(/* webpackChunkName: "vue-lazy" */'vue')
const { default: Vue } = await import(/* webpackChunkName: "vue-lazy2" */'vue')
const { default: AssistantModal } = await import(/* webpackChunkName: "assistant-modal-lazy" */'./components/AssistantModal.vue')
Vue.mixin({ methods: { t, n } })

Expand Down Expand Up @@ -167,13 +167,13 @@ async function openAssistantResult(task) {
})
}

async function addAssistantMenuEntry() {
export async function addAssistantMenuEntry() {
const headerRight = document.querySelector('#header .header-right')
const menuEntry = document.createElement('div')
menuEntry.id = 'assistant'
headerRight.prepend(menuEntry)

const { default: Vue } = await import(/* webpackChunkName: "vue-lazy" */'vue')
const { default: Vue } = await import(/* webpackChunkName: "vue-lazy3" */'vue')
const { default: AssistantHeaderMenuEntry } = await import(/* webpackChunkName: "assistant-modal-lazy" */'./components/AssistantHeaderMenuEntry.vue')
Vue.mixin({ methods: { t, n } })

Expand All @@ -189,22 +189,3 @@ async function addAssistantMenuEntry() {
})
})
}

/**
* Expose OCA.TPAssistant.openTextProcessingModal to let apps use the assistant
* and add a header right menu entry
*/
function init() {
if (!OCA.TPAssistant) {
/**
* @namespace
*/
OCA.TPAssistant = {
openAssistantForm,
}
}
addAssistantMenuEntry()
}

init()
subscribeToNotifications()
20 changes: 20 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { subscribeToNotifications, addAssistantMenuEntry, openAssistantForm } from './assistant.js'

/**
* Expose OCA.TPAssistant.openTextProcessingModal to let apps use the assistant
* and add a header right menu entry
*/
function init() {
if (!OCA.TPAssistant) {
/**
* @namespace
*/
OCA.TPAssistant = {
openAssistantForm,
}
subscribeToNotifications()
addAssistantMenuEntry()
}
}

init()
11 changes: 0 additions & 11 deletions src/utils.js

This file was deleted.

14 changes: 12 additions & 2 deletions src/views/TaskResultPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
class="form"
:input="task.input"
:output="task.output"
:selected-task-type-id="task.type" />
:selected-task-type-id="task.type"
@submit="onSubmit" />
</div>
</NcAppContent>
</NcContent>
Expand All @@ -20,6 +21,7 @@ import NcAppContent from '@nextcloud/vue/dist/Components/NcAppContent.js'
import AssistantForm from '../components/AssistantForm.vue'
import { loadState } from '@nextcloud/initial-state'
import { scheduleTask } from '../assistant.js'
export default {
name: 'TaskResultPage',
Expand All @@ -43,10 +45,18 @@ export default {
},
mounted() {
console.debug('aaaaa MOUNTED', this.task)
},
methods: {
onSubmit(data) {
scheduleTask(this.task.appId, this.task.identifier, data.taskTypeId, data.input)
.then((response) => {
console.debug('scheduled task', response.data?.ocs?.data?.task)
})
.catch(error => {
console.error('Assistant scheduling error', error)
})
},
},
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const appId = 'textprocessing_assistant'
webpackConfig.entry = {
personalSettings: { import: path.join(__dirname, 'src', 'personalSettings.js'), filename: appId + '-personalSettings.js' },
adminSettings: { import: path.join(__dirname, 'src', 'adminSettings.js'), filename: appId + '-adminSettings.js' },
assistant: { import: path.join(__dirname, 'src', 'assistant.js'), filename: appId + '-assistant.js' },
main: { import: path.join(__dirname, 'src', 'main.js'), filename: appId + '-main.js' },
taskResultPage: { import: path.join(__dirname, 'src', 'taskResultPage.js'), filename: appId + '-taskResultPage.js' },
}

Expand Down

0 comments on commit f1204ff

Please sign in to comment.