AppUpdateNotifier.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0-or-later
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Notification;
  26. use OCA\UpdateNotification\AppInfo\Application;
  27. use OCP\App\IAppManager;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserManager;
  30. use OCP\L10N\IFactory;
  31. use OCP\Notification\IAction;
  32. use OCP\Notification\IManager as INotificationManager;
  33. use OCP\Notification\INotification;
  34. use OCP\Notification\INotifier;
  35. use Psr\Log\LoggerInterface;
  36. class AppUpdateNotifier implements INotifier {
  37. public function __construct(
  38. private IFactory $l10nFactory,
  39. private INotificationManager $notificationManager,
  40. private IUserManager $userManager,
  41. private IURLGenerator $urlGenerator,
  42. private IAppManager $appManager,
  43. private LoggerInterface $logger,
  44. ) {
  45. }
  46. public function getID(): string {
  47. return 'updatenotification_app_updated';
  48. }
  49. /**
  50. * Human readable name describing the notifier
  51. */
  52. public function getName(): string {
  53. return $this->l10nFactory->get(Application::APP_NAME)->t('App updated');
  54. }
  55. /**
  56. * @param INotification $notification
  57. * @param string $languageCode The code of the language that should be used to prepare the notification
  58. * @return INotification
  59. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  60. */
  61. public function prepare(INotification $notification, string $languageCode): INotification {
  62. if ($notification->getApp() !== Application::APP_NAME) {
  63. throw new \InvalidArgumentException('Unknown app');
  64. }
  65. if ($notification->getSubject() !== 'app_updated') {
  66. throw new \InvalidArgumentException('Unknown subject');
  67. }
  68. $appId = $notification->getSubjectParameters()[0];
  69. $appInfo = $this->appManager->getAppInfo($appId, lang:$languageCode);
  70. if ($appInfo === null) {
  71. throw new \InvalidArgumentException('App info not found');
  72. }
  73. // Prepare translation factory for requested language
  74. $l = $this->l10nFactory->get(Application::APP_NAME, $languageCode);
  75. $icon = $this->appManager->getAppIcon($appId, true);
  76. if ($icon === null) {
  77. $icon = $this->urlGenerator->imagePath('core', 'actions/change.svg');
  78. }
  79. $action = $notification->createAction();
  80. $action
  81. ->setLabel($l->t('See what\'s new'))
  82. ->setParsedLabel($l->t('See what\'s new'))
  83. ->setLink($this->urlGenerator->linkToRouteAbsolute('updatenotification.Changelog.showChangelog', ['app' => $appId, 'version' => $this->appManager->getAppVersion($appId)]), IAction::TYPE_WEB);
  84. $notification
  85. ->setIcon($this->urlGenerator->getAbsoluteURL($icon))
  86. ->addParsedAction($action)
  87. ->setRichSubject(
  88. $l->t('{app} updated to version {version}'),
  89. [
  90. 'app' => [
  91. 'type' => 'app',
  92. 'id' => $appId,
  93. 'name' => $appInfo['name'],
  94. ],
  95. 'version' => [
  96. 'type' => 'highlight',
  97. 'id' => $appId,
  98. 'name' => $appInfo['version'],
  99. ],
  100. ],
  101. );
  102. return $notification;
  103. }
  104. }