diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index 5d78f1780..000000000 --- a/appinfo/app.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * @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 . - * - */ - -use OCA\FirstRunWizard\AppInfo\Application; - -$app = \OC::$server->query(Application::class); -$app->register(); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4379d00ee..333df9f22 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,75 +24,32 @@ namespace OCA\FirstRunWizard\AppInfo; use OCA\Files\Event\LoadAdditionalScriptsEvent; -use OCA\FirstRunWizard\Notification\AppHint; +use OCA\FirstRunWizard\Listener\AppEnabledListener; +use OCA\FirstRunWizard\Listener\BeforeFilesAppTemplateRenderedListener; +use OCA\FirstRunWizard\Listener\BeforeTemplateRenderedListener; use OCA\FirstRunWizard\Notification\Notifier; +use OCP\App\ManagerEvent; use OCP\AppFramework\App; -use OCP\AppFramework\Http\TemplateResponse; -use OCP\EventDispatcher\IEventDispatcher; -use OCP\IConfig; -use OCP\IInitialStateService; -use OCP\IL10N; -use OCP\IServerContainer; -use OCP\IUser; -use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; -class Application extends App { - - /** @var bool */ - protected $isCLI; +class Application extends App implements IBootstrap { public function __construct() { parent::__construct('firstrunwizard'); - $this->isCLI = \OC::$CLI; - } - - public function register() { - if (!$this->isCLI) { - $this->registerScripts(); - $this->registerNotificationNotifier(); - } } - protected function registerScripts() { - /** @var IServerContainer $server */ - $server = $this->getContainer()->getServer(); - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $server->query(IEventDispatcher::class); - - $dispatcher->addListener(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, function() { - \OC_Util::addScript('firstrunwizard', 'about'); - }); - + public function register(IRegistrationContext $context): void { + $context->registerEventListener(ManagerEvent::EVENT_APP_ENABLE, AppEnabledListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); // Display the first run wizard only on the files app, - $dispatcher->addListener(LoadAdditionalScriptsEvent::class, function() use ($server) { - /** @var IUserSession $userSession */ - $userSession = $this->getContainer()->query(IUserSession::class); - $user = $userSession->getUser(); - - if (!$user instanceof IUser) { - return; - } - - /** @var IConfig $config */ - $config = $this->getContainer()->query(IConfig::class); - $appHint = $this->getContainer()->query(AppHint::class); - - if ($config->getUserValue($user->getUID(), 'firstrunwizard', 'show', '1') !== '0') { - \OC_Util::addScript('firstrunwizard', 'activate'); - - $jobList = $this->getContainer()->getServer()->getJobList(); - $jobList->add('OCA\FirstRunWizard\Notification\BackgroundJob', ['uid' => $userSession->getUser()->getUID()]); - } - $appHint->sendAppHintNotifications(); - }); + $context->registerEventListener(LoadAdditionalScriptsEvent::class, BeforeFilesAppTemplateRenderedListener::class); } - protected function registerNotificationNotifier() { - $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); - - /** @var AppHint $appHint */ - $appHint = $this->getContainer()->query(AppHint::class); - $appHint->registerAppListener(); + public function boot(IBootContext $context): void { + $serverContainer = $context->getServerContainer(); + $serverContainer->getNotificationManager()->registerNotifierService(Notifier::class); } } diff --git a/lib/Listener/AppEnabledListener.php b/lib/Listener/AppEnabledListener.php new file mode 100644 index 000000000..a4ca125fa --- /dev/null +++ b/lib/Listener/AppEnabledListener.php @@ -0,0 +1,49 @@ + + * + * @author Morris Jobke + * + * @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\FirstRunWizard\Listener; + +use OCA\FirstRunWizard\Notification\AppHint; +use OCP\App\ManagerEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +class AppEnabledListener implements IEventListener { + /** @var AppHint */ + private $appHint; + + public function __construct(AppHint $appHint) { + $this->appHint = $appHint; + } + + public function handle(Event $event): void { + if (!$event instanceof ManagerEvent) { + return; + } + + $this->appHint->dismissNotification($event->getAppID()); + } +} diff --git a/lib/Listener/BeforeFilesAppTemplateRenderedListener.php b/lib/Listener/BeforeFilesAppTemplateRenderedListener.php new file mode 100644 index 000000000..5730c6d85 --- /dev/null +++ b/lib/Listener/BeforeFilesAppTemplateRenderedListener.php @@ -0,0 +1,73 @@ + + * + * @author Morris Jobke + * + * @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\FirstRunWizard\Listener; + +use OCA\FirstRunWizard\Notification\AppHint; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\IConfig; +use OCP\IUser; +use OCP\IUserSession; + +class BeforeFilesAppTemplateRenderedListener implements IEventListener { + /** @var IUserSession */ + private $userSession; + /** @var IConfig */ + private $config; + /** @var AppHint */ + private $appHint; + /** @var IJobList */ + private $jobList; + + public function __construct( + IConfig $config, + IUserSession $userSession, + IJobList $jobList, + AppHint $appHint + ) { + $this->userSession = $userSession; + $this->config = $config; + $this->appHint = $appHint; + $this->jobList = $jobList; + } + + public function handle(Event $event): void { + $user = $this->userSession->getUser(); + if (!$user instanceof IUser) { + return; + } + + if ($this->config->getUserValue($user->getUID(), 'firstrunwizard', 'show', '1') !== '0') { + \OC_Util::addScript('firstrunwizard', 'activate'); + + $this->jobList->add('OCA\FirstRunWizard\Notification\BackgroundJob', ['uid' => $this->userSession->getUser()->getUID()]); + } + $this->appHint->sendAppHintNotifications(); + } +} diff --git a/tests/AppInfo/AppPhpTest.php b/lib/Listener/BeforeTemplateRenderedListener.php similarity index 50% rename from tests/AppInfo/AppPhpTest.php rename to lib/Listener/BeforeTemplateRenderedListener.php index b9f9032ce..0de4b845a 100644 --- a/tests/AppInfo/AppPhpTest.php +++ b/lib/Listener/BeforeTemplateRenderedListener.php @@ -1,8 +1,11 @@ + * @copyright Copyright (c) 2020 Morris Jobke * - * @author Joas Schilling + * @author Morris Jobke * * @license GNU AGPL version 3 or any later version * @@ -17,22 +20,25 @@ * 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 . + * along with this program. If not, see . * */ -namespace OCA\FirstRunWizard\Tests\AppInfo; +namespace OCA\FirstRunWizard\Listener; -use Test\TestCase; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; -/** - * Class AppPhpTest - * - * @package OCA\FirstRunWizard\Tests\AppInfo - */ -class AppPhpTest extends TestCase { - public function testRoutes() { - $void = include(__DIR__ . '/../../appinfo/app.php'); - $this->assertSame(1, $void); // See http://de2.php.net/manual/de/function.include.php#example-137 +class BeforeTemplateRenderedListener implements IEventListener { + public function __construct() { + } + + public function handle(Event $event): void { + if (!$event instanceof BeforeTemplateRenderedEvent || !$event->isLoggedIn()) { + return; + } + + \OC_Util::addScript('firstrunwizard', 'about'); } } diff --git a/lib/Notification/AppHint.php b/lib/Notification/AppHint.php index 6e0351b26..b1473f06b 100644 --- a/lib/Notification/AppHint.php +++ b/lib/Notification/AppHint.php @@ -24,8 +24,6 @@ namespace OCA\FirstRunWizard\Notification; use OCP\App\IAppManager; -use OCP\App\ManagerEvent; -use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroupManager; use OCP\Notification\IManager as INotificationManager; @@ -44,20 +42,16 @@ class AppHint { /** @var IConfig */ protected $config; - /** @var IEventDispatcher */ - private $dispatcher; - /** @var string */ private $userId; const APP_HINT_VERSION = '18'; - public function __construct(INotificationManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IConfig $config, IEventDispatcher $eventDispatcher, $userId) { + public function __construct(INotificationManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IConfig $config, $userId) { $this->notificationManager = $notificationManager; $this->groupManager = $groupManager; $this->appManager =$appManager; $this->config = $config; - $this->dispatcher = $eventDispatcher; $this->userId = $userId; } @@ -72,12 +66,6 @@ public function sendAppHintNotifications(): void { } } - public function registerAppListener(): void { - $this->dispatcher->addListener(ManagerEvent::EVENT_APP_ENABLE, function(ManagerEvent $event) { - $this->dismissNotification($event->getAppID()); - }); - } - protected function sendNotification(string $app, string $user): void { $notification = $this->generateNotification($app, $user); if ( @@ -93,7 +81,7 @@ protected function sendNotification(string $app, string $user): void { } } - protected function dismissNotification(string $app) { + public function dismissNotification(string $app) { $notification = $this->notificationManager->createNotification(); $notification->setApp('firstrunwizard') ->setSubject('apphint-'. $app) diff --git a/tests/AppInfo/ApplicationTest.php b/tests/AppInfo/ApplicationTest.php index 0c55bda58..77db5e764 100644 --- a/tests/AppInfo/ApplicationTest.php +++ b/tests/AppInfo/ApplicationTest.php @@ -78,54 +78,4 @@ public function testContainerQuery($service, $expected) { $app = new Application(); $this->assertInstanceOf($expected, $app->getContainer()->query($service)); } - - public function dataRegister() { - return [ - [false, true], - [true, false], - ]; - } - - /** - * @dataProvider dataRegister - * @param bool $isCLI - * @param bool $register - */ - public function testRegister($isCLI, $register) { - /** @var Application|\PHPUnit_Framework_MockObject_MockObject $app */ - $app = $this->getMockBuilder(Application::class) - ->disableOriginalConstructor() - ->setMethods([ - 'registerScripts', - 'registerNotificationNotifier', - ]) - ->getMock(); - - if ($register) { - $app->expects($this->once()) - ->method('registerScripts'); - $app->expects($this->once()) - ->method('registerNotificationNotifier'); - } else { - $app->expects($this->never()) - ->method('registerScripts'); - $app->expects($this->never()) - ->method('registerNotificationNotifier'); - } - - $this->invokePrivate($app, 'isCLI', [$isCLI]); - $app->register(); - } - - public function testRegisterNotifier() { - $app = new Application(); - - $manager = $this->createMock(IManager::class); - $manager->expects($this->once()) - ->method('registerNotifierService') - ->with(Notifier::class); - - $this->overwriteService(IManager::class, $manager); - $this->invokePrivate($app, 'registerNotificationNotifier'); - } }