Completed
Push — master ( c47ca2...c47ca2 )
by Maxence
12s
created

EventsService::dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
725
		//	$event->setAuthor($this->userId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
726
		}
727
728
		return $event;
729
	}
730
731
732
	/**
733
	 * Publish the event to the users.
734
	 *
735
	 * @param IEvent $event
736
	 * @param array $users
737
	 */
738
	private function publishEvent(IEvent $event, array $users) {
739
		foreach ($users AS $user) {
740
			if ($user instanceof IUser) {
741
				$userId = $user->getUID();
742
			} else if ($user instanceof Member) {
743
				$userId = $user->getUserId();
744
			} else {
745
				continue;
746
			}
747
748
			$event->setAffectedUser($userId);
749
			$this->activityManager->publish($event);
750
		}
751
	}
752
	
753
	private function dispatch($context, $arguments) {
754
		\OC::$server->getEventDispatcher()->dispatch($context, new GenericEvent(null,$arguments));
755
	}
756
757
}