Notifier.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Comments\Notification;
  8. use OCP\Comments\IComment;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\Comments\NotFoundException;
  11. use OCP\Files\IRootFolder;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use OCP\L10N\IFactory;
  15. use OCP\Notification\AlreadyProcessedException;
  16. use OCP\Notification\INotification;
  17. use OCP\Notification\INotifier;
  18. use OCP\Notification\UnknownNotificationException;
  19. class Notifier implements INotifier {
  20. public function __construct(
  21. protected IFactory $l10nFactory,
  22. protected IRootFolder $rootFolder,
  23. protected ICommentsManager $commentsManager,
  24. protected IURLGenerator $url,
  25. protected IUserManager $userManager,
  26. ) {
  27. }
  28. /**
  29. * Identifier of the notifier, only use [a-z0-9_]
  30. *
  31. * @return string
  32. * @since 17.0.0
  33. */
  34. public function getID(): string {
  35. return 'comments';
  36. }
  37. /**
  38. * Human readable name describing the notifier
  39. *
  40. * @return string
  41. * @since 17.0.0
  42. */
  43. public function getName(): string {
  44. return $this->l10nFactory->get('comments')->t('Comments');
  45. }
  46. /**
  47. * @param INotification $notification
  48. * @param string $languageCode The code of the language that should be used to prepare the notification
  49. * @return INotification
  50. * @throws UnknownNotificationException When the notification was not prepared by a notifier
  51. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  52. * @since 9.0.0
  53. */
  54. public function prepare(INotification $notification, string $languageCode): INotification {
  55. if ($notification->getApp() !== 'comments') {
  56. throw new UnknownNotificationException();
  57. }
  58. try {
  59. $comment = $this->commentsManager->get($notification->getObjectId());
  60. } catch (NotFoundException $e) {
  61. // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all
  62. throw new UnknownNotificationException('Comment not found', 0, $e);
  63. }
  64. $l = $this->l10nFactory->get('comments', $languageCode);
  65. $displayName = $comment->getActorId();
  66. $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER;
  67. if ($comment->getActorType() === 'users') {
  68. $commenter = $this->userManager->getDisplayName($comment->getActorId());
  69. if ($commenter !== null) {
  70. $displayName = $commenter;
  71. }
  72. }
  73. switch ($notification->getSubject()) {
  74. case 'mention':
  75. $parameters = $notification->getSubjectParameters();
  76. if ($parameters[0] !== 'files') {
  77. throw new UnknownNotificationException('Unsupported comment object');
  78. }
  79. $userFolder = $this->rootFolder->getUserFolder($notification->getUser());
  80. $nodes = $userFolder->getById((int)$parameters[1]);
  81. if (empty($nodes)) {
  82. throw new AlreadyProcessedException();
  83. }
  84. $node = $nodes[0];
  85. $path = rtrim($node->getPath(), '/');
  86. if (str_starts_with($path, '/' . $notification->getUser() . '/files/')) {
  87. // Remove /user/files/...
  88. $fullPath = $path;
  89. [,,, $path] = explode('/', $fullPath, 4);
  90. }
  91. $subjectParameters = [
  92. 'file' => [
  93. 'type' => 'file',
  94. 'id' => $comment->getObjectId(),
  95. 'name' => $node->getName(),
  96. 'path' => $path,
  97. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
  98. ],
  99. ];
  100. if ($isDeletedActor) {
  101. $subject = $l->t('You were mentioned on "{file}", in a comment by an account that has since been deleted');
  102. } else {
  103. $subject = $l->t('{user} mentioned you in a comment on "{file}"');
  104. $subjectParameters['user'] = [
  105. 'type' => 'user',
  106. 'id' => $comment->getActorId(),
  107. 'name' => $displayName,
  108. ];
  109. }
  110. [$message, $messageParameters] = $this->commentToRichMessage($comment);
  111. $notification->setRichSubject($subject, $subjectParameters)
  112. ->setRichMessage($message, $messageParameters)
  113. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')))
  114. ->setLink($this->url->linkToRouteAbsolute(
  115. 'comments.Notifications.view',
  116. ['id' => $comment->getId()])
  117. );
  118. return $notification;
  119. break;
  120. default:
  121. throw new UnknownNotificationException('Invalid subject');
  122. }
  123. }
  124. public function commentToRichMessage(IComment $comment): array {
  125. $message = $comment->getMessage();
  126. $messageParameters = [];
  127. $mentionTypeCount = [];
  128. $mentions = $comment->getMentions();
  129. foreach ($mentions as $mention) {
  130. if ($mention['type'] === 'user') {
  131. $userDisplayName = $this->userManager->getDisplayName($mention['id']);
  132. if ($userDisplayName === null) {
  133. continue;
  134. }
  135. }
  136. if (!array_key_exists($mention['type'], $mentionTypeCount)) {
  137. $mentionTypeCount[$mention['type']] = 0;
  138. }
  139. $mentionTypeCount[$mention['type']]++;
  140. // To keep a limited character set in parameter IDs ([a-zA-Z0-9-])
  141. // the mention parameter ID does not include the mention ID (which
  142. // could contain characters like '@' for user IDs) but a one-based
  143. // index of the mentions of that type.
  144. $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']];
  145. $message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message);
  146. if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
  147. $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message);
  148. }
  149. try {
  150. $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
  151. } catch (\OutOfBoundsException $e) {
  152. // There is no registered display name resolver for the mention
  153. // type, so the client decides what to display.
  154. $displayName = '';
  155. }
  156. $messageParameters[$mentionParameterId] = [
  157. 'type' => $mention['type'],
  158. 'id' => $mention['id'],
  159. 'name' => $displayName
  160. ];
  161. }
  162. return [$message, $messageParameters];
  163. }
  164. }