|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\Files\Activity; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\Activity\IEvent; |
|
25
|
|
|
use OCP\Activity\IManager; |
|
26
|
|
|
use OCP\Activity\IProvider; |
|
27
|
|
|
use OCP\IL10N; |
|
28
|
|
|
use OCP\IURLGenerator; |
|
29
|
|
|
use OCP\IUser; |
|
30
|
|
|
use OCP\IUserManager; |
|
31
|
|
|
|
|
32
|
|
|
class Provider implements IProvider { |
|
33
|
|
|
|
|
34
|
|
|
/** @var IL10N */ |
|
35
|
|
|
protected $l; |
|
36
|
|
|
|
|
37
|
|
|
/** @var IURLGenerator */ |
|
38
|
|
|
protected $url; |
|
39
|
|
|
|
|
40
|
|
|
/** @var IManager */ |
|
41
|
|
|
protected $activityManager; |
|
42
|
|
|
|
|
43
|
|
|
/** @var IUserManager */ |
|
44
|
|
|
protected $userManager; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string[] cached displayNames - key is the UID and value the displayname */ |
|
47
|
|
|
protected $displayNames = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param IL10N $l |
|
51
|
|
|
* @param IURLGenerator $url |
|
52
|
|
|
* @param IManager $activityManager |
|
53
|
|
|
* @param IUserManager $userManager |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, IUserManager $userManager) { |
|
|
|
|
|
|
56
|
|
|
$this->l = $l; |
|
57
|
|
|
$this->url = $url; |
|
58
|
|
|
$this->activityManager = $activityManager; |
|
59
|
|
|
$this->userManager = $userManager; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param IEvent $event |
|
64
|
|
|
* @param IEvent|null $previousEvent |
|
65
|
|
|
* @return IEvent |
|
66
|
|
|
* @throws \InvalidArgumentException |
|
67
|
|
|
* @since 11.0.0 |
|
68
|
|
|
*/ |
|
69
|
|
|
public function parse(IEvent $event, IEvent $previousEvent = null) { |
|
70
|
|
|
if ($event->getApp() !== 'files') { |
|
71
|
|
|
throw new \InvalidArgumentException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($previousEvent instanceof IEvent && $event->getSubject() !== $previousEvent->getSubject()) { |
|
75
|
|
|
// Different subject means not the same string, so no grouping |
|
76
|
|
|
$previousEvent = null; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) { |
|
80
|
|
|
try { |
|
81
|
|
|
return $this->parseShortVersion($event); |
|
82
|
|
|
} catch (\InvalidArgumentException $e) { |
|
83
|
|
|
// Ignore and simply use the long version... |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->parseLongVersion($event); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param IEvent $event |
|
92
|
|
|
* @return IEvent |
|
93
|
|
|
* @throws \InvalidArgumentException |
|
94
|
|
|
* @since 11.0.0 |
|
95
|
|
|
*/ |
|
96
|
|
|
public function parseShortVersion(IEvent $event) { |
|
97
|
|
|
$parsedParameters = $this->getParsedParameters($event->getSubject(), $event->getSubjectParameters()); |
|
98
|
|
|
$richParameters = $this->getRichParameters($event->getSubject(), $event->getSubjectParameters()); |
|
99
|
|
|
|
|
100
|
|
|
if ($event->getSubject() === 'created_by') { |
|
101
|
|
|
$event->setParsedSubject($this->l->t('Created by %s', [$parsedParameters[1]])) |
|
102
|
|
|
->setRichSubject($this->l->t('Created by {user1}'), ['user1' => $richParameters['user1']]) |
|
103
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg'))); |
|
104
|
|
|
} else if ($event->getSubject() === 'changed_by') { |
|
105
|
|
|
$event->setParsedSubject($this->l->t('Changed by %2$s', [$parsedParameters[1]])) |
|
106
|
|
|
->setRichSubject($this->l->t('Changed by {user1}'), ['user1' => $richParameters['user1']]) |
|
107
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
108
|
|
|
} else if ($event->getSubject() === 'deleted_by') { |
|
109
|
|
|
$event->setParsedSubject($this->l->t('Deleted by %2$s', [$parsedParameters[1]])) |
|
110
|
|
|
->setRichSubject($this->l->t('Deleted by {user1}'), ['user1' => $richParameters['user1']]) |
|
111
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg'))); |
|
112
|
|
|
} else if ($event->getSubject() === 'restored_by') { |
|
113
|
|
|
$event->setParsedSubject($this->l->t('Restored by %2$s', [$parsedParameters[1]])) |
|
114
|
|
|
->setRichSubject($this->l->t('Restored by {user1}'), ['user1' => $richParameters['user1']]); |
|
115
|
|
|
} else if ($event->getSubject() === 'renamed_by') { |
|
116
|
|
|
$event->setParsedSubject($this->l->t('Renamed by %2$s', [$parsedParameters[1]])) |
|
117
|
|
|
->setRichSubject($this->l->t('Renamed by {user1}'), ['user1' => $richParameters['user1']]) |
|
118
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
119
|
|
|
} else if ($event->getSubject() === 'moved_by') { |
|
120
|
|
|
$event->setParsedSubject($this->l->t('Moved by %2$s', [$parsedParameters[1]])) |
|
121
|
|
|
->setRichSubject($this->l->t('Moved by {user1}'), ['user1' => $richParameters['user1']]) |
|
122
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
123
|
|
|
} else { |
|
124
|
|
|
throw new \InvalidArgumentException(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $event; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param IEvent $event |
|
132
|
|
|
* @return IEvent |
|
133
|
|
|
* @throws \InvalidArgumentException |
|
134
|
|
|
* @since 11.0.0 |
|
135
|
|
|
*/ |
|
136
|
|
|
public function parseLongVersion(IEvent $event) { |
|
137
|
|
|
$parsedParameters = $this->getParsedParameters($event->getSubject(), $event->getSubjectParameters()); |
|
138
|
|
|
$richParameters = $this->getRichParameters($event->getSubject(), $event->getSubjectParameters()); |
|
139
|
|
|
|
|
140
|
|
|
if ($event->getSubject() === 'created_self') { |
|
141
|
|
|
$event->setParsedSubject($this->l->t('You created %1$s', $parsedParameters)) |
|
142
|
|
|
->setRichSubject($this->l->t('You created {file1}'), $richParameters) |
|
143
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg'))); |
|
144
|
|
|
} else if ($event->getSubject() === 'created_by') { |
|
145
|
|
|
$event->setParsedSubject($this->l->t('%2$s created %1$s', $parsedParameters)) |
|
146
|
|
|
->setRichSubject($this->l->t('{user1} created {file1}'), $richParameters) |
|
147
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg'))); |
|
148
|
|
|
} else if ($event->getSubject() === 'created_public') { |
|
149
|
|
|
$event->setParsedSubject($this->l->t('%1$s was created in a public folder', $parsedParameters)) |
|
150
|
|
|
->setRichSubject($this->l->t('{file1} was created in a public folder'), $richParameters) |
|
151
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg'))); |
|
152
|
|
|
} else if ($event->getSubject() === 'changed_self') { |
|
153
|
|
|
$event->setParsedSubject($this->l->t('You changed %1$s', $parsedParameters)) |
|
154
|
|
|
->setRichSubject($this->l->t('You changed {file1}'), $richParameters) |
|
155
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
156
|
|
|
} else if ($event->getSubject() === 'changed_by') { |
|
157
|
|
|
$event->setParsedSubject($this->l->t('%2$s changed %1$s', $parsedParameters)) |
|
158
|
|
|
->setRichSubject($this->l->t('{user1} changed {file1}'), $richParameters) |
|
159
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
160
|
|
|
} else if ($event->getSubject() === 'deleted_self') { |
|
161
|
|
|
$event->setParsedSubject($this->l->t('You deleted %1$s', $parsedParameters)) |
|
162
|
|
|
->setRichSubject($this->l->t('You deleted {file1}'), $richParameters) |
|
163
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg'))); |
|
164
|
|
|
} else if ($event->getSubject() === 'deleted_by') { |
|
165
|
|
|
$event->setParsedSubject($this->l->t('%2$s deleted %1$s', $parsedParameters)) |
|
166
|
|
|
->setRichSubject($this->l->t('{user1} deleted {file1}'), $richParameters) |
|
167
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg'))); |
|
168
|
|
|
} else if ($event->getSubject() === 'restored_self') { |
|
169
|
|
|
$event->setParsedSubject($this->l->t('You restored %1$s', $parsedParameters)) |
|
170
|
|
|
->setRichSubject($this->l->t('You restored {file1}'), $richParameters); |
|
171
|
|
|
} else if ($event->getSubject() === 'restored_by') { |
|
172
|
|
|
$event->setParsedSubject($this->l->t('%2$s restored %1$s', $parsedParameters)) |
|
173
|
|
|
->setRichSubject($this->l->t('{user1} restored {file1}'), $richParameters); |
|
174
|
|
|
} else if ($event->getSubject() === 'renamed_self') { |
|
175
|
|
|
$event->setParsedSubject($this->l->t('You renamed %2$s to %1$s', $parsedParameters)) |
|
176
|
|
|
->setRichSubject($this->l->t('You renamed {file2} to {file1}'), $richParameters) |
|
177
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
178
|
|
|
} else if ($event->getSubject() === 'renamed_by') { |
|
179
|
|
|
$event->setParsedSubject($this->l->t('%2$s renamed %3$s to %1$s', $parsedParameters)) |
|
180
|
|
|
->setRichSubject($this->l->t('{user1} renamed {file2} to {file1}'), $richParameters) |
|
181
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
182
|
|
|
} else if ($event->getSubject() === 'moved_self') { |
|
183
|
|
|
$event->setParsedSubject($this->l->t('You moved %2$s to %1$s', $parsedParameters)) |
|
184
|
|
|
->setRichSubject($this->l->t('You moved {file2} to {file1}'), $richParameters) |
|
185
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
186
|
|
|
} else if ($event->getSubject() === 'moved_by') { |
|
187
|
|
|
$event->setParsedSubject($this->l->t('%2$s moved %3$s to %1$s', $parsedParameters)) |
|
188
|
|
|
->setRichSubject($this->l->t('{user1} moved {file2} to {file1}'), $richParameters) |
|
189
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg'))); |
|
190
|
|
|
} else { |
|
191
|
|
|
throw new \InvalidArgumentException(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $event; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
protected function getParsedParameters($subject, array $parameters) { |
|
198
|
|
|
switch ($subject) { |
|
199
|
|
|
case 'created_self': |
|
200
|
|
|
case 'created_public': |
|
201
|
|
|
case 'changed_self': |
|
202
|
|
|
case 'deleted_self': |
|
203
|
|
|
case 'restored_self': |
|
204
|
|
|
return [ |
|
205
|
|
|
array_shift($parameters[0]), |
|
206
|
|
|
]; |
|
207
|
|
|
case 'created_by': |
|
208
|
|
|
case 'changed_by': |
|
209
|
|
|
case 'deleted_by': |
|
210
|
|
|
case 'restored_by': |
|
211
|
|
|
return [ |
|
212
|
|
|
array_shift($parameters[0]), |
|
213
|
|
|
$parameters[1], |
|
214
|
|
|
]; |
|
215
|
|
|
case 'renamed_self': |
|
216
|
|
|
case 'moved_self': |
|
217
|
|
|
return [ |
|
218
|
|
|
array_shift($parameters[0]), |
|
219
|
|
|
array_shift($parameters[1]), |
|
220
|
|
|
]; |
|
221
|
|
|
case 'renamed_by': |
|
222
|
|
|
case 'moved_by': |
|
223
|
|
|
return [ |
|
224
|
|
|
array_shift($parameters[0]), |
|
225
|
|
|
$parameters[1], |
|
226
|
|
|
array_shift($parameters[2]), |
|
227
|
|
|
]; |
|
228
|
|
|
} |
|
229
|
|
|
return []; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
protected function getRichParameters($subject, array $parameters) { |
|
233
|
|
|
switch ($subject) { |
|
234
|
|
|
case 'created_self': |
|
235
|
|
|
case 'created_public': |
|
236
|
|
|
case 'changed_self': |
|
237
|
|
|
case 'deleted_self': |
|
238
|
|
|
case 'restored_self': |
|
239
|
|
|
return [ |
|
240
|
|
|
'file1' => $this->getRichFileParameter($parameters[0]), |
|
241
|
|
|
]; |
|
242
|
|
|
case 'created_by': |
|
243
|
|
|
case 'changed_by': |
|
244
|
|
|
case 'deleted_by': |
|
245
|
|
|
case 'restored_by': |
|
246
|
|
|
return [ |
|
247
|
|
|
'file1' => $this->getRichFileParameter($parameters[0]), |
|
248
|
|
|
'user1' => $this->getRichUserParameter($parameters[1]), |
|
249
|
|
|
]; |
|
250
|
|
|
case 'renamed_self': |
|
251
|
|
|
case 'moved_self': |
|
252
|
|
|
return [ |
|
253
|
|
|
'file1' => $this->getRichFileParameter($parameters[0]), |
|
254
|
|
|
'file2' => $this->getRichFileParameter($parameters[1]), |
|
255
|
|
|
]; |
|
256
|
|
|
case 'renamed_by': |
|
257
|
|
|
case 'moved_by': |
|
258
|
|
|
return [ |
|
259
|
|
|
'file1' => $this->getRichFileParameter($parameters[0]), |
|
260
|
|
|
'user1' => $this->getRichUserParameter($parameters[1]), |
|
261
|
|
|
'file2' => $this->getRichFileParameter($parameters[2]), |
|
262
|
|
|
]; |
|
263
|
|
|
} |
|
264
|
|
|
return []; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
protected function getRichFileParameter($parameter) { |
|
268
|
|
|
$path = reset($parameter); |
|
269
|
|
|
$id = key($parameter); |
|
270
|
|
|
return [ |
|
271
|
|
|
'type' => 'file', |
|
272
|
|
|
'id' => $id, |
|
273
|
|
|
'name' => basename($path), |
|
274
|
|
|
'path' => $path, |
|
275
|
|
|
]; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
View Code Duplication |
protected function getRichUserParameter($uid) { |
|
|
|
|
|
|
279
|
|
|
if (!isset($this->displayNames[$uid])) { |
|
280
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return [ |
|
284
|
|
|
'type' => 'user', |
|
285
|
|
|
'id' => $uid, |
|
286
|
|
|
'name' => $this->displayNames[$uid], |
|
287
|
|
|
]; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param string $uid |
|
292
|
|
|
* @return string |
|
293
|
|
|
*/ |
|
294
|
|
|
protected function getDisplayName($uid) { |
|
295
|
|
|
$user = $this->userManager->get($uid); |
|
296
|
|
|
if ($user instanceof IUser) { |
|
297
|
|
|
return $user->getDisplayName(); |
|
298
|
|
|
} else { |
|
299
|
|
|
return $uid; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.