1
0

AppUpdateNotifier.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UpdateNotification\Notification;
  8. use OCA\UpdateNotification\AppInfo\Application;
  9. use OCP\App\IAppManager;
  10. use OCP\IURLGenerator;
  11. use OCP\IUserManager;
  12. use OCP\L10N\IFactory;
  13. use OCP\Notification\AlreadyProcessedException;
  14. use OCP\Notification\IAction;
  15. use OCP\Notification\IManager as INotificationManager;
  16. use OCP\Notification\INotification;
  17. use OCP\Notification\INotifier;
  18. use OCP\Notification\UnknownNotificationException;
  19. use Psr\Log\LoggerInterface;
  20. class AppUpdateNotifier implements INotifier {
  21. public function __construct(
  22. private IFactory $l10nFactory,
  23. private INotificationManager $notificationManager,
  24. private IUserManager $userManager,
  25. private IURLGenerator $urlGenerator,
  26. private IAppManager $appManager,
  27. private LoggerInterface $logger,
  28. ) {
  29. }
  30. public function getID(): string {
  31. return 'updatenotification_app_updated';
  32. }
  33. /**
  34. * Human readable name describing the notifier
  35. */
  36. public function getName(): string {
  37. return $this->l10nFactory->get(Application::APP_NAME)->t('App updated');
  38. }
  39. /**
  40. * @param INotification $notification
  41. * @param string $languageCode The code of the language that should be used to prepare the notification
  42. * @return INotification
  43. * @throws UnknownNotificationException When the notification was not prepared by a notifier
  44. * @throws AlreadyProcessedException When the app is no longer known
  45. */
  46. public function prepare(INotification $notification, string $languageCode): INotification {
  47. if ($notification->getApp() !== Application::APP_NAME) {
  48. throw new UnknownNotificationException('Unknown app');
  49. }
  50. if ($notification->getSubject() !== 'app_updated') {
  51. throw new UnknownNotificationException('Unknown subject');
  52. }
  53. $appId = $notification->getSubjectParameters()[0];
  54. $appInfo = $this->appManager->getAppInfo($appId, lang:$languageCode);
  55. if ($appInfo === null) {
  56. throw new AlreadyProcessedException();
  57. }
  58. // Prepare translation factory for requested language
  59. $l = $this->l10nFactory->get(Application::APP_NAME, $languageCode);
  60. $icon = $this->appManager->getAppIcon($appId, true);
  61. if ($icon === null) {
  62. $icon = $this->urlGenerator->imagePath('core', 'actions/change.svg');
  63. }
  64. $action = $notification->createAction();
  65. $action
  66. ->setLabel($l->t('See what\'s new'))
  67. ->setParsedLabel($l->t('See what\'s new'))
  68. ->setLink($this->urlGenerator->linkToRouteAbsolute('updatenotification.Changelog.showChangelog', ['app' => $appId, 'version' => $this->appManager->getAppVersion($appId)]), IAction::TYPE_WEB);
  69. $notification
  70. ->setIcon($this->urlGenerator->getAbsoluteURL($icon))
  71. ->addParsedAction($action)
  72. ->setRichSubject(
  73. $l->t('{app} updated to version {version}'),
  74. [
  75. 'app' => [
  76. 'type' => 'app',
  77. 'id' => $appId,
  78. 'name' => $appInfo['name'],
  79. ],
  80. 'version' => [
  81. 'type' => 'highlight',
  82. 'id' => $appId,
  83. 'name' => $appInfo['version'],
  84. ],
  85. ],
  86. );
  87. return $notification;
  88. }
  89. }