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