Notifier.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UpdateNotification\Notification;
  24. use OCP\IGroupManager;
  25. use OCP\IURLGenerator;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. use OCP\L10N\IFactory;
  29. use OCP\Notification\IManager;
  30. use OCP\Notification\INotification;
  31. use OCP\Notification\INotifier;
  32. class Notifier implements INotifier {
  33. /** @var IURLGenerator */
  34. protected $url;
  35. /** @var IManager */
  36. protected $notificationManager;
  37. /** @var IFactory */
  38. protected $l10NFactory;
  39. /** @var IUserSession */
  40. protected $userSession;
  41. /** @var IGroupManager */
  42. protected $groupManager;
  43. /** @var string[] */
  44. protected $appVersions;
  45. /**
  46. * Notifier constructor.
  47. *
  48. * @param IURLGenerator $url
  49. * @param IManager $notificationManager
  50. * @param IFactory $l10NFactory
  51. * @param IUserSession $userSession
  52. * @param IGroupManager $groupManager
  53. */
  54. public function __construct(IURLGenerator $url, IManager $notificationManager, IFactory $l10NFactory, IUserSession $userSession, IGroupManager $groupManager) {
  55. $this->url = $url;
  56. $this->notificationManager = $notificationManager;
  57. $this->l10NFactory = $l10NFactory;
  58. $this->userSession = $userSession;
  59. $this->groupManager = $groupManager;
  60. $this->appVersions = $this->getAppVersions();
  61. }
  62. /**
  63. * @param INotification $notification
  64. * @param string $languageCode The code of the language that should be used to prepare the notification
  65. * @return INotification
  66. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  67. * @since 9.0.0
  68. */
  69. public function prepare(INotification $notification, $languageCode) {
  70. if ($notification->getApp() !== 'updatenotification') {
  71. throw new \InvalidArgumentException();
  72. }
  73. $l = $this->l10NFactory->get('updatenotification', $languageCode);
  74. if ($notification->getObjectType() === 'core') {
  75. $this->updateAlreadyInstalledCheck($notification, $this->getCoreVersions());
  76. $parameters = $notification->getSubjectParameters();
  77. $notification->setParsedSubject($l->t('Update to %1$s is available.', [$parameters['version']]));
  78. if ($this->isAdmin()) {
  79. $notification->setLink($this->url->linkToRouteAbsolute('settings.AdminSettings.index') . '#updater');
  80. }
  81. } else {
  82. $appInfo = $this->getAppInfo($notification->getObjectType());
  83. $appName = ($appInfo === null) ? $notification->getObjectType() : $appInfo['name'];
  84. if (isset($this->appVersions[$notification->getObjectType()])) {
  85. $this->updateAlreadyInstalledCheck($notification, $this->appVersions[$notification->getObjectType()]);
  86. }
  87. $notification->setParsedSubject($l->t('Update for %1$s to version %2$s is available.', [$appName, $notification->getObjectId()]))
  88. ->setRichSubject($l->t('Update for {app} to version %s is available.', $notification->getObjectId()), [
  89. 'app' => [
  90. 'type' => 'app',
  91. 'id' => $notification->getObjectType(),
  92. 'name' => $appName,
  93. ]
  94. ]);
  95. if ($this->isAdmin()) {
  96. $notification->setLink($this->url->linkToRouteAbsolute('settings.AppSettings.viewApps') . '#app-' . $notification->getObjectType());
  97. }
  98. }
  99. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('updatenotification', 'notification.svg')));
  100. return $notification;
  101. }
  102. /**
  103. * Remove the notification and prevent rendering, when the update is installed
  104. *
  105. * @param INotification $notification
  106. * @param string $installedVersion
  107. * @throws \InvalidArgumentException When the update is already installed
  108. */
  109. protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
  110. if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
  111. $this->notificationManager->markProcessed($notification);
  112. throw new \InvalidArgumentException();
  113. }
  114. }
  115. /**
  116. * @return bool
  117. */
  118. protected function isAdmin() {
  119. $user = $this->userSession->getUser();
  120. if ($user instanceof IUser) {
  121. return $this->groupManager->isAdmin($user->getUID());
  122. }
  123. return false;
  124. }
  125. protected function getCoreVersions() {
  126. return implode('.', \OCP\Util::getVersion());
  127. }
  128. protected function getAppVersions() {
  129. return \OC_App::getAppVersions();
  130. }
  131. protected function getAppInfo($appId) {
  132. return \OC_App::getAppInfo($appId);
  133. }
  134. }