diff --git a/lib/IRelatedResourceProvider.php b/lib/IRelatedResourceProvider.php index 9d55aec9..8564da20 100644 --- a/lib/IRelatedResourceProvider.php +++ b/lib/IRelatedResourceProvider.php @@ -31,6 +31,7 @@ namespace OCA\RelatedResources; +use OCA\Circles\CirclesManager; use OCA\Circles\Model\FederatedUser; interface IRelatedResourceProvider { @@ -46,11 +47,12 @@ public function loadWeightCalculator(): array; /** * convert item to IRelatedResource, based on available shares * + * @param CirclesManager $circlesManager * @param string $itemId * * @return IRelatedResource|null */ - public function getRelatedFromItem(string $itemId): ?IRelatedResource; + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource; /** * returns itemIds (as string) the entity have access to @@ -64,9 +66,10 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array; /** * improve a related resource before sending result to front-end. * + * @param CirclesManager $circlesManager * @param IRelatedResource $entry * * @return void */ - public function improveRelatedResource(IRelatedResource $entry): void; + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void; } diff --git a/lib/RelatedResourceProviders/CalendarRelatedResourceProvider.php b/lib/RelatedResourceProviders/CalendarRelatedResourceProvider.php index c4cc3614..ee0fbea5 100644 --- a/lib/RelatedResourceProviders/CalendarRelatedResourceProvider.php +++ b/lib/RelatedResourceProviders/CalendarRelatedResourceProvider.php @@ -57,7 +57,6 @@ class CalendarRelatedResourceProvider implements IRelatedResourceProvider { private IURLGenerator $urlGenerator; private IL10N $l10n; private CalendarShareRequest $calendarShareRequest; - private ?CirclesManager $circlesManager = null; public function __construct( IURLGenerator $urlGenerator, @@ -67,10 +66,6 @@ public function __construct( $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; $this->calendarShareRequest = $calendarShareRequest; - try { - $this->circlesManager = Server::get(CirclesManager::class); - } catch (ContainerExceptionInterface | AutoloadNotAllowedException $e) { - } } public function getProviderId(): string { @@ -86,11 +81,7 @@ public function loadWeightCalculator(): array { * * @return IRelatedResource|null */ - public function getRelatedFromItem(string $itemId): ?IRelatedResource { - if ($this->circlesManager === null) { - return null; - } - + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource { [$principalUri, $uri] = explode(':', $itemId, 2); $itemId = (int)$itemId; @@ -104,7 +95,7 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { $related = $this->convertToRelatedResource($calendar); if (strtolower(substr($calendar->getCalendarPrincipalUri(), 0, 17)) === 'principals/users/') { $calendarOwner = substr($calendar->getCalendarPrincipalUri(), 17); - $owner = $this->circlesManager->getFederatedUser($calendarOwner, Member::TYPE_USER); + $owner = $circlesManager->getFederatedUser($calendarOwner, Member::TYPE_USER); $related->addToVirtualGroup($owner->getSingleId()); } @@ -114,7 +105,7 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { } catch (Exception $e) { continue; } - $this->processCalendarShare($related, $share); + $this->processCalendarShare($circlesManager, $related, $share); } return $related; @@ -145,7 +136,7 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array { } - public function improveRelatedResource(IRelatedResource $entry): void { + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void { } @@ -202,9 +193,12 @@ private function convertToRelatedResource(Calendar $calendar): IRelatedResource * @param RelatedResource $related * @param CalendarShare $share */ - private function processCalendarShare(RelatedResource $related, CalendarShare $share) { + private function processCalendarShare( + CirclesManager $circlesManager, + RelatedResource $related, + CalendarShare $share) { try { - $participant = $this->circlesManager->getFederatedUser($share->getUser(), $share->getType()); + $participant = $circlesManager->getFederatedUser($share->getUser(), $share->getType()); if ($share->getType() === Member::TYPE_USER) { $related->addToVirtualGroup($participant->getSingleId()); diff --git a/lib/RelatedResourceProviders/DeckRelatedResourceProvider.php b/lib/RelatedResourceProviders/DeckRelatedResourceProvider.php index d9d29774..85f68cb0 100644 --- a/lib/RelatedResourceProviders/DeckRelatedResourceProvider.php +++ b/lib/RelatedResourceProviders/DeckRelatedResourceProvider.php @@ -43,12 +43,9 @@ use OCA\RelatedResources\Model\DeckShare; use OCA\RelatedResources\Model\RelatedResource; use OCA\RelatedResources\Tools\Traits\TArrayTools; -use OCP\AutoloadNotAllowedException; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\Server; use OCP\Share\IShare; -use Psr\Container\ContainerExceptionInterface; class DeckRelatedResourceProvider implements IRelatedResourceProvider { use TArrayTools; @@ -59,8 +56,6 @@ class DeckRelatedResourceProvider implements IRelatedResourceProvider { private IUrlGenerator $urlGenerator; private IL10N $l10n; private DeckRequest $deckSharesRequest; - private ?CirclesManager $circlesManager = null; - public function __construct( IUrlGenerator $urlGenerator, @@ -70,10 +65,6 @@ public function __construct( $this->urlGenerator = $urlGenerator; $this->l10n = $l10n; $this->deckSharesRequest = $deckSharesRequest; - try { - $this->circlesManager = Server::get(CirclesManager::class); - } catch (ContainerExceptionInterface | AutoloadNotAllowedException $e) { - } } public function getProviderId(): string { @@ -90,11 +81,7 @@ public function loadWeightCalculator(): array { * * @return IRelatedResource|null */ - public function getRelatedFromItem(string $itemId): ?IRelatedResource { - if ($this->circlesManager === null) { - return null; - } - + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource { $itemId = (int)$itemId; /** @var DeckBoard $board */ @@ -105,11 +92,11 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { } $related = $this->convertToRelatedResource($board); - $owner = $this->circlesManager->getFederatedUser($board->getOwner(), Member::TYPE_USER); + $owner = $circlesManager->getFederatedUser($board->getOwner(), Member::TYPE_USER); $related->addToVirtualGroup($owner->getSingleId()); foreach ($this->deckSharesRequest->getSharesByBoardId($itemId) as $share) { - $this->processDeckShare($related, $share); + $this->processDeckShare($circlesManager, $related, $share); } return $related; @@ -140,7 +127,7 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array { } - public function improveRelatedResource(IRelatedResource $entry): void { + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void { } private function convertToRelatedResource(DeckBoard $board): IRelatedResource { @@ -181,9 +168,13 @@ private function convertToRelatedResource(DeckBoard $board): IRelatedResource { * @param RelatedResource $related * @param DeckShare $share */ - private function processDeckShare(RelatedResource $related, DeckShare $share) { + private function processDeckShare( + CirclesManager $circlesManager, + RelatedResource $related, + DeckShare $share + ) { try { - $participant = $this->convertDeckShare($share); + $participant = $this->convertDeckShare($circlesManager, $share); if ($share->getRecipientType() === IShare::TYPE_USER) { $related->addToVirtualGroup($participant->getSingleId()); } else { @@ -196,33 +187,20 @@ private function processDeckShare(RelatedResource $related, DeckShare $share) { /** + * @param CirclesManager $circlesManager * @param DeckShare $share * * @return FederatedUser * @throws Exception */ - public function convertDeckShare(DeckShare $share): FederatedUser { - if (is_null($this->circlesManager)) { - throw new Exception('Circles needs to be enabled'); - } - - switch ($share->getRecipientType()) { - case IShare::TYPE_USER: - $type = Member::TYPE_USER; - break; - - case IShare::TYPE_GROUP: - $type = Member::TYPE_GROUP; - break; - - case IShare::TYPE_CIRCLE: - $type = Member::TYPE_SINGLE; - break; - - default: - throw new Exception('unknown deck share type (' . $share->getRecipientType() . ')'); - } - - return $this->circlesManager->getFederatedUser($share->getRecipientId(), $type); + public function convertDeckShare(CirclesManager $circlesManager, DeckShare $share): FederatedUser { + $type = match ($share->getRecipientType()) { + IShare::TYPE_USER => Member::TYPE_USER, + IShare::TYPE_GROUP => Member::TYPE_GROUP, + IShare::TYPE_CIRCLE => Member::TYPE_SINGLE, + default => throw new Exception('unknown deck share type (' . $share->getRecipientType() . ')'), + }; + + return $circlesManager->getFederatedUser($share->getRecipientId(), $type); } } diff --git a/lib/RelatedResourceProviders/FilesRelatedResourceProvider.php b/lib/RelatedResourceProviders/FilesRelatedResourceProvider.php index 3a701129..0a6140a4 100644 --- a/lib/RelatedResourceProviders/FilesRelatedResourceProvider.php +++ b/lib/RelatedResourceProviders/FilesRelatedResourceProvider.php @@ -44,25 +44,19 @@ use OCA\RelatedResources\Model\FilesShare; use OCA\RelatedResources\Model\RelatedResource; use OCA\RelatedResources\Tools\Traits\TArrayTools; -use OCP\AutoloadNotAllowedException; use OCP\Files\InvalidPathException; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\Server; use OCP\Share\IShare; -use Psr\Container\ContainerExceptionInterface; class FilesRelatedResourceProvider implements IRelatedResourceProvider { use TArrayTools; private const PROVIDER_ID = 'files'; - - private ?CirclesManager $circlesManager = null; - public function __construct( private IRootFolder $rootFolder, private IURLGenerator $urlGenerator, @@ -70,10 +64,6 @@ public function __construct( private FilesShareRequest $filesShareRequest, private GroupFoldersRelatedResourceProvider $groupFoldersRRProvider, ) { - try { - $this->circlesManager = Server::get(CirclesManager::class); - } catch (ContainerExceptionInterface|AutoloadNotAllowedException $e) { - } } public function getProviderId(): string { @@ -86,11 +76,7 @@ public function loadWeightCalculator(): array { } - public function getRelatedFromItem(string $itemId): ?IRelatedResource { - if ($this->circlesManager === null) { - return null; - } - + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource { $itemId = (int)$itemId; if ($itemId <= 1) { return null; @@ -98,7 +84,7 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { $related = null; try { - $itemEntries = $this->getItemIdsFromParentPath($itemId); + $itemEntries = $this->getItemIdsFromParentPath($circlesManager, $itemId); } catch (Exception $e) { $itemEntries = [['id' => $itemId]]; } @@ -119,10 +105,10 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { if ($related === null) { $related = $this->convertToRelatedResource($share); } - $this->processShareRecipient($related, $share); + $this->processShareRecipient($circlesManager, $related, $share); } - $related = $this->managerGroupFolders($related, $itemEntries); + $related = $this->managerGroupFolders($circlesManager, $related, $itemEntries); return $related; } @@ -152,8 +138,8 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array { } - public function improveRelatedResource(IRelatedResource $entry): void { - $current = $this->circlesManager->getCurrentFederatedUser(); + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void { + $current = $circlesManager->getCurrentFederatedUser(); if (!$current->isLocal() || $current->getUserType() !== Member::TYPE_USER) { return; } @@ -205,7 +191,11 @@ private function convertToRelatedResource(FilesShare $share): IRelatedResource { /** * @param list $itemEntries */ - private function managerGroupFolders(?IRelatedResource $related, array $itemEntries): ?IRelatedResource { + private function managerGroupFolders( + CirclesManager $circlesManager, + ?IRelatedResource $related, + array $itemEntries + ): ?IRelatedResource { foreach ($itemEntries as $entry) { if (($entry['type'] ?? '') !== 'groupfolder') { continue; @@ -220,7 +210,11 @@ private function managerGroupFolders(?IRelatedResource $related, array $itemEntr $related = $this->groupFoldersRRProvider->convertToRelatedResource($folder); } - $this->groupFoldersRRProvider->processApplicableMap($related, $folder['groups'] ?? []); + $this->groupFoldersRRProvider->processApplicableMap( + $circlesManager, + $related, + $folder['groups'] ?? [] + ); } return $related; @@ -236,8 +230,8 @@ private function managerGroupFolders(?IRelatedResource $related, array $itemEntr * @throws NotPermittedException * @throws NoUserException */ - private function getItemIdsFromParentPath(int $itemId): array { - $current = $this->circlesManager->getCurrentFederatedUser(); + private function getItemIdsFromParentPath(CirclesManager $circlesManager, int $itemId): array { + $current = $circlesManager->getCurrentFederatedUser(); if (!$current->isLocal() || $current->getUserType() !== Member::TYPE_USER) { return [['id' => $itemId]]; } @@ -273,15 +267,21 @@ private function getItemIdsFromParentPath(int $itemId): array { * @param RelatedResource $related * @param FilesShare $share */ - private function processShareRecipient(RelatedResource $related, FilesShare $share) { + private function processShareRecipient( + CirclesManager $circlesManager, + RelatedResource $related, + FilesShare $share + ) { try { $sharedWith = $this->convertShareRecipient( + $circlesManager, $share->getShareType(), $share->getSharedWith() ); if ($share->getShareType() === IShare::TYPE_USER) { $shareCreator = $this->convertShareRecipient( + $circlesManager, IShare::TYPE_USER, $share->getShareCreator() ); @@ -308,28 +308,18 @@ private function processShareRecipient(RelatedResource $related, FilesShare $sha * @return FederatedUser * @throws Exception */ - private function convertShareRecipient(int $shareType, string $sharedWith): FederatedUser { - if (is_null($this->circlesManager)) { - throw new Exception('Circles needs to be enabled'); - } - - switch ($shareType) { - case IShare::TYPE_USER: - $type = Member::TYPE_USER; - break; - - case IShare::TYPE_GROUP: - $type = Member::TYPE_GROUP; - break; - - case IShare::TYPE_CIRCLE: - $type = Member::TYPE_SINGLE; - break; - - default: - throw new Exception('unknown share type (' . $shareType . ')'); - } - - return $this->circlesManager->getFederatedUser($sharedWith, $type); + private function convertShareRecipient( + CirclesManager $circlesManager, + int $shareType, + string $sharedWith + ): FederatedUser { + $type = match ($shareType) { + IShare::TYPE_USER => Member::TYPE_USER, + IShare::TYPE_GROUP => Member::TYPE_GROUP, + IShare::TYPE_CIRCLE => Member::TYPE_SINGLE, + default => throw new Exception('unknown share type (' . $shareType . ')'), + }; + + return $circlesManager->getFederatedUser($sharedWith, $type); } } diff --git a/lib/RelatedResourceProviders/GroupFoldersRelatedResourceProvider.php b/lib/RelatedResourceProviders/GroupFoldersRelatedResourceProvider.php index 49594b85..36b96183 100644 --- a/lib/RelatedResourceProviders/GroupFoldersRelatedResourceProvider.php +++ b/lib/RelatedResourceProviders/GroupFoldersRelatedResourceProvider.php @@ -51,7 +51,6 @@ class GroupFoldersRelatedResourceProvider implements IRelatedResourceProvider { private const PROVIDER_ID = 'groupfolders'; - private ?CirclesManager $circlesManager = null; private ?FolderManager $folderManager = null; /** * @var array>, id: int, mount_point: mixed, quota: int, size: 0}> @@ -65,7 +64,6 @@ public function __construct( private FilesShareRequest $filesShareRequest, ) { try { - $this->circlesManager = Server::get(CirclesManager::class); $this->folderManager = Server::get(FolderManager::class); $this->folders = $this->folderManager->getAllFolders(); } catch (ContainerExceptionInterface|AutoloadNotAllowedException $e) { @@ -80,11 +78,7 @@ public function loadWeightCalculator(): array { return []; } - public function getRelatedFromItem(string $itemId): ?IRelatedResource { - if ($this->circlesManager === null) { - return null; - } - + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource { $itemId = (int)$itemId; if ($itemId < 1) { return null; @@ -98,7 +92,7 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { } $related = $this->convertToRelatedResource($folder); - $this->processApplicableMap($related, $folder['groups'] ?? []); + $this->processApplicableMap($circlesManager, $related, $folder['groups'] ?? []); return $related; } @@ -125,7 +119,7 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array { } - public function improveRelatedResource(IRelatedResource $entry): void { + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void { } /** @@ -167,13 +161,17 @@ public function convertToRelatedResource(array $folderData): IRelatedResource { * @param RelatedResource $related * @param array> $applicableMap */ - public function processApplicableMap(RelatedResource $related, array $applicableMap): void { + public function processApplicableMap( + CirclesManager $circlesManager, + RelatedResource $related, + array $applicableMap + ): void { foreach ($applicableMap as $k => $entry) { $entityId = ''; if ($entry['type'] === 'circle') { $entityId = (string)$k; } elseif ($entry['type'] === 'group') { - $federatedGroup = $this->circlesManager->getFederatedUser($k, Member::TYPE_GROUP); + $federatedGroup = $circlesManager->getFederatedUser($k, Member::TYPE_GROUP); $entityId = $federatedGroup->getSingleId(); } diff --git a/lib/RelatedResourceProviders/TalkRelatedResourceProvider.php b/lib/RelatedResourceProviders/TalkRelatedResourceProvider.php index a9bfa74d..d8f14c9e 100644 --- a/lib/RelatedResourceProviders/TalkRelatedResourceProvider.php +++ b/lib/RelatedResourceProviders/TalkRelatedResourceProvider.php @@ -44,11 +44,8 @@ use OCA\RelatedResources\Model\TalkActor; use OCA\RelatedResources\Model\TalkRoom; use OCA\RelatedResources\Tools\Traits\TArrayTools; -use OCP\AutoloadNotAllowedException; use OCP\IL10N; use OCP\IURLGenerator; -use OCP\Server; -use Psr\Container\ContainerExceptionInterface; use Psr\Log\LoggerInterface; class TalkRelatedResourceProvider implements IRelatedResourceProvider { @@ -56,18 +53,12 @@ class TalkRelatedResourceProvider implements IRelatedResourceProvider { private const PROVIDER_ID = 'talk'; - private ?CirclesManager $circlesManager = null; - public function __construct( private IURLGenerator $urlGenerator, private IL10N $l10n, private TalkRoomRequest $talkRoomRequest, private LoggerInterface $logger ) { - try { - $this->circlesManager = Server::get(CirclesManager::class); - } catch (ContainerExceptionInterface | AutoloadNotAllowedException $e) { - } } @@ -86,11 +77,7 @@ public function loadWeightCalculator(): array { * * @return IRelatedResource|null */ - public function getRelatedFromItem(string $itemId): ?IRelatedResource { - if ($this->circlesManager === null) { - return null; - } - + public function getRelatedFromItem(CirclesManager $circlesManager, string $itemId): ?IRelatedResource { /** @var TalkRoom $room */ try { $room = $this->talkRoomRequest->getRoomByToken($itemId); @@ -100,7 +87,7 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { $related = $this->convertToRelatedResource($room); foreach ($this->talkRoomRequest->getActorsByToken($room->getToken()) as $actor) { - $this->processRoomParticipant($related, $actor); + $this->processRoomParticipant($circlesManager, $related, $actor); } if (!$related->isGroupShared()) { @@ -119,16 +106,16 @@ public function getRelatedFromItem(string $itemId): ?IRelatedResource { } - public function improveRelatedResource(IRelatedResource $entry): void { + public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void { if (!$entry->hasMeta('1on1')) { return; } try { - $current = $this->circlesManager->getCurrentFederatedUser(); + $current = $circlesManager->getCurrentFederatedUser(); } catch (FederatedUserNotFoundException $e) { - $this->circlesManager->startSession(); // enforce new session if not available - $current = $this->circlesManager->getCurrentFederatedUser(); + $circlesManager->startSession(); // enforce new session if not available + $current = $circlesManager->getCurrentFederatedUser(); $this->logger->info('session restarted', ['current' => $current]); } @@ -207,9 +194,13 @@ private function convertToRelatedResource(TalkRoom $share): IRelatedResource { * @param RelatedResource $related * @param TalkActor $actor */ - private function processRoomParticipant(RelatedResource $related, TalkActor $actor) { + private function processRoomParticipant( + CirclesManager $circlesManager, + RelatedResource $related, + TalkActor $actor + ) { try { - $participant = $this->convertRoomParticipant($actor); + $participant = $this->convertRoomParticipant($circlesManager, $actor); if ($actor->getActorType() === 'users') { $related->addToVirtualGroup($participant->getSingleId()); } else { @@ -227,11 +218,7 @@ private function processRoomParticipant(RelatedResource $related, TalkActor $act * @return FederatedUser * @throws Exception */ - public function convertRoomParticipant(TalkActor $actor): FederatedUser { - if (is_null($this->circlesManager)) { - throw new Exception('Circles needs to be enabled'); - } - + public function convertRoomParticipant(CirclesManager $circlesManager, TalkActor $actor): FederatedUser { switch ($actor->getActorType()) { case 'users': $type = Member::TYPE_USER; @@ -249,6 +236,6 @@ public function convertRoomParticipant(TalkActor $actor): FederatedUser { throw new Exception('unknown actor type (' . $actor->getActorType() . ')'); } - return $this->circlesManager->getFederatedUser($actor->getActorId(), $type); + return $circlesManager->getFederatedUser($actor->getActorId(), $type); } } diff --git a/lib/Service/RelatedService.php b/lib/Service/RelatedService.php index af79108d..5c40711c 100644 --- a/lib/Service/RelatedService.php +++ b/lib/Service/RelatedService.php @@ -226,7 +226,7 @@ public function getRelatedFromItem(string $providerId, string $itemId): RelatedR } $result = $this->getRelatedResourceProvider($providerId) - ->getRelatedFromItem($itemId); + ->getRelatedFromItem($this->circlesManager, $itemId); $this->logger->debug('get related to ' . $providerId . '.' . $itemId . ' - ' . json_encode($result)); @@ -470,7 +470,7 @@ private function filterUnavailableResults(array $result): array { private function improveResult(array $result): array { foreach ($result as $entry) { $this->getRelatedResourceProvider($entry->getProviderId()) - ->improveRelatedResource($entry); + ->improveRelatedResource($this->circlesManager, $entry); } return $result;