Application.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if ($config->getSystemValue('updatechecker', true) !== true) {
  47. // Updater check is disabled
  48. return;
  49. }
  50. $user = $userSession->getUser();
  51. if (!$user instanceof IUser) {
  52. // Nothing to do for guests
  53. return;
  54. }
  55. if (!$appManager->isEnabledForUser('notifications') &&
  56. $groupManager->isAdmin($user->getUID())) {
  57. try {
  58. $updateChecker = $container->get(UpdateChecker::class);
  59. } catch (ContainerExceptionInterface $e) {
  60. $logger->error($e->getMessage(), ['exception' => $e]);
  61. return;
  62. }
  63. if ($updateChecker->getUpdateState() !== []) {
  64. Util::addScript('updatenotification', 'legacy-notification');
  65. \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
  66. }
  67. }
  68. });
  69. }
  70. }