1
0

Notifier.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\FilesReminders\Notification;
  8. use OCA\FilesReminders\AppInfo\Application;
  9. use OCP\Files\FileInfo;
  10. use OCP\Files\IRootFolder;
  11. use OCP\IURLGenerator;
  12. use OCP\L10N\IFactory;
  13. use OCP\Notification\IAction;
  14. use OCP\Notification\INotification;
  15. use OCP\Notification\INotifier;
  16. use OCP\Notification\UnknownNotificationException;
  17. class Notifier implements INotifier {
  18. public function __construct(
  19. protected IFactory $l10nFactory,
  20. protected IURLGenerator $urlGenerator,
  21. protected IRootFolder $root,
  22. ) {
  23. }
  24. public function getID(): string {
  25. return Application::APP_ID;
  26. }
  27. public function getName(): string {
  28. return $this->l10nFactory->get(Application::APP_ID)->t('File reminders');
  29. }
  30. /**
  31. * @throws UnknownNotificationException
  32. */
  33. public function prepare(INotification $notification, string $languageCode): INotification {
  34. $l = $this->l10nFactory->get(Application::APP_ID, $languageCode);
  35. if ($notification->getApp() !== Application::APP_ID) {
  36. throw new UnknownNotificationException();
  37. }
  38. switch ($notification->getSubject()) {
  39. case 'reminder-due':
  40. $params = $notification->getSubjectParameters();
  41. $fileId = $params['fileId'];
  42. $node = $this->root->getUserFolder($notification->getUser())->getFirstNodeById($fileId);
  43. if (!$node) {
  44. throw new UnknownNotificationException();
  45. }
  46. $path = rtrim($node->getPath(), '/');
  47. if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) {
  48. // Remove /user/files/...
  49. $fullPath = $path;
  50. [,,, $path] = explode('/', $fullPath, 4);
  51. }
  52. $link = $this->urlGenerator->linkToRouteAbsolute(
  53. 'files.viewcontroller.showFile',
  54. ['fileid' => $node->getId()],
  55. );
  56. // TRANSLATORS The name placeholder is for a file or folder name
  57. $subject = $l->t('Reminder for {name}');
  58. $notification
  59. ->setRichSubject(
  60. $subject,
  61. [
  62. 'name' => [
  63. 'type' => 'highlight',
  64. 'id' => $node->getId(),
  65. 'name' => $node->getName(),
  66. ],
  67. ],
  68. )
  69. ->setLink($link);
  70. $label = match ($node->getType()) {
  71. FileInfo::TYPE_FILE => $l->t('View file'),
  72. FileInfo::TYPE_FOLDER => $l->t('View folder'),
  73. };
  74. $this->addActionButton($notification, $label);
  75. break;
  76. default:
  77. throw new UnknownNotificationException();
  78. }
  79. return $notification;
  80. }
  81. protected function addActionButton(INotification $notification, string $label): void {
  82. $action = $notification->createAction();
  83. $action->setLabel($label)
  84. ->setParsedLabel($label)
  85. ->setLink($notification->getLink(), IAction::TYPE_WEB)
  86. ->setPrimary(true);
  87. $notification->addParsedAction($action);
  88. }
  89. }