Completed
Push — master ( 2813c1...14a1cb )
by Maxence
01:59
created

ProviderParser::generateRemoteCircleParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Activity;
29
30
use OCA\Circles\Api\v1\Circles;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\FederatedLink;
33
use OCA\Circles\Model\Member;
34
use OCA\Circles\Service\FederatedLinkService;
35
use OCA\Circles\Service\MiscService;
36
use OCP\Activity\IEvent;
37
use OCP\Activity\IManager;
38
use OCP\IL10N;
39
use OCP\IURLGenerator;
40
41
class ProviderParser {
42
43
44
	/** @var MiscService */
45
	protected $miscService;
46
47
	/** @var IL10N */
48
	protected $l10n;
49
50
	/** @var IURLGenerator */
51
	protected $url;
52
53
	/** @var IManager */
54
	protected $activityManager;
55
56
	public function __construct(
57
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
58
	) {
59
		$this->url = $url;
60
		$this->activityManager = $activityManager;
61
		$this->l10n = $l10n;
62
		$this->miscService = $miscService;
63
	}
64
65
66
	/**
67
	 * general function to generate Circle event.
68
	 *
69
	 * @param IEvent $event
70
	 * @param Circle $circle
71
	 * @param FederatedLink|null $remote
72
	 * @param string $ownEvent
73
	 * @param string $othersEvent
74
	 */
75
	protected function parseCircleEvent(
76
		IEvent &$event, Circle $circle, $remote, $ownEvent, $othersEvent
77
	) {
78
		$data = [
79
			'author' => $this->generateViewerParameter($circle),
80
			'circle' => $this->generateCircleParameter($circle),
81
			'remote' => ($remote === null) ? '' : $this->generateRemoteCircleParameter($remote)
82
		];
83
84
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
85
			$event->setRichSubject($ownEvent, $data);
86
87
			return;
88
		}
89
90
		$event->setRichSubject($othersEvent, $data);
91
	}
92
93
94
	/**
95
	 * general function to generate Member event.
96
	 *
97
	 * @param Circle $circle
98
	 * @param $member
99
	 * @param IEvent $event
100
	 * @param $ownEvent
101
	 * @param $othersEvent
102
	 */
103
	protected function parseMemberEvent(
104
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
105
	) {
106
		$data = [
107
			'circle' => $this->generateCircleParameter($circle),
108
			'member' => $this->generateUserParameter($member)
109
		];
110
111
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
112
		) {
113
			$event->setRichSubject($ownEvent, $data);
114
115
			return;
116
		}
117
118
		$event->setRichSubject($othersEvent, $data);
119
	}
120
121
122
	/**
123
	 * general function to generate Link event.
124
	 *
125
	 * @param Circle $circle
126
	 * @param FederatedLink $remote
127
	 * @param IEvent $event
128
	 * @param string $line
129
	 */
130
	protected function parseLinkEvent(IEvent &$event, Circle $circle, FederatedLink $remote, $line
131
	) {
132
		$data = [
133
			'circle' => $this->generateCircleParameter($circle),
134
			'remote' => $this->generateRemoteCircleParameter($remote)
135
		];
136
137
		$event->setRichSubject($line, $data);
138
	}
139
140
141
	/**
142
	 * general function to generate Circle+Member event.
143
	 *
144
	 * @param Circle $circle
145
	 * @param Member $member
146
	 * @param IEvent $event
147
	 * @param string $ownEvent
148
	 * @param string $othersEvent
149
	 */
150
	protected function parseCircleMemberEvent(
151
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
152
	) {
153
		$data = [
154
			'author'   => $this->generateViewerParameter($circle),
155
			'circle'   => $this->generateCircleParameter($circle),
156
			'member'   => $this->generateUserParameter($member),
157
			'external' => $this->generateExternalMemberParameter($member),
158
			'group'    => $this->generateGroupParameter($member),
159
		];
160
161
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
162
			$event->setRichSubject($ownEvent, $data);
163
164
			return;
165
		}
166
167
		$event->setRichSubject($othersEvent, $data);
168
	}
169
170
171
	/**
172
	 * general function to generate Circle+Member advanced event.
173
	 *
174
	 * @param Circle $circle
175
	 * @param Member $member
176
	 * @param IEvent $event
177
	 * @param $ownEvent
178
	 * @param $targetEvent
179
	 * @param $othersEvent
180
	 */
181
	protected function parseCircleMemberAdvancedEvent(
182
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $targetEvent, $othersEvent
183
	) {
184
		$data = [
185
			'author' => $this->generateViewerParameter($circle),
186
			'circle' => $this->generateCircleParameter($circle),
187
			'member' => $this->generateUserParameter($member)
188
		];
189
190
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
191
			$event->setRichSubject($ownEvent, $data);
192
193
			return;
194
		}
195
196
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
197
			$event->setRichSubject($targetEvent, $data);
198
199
			return;
200
		}
201
202
		$event->setRichSubject($othersEvent, $data);
203
	}
204
205
206
	/**
207
	 * @param Circle $circle
208
	 * @param string $userId
209
	 *
210
	 * @return bool
211
	 */
212
	protected function isViewerTheAuthor(Circle $circle, $userId) {
213
		if ($circle->getViewer() === null) {
214
			return false;
215
		}
216
217
		if ($circle->getViewer()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $circle->getViewe...etUserId() === $userId;.
Loading history...
218
				   ->getUserId() === $userId) {
219
			return true;
220
		}
221
222
		return false;
223
	}
224
225
226
	/**
227
	 * @param Circle $circle
228
	 *
229
	 * @return string|array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
230
	 */
231
	protected function generateViewerParameter(Circle $circle) {
232
		if ($circle->getViewer() === null) {
233
			return '';
234
		}
235
236
		return $this->generateUserParameter($circle->getViewer());
237
	}
238
239
240
	/**
241
	 * @param Member $member
242
	 *
243
	 * @return array|string <string,string|integer>
244
	 */
245
	protected function generateExternalMemberParameter(Member $member) {
246
		return [
247
			'type'   => 'member_' . $member->getType(),
248
			'id'     => $member->getUserId(),
249
			'name'   => $member->getDisplayName() . ' (' . $member->getTypeString() . ')',
250
			'parsed' => $member->getDisplayName()
251
		];
252
	}
253
254
255
	/**
256
	 * @param Circle $circle
257
	 *
258
	 * @return array<string,string|integer>
259
	 */
260
	protected function generateCircleParameter(Circle $circle) {
261
		return [
262
			'type'   => 'circle',
263
			'id'     => $circle->getId(),
264
			'name'   => $circle->getName(),
265
			'parsed' => $circle->getName(),
266
			'link'   => Circles::generateLink($circle->getUniqueId())
267
		];
268
	}
269
270
271
	/**
272
	 * @param FederatedLink $link
273
	 *
274
	 * @return array<string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
275
	 */
276
	protected function generateRemoteCircleParameter(FederatedLink $link) {
277
		return [
278
			'type'   => 'circle',
279
			'id'     => $link->getUniqueId(),
280
			'name'   => $link->getToken() . '@' . $link->getAddress(),
281
			'parsed' => $link->getToken() . '@' . $link->getAddress()
282
		];
283
	}
284
285
286
	/**
287
	 * @param Member $member
288
	 *
289
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
290
	 */
291
	protected function generateUserParameter(Member $member) {
292
		return [
293
			'type'   => 'user',
294
			'id'     => $member->getUserId(),
295
			'name'   => $this->miscService->getDisplayName($member->getUserId(), true),
296
			'parsed' => $this->miscService->getDisplayName($member->getUserId(), true)
297
		];
298
	}
299
300
301
	/**
302
	 * @param Member $group
303
	 *
304
	 * @return array <string,string|integer>
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
305
	 */
306
	protected function generateGroupParameter($group) {
307
		return [
308
			'type'   => 'group',
309
			'id'     => $group->getUserId(),
310
			'name'   => $group->getUserId(),
311
			'parsed' => $group->getUserId()
312
		];
313
	}
314
315
}