1
0

Notifier.php 2.8 KB

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