|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\Tables\Helper; |
| 9 | + |
| 10 | +use OCA\Circles\CirclesManager; |
| 11 | +use OCA\Circles\Model\Member; |
| 12 | +use OCA\Circles\Model\Probes\CircleProbe; |
| 13 | +use OCP\App\IAppManager; |
| 14 | +use OCP\Server; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | +use Throwable; |
| 17 | + |
| 18 | +/** |
| 19 | + * @psalm-suppress UndefinedClass |
| 20 | + */ |
| 21 | +class CircleHelper { |
| 22 | + private bool $circlesEnabled; |
| 23 | + private ?CirclesManager $circlesManager; |
| 24 | + |
| 25 | + /** |
| 26 | + * @psalm-suppress UndefinedClass |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + private LoggerInterface $logger, |
| 30 | + IAppManager $appManager |
| 31 | + ) { |
| 32 | + $this->circlesEnabled = $appManager->isEnabledForUser('circles'); |
| 33 | + $this->circlesManager = null; |
| 34 | + |
| 35 | + if ($this->circlesEnabled) { |
| 36 | + try { |
| 37 | + $this->circlesManager = Server::get(CirclesManager::class); |
| 38 | + } catch (Throwable $e) { |
| 39 | + $this->logger->warning('Failed to get CirclesManager: ' . $e->getMessage()); |
| 40 | + $this->circlesEnabled = false; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public function isCirclesEnabled(): bool { |
| 46 | + return $this->circlesEnabled; |
| 47 | + } |
| 48 | + |
| 49 | + public function getCircleDisplayName(string $circleId, string $userId): string { |
| 50 | + if (!$this->circlesEnabled) { |
| 51 | + return $circleId; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); |
| 56 | + $this->circlesManager->startSession($federatedUser); |
| 57 | + |
| 58 | + $circle = $this->circlesManager->getCircle($circleId); |
| 59 | + return $circle ? ($circle->getDisplayName() ?: $circleId) : $circleId; |
| 60 | + } catch (Throwable $e) { |
| 61 | + $this->logger->warning('Failed to get circle display name: ' . $e->getMessage(), [ |
| 62 | + 'circleId' => $circleId, |
| 63 | + 'userId' => $userId |
| 64 | + ]); |
| 65 | + return $circleId; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public function getUserCircles(string $userId): array { |
| 70 | + if (!$this->circlesEnabled) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); |
| 76 | + $this->circlesManager->startSession($federatedUser); |
| 77 | + $probe = new CircleProbe(); |
| 78 | + $probe->mustBeMember(); |
| 79 | + return $this->circlesManager->getCircles($probe); |
| 80 | + } catch (Throwable $e) { |
| 81 | + $this->logger->warning('Failed to get user circles: ' . $e->getMessage()); |
| 82 | + return []; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments