You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Provider.php 7.8KB

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