-
Notifications
You must be signed in to change notification settings - Fork 25
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 <hoangmaths96@gmail.com>
- Loading branch information
Showing
17 changed files
with
354 additions
and
60 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
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,87 @@ | ||
<?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 | ||
) { | ||
$this->logger = $logger; | ||
$this->circlesEnabled = $appManager->isEnabledForUser('circles'); | ||
$this->circlesManager = null; | ||
|
||
if ($this->circlesEnabled) { | ||
try { | ||
$this->circlesManager = Server::get(CirclesManager::class); | ||
} catch (Throwable $e) { | ||
$this->logger->warning('Failed to get CirclesManager: ' . $e->getMessage()); | ||
$this->circlesEnabled = false; | ||
} | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.