Provider.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 IFactory $languageFactory;
  37. protected ?IL10N $l = null;
  38. protected IUrlGenerator $url;
  39. protected ICommentsManager $commentsManager;
  40. protected IUserManager $userManager;
  41. protected IManager $activityManager;
  42. public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) {
  43. $this->languageFactory = $languageFactory;
  44. $this->url = $url;
  45. $this->commentsManager = $commentsManager;
  46. $this->userManager = $userManager;
  47. $this->activityManager = $activityManager;
  48. }
  49. /**
  50. * @param string $language
  51. * @param IEvent $event
  52. * @param IEvent|null $previousEvent
  53. * @return IEvent
  54. * @throws \InvalidArgumentException
  55. * @since 11.0.0
  56. */
  57. public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
  58. if ($event->getApp() !== 'comments') {
  59. throw new \InvalidArgumentException();
  60. }
  61. $this->l = $this->languageFactory->get('comments', $language);
  62. if ($event->getSubject() === 'add_comment_subject') {
  63. $this->parseMessage($event);
  64. if ($this->activityManager->getRequirePNG()) {
  65. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png')));
  66. } else {
  67. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')));
  68. }
  69. if ($this->activityManager->isFormattingFilteredObject()) {
  70. try {
  71. return $this->parseShortVersion($event);
  72. } catch (\InvalidArgumentException $e) {
  73. // Ignore and simply use the long version...
  74. }
  75. }
  76. return $this->parseLongVersion($event);
  77. } else {
  78. throw new \InvalidArgumentException();
  79. }
  80. }
  81. /**
  82. * @throws \InvalidArgumentException
  83. */
  84. protected function parseShortVersion(IEvent $event): IEvent {
  85. $subjectParameters = $this->getSubjectParameters($event);
  86. if ($event->getSubject() === 'add_comment_subject') {
  87. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  88. $event->setRichSubject($this->l->t('You commented'), []);
  89. } else {
  90. $author = $this->generateUserParameter($subjectParameters['actor']);
  91. $event->setRichSubject($this->l->t('{author} commented'), [
  92. 'author' => $author,
  93. ]);
  94. }
  95. } else {
  96. throw new \InvalidArgumentException();
  97. }
  98. return $event;
  99. }
  100. /**
  101. * @throws \InvalidArgumentException
  102. */
  103. protected function parseLongVersion(IEvent $event): IEvent {
  104. $subjectParameters = $this->getSubjectParameters($event);
  105. if ($event->getSubject() === 'add_comment_subject') {
  106. if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) {
  107. $event->setParsedSubject($this->l->t('You commented on %1$s', [
  108. $subjectParameters['filePath'],
  109. ]))
  110. ->setRichSubject($this->l->t('You commented on {file}'), [
  111. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  112. ]);
  113. } else {
  114. $author = $this->generateUserParameter($subjectParameters['actor']);
  115. $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [
  116. $author['name'],
  117. $subjectParameters['filePath'],
  118. ]))
  119. ->setRichSubject($this->l->t('{author} commented on {file}'), [
  120. 'author' => $author,
  121. 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']),
  122. ]);
  123. }
  124. } else {
  125. throw new \InvalidArgumentException();
  126. }
  127. return $event;
  128. }
  129. protected function getSubjectParameters(IEvent $event): array {
  130. $subjectParameters = $event->getSubjectParameters();
  131. if (isset($subjectParameters['fileId'])) {
  132. return $subjectParameters;
  133. }
  134. // Fix subjects from 12.0.3 and older
  135. //
  136. // Do NOT Remove unless necessary
  137. // Removing this will break parsing of activities that were created on
  138. // Nextcloud 12, so we should keep this as long as it's acceptable.
  139. // Otherwise if people upgrade over multiple releases in a short period,
  140. // they will get the dead entries in their stream.
  141. return [
  142. 'actor' => $subjectParameters[0],
  143. 'fileId' => $event->getObjectId(),
  144. 'filePath' => trim($subjectParameters[1], '/'),
  145. ];
  146. }
  147. protected function parseMessage(IEvent $event): void {
  148. $messageParameters = $event->getMessageParameters();
  149. if (empty($messageParameters)) {
  150. // Email
  151. return;
  152. }
  153. $commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0];
  154. try {
  155. $comment = $this->commentsManager->get((string) $commentId);
  156. $message = $comment->getMessage();
  157. $mentionCount = 1;
  158. $mentions = [];
  159. foreach ($comment->getMentions() as $mention) {
  160. if ($mention['type'] !== 'user') {
  161. continue;
  162. }
  163. $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message);
  164. if (!str_contains($mention['id'], ' ') && !str_starts_with($mention['id'], 'guest/')) {
  165. $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message);
  166. }
  167. $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']);
  168. $mentionCount++;
  169. }
  170. $event->setParsedMessage($comment->getMessage())
  171. ->setRichMessage($message, $mentions);
  172. } catch (NotFoundException $e) {
  173. }
  174. }
  175. protected function generateFileParameter(int $id, string $path): array {
  176. return [
  177. 'type' => 'file',
  178. 'id' => $id,
  179. 'name' => basename($path),
  180. 'path' => $path,
  181. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  182. ];
  183. }
  184. protected function generateUserParameter(string $uid): array {
  185. return [
  186. 'type' => 'user',
  187. 'id' => $uid,
  188. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  189. ];
  190. }
  191. }