-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hoang Pham <[email protected]>
- Loading branch information
Showing
15 changed files
with
342 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Tables\Helper; | ||
|
||
use OCA\Circles\CirclesManager; | ||
use OCA\Circles\Model\Member; | ||
use OCA\Circles\Model\Probes\CircleProbe; | ||
use OCP\App\IAppManager; | ||
use OCP\Server; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @psalm-suppress UndefinedClass | ||
*/ | ||
class CircleHelper { | ||
private LoggerInterface $logger; | ||
private bool $circlesEnabled; | ||
private ?CirclesManager $circlesManager; | ||
|
||
/** | ||
* @psalm-suppress UndefinedClass | ||
*/ | ||
public function __construct( | ||
LoggerInterface $logger, | ||
IAppManager $appManager, | ||
?CirclesManager $circlesManager = null | ||
) { | ||
$this->logger = $logger; | ||
$this->circlesEnabled = $appManager->isEnabledForUser('circles'); | ||
if ($this->circlesEnabled) { | ||
try { | ||
$this->circlesManager = $circlesManager ?? Server::get(CirclesManager::class); | ||
} catch (Throwable $e) { | ||
$this->logger->warning('Failed to get CirclesManager: ' . $e->getMessage()); | ||
$this->circlesManager = null; | ||
$this->circlesEnabled = false; | ||
} | ||
} else { | ||
$this->circlesManager = null; | ||
} | ||
} | ||
|
||
public function isCirclesEnabled(): bool { | ||
return $this->circlesEnabled; | ||
} | ||
|
||
public function getCircleDisplayName(string $circleId, string $userId): string { | ||
if (!$this->circlesEnabled) { | ||
return $circleId; | ||
} | ||
|
||
try { | ||
$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); | ||
$this->circlesManager->startSession($federatedUser); | ||
|
||
$circle = $this->circlesManager->getCircle($circleId); | ||
return $circle ? ($circle->getDisplayName() ?: $circleId) : $circleId; | ||
} catch (Throwable $e) { | ||
$this->logger->warning('Failed to get circle display name: ' . $e->getMessage(), [ | ||
'circleId' => $circleId, | ||
'userId' => $userId | ||
]); | ||
return $circleId; | ||
} | ||
} | ||
|
||
public function getUserCircles(string $userId): array { | ||
if (!$this->circlesEnabled) { | ||
return []; | ||
} | ||
|
||
try { | ||
$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER); | ||
$this->circlesManager->startSession($federatedUser); | ||
$probe = new CircleProbe(); | ||
$probe->mustBeMember(); | ||
return $this->circlesManager->getCircles($probe); | ||
} catch (Throwable $e) { | ||
$this->logger->warning('Failed to get user circles: ' . $e->getMessage()); | ||
return []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.