Completed
Push — activities ( 7ed318...f6cb6d )
by Maxence
02:57
created

Provider::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
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
			default:
161
				throw new InvalidArgumentException();
162
		}
163
	}
164
165
	/**
166
	 * @param string $lang
167
	 * @param Circle $circle
168
	 * @param IEvent $event
169
	 *
170
	 * @return IEvent
171
	 */
172 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...
173
		if ($circle->getOwner()
174
				   ->getUserId() === $this->activityManager->getCurrentUserId()
175
		) {
176
			$event->setRichSubject(
177
				$this->l10n->t('You created the circle {circle}'),
178
				['circle' => $this->generateCircleParameter($circle)]
179
			);
180
181
		} else {
182
			$event->setRichSubject(
183
				$this->l10n->t('{author} created the circle {circle}'),
184
				[
185
					'author' => $author = $this->generateUserParameter(
186
						$circle->getOwner()
187
							   ->getUserId()
188
					),
189
					'circle' => $this->generateCircleParameter($circle)
190
				]
191
			);
192
		}
193
194
		return $event;
195
	}
196
197
198
	/**
199
	 * @param string $lang
200
	 * @param Circle $circle
201
	 * @param IEvent $event
202
	 *
203
	 * @return IEvent
204
	 */
205 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...
206
		if ($circle->getOwner()
207
				   ->getUserId() === $this->activityManager->getCurrentUserId()
208
		) {
209
			$event->setRichSubject(
210
				$this->l10n->t('You deleted {circle}'),
211
				['circle' => $this->generateCircleParameter($circle)]
212
			);
213
		} else {
214
			$event->setRichSubject(
215
				$this->l10n->t('{author} deleted {circle}'),
216
				[
217
					'author' => $this->generateUserParameter(
218
						$circle->getOwner()
219
							   ->getUserId()
220
					),
221
					'circle' => $this->generateCircleParameter($circle)
222
				]
223
			);
224
		}
225
226
		return $event;
227
	}
228
229
230
	/**
231
	 * @param string $lang
232
	 * @param Circle $circle
233
	 * @param Member $member
234
	 * @param IEvent $event
235
	 *
236
	 * @return IEvent
237
	 */
238 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...
239
240
		if ($circle->getUser()
241
				   ->getUserId() === $this->activityManager->getCurrentUserId()
242
		) {
243
			$event->setRichSubject(
244
				$this->l10n->t('You invited {member} into {circle}'),
245
				[
246
					'circle' => $this->generateCircleParameter($circle),
247
					'member' => $this->generateMemberParameter($member)
248
				]
249
			);
250
251
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
252
			$event->setRichSubject(
253
				$this->l10n->t('You have been invited into {circle} by {author}'),
254
				[
255
					'author' => $this->generateUserParameter(
256
						$circle->getUser()
257
							   ->getUserId()
258
					),
259
					'circle' => $this->generateCircleParameter($circle)
260
				]
261
			);
262
263
		} else {
264
			$event->setRichSubject(
265
				$this->l10n->t('{member} have been invited into {circle} by {author}'),
266
				[
267
					'author' => $this->generateUserParameter(
268
						$circle->getUser()
269
							   ->getUserId()
270
					),
271
					'circle' => $this->generateCircleParameter($circle),
272
					'member' => $this->generateMemberParameter($member)
273
				]
274
			);
275
		}
276
277
		return $event;
278
	}
279
280
281
	/**
282
	 * @param $lang
283
	 * @param Circle $circle
284
	 * @param Member $member
285
	 * @param IEvent $event
286
	 *
287
	 * @return IEvent
288
	 */
289
	private function parseMemberRequestInvitation(
290
		$lang, Circle $circle, Member $member, IEvent $event
291
	) {
292
		if ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
293
			$event->setRichSubject(
294
				$this->l10n->t('You requested an invitation to {circle}'),
295
				['circle' => $this->generateCircleParameter($circle)]
296
			);
297
298
		} else {
299
			$event->setRichSubject(
300
				$this->l10n->t(
301
					'{author} has requested an invitation into {circle}'
302
				), [
303
					'author' => $this->generateMemberParameter($member),
304
					'circle' => $this->generateCircleParameter($circle)
305
				]
306
			);
307
		}
308
309
		return $event;
310
	}
311
312
313
	/**
314
	 * @param $lang
315
	 * @param Circle $circle
316
	 * @param Member $member
317
	 * @param IEvent $event
318
	 *
319
	 * @return IEvent
320
	 */
321 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...
322
		if ($circle->getUser()
323
				   ->getUserId() === $this->activityManager->getCurrentUserId()
324
		) {
325
			$event->setRichSubject(
326
				$this->l10n->t('You joined {circle}'),
327
				['circle' => $this->generateCircleParameter($circle)]
328
			);
329
330
		} else {
331
			$event->setRichSubject(
332
				$this->l10n->t(
333
					'{member} has joined the circle {circle}'
334
				), [
335
					'circle' => $this->generateCircleParameter($circle),
336
					'member' => $this->generateMemberParameter($member)
337
				]
338
			);
339
		}
340
341
		return $event;
342
	}
343
344
345
	/**
346
	 * @param $lang
347
	 * @param Circle $circle
348
	 * @param Member $member
349
	 * @param IEvent $event
350
	 *
351
	 * @return IEvent
352
	 */
353 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...
354
355
		if ($circle->getUser()
356
				   ->getUserId() === $this->activityManager->getCurrentUserId()
357
		) {
358
			$event->setRichSubject(
359
				$this->l10n->t('You added {member} as member to {circle}'),
360
				[
361
					'circle' => $this->generateCircleParameter($circle),
362
					'member' => $this->generateMemberParameter($member)
363
				]
364
			);
365
366
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
367
			$event->setRichSubject(
368
				$this->l10n->t('You were added as member to {circle} by {author}'),
369
				[
370
					'author' => $this->generateUserParameter(
371
						$circle->getUser()
372
							   ->getUserId()
373
					),
374
					'circle' => $this->generateCircleParameter($circle)
375
				]
376
			);
377
378
		} else {
379
			$event->setRichSubject(
380
				$this->l10n->t(
381
					'{member} was added as member to {circle} by {author}'
382
				), [
383
					'author' => $this->generateUserParameter(
384
						$circle->getUser()
385
							   ->getUserId()
386
					),
387
					'circle' => $this->generateCircleParameter($circle),
388
					'member' => $this->generateMemberParameter($member)
389
				]
390
			);
391
		}
392
393
		return $event;
394
	}
395
396
397
	/**
398
	 * @param $lang
399
	 * @param Circle $circle
400
	 * @param Member $member
401
	 * @param IEvent $event
402
	 *
403
	 * @return IEvent
404
	 */
405 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...
406
		if ($circle->getUser()
407
				   ->getUserId() === $this->activityManager->getCurrentUserId()
408
		) {
409
			$event->setRichSubject(
410
				$this->l10n->t('You left {circle}'),
411
				['circle' => $this->generateCircleParameter($circle)]
412
			);
413
414
		} else {
415
			$event->setRichSubject(
416
				$this->l10n->t(
417
					'{member} has left {circle}'
418
				), [
419
					'circle' => $this->generateCircleParameter($circle),
420
					'member' => $this->generateMemberParameter($member)
421
				]
422
			);
423
		}
424
425
		return $event;
426
	}
427
428
429
	/**
430
	 * @param $lang
431
	 * @param Circle $circle
432
	 * @param Member $member
433
	 * @param IEvent $event
434
	 *
435
	 * @return IEvent
436
	 */
437 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...
438
439
		if ($circle->getUser()
440
				   ->getUserId() === $this->activityManager->getCurrentUserId()
441
		) {
442
			$event->setRichSubject(
443
				$this->l10n->t('You removed {member} from {circle}'),
444
				[
445
					'circle' => $this->generateCircleParameter($circle),
446
					'member' => $this->generateMemberParameter($member)
447
				]
448
			);
449
450
		} elseif ($member->getUserId() === $this->activityManager->getCurrentUserId()) {
451
			$event->setRichSubject(
452
				$this->l10n->t('You were removed from {circle} by {author}'),
453
				[
454
					'author' => $this->generateUserParameter(
455
						$circle->getUser()
456
							   ->getUserId()
457
					),
458
					'circle' => $this->generateCircleParameter($circle)
459
				]
460
			);
461
462
		} else {
463
			$event->setRichSubject(
464
				$this->l10n->t(
465
					'{member} was removed from {circle} by {author}'
466
				), [
467
					'author' => $this->generateUserParameter(
468
						$circle->getUser()
469
							   ->getUserId()
470
					),
471
					'circle' => $this->generateCircleParameter($circle),
472
					'member' => $this->generateMemberParameter($member)
473
				]
474
			);
475
		}
476
477
		return $event;
478
	}
479
480
481
482
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
483
//
484
//	/**
485
//	 * @param string $lang
486
//	 * @param IEvent $event
487
//	 *
488
//	 * @return IEvent
489
//	 */
490
//	private function parsePopulation($lang, IEvent $event) {
491
//		if ($event->getType() !== 'circles_population') {
492
//			return $event;
493
//		}
494
//
495
//		return $event;
496
//	}
497
//
498
//
499
//	/**
500
//	 * @param string $lang
501
//	 * @param IEvent $event
502
//	 *
503
//	 * @return IEvent
504
//	 */
505
//	private function parseRights($lang, IEvent $event) {
506
//		if ($event->getType() !== 'circles_rights') {
507
//			return $event;
508
//		}
509
//
510
//		return $event;
511
//	}
512
//
513
//
514
//	/**
515
//	 * @param string $lang
516
//	 * @param IEvent $event
517
//	 *
518
//	 * @return IEvent
519
//	 */
520
//	private function parseShares($lang, IEvent $event) {
521
//		if ($event->getType() !== 'circles_shares') {
522
//			return $event;
523
//		}
524
//
525
//		return $event;
526
//	}
527
//
528
//	private function parseMood(IEvent &$event, $mood) {
529
//
530
//		if (key_exists('website', $mood)) {
531
//			$event->setRichMessage(
532
//				$mood['text'] . '{opengraph}',
533
//				['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])]
534
//			);
535
//		} else {
536
//			$event->setParsedMessage($mood['text']);
537
//		}
538
//
539
//	}
540
541
542
	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...
543
544
		$this->activityManager->getCurrentUserId();
545
546
		if ($frame->getAuthor() === $this->activityManager->getCurrentUserId()
547
			&& $frame->getCloudId() === null
548
		) {
549
550
			$event->setParsedSubject(
551
				$this->l10n->t(
552
					'You shared a mood with %1$s', ['circle1, circle2']
553
				)
554
			)
555
				  ->setRichSubject(
556
					  $this->l10n->t(
557
						  'You shared a mood with {circles}'
558
					  ),
559
					  ['circles' => $this->generateCircleParameter($frame)]
560
561
				  );
562
563
		} else {
564
565
			$author = $this->generateUserParameter($frame);
566
			$event->setParsedSubject(
567
				$this->l10n->t(
568
					'%1$s shared a mood with %2$s', [
569
													  $author['name'],
570
													  'circle1, circle2'
571
												  ]
572
				)
573
			)
574
				  ->setRichSubject(
575
					  $this->l10n->t(
576
						  '{author} shared a mood with {circles}'
577
					  ), [
578
						  'author'  => $author,
579
						  'circles' => $this->generateCircleParameter($frame)
580
					  ]
581
				  );
582
		}
583
	}
584
585
586
	private function generateCircleParameter(Circle $circle) {
587
		return [
588
			'type' => 'circle',
589
			'id'   => $circle->getId(),
590
			'name' => $circle->getName(),
591
			'link' => \OC::$server->getURLGenerator()
592
								  ->linkToRoute('circles.Navigation.navigate')
593
					  . '#' . $circle->getId()
594
		];
595
	}
596
597
598
	private function generateMemberParameter(Member $member) {
599
		return $this->generateUserParameter($member->getUserId());
600
	}
601
602
	/**
603
	 * @param $userId
604
	 *
605
	 * @return array
606
	 */
607
	private function generateUserParameter($userId) {
608
		return [
609
			'type' => 'user',
610
			'id'   => $userId,
611
			'name' => $userId
612
		];
613
	}
614
}
615