Completed
Push — master ( 70edb2...6362ed )
by Maxence
02:18
created

EventsService::onLinkRequestSent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
use OCA\Circles\Db\CirclesRequest;
30
use OCA\Circles\Db\MembersRequest;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\FederatedLink;
33
use OCA\Circles\Model\Member;
34
use OCP\Activity\IEvent;
35
use OCP\Activity\IManager;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
39
class EventsService {
40
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var IManager */
46
	private $activityManager;
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	/** @var CirclesRequest */
52
	private $circlesRequest;
53
54
	/** @var MembersRequest */
55
	private $membersRequest;
56
57
	/** @var MiscService */
58
	private $miscService;
59
60
61
	/**
62
	 * Events constructor.
63
	 *
64
	 * @param string $userId
65
	 * @param IManager $activityManager
66
	 * @param IUserManager $userManager
67
	 * @param CirclesRequest $circlesRequest
68
	 * @param MembersRequest $membersRequest
69
	 * @param MiscService $miscService
70
	 */
71
	public function __construct(
72
		$userId, IManager $activityManager, IUserManager $userManager,
73
		CirclesRequest $circlesRequest, MembersRequest $membersRequest, MiscService $miscService
74
	) {
75
		$this->userId = $userId;
76
		$this->activityManager = $activityManager;
77
		$this->userManager = $userManager;
78
		$this->circlesRequest = $circlesRequest;
79
		$this->membersRequest = $membersRequest;
80
		$this->miscService = $miscService;
81
	}
82
83
84
	/**
85
	 * onCircleCreation()
86
	 *
87
	 * Called when a circle is created.
88
	 * Broadcast an activity to the cloud
89
	 * We won't do anything if the circle is not PUBLIC or PRIVATE
90
	 *
91
	 * @param Circle $circle
92
	 */
93
	public function onCircleCreation(Circle $circle) {
94
		if ($circle->getType() !== Circle::CIRCLES_PUBLIC
95
			&& $circle->getType() !== Circle::CIRCLES_PRIVATE
96
		) {
97
			return;
98
		}
99
100
		$event = $this->generateEvent('circles_as_member');
101
		$event->setSubject('circle_create', ['circle' => json_encode($circle)]);
102
103
		$this->userManager->callForSeenUsers(
104
			function($user) use ($event) {
105
				/** @var IUser $user */
106
				$this->publishEvent($event, [$user]);
107
			}
108
		);
109
	}
110
111
112
	/**
113
	 * onCircleDestruction()
114
	 *
115
	 * Called when a circle is destroyed.
116
	 * Broadcast an activity on its members.
117
	 * We won't do anything if the circle is PERSONAL
118
	 *
119
	 * @param Circle $circle
120
	 */
121 View Code Duplication
	public function onCircleDestruction(Circle $circle) {
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...
122
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
123
			return;
124
		}
125
126
		$event = $this->generateEvent('circles_as_member');
127
		$event->setSubject('circle_delete', ['circle' => json_encode($circle)]);
128
		$this->publishEvent(
129
			$event,
130
			$this->membersRequest->forceGetMembers(
131
				$circle->getUniqueId(), Member::LEVEL_MEMBER, true
132
			)
133
		);
134
	}
135
136
137
	/**
138
	 * onMemberNew()
139
	 *
140
	 * Called when a member is added to a circle.
141
	 * Broadcast an activity to the new member and to the moderators of the circle.
142
	 * We won't do anything if the circle is PERSONAL
143
	 * If the level is still 0, we will redirect to onMemberAlmost and manage the
144
	 * invitation/request from there
145
	 * If the level is Owner, we ignore the event.
146
	 *
147
	 * @param Circle $circle
148
	 * @param Member $member
149
	 */
150
	public function onMemberNew(Circle $circle, Member $member) {
151
		if ($member->getLevel() === Member::LEVEL_OWNER
152
			|| $circle->getType() === Circle::CIRCLES_PERSONAL
153
		) {
154
			return;
155
		}
156
157
		if ($member->getLevel() === Member::LEVEL_NONE) {
158
			$this->onMemberAlmost($circle, $member);
159
160
			return;
161
		}
162
163
		$event = $this->generateEvent('circles_as_member');
164
		$event->setSubject(
165
			($this->userId === $member->getUserId()) ? 'member_join' : 'member_add',
166
			['circle' => json_encode($circle), 'member' => json_encode($member)]
167
		);
168
169
		$this->publishEvent(
170
			$event, array_merge(
171
					  [$member],
172
					  $this->membersRequest->forceGetMembers(
173
						  $circle->getUniqueId(), Member::LEVEL_MODERATOR, true
174
					  )
175
				  )
176
		);
177
	}
178
179
180
	/**
181
	 * onMemberAlmost()
182
	 *
183
	 * Called when a member is added to a circle with level=0
184
	 * Trigger onMemberInvitation() or onMemberInvitationRequest() based on Member Status
185
	 *
186
	 * @param Circle $circle
187
	 * @param Member $member
188
	 */
189
	private function onMemberAlmost(Circle $circle, Member $member) {
190
191
		switch ($member->getStatus()) {
192
			case Member::STATUS_INVITED:
193
				$this->onMemberInvitation($circle, $member);
194
195
				return;
196
197
			case Member::STATUS_REQUEST:
198
				$this->onMemberInvitationRequest($circle, $member);
199
200
				return;
201
		}
202
	}
203
204
205
	/**
206
	 * onMemberInvitation()
207
	 *
208
	 * Called when a member is invited to a circle.
209
	 * Broadcast an activity to the invited member and to the moderators of the circle.
210
	 *
211
	 * @param Circle $circle
212
	 * @param Member $member
213
	 */
214 View Code Duplication
	public function onMemberInvitation(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...
215
		if ($circle->getType() !== Circle::CIRCLES_PRIVATE) {
216
			return;
217
		}
218
219
		$event = $this->generateEvent('circles_as_moderator');
220
		$event->setSubject(
221
			'member_invited', ['circle' => json_encode($circle), 'member' => json_encode($member)]
222
		);
223
224
		$this->publishEvent(
225
			$event, array_merge(
226
					  [$member],
227
					  $this->membersRequest->forceGetMembers(
228
						  $circle->getUniqueId(), Member::LEVEL_MODERATOR, true
229
					  )
230
				  )
231
		);
232
	}
233
234
235
	/**
236
	 * onMemberInvitationRequest()
237
	 *
238
	 * Called when a member request an invitation to a private circle.
239
	 * Broadcast an activity to the requester and to the moderators of the circle.
240
	 *
241
	 * @param Circle $circle
242
	 * @param Member $member
243
	 */
244 View Code Duplication
	public function onMemberInvitationRequest(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...
245
		if ($circle->getType() !== Circle::CIRCLES_PRIVATE) {
246
			return;
247
		}
248
249
		$event = $this->generateEvent('circles_as_moderator');
250
		$event->setSubject(
251
			'member_request_invitation',
252
			['circle' => json_encode($circle), 'member' => json_encode($member)]
253
		);
254
255
		$this->publishEvent(
256
			$event, array_merge(
257
					  [$member],
258
					  $this->membersRequest->forceGetMembers(
259
						  $circle->getUniqueId(), Member::LEVEL_MODERATOR, true
260
					  )
261
				  )
262
		);
263
264
	}
265
266
267
	/**
268
	 * onMemberLeaving()
269
	 *
270
	 * Called when a member is removed from a circle.
271
	 * Broadcast an activity to the leaving member and to the moderators of the circle.
272
	 * We won't do anything if the circle is PERSONAL
273
	 *
274
	 * @param Circle $circle
275
	 * @param Member $member
276
	 */
277
	public function onMemberLeaving(Circle $circle, Member $member) {
278
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
279
			return;
280
		}
281
282
		$event = $this->generateEvent('circles_as_member');
283
		$event->setSubject(
284
			($this->userId === $member->getUserId()) ? 'member_left' : 'member_remove',
285
			['circle' => json_encode($circle), 'member' => json_encode($member)]
286
		);
287
288
		$this->publishEvent(
289
			$event, array_merge(
290
					  [$member],
291
					  $this->membersRequest->forceGetMembers(
292
						  $circle->getUniqueId(), Member::LEVEL_MODERATOR, true
293
					  )
294
				  )
295
		);
296
297
	}
298
299
300
	/**
301
	 * onMemberLevel()
302
	 *
303
	 * Called when a member have his level changed.
304
	 * Broadcast an activity to all moderator of the circle, and the member if he is demoted.
305
	 * If the level is Owner, we identify the event as a Coup d'Etat and we broadcast all members.
306
	 *
307
	 * @param Circle $circle
308
	 * @param Member $member
309
	 */
310 View Code Duplication
	public function onMemberLevel(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...
311
		if ($member->getLevel() === Member::LEVEL_OWNER) {
312
			$this->onMemberOwner($circle, $member);
313
314
			return;
315
		}
316
317
		$event = $this->generateEvent('circles_as_moderator');
318
		$event->setSubject(
319
			'member_level',
320
			['circle' => json_encode($circle), 'member' => json_encode($member)]
321
		);
322
323
		$mods = $this->membersRequest->forceGetMembers(
324
			$circle->getUniqueId(), Member::LEVEL_MODERATOR, true
325
		);
326
		$this->membersRequest->avoidDuplicateMembers($mods, [$member]);
327
328
		$this->publishEvent($event, $mods);
329
	}
330
331
332
	/**
333
	 * onMemberOwner()
334
	 *
335
	 * Called when the owner rights of a circle have be given to another member.
336
	 *
337
	 * @param Circle $circle
338
	 * @param Member $member
339
	 */
340 View Code Duplication
	private function onMemberOwner(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...
341
		$event = $this->generateEvent('circles_as_moderator');
342
		$event->setSubject(
343
			'member_owner',
344
			['circle' => json_encode($circle), 'member' => json_encode($member)]
345
		);
346
347
		$this->publishEvent(
348
			$event,
349
			$this->membersRequest->forceGetMembers(
350
				$circle->getUniqueId(), Member::LEVEL_MEMBER, true
351
			)
352
		);
353
	}
354
355
356
	/**
357
	 * onGroupLink()
358
	 *
359
	 * Called when a group is linked to a circle.
360
	 * Broadcast an activity to the member of the linked group and to the moderators of the circle.
361
	 * We won't do anything if the circle is PERSONAL
362
	 *
363
	 * @param Circle $circle
364
	 * @param Member $group
365
	 */
366 View Code Duplication
	public function onGroupLink(Circle $circle, Member $group) {
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...
367
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
368
			return;
369
		}
370
371
		$event = $this->generateEvent('circles_as_moderator');
372
		$event->setSubject(
373
			'group_link',
374
			['circle' => json_encode($circle), 'group' => json_encode($group)]
375
		);
376
377
		$mods = $this->membersRequest->forceGetMembers(
378
			$circle->getUniqueId(), Member::LEVEL_MODERATOR, true
379
		);
380
		$this->membersRequest->avoidDuplicateMembers(
381
			$mods, $this->membersRequest->getGroupMemberMembers($group)
382
		);
383
384
		$this->publishEvent($event, $mods);
385
	}
386
387
388
	/**
389
	 * onGroupUnlink()
390
	 *
391
	 * Called when a group is unlinked from a circle.
392
	 * Broadcast an activity to the member of the unlinked group and to the moderators of the
393
	 * circle. We won't do anything if the circle is PERSONAL
394
	 *
395
	 * @param Circle $circle
396
	 * @param Member $group
397
	 */
398 View Code Duplication
	public function onGroupUnlink(Circle $circle, Member $group) {
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...
399
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
400
			return;
401
		}
402
403
		$event = $this->generateEvent('circles_as_moderator');
404
		$event->setSubject(
405
			'group_unlink',
406
			['circle' => json_encode($circle), 'group' => json_encode($group)]
407
		);
408
409
		$mods = $this->membersRequest->forceGetMembers(
410
			$circle->getUniqueId(), Member::LEVEL_MODERATOR, true
411
		);
412
		$this->membersRequest->avoidDuplicateMembers(
413
			$mods, $this->membersRequest->getGroupMemberMembers($group)
414
		);
415
416
		$this->publishEvent($event, $mods);
417
	}
418
419
420
	/**
421
	 * onGroupLevel()
422
	 *
423
	 * Called when a linked group have his level changed.
424
	 * Broadcast an activity to all moderator of the circle, and the group members in case of
425
	 * demotion.
426
	 *
427
	 * @param Circle $circle
428
	 * @param Member $group
429
	 */
430 View Code Duplication
	public function onGroupLevel(Circle $circle, Member $group) {
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...
431
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
432
			return;
433
		}
434
435
		$event = $this->generateEvent('circles_as_moderator');
436
		$event->setSubject(
437
			'group_level',
438
			['circle' => json_encode($circle), 'group' => json_encode($group)]
439
		);
440
441
		$mods = $this->membersRequest->forceGetMembers(
442
			$circle->getUniqueId(), Member::LEVEL_MODERATOR, true
443
		);
444
		$this->membersRequest->avoidDuplicateMembers(
445
			$mods, $this->membersRequest->getGroupMemberMembers($group)
446
		);
447
448
		$this->publishEvent($event, $mods);
449
	}
450
451
452
	/**
453
	 * onLinkRequestSent()
454
	 *
455
	 * Called when a request to generate a link with a remote circle is sent.
456
	 * Broadcast an activity to the moderators of the circle.
457
	 *
458
	 * @param Circle $circle
459
	 * @param FederatedLink $link
460
	 */
461 View Code Duplication
	public function onLinkRequestSent(Circle $circle, FederatedLink $link) {
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...
462
		$event = $this->generateEvent('circles_as_moderator');
463
		$event->setSubject(
464
			'link_request_sent',
465
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
466
		);
467
468
		$this->publishEvent(
469
			$event, $this->membersRequest->forceGetMembers(
470
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
471
		)
472
		);
473
	}
474
475
476
	/**
477
	 * onLinkRequestReceived()
478
	 *
479
	 * Called when a request to generate a link from a remote host is received.
480
	 * Broadcast an activity to the moderators of the circle.
481
	 *
482
	 * @param Circle $circle
483
	 * @param FederatedLink $link
484
	 */
485 View Code Duplication
	public function onLinkRequestReceived(Circle $circle, FederatedLink $link) {
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...
486
		$event = $this->generateEvent('circles_as_moderator');
487
		$event->setSubject(
488
			'link_request_received',
489
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
490
		);
491
492
		$this->publishEvent(
493
			$event, $this->membersRequest->forceGetMembers(
494
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
495
		)
496
		);
497
	}
498
499
500
	/**
501
	 * onLinkRequestRejected()
502
	 *
503
	 * Called when a request to generate a link from a remote host is dismissed.
504
	 * Broadcast an activity to the moderators of the circle.
505
	 *
506
	 * @param Circle $circle
507
	 * @param FederatedLink $link
508
	 */
509 View Code Duplication
	public function onLinkRequestRejected(Circle $circle, FederatedLink $link) {
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...
510
		$event = $this->generateEvent('circles_as_moderator');
511
		$event->setSubject(
512
			'link_request_rejected',
513
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
514
		);
515
516
		$this->publishEvent(
517
			$event, $this->membersRequest->forceGetMembers(
518
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
519
		)
520
		);
521
	}
522
523
524
	/**
525
	 * onLinkRequestRejected()
526
	 *
527
	 * Called when a request to generate a link from a remote host is dismissed.
528
	 * Broadcast an activity to the moderators of the circle.
529
	 *
530
	 * @param Circle $circle
531
	 * @param FederatedLink $link
532
	 */
533 View Code Duplication
	public function onLinkRequestCanceled(Circle $circle, FederatedLink $link) {
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...
534
		$event = $this->generateEvent('circles_as_moderator');
535
		$event->setSubject(
536
			'link_request_canceled',
537
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
538
		);
539
540
		$this->publishEvent(
541
			$event, $this->membersRequest->forceGetMembers(
542
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
543
		)
544
		);
545
	}
546
547
548
	/**
549
	 * onLinkRequestAccepted()
550
	 *
551
	 * Called when a request to generate a link from a remote host is accepted.
552
	 * Broadcast an activity to the moderators of the circle.
553
	 *
554
	 * @param Circle $circle
555
	 * @param FederatedLink $link
556
	 */
557 View Code Duplication
	public function onLinkRequestAccepted(Circle $circle, FederatedLink $link) {
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...
558
		$event = $this->generateEvent('circles_as_moderator');
559
		$event->setSubject(
560
			'link_request_accepted',
561
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
562
		);
563
564
		$this->publishEvent(
565
			$event, $this->membersRequest->forceGetMembers(
566
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
567
		)
568
		);
569
	}
570
571
572
	/**
573
	 * onLinkUp()
574
	 *
575
	 * Called when a link is Up and Running.
576
	 * Broadcast an activity to the moderators of the circle.
577
	 *
578
	 * @param Circle $circle
579
	 * @param FederatedLink $link
580
	 */
581 View Code Duplication
	public function onLinkRequestAccepting(Circle $circle, FederatedLink $link) {
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...
582
		$event = $this->generateEvent('circles_as_moderator');
583
		$event->setSubject(
584
			'link_request_accepting',
585
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
586
		);
587
588
		$this->publishEvent(
589
			$event, $this->membersRequest->forceGetMembers(
590
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
591
		)
592
		);
593
	}
594
595
596
	/**
597
	 * onLinkUp()
598
	 *
599
	 * Called when a link is Up and Running.
600
	 * Broadcast an activity to the moderators of the circle.
601
	 *
602
	 * @param Circle $circle
603
	 * @param FederatedLink $link
604
	 */
605 View Code Duplication
	public function onLinkUp(Circle $circle, FederatedLink $link) {
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...
606
		$event = $this->generateEvent('circles_as_moderator');
607
		$event->setSubject(
608
			'link_up',
609
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
610
		);
611
612
		$this->publishEvent(
613
			$event, $this->membersRequest->forceGetMembers(
614
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
615
		)
616
		);
617
	}
618
619
620
	/**
621
	 * onLinkDown()
622
	 *
623
	 * Called when a link is closed (usually by remote).
624
	 * Broadcast an activity to the moderators of the circle.
625
	 *
626
	 * @param Circle $circle
627
	 * @param FederatedLink $link
628
	 */
629 View Code Duplication
	public function onLinkDown(Circle $circle, FederatedLink $link) {
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...
630
		$event = $this->generateEvent('circles_as_moderator');
631
		$event->setSubject(
632
			'link_down',
633
			['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
634
		);
635
636
		$this->publishEvent(
637
			$event, $this->membersRequest->forceGetMembers(
638
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
639
		)
640
		);
641
	}
642
643
644
	/**
645
	 * onLinkRemove()
646
	 *
647
	 * Called when a link is removed.
648
	 * Subject is based on the current status of the Link.
649
	 * Broadcast an activity to the moderators of the circle.
650
	 *
651
	 * @param Circle $circle
652
	 * @param FederatedLink $link
653
	 */
654
	public function onLinkRemove(Circle $circle, FederatedLink $link) {
655
		$event = $this->generateEvent('circles_as_moderator');
656
657
		if ($link->getStatus() === FederatedLink::STATUS_LINK_DOWN) {
658
			return;
659
		}
660
661
		$subject = 'link_remove';
662
		if ($link->getStatus() === FederatedLink::STATUS_LINK_REQUESTED) {
663
			$subject = 'link_request_removed';
664
		} elseif ($link->getStatus() === FederatedLink::STATUS_REQUEST_SENT) {
665
			$subject = 'link_request_canceling';
666
		}
667
668
		$event->setSubject(
669
			$subject, ['circle' => $circle->getJson(false, true), 'link' => $link->getJson()]
670
		);
671
672
		$this->publishEvent(
673
			$event, $this->membersRequest->forceGetMembers(
674
			$link->getCircleId(), Member::LEVEL_MODERATOR, true
675
		)
676
		);
677
	}
678
679
680
	/**
681
	 * generateEvent()
682
	 * Create an Activity Event with the basic settings for the app.
683
	 *
684
	 * @param $type
685
	 *
686
	 * @return \OCP\Activity\IEvent
687
	 */
688
	private function generateEvent($type) {
689
		$event = $this->activityManager->generateEvent();
690
		$event->setApp('circles')
691
			  ->setType($type)
692
			  ->setAuthor($this->userId);
693
694
		return $event;
695
	}
696
697
698
	/**
699
	 * Publish the event to the users.
700
	 *
701
	 * @param IEvent $event
702
	 * @param array $users
703
	 */
704
	private function publishEvent(IEvent $event, array $users) {
705
		foreach ($users AS $user) {
706
			if ($user instanceof IUser) {
707
				$userId = $user->getUID();
708
			} else if ($user instanceof Member) {
709
				$userId = $user->getUserId();
710
			} else {
711
				continue;
712
			}
713
714
			$event->setAffectedUser($userId);
715
			$this->activityManager->publish($event);
716
		}
717
	}
718
719
720
}