From d4c584d2a1f41e47ef6866da3feef418f036f8eb Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 18 Nov 2024 12:05:12 +0100 Subject: [PATCH] fix(circles): Use `probeCircles()` instead of `getCircles()` `probeCircles()` is lighter and more performant. Fixes: #498 Signed-off-by: Jonas --- lib/Service/CircleHelper.php | 11 +++++++---- lib/Service/CollectiveHelper.php | 9 +++++---- tests/stub.phpstub | 16 +++++++++++----- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/Service/CircleHelper.php b/lib/Service/CircleHelper.php index c39c4afe5..8c4537549 100644 --- a/lib/Service/CircleHelper.php +++ b/lib/Service/CircleHelper.php @@ -18,6 +18,7 @@ use OCA\Circles\Model\FederatedUser; use OCA\Circles\Model\Member; use OCA\Circles\Model\Probes\CircleProbe; +use OCA\Circles\Model\Probes\DataProbe; use OCA\Circles\Tools\Exceptions\InvalidItemException; use OCP\AppFramework\QueryException; use OCP\AutoloadNotAllowedException; @@ -88,9 +89,11 @@ private function startSuperSession(): void { public function getCircles(?string $userId = null): array { try { $this->startSession($userId); - $probe = new CircleProbe(); - $probe->mustBeMember(); - $circles = $this->circlesManager->getCircles($probe, true); + $circleProbe = new CircleProbe(); + $circleProbe->mustBeMember(); + $dataProbe = new DataProbe(); + $dataProbe->add(DataProbe::INITIATOR); + $circles = $this->circlesManager->probeCircles($circleProbe, $dataProbe); } catch (RequestBuilderException| FederatedItemException $e) { throw new NotPermittedException($e->getMessage(), 0, $e); @@ -149,7 +152,7 @@ public function findCircle(string $name, string $userId, int $level = Member::LE private function existsCircle(string $name): bool { $this->circlesManager->startSuperSession(); try { - $circles = $this->circlesManager->getCircles(); + $circles = $this->circlesManager->probeCircles(); } catch (InitiatorNotFoundException|RequestBuilderException $e) { throw new NotPermittedException($e->getMessage(), 0, $e); } diff --git a/lib/Service/CollectiveHelper.php b/lib/Service/CollectiveHelper.php index 9c3d1f13a..b0cb0f1bd 100644 --- a/lib/Service/CollectiveHelper.php +++ b/lib/Service/CollectiveHelper.php @@ -28,7 +28,7 @@ public function __construct( */ public function getCollectivesForUser(string $userId, bool $getLevel = true, bool $getUserSettings = true): array { $circles = $this->circleHelper->getCircles($userId); - $cids = array_map(fn ($circle) => $circle->getSingleId(), $circles); + $cids = array_map(static fn ($circle) => $circle->getSingleId(), $circles); $circles = array_combine($cids, $circles); /** @var Collective[] $collectives */ $collectives = $this->collectiveMapper->findByCircleIds($cids); @@ -55,13 +55,14 @@ public function getCollectivesForUser(string $userId, bool $getLevel = true, boo */ public function getCollectivesTrashForUser(string $userId): array { $circles = $this->circleHelper->getCircles($userId); - $cids = array_map(fn ($circle) => $circle->getSingleId(), $circles); + $cids = array_map(static fn ($circle) => $circle->getSingleId(), $circles); $circles = array_combine($cids, $circles); $collectives = $this->collectiveMapper->findTrashByCircleIdsAndUser($cids, $userId); foreach ($collectives as $c) { $cid = $c->getCircleId(); - $c->setName($circles[$cid]->getSanitizedName()); - $c->setLevel($this->circleHelper->getLevel($cid, $userId)); + $circle = $circles[$cid]; + $c->setName($circle->getSanitizedName()); + $c->setLevel($circle->getInitiator()->getLevel()); } return $collectives; } diff --git a/tests/stub.phpstub b/tests/stub.phpstub index 00ab9e67e..a88933a48 100644 --- a/tests/stub.phpstub +++ b/tests/stub.phpstub @@ -651,6 +651,10 @@ namespace OCA\Circles\Model\Probes { class CircleProbe { public function mustBeMember(bool $must = true): self {} } + class DataProbe { + public const INITIATOR = 'h'; + public function add(string $key, array $path = []): self {} + } } namespace OCA\Circles\Events { @@ -689,16 +693,18 @@ namespace OCA\Circles { use OCA\Circles\Model\Circle; use OCA\Circles\Model\FederatedUser; use OCA\Circles\Model\Probes\CircleProbe; + use OCA\Circles\Model\Probes\DataProbe; class CirclesManager { - public function startSuperSession(): void {} - public function startSession(?FederatedUser $federatedUser = null): void {} - public function getCircles(?CircleProbe $probe = null): array {} - public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle {} - public function flagAsAppManaged(string $circleId, bool $enabled = true): void {} public function getFederatedUser(string $federatedId, int $type = Member::TYPE_SINGLE): FederatedUser {} + public function startSession(?FederatedUser $federatedUser = null): void {} + public function startSuperSession(): void {} public function stopSession(): void {} public function createCircle(string $name, ?FederatedUser $owner = null, bool $personal = false, bool $local = false): Circle {} public function destroyCircle(string $singleId): void {} + public function getCircles(?CircleProbe $probe = null): array {} + public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle {} + public function flagAsAppManaged(string $circleId, bool $enabled = true): void {} + public function probeCircles(?CircleProbe $circleProbe = null, ?DataProbe $dataProbe = null): array {} } }