@@ -39,195 +39,195 @@ |
||
| 39 | 39 | |
| 40 | 40 | class Notifier implements INotifier { |
| 41 | 41 | |
| 42 | - /** @var IFactory */ |
|
| 43 | - protected $l10nFactory; |
|
| 44 | - |
|
| 45 | - /** @var IRootFolder */ |
|
| 46 | - protected $rootFolder; |
|
| 47 | - |
|
| 48 | - /** @var ICommentsManager */ |
|
| 49 | - protected $commentsManager; |
|
| 50 | - |
|
| 51 | - /** @var IURLGenerator */ |
|
| 52 | - protected $url; |
|
| 53 | - |
|
| 54 | - /** @var IUserManager */ |
|
| 55 | - protected $userManager; |
|
| 56 | - |
|
| 57 | - public function __construct( |
|
| 58 | - IFactory $l10nFactory, |
|
| 59 | - IRootFolder $rootFolder, |
|
| 60 | - ICommentsManager $commentsManager, |
|
| 61 | - IURLGenerator $url, |
|
| 62 | - IUserManager $userManager |
|
| 63 | - ) { |
|
| 64 | - $this->l10nFactory = $l10nFactory; |
|
| 65 | - $this->rootFolder = $rootFolder; |
|
| 66 | - $this->commentsManager = $commentsManager; |
|
| 67 | - $this->url = $url; |
|
| 68 | - $this->userManager = $userManager; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Identifier of the notifier, only use [a-z0-9_] |
|
| 73 | - * |
|
| 74 | - * @return string |
|
| 75 | - * @since 17.0.0 |
|
| 76 | - */ |
|
| 77 | - public function getID(): string { |
|
| 78 | - return 'comments'; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Human readable name describing the notifier |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - * @since 17.0.0 |
|
| 86 | - */ |
|
| 87 | - public function getName(): string { |
|
| 88 | - return $this->l10nFactory->get('comments')->t('Comments'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param INotification $notification |
|
| 93 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 94 | - * @return INotification |
|
| 95 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 96 | - * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
|
| 97 | - * @since 9.0.0 |
|
| 98 | - */ |
|
| 99 | - public function prepare(INotification $notification, string $languageCode): INotification { |
|
| 100 | - if ($notification->getApp() !== 'comments') { |
|
| 101 | - throw new \InvalidArgumentException(); |
|
| 102 | - } |
|
| 103 | - try { |
|
| 104 | - $comment = $this->commentsManager->get($notification->getObjectId()); |
|
| 105 | - } catch (NotFoundException $e) { |
|
| 106 | - // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
|
| 107 | - throw new \InvalidArgumentException('Comment not found', 0, $e); |
|
| 108 | - } |
|
| 109 | - $l = $this->l10nFactory->get('comments', $languageCode); |
|
| 110 | - $displayName = $comment->getActorId(); |
|
| 111 | - $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
|
| 112 | - if ($comment->getActorType() === 'users') { |
|
| 113 | - $commenter = $this->userManager->get($comment->getActorId()); |
|
| 114 | - if ($commenter instanceof IUser) { |
|
| 115 | - $displayName = $commenter->getDisplayName(); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - switch ($notification->getSubject()) { |
|
| 120 | - case 'mention': |
|
| 121 | - $parameters = $notification->getSubjectParameters(); |
|
| 122 | - if ($parameters[0] !== 'files') { |
|
| 123 | - throw new \InvalidArgumentException('Unsupported comment object'); |
|
| 124 | - } |
|
| 125 | - $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
|
| 126 | - $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 127 | - if (empty($nodes)) { |
|
| 128 | - throw new AlreadyProcessedException(); |
|
| 129 | - } |
|
| 130 | - $node = $nodes[0]; |
|
| 131 | - |
|
| 132 | - $path = rtrim($node->getPath(), '/'); |
|
| 133 | - if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 134 | - // Remove /user/files/... |
|
| 135 | - $fullPath = $path; |
|
| 136 | - list(,,, $path) = explode('/', $fullPath, 4); |
|
| 137 | - } |
|
| 138 | - $subjectParameters = [ |
|
| 139 | - 'file' => [ |
|
| 140 | - 'type' => 'file', |
|
| 141 | - 'id' => $comment->getObjectId(), |
|
| 142 | - 'name' => $node->getName(), |
|
| 143 | - 'path' => $path, |
|
| 144 | - 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
|
| 145 | - ], |
|
| 146 | - ]; |
|
| 147 | - |
|
| 148 | - if ($isDeletedActor) { |
|
| 149 | - $subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
|
| 150 | - } else { |
|
| 151 | - $subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
|
| 152 | - $subjectParameters['user'] = [ |
|
| 153 | - 'type' => 'user', |
|
| 154 | - 'id' => $comment->getActorId(), |
|
| 155 | - 'name' => $displayName, |
|
| 156 | - ]; |
|
| 157 | - } |
|
| 158 | - list($message, $messageParameters) = $this->commentToRichMessage($comment); |
|
| 159 | - $notification->setRichSubject($subject, $subjectParameters) |
|
| 160 | - ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
|
| 161 | - ->setRichMessage($message, $messageParameters) |
|
| 162 | - ->setParsedMessage($this->richToParsed($message, $messageParameters)) |
|
| 163 | - ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
|
| 164 | - ->setLink($this->url->linkToRouteAbsolute( |
|
| 165 | - 'comments.Notifications.view', |
|
| 166 | - ['id' => $comment->getId()]) |
|
| 167 | - ); |
|
| 168 | - |
|
| 169 | - return $notification; |
|
| 170 | - break; |
|
| 171 | - |
|
| 172 | - default: |
|
| 173 | - throw new \InvalidArgumentException('Invalid subject'); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - public function commentToRichMessage(IComment $comment): array { |
|
| 178 | - $message = $comment->getMessage(); |
|
| 179 | - $messageParameters = []; |
|
| 180 | - $mentionTypeCount = []; |
|
| 181 | - $mentions = $comment->getMentions(); |
|
| 182 | - foreach ($mentions as $mention) { |
|
| 183 | - if ($mention['type'] === 'user') { |
|
| 184 | - $user = $this->userManager->get($mention['id']); |
|
| 185 | - if (!$user instanceof IUser) { |
|
| 186 | - continue; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - if (!array_key_exists($mention['type'], $mentionTypeCount)) { |
|
| 190 | - $mentionTypeCount[$mention['type']] = 0; |
|
| 191 | - } |
|
| 192 | - $mentionTypeCount[$mention['type']]++; |
|
| 193 | - // To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) |
|
| 194 | - // the mention parameter ID does not include the mention ID (which |
|
| 195 | - // could contain characters like '@' for user IDs) but a one-based |
|
| 196 | - // index of the mentions of that type. |
|
| 197 | - $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 198 | - $message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message); |
|
| 199 | - if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
|
| 200 | - $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - try { |
|
| 204 | - $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
| 205 | - } catch (\OutOfBoundsException $e) { |
|
| 206 | - // There is no registered display name resolver for the mention |
|
| 207 | - // type, so the client decides what to display. |
|
| 208 | - $displayName = ''; |
|
| 209 | - } |
|
| 210 | - $messageParameters[$mentionParameterId] = [ |
|
| 211 | - 'type' => $mention['type'], |
|
| 212 | - 'id' => $mention['id'], |
|
| 213 | - 'name' => $displayName |
|
| 214 | - ]; |
|
| 215 | - } |
|
| 216 | - return [$message, $messageParameters]; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public function richToParsed(string $message, array $parameters): string { |
|
| 220 | - $placeholders = $replacements = []; |
|
| 221 | - foreach ($parameters as $placeholder => $parameter) { |
|
| 222 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 223 | - if ($parameter['type'] === 'user') { |
|
| 224 | - $replacements[] = '@' . $parameter['name']; |
|
| 225 | - } elseif ($parameter['type'] === 'file') { |
|
| 226 | - $replacements[] = $parameter['path']; |
|
| 227 | - } else { |
|
| 228 | - $replacements[] = $parameter['name']; |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - return str_replace($placeholders, $replacements, $message); |
|
| 232 | - } |
|
| 42 | + /** @var IFactory */ |
|
| 43 | + protected $l10nFactory; |
|
| 44 | + |
|
| 45 | + /** @var IRootFolder */ |
|
| 46 | + protected $rootFolder; |
|
| 47 | + |
|
| 48 | + /** @var ICommentsManager */ |
|
| 49 | + protected $commentsManager; |
|
| 50 | + |
|
| 51 | + /** @var IURLGenerator */ |
|
| 52 | + protected $url; |
|
| 53 | + |
|
| 54 | + /** @var IUserManager */ |
|
| 55 | + protected $userManager; |
|
| 56 | + |
|
| 57 | + public function __construct( |
|
| 58 | + IFactory $l10nFactory, |
|
| 59 | + IRootFolder $rootFolder, |
|
| 60 | + ICommentsManager $commentsManager, |
|
| 61 | + IURLGenerator $url, |
|
| 62 | + IUserManager $userManager |
|
| 63 | + ) { |
|
| 64 | + $this->l10nFactory = $l10nFactory; |
|
| 65 | + $this->rootFolder = $rootFolder; |
|
| 66 | + $this->commentsManager = $commentsManager; |
|
| 67 | + $this->url = $url; |
|
| 68 | + $this->userManager = $userManager; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Identifier of the notifier, only use [a-z0-9_] |
|
| 73 | + * |
|
| 74 | + * @return string |
|
| 75 | + * @since 17.0.0 |
|
| 76 | + */ |
|
| 77 | + public function getID(): string { |
|
| 78 | + return 'comments'; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Human readable name describing the notifier |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + * @since 17.0.0 |
|
| 86 | + */ |
|
| 87 | + public function getName(): string { |
|
| 88 | + return $this->l10nFactory->get('comments')->t('Comments'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param INotification $notification |
|
| 93 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 94 | + * @return INotification |
|
| 95 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 96 | + * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted |
|
| 97 | + * @since 9.0.0 |
|
| 98 | + */ |
|
| 99 | + public function prepare(INotification $notification, string $languageCode): INotification { |
|
| 100 | + if ($notification->getApp() !== 'comments') { |
|
| 101 | + throw new \InvalidArgumentException(); |
|
| 102 | + } |
|
| 103 | + try { |
|
| 104 | + $comment = $this->commentsManager->get($notification->getObjectId()); |
|
| 105 | + } catch (NotFoundException $e) { |
|
| 106 | + // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
|
| 107 | + throw new \InvalidArgumentException('Comment not found', 0, $e); |
|
| 108 | + } |
|
| 109 | + $l = $this->l10nFactory->get('comments', $languageCode); |
|
| 110 | + $displayName = $comment->getActorId(); |
|
| 111 | + $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
|
| 112 | + if ($comment->getActorType() === 'users') { |
|
| 113 | + $commenter = $this->userManager->get($comment->getActorId()); |
|
| 114 | + if ($commenter instanceof IUser) { |
|
| 115 | + $displayName = $commenter->getDisplayName(); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + switch ($notification->getSubject()) { |
|
| 120 | + case 'mention': |
|
| 121 | + $parameters = $notification->getSubjectParameters(); |
|
| 122 | + if ($parameters[0] !== 'files') { |
|
| 123 | + throw new \InvalidArgumentException('Unsupported comment object'); |
|
| 124 | + } |
|
| 125 | + $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
|
| 126 | + $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 127 | + if (empty($nodes)) { |
|
| 128 | + throw new AlreadyProcessedException(); |
|
| 129 | + } |
|
| 130 | + $node = $nodes[0]; |
|
| 131 | + |
|
| 132 | + $path = rtrim($node->getPath(), '/'); |
|
| 133 | + if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 134 | + // Remove /user/files/... |
|
| 135 | + $fullPath = $path; |
|
| 136 | + list(,,, $path) = explode('/', $fullPath, 4); |
|
| 137 | + } |
|
| 138 | + $subjectParameters = [ |
|
| 139 | + 'file' => [ |
|
| 140 | + 'type' => 'file', |
|
| 141 | + 'id' => $comment->getObjectId(), |
|
| 142 | + 'name' => $node->getName(), |
|
| 143 | + 'path' => $path, |
|
| 144 | + 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
|
| 145 | + ], |
|
| 146 | + ]; |
|
| 147 | + |
|
| 148 | + if ($isDeletedActor) { |
|
| 149 | + $subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
|
| 150 | + } else { |
|
| 151 | + $subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
|
| 152 | + $subjectParameters['user'] = [ |
|
| 153 | + 'type' => 'user', |
|
| 154 | + 'id' => $comment->getActorId(), |
|
| 155 | + 'name' => $displayName, |
|
| 156 | + ]; |
|
| 157 | + } |
|
| 158 | + list($message, $messageParameters) = $this->commentToRichMessage($comment); |
|
| 159 | + $notification->setRichSubject($subject, $subjectParameters) |
|
| 160 | + ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
|
| 161 | + ->setRichMessage($message, $messageParameters) |
|
| 162 | + ->setParsedMessage($this->richToParsed($message, $messageParameters)) |
|
| 163 | + ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
|
| 164 | + ->setLink($this->url->linkToRouteAbsolute( |
|
| 165 | + 'comments.Notifications.view', |
|
| 166 | + ['id' => $comment->getId()]) |
|
| 167 | + ); |
|
| 168 | + |
|
| 169 | + return $notification; |
|
| 170 | + break; |
|
| 171 | + |
|
| 172 | + default: |
|
| 173 | + throw new \InvalidArgumentException('Invalid subject'); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + public function commentToRichMessage(IComment $comment): array { |
|
| 178 | + $message = $comment->getMessage(); |
|
| 179 | + $messageParameters = []; |
|
| 180 | + $mentionTypeCount = []; |
|
| 181 | + $mentions = $comment->getMentions(); |
|
| 182 | + foreach ($mentions as $mention) { |
|
| 183 | + if ($mention['type'] === 'user') { |
|
| 184 | + $user = $this->userManager->get($mention['id']); |
|
| 185 | + if (!$user instanceof IUser) { |
|
| 186 | + continue; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + if (!array_key_exists($mention['type'], $mentionTypeCount)) { |
|
| 190 | + $mentionTypeCount[$mention['type']] = 0; |
|
| 191 | + } |
|
| 192 | + $mentionTypeCount[$mention['type']]++; |
|
| 193 | + // To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) |
|
| 194 | + // the mention parameter ID does not include the mention ID (which |
|
| 195 | + // could contain characters like '@' for user IDs) but a one-based |
|
| 196 | + // index of the mentions of that type. |
|
| 197 | + $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 198 | + $message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message); |
|
| 199 | + if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
|
| 200 | + $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + try { |
|
| 204 | + $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
| 205 | + } catch (\OutOfBoundsException $e) { |
|
| 206 | + // There is no registered display name resolver for the mention |
|
| 207 | + // type, so the client decides what to display. |
|
| 208 | + $displayName = ''; |
|
| 209 | + } |
|
| 210 | + $messageParameters[$mentionParameterId] = [ |
|
| 211 | + 'type' => $mention['type'], |
|
| 212 | + 'id' => $mention['id'], |
|
| 213 | + 'name' => $displayName |
|
| 214 | + ]; |
|
| 215 | + } |
|
| 216 | + return [$message, $messageParameters]; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public function richToParsed(string $message, array $parameters): string { |
|
| 220 | + $placeholders = $replacements = []; |
|
| 221 | + foreach ($parameters as $placeholder => $parameter) { |
|
| 222 | + $placeholders[] = '{' . $placeholder . '}'; |
|
| 223 | + if ($parameter['type'] === 'user') { |
|
| 224 | + $replacements[] = '@' . $parameter['name']; |
|
| 225 | + } elseif ($parameter['type'] === 'file') { |
|
| 226 | + $replacements[] = $parameter['path']; |
|
| 227 | + } else { |
|
| 228 | + $replacements[] = $parameter['name']; |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + return str_replace($placeholders, $replacements, $message); |
|
| 232 | + } |
|
| 233 | 233 | } |
@@ -123,14 +123,14 @@ discard block |
||
| 123 | 123 | throw new \InvalidArgumentException('Unsupported comment object'); |
| 124 | 124 | } |
| 125 | 125 | $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
| 126 | - $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 126 | + $nodes = $userFolder->getById((int) $parameters[1]); |
|
| 127 | 127 | if (empty($nodes)) { |
| 128 | 128 | throw new AlreadyProcessedException(); |
| 129 | 129 | } |
| 130 | 130 | $node = $nodes[0]; |
| 131 | 131 | |
| 132 | 132 | $path = rtrim($node->getPath(), '/'); |
| 133 | - if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 133 | + if (strpos($path, '/'.$notification->getUser().'/files/') === 0) { |
|
| 134 | 134 | // Remove /user/files/... |
| 135 | 135 | $fullPath = $path; |
| 136 | 136 | list(,,, $path) = explode('/', $fullPath, 4); |
@@ -194,10 +194,10 @@ discard block |
||
| 194 | 194 | // the mention parameter ID does not include the mention ID (which |
| 195 | 195 | // could contain characters like '@' for user IDs) but a one-based |
| 196 | 196 | // index of the mentions of that type. |
| 197 | - $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 198 | - $message = str_replace('@"' . $mention['id'] . '"', '{' . $mentionParameterId . '}', $message); |
|
| 197 | + $mentionParameterId = 'mention-'.$mention['type'].$mentionTypeCount[$mention['type']]; |
|
| 198 | + $message = str_replace('@"'.$mention['id'].'"', '{'.$mentionParameterId.'}', $message); |
|
| 199 | 199 | if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
| 200 | - $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 200 | + $message = str_replace('@'.$mention['id'], '{'.$mentionParameterId.'}', $message); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | try { |
@@ -219,9 +219,9 @@ discard block |
||
| 219 | 219 | public function richToParsed(string $message, array $parameters): string { |
| 220 | 220 | $placeholders = $replacements = []; |
| 221 | 221 | foreach ($parameters as $placeholder => $parameter) { |
| 222 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 222 | + $placeholders[] = '{'.$placeholder.'}'; |
|
| 223 | 223 | if ($parameter['type'] === 'user') { |
| 224 | - $replacements[] = '@' . $parameter['name']; |
|
| 224 | + $replacements[] = '@'.$parameter['name']; |
|
| 225 | 225 | } elseif ($parameter['type'] === 'file') { |
| 226 | 226 | $replacements[] = $parameter['path']; |
| 227 | 227 | } else { |
@@ -37,238 +37,238 @@ |
||
| 37 | 37 | |
| 38 | 38 | class Provider implements IProvider { |
| 39 | 39 | |
| 40 | - /** @var IFactory */ |
|
| 41 | - protected $languageFactory; |
|
| 42 | - |
|
| 43 | - /** @var IL10N */ |
|
| 44 | - protected $l; |
|
| 45 | - |
|
| 46 | - /** @var IURLGenerator */ |
|
| 47 | - protected $url; |
|
| 48 | - |
|
| 49 | - /** @var ICommentsManager */ |
|
| 50 | - protected $commentsManager; |
|
| 51 | - |
|
| 52 | - /** @var IUserManager */ |
|
| 53 | - protected $userManager; |
|
| 54 | - |
|
| 55 | - /** @var IManager */ |
|
| 56 | - protected $activityManager; |
|
| 57 | - |
|
| 58 | - /** @var string[] */ |
|
| 59 | - protected $displayNames = []; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param IFactory $languageFactory |
|
| 63 | - * @param IURLGenerator $url |
|
| 64 | - * @param ICommentsManager $commentsManager |
|
| 65 | - * @param IUserManager $userManager |
|
| 66 | - * @param IManager $activityManager |
|
| 67 | - */ |
|
| 68 | - public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { |
|
| 69 | - $this->languageFactory = $languageFactory; |
|
| 70 | - $this->url = $url; |
|
| 71 | - $this->commentsManager = $commentsManager; |
|
| 72 | - $this->userManager = $userManager; |
|
| 73 | - $this->activityManager = $activityManager; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string $language |
|
| 78 | - * @param IEvent $event |
|
| 79 | - * @param IEvent|null $previousEvent |
|
| 80 | - * @return IEvent |
|
| 81 | - * @throws \InvalidArgumentException |
|
| 82 | - * @since 11.0.0 |
|
| 83 | - */ |
|
| 84 | - public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
| 85 | - if ($event->getApp() !== 'comments') { |
|
| 86 | - throw new \InvalidArgumentException(); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $this->l = $this->languageFactory->get('comments', $language); |
|
| 90 | - |
|
| 91 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 92 | - $this->parseMessage($event); |
|
| 93 | - if ($this->activityManager->getRequirePNG()) { |
|
| 94 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png'))); |
|
| 95 | - } else { |
|
| 96 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if ($this->activityManager->isFormattingFilteredObject()) { |
|
| 100 | - try { |
|
| 101 | - return $this->parseShortVersion($event); |
|
| 102 | - } catch (\InvalidArgumentException $e) { |
|
| 103 | - // Ignore and simply use the long version... |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return $this->parseLongVersion($event); |
|
| 108 | - } else { |
|
| 109 | - throw new \InvalidArgumentException(); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param IEvent $event |
|
| 115 | - * @return IEvent |
|
| 116 | - * @throws \InvalidArgumentException |
|
| 117 | - */ |
|
| 118 | - protected function parseShortVersion(IEvent $event) { |
|
| 119 | - $subjectParameters = $this->getSubjectParameters($event); |
|
| 120 | - |
|
| 121 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 122 | - if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 123 | - $event->setParsedSubject($this->l->t('You commented')) |
|
| 124 | - ->setRichSubject($this->l->t('You commented'), []); |
|
| 125 | - } else { |
|
| 126 | - $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 127 | - $event->setParsedSubject($this->l->t('%1$s commented', [$author['name']])) |
|
| 128 | - ->setRichSubject($this->l->t('{author} commented'), [ |
|
| 129 | - 'author' => $author, |
|
| 130 | - ]); |
|
| 131 | - } |
|
| 132 | - } else { |
|
| 133 | - throw new \InvalidArgumentException(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $event; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param IEvent $event |
|
| 141 | - * @return IEvent |
|
| 142 | - * @throws \InvalidArgumentException |
|
| 143 | - */ |
|
| 144 | - protected function parseLongVersion(IEvent $event) { |
|
| 145 | - $subjectParameters = $this->getSubjectParameters($event); |
|
| 146 | - |
|
| 147 | - if ($event->getSubject() === 'add_comment_subject') { |
|
| 148 | - if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 149 | - $event->setParsedSubject($this->l->t('You commented on %1$s', [ |
|
| 150 | - $subjectParameters['filePath'], |
|
| 151 | - ])) |
|
| 152 | - ->setRichSubject($this->l->t('You commented on {file}'), [ |
|
| 153 | - 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 154 | - ]); |
|
| 155 | - } else { |
|
| 156 | - $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 157 | - $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [ |
|
| 158 | - $author['name'], |
|
| 159 | - $subjectParameters['filePath'], |
|
| 160 | - ])) |
|
| 161 | - ->setRichSubject($this->l->t('{author} commented on {file}'), [ |
|
| 162 | - 'author' => $author, |
|
| 163 | - 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 164 | - ]); |
|
| 165 | - } |
|
| 166 | - } else { |
|
| 167 | - throw new \InvalidArgumentException(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - return $event; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - protected function getSubjectParameters(IEvent $event) { |
|
| 174 | - $subjectParameters = $event->getSubjectParameters(); |
|
| 175 | - if (isset($subjectParameters['fileId'])) { |
|
| 176 | - return $subjectParameters; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Fix subjects from 12.0.3 and older |
|
| 180 | - // |
|
| 181 | - // Do NOT Remove unless necessary |
|
| 182 | - // Removing this will break parsing of activities that were created on |
|
| 183 | - // Nextcloud 12, so we should keep this as long as it's acceptable. |
|
| 184 | - // Otherwise if people upgrade over multiple releases in a short period, |
|
| 185 | - // they will get the dead entries in their stream. |
|
| 186 | - return [ |
|
| 187 | - 'actor' => $subjectParameters[0], |
|
| 188 | - 'fileId' => $event->getObjectId(), |
|
| 189 | - 'filePath' => trim($subjectParameters[1], '/'), |
|
| 190 | - ]; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param IEvent $event |
|
| 195 | - */ |
|
| 196 | - protected function parseMessage(IEvent $event) { |
|
| 197 | - $messageParameters = $event->getMessageParameters(); |
|
| 198 | - if (empty($messageParameters)) { |
|
| 199 | ||
| 200 | - return; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - $commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0]; |
|
| 204 | - |
|
| 205 | - try { |
|
| 206 | - $comment = $this->commentsManager->get((string) $commentId); |
|
| 207 | - $message = $comment->getMessage(); |
|
| 208 | - |
|
| 209 | - $mentionCount = 1; |
|
| 210 | - $mentions = []; |
|
| 211 | - foreach ($comment->getMentions() as $mention) { |
|
| 212 | - if ($mention['type'] !== 'user') { |
|
| 213 | - continue; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message); |
|
| 217 | - if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
|
| 218 | - $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 222 | - $mentionCount++; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $event->setParsedMessage($comment->getMessage()) |
|
| 226 | - ->setRichMessage($message, $mentions); |
|
| 227 | - } catch (NotFoundException $e) { |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * @param int $id |
|
| 233 | - * @param string $path |
|
| 234 | - * @return array |
|
| 235 | - */ |
|
| 236 | - protected function generateFileParameter($id, $path) { |
|
| 237 | - return [ |
|
| 238 | - 'type' => 'file', |
|
| 239 | - 'id' => $id, |
|
| 240 | - 'name' => basename($path), |
|
| 241 | - 'path' => $path, |
|
| 242 | - 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
| 243 | - ]; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param string $uid |
|
| 248 | - * @return array |
|
| 249 | - */ |
|
| 250 | - protected function generateUserParameter($uid) { |
|
| 251 | - if (!isset($this->displayNames[$uid])) { |
|
| 252 | - $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return [ |
|
| 256 | - 'type' => 'user', |
|
| 257 | - 'id' => $uid, |
|
| 258 | - 'name' => $this->displayNames[$uid], |
|
| 259 | - ]; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * @param string $uid |
|
| 264 | - * @return string |
|
| 265 | - */ |
|
| 266 | - protected function getDisplayName($uid) { |
|
| 267 | - $user = $this->userManager->get($uid); |
|
| 268 | - if ($user instanceof IUser) { |
|
| 269 | - return $user->getDisplayName(); |
|
| 270 | - } else { |
|
| 271 | - return $uid; |
|
| 272 | - } |
|
| 273 | - } |
|
| 40 | + /** @var IFactory */ |
|
| 41 | + protected $languageFactory; |
|
| 42 | + |
|
| 43 | + /** @var IL10N */ |
|
| 44 | + protected $l; |
|
| 45 | + |
|
| 46 | + /** @var IURLGenerator */ |
|
| 47 | + protected $url; |
|
| 48 | + |
|
| 49 | + /** @var ICommentsManager */ |
|
| 50 | + protected $commentsManager; |
|
| 51 | + |
|
| 52 | + /** @var IUserManager */ |
|
| 53 | + protected $userManager; |
|
| 54 | + |
|
| 55 | + /** @var IManager */ |
|
| 56 | + protected $activityManager; |
|
| 57 | + |
|
| 58 | + /** @var string[] */ |
|
| 59 | + protected $displayNames = []; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param IFactory $languageFactory |
|
| 63 | + * @param IURLGenerator $url |
|
| 64 | + * @param ICommentsManager $commentsManager |
|
| 65 | + * @param IUserManager $userManager |
|
| 66 | + * @param IManager $activityManager |
|
| 67 | + */ |
|
| 68 | + public function __construct(IFactory $languageFactory, IURLGenerator $url, ICommentsManager $commentsManager, IUserManager $userManager, IManager $activityManager) { |
|
| 69 | + $this->languageFactory = $languageFactory; |
|
| 70 | + $this->url = $url; |
|
| 71 | + $this->commentsManager = $commentsManager; |
|
| 72 | + $this->userManager = $userManager; |
|
| 73 | + $this->activityManager = $activityManager; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string $language |
|
| 78 | + * @param IEvent $event |
|
| 79 | + * @param IEvent|null $previousEvent |
|
| 80 | + * @return IEvent |
|
| 81 | + * @throws \InvalidArgumentException |
|
| 82 | + * @since 11.0.0 |
|
| 83 | + */ |
|
| 84 | + public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
| 85 | + if ($event->getApp() !== 'comments') { |
|
| 86 | + throw new \InvalidArgumentException(); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $this->l = $this->languageFactory->get('comments', $language); |
|
| 90 | + |
|
| 91 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 92 | + $this->parseMessage($event); |
|
| 93 | + if ($this->activityManager->getRequirePNG()) { |
|
| 94 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.png'))); |
|
| 95 | + } else { |
|
| 96 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if ($this->activityManager->isFormattingFilteredObject()) { |
|
| 100 | + try { |
|
| 101 | + return $this->parseShortVersion($event); |
|
| 102 | + } catch (\InvalidArgumentException $e) { |
|
| 103 | + // Ignore and simply use the long version... |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return $this->parseLongVersion($event); |
|
| 108 | + } else { |
|
| 109 | + throw new \InvalidArgumentException(); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param IEvent $event |
|
| 115 | + * @return IEvent |
|
| 116 | + * @throws \InvalidArgumentException |
|
| 117 | + */ |
|
| 118 | + protected function parseShortVersion(IEvent $event) { |
|
| 119 | + $subjectParameters = $this->getSubjectParameters($event); |
|
| 120 | + |
|
| 121 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 122 | + if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 123 | + $event->setParsedSubject($this->l->t('You commented')) |
|
| 124 | + ->setRichSubject($this->l->t('You commented'), []); |
|
| 125 | + } else { |
|
| 126 | + $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 127 | + $event->setParsedSubject($this->l->t('%1$s commented', [$author['name']])) |
|
| 128 | + ->setRichSubject($this->l->t('{author} commented'), [ |
|
| 129 | + 'author' => $author, |
|
| 130 | + ]); |
|
| 131 | + } |
|
| 132 | + } else { |
|
| 133 | + throw new \InvalidArgumentException(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $event; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param IEvent $event |
|
| 141 | + * @return IEvent |
|
| 142 | + * @throws \InvalidArgumentException |
|
| 143 | + */ |
|
| 144 | + protected function parseLongVersion(IEvent $event) { |
|
| 145 | + $subjectParameters = $this->getSubjectParameters($event); |
|
| 146 | + |
|
| 147 | + if ($event->getSubject() === 'add_comment_subject') { |
|
| 148 | + if ($subjectParameters['actor'] === $this->activityManager->getCurrentUserId()) { |
|
| 149 | + $event->setParsedSubject($this->l->t('You commented on %1$s', [ |
|
| 150 | + $subjectParameters['filePath'], |
|
| 151 | + ])) |
|
| 152 | + ->setRichSubject($this->l->t('You commented on {file}'), [ |
|
| 153 | + 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 154 | + ]); |
|
| 155 | + } else { |
|
| 156 | + $author = $this->generateUserParameter($subjectParameters['actor']); |
|
| 157 | + $event->setParsedSubject($this->l->t('%1$s commented on %2$s', [ |
|
| 158 | + $author['name'], |
|
| 159 | + $subjectParameters['filePath'], |
|
| 160 | + ])) |
|
| 161 | + ->setRichSubject($this->l->t('{author} commented on {file}'), [ |
|
| 162 | + 'author' => $author, |
|
| 163 | + 'file' => $this->generateFileParameter($subjectParameters['fileId'], $subjectParameters['filePath']), |
|
| 164 | + ]); |
|
| 165 | + } |
|
| 166 | + } else { |
|
| 167 | + throw new \InvalidArgumentException(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + return $event; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + protected function getSubjectParameters(IEvent $event) { |
|
| 174 | + $subjectParameters = $event->getSubjectParameters(); |
|
| 175 | + if (isset($subjectParameters['fileId'])) { |
|
| 176 | + return $subjectParameters; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Fix subjects from 12.0.3 and older |
|
| 180 | + // |
|
| 181 | + // Do NOT Remove unless necessary |
|
| 182 | + // Removing this will break parsing of activities that were created on |
|
| 183 | + // Nextcloud 12, so we should keep this as long as it's acceptable. |
|
| 184 | + // Otherwise if people upgrade over multiple releases in a short period, |
|
| 185 | + // they will get the dead entries in their stream. |
|
| 186 | + return [ |
|
| 187 | + 'actor' => $subjectParameters[0], |
|
| 188 | + 'fileId' => $event->getObjectId(), |
|
| 189 | + 'filePath' => trim($subjectParameters[1], '/'), |
|
| 190 | + ]; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param IEvent $event |
|
| 195 | + */ |
|
| 196 | + protected function parseMessage(IEvent $event) { |
|
| 197 | + $messageParameters = $event->getMessageParameters(); |
|
| 198 | + if (empty($messageParameters)) { |
|
| 199 | ||
| 200 | + return; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + $commentId = isset($messageParameters['commentId']) ? $messageParameters['commentId'] : $messageParameters[0]; |
|
| 204 | + |
|
| 205 | + try { |
|
| 206 | + $comment = $this->commentsManager->get((string) $commentId); |
|
| 207 | + $message = $comment->getMessage(); |
|
| 208 | + |
|
| 209 | + $mentionCount = 1; |
|
| 210 | + $mentions = []; |
|
| 211 | + foreach ($comment->getMentions() as $mention) { |
|
| 212 | + if ($mention['type'] !== 'user') { |
|
| 213 | + continue; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message); |
|
| 217 | + if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
|
| 218 | + $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 222 | + $mentionCount++; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $event->setParsedMessage($comment->getMessage()) |
|
| 226 | + ->setRichMessage($message, $mentions); |
|
| 227 | + } catch (NotFoundException $e) { |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * @param int $id |
|
| 233 | + * @param string $path |
|
| 234 | + * @return array |
|
| 235 | + */ |
|
| 236 | + protected function generateFileParameter($id, $path) { |
|
| 237 | + return [ |
|
| 238 | + 'type' => 'file', |
|
| 239 | + 'id' => $id, |
|
| 240 | + 'name' => basename($path), |
|
| 241 | + 'path' => $path, |
|
| 242 | + 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
| 243 | + ]; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param string $uid |
|
| 248 | + * @return array |
|
| 249 | + */ |
|
| 250 | + protected function generateUserParameter($uid) { |
|
| 251 | + if (!isset($this->displayNames[$uid])) { |
|
| 252 | + $this->displayNames[$uid] = $this->getDisplayName($uid); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return [ |
|
| 256 | + 'type' => 'user', |
|
| 257 | + 'id' => $uid, |
|
| 258 | + 'name' => $this->displayNames[$uid], |
|
| 259 | + ]; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * @param string $uid |
|
| 264 | + * @return string |
|
| 265 | + */ |
|
| 266 | + protected function getDisplayName($uid) { |
|
| 267 | + $user = $this->userManager->get($uid); |
|
| 268 | + if ($user instanceof IUser) { |
|
| 269 | + return $user->getDisplayName(); |
|
| 270 | + } else { |
|
| 271 | + return $uid; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | 274 | } |
@@ -213,12 +213,12 @@ |
||
| 213 | 213 | continue; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - $message = str_replace('@"' . $mention['id'] . '"', '{mention' . $mentionCount . '}', $message); |
|
| 216 | + $message = str_replace('@"'.$mention['id'].'"', '{mention'.$mentionCount.'}', $message); |
|
| 217 | 217 | if (strpos($mention['id'], ' ') === false && strpos($mention['id'], 'guest/') !== 0) { |
| 218 | - $message = str_replace('@' . $mention['id'], '{mention' . $mentionCount . '}', $message); |
|
| 218 | + $message = str_replace('@'.$mention['id'], '{mention'.$mentionCount.'}', $message); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - $mentions['mention' . $mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 221 | + $mentions['mention'.$mentionCount] = $this->generateUserParameter($mention['id']); |
|
| 222 | 222 | $mentionCount++; |
| 223 | 223 | } |
| 224 | 224 | |
@@ -31,429 +31,429 @@ |
||
| 31 | 31 | use OCP\Comments\MessageTooLongException; |
| 32 | 32 | |
| 33 | 33 | class Comment implements IComment { |
| 34 | - protected $data = [ |
|
| 35 | - 'id' => '', |
|
| 36 | - 'parentId' => '0', |
|
| 37 | - 'topmostParentId' => '0', |
|
| 38 | - 'childrenCount' => '0', |
|
| 39 | - 'message' => '', |
|
| 40 | - 'verb' => '', |
|
| 41 | - 'actorType' => '', |
|
| 42 | - 'actorId' => '', |
|
| 43 | - 'objectType' => '', |
|
| 44 | - 'objectId' => '', |
|
| 45 | - 'referenceId' => null, |
|
| 46 | - 'creationDT' => null, |
|
| 47 | - 'latestChildDT' => null, |
|
| 48 | - ]; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Comment constructor. |
|
| 52 | - * |
|
| 53 | - * @param array $data optional, array with keys according to column names from |
|
| 54 | - * the comments database scheme |
|
| 55 | - */ |
|
| 56 | - public function __construct(array $data = null) { |
|
| 57 | - if (is_array($data)) { |
|
| 58 | - $this->fromArray($data); |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * returns the ID of the comment |
|
| 64 | - * |
|
| 65 | - * It may return an empty string, if the comment was not stored. |
|
| 66 | - * It is expected that the concrete Comment implementation gives an ID |
|
| 67 | - * by itself (e.g. after saving). |
|
| 68 | - * |
|
| 69 | - * @return string |
|
| 70 | - * @since 9.0.0 |
|
| 71 | - */ |
|
| 72 | - public function getId() { |
|
| 73 | - return $this->data['id']; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * sets the ID of the comment and returns itself |
|
| 78 | - * |
|
| 79 | - * It is only allowed to set the ID only, if the current id is an empty |
|
| 80 | - * string (which means it is not stored in a database, storage or whatever |
|
| 81 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 82 | - * not permitted and must result in an IllegalIDChangeException. |
|
| 83 | - * |
|
| 84 | - * @param string $id |
|
| 85 | - * @return IComment |
|
| 86 | - * @throws IllegalIDChangeException |
|
| 87 | - * @since 9.0.0 |
|
| 88 | - */ |
|
| 89 | - public function setId($id) { |
|
| 90 | - if (!is_string($id)) { |
|
| 91 | - throw new \InvalidArgumentException('String expected.'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $id = trim($id); |
|
| 95 | - if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 96 | - $this->data['id'] = $id; |
|
| 97 | - return $this; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * returns the parent ID of the comment |
|
| 105 | - * |
|
| 106 | - * @return string |
|
| 107 | - * @since 9.0.0 |
|
| 108 | - */ |
|
| 109 | - public function getParentId() { |
|
| 110 | - return $this->data['parentId']; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * sets the parent ID and returns itself |
|
| 115 | - * |
|
| 116 | - * @param string $parentId |
|
| 117 | - * @return IComment |
|
| 118 | - * @since 9.0.0 |
|
| 119 | - */ |
|
| 120 | - public function setParentId($parentId) { |
|
| 121 | - if (!is_string($parentId)) { |
|
| 122 | - throw new \InvalidArgumentException('String expected.'); |
|
| 123 | - } |
|
| 124 | - $this->data['parentId'] = trim($parentId); |
|
| 125 | - return $this; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * returns the topmost parent ID of the comment |
|
| 130 | - * |
|
| 131 | - * @return string |
|
| 132 | - * @since 9.0.0 |
|
| 133 | - */ |
|
| 134 | - public function getTopmostParentId() { |
|
| 135 | - return $this->data['topmostParentId']; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * sets the topmost parent ID and returns itself |
|
| 141 | - * |
|
| 142 | - * @param string $id |
|
| 143 | - * @return IComment |
|
| 144 | - * @since 9.0.0 |
|
| 145 | - */ |
|
| 146 | - public function setTopmostParentId($id) { |
|
| 147 | - if (!is_string($id)) { |
|
| 148 | - throw new \InvalidArgumentException('String expected.'); |
|
| 149 | - } |
|
| 150 | - $this->data['topmostParentId'] = trim($id); |
|
| 151 | - return $this; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * returns the number of children |
|
| 156 | - * |
|
| 157 | - * @return int |
|
| 158 | - * @since 9.0.0 |
|
| 159 | - */ |
|
| 160 | - public function getChildrenCount() { |
|
| 161 | - return $this->data['childrenCount']; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * sets the number of children |
|
| 166 | - * |
|
| 167 | - * @param int $count |
|
| 168 | - * @return IComment |
|
| 169 | - * @since 9.0.0 |
|
| 170 | - */ |
|
| 171 | - public function setChildrenCount($count) { |
|
| 172 | - if (!is_int($count)) { |
|
| 173 | - throw new \InvalidArgumentException('Integer expected.'); |
|
| 174 | - } |
|
| 175 | - $this->data['childrenCount'] = $count; |
|
| 176 | - return $this; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * returns the message of the comment |
|
| 181 | - * |
|
| 182 | - * @return string |
|
| 183 | - * @since 9.0.0 |
|
| 184 | - */ |
|
| 185 | - public function getMessage() { |
|
| 186 | - return $this->data['message']; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * sets the message of the comment and returns itself |
|
| 191 | - * |
|
| 192 | - * @param string $message |
|
| 193 | - * @param int $maxLength |
|
| 194 | - * @return IComment |
|
| 195 | - * @throws MessageTooLongException |
|
| 196 | - * @since 9.0.0 |
|
| 197 | - */ |
|
| 198 | - public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 199 | - if (!is_string($message)) { |
|
| 200 | - throw new \InvalidArgumentException('String expected.'); |
|
| 201 | - } |
|
| 202 | - $message = trim($message); |
|
| 203 | - if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 204 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 205 | - } |
|
| 206 | - $this->data['message'] = $message; |
|
| 207 | - return $this; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * returns an array containing mentions that are included in the comment |
|
| 212 | - * |
|
| 213 | - * @return array each mention provides a 'type' and an 'id', see example below |
|
| 214 | - * @since 11.0.0 |
|
| 215 | - * |
|
| 216 | - * The return array looks like: |
|
| 217 | - * [ |
|
| 218 | - * [ |
|
| 219 | - * 'type' => 'user', |
|
| 220 | - * 'id' => 'citizen4' |
|
| 221 | - * ], |
|
| 222 | - * [ |
|
| 223 | - * 'type' => 'group', |
|
| 224 | - * 'id' => 'media' |
|
| 225 | - * ], |
|
| 226 | - * … |
|
| 227 | - * ] |
|
| 228 | - * |
|
| 229 | - */ |
|
| 230 | - public function getMentions() { |
|
| 231 | - $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 232 | - if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 233 | - return []; |
|
| 234 | - } |
|
| 235 | - $uids = array_unique($mentions[0]); |
|
| 236 | - usort($uids, static function ($uid1, $uid2) { |
|
| 237 | - return mb_strlen($uid2) <=> mb_strlen($uid1); |
|
| 238 | - }); |
|
| 239 | - $result = []; |
|
| 240 | - foreach ($uids as $uid) { |
|
| 241 | - $cleanUid = trim(substr($uid, 1), '"'); |
|
| 242 | - if (strpos($cleanUid, 'guest/') === 0) { |
|
| 243 | - $result[] = ['type' => 'guest', 'id' => $cleanUid]; |
|
| 244 | - } else { |
|
| 245 | - $result[] = ['type' => 'user', 'id' => $cleanUid]; |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - return $result; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * returns the verb of the comment |
|
| 253 | - * |
|
| 254 | - * @return string |
|
| 255 | - * @since 9.0.0 |
|
| 256 | - */ |
|
| 257 | - public function getVerb() { |
|
| 258 | - return $this->data['verb']; |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 263 | - * |
|
| 264 | - * @param string $verb |
|
| 265 | - * @return IComment |
|
| 266 | - * @since 9.0.0 |
|
| 267 | - */ |
|
| 268 | - public function setVerb($verb) { |
|
| 269 | - if (!is_string($verb) || !trim($verb)) { |
|
| 270 | - throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 271 | - } |
|
| 272 | - $this->data['verb'] = trim($verb); |
|
| 273 | - return $this; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * returns the actor type |
|
| 278 | - * |
|
| 279 | - * @return string |
|
| 280 | - * @since 9.0.0 |
|
| 281 | - */ |
|
| 282 | - public function getActorType() { |
|
| 283 | - return $this->data['actorType']; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * returns the actor ID |
|
| 288 | - * |
|
| 289 | - * @return string |
|
| 290 | - * @since 9.0.0 |
|
| 291 | - */ |
|
| 292 | - public function getActorId() { |
|
| 293 | - return $this->data['actorId']; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * sets (overwrites) the actor type and id |
|
| 298 | - * |
|
| 299 | - * @param string $actorType e.g. 'users' |
|
| 300 | - * @param string $actorId e.g. 'zombie234' |
|
| 301 | - * @return IComment |
|
| 302 | - * @since 9.0.0 |
|
| 303 | - */ |
|
| 304 | - public function setActor($actorType, $actorId) { |
|
| 305 | - if ( |
|
| 306 | - !is_string($actorType) || !trim($actorType) |
|
| 307 | - || !is_string($actorId) || $actorId === '' |
|
| 308 | - ) { |
|
| 309 | - throw new \InvalidArgumentException('String expected.'); |
|
| 310 | - } |
|
| 311 | - $this->data['actorType'] = trim($actorType); |
|
| 312 | - $this->data['actorId'] = $actorId; |
|
| 313 | - return $this; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * returns the creation date of the comment. |
|
| 318 | - * |
|
| 319 | - * If not explicitly set, it shall default to the time of initialization. |
|
| 320 | - * |
|
| 321 | - * @return \DateTime |
|
| 322 | - * @since 9.0.0 |
|
| 323 | - */ |
|
| 324 | - public function getCreationDateTime() { |
|
| 325 | - return $this->data['creationDT']; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * sets the creation date of the comment and returns itself |
|
| 330 | - * |
|
| 331 | - * @param \DateTime $timestamp |
|
| 332 | - * @return IComment |
|
| 333 | - * @since 9.0.0 |
|
| 334 | - */ |
|
| 335 | - public function setCreationDateTime(\DateTime $timestamp) { |
|
| 336 | - $this->data['creationDT'] = $timestamp; |
|
| 337 | - return $this; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * returns the DateTime of the most recent child, if set, otherwise null |
|
| 342 | - * |
|
| 343 | - * @return \DateTime|null |
|
| 344 | - * @since 9.0.0 |
|
| 345 | - */ |
|
| 346 | - public function getLatestChildDateTime() { |
|
| 347 | - return $this->data['latestChildDT']; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * sets the date of the most recent child |
|
| 352 | - * |
|
| 353 | - * @param \DateTime $dateTime |
|
| 354 | - * @return IComment |
|
| 355 | - * @since 9.0.0 |
|
| 356 | - */ |
|
| 357 | - public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 358 | - $this->data['latestChildDT'] = $dateTime; |
|
| 359 | - return $this; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * returns the object type the comment is attached to |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - * @since 9.0.0 |
|
| 367 | - */ |
|
| 368 | - public function getObjectType() { |
|
| 369 | - return $this->data['objectType']; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * returns the object id the comment is attached to |
|
| 374 | - * |
|
| 375 | - * @return string |
|
| 376 | - * @since 9.0.0 |
|
| 377 | - */ |
|
| 378 | - public function getObjectId() { |
|
| 379 | - return $this->data['objectId']; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * sets (overwrites) the object of the comment |
|
| 384 | - * |
|
| 385 | - * @param string $objectType e.g. 'files' |
|
| 386 | - * @param string $objectId e.g. '16435' |
|
| 387 | - * @return IComment |
|
| 388 | - * @since 9.0.0 |
|
| 389 | - */ |
|
| 390 | - public function setObject($objectType, $objectId) { |
|
| 391 | - if ( |
|
| 392 | - !is_string($objectType) || !trim($objectType) |
|
| 393 | - || !is_string($objectId) || trim($objectId) === '' |
|
| 394 | - ) { |
|
| 395 | - throw new \InvalidArgumentException('String expected.'); |
|
| 396 | - } |
|
| 397 | - $this->data['objectType'] = trim($objectType); |
|
| 398 | - $this->data['objectId'] = trim($objectId); |
|
| 399 | - return $this; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * returns the reference id of the comment |
|
| 404 | - * |
|
| 405 | - * @return string|null |
|
| 406 | - * @since 19.0.0 |
|
| 407 | - */ |
|
| 408 | - public function getReferenceId(): ?string { |
|
| 409 | - return $this->data['referenceId']; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * sets (overwrites) the reference id of the comment |
|
| 414 | - * |
|
| 415 | - * @param string $referenceId e.g. sha256 hash sum |
|
| 416 | - * @return IComment |
|
| 417 | - * @since 19.0.0 |
|
| 418 | - */ |
|
| 419 | - public function setReferenceId(?string $referenceId): IComment { |
|
| 420 | - if ($referenceId === null) { |
|
| 421 | - $this->data['referenceId'] = $referenceId; |
|
| 422 | - } else { |
|
| 423 | - $referenceId = trim($referenceId); |
|
| 424 | - if ($referenceId === '') { |
|
| 425 | - throw new \InvalidArgumentException('Non empty string expected.'); |
|
| 426 | - } |
|
| 427 | - $this->data['referenceId'] = $referenceId; |
|
| 428 | - } |
|
| 429 | - return $this; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * sets the comment data based on an array with keys as taken from the |
|
| 434 | - * database. |
|
| 435 | - * |
|
| 436 | - * @param array $data |
|
| 437 | - * @return IComment |
|
| 438 | - */ |
|
| 439 | - protected function fromArray($data) { |
|
| 440 | - foreach (array_keys($data) as $key) { |
|
| 441 | - // translate DB keys to internal setter names |
|
| 442 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 443 | - $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 444 | - |
|
| 445 | - if (method_exists($this, $setter)) { |
|
| 446 | - $this->$setter($data[$key]); |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - foreach (['actor', 'object'] as $role) { |
|
| 451 | - if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 452 | - $setter = 'set' . ucfirst($role); |
|
| 453 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return $this; |
|
| 458 | - } |
|
| 34 | + protected $data = [ |
|
| 35 | + 'id' => '', |
|
| 36 | + 'parentId' => '0', |
|
| 37 | + 'topmostParentId' => '0', |
|
| 38 | + 'childrenCount' => '0', |
|
| 39 | + 'message' => '', |
|
| 40 | + 'verb' => '', |
|
| 41 | + 'actorType' => '', |
|
| 42 | + 'actorId' => '', |
|
| 43 | + 'objectType' => '', |
|
| 44 | + 'objectId' => '', |
|
| 45 | + 'referenceId' => null, |
|
| 46 | + 'creationDT' => null, |
|
| 47 | + 'latestChildDT' => null, |
|
| 48 | + ]; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Comment constructor. |
|
| 52 | + * |
|
| 53 | + * @param array $data optional, array with keys according to column names from |
|
| 54 | + * the comments database scheme |
|
| 55 | + */ |
|
| 56 | + public function __construct(array $data = null) { |
|
| 57 | + if (is_array($data)) { |
|
| 58 | + $this->fromArray($data); |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * returns the ID of the comment |
|
| 64 | + * |
|
| 65 | + * It may return an empty string, if the comment was not stored. |
|
| 66 | + * It is expected that the concrete Comment implementation gives an ID |
|
| 67 | + * by itself (e.g. after saving). |
|
| 68 | + * |
|
| 69 | + * @return string |
|
| 70 | + * @since 9.0.0 |
|
| 71 | + */ |
|
| 72 | + public function getId() { |
|
| 73 | + return $this->data['id']; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * sets the ID of the comment and returns itself |
|
| 78 | + * |
|
| 79 | + * It is only allowed to set the ID only, if the current id is an empty |
|
| 80 | + * string (which means it is not stored in a database, storage or whatever |
|
| 81 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 82 | + * not permitted and must result in an IllegalIDChangeException. |
|
| 83 | + * |
|
| 84 | + * @param string $id |
|
| 85 | + * @return IComment |
|
| 86 | + * @throws IllegalIDChangeException |
|
| 87 | + * @since 9.0.0 |
|
| 88 | + */ |
|
| 89 | + public function setId($id) { |
|
| 90 | + if (!is_string($id)) { |
|
| 91 | + throw new \InvalidArgumentException('String expected.'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $id = trim($id); |
|
| 95 | + if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 96 | + $this->data['id'] = $id; |
|
| 97 | + return $this; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * returns the parent ID of the comment |
|
| 105 | + * |
|
| 106 | + * @return string |
|
| 107 | + * @since 9.0.0 |
|
| 108 | + */ |
|
| 109 | + public function getParentId() { |
|
| 110 | + return $this->data['parentId']; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * sets the parent ID and returns itself |
|
| 115 | + * |
|
| 116 | + * @param string $parentId |
|
| 117 | + * @return IComment |
|
| 118 | + * @since 9.0.0 |
|
| 119 | + */ |
|
| 120 | + public function setParentId($parentId) { |
|
| 121 | + if (!is_string($parentId)) { |
|
| 122 | + throw new \InvalidArgumentException('String expected.'); |
|
| 123 | + } |
|
| 124 | + $this->data['parentId'] = trim($parentId); |
|
| 125 | + return $this; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * returns the topmost parent ID of the comment |
|
| 130 | + * |
|
| 131 | + * @return string |
|
| 132 | + * @since 9.0.0 |
|
| 133 | + */ |
|
| 134 | + public function getTopmostParentId() { |
|
| 135 | + return $this->data['topmostParentId']; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * sets the topmost parent ID and returns itself |
|
| 141 | + * |
|
| 142 | + * @param string $id |
|
| 143 | + * @return IComment |
|
| 144 | + * @since 9.0.0 |
|
| 145 | + */ |
|
| 146 | + public function setTopmostParentId($id) { |
|
| 147 | + if (!is_string($id)) { |
|
| 148 | + throw new \InvalidArgumentException('String expected.'); |
|
| 149 | + } |
|
| 150 | + $this->data['topmostParentId'] = trim($id); |
|
| 151 | + return $this; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * returns the number of children |
|
| 156 | + * |
|
| 157 | + * @return int |
|
| 158 | + * @since 9.0.0 |
|
| 159 | + */ |
|
| 160 | + public function getChildrenCount() { |
|
| 161 | + return $this->data['childrenCount']; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * sets the number of children |
|
| 166 | + * |
|
| 167 | + * @param int $count |
|
| 168 | + * @return IComment |
|
| 169 | + * @since 9.0.0 |
|
| 170 | + */ |
|
| 171 | + public function setChildrenCount($count) { |
|
| 172 | + if (!is_int($count)) { |
|
| 173 | + throw new \InvalidArgumentException('Integer expected.'); |
|
| 174 | + } |
|
| 175 | + $this->data['childrenCount'] = $count; |
|
| 176 | + return $this; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * returns the message of the comment |
|
| 181 | + * |
|
| 182 | + * @return string |
|
| 183 | + * @since 9.0.0 |
|
| 184 | + */ |
|
| 185 | + public function getMessage() { |
|
| 186 | + return $this->data['message']; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * sets the message of the comment and returns itself |
|
| 191 | + * |
|
| 192 | + * @param string $message |
|
| 193 | + * @param int $maxLength |
|
| 194 | + * @return IComment |
|
| 195 | + * @throws MessageTooLongException |
|
| 196 | + * @since 9.0.0 |
|
| 197 | + */ |
|
| 198 | + public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 199 | + if (!is_string($message)) { |
|
| 200 | + throw new \InvalidArgumentException('String expected.'); |
|
| 201 | + } |
|
| 202 | + $message = trim($message); |
|
| 203 | + if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 204 | + throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 205 | + } |
|
| 206 | + $this->data['message'] = $message; |
|
| 207 | + return $this; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * returns an array containing mentions that are included in the comment |
|
| 212 | + * |
|
| 213 | + * @return array each mention provides a 'type' and an 'id', see example below |
|
| 214 | + * @since 11.0.0 |
|
| 215 | + * |
|
| 216 | + * The return array looks like: |
|
| 217 | + * [ |
|
| 218 | + * [ |
|
| 219 | + * 'type' => 'user', |
|
| 220 | + * 'id' => 'citizen4' |
|
| 221 | + * ], |
|
| 222 | + * [ |
|
| 223 | + * 'type' => 'group', |
|
| 224 | + * 'id' => 'media' |
|
| 225 | + * ], |
|
| 226 | + * … |
|
| 227 | + * ] |
|
| 228 | + * |
|
| 229 | + */ |
|
| 230 | + public function getMentions() { |
|
| 231 | + $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 232 | + if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 233 | + return []; |
|
| 234 | + } |
|
| 235 | + $uids = array_unique($mentions[0]); |
|
| 236 | + usort($uids, static function ($uid1, $uid2) { |
|
| 237 | + return mb_strlen($uid2) <=> mb_strlen($uid1); |
|
| 238 | + }); |
|
| 239 | + $result = []; |
|
| 240 | + foreach ($uids as $uid) { |
|
| 241 | + $cleanUid = trim(substr($uid, 1), '"'); |
|
| 242 | + if (strpos($cleanUid, 'guest/') === 0) { |
|
| 243 | + $result[] = ['type' => 'guest', 'id' => $cleanUid]; |
|
| 244 | + } else { |
|
| 245 | + $result[] = ['type' => 'user', 'id' => $cleanUid]; |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + return $result; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * returns the verb of the comment |
|
| 253 | + * |
|
| 254 | + * @return string |
|
| 255 | + * @since 9.0.0 |
|
| 256 | + */ |
|
| 257 | + public function getVerb() { |
|
| 258 | + return $this->data['verb']; |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 263 | + * |
|
| 264 | + * @param string $verb |
|
| 265 | + * @return IComment |
|
| 266 | + * @since 9.0.0 |
|
| 267 | + */ |
|
| 268 | + public function setVerb($verb) { |
|
| 269 | + if (!is_string($verb) || !trim($verb)) { |
|
| 270 | + throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 271 | + } |
|
| 272 | + $this->data['verb'] = trim($verb); |
|
| 273 | + return $this; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * returns the actor type |
|
| 278 | + * |
|
| 279 | + * @return string |
|
| 280 | + * @since 9.0.0 |
|
| 281 | + */ |
|
| 282 | + public function getActorType() { |
|
| 283 | + return $this->data['actorType']; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * returns the actor ID |
|
| 288 | + * |
|
| 289 | + * @return string |
|
| 290 | + * @since 9.0.0 |
|
| 291 | + */ |
|
| 292 | + public function getActorId() { |
|
| 293 | + return $this->data['actorId']; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * sets (overwrites) the actor type and id |
|
| 298 | + * |
|
| 299 | + * @param string $actorType e.g. 'users' |
|
| 300 | + * @param string $actorId e.g. 'zombie234' |
|
| 301 | + * @return IComment |
|
| 302 | + * @since 9.0.0 |
|
| 303 | + */ |
|
| 304 | + public function setActor($actorType, $actorId) { |
|
| 305 | + if ( |
|
| 306 | + !is_string($actorType) || !trim($actorType) |
|
| 307 | + || !is_string($actorId) || $actorId === '' |
|
| 308 | + ) { |
|
| 309 | + throw new \InvalidArgumentException('String expected.'); |
|
| 310 | + } |
|
| 311 | + $this->data['actorType'] = trim($actorType); |
|
| 312 | + $this->data['actorId'] = $actorId; |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * returns the creation date of the comment. |
|
| 318 | + * |
|
| 319 | + * If not explicitly set, it shall default to the time of initialization. |
|
| 320 | + * |
|
| 321 | + * @return \DateTime |
|
| 322 | + * @since 9.0.0 |
|
| 323 | + */ |
|
| 324 | + public function getCreationDateTime() { |
|
| 325 | + return $this->data['creationDT']; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * sets the creation date of the comment and returns itself |
|
| 330 | + * |
|
| 331 | + * @param \DateTime $timestamp |
|
| 332 | + * @return IComment |
|
| 333 | + * @since 9.0.0 |
|
| 334 | + */ |
|
| 335 | + public function setCreationDateTime(\DateTime $timestamp) { |
|
| 336 | + $this->data['creationDT'] = $timestamp; |
|
| 337 | + return $this; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * returns the DateTime of the most recent child, if set, otherwise null |
|
| 342 | + * |
|
| 343 | + * @return \DateTime|null |
|
| 344 | + * @since 9.0.0 |
|
| 345 | + */ |
|
| 346 | + public function getLatestChildDateTime() { |
|
| 347 | + return $this->data['latestChildDT']; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * sets the date of the most recent child |
|
| 352 | + * |
|
| 353 | + * @param \DateTime $dateTime |
|
| 354 | + * @return IComment |
|
| 355 | + * @since 9.0.0 |
|
| 356 | + */ |
|
| 357 | + public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 358 | + $this->data['latestChildDT'] = $dateTime; |
|
| 359 | + return $this; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * returns the object type the comment is attached to |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + * @since 9.0.0 |
|
| 367 | + */ |
|
| 368 | + public function getObjectType() { |
|
| 369 | + return $this->data['objectType']; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * returns the object id the comment is attached to |
|
| 374 | + * |
|
| 375 | + * @return string |
|
| 376 | + * @since 9.0.0 |
|
| 377 | + */ |
|
| 378 | + public function getObjectId() { |
|
| 379 | + return $this->data['objectId']; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * sets (overwrites) the object of the comment |
|
| 384 | + * |
|
| 385 | + * @param string $objectType e.g. 'files' |
|
| 386 | + * @param string $objectId e.g. '16435' |
|
| 387 | + * @return IComment |
|
| 388 | + * @since 9.0.0 |
|
| 389 | + */ |
|
| 390 | + public function setObject($objectType, $objectId) { |
|
| 391 | + if ( |
|
| 392 | + !is_string($objectType) || !trim($objectType) |
|
| 393 | + || !is_string($objectId) || trim($objectId) === '' |
|
| 394 | + ) { |
|
| 395 | + throw new \InvalidArgumentException('String expected.'); |
|
| 396 | + } |
|
| 397 | + $this->data['objectType'] = trim($objectType); |
|
| 398 | + $this->data['objectId'] = trim($objectId); |
|
| 399 | + return $this; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * returns the reference id of the comment |
|
| 404 | + * |
|
| 405 | + * @return string|null |
|
| 406 | + * @since 19.0.0 |
|
| 407 | + */ |
|
| 408 | + public function getReferenceId(): ?string { |
|
| 409 | + return $this->data['referenceId']; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * sets (overwrites) the reference id of the comment |
|
| 414 | + * |
|
| 415 | + * @param string $referenceId e.g. sha256 hash sum |
|
| 416 | + * @return IComment |
|
| 417 | + * @since 19.0.0 |
|
| 418 | + */ |
|
| 419 | + public function setReferenceId(?string $referenceId): IComment { |
|
| 420 | + if ($referenceId === null) { |
|
| 421 | + $this->data['referenceId'] = $referenceId; |
|
| 422 | + } else { |
|
| 423 | + $referenceId = trim($referenceId); |
|
| 424 | + if ($referenceId === '') { |
|
| 425 | + throw new \InvalidArgumentException('Non empty string expected.'); |
|
| 426 | + } |
|
| 427 | + $this->data['referenceId'] = $referenceId; |
|
| 428 | + } |
|
| 429 | + return $this; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * sets the comment data based on an array with keys as taken from the |
|
| 434 | + * database. |
|
| 435 | + * |
|
| 436 | + * @param array $data |
|
| 437 | + * @return IComment |
|
| 438 | + */ |
|
| 439 | + protected function fromArray($data) { |
|
| 440 | + foreach (array_keys($data) as $key) { |
|
| 441 | + // translate DB keys to internal setter names |
|
| 442 | + $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 443 | + $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 444 | + |
|
| 445 | + if (method_exists($this, $setter)) { |
|
| 446 | + $this->$setter($data[$key]); |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + foreach (['actor', 'object'] as $role) { |
|
| 451 | + if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 452 | + $setter = 'set' . ucfirst($role); |
|
| 453 | + $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return $this; |
|
| 458 | + } |
|
| 459 | 459 | } |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | } |
| 202 | 202 | $message = trim($message); |
| 203 | 203 | if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
| 204 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 204 | + throw new MessageTooLongException('Comment message must not exceed '.$maxLength.' characters'); |
|
| 205 | 205 | } |
| 206 | 206 | $this->data['message'] = $message; |
| 207 | 207 | return $this; |
@@ -233,7 +233,7 @@ discard block |
||
| 233 | 233 | return []; |
| 234 | 234 | } |
| 235 | 235 | $uids = array_unique($mentions[0]); |
| 236 | - usort($uids, static function ($uid1, $uid2) { |
|
| 236 | + usort($uids, static function($uid1, $uid2) { |
|
| 237 | 237 | return mb_strlen($uid2) <=> mb_strlen($uid1); |
| 238 | 238 | }); |
| 239 | 239 | $result = []; |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | protected function fromArray($data) { |
| 440 | 440 | foreach (array_keys($data) as $key) { |
| 441 | 441 | // translate DB keys to internal setter names |
| 442 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 442 | + $setter = 'set'.implode('', array_map('ucfirst', explode('_', $key))); |
|
| 443 | 443 | $setter = str_replace('Timestamp', 'DateTime', $setter); |
| 444 | 444 | |
| 445 | 445 | if (method_exists($this, $setter)) { |
@@ -448,9 +448,9 @@ discard block |
||
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | foreach (['actor', 'object'] as $role) { |
| 451 | - if (isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 452 | - $setter = 'set' . ucfirst($role); |
|
| 453 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 451 | + if (isset($data[$role.'_type']) && isset($data[$role.'_id'])) { |
|
| 452 | + $setter = 'set'.ucfirst($role); |
|
| 453 | + $this->$setter($data[$role.'_type'], $data[$role.'_id']); |
|
| 454 | 454 | } |
| 455 | 455 | } |
| 456 | 456 | |