You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notifier.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\UpdateNotification\Notification;
  25. use OCP\IConfig;
  26. use OCP\IGroupManager;
  27. use OCP\IURLGenerator;
  28. use OCP\IUser;
  29. use OCP\IUserSession;
  30. use OCP\L10N\IFactory;
  31. use OCP\Notification\IManager;
  32. use OCP\Notification\INotification;
  33. use OCP\Notification\INotifier;
  34. use OCP\Util;
  35. class Notifier implements INotifier {
  36. /** @var IURLGenerator */
  37. protected $url;
  38. /** @var IConfig */
  39. protected $config;
  40. /** @var IManager */
  41. protected $notificationManager;
  42. /** @var IFactory */
  43. protected $l10NFactory;
  44. /** @var IUserSession */
  45. protected $userSession;
  46. /** @var IGroupManager */
  47. protected $groupManager;
  48. /** @var string[] */
  49. protected $appVersions;
  50. /**
  51. * Notifier constructor.
  52. *
  53. * @param IURLGenerator $url
  54. * @param IConfig $config
  55. * @param IManager $notificationManager
  56. * @param IFactory $l10NFactory
  57. * @param IUserSession $userSession
  58. * @param IGroupManager $groupManager
  59. */
  60. public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
  61. $this->url = $url;
  62. $this->notificationManager = $notificationManager;
  63. $this->config = $config;
  64. $this->l10NFactory = $l10NFactory;
  65. $this->userSession = $userSession;
  66. $this->groupManager = $groupManager;
  67. $this->appVersions = $this->getAppVersions();
  68. }
  69. /**
  70. * @param INotification $notification
  71. * @param string $languageCode The code of the language that should be used to prepare the notification
  72. * @return INotification
  73. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  74. * @since 9.0.0
  75. */
  76. public function prepare(INotification $notification, $languageCode): INotification {
  77. if ($notification->getApp() !== 'updatenotification') {
  78. throw new \InvalidArgumentException('Unknown app id');
  79. }
  80. $l = $this->l10NFactory->get('updatenotification', $languageCode);
  81. if ($notification->getSubject() === 'connection_error') {
  82. $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0);
  83. if ($errors === 0) {
  84. $this->notificationManager->markProcessed($notification);
  85. throw new \InvalidArgumentException('Update checked worked again');
  86. }
  87. $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors]))
  88. ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.'));
  89. } elseif ($notification->getObjectType() === 'core') {
  90. $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
  91. $parameters = $notification->getSubjectParameters();
  92. $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]));
  93. if ($this->isAdmin()) {
  94. $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
  95. }
  96. } else {
  97. $appInfo = $this->getAppInfo($notification->getObjectType());
  98. $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
  99. if (isset($this->appVersions[$notification->getObjectType()])) {
  100. $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
  101. }
  102. $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()]))
  103. ->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
  104. 'app' => [
  105. 'type' => 'app',
  106. 'id' => $notification->getObjectType(),
  107. 'name' => $appName,
  108. ]
  109. ]);
  110. if ($this->isAdmin()) {
  111. $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
  112. }
  113. }
  114. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
  115. return $notification;
  116. }
  117. /**
  118. * Remove the notification and prevent rendering, when the update is installed
  119. *
  120. * @param INotification $notification
  121. * @param string $installedVersion
  122. * @throws \InvalidArgumentException When the update is already installed
  123. */
  124. protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
  125. if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
  126. $this->notificationManager->markProcessed($notification);
  127. throw new \InvalidArgumentException('Update already installed');
  128. }
  129. }
  130. /**
  131. * @return bool
  132. */
  133. protected function isAdmin(): bool {
  134. $user = $this->userSession->getUser();
  135. if ($user instanceof IUser) {
  136. return $this->groupManager->isAdmin($user->getUID());
  137. }
  138. return false;
  139. }
  140. protected function getCoreVersions(): string {
  141. return implode('.', Util::getVersion());
  142. }
  143. protected function getAppVersions(): array {
  144. return \OC_App::getAppVersions();
  145. }
  146. protected function getAppInfo($appId) {
  147. return \OC_App::getAppInfo($appId);
  148. }
  149. }