Application.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\UpdateNotification\AppInfo;
  28. use OCA\UpdateNotification\Notification\Notifier;
  29. use OCA\UpdateNotification\UpdateChecker;
  30. use OCP\App\IAppManager;
  31. use OCP\AppFramework\App;
  32. use OCP\AppFramework\Bootstrap\IBootContext;
  33. use OCP\AppFramework\Bootstrap\IBootstrap;
  34. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  35. use OCP\AppFramework\IAppContainer;
  36. use OCP\AppFramework\QueryException;
  37. use OCP\IConfig;
  38. use OCP\IGroupManager;
  39. use OCP\ILogger;
  40. use OCP\IUser;
  41. use OCP\IUserSession;
  42. use OCP\Notification\IManager as INotificationManager;
  43. use OCP\Util;
  44. class Application extends App implements IBootstrap {
  45. public function __construct() {
  46. parent::__construct('updatenotification', []);
  47. }
  48. public function register(IRegistrationContext $context): void {
  49. }
  50. public function boot(IBootContext $context): void {
  51. $context->injectFn(function (IConfig $config,
  52. INotificationManager $notificationsManager,
  53. IUserSession $userSession,
  54. IAppManager $appManager,
  55. IGroupManager $groupManager,
  56. IAppContainer $appContainer,
  57. ILogger $logger) {
  58. if ($config->getSystemValue('updatechecker', true) !== true) {
  59. // Updater check is disabled
  60. return;
  61. }
  62. // Always register the notifier, so background jobs (without a user) can send push notifications
  63. $notificationsManager->registerNotifierService(Notifier::class);
  64. $user = $userSession->getUser();
  65. if (!$user instanceof IUser) {
  66. // Nothing to do for guests
  67. return;
  68. }
  69. if (!$appManager->isEnabledForUser('notifications') &&
  70. $groupManager->isAdmin($user->getUID())) {
  71. try {
  72. $updateChecker = $appContainer->get(UpdateChecker::class);
  73. } catch (QueryException $e) {
  74. $logger->logException($e);
  75. return;
  76. }
  77. if ($updateChecker->getUpdateState() !== []) {
  78. Util::addScript('updatenotification', 'legacy-notification');
  79. \OC_Hook::connect('\OCP\Config', 'js', $updateChecker, 'populateJavaScriptVariables');
  80. }
  81. }
  82. });
  83. }
  84. }