-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement notifications and custom handling in the web UI (open resul…
…ts in a modal) Signed-off-by: Julien Veyssier <[email protected]>
- Loading branch information
Showing
9 changed files
with
237 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\TPAssistant\Listener; | ||
|
||
use OCA\TPAssistant\AppInfo\Application; | ||
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; | ||
use OCP\AppFramework\Http\TemplateResponse; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\IUser; | ||
use OCP\IUserSession; | ||
use OCP\Util; | ||
|
||
class BeforeTemplateRenderedListener implements IEventListener { | ||
|
||
public function __construct( | ||
private IUserSession $userSession, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof BeforeTemplateRenderedEvent)) { | ||
// Unrelated | ||
return; | ||
} | ||
|
||
if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) { | ||
return; | ||
} | ||
|
||
if (!$this->userSession->getUser() instanceof IUser) { | ||
return; | ||
} | ||
|
||
Util::addScript(Application::APP_ID, Application::APP_ID . '-assistant'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace OCA\TPAssistant\Notification; | ||
|
||
use InvalidArgumentException; | ||
use OCA\TPAssistant\AppInfo\Application; | ||
use OCP\IURLGenerator; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use OCP\L10N\IFactory; | ||
use OCP\Notification\IAction; | ||
use OCP\Notification\INotification; | ||
|
||
use OCP\Notification\INotifier; | ||
|
||
class Notifier implements INotifier { | ||
|
||
public function __construct( | ||
private IFactory $factory, | ||
private IUserManager $userManager, | ||
private IURLGenerator $url, | ||
private ?string $userId, | ||
) { | ||
} | ||
|
||
/** | ||
* Identifier of the notifier, only use [a-z0-9_] | ||
* | ||
* @return string | ||
* @since 17.0.0 | ||
*/ | ||
public function getID(): string { | ||
return Application::APP_ID; | ||
} | ||
/** | ||
* Human readable name describing the notifier | ||
* | ||
* @return string | ||
* @since 17.0.0 | ||
*/ | ||
public function getName(): string { | ||
return $this->factory->get(Application::APP_ID)->t('Nextcloud assistant'); | ||
} | ||
|
||
/** | ||
* @param INotification $notification | ||
* @param string $languageCode The code of the language that should be used to prepare the notification | ||
* @return INotification | ||
* @throws InvalidArgumentException When the notification was not prepared by a notifier | ||
* @since 9.0.0 | ||
*/ | ||
public function prepare(INotification $notification, string $languageCode): INotification { | ||
if ($notification->getApp() !== Application::APP_ID) { | ||
// Not my app => throw | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
$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 = $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; | ||
|
||
case 'failure': | ||
return $notification; | ||
|
||
default: | ||
// Unknown subject => Unknown notification => throw | ||
throw new InvalidArgumentException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters