Completed
Push — master ( 138f00...e3741d )
by Maxence
16s queued 10s
created

MembersService::addMassiveMails()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2568
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
/**
3
 * Circles - bring cloud-users closer
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
30
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
31
use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException;
32
use daita\MySmallPhpTools\Model\Nextcloud\NC19Request;
33
use daita\MySmallPhpTools\Model\Request;
34
use daita\MySmallPhpTools\Traits\Nextcloud\TNC19Request;
35
use daita\MySmallPhpTools\Traits\TArrayTools;
36
use Exception;
37
use OC;
38
use OC\User\NoUserException;
39
use OCA\Circles\Circles\FileSharingBroadcaster;
40
use OCA\Circles\Db\AccountsRequest;
41
use OCA\Circles\Db\CirclesRequest;
42
use OCA\Circles\Db\MembersRequest;
43
use OCA\Circles\Db\SharesRequest;
44
use OCA\Circles\Db\TokensRequest;
45
use OCA\Circles\Exceptions\CircleDoesNotExistException;
46
use OCA\Circles\Exceptions\CircleTypeNotValidException;
47
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
48
use OCA\Circles\Exceptions\EmailAccountInvalidFormatException;
49
use OCA\Circles\Exceptions\GroupDoesNotExistException;
50
use OCA\Circles\Exceptions\GSStatusException;
51
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
52
use OCA\Circles\Exceptions\MemberCantJoinCircleException;
53
use OCA\Circles\Exceptions\MemberDoesNotExistException;
54
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
55
use OCA\Circles\Model\Circle;
56
use OCA\Circles\Model\GlobalScale\GSEvent;
57
use OCA\Circles\Model\Member;
58
use OCP\IL10N;
59
use OCP\IUserManager;
60
61
62
/**
63
 * Class MembersService
64
 *
65
 * @package OCA\Circles\Service
66
 */
67
class MembersService {
68
69
70
	use TNC19Request;
71
	use TArrayTools;
72
73
74
	/** @var string */
75
	private $userId;
76
77
	/** @var IL10N */
78
	private $l10n;
79
80
	/** @var IUserManager */
81
	private $userManager;
82
83
	/** @var ConfigService */
84
	private $configService;
85
86
	/** @var CirclesRequest */
87
	private $circlesRequest;
88
89
	/** @var MembersRequest */
90
	private $membersRequest;
91
92
	/** @var AccountsRequest */
93
	private $accountsRequest;
94
95
	/** @var SharesRequest */
96
	private $sharesRequest;
97
98
	/** @var TokensRequest */
99
	private $tokensRequest;
100
101
	/** @var EventsService */
102
	private $eventsService;
103
104
	/** @var GSUpstreamService */
105
	private $gsUpstreamService;
106
107
	/** @var FileSharingBroadcaster */
108
	private $fileSharingBroadcaster;
109
110
	/** @var MiscService */
111
	private $miscService;
112
113
	/**
114
	 * MembersService constructor.
115
	 *
116
	 * @param string $userId
117
	 * @param IL10N $l10n
118
	 * @param IUserManager $userManager
119
	 * @param ConfigService $configService
120
	 * @param CirclesRequest $circlesRequest
121
	 * @param MembersRequest $membersRequest
122
	 * @param AccountsRequest $accountsRequest
123
	 * @param SharesRequest $sharesRequest
124
	 * @param TokensRequest $tokensRequest
125
	 * @param EventsService $eventsService
126
	 * @param GSUpstreamService $gsUpstreamService
127
	 * @param FileSharingBroadcaster $fileSharingBroadcaster
128
	 * @param MiscService $miscService
129
	 */
130
	public function __construct(
131
		$userId, IL10N $l10n, IUserManager $userManager, ConfigService $configService,
132
		CirclesRequest $circlesRequest, MembersRequest $membersRequest, AccountsRequest $accountsRequest,
133
		SharesRequest $sharesRequest, TokensRequest $tokensRequest, EventsService $eventsService,
134
		GSUpstreamService $gsUpstreamService, FileSharingBroadcaster $fileSharingBroadcaster,
135
		MiscService $miscService
136
	) {
137
		$this->userId = $userId;
138
		$this->l10n = $l10n;
139
		$this->userManager = $userManager;
140
		$this->configService = $configService;
141
		$this->circlesRequest = $circlesRequest;
142
		$this->membersRequest = $membersRequest;
143
		$this->accountsRequest = $accountsRequest;
144
		$this->sharesRequest = $sharesRequest;
145
		$this->tokensRequest = $tokensRequest;
146
		$this->eventsService = $eventsService;
147
		$this->gsUpstreamService = $gsUpstreamService;
148
		$this->fileSharingBroadcaster = $fileSharingBroadcaster;
149
		$this->miscService = $miscService;
150
	}
151
152
153
	/**
154
	 * addMember();
155
	 *
156
	 * add a new member to a circle.
157
	 *
158
	 * @param string $circleUniqueId
159
	 * @param $ident
160
	 * @param int $type
161
	 * @param string $instance
162
	 *
163
	 * @param bool $force
164
	 *
165
	 * @return array
166
	 * @throws Exception
167
	 */
168
	public function addMember($circleUniqueId, $ident, $type, string $instance, bool $force = false) {
169
		if ($force === true) {
170
			$circle = $this->circlesRequest->forceGetCircle($circleUniqueId);
171
		} else {
172
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
173
			$circle->getHigherViewer()
174
				   ->hasToBeModerator();
175
		}
176
177
		$curr = $this->membersRequest->getMembers($circle->getUniqueId(), $circle->getHigherViewer(), $force);
178
179
		$new = $this->addMassiveMembers($circle, $ident, $type);
180
		if (empty($new)) {
181
			$new = [$this->addSingleMember($circle, $ident, $type, $instance, $force)];
182
		}
183
184
		return $this->filterDuplicate($curr, $new);
185
	}
186
187
188
	/**
189
	 * add a single member to a circle.
190
	 *
191
	 * @param Circle $circle
192
	 * @param string $ident
193
	 * @param int $type
194
	 *
195
	 * @param string $instance
196
	 * @param bool $force
197
	 *
198
	 * @return Member
199
	 * @throws EmailAccountInvalidFormatException
200
	 * @throws NoUserException
201
	 * @throws Exception
202
	 */
203
	private function addSingleMember(Circle $circle, $ident, $type, $instance = '', bool $force = false
204
	): Member {
205
		$this->verifyIdentBasedOnItsType($ident, $type, $instance);
206
		$this->verifyIdentContact($ident, $type);
207
208
		$member = $this->membersRequest->getFreshNewMember($circle->getUniqueId(), $ident, $type, $instance);
209
		$this->updateCachedName($member);
210
211
		$event = new GSEvent(GSEvent::MEMBER_ADD, false, $force);
212
		$event->setSeverity(GSEvent::SEVERITY_HIGH);
213
		$event->setAsync(true);
214
		$event->setCircle($circle);
215
		$event->setMember($member);
216
		$this->gsUpstreamService->newEvent($event);
217
218
		$new = $event->getMember();
219
		$new->setJoined($this->l10n->t('now'));
220
		if ($circle->getType() === Circle::CIRCLES_CLOSED) {
221
//			$new->setLevel(Member::LEVEL_MEMBER);
222
			$new->setStatus(Member::STATUS_INVITED);
223
		} else {
224
//			$new->setLevel(Member::LEVEL_MEMBER);
225
			$new->setStatus(Member::STATUS_MEMBER);
226
		}
227
228
		if ($new->getInstance() === $this->configService->getLocalCloudId()) {
229
			$new->setInstance('');
230
		}
231
232
		return $new;
233
	}
234
235
236
	/**
237
	 * add a bunch of users to a circle based on the type of the 'bunch'
238
	 *
239
	 * @param Circle $circle
240
	 * @param string $ident
241
	 * @param int $type
242
	 *
243
	 * @return Member[]
244
	 * @throws Exception
245
	 */
246
	private function addMassiveMembers(Circle $circle, $ident, $type): array {
247
		if ($type === Member::TYPE_GROUP) {
248
			return $this->addGroupMembers($circle, $ident);
249
		}
250
251
		if ($type === Member::TYPE_USER) {
252
			return $this->addMassiveMails($circle, $ident);
253
		}
254
255
		return [];
256
	}
257
258
259
	/**
260
	 * add a new member based on its type.
261
	 *
262
	 * @param Circle $circle
263
	 * @param Member $member
264
	 *
265
	 * @throws CircleTypeNotValidException
266
	 * @throws MemberCantJoinCircleException
267
	 */
268
	public function addMemberBasedOnItsType(Circle $circle, Member $member) {
269
		$this->addLocalMember($circle, $member);
270
		$this->addEmailAddress($member);
271
		$this->addContact($member);
272
	}
273
274
275
	/**
276
	 * @param Circle $circle
277
	 * @param Member $member
278
	 *
279
	 * @throws CircleTypeNotValidException
280
	 * @throws MemberCantJoinCircleException
281
	 */
282
	private function addLocalMember(Circle $circle, Member $member) {
283
284
		if ($member->getType() !== Member::TYPE_USER) {
285
			return;
286
		}
287
288
		$member->inviteToCircle($circle->getType());
289
290
		if ($circle->getType() === Circle::CIRCLES_CLOSED && $this->configService->isInvitationSkipped()) {
291
			$member->joinCircle($circle->getType());
292
		}
293
	}
294
295
296
	/**
297
	 * add mail address as contact.
298
	 *
299
	 * @param Member $member
300
	 */
301
	private function addEmailAddress(Member $member) {
302
303
		if ($member->getType() !== Member::TYPE_MAIL) {
304
			return;
305
		}
306
307
		$member->addMemberToCircle();
308
	}
309
310
311
	/**
312
	 * // TODO - check this on GS setup
313
	 * Add contact as member.
314
	 *
315
	 * @param Member $member
316
	 */
317
	private function addContact(Member $member) {
318
319
		if ($member->getType() !== Member::TYPE_CONTACT) {
320
			return;
321
		}
322
323
		$member->addMemberToCircle();
324
	}
325
326
327
	/**
328
	 * // TODO - check this on GS setup
329
	 * Verify the availability of an ident, based on its type.
330
	 *
331
	 * @param string $ident
332
	 * @param int $type
333
	 * @param string $instance
334
	 *
335
	 * @throws EmailAccountInvalidFormatException
336
	 * @throws NoUserException
337
	 */
338
	public function verifyIdentBasedOnItsType(&$ident, $type, string $instance = '') {
339
		if ($instance === $this->configService->getLocalCloudId()) {
340
			$instance = '';
341
		}
342
343
		$this->verifyIdentLocalMember($ident, $type, $instance);
344
		$this->verifyIdentEmailAddress($ident, $type);
345
//		$this->verifyIdentContact($ident, $type);
346
	}
347
348
349
	/**
350
	 * Verify if a local account is valid.
351
	 *
352
	 * @param $ident
353
	 * @param $type
354
	 *
355
	 * @param string $instance
356
	 *
357
	 * @throws NoUserException
358
	 */
359
	private function verifyIdentLocalMember(&$ident, $type, string $instance = '') {
360
		if ($type !== Member::TYPE_USER) {
361
			return;
362
		}
363
364
		if ($instance === '') {
365
			try {
366
				$ident = $this->miscService->getRealUserId($ident);
367
			} catch (NoUserException $e) {
0 ignored issues
show
Bug introduced by
The class OC\User\NoUserException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
368
				throw new NoUserException($this->l10n->t("This user does not exist"));
369
			}
370
		}
371
	}
372
373
374
	/**
375
	 * Verify if a mail have a valid format.
376
	 *
377
	 * @param string $ident
378
	 * @param int $type
379
	 *
380
	 * @throws EmailAccountInvalidFormatException
381
	 */
382
	private function verifyIdentEmailAddress(string $ident, int $type) {
383
		if ($type !== Member::TYPE_MAIL) {
384
			return;
385
		}
386
387
		if ($this->configService->isAccountOnly()) {
388
			throw new EmailAccountInvalidFormatException(
389
				$this->l10n->t('You cannot add a mail address as member of your Circle')
390
			);
391
		}
392
393
		if (!filter_var($ident, FILTER_VALIDATE_EMAIL)) {
394
			throw new EmailAccountInvalidFormatException(
395
				$this->l10n->t('Email format is not valid')
396
			);
397
		}
398
	}
399
400
401
	/**
402
	 * Verify if a contact exist in current user address books.
403
	 *
404
	 * @param $ident
405
	 * @param $type
406
	 *
407
	 * @throws NoUserException
408
	 * @throws EmailAccountInvalidFormatException
409
	 */
410
	private function verifyIdentContact(&$ident, $type) {
411
		if ($type !== Member::TYPE_CONTACT) {
412
			return;
413
		}
414
415
		if ($this->configService->isAccountOnly()) {
416
			throw new EmailAccountInvalidFormatException(
417
				$this->l10n->t('You cannot add a contact as member of your Circle')
418
			);
419
		}
420
421
		$tmpContact = $this->userId . ':' . $ident;
422
		$result = MiscService::getContactData($tmpContact);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Service\MiscService::getContactData() has been deprecated.

This method has been deprecated.

Loading history...
423
		if (empty($result)) {
424
			throw new NoUserException($this->l10n->t("This contact is not available"));
425
		}
426
427
		$ident = $tmpContact;
428
	}
429
430
431
	/**
432
	 * @param Circle $circle
433
	 * @param string $groupId
434
	 *
435
	 * @return Member[]
436
	 * @throws Exception
437
	 */
438
	private function addGroupMembers(Circle $circle, $groupId): array {
439
		$group = OC::$server->getGroupManager()
440
							->get($groupId);
441
		if ($group === null) {
442
			throw new GroupDoesNotExistException($this->l10n->t('This group does not exist'));
443
		}
444
445
		$members = [];
446
		foreach ($group->getUsers() as $user) {
447
			try {
448
				$members[] = $this->addSingleMember($circle, $user->getUID(), Member::TYPE_USER);
449
			} catch (MemberAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
450
			} catch (Exception $e) {
451
				throw $e;
452
			}
453
		}
454
455
		return $members;
456
	}
457
458
459
	/**
460
	 * // TODO - check this on GS setup
461
	 *
462
	 * @param Circle $circle
463
	 * @param string $mails
464
	 *
465
	 * @return Member[]
466
	 */
467
	private function addMassiveMails(Circle $circle, $mails): array {
468
469
		$mails = trim($mails);
470
		if (substr($mails, 0, 6) !== 'mails:') {
471
			return [];
472
		}
473
474
		$mails = substr($mails, 6);
475
		$members = [];
476
		foreach (explode(' ', $mails) as $mail) {
477
			if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
478
				continue;
479
			}
480
481
			try {
482
				$members[] = $this->addMember($circle->getUniqueId(), $mail, Member::TYPE_MAIL, '');
483
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
484
			}
485
		}
486
487
		return $members;
488
	}
489
490
491
	/**
492
	 * getMember();
493
	 *
494
	 * Will return any data of a user related to a circle (as a Member). User can be a 'non-member'
495
	 * Viewer needs to be at least Member of the Circle
496
	 *
497
	 * @param $circleId
498
	 * @param $userId
499
	 * @param $type
500
	 * @param bool $forceAll
501
	 *
502
	 * @return Member
503
	 * @throws CircleDoesNotExistException
504
	 * @throws ConfigNoCircleAvailableException
505
	 * @throws MemberDoesNotExistException
506
	 */
507
	public function getMember($circleId, $userId, $type, $forceAll = false) {
508
		if (!$forceAll) {
509
			$this->circlesRequest->getCircle($circleId, $this->userId)
510
								 ->getHigherViewer()
511
								 ->hasToBeMember();
512
		}
513
514
		$member = $this->membersRequest->forceGetMember($circleId, $userId, $type);
515
		$member->setNote('');
516
517
		return $member;
518
	}
519
520
521
	/**
522
	 * @param string $memberId
523
	 *
524
	 * @return Member
525
	 * @throws MemberDoesNotExistException
526
	 */
527
	public function getMemberById(string $memberId): Member {
528
		return $this->membersRequest->forceGetMemberById($memberId);
529
	}
530
531
532
	/**
533
	 * @param Member $member
534
	 *
535
	 * @throws Exception
536
	 */
537
	public function updateMember(Member $member) {
538
		$event = new GSEvent(GSEvent::MEMBER_UPDATE);
539
		$event->setMember($member);
540
		$event->setCircle($this->getCircleFromMembership($member));
541
542
		$this->gsUpstreamService->newEvent($event);
543
	}
544
545
546
	/**
547
	 * @param string $circleUniqueId
548
	 * @param string $name
549
	 * @param int $type
550
	 * @param string $instance
551
	 * @param int $level
552
	 * @param bool $force
553
	 *
554
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Member[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
555
	 * @throws CircleDoesNotExistException
556
	 * @throws CircleTypeNotValidException
557
	 * @throws ConfigNoCircleAvailableException
558
	 * @throws MemberDoesNotExistException
559
	 * @throws Exception
560
	 */
561
	public function levelMember(
562
		string $circleUniqueId, string $name, int $type, string $instance, int $level, bool $force = false
563
	) {
564
		$level = (int)$level;
565
		if ($force === false) {
566
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
567
		} else {
568
			$circle = $this->circlesRequest->forceGetCircle($circleUniqueId);
569
		}
570
571
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
572
			throw new CircleTypeNotValidException(
573
				$this->l10n->t('You cannot edit level in a personal circle')
574
			);
575
		}
576
577
		$member = $this->membersRequest->forceGetMember($circle->getUniqueId(), $name, $type, $instance);
578
		if ($member->getLevel() !== $level) {
579
			$event = new GSEvent(GSEvent::MEMBER_LEVEL, false, $force);
580
			$event->setCircle($circle);
581
582
			$event->getData()
583
				  ->sInt('level', $level);
584
			$event->setMember($member);
585
			$this->gsUpstreamService->newEvent($event);
586
		}
587
588
		if ($force === false) {
589
			return $this->membersRequest->getMembers(
590
				$circle->getUniqueId(), $circle->getHigherViewer()
591
			);
592
		} else {
593
			return $this->membersRequest->forceGetMembers($circle->getUniqueId());
594
		}
595
596
	}
597
598
599
	/**
600
	 * @param string $circleUniqueId
601
	 * @param string $name
602
	 * @param int $type
603
	 * @param string $instance
604
	 * @param bool $force
605
	 *
606
	 * @return Member[]
607
	 * @throws CircleDoesNotExistException
608
	 * @throws ConfigNoCircleAvailableException
609
	 * @throws MemberDoesNotExistException
610
	 * @throws MemberIsNotModeratorException
611
	 * @throws Exception
612
	 */
613
	public function removeMember(
614
		string $circleUniqueId, string $name, int $type, string $instance, bool $force = false
615
	): array {
616
617
		if ($force === false) {
618
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
619
			$circle->getHigherViewer()
620
				   ->hasToBeModerator();
621
		} else {
622
			$circle = $this->circlesRequest->forceGetCircle($circleUniqueId);
623
		}
624
625
		$member = $this->membersRequest->forceGetMember($circleUniqueId, $name, $type, $instance);
626
627
		$event = new GSEvent(GSEvent::MEMBER_REMOVE, false, $force);
628
		$event->setCircle($circle);
629
		$event->setMember($member);
630
		$this->gsUpstreamService->newEvent($event);
631
632
		if ($force === false) {
633
			return $this->membersRequest->getMembers(
634
				$circle->getUniqueId(), $circle->getHigherViewer()
635
			);
636
		} else {
637
			return $this->membersRequest->forceGetMembers($circle->getUniqueId());
638
		}
639
	}
640
641
642
	/**
643
	 * When a user is removed, remove him from all Circles
644
	 *
645
	 * @param string $userId
646
	 *
647
	 * @throws Exception
648
	 */
649
	public function onUserRemoved(string $userId) {
650
		$event = new GSEvent(GSEvent::USER_DELETED, true, true);
651
652
		$member = new Member($userId);
653
		$event->setMember($member);
654
		$event->getData()
655
			  ->s('userId', $userId);
656
657
		$this->gsUpstreamService->newEvent($event);
658
	}
659
660
661
	/**
662
	 * @param Member $member
663
	 * @param bool $fresh
664
	 */
665
	public function updateCachedName(Member $member, bool $fresh = true) {
666
		try {
667
			$cachedName = '';
668
			if ($member->getType() === Member::TYPE_USER) {
669
				$cachedName = $this->getUserDisplayName($member->getUserId(), $fresh);
670
			}
671
672
			if ($member->getType() === Member::TYPE_CONTACT) {
673
				$cachedName = $this->miscService->getContactDisplayName($member->getUserId());
674
			}
675
676
			if ($cachedName !== '') {
677
				$member->setCachedName($cachedName);
678
			}
679
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
680
		}
681
	}
682
683
684
	/**
685
	 * @param Circle $circle
686
	 */
687
	public function updateCachedFromCircle(Circle $circle) {
688
		$members = $this->membersRequest->forceGetMembers(
689
			$circle->getUniqueId(), Member::LEVEL_NONE, Member::TYPE_USER
690
		);
691
692
		foreach ($members as $member) {
693
			$this->updateCachedName($member, false);
694
			$this->membersRequest->updateMemberInfo($member);
695
		}
696
	}
697
698
699
	/**
700
	 * @param string $ident
701
	 * @param bool $fresh
702
	 *
703
	 * @return string
704
	 * @throws GSStatusException
705
	 */
706
	public function getUserDisplayName(string $ident, bool $fresh = false): string {
707
		if ($this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
708
			return $this->getGlobalScaleUserDisplayName($ident);
709
		}
710
711
		if (!$fresh) {
712
			try {
713
				$account = $this->accountsRequest->getFromUserId($ident);
714
715
				return $this->get('displayName', $account);
716
			} catch (MemberDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
717
			}
718
		}
719
720
		$user = $this->userManager->get($ident);
721
		if ($user === null) {
722
			return '';
723
		}
724
725
		return $user->getDisplayName();
726
	}
727
728
729
	/**
730
	 * @param string $ident
731
	 *
732
	 * @return string
733
	 * @throws GSStatusException
734
	 */
735
	private function getGlobalScaleUserDisplayName(string $ident): string {
736
		$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
737
738
		$request = new NC19Request('/users', Request::TYPE_GET);
739
		$this->configService->configureRequest($request);
740
		$request->setProtocols(['https', 'http']);
741
		$request->addData('search', $ident);
742
		$request->addData('exact', '1');
743
		$request->setAddressFromUrl($lookup);
744
745
		try {
746
			$users = $this->retrieveJson($request);
747
748
			return $this->get('name.value', $users);
749
		} catch (
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
750
		RequestNetworkException |
751
		RequestResultNotJsonException $e
752
		) {
753
		}
754
755
		return '';
756
	}
757
758
759
	/**
760
	 * @param Member $member
761
	 *
762
	 * @return Circle
763
	 * @throws CircleDoesNotExistException
764
	 */
765
	public function getCircleFromMembership(Member $member): Circle {
766
		return $this->circlesRequest->forceGetCircle($member->getCircleId());
767
	}
768
769
770
	/**
771
	 * @param Member[] $curr
772
	 * @param Member[] $new
773
	 *
774
	 * @return array
775
	 */
776
	private function filterDuplicate(array $curr, array $new): array {
777
		$base = [];
778
		foreach ($curr as $currMember) {
779
			$known = false;
780
			foreach ($new as $newMember) {
781
				if ($newMember->getMemberId() === $currMember->getMemberId()) {
782
					$known = true;
783
				}
784
			}
785
			if (!$known) {
786
				$base[] = $currMember;
787
			}
788
		}
789
790
		return array_merge($base, $new);
791
	}
792
793
794
}
795
796