Notifier.php 6.4 KB

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