1
0

Notifier.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ->setRichSubject($l->t('Update to {serverAndVersion} is available.'), [
  115. 'serverAndVersion' => [
  116. 'type' => 'highlight',
  117. 'id' => $notification->getObjectType(),
  118. 'name' => $parameters['version'],
  119. ]
  120. ]);
  121. if ($this->isAdmin()) {
  122. $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
  123. }
  124. } else {
  125. $appInfo = $this->getAppInfo($notification->getObjectType(), $languageCode);
  126. $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
  127. if (isset($this->appVersions[$notification->getObjectType()])) {
  128. $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
  129. }
  130. $notification->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
  131. 'app' => [
  132. 'type' => 'app',
  133. 'id' => $notification->getObjectType(),
  134. 'name' => $appName,
  135. ]
  136. ]);
  137. if ($this->isAdmin()) {
  138. $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
  139. }
  140. }
  141. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
  142. return $notification;
  143. }
  144. /**
  145. * Remove the notification and prevent rendering, when the update is installed
  146. *
  147. * @param INotification $notification
  148. * @param string $installedVersion
  149. * @throws AlreadyProcessedException When the update is already installed
  150. */
  151. protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
  152. if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
  153. throw new AlreadyProcessedException();
  154. }
  155. }
  156. /**
  157. * @return bool
  158. */
  159. protected function isAdmin(): bool {
  160. $user = $this->userSession->getUser();
  161. if ($user instanceof IUser) {
  162. return $this->groupManager->isAdmin($user->getUID());
  163. }
  164. return false;
  165. }
  166. protected function getCoreVersions(): string {
  167. return implode('.', Util::getVersion());
  168. }
  169. protected function getAppVersions(): array {
  170. return \OC_App::getAppVersions();
  171. }
  172. protected function getAppInfo($appId, $languageCode) {
  173. return \OC_App::getAppInfo($appId, false, $languageCode);
  174. }
  175. }