@@ -37,169 +37,169 @@ |
||
| 37 | 37 | |
| 38 | 38 | class Notifier implements INotifier { |
| 39 | 39 | |
| 40 | - /** @var IFactory */ |
|
| 41 | - protected $l10nFactory; |
|
| 42 | - |
|
| 43 | - /** @var IRootFolder */ |
|
| 44 | - protected $rootFolder; |
|
| 45 | - |
|
| 46 | - /** @var ICommentsManager */ |
|
| 47 | - protected $commentsManager; |
|
| 48 | - |
|
| 49 | - /** @var IURLGenerator */ |
|
| 50 | - protected $url; |
|
| 51 | - |
|
| 52 | - /** @var IUserManager */ |
|
| 53 | - protected $userManager; |
|
| 54 | - |
|
| 55 | - public function __construct( |
|
| 56 | - IFactory $l10nFactory, |
|
| 57 | - IRootFolder $rootFolder, |
|
| 58 | - ICommentsManager $commentsManager, |
|
| 59 | - IURLGenerator $url, |
|
| 60 | - IUserManager $userManager |
|
| 61 | - ) { |
|
| 62 | - $this->l10nFactory = $l10nFactory; |
|
| 63 | - $this->rootFolder = $rootFolder; |
|
| 64 | - $this->commentsManager = $commentsManager; |
|
| 65 | - $this->url = $url; |
|
| 66 | - $this->userManager = $userManager; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param INotification $notification |
|
| 71 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 72 | - * @return INotification |
|
| 73 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 74 | - */ |
|
| 75 | - public function prepare(INotification $notification, $languageCode) { |
|
| 76 | - if($notification->getApp() !== 'comments') { |
|
| 77 | - throw new \InvalidArgumentException(); |
|
| 78 | - } |
|
| 79 | - try { |
|
| 80 | - $comment = $this->commentsManager->get($notification->getObjectId()); |
|
| 81 | - } catch(NotFoundException $e) { |
|
| 82 | - // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
|
| 83 | - throw new \InvalidArgumentException('Comment not found', 0, $e); |
|
| 84 | - } |
|
| 85 | - $l = $this->l10nFactory->get('comments', $languageCode); |
|
| 86 | - $displayName = $comment->getActorId(); |
|
| 87 | - $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
|
| 88 | - if ($comment->getActorType() === 'users') { |
|
| 89 | - $commenter = $this->userManager->get($comment->getActorId()); |
|
| 90 | - if ($commenter instanceof IUser) { |
|
| 91 | - $displayName = $commenter->getDisplayName(); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - switch ($notification->getSubject()) { |
|
| 96 | - case 'mention': |
|
| 97 | - $parameters = $notification->getSubjectParameters(); |
|
| 98 | - if($parameters[0] !== 'files') { |
|
| 99 | - throw new \InvalidArgumentException('Unsupported comment object'); |
|
| 100 | - } |
|
| 101 | - $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
|
| 102 | - $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 103 | - if(empty($nodes)) { |
|
| 104 | - throw new \InvalidArgumentException('Cannot resolve file ID to node instance'); |
|
| 105 | - } |
|
| 106 | - $node = $nodes[0]; |
|
| 107 | - |
|
| 108 | - $path = rtrim($node->getPath(), '/'); |
|
| 109 | - if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 110 | - // Remove /user/files/... |
|
| 111 | - $fullPath = $path; |
|
| 112 | - list(,,, $path) = explode('/', $fullPath, 4); |
|
| 113 | - } |
|
| 114 | - $subjectParameters = [ |
|
| 115 | - 'file' => [ |
|
| 116 | - 'type' => 'file', |
|
| 117 | - 'id' => $comment->getObjectId(), |
|
| 118 | - 'name' => $node->getName(), |
|
| 119 | - 'path' => $path, |
|
| 120 | - 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
|
| 121 | - ], |
|
| 122 | - ]; |
|
| 123 | - |
|
| 124 | - if ($isDeletedActor) { |
|
| 125 | - $subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
|
| 126 | - } else { |
|
| 127 | - $subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
|
| 128 | - $subjectParameters['user'] = [ |
|
| 129 | - 'type' => 'user', |
|
| 130 | - 'id' => $comment->getActorId(), |
|
| 131 | - 'name' => $displayName, |
|
| 132 | - ]; |
|
| 133 | - } |
|
| 134 | - list($message, $messageParameters) = $this->commentToRichMessage($comment); |
|
| 135 | - $notification->setRichSubject($subject, $subjectParameters) |
|
| 136 | - ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
|
| 137 | - ->setRichMessage($message, $messageParameters) |
|
| 138 | - ->setParsedMessage($this->richToParsed($message, $messageParameters)) |
|
| 139 | - ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
|
| 140 | - ->setLink($this->url->linkToRouteAbsolute( |
|
| 141 | - 'comments.Notifications.view', |
|
| 142 | - ['id' => $comment->getId()]) |
|
| 143 | - ); |
|
| 144 | - |
|
| 145 | - return $notification; |
|
| 146 | - break; |
|
| 147 | - |
|
| 148 | - default: |
|
| 149 | - throw new \InvalidArgumentException('Invalid subject'); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function commentToRichMessage(IComment $comment): array { |
|
| 154 | - $message = $comment->getMessage(); |
|
| 155 | - $messageParameters = []; |
|
| 156 | - $mentionTypeCount = []; |
|
| 157 | - $mentions = $comment->getMentions(); |
|
| 158 | - foreach ($mentions as $mention) { |
|
| 159 | - if ($mention['type'] === 'user') { |
|
| 160 | - $user = $this->userManager->get($mention['id']); |
|
| 161 | - if (!$user instanceof IUser) { |
|
| 162 | - continue; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - if (!array_key_exists($mention['type'], $mentionTypeCount)) { |
|
| 166 | - $mentionTypeCount[$mention['type']] = 0; |
|
| 167 | - } |
|
| 168 | - $mentionTypeCount[$mention['type']]++; |
|
| 169 | - // To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) |
|
| 170 | - // the mention parameter ID does not include the mention ID (which |
|
| 171 | - // could contain characters like '@' for user IDs) but a one-based |
|
| 172 | - // index of the mentions of that type. |
|
| 173 | - $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 174 | - $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 175 | - try { |
|
| 176 | - $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
| 177 | - } catch (\OutOfBoundsException $e) { |
|
| 178 | - // There is no registered display name resolver for the mention |
|
| 179 | - // type, so the client decides what to display. |
|
| 180 | - $displayName = ''; |
|
| 181 | - } |
|
| 182 | - $messageParameters[$mentionParameterId] = [ |
|
| 183 | - 'type' => $mention['type'], |
|
| 184 | - 'id' => $mention['id'], |
|
| 185 | - 'name' => $displayName |
|
| 186 | - ]; |
|
| 187 | - } |
|
| 188 | - return [$message, $messageParameters]; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function richToParsed(string $message, array $parameters): string { |
|
| 192 | - $placeholders = $replacements = []; |
|
| 193 | - foreach ($parameters as $placeholder => $parameter) { |
|
| 194 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 195 | - if ($parameter['type'] === 'user') { |
|
| 196 | - $replacements[] = '@' . $parameter['name']; |
|
| 197 | - } else if ($parameter['type'] === 'file') { |
|
| 198 | - $replacements[] = $parameter['path']; |
|
| 199 | - } else { |
|
| 200 | - $replacements[] = $parameter['name']; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - return str_replace($placeholders, $replacements, $message); |
|
| 204 | - } |
|
| 40 | + /** @var IFactory */ |
|
| 41 | + protected $l10nFactory; |
|
| 42 | + |
|
| 43 | + /** @var IRootFolder */ |
|
| 44 | + protected $rootFolder; |
|
| 45 | + |
|
| 46 | + /** @var ICommentsManager */ |
|
| 47 | + protected $commentsManager; |
|
| 48 | + |
|
| 49 | + /** @var IURLGenerator */ |
|
| 50 | + protected $url; |
|
| 51 | + |
|
| 52 | + /** @var IUserManager */ |
|
| 53 | + protected $userManager; |
|
| 54 | + |
|
| 55 | + public function __construct( |
|
| 56 | + IFactory $l10nFactory, |
|
| 57 | + IRootFolder $rootFolder, |
|
| 58 | + ICommentsManager $commentsManager, |
|
| 59 | + IURLGenerator $url, |
|
| 60 | + IUserManager $userManager |
|
| 61 | + ) { |
|
| 62 | + $this->l10nFactory = $l10nFactory; |
|
| 63 | + $this->rootFolder = $rootFolder; |
|
| 64 | + $this->commentsManager = $commentsManager; |
|
| 65 | + $this->url = $url; |
|
| 66 | + $this->userManager = $userManager; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param INotification $notification |
|
| 71 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
| 72 | + * @return INotification |
|
| 73 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
| 74 | + */ |
|
| 75 | + public function prepare(INotification $notification, $languageCode) { |
|
| 76 | + if($notification->getApp() !== 'comments') { |
|
| 77 | + throw new \InvalidArgumentException(); |
|
| 78 | + } |
|
| 79 | + try { |
|
| 80 | + $comment = $this->commentsManager->get($notification->getObjectId()); |
|
| 81 | + } catch(NotFoundException $e) { |
|
| 82 | + // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
|
| 83 | + throw new \InvalidArgumentException('Comment not found', 0, $e); |
|
| 84 | + } |
|
| 85 | + $l = $this->l10nFactory->get('comments', $languageCode); |
|
| 86 | + $displayName = $comment->getActorId(); |
|
| 87 | + $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER; |
|
| 88 | + if ($comment->getActorType() === 'users') { |
|
| 89 | + $commenter = $this->userManager->get($comment->getActorId()); |
|
| 90 | + if ($commenter instanceof IUser) { |
|
| 91 | + $displayName = $commenter->getDisplayName(); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + switch ($notification->getSubject()) { |
|
| 96 | + case 'mention': |
|
| 97 | + $parameters = $notification->getSubjectParameters(); |
|
| 98 | + if($parameters[0] !== 'files') { |
|
| 99 | + throw new \InvalidArgumentException('Unsupported comment object'); |
|
| 100 | + } |
|
| 101 | + $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
|
| 102 | + $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 103 | + if(empty($nodes)) { |
|
| 104 | + throw new \InvalidArgumentException('Cannot resolve file ID to node instance'); |
|
| 105 | + } |
|
| 106 | + $node = $nodes[0]; |
|
| 107 | + |
|
| 108 | + $path = rtrim($node->getPath(), '/'); |
|
| 109 | + if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 110 | + // Remove /user/files/... |
|
| 111 | + $fullPath = $path; |
|
| 112 | + list(,,, $path) = explode('/', $fullPath, 4); |
|
| 113 | + } |
|
| 114 | + $subjectParameters = [ |
|
| 115 | + 'file' => [ |
|
| 116 | + 'type' => 'file', |
|
| 117 | + 'id' => $comment->getObjectId(), |
|
| 118 | + 'name' => $node->getName(), |
|
| 119 | + 'path' => $path, |
|
| 120 | + 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
|
| 121 | + ], |
|
| 122 | + ]; |
|
| 123 | + |
|
| 124 | + if ($isDeletedActor) { |
|
| 125 | + $subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
|
| 126 | + } else { |
|
| 127 | + $subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
|
| 128 | + $subjectParameters['user'] = [ |
|
| 129 | + 'type' => 'user', |
|
| 130 | + 'id' => $comment->getActorId(), |
|
| 131 | + 'name' => $displayName, |
|
| 132 | + ]; |
|
| 133 | + } |
|
| 134 | + list($message, $messageParameters) = $this->commentToRichMessage($comment); |
|
| 135 | + $notification->setRichSubject($subject, $subjectParameters) |
|
| 136 | + ->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
|
| 137 | + ->setRichMessage($message, $messageParameters) |
|
| 138 | + ->setParsedMessage($this->richToParsed($message, $messageParameters)) |
|
| 139 | + ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
|
| 140 | + ->setLink($this->url->linkToRouteAbsolute( |
|
| 141 | + 'comments.Notifications.view', |
|
| 142 | + ['id' => $comment->getId()]) |
|
| 143 | + ); |
|
| 144 | + |
|
| 145 | + return $notification; |
|
| 146 | + break; |
|
| 147 | + |
|
| 148 | + default: |
|
| 149 | + throw new \InvalidArgumentException('Invalid subject'); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function commentToRichMessage(IComment $comment): array { |
|
| 154 | + $message = $comment->getMessage(); |
|
| 155 | + $messageParameters = []; |
|
| 156 | + $mentionTypeCount = []; |
|
| 157 | + $mentions = $comment->getMentions(); |
|
| 158 | + foreach ($mentions as $mention) { |
|
| 159 | + if ($mention['type'] === 'user') { |
|
| 160 | + $user = $this->userManager->get($mention['id']); |
|
| 161 | + if (!$user instanceof IUser) { |
|
| 162 | + continue; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + if (!array_key_exists($mention['type'], $mentionTypeCount)) { |
|
| 166 | + $mentionTypeCount[$mention['type']] = 0; |
|
| 167 | + } |
|
| 168 | + $mentionTypeCount[$mention['type']]++; |
|
| 169 | + // To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) |
|
| 170 | + // the mention parameter ID does not include the mention ID (which |
|
| 171 | + // could contain characters like '@' for user IDs) but a one-based |
|
| 172 | + // index of the mentions of that type. |
|
| 173 | + $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 174 | + $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 175 | + try { |
|
| 176 | + $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
| 177 | + } catch (\OutOfBoundsException $e) { |
|
| 178 | + // There is no registered display name resolver for the mention |
|
| 179 | + // type, so the client decides what to display. |
|
| 180 | + $displayName = ''; |
|
| 181 | + } |
|
| 182 | + $messageParameters[$mentionParameterId] = [ |
|
| 183 | + 'type' => $mention['type'], |
|
| 184 | + 'id' => $mention['id'], |
|
| 185 | + 'name' => $displayName |
|
| 186 | + ]; |
|
| 187 | + } |
|
| 188 | + return [$message, $messageParameters]; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function richToParsed(string $message, array $parameters): string { |
|
| 192 | + $placeholders = $replacements = []; |
|
| 193 | + foreach ($parameters as $placeholder => $parameter) { |
|
| 194 | + $placeholders[] = '{' . $placeholder . '}'; |
|
| 195 | + if ($parameter['type'] === 'user') { |
|
| 196 | + $replacements[] = '@' . $parameter['name']; |
|
| 197 | + } else if ($parameter['type'] === 'file') { |
|
| 198 | + $replacements[] = $parameter['path']; |
|
| 199 | + } else { |
|
| 200 | + $replacements[] = $parameter['name']; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + return str_replace($placeholders, $replacements, $message); |
|
| 204 | + } |
|
| 205 | 205 | } |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
| 74 | 74 | */ |
| 75 | 75 | public function prepare(INotification $notification, $languageCode) { |
| 76 | - if($notification->getApp() !== 'comments') { |
|
| 76 | + if ($notification->getApp() !== 'comments') { |
|
| 77 | 77 | throw new \InvalidArgumentException(); |
| 78 | 78 | } |
| 79 | 79 | try { |
| 80 | 80 | $comment = $this->commentsManager->get($notification->getObjectId()); |
| 81 | - } catch(NotFoundException $e) { |
|
| 81 | + } catch (NotFoundException $e) { |
|
| 82 | 82 | // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all |
| 83 | 83 | throw new \InvalidArgumentException('Comment not found', 0, $e); |
| 84 | 84 | } |
@@ -95,18 +95,18 @@ discard block |
||
| 95 | 95 | switch ($notification->getSubject()) { |
| 96 | 96 | case 'mention': |
| 97 | 97 | $parameters = $notification->getSubjectParameters(); |
| 98 | - if($parameters[0] !== 'files') { |
|
| 98 | + if ($parameters[0] !== 'files') { |
|
| 99 | 99 | throw new \InvalidArgumentException('Unsupported comment object'); |
| 100 | 100 | } |
| 101 | 101 | $userFolder = $this->rootFolder->getUserFolder($notification->getUser()); |
| 102 | - $nodes = $userFolder->getById((int)$parameters[1]); |
|
| 103 | - if(empty($nodes)) { |
|
| 102 | + $nodes = $userFolder->getById((int) $parameters[1]); |
|
| 103 | + if (empty($nodes)) { |
|
| 104 | 104 | throw new \InvalidArgumentException('Cannot resolve file ID to node instance'); |
| 105 | 105 | } |
| 106 | 106 | $node = $nodes[0]; |
| 107 | 107 | |
| 108 | 108 | $path = rtrim($node->getPath(), '/'); |
| 109 | - if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { |
|
| 109 | + if (strpos($path, '/'.$notification->getUser().'/files/') === 0) { |
|
| 110 | 110 | // Remove /user/files/... |
| 111 | 111 | $fullPath = $path; |
| 112 | 112 | list(,,, $path) = explode('/', $fullPath, 4); |
@@ -170,8 +170,8 @@ discard block |
||
| 170 | 170 | // the mention parameter ID does not include the mention ID (which |
| 171 | 171 | // could contain characters like '@' for user IDs) but a one-based |
| 172 | 172 | // index of the mentions of that type. |
| 173 | - $mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
| 174 | - $message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
| 173 | + $mentionParameterId = 'mention-'.$mention['type'].$mentionTypeCount[$mention['type']]; |
|
| 174 | + $message = str_replace('@'.$mention['id'], '{'.$mentionParameterId.'}', $message); |
|
| 175 | 175 | try { |
| 176 | 176 | $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
| 177 | 177 | } catch (\OutOfBoundsException $e) { |
@@ -191,9 +191,9 @@ discard block |
||
| 191 | 191 | public function richToParsed(string $message, array $parameters): string { |
| 192 | 192 | $placeholders = $replacements = []; |
| 193 | 193 | foreach ($parameters as $placeholder => $parameter) { |
| 194 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 194 | + $placeholders[] = '{'.$placeholder.'}'; |
|
| 195 | 195 | if ($parameter['type'] === 'user') { |
| 196 | - $replacements[] = '@' . $parameter['name']; |
|
| 196 | + $replacements[] = '@'.$parameter['name']; |
|
| 197 | 197 | } else if ($parameter['type'] === 'file') { |
| 198 | 198 | $replacements[] = $parameter['path']; |
| 199 | 199 | } else { |