1
0

Notifier.php 5.4 KB

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