Completed
Push — activities ( f6cb6d...dc96a4 )
by Maxence
02:54
created

Provider::parseAsModerator()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 20
nc 7
nop 2
1
<?php
2
3
4
namespace OCA\Circles\Activity;
5
6
7
use Exception;
8
use InvalidArgumentException;
9
use OCA\Circles\Model\Circle;
10
use OCA\Circles\Model\Member;
11
use OCA\Circles\Model\SharingFrame;
12
use OCA\Circles\Service\CirclesService;
13
use OCA\Circles\Service\MiscService;
14
use OCP\Activity\IEvent;
15
use OCP\Activity\IManager;
16
use OCP\Activity\IProvider;
17
use OCP\IL10N;
18
use OCP\IURLGenerator;
19
20
21
class Provider implements IProvider {
22
23
	/** @var MiscService */
24
	protected $miscService;
25
26
	/** @var IL10N */
27
	protected $l10n;
28
29
	/** @var IURLGenerator */
30
	protected $url;
31
32
	/** @var IManager */
33
	protected $activityManager;
34
35
	public function __construct(
36
		IURLGenerator $url, IManager $activityManager, IL10N $l10n, MiscService $miscService
37
	) {
38
		$this->url = $url;
39
		$this->activityManager = $activityManager;
40
		$this->l10n = $l10n;
41
		$this->miscService = $miscService;
42
	}
43
44
45
	/**
46
	 * @param string $lang
47
	 * @param IEvent $event
48
	 * @param IEvent|null $previousEvent
49
	 *
50
	 * @return IEvent
51
	 */
52
	public function parse($lang, IEvent $event, IEvent $previousEvent = null) {
53
54
		if ($event->getApp() !== 'circles') {
55
			throw new \InvalidArgumentException();
56
		}
57
58
		$event = $this->parseAsMember($lang, $event);
59
		$event = $this->parseAsModerator($lang, $event);
60
61
		return $event;
62
	}
63
64
65
	/**
66
	 * @param string $lang
67
	 * @param IEvent $event
68
	 *
69
	 * @return IEvent
70
	 * @throws Exception
71
	 */
72
	private function parseAsMember($lang, IEvent $event) {
73
		if ($event->getType() !== 'circles_as_member') {
74
			return $event;
75
		}
76
77
		$params = $event->getSubjectParameters();
78
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
79
		if ($circle === null) {
80
			return $event;
81
		}
82
83
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
84
		switch ($event->getSubject()) {
85
			case 'circle_create':
86
				return $this->parseCircleCreate($lang, $circle, $event);
87
88
			case 'circle_delete':
89
				return $this->parseCircleDelete($lang, $circle, $event);
90
		}
91
92
		$event = $this->parseMembersAsMember($lang, $circle, $event);
93
94
		return $event;
95
	}
96
97
98
	/**
99
	 * @param $lang
100
	 * @param Circle $circle
101
	 * @param IEvent $event
102
	 *
103
	 * @return IEvent
104
	 */
105
	private function parseMembersAsMember($lang, Circle $circle, IEvent $event) {
106
		$params = $event->getSubjectParameters();
107
108
		$member = Member::fromJSON($this->l10n, $params['member']);
109
		if ($member === null) {
110
			return $event;
111
		}
112
113
		switch ($event->getSubject()) {
114
			case 'member_join':
115
				return $this->parseMemberJoin($lang, $circle, $member, $event);
116
117
			case 'member_add':
118
				return $this->parseMemberAdd($lang, $circle, $member, $event);
119
120
			case 'member_left':
121
				return $this->parseMemberLeft($lang, $circle, $member, $event);
122
123
			case 'member_remove':
124
				return $this->parseMemberRemove($lang, $circle, $member, $event);
125
126
			default:
127
				return $event;
128
		}
129
130
	}
131
132
133
	/**
134
	 * @param string $lang
135
	 * @param IEvent $event
136
	 *
137
	 * @return IEvent
138
	 */
139
	private function parseAsModerator($lang, IEvent $event) {
140
		if ($event->getType() !== 'circles_as_moderator') {
141
			return $event;
142
		}
143
144
		$params = $event->getSubjectParameters();
145
		$circle = Circle::fromJSON($this->l10n, $params['circle']);
146
		$member = Member::fromJSON($this->l10n, $params['member']);
147
		if ($member === null || $circle === null) {
148
			return $event;
149
		}
150
151
		$event->setIcon(CirclesService::getCircleIcon($circle->getType()));
152
153
		switch ($event->getSubject()) {
154
			case 'member_invited':
155
				return $this->parseMemberInvited($lang, $circle, $member, $event);
156
157
			case 'member_request_invitation':
158
				return $this->parseMemberRequestInvitation($lang, $circle, $member, $event);
159
160
			case 'member_level':
161
				return $this->parseMemberLevel($lang, $circle, $member, $event);
162
163
			case 'member_owner':
164
				return $this->parseMemberOwner($lang, $circle, $member, $event);
165
166
			default:
167
				throw new InvalidArgumentException();
168
		}
169
	}
170
171
	/**
172
	 * @param string $lang
173
	 * @param Circle $circle
174
	 * @param IEvent $event
175
	 *
176
	 * @return IEvent
177
	 */
178 View Code Duplication
	private function parseCircleCreate($lang, Circle $circle, IEvent $event) {
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...
179
		if ($circle->getOwner()
180
				   ->getUserId() === $this->activityManager->getCurrentUserId()
181
		) {
182
			$event->setRichSubject(
183
				$this->l10n->t('You created the circle {circle}'),
184
				['circle' => $this->generateCircleParameter($circle)]
185
			);
186
187
		} else {
188
			$event->setRichSubject(
189
				$this->l10n->t('{author} created the circle {circle}'),
190
				[
191
					'author' => $author = $this->generateUserParameter(
192
						$circle->getOwner()
193
							   ->getUserId()
194
					),
195
					'circle' => $this->generateCircleParameter($circle)
196
				]
197
			);
198
		}
199
200
		return $event;
201
	}
202
203
204
	/**
205
	 * @param string $lang
206
	 * @param Circle $circle
207
	 * @param IEvent $event
208
	 *
209
	 * @return IEvent
210
	 */
211 View Code Duplication
	private function parseCircleDelete($lang, Circle $circle, IEvent $event) {
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...
212
		if ($circle->getOwner()
213
				   ->getUserId() === $this->activityManager->getCurrentUserId()
214
		) {
215
			$event->setRichSubject(
216
				$this->l10n->t('You deleted {circle}'),
217
				['circle' => $this->generateCircleParameter($circle)]
218
			);
219
		} else {
220
			$event->setRichSubject(
221
				$this->l10n->t('{author} deleted {circle}'),
222
				[
223
					'author' => $this->generateUserParameter(
224
						$circle->getOwner()
225
							   ->getUserId()
226
					),
227
					'circle' => $this->generateCircleParameter($circle)
228
				]
229
			);
230
		}
231
232
		return $event;
233
	}
234
235
236
	/**
237
	 * @param string $lang
238
	 * @param Circle $circle
239
	 * @param Member $member
240
	 * @param IEvent $event
241
	 *
242
	 * @return IEvent
243
	 */
244 View Code Duplication
	private function parseMemberInvited($lang, Circle $circle, Member $member, IEvent $event) {
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...
245
246
		if ($circle->getUser()
247
				   ->getUserId() === $this->activityManager->getCurrentUserId()
248
		) {
249
			$event->setRichSubject(
250
				$this->l10n->t('You invited {member} into {circle}'),
251
				[
252
					'circle' => $this->generateCircleParameter($circle),
253
					'member' => $this->generateMemberParameter($member)
254
				]
255
			);
256
257
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
258
			$event->setRichSubject(
259
				$this->l10n->t('You have been invited into {circle} by {author}'),
260
				[
261
					'author' => $this->generateUserParameter(
262
						$circle->getUser()
263
							   ->getUserId()
264
					),
265
					'circle' => $this->generateCircleParameter($circle)
266
				]
267
			);
268
269
		} else {
270
			$event->setRichSubject(
271
				$this->l10n->t('{member} have been invited into {circle} by {author}'),
272
				[
273
					'author' => $this->generateUserParameter(
274
						$circle->getUser()
275
							   ->getUserId()
276
					),
277
					'circle' => $this->generateCircleParameter($circle),
278
					'member' => $this->generateMemberParameter($member)
279
				]
280
			);
281
		}
282
283
		return $event;
284
	}
285
286
287
	/**
288
	 * @param $lang
289
	 * @param Circle $circle
290
	 * @param Member $member
291
	 * @param IEvent $event
292
	 *
293
	 * @return IEvent
294
	 */
295 View Code Duplication
	private function parseMemberRequestInvitation(
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...
296
		$lang, Circle $circle, Member $member, IEvent $event
297
	) {
298
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
299
			$event->setRichSubject(
300
				$this->l10n->t('You requested an invitation to {circle}'),
301
				['circle' => $this->generateCircleParameter($circle)]
302
			);
303
304
		} else {
305
			$event->setRichSubject(
306
				$this->l10n->t(
307
					'{author} has requested an invitation into {circle}'
308
				), [
309
					'author' => $this->generateMemberParameter($member),
310
					'circle' => $this->generateCircleParameter($circle)
311
				]
312
			);
313
		}
314
315
		return $event;
316
	}
317
318
319
	/**
320
	 * @param $lang
321
	 * @param Circle $circle
322
	 * @param Member $member
323
	 * @param IEvent $event
324
	 *
325
	 * @return IEvent
326
	 */
327 View Code Duplication
	private function parseMemberJoin($lang, Circle $circle, Member $member, IEvent $event) {
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...
328
		if ($circle->getUser()
329
				   ->getUserId() === $this->activityManager->getCurrentUserId()
330
		) {
331
			$event->setRichSubject(
332
				$this->l10n->t('You joined {circle}'),
333
				['circle' => $this->generateCircleParameter($circle)]
334
			);
335
336
		} else {
337
			$event->setRichSubject(
338
				$this->l10n->t(
339
					'{member} has joined the circle {circle}'
340
				), [
341
					'circle' => $this->generateCircleParameter($circle),
342
					'member' => $this->generateMemberParameter($member)
343
				]
344
			);
345
		}
346
347
		return $event;
348
	}
349
350
351
	/**
352
	 * @param $lang
353
	 * @param Circle $circle
354
	 * @param Member $member
355
	 * @param IEvent $event
356
	 *
357
	 * @return IEvent
358
	 */
359 View Code Duplication
	private function parseMemberAdd($lang, Circle $circle, Member $member, IEvent $event) {
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...
360
361
		if ($circle->getUser()
362
				   ->getUserId() === $this->activityManager->getCurrentUserId()
363
		) {
364
			$event->setRichSubject(
365
				$this->l10n->t('You added {member} as member to {circle}'),
366
				[
367
					'circle' => $this->generateCircleParameter($circle),
368
					'member' => $this->generateMemberParameter($member)
369
				]
370
			);
371
372
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
373
			$event->setRichSubject(
374
				$this->l10n->t('You were added as member to {circle} by {author}'),
375
				[
376
					'author' => $this->generateUserParameter(
377
						$circle->getUser()
378
							   ->getUserId()
379
					),
380
					'circle' => $this->generateCircleParameter($circle)
381
				]
382
			);
383
384
		} else {
385
			$event->setRichSubject(
386
				$this->l10n->t(
387
					'{member} was added as member to {circle} by {author}'
388
				), [
389
					'author' => $this->generateUserParameter(
390
						$circle->getUser()
391
							   ->getUserId()
392
					),
393
					'circle' => $this->generateCircleParameter($circle),
394
					'member' => $this->generateMemberParameter($member)
395
				]
396
			);
397
		}
398
399
		return $event;
400
	}
401
402
403
	/**
404
	 * @param $lang
405
	 * @param Circle $circle
406
	 * @param Member $member
407
	 * @param IEvent $event
408
	 *
409
	 * @return IEvent
410
	 */
411 View Code Duplication
	private function parseMemberLeft($lang, Circle $circle, Member $member, IEvent $event) {
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...
412
		if ($circle->getUser()
413
				   ->getUserId() === $this->activityManager->getCurrentUserId()
414
		) {
415
			$event->setRichSubject(
416
				$this->l10n->t('You left {circle}'),
417
				['circle' => $this->generateCircleParameter($circle)]
418
			);
419
420
		} else {
421
			$event->setRichSubject(
422
				$this->l10n->t(
423
					'{member} has left {circle}'
424
				), [
425
					'circle' => $this->generateCircleParameter($circle),
426
					'member' => $this->generateMemberParameter($member)
427
				]
428
			);
429
		}
430
431
		return $event;
432
	}
433
434
435
	/**
436
	 * @param $lang
437
	 * @param Circle $circle
438
	 * @param Member $member
439
	 * @param IEvent $event
440
	 *
441
	 * @return IEvent
442
	 */
443 View Code Duplication
	private function parseMemberRemove($lang, Circle $circle, Member $member, IEvent $event) {
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...
444
445
		if ($circle->getUser()
446
				   ->getUserId() === $this->activityManager->getCurrentUserId()
447
		) {
448
			$event->setRichSubject(
449
				$this->l10n->t('You removed {member} from {circle}'),
450
				[
451
					'circle' => $this->generateCircleParameter($circle),
452
					'member' => $this->generateMemberParameter($member)
453
				]
454
			);
455
456
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
457
			$event->setRichSubject(
458
				$this->l10n->t('You were removed from {circle} by {author}'),
459
				[
460
					'author' => $this->generateUserParameter(
461
						$circle->getUser()
462
							   ->getUserId()
463
					),
464
					'circle' => $this->generateCircleParameter($circle)
465
				]
466
			);
467
468
		} else {
469
			$event->setRichSubject(
470
				$this->l10n->t(
471
					'{member} was removed from {circle} by {author}'
472
				), [
473
					'author' => $this->generateUserParameter(
474
						$circle->getUser()
475
							   ->getUserId()
476
					),
477
					'circle' => $this->generateCircleParameter($circle),
478
					'member' => $this->generateMemberParameter($member)
479
				]
480
			);
481
		}
482
483
		return $event;
484
	}
485
486
487
	/**
488
	 * @param $lang
489
	 * @param Circle $circle
490
	 * @param Member $member
491
	 * @param IEvent $event
492
	 *
493
	 * @return IEvent
494
	 */
495
	private function parseMemberLevel($lang, Circle $circle, Member $member, IEvent $event) {
496
497
		if ($circle->getUser()
498
				   ->getUserId() === $this->activityManager->getCurrentUserId()
499
		) {
500
			$event->setRichSubject(
501
				$this->l10n->t(
502
					'You changed {member}\'s level in {circle} to %1$s',
503
					[$this->l10n->t($member->getLevelString())]
504
				),
505
				[
506
					'circle' => $this->generateCircleParameter($circle),
507
					'member' => $this->generateMemberParameter($member)
508
				]
509
			);
510
511
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
512
			$event->setRichSubject(
513
				$this->l10n->t(
514
					'{author} changed your level in {circle} to %1$s',
515
					[$this->l10n->t($member->getLevelString())]
516
				),
517
				[
518
					'author' => $this->generateUserParameter(
519
						$circle->getUser()
520
							   ->getUserId()
521
					),
522
					'circle' => $this->generateCircleParameter($circle),
523
					'level'  => $this->l10n->t($member->getLevelString())
524
				]
525
			);
526
527
		} else {
528
			$event->setRichSubject(
529
				$this->l10n->t(
530
					'{author} changed {member}\'s level in {circle} to %1$s',
531
					[$this->l10n->t($member->getLevelString())]
532
				), [
533
					'author' => $this->generateUserParameter(
534
						$circle->getUser()
535
							   ->getUserId()
536
					),
537
					'circle' => $this->generateCircleParameter($circle),
538
					'member' => $this->generateMemberParameter($member),
539
					'level'  => $this->l10n->t($member->getLevelString())
540
				]
541
			);
542
		}
543
544
		return $event;
545
	}
546
547
548
	/**
549
	 * @param $lang
550
	 * @param Circle $circle
551
	 * @param Member $member
552
	 * @param IEvent $event
553
	 *
554
	 * @return IEvent
555
	 */
556 View Code Duplication
	private function parseMemberOwner($lang, Circle $circle, Member $member, IEvent $event) {
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...
557
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()
558
		) {
559
			$event->setRichSubject(
560
				$this->l10n->t('You are the new owner of {circle}'),
561
				['circle' => $this->generateCircleParameter($circle)]
562
			);
563
564
		} else {
565
			$event->setRichSubject(
566
				$this->l10n->t(
567
					'{member} is the new owner of {circle}'
568
				), [
569
					'circle' => $this->generateCircleParameter($circle),
570
					'member' => $this->generateMemberParameter($member)
571
				]
572
			);
573
		}
574
575
		return $event;
576
	}
577
578
	private function parseActivityHeader(IEvent &$event, SharingFrame $frame) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
579
580
		$this->activityManager->getCurrentUserId();
581
582
		if ($frame->getAuthor() === $this->activityManager->getCurrentUserId()
583
			&& $frame->getCloudId() === null
584
		) {
585
586
			$event->setParsedSubject(
587
				$this->l10n->t(
588
					'You shared a mood with %1$s', ['circle1, circle2']
589
				)
590
			)
591
				  ->setRichSubject(
592
					  $this->l10n->t(
593
						  'You shared a mood with {circles}'
594
					  ),
595
					  ['circles' => $this->generateCircleParameter($frame)]
596
597
				  );
598
599
		} else {
600
601
			$author = $this->generateUserParameter($frame);
602
			$event->setParsedSubject(
603
				$this->l10n->t(
604
					'%1$s shared a mood with %2$s', [
605
													  $author['name'],
606
													  'circle1, circle2'
607
												  ]
608
				)
609
			)
610
				  ->setRichSubject(
611
					  $this->l10n->t(
612
						  '{author} shared a mood with {circles}'
613
					  ), [
614
						  'author'  => $author,
615
						  'circles' => $this->generateCircleParameter($frame)
616
					  ]
617
				  );
618
		}
619
	}
620
621
622
	private function generateMemberParameter(
623
		Member $member
624
	) {
625
		return $this->generateUserParameter($member->getUserId());
626
	}
627
628
629
	private function generateCircleParameter(
630
		Circle $circle
631
	) {
632
		return [
633
			'type' => 'circle',
634
			'id'   => $circle->getId(),
635
			'name' => $circle->getName(),
636
			'link' => \OC::$server->getURLGenerator()
637
								  ->linkToRoute('circles.Navigation.navigate')
638
					  . '#' . $circle->getId()
639
		];
640
	}
641
642
	/**
643
	 * @param $userId
644
	 *
645
	 * @return array
646
	 */
647
	private function generateUserParameter(
648
		$userId
649
	) {
650
		return [
651
			'type' => 'user',
652
			'id'   => $userId,
653
			'name' => $userId
654
		];
655
	}
656
}
657