Provider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Comments\Activity;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IManager;
  28. use OCP\Activity\IProvider;
  29. use OCP\Comments\ICommentsManager;
  30. use OCP\Comments\NotFoundException;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUserManager;
  34. use OCP\L10N\IFactory;
  35. class Provider implements IProvider {
  36. protected ?IL10N $l = null;
  37. public function __construct(
  38. protected IFactory $languageFactory,
  39. protected IURLGenerator $url,
  40. protected ICommentsManager $commentsManager,
  41. protected IUserManager $userManager,
  42. protected IManager $activityManager,
  43. ) {
  44. }
  45. /**
  46. * @param string $language
  47. * @param IEvent $event
  48. * @param IEvent|null $previousEvent
  49. * @return IEvent
  50. * @throws \InvalidArgumentException
  51. * @since 11.0.0
  52. */
  53. public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
  54. if ($event->getApp() !== 'comments') {
  55. throw new \InvalidArgumentException();
  56. }
  57. $this->l = $this->languageFactory->get('comments', $language);
  58. if ($event->getSubject() === 'add_comment_subject') {
  59. $this->parseMessage($event);
  60. if ($this->activityManager->getRequirePNG()) {
  61. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
  62. } else {
  63. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
  64. }
  65. if ($this->activityManager->isFormattingFilteredObject()) {
  66. try {
  67. return $this->parseShortVersion($event);
  68. } catch (\InvalidArgumentException $e) {
  69. // Ignore and simply use the long version...
  70. }
  71. }
  72. return $this->parseLongVersion($event);
  73. } else {
  74. throw new \InvalidArgumentException();
  75. }
  76. }
  77. /**
  78. * @throws \InvalidArgumentException
  79. */
  80. protected function parseShortVersion(IEvent $event): IEvent {
  81. $subjectParameters = $this->getSubjectParameters($event);
  82. if ($event->getSubject() === 'add_comment_subject') {
  83. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  84. $event->setRichSubject($this->l->t('You commented'), []);
  85. } else {
  86. $author = $this->generateUserParameter($subjectParameters['actor']);
  87. $event->setRichSubject($this->l->t('{author} commented'), [
  88. 'author' => $author,
  89. ]);
  90. }
  91. } else {
  92. throw new \InvalidArgumentException();
  93. }
  94. return $event;
  95. }
  96. /**
  97. * @throws \InvalidArgumentException
  98. */
  99. protected function parseLongVersion(IEvent $event): IEvent {
  100. $subjectParameters = $this->getSubjectParameters($event);
  101. if ($event->getSubject() === 'add_comment_subject') {
  102. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  103. $event->setParsedSubject($this->l->t('You commented on %1$s', [
  104. $subjectParameters['filePath'],
  105. ]))
  106. ->setRichSubject($this->l->t('You commented on {file}'), [
  107. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  108. ]);
  109. } else {
  110. $author = $this->generateUserParameter($subjectParameters['actor']);
  111. $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [
  112. $author['name'],
  113. $subjectParameters['filePath'],
  114. ]))
  115. ->setRichSubject($this->l->t('{author} commented on {file}'), [
  116. 'author' => $author,
  117. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  118. ]);
  119. }
  120. } else {
  121. throw new \InvalidArgumentException();
  122. }
  123. return $event;
  124. }
  125. protected function getSubjectParameters(IEvent $event): array {
  126. $subjectParameters = $event->getSubjectParameters();
  127. if (isset($subjectParameters['fileId'])) {
  128. return $subjectParameters;
  129. }
  130. // Fix subjects from 12.0.3 and older
  131. //
  132. // Do NOT Remove unless necessary
  133. // Removing this will break parsing of activities that were created on
  134. // Nextcloud 12, so we should keep this as long as it's acceptable.
  135. // Otherwise if people upgrade over multiple releases in a short period,
  136. // they will get the dead entries in their stream.
  137. return [
  138. 'actor' => $subjectParameters[0],
  139. 'fileId' => $event->getObjectId(),
  140. 'filePath' => trim($subjectParameters[1], '/'),
  141. ];
  142. }
  143. protected function parseMessage(IEvent $event): void {
  144. $messageParameters = $event->getMessageParameters();
  145. if (empty($messageParameters)) {
  146. // Email
  147. return;
  148. }
  149. $commentId = $messageParameters['commentId'] ?? $messageParameters[0];
  150. try {
  151. $comment = $this->commentsManager->get((string) $commentId);
  152. $message = $comment->getMessage();
  153. $mentionCount = 1;
  154. $mentions = [];
  155. foreach ($comment->getMentions() as $mention) {
  156. if ($mention['type'] !== 'user') {
  157. continue;
  158. }
  159. $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
  160. if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
  161. $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
  162. }
  163. $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
  164. $mentionCount++;
  165. }
  166. $event->setParsedMessage($comment->getMessage())
  167. ->setRichMessage($message, $mentions);
  168. } catch (NotFoundException $e) {
  169. }
  170. }
  171. protected function generateFileParameter(int $id, string $path): array {
  172. return [
  173. 'type' => 'file',
  174. 'id' => $id,
  175. 'name' => basename($path),
  176. 'path' => $path,
  177. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  178. ];
  179. }
  180. protected function generateUserParameter(string $uid): array {
  181. return [
  182. 'type' => 'user',
  183. 'id' => $uid,
  184. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  185. ];
  186. }
  187. }