|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license AGPL-3.0 |
|
10
|
|
|
* |
|
11
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
13
|
|
|
* as published by the Free Software Foundation. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Comments\Notification; |
|
26
|
|
|
|
|
27
|
|
|
use OCP\Comments\IComment; |
|
28
|
|
|
use OCP\Comments\ICommentsManager; |
|
29
|
|
|
use OCP\Comments\NotFoundException; |
|
30
|
|
|
use OCP\Files\IRootFolder; |
|
31
|
|
|
use OCP\IURLGenerator; |
|
32
|
|
|
use OCP\IUser; |
|
33
|
|
|
use OCP\IUserManager; |
|
34
|
|
|
use OCP\L10N\IFactory; |
|
35
|
|
|
use OCP\Notification\INotification; |
|
36
|
|
|
use OCP\Notification\INotifier; |
|
37
|
|
|
|
|
38
|
|
|
class Notifier implements INotifier { |
|
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
|
|
|
|
|
115
|
|
|
$subjectParameters = [ |
|
116
|
|
|
'file' => [ |
|
117
|
|
|
'type' => 'file', |
|
118
|
|
|
'id' => $comment->getObjectId(), |
|
119
|
|
|
'name' => $node->getName(), |
|
120
|
|
|
'path' => $path, |
|
121
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]), |
|
122
|
|
|
], |
|
123
|
|
|
]; |
|
124
|
|
|
|
|
125
|
|
|
if ($isDeletedActor) { |
|
126
|
|
|
$subject = $l->t('You were mentioned on “{file}”, in a comment by a user that has since been deleted'); |
|
127
|
|
|
} else { |
|
128
|
|
|
$subject = $l->t('{user} mentioned you in a comment on “{file}”'); |
|
129
|
|
|
$subjectParameters['user'] = [ |
|
130
|
|
|
'type' => 'user', |
|
131
|
|
|
'id' => $comment->getActorId(), |
|
132
|
|
|
'name' => $displayName, |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
list($message, $messageParameters) = $this->commentToRichMessage($comment); |
|
137
|
|
|
|
|
138
|
|
|
$notification->setRichSubject($subject, $subjectParameters) |
|
139
|
|
|
->setParsedSubject($this->richToParsed($subject, $subjectParameters)) |
|
140
|
|
|
->setRichMessage($message, $messageParameters) |
|
141
|
|
|
->setParsedMessage($this->richToParsed($message, $messageParameters)) |
|
142
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg'))) |
|
143
|
|
|
->setLink($this->url->linkToRouteAbsolute( |
|
144
|
|
|
'comments.Notifications.view', |
|
145
|
|
|
['id' => $comment->getId()]) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return $notification; |
|
149
|
|
|
break; |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
default: |
|
152
|
|
|
throw new \InvalidArgumentException('Invalid subject'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function commentToRichMessage(IComment $comment): array { |
|
158
|
|
|
$message = $comment->getMessage(); |
|
159
|
|
|
$messageParameters = []; |
|
160
|
|
|
|
|
161
|
|
|
$mentionTypeCount = []; |
|
162
|
|
|
|
|
163
|
|
|
$mentions = $comment->getMentions(); |
|
164
|
|
|
foreach ($mentions as $mention) { |
|
165
|
|
|
if ($mention['type'] === 'user') { |
|
166
|
|
|
$user = $this->userManager->get($mention['id']); |
|
167
|
|
|
if (!$user instanceof IUser) { |
|
168
|
|
|
continue; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!array_key_exists($mention['type'], $mentionTypeCount)) { |
|
173
|
|
|
$mentionTypeCount[$mention['type']] = 0; |
|
174
|
|
|
} |
|
175
|
|
|
$mentionTypeCount[$mention['type']]++; |
|
176
|
|
|
|
|
177
|
|
|
// To keep a limited character set in parameter IDs ([a-zA-Z0-9-]) |
|
178
|
|
|
// the mention parameter ID does not include the mention ID (which |
|
179
|
|
|
// could contain characters like '@' for user IDs) but a one-based |
|
180
|
|
|
// index of the mentions of that type. |
|
181
|
|
|
$mentionParameterId = 'mention-' . $mention['type'] . $mentionTypeCount[$mention['type']]; |
|
182
|
|
|
|
|
183
|
|
|
$message = str_replace('@' . $mention['id'], '{' . $mentionParameterId . '}', $message); |
|
184
|
|
|
|
|
185
|
|
|
try { |
|
186
|
|
|
$displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']); |
|
187
|
|
|
} catch (\OutOfBoundsException $e) { |
|
188
|
|
|
// There is no registered display name resolver for the mention |
|
189
|
|
|
// type, so the client decides what to display. |
|
190
|
|
|
$displayName = ''; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$messageParameters[$mentionParameterId] = [ |
|
194
|
|
|
'type' => $mention['type'], |
|
195
|
|
|
'id' => $mention['id'], |
|
196
|
|
|
'name' => $displayName |
|
197
|
|
|
]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return [$message, $messageParameters]; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function richToParsed(string $message, array $parameters): string { |
|
204
|
|
|
$placeholders = $replacements = []; |
|
205
|
|
|
foreach ($parameters as $placeholder => $parameter) { |
|
206
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
|
207
|
|
|
if ($parameter['type'] === 'user') { |
|
208
|
|
|
$replacements[] = '@' . $parameter['name']; |
|
209
|
|
|
} else if ($parameter['type'] === 'file') { |
|
210
|
|
|
$replacements[] = $parameter['path']; |
|
211
|
|
|
} else { |
|
212
|
|
|
$replacements[] = $parameter['name']; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return str_replace($placeholders, $replacements, $message); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.