Skip to content

Commit da7402f

Browse files
Merge pull request #2115 from nextcloud/fix/noid/switch-to-probecircle
2 parents 1ac87f8 + cfdd19b commit da7402f

7 files changed

Lines changed: 119 additions & 2 deletions

lib/CirclesManager.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle
344344
return $this->circleService->getCircle($singleId, $probe);
345345
}
346346

347+
/**
348+
* better than using getCircle() if only interested in teams current user is member of
349+
*
350+
* @since 33.0.0
351+
*/
352+
public function probeCircle(string $singleId, ?CircleProbe $probe = null, ?DataProbe $dataProbe = null): Circle {
353+
return $this->circleService->probeCircle($singleId, $probe, $dataProbe);
354+
}
355+
356+
/**
357+
* get details from a list of circles the current user is a member of
358+
*
359+
* @since 33.0.0
360+
*/
361+
public function getCirclesByIds(array $ids, ?DataProbe $dataProbe = null): array {
362+
return $this->circleService->probeCirclesByIds($ids, $dataProbe);
363+
}
364+
347365

348366
/**
349367
* @param Circle $circle

lib/Db/CircleRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,36 @@ public function probeCircles(
235235
return $this->getItemsFromRequest($qb);
236236
}
237237

238+
/**
239+
* returns details about circles using a list of ids.
240+
* initiator permissions are respected
241+
*/
242+
public function probeCirclesByIds(
243+
IFederatedUser $initiator,
244+
array $ids,
245+
DataProbe $dataProbe,
246+
): array {
247+
$qb = $this->getCircleSelectSql();
248+
if (!$dataProbe->has(DataProbe::MEMBERSHIPS)) {
249+
$dataProbe->add(DataProbe::MEMBERSHIPS);
250+
}
251+
252+
$qb->setSqlPath(CoreQueryBuilder::CIRCLE, $dataProbe->getPath());
253+
$qb->leftJoinOwner(CoreQueryBuilder::CIRCLE);
254+
$qb->innerJoinMembership(null, CoreQueryBuilder::CIRCLE);
255+
256+
$aliasMembership = $qb->generateAlias(CoreQueryBuilder::CIRCLE, CoreQueryBuilder::MEMBERSHIPS);
257+
$limit = $qb->exprLimit('single_id', $initiator->getSingleId(), $aliasMembership);
258+
$qb->completeProbeWithInitiator(CoreQueryBuilder::CIRCLE, 'single_id', $aliasMembership);
259+
$qb->andWhere(
260+
$limit,
261+
$qb->expr()->in(CoreQueryBuilder::CIRCLE . '.unique_id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_STR_ARRAY))
262+
);
263+
$qb->resetSqlPath();
264+
265+
return $this->getItemsFromRequest($qb);
266+
}
267+
238268
/**
239269
* @param IFederatedUser|null $initiator
240270
* @param CircleProbe $circleProbe

lib/Db/CoreQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ public function leftJoinOwner(string $alias, string $field = 'unique_id'): void
843843
* @param string $field
844844
*/
845845
public function innerJoinMembership(
846-
CircleProbe $probe,
846+
?CircleProbe $probe,
847847
string $alias,
848848
string $field = 'unique_id',
849849
): void {
@@ -862,7 +862,7 @@ public function innerJoinMembership(
862862
$on = $expr->andX($expr->eq($aliasMembership . '.circle_id', $alias . '.' . $field));
863863

864864
// limit on membership level if requested
865-
$minLevel = $probe->getMinimumLevel();
865+
$minLevel = $probe?->getMinimumLevel() ?? 0;
866866
if ($minLevel > Member::LEVEL_MEMBER) {
867867
$on->add($this->exprGt('level', $minLevel, true, $aliasMembership));
868868
}

lib/Db/ShareWrapperRequest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ public function getSharesToCircle(
181181
}
182182

183183

184+
/**
185+
* @param string $circleId
186+
* @param FederatedUser|null $shareRecipient
187+
* @param FederatedUser|null $shareInitiator
188+
* @param bool $completeDetails
189+
*
190+
* @return ShareWrapper[]
191+
* @throws RequestBuilderException
192+
*/
193+
public function getSharesToCircles(array $circleIds): array {
194+
$qb = $this->getShareSelectSql();
195+
$qb->limitNull('parent', false);
196+
$qb->expr()->in('share_with', $qb->createNamedParameter($circleIds, IQueryBuilder::PARAM_STR_ARRAY));
197+
return $this->getItemsFromRequest($qb);
198+
}
199+
200+
201+
184202
/**
185203
* @param int $shareId
186204
* @param FederatedUser|null $federatedUser

lib/FileSharingTeamResourceProvider.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ public function getSharedWith(string $teamId): array {
4343
}
4444

4545
$shares = $this->shareByCircleProvider->getSharesToCircle($teamId);
46+
return $this->convertWrappedShareToResource($shares);
47+
}
48+
49+
/**
50+
* @return array<string, TeamResource[]>
51+
*/
52+
public function getSharedWithList(array $teams): array {
53+
$data = $shares = [];
54+
foreach ($this->shareByCircleProvider->getSharesToCircles($teams) as $share) {
55+
if (!array_key_exists($share->getId(), $shares)) {
56+
$shares[$share->getSharedWith()] = [];
57+
}
58+
$shares[$share->getSharedWith()][] = $share;
59+
}
60+
61+
foreach ($teams as $teamId) {
62+
$data[$teamId] = $this->convertWrappedShareToResource($shares[$teamId]);
63+
}
64+
65+
return $data;
66+
}
67+
68+
/**
69+
* convert list of ShareWrapper to TeamResource
70+
*
71+
* @param ShareWrapper[] $shares
72+
* @return TeamResource[]
73+
*/
74+
private function convertWrappedShareToResource(array $shares): array {
4675
usort($shares, function ($a, $b) {
4776
return (int)($b->getItemType() === 'folder') - (int)($a->getItemType() === 'folder');
4877
});

lib/Service/CircleService.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,4 +811,19 @@ public function probeCircles(CircleProbe $circleProbe, ?DataProbe $dataProbe = n
811811
$dataProbe
812812
);
813813
}
814+
815+
/**
816+
* @return Circle[]
817+
*/
818+
public function probeCirclesByIds(array $ids, ?DataProbe $dataProbe = null): array {
819+
if (empty($ids)) {
820+
return [];
821+
}
822+
$this->federatedUserService->mustHaveCurrentUser();
823+
return $this->circleRequest->probeCirclesByIds(
824+
$this->federatedUserService->getCurrentUser(),
825+
$ids,
826+
$dataProbe ?? new DataProbe()
827+
);
828+
}
814829
}

lib/Service/ShareWrapperService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ public function getSharesToCircle(
152152
);
153153
}
154154

155+
/**
156+
* @return ShareWrapper[]
157+
*/
158+
public function getSharesToCircles(array $circleIds): array {
159+
return $this->shareWrapperRequest->getSharesToCircles($circleIds);
160+
}
161+
155162

156163
/**
157164
* @param int $shareId

0 commit comments

Comments
 (0)