Provider.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Comments\Activity;
  7. use OCP\Activity\IEvent;
  8. use OCP\Activity\IManager;
  9. use OCP\Activity\IProvider;
  10. use OCP\Comments\ICommentsManager;
  11. use OCP\Comments\NotFoundException;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\IUserManager;
  15. use OCP\L10N\IFactory;
  16. class Provider implements IProvider {
  17. protected ?IL10N $l = null;
  18. public function __construct(
  19. protected IFactory $languageFactory,
  20. protected IURLGenerator $url,
  21. protected ICommentsManager $commentsManager,
  22. protected IUserManager $userManager,
  23. protected IManager $activityManager,
  24. ) {
  25. }
  26. /**
  27. * @param string $language
  28. * @param IEvent $event
  29. * @param IEvent|null $previousEvent
  30. * @return IEvent
  31. * @throws \InvalidArgumentException
  32. * @since 11.0.0
  33. */
  34. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  35. if ($event->getApp() !== 'comments') {
  36. throw new \InvalidArgumentException();
  37. }
  38. $this->l = $this->languageFactory->get('comments', $language);
  39. if ($event->getSubject() === 'add_comment_subject') {
  40. $this->parseMessage($event);
  41. if ($this->activityManager->getRequirePNG()) {
  42. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
  43. } else {
  44. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
  45. }
  46. if ($this->activityManager->isFormattingFilteredObject()) {
  47. try {
  48. return $this->parseShortVersion($event);
  49. } catch (\InvalidArgumentException $e) {
  50. // Ignore and simply use the long version...
  51. }
  52. }
  53. return $this->parseLongVersion($event);
  54. } else {
  55. throw new \InvalidArgumentException();
  56. }
  57. }
  58. /**
  59. * @throws \InvalidArgumentException
  60. */
  61. protected function parseShortVersion(IEvent $event): IEvent {
  62. $subjectParameters = $this->getSubjectParameters($event);
  63. if ($event->getSubject() === 'add_comment_subject') {
  64. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  65. $event->setRichSubject($this->l->t('You commented'), []);
  66. } else {
  67. $author = $this->generateUserParameter($subjectParameters['actor']);
  68. $event->setRichSubject($this->l->t('{author} commented'), [
  69. 'author' => $author,
  70. ]);
  71. }
  72. } else {
  73. throw new \InvalidArgumentException();
  74. }
  75. return $event;
  76. }
  77. /**
  78. * @throws \InvalidArgumentException
  79. */
  80. protected function parseLongVersion(IEvent $event): IEvent {
  81. $subjectParameters = $this->getSubjectParameters($event);
  82. if ($event->getSubject() === 'add_comment_subject') {
  83. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  84. $event->setParsedSubject($this->l->t('You commented on %1$s', [
  85. $subjectParameters['filePath'],
  86. ]))
  87. ->setRichSubject($this->l->t('You commented on {file}'), [
  88. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  89. ]);
  90. } else {
  91. $author = $this->generateUserParameter($subjectParameters['actor']);
  92. $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [
  93. $author['name'],
  94. $subjectParameters['filePath'],
  95. ]))
  96. ->setRichSubject($this->l->t('{author} commented on {file}'), [
  97. 'author' => $author,
  98. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  99. ]);
  100. }
  101. } else {
  102. throw new \InvalidArgumentException();
  103. }
  104. return $event;
  105. }
  106. protected function getSubjectParameters(IEvent $event): array {
  107. $subjectParameters = $event->getSubjectParameters();
  108. if (isset($subjectParameters['fileId'])) {
  109. return $subjectParameters;
  110. }
  111. // Fix subjects from 12.0.3 and older
  112. //
  113. // Do NOT Remove unless necessary
  114. // Removing this will break parsing of activities that were created on
  115. // Nextcloud 12, so we should keep this as long as it's acceptable.
  116. // Otherwise if people upgrade over multiple releases in a short period,
  117. // they will get the dead entries in their stream.
  118. return [
  119. 'actor' => $subjectParameters[0],
  120. 'fileId' => $event->getObjectId(),
  121. 'filePath' => trim($subjectParameters[1], '/'),
  122. ];
  123. }
  124. protected function parseMessage(IEvent $event): void {
  125. $messageParameters = $event->getMessageParameters();
  126. if (empty($messageParameters)) {
  127. // Email
  128. return;
  129. }
  130. $commentId = $messageParameters['commentId'] ?? $messageParameters[0];
  131. try {
  132. $comment = $this->commentsManager->get((string) $commentId);
  133. $message = $comment->getMessage();
  134. $mentionCount = 1;
  135. $mentions = [];
  136. foreach ($comment->getMentions() as $mention) {
  137. if ($mention['type'] !== 'user') {
  138. continue;
  139. }
  140. $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
  141. if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
  142. $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
  143. }
  144. $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
  145. $mentionCount++;
  146. }
  147. $event->setParsedMessage($comment->getMessage())
  148. ->setRichMessage($message, $mentions);
  149. } catch (NotFoundException $e) {
  150. }
  151. }
  152. protected function generateFileParameter(int $id, string $path): array {
  153. return [
  154. 'type' => 'file',
  155. 'id' => $id,
  156. 'name' => basename($path),
  157. 'path' => $path,
  158. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  159. ];
  160. }
  161. protected function generateUserParameter(string $uid): array {
  162. return [
  163. 'type' => 'user',
  164. 'id' => $uid,
  165. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  166. ];
  167. }
  168. }