1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2019 Alexandru Puiu <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @author Julius Härtl <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OCA\Deck\Activity; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
use cogpowered\FineDiff\Diff; |
30
|
|
|
use OCA\Deck\Db\Acl; |
31
|
|
|
use OCP\Activity\IEvent; |
32
|
|
|
use OCP\Activity\IProvider; |
33
|
|
|
use OCP\Comments\IComment; |
34
|
|
|
use OCP\Comments\ICommentsManager; |
35
|
|
|
use OCP\Comments\NotFoundException; |
36
|
|
|
use OCP\IConfig; |
37
|
|
|
use OCP\IURLGenerator; |
38
|
|
|
use OCP\IUserManager; |
39
|
|
|
use OCP\L10N\IFactory; |
40
|
|
|
|
41
|
|
|
class DeckProvider implements IProvider { |
42
|
|
|
|
43
|
|
|
/** @var string */ |
44
|
|
|
private $userId; |
45
|
|
|
/** @var IURLGenerator */ |
46
|
|
|
private $urlGenerator; |
47
|
|
|
/** @var ActivityManager */ |
48
|
|
|
private $activityManager; |
49
|
|
|
/** @var IUserManager */ |
50
|
|
|
private $userManager; |
51
|
|
|
/** @var ICommentsManager */ |
52
|
|
|
private $commentsManager; |
53
|
|
|
/** @var IFactory */ |
54
|
|
|
private $l10nFactory; |
55
|
|
|
/** @var IConfig */ |
56
|
|
|
private $config; |
57
|
|
|
|
58
|
|
|
public function __construct(IURLGenerator $urlGenerator, ActivityManager $activityManager, IUserManager $userManager, ICommentsManager $commentsManager, IFactory $l10n, IConfig $config, $userId) { |
59
|
|
|
$this->userId = $userId; |
60
|
|
|
$this->urlGenerator = $urlGenerator; |
61
|
|
|
$this->activityManager = $activityManager; |
62
|
|
|
$this->commentsManager = $commentsManager; |
63
|
|
|
$this->userManager = $userManager; |
64
|
|
|
$this->l10nFactory = $l10n; |
65
|
|
|
$this->config = $config; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $language The language which should be used for translating, e.g. "en" |
70
|
|
|
* @param IEvent $event The current event which should be parsed |
71
|
|
|
* @param IEvent|null $previousEvent A potential previous event which you can combine with the current one. |
72
|
|
|
* To do so, simply use setChildEvent($previousEvent) after setting the |
73
|
|
|
* combined subject on the current event. |
74
|
|
|
* @return IEvent |
75
|
|
|
* @throws \InvalidArgumentException Should be thrown if your provider does not know this event |
76
|
|
|
* @since 11.0.0 |
77
|
|
|
*/ |
78
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
79
|
|
|
if ($event->getApp() !== 'deck') { |
80
|
|
|
throw new \InvalidArgumentException(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$event = $this->getIcon($event); |
84
|
|
|
|
85
|
|
|
$subjectIdentifier = $event->getSubject(); |
86
|
|
|
$subjectParams = $event->getSubjectParameters(); |
87
|
|
|
$ownActivity = ($event->getAuthor() === $this->userId); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Map stored parameter objects to rich string types |
91
|
|
|
*/ |
92
|
|
|
|
93
|
|
|
$author = $event->getAuthor(); |
94
|
|
|
// get author if |
95
|
|
|
if (($author === '' || $author === ActivityManager::DECK_NOAUTHOR_COMMENT_SYSTEM_ENFORCED) && array_key_exists('author', $subjectParams)) { |
96
|
|
|
$author = $subjectParams['author']; |
97
|
|
|
unset($subjectParams['author']); |
98
|
|
|
} |
99
|
|
|
$user = $this->userManager->get($author); |
100
|
|
|
if ($user !== null) { |
101
|
|
|
$params = [ |
102
|
|
|
'user' => [ |
103
|
|
|
'type' => 'user', |
104
|
|
|
'id' => $author, |
105
|
|
|
'name' => $user !== null ? $user->getDisplayName() : $author |
106
|
|
|
], |
107
|
|
|
]; |
108
|
|
|
$event->setAuthor($author); |
109
|
|
|
} |
110
|
|
|
if ($event->getObjectType() === ActivityManager::DECK_OBJECT_BOARD) { |
111
|
|
View Code Duplication |
if (isset($subjectParams['board']) && $event->getObjectName() === '') { |
|
|
|
|
112
|
|
|
$event->setObject($event->getObjectType(), $event->getObjectId(), $subjectParams['board']['title']); |
113
|
|
|
} |
114
|
|
|
$board = [ |
115
|
|
|
'type' => 'highlight', |
116
|
|
|
'id' => $event->getObjectId(), |
117
|
|
|
'name' => $event->getObjectName(), |
118
|
|
|
'link' => $this->deckUrl('/board/' . $event->getObjectId()), |
119
|
|
|
]; |
120
|
|
|
$params['board'] = $board; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (isset($subjectParams['card']) && $event->getObjectType() === ActivityManager::DECK_OBJECT_CARD) { |
124
|
|
View Code Duplication |
if ($event->getObjectName() === '') { |
|
|
|
|
125
|
|
|
$event->setObject($event->getObjectType(), $event->getObjectId(), $subjectParams['card']['title']); |
126
|
|
|
} |
127
|
|
|
$card = [ |
128
|
|
|
'type' => 'highlight', |
129
|
|
|
'id' => $event->getObjectId(), |
130
|
|
|
'name' => $event->getObjectName(), |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
if (array_key_exists('board', $subjectParams)) { |
134
|
|
|
$archivedParam = $subjectParams['card']['archived'] ? 'archived' : ''; |
135
|
|
|
$card['link'] = $this->deckUrl('/board/' . $subjectParams['board']['id'] . '/' . $archivedParam . '/card/' . $event->getObjectId()); |
136
|
|
|
} |
137
|
|
|
$params['card'] = $card; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$params = $this->parseParamForBoard('board', $subjectParams, $params); |
141
|
|
|
$params = $this->parseParamForStack('stack', $subjectParams, $params); |
142
|
|
|
$params = $this->parseParamForStack('stackBefore', $subjectParams, $params); |
143
|
|
|
$params = $this->parseParamForAttachment('attachment', $subjectParams, $params); |
144
|
|
|
$params = $this->parseParamForLabel($subjectParams, $params); |
145
|
|
|
$params = $this->parseParamForAssignedUser($subjectParams, $params); |
146
|
|
|
$params = $this->parseParamForAcl($subjectParams, $params); |
147
|
|
|
$params = $this->parseParamForChanges($subjectParams, $params, $event); |
148
|
|
|
$params = $this->parseParamForComment($subjectParams, $params, $event); |
149
|
|
|
$params = $this->parseParamForDuedate($subjectParams, $params, $event); |
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
$subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity); |
153
|
|
|
$this->setSubjects($event, $subject, $params); |
154
|
|
|
} catch (\Exception $e) { |
|
|
|
|
155
|
|
|
} |
156
|
|
|
return $event; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param IEvent $event |
161
|
|
|
* @param string $subject |
162
|
|
|
* @param array $parameters |
163
|
|
|
*/ |
164
|
|
|
protected function setSubjects(IEvent $event, $subject, array $parameters) { |
165
|
|
|
$placeholders = $replacements = $richParameters = []; |
166
|
|
|
foreach ($parameters as $placeholder => $parameter) { |
167
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
168
|
|
|
if (is_array($parameter) && array_key_exists('name', $parameter)) { |
169
|
|
|
$replacements[] = $parameter['name']; |
170
|
|
|
$richParameters[$placeholder] = $parameter; |
171
|
|
|
} else { |
172
|
|
|
$replacements[] = ''; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
177
|
|
|
->setRichSubject($subject, $richParameters); |
178
|
|
|
$event->setSubject($subject, $parameters); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
private function getIcon(IEvent $event) { |
182
|
|
|
$event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg')); |
183
|
|
View Code Duplication |
if (strpos($event->getSubject(), '_update') !== false) { |
|
|
|
|
184
|
|
|
$event->setIcon($this->urlGenerator->imagePath('files', 'change.svg')); |
185
|
|
|
} |
186
|
|
View Code Duplication |
if (strpos($event->getSubject(), '_create') !== false) { |
|
|
|
|
187
|
|
|
$event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg')); |
188
|
|
|
} |
189
|
|
View Code Duplication |
if (strpos($event->getSubject(), '_delete') !== false) { |
|
|
|
|
190
|
|
|
$event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg')); |
191
|
|
|
} |
192
|
|
View Code Duplication |
if (strpos($event->getSubject(), 'archive') !== false) { |
|
|
|
|
193
|
|
|
$event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg')); |
194
|
|
|
} |
195
|
|
View Code Duplication |
if (strpos($event->getSubject(), '_restore') !== false) { |
|
|
|
|
196
|
|
|
$event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg')); |
197
|
|
|
} |
198
|
|
View Code Duplication |
if (strpos($event->getSubject(), 'attachment_') !== false) { |
|
|
|
|
199
|
|
|
$event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg')); |
200
|
|
|
} |
201
|
|
View Code Duplication |
if (strpos($event->getSubject(), 'comment_') !== false) { |
|
|
|
|
202
|
|
|
$event->setIcon($this->urlGenerator->imagePath('core', 'actions/comment.svg')); |
203
|
|
|
} |
204
|
|
|
if (strpos($event->getSubject(), 'label_') !== false) { |
205
|
|
|
$event->setIcon($this->urlGenerator->imagePath('core', 'actions/tag.svg')); |
206
|
|
|
} |
207
|
|
|
return $event; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
private function parseParamForBoard($paramName, $subjectParams, $params) { |
211
|
|
|
if (array_key_exists($paramName, $subjectParams)) { |
212
|
|
|
$params[$paramName] = [ |
213
|
|
|
'type' => 'highlight', |
214
|
|
|
'id' => $subjectParams[$paramName]['id'], |
215
|
|
|
'name' => $subjectParams[$paramName]['title'], |
216
|
|
|
'link' => $this->deckUrl('/board/' . $subjectParams[$paramName]['id'] . '/'), |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
return $params; |
220
|
|
|
} |
221
|
|
|
private function parseParamForStack($paramName, $subjectParams, $params) { |
222
|
|
|
if (array_key_exists($paramName, $subjectParams)) { |
223
|
|
|
$params[$paramName] = [ |
224
|
|
|
'type' => 'highlight', |
225
|
|
|
'id' => $subjectParams[$paramName]['id'], |
226
|
|
|
'name' => $subjectParams[$paramName]['title'], |
227
|
|
|
]; |
228
|
|
|
} |
229
|
|
|
return $params; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
private function parseParamForAttachment($paramName, $subjectParams, $params) { |
233
|
|
|
if (array_key_exists($paramName, $subjectParams)) { |
234
|
|
|
$params[$paramName] = [ |
235
|
|
|
'type' => 'highlight', |
236
|
|
|
'id' => $subjectParams[$paramName]['id'], |
237
|
|
|
'name' => $subjectParams[$paramName]['data'], |
238
|
|
|
'link' => $this->urlGenerator->linkToRoute('deck.attachment.display', ['cardId' => $subjectParams['card']['id'], 'attachmentId' => $subjectParams['attachment']['id']]), |
239
|
|
|
]; |
240
|
|
|
} |
241
|
|
|
return $params; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
private function parseParamForAssignedUser($subjectParams, $params) { |
245
|
|
|
if (array_key_exists('assigneduser', $subjectParams)) { |
246
|
|
|
$user = $this->userManager->get($subjectParams['assigneduser']); |
247
|
|
|
$params['assigneduser'] = [ |
248
|
|
|
'type' => 'user', |
249
|
|
|
'id' => $subjectParams['assigneduser'], |
250
|
|
|
'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser'] |
251
|
|
|
]; |
252
|
|
|
} |
253
|
|
|
return $params; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function parseParamForLabel($subjectParams, $params) { |
257
|
|
|
if (array_key_exists('label', $subjectParams)) { |
258
|
|
|
$params['label'] = [ |
259
|
|
|
'type' => 'highlight', |
260
|
|
|
'id' => $subjectParams['label']['id'], |
261
|
|
|
'name' => $subjectParams['label']['title'] |
262
|
|
|
]; |
263
|
|
|
} |
264
|
|
|
return $params; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
private function parseParamForAcl($subjectParams, $params) { |
268
|
|
|
if (array_key_exists('acl', $subjectParams)) { |
269
|
|
|
if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) { |
270
|
|
|
$user = $this->userManager->get($subjectParams['acl']['participant']); |
271
|
|
|
$params['acl'] = [ |
272
|
|
|
'type' => 'user', |
273
|
|
|
'id' => $subjectParams['acl']['participant'], |
274
|
|
|
'name' => $user !== null ? $user->getDisplayName() : $subjectParams['acl']['participant'] |
275
|
|
|
]; |
276
|
|
|
} else { |
277
|
|
|
$params['acl'] = [ |
278
|
|
|
'type' => 'highlight', |
279
|
|
|
'id' => $subjectParams['acl']['participant'], |
280
|
|
|
'name' => $subjectParams['acl']['participant'] |
281
|
|
|
]; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
return $params; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
private function parseParamForComment($subjectParams, $params, IEvent $event) { |
288
|
|
|
if (array_key_exists('comment', $subjectParams)) { |
289
|
|
|
/** @var IComment $comment */ |
290
|
|
|
try { |
291
|
|
|
$comment = $this->commentsManager->get((int)$subjectParams['comment']); |
292
|
|
|
$event->setParsedMessage($comment->getMessage()); |
293
|
|
|
$params['comment'] =[ |
294
|
|
|
'type' => 'highlight', |
295
|
|
|
'id' => $subjectParams['comment'], |
296
|
|
|
'name' => $comment->getMessage() |
297
|
|
|
]; |
298
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
return $params; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
private function parseParamForDuedate($subjectParams, $params, IEvent $event) { |
305
|
|
|
if (array_key_exists('after', $subjectParams) && $event->getSubject() === ActivityManager::SUBJECT_CARD_UPDATE_DUEDATE) { |
306
|
|
|
$userLanguage = $this->config->getUserValue($event->getAuthor(), 'core', 'lang', $this->l10nFactory->findLanguage()); |
307
|
|
|
$userLocale = $this->config->getUserValue($event->getAuthor(), 'core', 'locale', $this->l10nFactory->findLocale()); |
308
|
|
|
$l10n = $this->l10nFactory->get('deck', $userLanguage, $userLocale); |
309
|
|
|
$date = new \DateTime($subjectParams['after']); |
310
|
|
|
$date->setTimezone(new \DateTimeZone(\date_default_timezone_get())); |
311
|
|
|
$params['after'] = [ |
312
|
|
|
'type' => 'highlight', |
313
|
|
|
'id' => 'dt:' . $subjectParams['after'], |
314
|
|
|
'name' => $l10n->l('datetime', $date) |
315
|
|
|
]; |
316
|
|
|
} |
317
|
|
|
return $params; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Add diff to message if the subject parameter 'diff' is set, otherwise |
322
|
|
|
* the changed values are added to before/after |
323
|
|
|
* |
324
|
|
|
* @param $subjectParams |
325
|
|
|
* @param $params |
326
|
|
|
* @return mixed |
327
|
|
|
*/ |
328
|
|
|
private function parseParamForChanges($subjectParams, $params, $event) { |
329
|
|
|
if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) { |
330
|
|
|
$diff = new Diff(); |
331
|
|
|
// Don't add diff as message since we are limited to 255 chars here |
332
|
|
|
//$event->setMessage($subjectParams['after']); |
|
|
|
|
333
|
|
|
$event->setParsedMessage('<pre class="visualdiff">' . $diff->render($subjectParams['before'], $subjectParams['after']) . '</pre>'); |
334
|
|
|
return $params; |
335
|
|
|
} |
336
|
|
|
if (array_key_exists('before', $subjectParams)) { |
337
|
|
|
$params['before'] = [ |
338
|
|
|
'type' => 'highlight', |
339
|
|
|
'id' => $subjectParams['before'], |
340
|
|
|
'name' => $subjectParams['before'] |
341
|
|
|
]; |
342
|
|
|
} |
343
|
|
View Code Duplication |
if (array_key_exists('after', $subjectParams)) { |
|
|
|
|
344
|
|
|
$params['after'] = [ |
345
|
|
|
'type' => 'highlight', |
346
|
|
|
'id' => $subjectParams['after'], |
347
|
|
|
'name' => $subjectParams['after'] |
348
|
|
|
]; |
349
|
|
|
} |
350
|
|
View Code Duplication |
if (array_key_exists('card', $subjectParams) && $event->getSubject() === ActivityManager::SUBJECT_CARD_UPDATE_TITLE) { |
|
|
|
|
351
|
|
|
$params['card'] = [ |
352
|
|
|
'type' => 'highlight', |
353
|
|
|
'id' => $subjectParams['after'], |
354
|
|
|
'name' => $subjectParams['after'] |
355
|
|
|
]; |
356
|
|
|
} |
357
|
|
|
return $params; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function deckUrl($endpoint) { |
361
|
|
|
return $this->urlGenerator->linkToRouteAbsolute('deck.page.index') . '#!' . $endpoint; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
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.