1
0

Notifier.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\AlreadyProcessedException;
  32. use OCP\Notification\IManager;
  33. use OCP\Notification\INotification;
  34. use OCP\Notification\INotifier;
  35. use OCP\Util;
  36. class Notifier implements INotifier {
  37. /** @var IURLGenerator */
  38. protected $url;
  39. /** @var IConfig */
  40. protected $config;
  41. /** @var IManager */
  42. protected $notificationManager;
  43. /** @var IFactory */
  44. protected $l10NFactory;
  45. /** @var IUserSession */
  46. protected $userSession;
  47. /** @var IGroupManager */
  48. protected $groupManager;
  49. /** @var string[] */
  50. protected $appVersions;
  51. /**
  52. * Notifier constructor.
  53. *
  54. * @param IURLGenerator $url
  55. * @param IConfig $config
  56. * @param IManager $notificationManager
  57. * @param IFactory $l10NFactory
  58. * @param IUserSession $userSession
  59. * @param IGroupManager $groupManager
  60. */
  61. public function __construct(IURLGenerator $url, IConfig $config, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
  62. $this->url = $url;
  63. $this->notificationManager = $notificationManager;
  64. $this->config = $config;
  65. $this->l10NFactory = $l10NFactory;
  66. $this->userSession = $userSession;
  67. $this->groupManager = $groupManager;
  68. $this->appVersions = $this->getAppVersions();
  69. }
  70. /**
  71. * Identifier of the notifier, only use [a-z0-9_]
  72. *
  73. * @return string
  74. * @since 17.0.0
  75. */
  76. public function getID(): string {
  77. return 'updatenotification';
  78. }
  79. /**
  80. * Human readable name describing the notifier
  81. *
  82. * @return string
  83. * @since 17.0.0
  84. */
  85. public function getName(): string {
  86. return $this->l10NFactory->get('updatenotification')->t('Update notifications');
  87. }
  88. /**
  89. * @param INotification $notification
  90. * @param string $languageCode The code of the language that should be used to prepare the notification
  91. * @return INotification
  92. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  93. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  94. * @since 9.0.0
  95. */
  96. public function prepare(INotification $notification, string $languageCode): INotification {
  97. if ($notification->getApp() !== 'updatenotification') {
  98. throw new \InvalidArgumentException('Unknown app id');
  99. }
  100. $l = $this->l10NFactory->get('updatenotification', $languageCode);
  101. if ($notification->getSubject() === 'connection_error') {
  102. $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', 0);
  103. if ($errors === 0) {
  104. $this->notificationManager->markProcessed($notification);
  105. throw new \InvalidArgumentException('Update checked worked again');
  106. }
  107. $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors]))
  108. ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.'));
  109. } elseif ($notification->getObjectType() === 'core') {
  110. $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
  111. $parameters = $notification->getSubjectParameters();
  112. $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]));
  113. if ($this->isAdmin()) {
  114. $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
  115. }
  116. } else {
  117. $appInfo = $this->getAppInfo($notification->getObjectType());
  118. $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
  119. if (isset($this->appVersions[$notification->getObjectType()])) {
  120. $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
  121. }
  122. $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()]))
  123. ->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
  124. 'app' => [
  125. 'type' => 'app',
  126. 'id' => $notification->getObjectType(),
  127. 'name' => $appName,
  128. ]
  129. ]);
  130. if ($this->isAdmin()) {
  131. $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
  132. }
  133. }
  134. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
  135. return $notification;
  136. }
  137. /**
  138. * Remove the notification and prevent rendering, when the update is installed
  139. *
  140. * @param INotification $notification
  141. * @param string $installedVersion
  142. * @throws AlreadyProcessedException When the update is already installed
  143. */
  144. protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
  145. if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
  146. throw new AlreadyProcessedException();
  147. }
  148. }
  149. /**
  150. * @return bool
  151. */
  152. protected function isAdmin(): bool {
  153. $user = $this->userSession->getUser();
  154. if ($user instanceof IUser) {
  155. return $this->groupManager->isAdmin($user->getUID());
  156. }
  157. return false;
  158. }
  159. protected function getCoreVersions(): string {
  160. return implode('.', Util::getVersion());
  161. }
  162. protected function getAppVersions(): array {
  163. return \OC_App::getAppVersions();
  164. }
  165. protected function getAppInfo($appId) {
  166. return \OC_App::getAppInfo($appId);
  167. }
  168. }