Application.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UpdateNotification\AppInfo;
  8. use OCA\UpdateNotification\Listener\AppUpdateEventListener;
  9. use OCA\UpdateNotification\Listener\BeforeTemplateRenderedEventListener;
  10. use OCA\UpdateNotification\Notification\AppUpdateNotifier;
  11. use OCA\UpdateNotification\Notification\Notifier;
  12. use OCA\UpdateNotification\UpdateChecker;
  13. use OCP\App\Events\AppUpdateEvent;
  14. use OCP\App\IAppManager;
  15. use OCP\AppFramework\App;
  16. use OCP\AppFramework\Bootstrap\IBootContext;
  17. use OCP\AppFramework\Bootstrap\IBootstrap;
  18. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  19. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  20. use OCP\IConfig;
  21. use OCP\IGroupManager;
  22. use OCP\IUser;
  23. use OCP\IUserSession;
  24. use OCP\Util;
  25. use Psr\Container\ContainerExceptionInterface;
  26. use Psr\Container\ContainerInterface;
  27. use Psr\Log\LoggerInterface;
  28. class Application extends App implements IBootstrap {
  29. public const APP_NAME = 'updatenotification';
  30. public function __construct() {
  31. parent::__construct(self::APP_NAME, []);
  32. }
  33. public function register(IRegistrationContext $context): void {
  34. $context->registerNotifierService(Notifier::class);
  35. $context->registerNotifierService(AppUpdateNotifier::class);
  36. $context->registerEventListener(AppUpdateEvent::class, AppUpdateEventListener::class);
  37. $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedEventListener::class);
  38. }
  39. public function boot(IBootContext $context): void {
  40. $context->injectFn(function (IConfig $config,
  41. IUserSession $userSession,
  42. IAppManager $appManager,
  43. IGroupManager $groupManager,
  44. ContainerInterface $container,
  45. LoggerInterface $logger,
  46. ): void {
  47. if ($config->getSystemValue('updatechecker', true) !== true) {
  48. // Updater check is disabled
  49. return;
  50. }
  51. $user = $userSession->getUser();
  52. if (!$user instanceof IUser) {
  53. // Nothing to do for guests
  54. return;
  55. }
  56. if (!$appManager->isEnabledForUser('notifications') &&
  57. $groupManager->isAdmin($user->getUID())) {
  58. try {
  59. $updateChecker = $container->get(UpdateChecker::class);
  60. } catch (ContainerExceptionInterface $e) {
  61. $logger->error($e->getMessage(), ['exception' => $e]);
  62. return;
  63. }
  64. if ($updateChecker->getUpdateState() !== []) {
  65. Util::addScript('updatenotification', 'update-notification-legacy');
  66. $updateChecker->setInitialState();
  67. }
  68. }
  69. });
  70. }
  71. }