Completed
Push — master ( e5f032...060fe4 )
by Maxence
02:52
created

Provider::parseAsModerator()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 13
nc 11
nop 3
1
<?php
2
3
4
namespace OCA\Circles\Activity;
5
6
7
use Exception;
8
use InvalidArgumentException;
9
use OCA\Circles\Api\v1\Circles;
10
use OCA\Circles\Model\Circle;
11
use OCA\Circles\Model\FederatedLink;
12
use OCA\Circles\Model\Member;
13
use OCA\Circles\Service\CirclesService;
14
use OCA\Circles\Service\MiscService;
15
use OCP\Activity\IEvent;
16
use OCP\Activity\IManager;
17
use OCP\Activity\IProvider;
18
use OCP\IL10N;
19
use OCP\IURLGenerator;
20
use OpenCloud\Common\Exceptions\InvalidArgumentError;
21
22
23
class Provider implements IProvider {
24
25
	/** @var MiscService */
26
	protected $miscService;
27
28
	/** @var IL10N */
29
	protected $l10n;
30
31
	/** @var IURLGenerator */
32
	protected $url;
33
34
	/** @var IManager */
35
	protected $activityManager;
36
37
	public function __construct(
38
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
39
	) {
40
		$this->url = $url;
41
		$this->activityManager = $activityManager;
42
		$this->l10n = $l10n;
43
		$this->miscService = $miscService;
44
	}
45
46
47
	/**
48
	 * @param string $lang
49
	 * @param IEvent $event
50
	 * @param IEvent|null $previousEvent
51
	 *
52
	 * @return IEvent
53
	 */
54
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
55
56
		if ($event->getApp() !== 'circles') {
57
			throw new \InvalidArgumentException();
58
		}
59
60
		try {
61
62
			$params = $event->getSubjectParameters();
63
			$circle = Circle::fromJSON($this->l10n, $params['circle']);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Circle::fromJSON() has been deprecated.

This method has been deprecated.

Loading history...
64
65
			$this->setIcon($event, $circle);
66
			$this->parseAsMember($event, $circle, $params);
67
			$this->parseAsModerator($event, $circle, $params);
68
			$this->generateParsedSubject($event);
69
70
			return $event;
71
		} catch (\Exception $e) {
72
			throw new \InvalidArgumentException();
73
		}
74
	}
75
76
77
	private function setIcon(IEvent &$event, Circle $circle) {
78
		$event->setIcon(
79
			CirclesService::getCircleIcon(
80
				$circle->getType(),
81
				(method_exists($this->activityManager, 'getRequirePNG')
82
				 && $this->activityManager->getRequirePNG())
83
			)
84
		);
85
	}
86
87
	/**
88
	 * @param Circle $circle
89
	 * @param IEvent $event
90
	 * @param array $params
91
	 *
92
	 * @return IEvent
93
	 */
94
	private function parseAsMember(IEvent &$event, Circle $circle, $params) {
95
		if ($event->getType() !== 'circles_as_member') {
96
			return $event;
97
		}
98
99
		switch ($event->getSubject()) {
100
			case 'circle_create':
101
				return $this->parseCircleEvent(
102
					$event, $circle, null,
103
					$this->l10n->t('You created the circle {circle}'),
104
					$this->l10n->t('{author} created the circle {circle}')
105
				);
106
107
			case 'circle_delete':
108
				return $this->parseCircleEvent(
109
					$event, $circle, null,
110
					$this->l10n->t('You deleted {circle}'),
111
					$this->l10n->t('{author} deleted {circle}')
112
				);
113
		}
114
115
		if (key_exists('member', $params)) {
116
			$this->parseMemberAsMember($event, $circle);
117
		}
118
119
		return $event;
120
	}
121
122
123
	/**
124
	 * @param Circle $circle
125
	 * @param IEvent $event
126
	 *
127
	 * @return IEvent
128
	 */
129
	private function parseMemberAsMember(IEvent &$event, Circle $circle) {
130
		$params = $event->getSubjectParameters();
131
		$member = Member::fromJSON2($params['member']);
132
133
		switch ($event->getSubject()) {
134
			case 'member_join':
135
				return $this->parseSubjectMemberJoin($event, $circle, $member);
136
137
			case 'member_add':
138
				return $this->parseSubjectMemberAdd($event, $circle, $member);
139
140
			case 'member_left':
141
				return $this->parseSubjectMemberLeft($event, $circle, $member);
142
143
			case 'member_remove':
144
				return $this->parseSubjectMemberRemove($event, $circle, $member);
145
		}
146
147
		return $event;
148
	}
149
150
151
	/**
152
	 * Parse on Subject 'member_join'.
153
	 * If circle is closed, we say that user accepted his invitation.
154
	 *
155
	 * @param IEvent $event
156
	 * @param Circle $circle
157
	 * @param Member $member
158
	 *
159
	 * @return IEvent
160
	 */
161 View Code Duplication
	private function parseSubjectMemberJoin(IEvent &$event, Circle $circle, Member $member) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
162
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
163
			return $this->parseCircleMemberEvent(
164
				$event, $circle, $member,
165
				$this->l10n->t('You accepted the invitation to join {circle}'),
166
				$this->l10n->t('{member} accepted the invitation to join {circle}')
167
			);
168
		} else {
169
			return $this->parseCircleMemberEvent(
170
				$event, $circle, $member,
171
				$this->l10n->t('You joined {circle}'),
172
				$this->l10n->t('{member} joined {circle}')
173
			);
174
		}
175
	}
176
177
178
	/**
179
	 * Parse on Subject 'member_add'.
180
	 * If circle is closed, we say that user's invitation was accepted.
181
	 *
182
	 * @param IEvent $event
183
	 * @param Circle $circle
184
	 * @param Member $member
185
	 *
186
	 * @return IEvent
187
	 */
188 View Code Duplication
	private function parseSubjectMemberAdd(IEvent &$event, Circle $circle, Member $member) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
189
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
190
			return $this->parseCircleMemberAdvancedEvent(
191
				$event, $circle, $member,
192
				$this->l10n->t("You accepted {member}'s request to join {circle}"),
193
				$this->l10n->t('Your request to join {circle} has been accepted by {author}'),
194
				$this->l10n->t("{member}'s request to join {circle} has been accepted by {author}")
195
			);
196
		} else {
197
			return $this->parseCircleMemberAdvancedEvent(
198
				$event, $circle, $member,
199
				$this->l10n->t('You added {member} as member to {circle}'),
200
				$this->l10n->t('You have been added as member to {circle} by {author}'),
201
				$this->l10n->t('{member} has been added as member to {circle} by {author}')
202
			);
203
		}
204
	}
205
206
207
	/**
208
	 * Parse on Subject 'member_left'.
209
	 * If circle is closed and member was not a real member, we say that member rejected his
210
	 * invitation.
211
	 *
212
	 * @param IEvent $event
213
	 * @param Circle $circle
214
	 * @param Member $member
215
	 *
216
	 * @return IEvent
217
	 */
218 View Code Duplication
	private function parseSubjectMemberLeft(IEvent &$event, Circle $circle, Member $member) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
219
		if ($circle->getType() === Circle::CIRCLES_CLOSED
220
			&& $member->getLevel() === Member::LEVEL_NONE) {
221
			return $this->parseCircleMemberEvent(
222
				$event, $circle, $member,
223
				$this->l10n->t("You rejected the invitation to join {circle}"),
224
				$this->l10n->t("{member} rejected the invitation to join {circle}")
225
			);
226
		} else {
227
			return $this->parseCircleMemberEvent(
228
				$event, $circle, $member,
229
				$this->l10n->t('You left {circle}'),
230
				$this->l10n->t('{member} left {circle}')
231
			);
232
		}
233
	}
234
235
236
	/**
237
	 * Parse on Subject 'member_remove'.
238
	 * If circle is closed and member was not a real member, we say that his invitation was
239
	 * rejected.
240
	 *
241
	 * @param IEvent $event
242
	 * @param Circle $circle
243
	 * @param Member $member
244
	 *
245
	 * @return IEvent
246
	 */
247 View Code Duplication
	private function parseSubjectMemberRemove(IEvent &$event, Circle $circle, Member $member) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
248
		if ($circle->getType() === Circle::CIRCLES_CLOSED
249
			&& $member->getLevel() === Member::LEVEL_NONE) {
250
			return $this->parseCircleMemberAdvancedEvent(
251
				$event, $circle, $member,
252
				$this->l10n->t("You dismissed {member}'s request to join {circle}"),
253
				$this->l10n->t('Your request to join {circle} has been dismissed by {author}'),
254
				$this->l10n->t("{member}'s request to join {circle} has been dismissed by {author}")
255
			);
256
		} else {
257
			return $this->parseCircleMemberAdvancedEvent(
258
				$event, $circle, $member,
259
				$this->l10n->t('You removed {member} from {circle}'),
260
				$this->l10n->t('You have been removed from {circle} by {author}'),
261
				$this->l10n->t('{member} has been removed from {circle} by {author}')
262
			);
263
		}
264
	}
265
266
267
	/**
268
	 * @param Circle $circle
269
	 * @param IEvent $event
270
	 * @param array $params
271
	 *
272
	 * @return IEvent
273
	 * @throws Exception
274
	 */
275
	private function parseAsModerator(IEvent &$event, Circle $circle, $params) {
276
		if ($event->getType() !== 'circles_as_moderator') {
277
			return $event;
278
		}
279
280
		try {
281
			if (key_exists('member', $params)) {
282
				return $this->parseMemberAsModerator($event, $circle);
283
			}
284
285
			if (key_exists('group', $params)) {
286
				return $this->parseGroupAsModerator($event, $circle);
287
			}
288
289
			if (key_exists('link', $params)) {
290
				return $this->parseLinkAsModerator($event, $circle);
291
			}
292
293
			throw new InvalidArgumentError();
294
		} catch (Exception $e) {
295
			throw $e;
296
		}
297
	}
298
299
300
	/**
301
	 * @param Circle $circle
302
	 * @param IEvent $event
303
	 *
304
	 * @return IEvent
305
	 */
306
	private function parseGroupAsModerator(IEvent &$event, Circle $circle) {
307
308
		$params = $event->getSubjectParameters();
309
		$group = Member::fromJSON2($params['group']);
310
311
		switch ($event->getSubject()) {
312
313 View Code Duplication
			case 'group_link':
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...
314
				return $this->parseCircleMemberEvent(
315
					$event, $circle, $group,
316
					$this->l10n->t('You linked {group} to {circle}'),
317
					$this->l10n->t('{group} has been linked to {circle} by {author}')
318
				);
319
320 View Code Duplication
			case 'group_unlink':
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...
321
				return $this->parseCircleMemberEvent(
322
					$event, $circle, $group,
323
					$this->l10n->t('You unlinked {group} from {circle}'),
324
					$this->l10n->t('{group} has been unlinked from {circle} by {author}')
325
				);
326
327
			case 'group_level':
328
				$level = [$this->l10n->t($group->getLevelString())];
329
330
				return $this->parseCircleMemberEvent(
331
					$event, $circle, $group,
332
					$this->l10n->t(
333
						'You changed the level of the linked group {group} in {circle} to %1$s',
334
						$level
335
					),
336
					$this->l10n->t(
337
						'{author} changed the level of the linked group {group} in {circle} to %1$s',
338
						$level
339
					)
340
				);
341
		}
342
343
		throw new InvalidArgumentException();
344
	}
345
346
347
	/**
348
	 * @param Circle $circle
349
	 * @param IEvent $event
350
	 *
351
	 * @return IEvent
352
	 */
353
	private function parseMemberAsModerator(IEvent &$event, Circle $circle) {
354
355
		$params = $event->getSubjectParameters();
356
		$member = Member::fromJSON2($params['member']);
357
358
		switch ($event->getSubject()) {
359
			case 'member_invited':
360
				return $this->parseCircleMemberAdvancedEvent(
361
					$event, $circle, $member,
362
					$this->l10n->t('You invited {member} to join {circle}'),
363
					$this->l10n->t('You have been invited to join {circle} by {author}'),
364
					$this->l10n->t('{member} has been invited to join {circle} by {author}')
365
				);
366
367
			case 'member_level':
368
				$level = [$this->l10n->t($member->getLevelString())];
369
370
				return $this->parseCircleMemberAdvancedEvent(
371
					$event, $circle, $member,
372
					$this->l10n->t('You changed {member}\'s level in {circle} to %1$s', $level),
373
					$this->l10n->t('{author} changed your level in {circle} to %1$s', $level),
374
					$this->l10n->t('{author} changed {member}\'s level in {circle} to %1$s', $level)
375
				);
376
377 View Code Duplication
			case 'member_request_invitation':
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...
378
				return $this->parseMemberEvent(
379
					$event, $circle, $member,
380
					$this->l10n->t('You sent a request to join {circle}'),
381
					$this->l10n->t('{member} sent a request to join {circle}')
382
				);
383
384 View Code Duplication
			case 'member_owner':
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...
385
				return $this->parseMemberEvent(
386
					$event, $circle, $member,
387
					$this->l10n->t('You are the new owner of {circle}'),
388
					$this->l10n->t('{member} is the new owner of {circle}')
389
				);
390
		}
391
392
		throw new InvalidArgumentException();
393
	}
394
395
396
	/**
397
	 * @param Circle $circle
398
	 * @param IEvent $event
399
	 *
400
	 * @return IEvent
401
	 */
402
	private function parseLinkAsModerator(IEvent &$event, Circle $circle) {
403
404
		$params = $event->getSubjectParameters();
405
		$remote = FederatedLink::fromJSON($params['link']);
406
407
		switch ($event->getSubject()) {
408 View Code Duplication
			case 'link_request_sent':
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...
409
				return $this->parseCircleEvent(
410
					$event, $circle, $remote,
411
					$this->l10n->t('You sent a request to link {circle} with {remote}'),
412
					$this->l10n->t('{author} sent a request to link {circle} with {remote}')
413
				);
414
415
			case 'link_request_received';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
416
				return $this->parseLinkEvent(
417
					$event, $circle, $remote,
418
					$this->l10n->t('{remote} requested a link with {circle}')
419
				);
420
421
			case 'link_request_rejected';
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
422
				return $this->parseLinkEvent(
423
					$event, $circle, $remote, $this->l10n->t(
424
					'The request to link {circle} with {remote} has been rejected'
425
				)
426
				);
427
428
			case 'link_request_canceled':
429
				return $this->parseLinkEvent(
430
					$event, $circle, $remote,
431
					$this->l10n->t(
432
						'The request to link {remote} with {circle} has been canceled remotely'
433
					)
434
				);
435
436
			case 'link_request_accepted':
437
				return $this->parseLinkEvent(
438
					$event, $circle, $remote,
439
					$this->l10n->t('The request to link {circle} with {remote} has been accepted')
440
				);
441
442 View Code Duplication
			case 'link_request_removed':
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...
443
				return $this->parseCircleEvent(
444
					$event, $circle, $remote,
445
					$this->l10n->t('You dismissed the request to link {remote} with {circle}'),
446
					$this->l10n->t('{author} dismissed the request to link {remote} with {circle}')
447
				);
448
449 View Code Duplication
			case 'link_request_canceling':
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...
450
				return $this->parseCircleEvent(
451
					$event, $circle, $remote,
452
					$this->l10n->t('You canceled the request to link {circle} with {remote}'),
453
					$this->l10n->t('{author} canceled the request to link {circle} with {remote}')
454
				);
455
456 View Code Duplication
			case 'link_request_accepting':
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...
457
				return $this->parseCircleEvent(
458
					$event, $circle, $remote,
459
					$this->l10n->t('You accepted the request to link {remote} with {circle}'),
460
					$this->l10n->t('{author} accepted the request to link {remote} with {circle}')
461
				);
462
463
			case 'link_up':
464
				return $this->parseLinkEvent(
465
					$event, $circle, $remote,
466
					$this->l10n->t('A link between {circle} and {remote} is now up and running')
467
				);
468
469
			case 'link_down':
470
				return $this->parseLinkEvent(
471
					$event, $circle, $remote,
472
					$this->l10n->t(
473
						'The link between {circle} and {remote} has been shutdown remotely'
474
					)
475
				);
476
477 View Code Duplication
			case 'link_remove':
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...
478
				return $this->parseCircleEvent(
479
					$event, $circle, $remote,
480
					$this->l10n->t('You closed the link between {circle} and {remote}'),
481
					$this->l10n->t('{author} closed the link between {circle} and {remote}')
482
				);
483
		}
484
485
		throw new InvalidArgumentException();
486
	}
487
488
489
	/**
490
	 * general function to generate Circle event.
491
	 *
492
	 * @param IEvent $event
493
	 * @param Circle $circle
494
	 * @param FederatedLink $remote
495
	 * @param string $ownEvent
496
	 * @param string $othersEvent
497
	 *
498
	 * @return IEvent
499
	 */
500
	private function parseCircleEvent(
501
		IEvent &$event, Circle $circle, $remote, $ownEvent, $othersEvent
502
	) {
503
		$data = [
504
			'author' => $this->generateViewerParameter($circle),
505
			'circle' => $this->generateCircleParameter($circle),
506
			'remote' => ($remote === null) ? '' : $this->generateRemoteCircleParameter($remote)
507
		];
508
509
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
510
			return $event->setRichSubject($ownEvent, $data);
511
		}
512
513
		return $event->setRichSubject($othersEvent, $data);
514
	}
515
516
517
	/**
518
	 * general function to generate Member event.
519
	 *
520
	 * @param Circle $circle
521
	 * @param $member
522
	 * @param IEvent $event
523
	 * @param $ownEvent
524
	 * @param $othersEvent
525
	 *
526
	 * @return IEvent
527
	 */
528
	private function parseMemberEvent(
529
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
530
	) {
531
		$data = [
532
			'circle' => $this->generateCircleParameter($circle),
533
			'member' => $this->generateUserParameter($member)
534
		];
535
536
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
537
		) {
538
			return $event->setRichSubject($ownEvent, $data);
539
		}
540
541
		return $event->setRichSubject($othersEvent, $data);
542
	}
543
544
545
	/**
546
	 * general function to generate Link event.
547
	 *
548
	 * @param Circle $circle
549
	 * @param FederatedLink $remote
550
	 * @param IEvent $event
551
	 * @param string $line
552
	 *
553
	 * @return IEvent
554
	 */
555
	private function parseLinkEvent(IEvent &$event, Circle $circle, FederatedLink $remote, $line) {
556
		$data = [
557
			'circle' => $this->generateCircleParameter($circle),
558
			'remote' => $this->generateRemoteCircleParameter($remote)
559
		];
560
561
		return $event->setRichSubject($line, $data);
562
	}
563
564
565
	/**
566
	 * general function to generate Circle+Member event.
567
	 *
568
	 * @param Circle $circle
569
	 * @param Member $member
570
	 * @param IEvent $event
571
	 * @param string $ownEvent
572
	 * @param string $othersEvent
573
	 *
574
	 * @return IEvent
575
	 */
576
	private function parseCircleMemberEvent(
577
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $othersEvent
578
	) {
579
		$data = [
580
			'author' => $this->generateViewerParameter($circle),
581
			'circle' => $this->generateCircleParameter($circle),
582
			'member' => $this->generateUserParameter($member),
583
			'group'  => $this->generateGroupParameter($member),
584
		];
585
586
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
587
			return $event->setRichSubject($ownEvent, $data);
588
		}
589
590
		return $event->setRichSubject($othersEvent, $data);
591
	}
592
593
594
	/**
595
	 * general function to generate Circle+Member advanced event.
596
	 *
597
	 * @param Circle $circle
598
	 * @param Member $member
599
	 * @param IEvent $event
600
	 *\
601
	 * @param $ownEvent
602
	 * @param $targetEvent
603
	 * @param $othersEvent
604
	 *
605
	 * @return IEvent
606
	 */
607
	private function parseCircleMemberAdvancedEvent(
608
		IEvent &$event, Circle $circle, Member $member, $ownEvent, $targetEvent, $othersEvent
609
	) {
610
		$data = [
611
			'author' => $this->generateViewerParameter($circle),
612
			'circle' => $this->generateCircleParameter($circle),
613
			'member' => $this->generateUserParameter($member)
614
		];
615
616
		if ($this->isViewerTheAuthor($circle, $this->activityManager->getCurrentUserId())) {
617
			return $event->setRichSubject($ownEvent, $data);
618
		}
619
620
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
621
			return $event->setRichSubject($targetEvent, $data);
622
		}
623
624
		return $event->setRichSubject($othersEvent, $data);
625
	}
626
627
628
	/**
629
	 * @param IEvent $event
630
	 */
631
	private function generateParsedSubject(IEvent &$event) {
632
		$subject = $event->getRichSubject();
633
		$params = $event->getRichSubjectParameters();
634
		$ak = array_keys($params);
635
		foreach ($ak as $k) {
636
			if (is_array($params[$k])) {
637
				$subject = str_replace('{' . $k . '}', $params[$k]['parsed'], $subject);
638
			}
639
		}
640
641
		$event->setParsedSubject($subject);
642
	}
643
644
645
	/**
646
	 * @param Circle $circle
647
	 * @param string $userId
648
	 *
649
	 * @return bool
650
	 */
651
	private function isViewerTheAuthor(Circle $circle, $userId) {
652
		if ($circle->getViewer() === null) {
653
			return false;
654
		}
655
656
		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...
657
				   ->getUserId() === $userId) {
658
			return true;
659
		}
660
661
		return false;
662
	}
663
664
665
	/**
666
	 * @param Circle $circle
667
	 *
668
	 * @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...
669
	 */
670
	private function generateViewerParameter(Circle $circle) {
671
		if ($circle->getViewer() === null) {
672
			return '';
673
		}
674
675
		return $this->generateUserParameter($circle->getViewer());
676
	}
677
678
679
	/**
680
	 * @param Circle $circle
681
	 *
682
	 * @return array<string,string|integer>
683
	 */
684
	private function generateCircleParameter(Circle $circle) {
685
		return [
686
			'type'   => 'circle',
687
			'id'     => $circle->getId(),
688
			'name'   => $circle->getName(),
689
			'parsed' => $circle->getName(),
690
			'link'   => Circles::generateLink($circle->getUniqueId())
691
		];
692
	}
693
694
695
	/**
696
	 * @param FederatedLink $link
697
	 *
698
	 * @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...
699
	 */
700
	private function generateRemoteCircleParameter(FederatedLink $link) {
701
		return [
702
			'type'   => 'circle',
703
			'id'     => $link->getUniqueId(),
704
			'name'   => $link->getToken() . '@' . $link->getAddress(),
705
			'parsed' => $link->getToken() . '@' . $link->getAddress()
706
		];
707
	}
708
709
710
	/**
711
	 * @param Member $member
712
	 *
713
	 * @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...
714
	 */
715
	private function generateUserParameter(Member $member) {
716
		return [
717
			'type'   => 'user',
718
			'id'     => $member->getUserId(),
719
			'name'   => $this->miscService->getDisplayName($member->getUserId(), true),
720
			'parsed' => $this->miscService->getDisplayName($member->getUserId(), true)
721
		];
722
	}
723
724
725
	/**
726
	 * @param Member $group
727
	 *
728
	 * @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...
729
	 */
730
	private function generateGroupParameter($group) {
731
		return [
732
			'type'   => 'group',
733
			'id'     => $group->getUserId(),
734
			'name'   => $group->getUserId(),
735
			'parsed' => $group->getUserId()
736
		];
737
	}
738
}
739