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