Provider.php 7.0 KB

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