Skip to content

Commit

Permalink
single init circlesmanager
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jun 5, 2023
1 parent 2d4de1b commit 6663d90
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 147 deletions.
7 changes: 5 additions & 2 deletions lib/IRelatedResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace OCA\RelatedResources;

use OCA\Circles\CirclesManager;
use OCA\Circles\Model\FederatedUser;

interface IRelatedResourceProvider {
Expand All @@ -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
Expand All @@ -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;
}
24 changes: 9 additions & 15 deletions lib/RelatedResourceProviders/CalendarRelatedResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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;

Expand All @@ -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());
}

Expand All @@ -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;
Expand Down Expand Up @@ -145,7 +136,7 @@ public function getItemsAvailableToEntity(FederatedUser $entity): array {
}


public function improveRelatedResource(IRelatedResource $entry): void {
public function improveRelatedResource(CirclesManager $circlesManager, IRelatedResource $entry): void {
}


Expand Down Expand Up @@ -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());
Expand Down
62 changes: 20 additions & 42 deletions lib/RelatedResourceProviders/DeckRelatedResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Loading

0 comments on commit 6663d90

Please sign in to comment.