diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index d6e8bc686..340bfb1c0 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -26,17 +26,16 @@ use OC\Authentication\Token\IProvider; use OCA\Notifications\App; use OCA\Notifications\Capabilities; +use OCA\Notifications\Listener\BeforeTemplateRenderedListener; use OCA\Notifications\Listener\UserDeletedListener; use OCA\Notifications\Notifier\AdminNotifications; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\IAppContainer; -use OCP\IRequest; -use OCP\IUserSession; use OCP\Notification\IManager; use OCP\User\Events\UserDeletedEvent; -use OCP\Util; class Application extends \OCP\AppFramework\App implements IBootstrap { public const APP_ID = 'notifications'; @@ -52,27 +51,18 @@ public function register(IRegistrationContext $context): void { return $c->getServer()->get(IProvider::class); }); + $context->registerNotifierService(AdminNotifications::class); + $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); } public function boot(IBootContext $context): void { $context->injectFn(\Closure::fromCallable([$this, 'registerAppAndNotifier'])); } - public function registerAppAndNotifier(IManager $notificationManager, IRequest $request, IUserSession $userSession): void { + public function registerAppAndNotifier(IManager $notificationManager): void { // notification app $notificationManager->registerApp(App::class); - - // admin notifications - $notificationManager->registerNotifierService(AdminNotifications::class); - - // User interface - if ($userSession->getUser() !== null - && strpos($request->getPathInfo(), '/s/') !== 0 - && strpos($request->getPathInfo(), '/login/') !== 0 - && substr($request->getScriptName(), 0 - \strlen('/index.php')) === '/index.php') { - Util::addScript('notifications', 'notifications-main'); - Util::addStyle('notifications', 'styles'); - } } } diff --git a/lib/Listener/BeforeTemplateRenderedListener.php b/lib/Listener/BeforeTemplateRenderedListener.php new file mode 100644 index 000000000..f1d761a0b --- /dev/null +++ b/lib/Listener/BeforeTemplateRenderedListener.php @@ -0,0 +1,49 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Notifications\Listener; + +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Util; + +class BeforeTemplateRenderedListener implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof BeforeTemplateRenderedEvent)) { + // Unrelated + return; + } + + if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) { + return; + } + + Util::addScript('notifications', 'notifications-main'); + Util::addStyle('notifications', 'styles'); + } +}