Provider.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Exceptions\UnknownActivityException;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IManager;
  10. use OCP\Activity\IProvider;
  11. use OCP\Comments\ICommentsManager;
  12. use OCP\Comments\NotFoundException;
  13. use OCP\IL10N;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserManager;
  16. use OCP\L10N\IFactory;
  17. class Provider implements IProvider {
  18. protected ?IL10N $l = null;
  19. public function __construct(
  20. protected IFactory $languageFactory,
  21. protected IURLGenerator $url,
  22. protected ICommentsManager $commentsManager,
  23. protected IUserManager $userManager,
  24. protected IManager $activityManager,
  25. ) {
  26. }
  27. /**
  28. * @param string $language
  29. * @param IEvent $event
  30. * @param IEvent|null $previousEvent
  31. * @return IEvent
  32. * @throws UnknownActivityException
  33. * @since 11.0.0
  34. */
  35. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  36. if ($event->getApp() !== 'comments') {
  37. throw new UnknownActivityException();
  38. }
  39. $this->l = $this->languageFactory->get('comments', $language);
  40. if ($event->getSubject() === 'add_comment_subject') {
  41. $this->parseMessage($event);
  42. if ($this->activityManager->getRequirePNG()) {
  43. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
  44. } else {
  45. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
  46. }
  47. if ($this->activityManager->isFormattingFilteredObject()) {
  48. try {
  49. return $this->parseShortVersion($event);
  50. } catch (UnknownActivityException) {
  51. // Ignore and simply use the long version...
  52. }
  53. }
  54. return $this->parseLongVersion($event);
  55. }
  56. throw new UnknownActivityException();
  57. }
  58. /**
  59. * @throws UnknownActivityException
  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 UnknownActivityException();
  74. }
  75. return $event;
  76. }
  77. /**
  78. * @throws UnknownActivityException
  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 UnknownActivityException();
  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. }