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

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