1
0

Notifier.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if ($notification->getSubject() !== 'update_available' && $notification->getSubject() !== 'connection_error') {
  103. throw new \InvalidArgumentException('Unknown subject');
  104. }
  105. $l = $this->l10NFactory->get('updatenotification', $languageCode);
  106. if ($notification->getSubject() === 'connection_error') {
  107. $errors = (int) $this->config->getAppValue('updatenotification', 'update_check_errors', '0');
  108. if ($errors === 0) {
  109. $this->notificationManager->markProcessed($notification);
  110. throw new \InvalidArgumentException('Update checked worked again');
  111. }
  112. $notification->setParsedSubject($l->t('The update server could not be reached since %d days to check for new updates.', [$errors]))
  113. ->setParsedMessage($l->t('Please check the Nextcloud and server log files for errors.'));
  114. } else {
  115. if ($notification->getObjectType() === 'core') {
  116. $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
  117. $parameters = $notification->getSubjectParameters();
  118. $notification->setRichSubject($l->t('Update to {serverAndVersion} is available.'), [
  119. 'serverAndVersion' => [
  120. 'type' => 'highlight',
  121. 'id' => $notification->getObjectType(),
  122. 'name' => $parameters['version'],
  123. ]
  124. ]);
  125. if ($this->isAdmin()) {
  126. $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index', ['section' => 'overview']) . '#version');
  127. }
  128. } else {
  129. $appInfo = $this->getAppInfo($notification->getObjectType(), $languageCode);
  130. $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
  131. if (isset($this->appVersions[$notification->getObjectType()])) {
  132. $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
  133. }
  134. $notification->setRichSubject($l->t('Update for {app} to version %s is available.', [$notification->getObjectId()]), [
  135. 'app' => [
  136. 'type' => 'app',
  137. 'id' => $notification->getObjectType(),
  138. 'name' => $appName,
  139. ]
  140. ]);
  141. if ($this->isAdmin()) {
  142. $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps', ['category' => 'updates']) . '#app-' . $notification->getObjectType());
  143. }
  144. }
  145. }
  146. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
  147. return $notification;
  148. }
  149. /**
  150. * Remove the notification and prevent rendering, when the update is installed
  151. *
  152. * @param INotification $notification
  153. * @param string $installedVersion
  154. * @throws AlreadyProcessedException When the update is already installed
  155. */
  156. protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
  157. if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
  158. throw new AlreadyProcessedException();
  159. }
  160. }
  161. /**
  162. * @return bool
  163. */
  164. protected function isAdmin(): bool {
  165. $user = $this->userSession->getUser();
  166. if ($user instanceof IUser) {
  167. return $this->groupManager->isAdmin($user->getUID());
  168. }
  169. return false;
  170. }
  171. protected function getCoreVersions(): string {
  172. return implode('.', Util::getVersion());
  173. }
  174. protected function getAppVersions(): array {
  175. return \OC_App::getAppVersions();
  176. }
  177. protected function getAppInfo($appId, $languageCode) {
  178. return \OCP\Server::get(IAppManager::class)->getAppInfo($appId, false, $languageCode);
  179. }
  180. }