Passed
Pull Request — master (#632)
by Julius
04:14
created

DeckProvider::parseParamForAttachment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Activity;
25
26
27
use cogpowered\FineDiff\Diff;
28
use OCA\Deck\Db\Acl;
29
use OCP\Activity\IEvent;
30
use OCP\Activity\IProvider;
31
use OCP\Comments\IComment;
32
use OCP\Comments\ICommentsManager;
33
use OCP\Comments\NotFoundException;
34
use OCP\IURLGenerator;
35
use OCP\IUserManager;
36
37
class DeckProvider implements IProvider {
38
39
	/** @var string */
40
	private $userId;
41
	/** @var IURLGenerator */
42
	private $urlGenerator;
43
	/** @var ActivityManager */
44
	private $activityManager;
45
	/** @var IUserManager */
46
	private $userManager;
47
	/** @var ICommentsManager */
48
	private $commentsManager;
49
50
	public function __construct(IURLGenerator $urlGenerator, ActivityManager $activityManager, IUserManager $userManager, ICommentsManager $commentsManager, $userId) {
51
		$this->userId = $userId;
52
		$this->urlGenerator = $urlGenerator;
53
		$this->activityManager = $activityManager;
54
		$this->commentsManager = $commentsManager;
55
		$this->userManager = $userManager;
56
	}
57
58
	/**
59
	 * @param string $language The language which should be used for translating, e.g. "en"
60
	 * @param IEvent $event The current event which should be parsed
61
	 * @param IEvent|null $previousEvent A potential previous event which you can combine with the current one.
62
	 *                                   To do so, simply use setChildEvent($previousEvent) after setting the
63
	 *                                   combined subject on the current event.
64
	 * @return IEvent
65
	 * @throws \InvalidArgumentException Should be thrown if your provider does not know this event
66
	 * @since 11.0.0
67
	 */
68
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
69
		if ($event->getApp() !== 'deck') {
70
			throw new \InvalidArgumentException();
71
		}
72
73
		$event = $this->getIcon($event);
74
75
		$subjectIdentifier = $event->getSubject();
76
		$subjectParams = $event->getSubjectParameters();
77
		$ownActivity = ($event->getAuthor() === $this->userId);
78
79
		/**
80
		 * Map stored parameter objects to rich string types
81
		 */
82
		$board = null;
83
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_BOARD) {
84
			$board = [
85
				'type' => 'highlight',
86
				'id' => $event->getObjectId(),
87
				'name' => $event->getObjectName(),
88
				'link' => $this->deckUrl('/board/' . $event->getObjectId()),
89
			];
90
		}
91
92
		$card = null;
93
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_CARD) {
94
			$card = [
95
				'type' => 'highlight',
96
				'id' => $event->getObjectId(),
97
				'name' => $event->getObjectName(),
98
			];
99
100
			if (array_key_exists('board', $subjectParams)) {
101
				$archivedParam = $subjectParams['card']['archived'] ? 'archived' : '';
102
				$card['link'] = $this->deckUrl('/board/' . $subjectParams['board']['id'] . '/' . $archivedParam . '/card/' . $event->getObjectId());
103
			}
104
		}
105
106
		$author = $event->getAuthor();
107
		$user = $this->userManager->get($author);
108
		$params = [
109
			'board' => $board,
110
			'card' => $card,
111
			'user' => [
112
				'type' => 'user',
113
				'id' => $author,
114
				'name' => $user !== null ? $user->getDisplayName() : $author
115
			],
116
		];
117
118
		$params = $this->parseParamForBoard('board', $subjectParams, $params);
119
		$params = $this->parseParamForStack('stack', $subjectParams, $params);
120
		$params = $this->parseParamForStack('stackBefore', $subjectParams, $params);
121
		$params = $this->parseParamForAttachment('attachment', $subjectParams, $params);
122
		$params = $this->parseParamForLabel($subjectParams, $params);
123
		$params = $this->parseParamForAssignedUser($subjectParams, $params);
124
		$params = $this->parseParamForAcl($subjectParams, $params);
125
		$params = $this->parseParamForChanges($subjectParams, $params, $event);
126
		$params = $this->parseParamForComment($subjectParams, $params, $event);
127
128
		try {
129
			$subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity);
130
			$event->setParsedSubject($subject);
131
			$event->setRichSubject($subject, $params);
132
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
133
		}
134
		return $event;
135
	}
136
137
	private function getIcon(IEvent $event) {
138
		$event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg'));
139 View Code Duplication
		if (strpos($event->getSubject(), '_update') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
140
			$event->setIcon($this->urlGenerator->imagePath('files', 'change.svg'));
141
		}
142 View Code Duplication
		if (strpos($event->getSubject(), '_create') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
143
			$event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg'));
144
		}
145 View Code Duplication
		if (strpos($event->getSubject(), '_delete') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
146
			$event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg'));
147
		}
148 View Code Duplication
		if (strpos($event->getSubject(), 'archive') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
149
			$event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg'));
150
		}
151 View Code Duplication
		if (strpos($event->getSubject(), '_restore') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
152
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg'));
153
		}
154 View Code Duplication
		if (strpos($event->getSubject(), 'attachment_') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
155
			$event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg'));
156
		}
157
		if (strpos($event->getSubject(), 'comment_') !== false) {
158
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/comment.svg'));
159
		}
160
		return $event;
161
	}
162
163
	private function parseParamForBoard($paramName, $subjectParams, $params) {
164
		if (array_key_exists($paramName, $subjectParams)) {
165
			$params[$paramName] = [
166
				'type' => 'highlight',
167
				'id' => $subjectParams[$paramName]['id'],
168
				'name' => $subjectParams[$paramName]['title'],
169
				'link' => $this->deckUrl('/board/' . $subjectParams[$paramName]['id'] . '/'),
170
			];
171
		}
172
		return $params;
173
	}
174
	private function parseParamForStack($paramName, $subjectParams, $params) {
175
		if (array_key_exists($paramName, $subjectParams)) {
176
			$params[$paramName] = [
177
				'type' => 'highlight',
178
				'id' => $subjectParams[$paramName]['id'],
179
				'name' => $subjectParams[$paramName]['title'],
180
			];
181
		}
182
		return $params;
183
	}
184
185
	private function parseParamForAttachment($paramName, $subjectParams, $params) {
186
		if (array_key_exists($paramName, $subjectParams)) {
187
			$params[$paramName] = [
188
				'type' => 'highlight',
189
				'id' => $subjectParams[$paramName]['id'],
190
				'name' => $subjectParams[$paramName]['data'],
191
				'link' => $this->urlGenerator->linkToRoute('deck.attachment.display', ['cardId' => $subjectParams['card']['id'], 'attachmentId' => $subjectParams['attachment']['id']]),
192
			];
193
		}
194
		return $params;
195
	}
196
197
	private function parseParamForAssignedUser($subjectParams, $params) {
198
		if (array_key_exists('assigneduser', $subjectParams)) {
199
			$user = $this->userManager->get($subjectParams['assigneduser']);
200
			$params['assigneduser'] = [
201
				'type' => 'user',
202
				'id' => $subjectParams['assigneduser'],
203
				'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser']
204
			];
205
		}
206
		return $params;
207
	}
208
209
	private function parseParamForLabel($subjectParams, $params) {
210 View Code Duplication
		if (array_key_exists('label', $subjectParams)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
211
			$params['label'] = [
212
				'type' => 'highlight',
213
				'id' => $subjectParams['label']['id'],
214
				'name' => $subjectParams['label']['title']
215
			];
216
		}
217
		return $params;
218
	}
219
220
	private function parseParamForAcl($subjectParams, $params) {
221
		if (array_key_exists('acl', $subjectParams)) {
222
			if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) {
223
				$user = $this->userManager->get($subjectParams['acl']['participant']);
224
				$params['acl'] = [
225
					'type' => 'user',
226
					'id' => $subjectParams['acl']['participant'],
227
					'name' => $user !== null ? $user->getDisplayName() : $subjectParams['acl']['participant']
228
				];
229 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
230
				$params['acl'] = [
231
					'type' => 'highlight',
232
					'id' => $subjectParams['acl']['participant'],
233
					'name' => $subjectParams['acl']['participant']
234
				];
235
			}
236
		}
237
		return $params;
238
	}
239
240
	private function parseParamForComment($subjectParams, $params, IEvent $event) {
241
		if (array_key_exists('comment', $subjectParams)) {
242
			/** @var IComment $comment */
243
			try {
244
				$comment = $this->commentsManager->get((int)$subjectParams['comment']);
245
				$event->setParsedMessage($comment->getMessage());
246
			} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
247
			}
248
			$params['comment'] = $subjectParams['comment'];
249
		}
250
		return $params;
251
	}
252
253
	/**
254
	 * Add diff to message if the subject parameter 'diff' is set, otherwise
255
	 * the changed values are added to before/after
256
	 *
257
	 * @param $subjectParams
258
	 * @param $params
259
	 * @return mixed
260
	 */
261
	private function parseParamForChanges($subjectParams, $params, $event) {
262
		if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) {
263
			$diff = new Diff();
264
			$event->setMessage($subjectParams['after']);
265
			$event->setParsedMessage('<pre class="visualdiff">' . $diff->render($subjectParams['before'], $subjectParams['after']) . '</pre>');
266
			return $params;
267
		}
268 View Code Duplication
		if (array_key_exists('before', $subjectParams)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
269
			$params['before'] = [
270
				'type' => 'highlight',
271
				'id' => $subjectParams['before'],
272
				'name' => $subjectParams['before']
273
			];
274
		}
275 View Code Duplication
		if (array_key_exists('after', $subjectParams)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
276
			$params['after'] = [
277
				'type' => 'highlight',
278
				'id' => $subjectParams['after'],
279
				'name' => $subjectParams['after']
280
			];
281
		}
282
		return $params;
283
	}
284
285
	public function deckUrl($endpoint) {
286
		return $this->urlGenerator->linkToRoute('deck.page.index') . '#!' . $endpoint;
287
	}
288
}
289