Notifier.php 6.3 KB

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