1
0

Notifier.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\FilesReminders\Notification;
  25. use InvalidArgumentException;
  26. use OCA\FilesReminders\AppInfo\Application;
  27. use OCP\Files\FileInfo;
  28. use OCP\Files\IRootFolder;
  29. use OCP\IURLGenerator;
  30. use OCP\L10N\IFactory;
  31. use OCP\Notification\AlreadyProcessedException;
  32. use OCP\Notification\IAction;
  33. use OCP\Notification\INotification;
  34. use OCP\Notification\INotifier;
  35. class Notifier implements INotifier {
  36. public function __construct(
  37. protected IFactory $l10nFactory,
  38. protected IURLGenerator $urlGenerator,
  39. protected IRootFolder $root,
  40. ) {
  41. }
  42. public function getID(): string {
  43. return Application::APP_ID;
  44. }
  45. public function getName(): string {
  46. return $this->l10nFactory->get(Application::APP_ID)->t('File reminders');
  47. }
  48. /**
  49. * @throws InvalidArgumentException
  50. * @throws AlreadyProcessedException
  51. */
  52. public function prepare(INotification $notification, string $languageCode): INotification {
  53. $l = $this->l10nFactory->get(Application::APP_ID, $languageCode);
  54. if ($notification->getApp() !== Application::APP_ID) {
  55. throw new InvalidArgumentException();
  56. }
  57. switch ($notification->getSubject()) {
  58. case 'reminder-due':
  59. $params = $notification->getSubjectParameters();
  60. $fileId = $params['fileId'];
  61. $node = $this->root->getUserFolder($notification->getUser())->getFirstNodeById($fileId);
  62. if (!$node) {
  63. throw new InvalidArgumentException();
  64. }
  65. $path = rtrim($node->getPath(), '/');
  66. if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) {
  67. // Remove /user/files/...
  68. $fullPath = $path;
  69. [,,, $path] = explode('/', $fullPath, 4);
  70. }
  71. $link = $this->urlGenerator->linkToRouteAbsolute(
  72. 'files.viewcontroller.showFile',
  73. ['fileid' => $node->getId()],
  74. );
  75. // TRANSLATORS The name placeholder is for a file or folder name
  76. $subject = $l->t('Reminder for {name}');
  77. $notification
  78. ->setRichSubject(
  79. $subject,
  80. [
  81. 'name' => [
  82. 'type' => 'highlight',
  83. 'id' => $node->getId(),
  84. 'name' => $node->getName(),
  85. ],
  86. ],
  87. )
  88. ->setLink($link);
  89. $label = match ($node->getType()) {
  90. FileInfo::TYPE_FILE => $l->t('View file'),
  91. FileInfo::TYPE_FOLDER => $l->t('View folder'),
  92. };
  93. $this->addActionButton($notification, $label);
  94. break;
  95. default:
  96. throw new InvalidArgumentException();
  97. break;
  98. }
  99. return $notification;
  100. }
  101. protected function addActionButton(INotification $notification, string $label): void {
  102. $action = $notification->createAction();
  103. $action->setLabel($label)
  104. ->setParsedLabel($label)
  105. ->setLink($notification->getLink(), IAction::TYPE_WEB)
  106. ->setPrimary(true);
  107. $notification->addParsedAction($action);
  108. }
  109. }