Skip to content

Commit 9ba3ce2

Browse files
ArtificialOwlprovokateurin
authored andcommitted
fix(team-api): get all teams details in a single request
Signed-off-by: Maxence Lange <[email protected]> Signed-off-by: provokateurin <[email protected]>
1 parent 2552950 commit 9ba3ce2

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

build/psalm-baseline.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4297,6 +4297,11 @@
42974297
<code><![CDATA[$tag]]></code>
42984298
</MoreSpecificImplementedParamType>
42994299
</file>
4300+
<file src="lib/private/Teams/TeamManager.php">
4301+
<UndefinedDocblockClass>
4302+
<code><![CDATA[Circle]]></code>
4303+
</UndefinedDocblockClass>
4304+
</file>
43004305
<file src="lib/private/URLGenerator.php">
43014306
<InvalidReturnStatement>
43024307
<code><![CDATA[$path]]></code>

core/Controller/TeamsApiController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ public function resolveOne(string $teamId): DataResponse {
6666
public function listTeams(string $providerId, string $resourceId): DataResponse {
6767
/** @psalm-suppress PossiblyNullArgument The route is limited to logged-in users */
6868
$teams = $this->teamManager->getTeamsForResource($providerId, $resourceId, $this->userId);
69-
$teams = array_values(array_map(function (Team $team) {
69+
$sharesPerTeams = $this->teamManager->getSharedWithList(array_map(fn (Team $team): string => $team->getId(), $teams), $this->userId);
70+
$listTeams = array_values(array_map(function (Team $team) use ($sharesPerTeams) {
7071
$response = $team->jsonSerialize();
71-
/** @psalm-suppress PossiblyNullArgument The route is limited to logged in users */
72-
$response['resources'] = array_map(static fn (TeamResource $resource) => $resource->jsonSerialize(), $this->teamManager->getSharedWith($team->getId(), $this->userId));
72+
$response['resources'] = array_map(static fn (TeamResource $resource) => $resource->jsonSerialize(), $sharesPerTeams[$team->getId()] ?? []);
7373
return $response;
7474
}, $teams));
7575

7676
return new DataResponse([
77-
'teams' => $teams,
77+
'teams' => $listTeams,
7878
]);
7979
}
8080
}

lib/private/Teams/TeamManager.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,38 @@ public function getSharedWith(string $teamId, string $userId): array {
8484
return array_values($resources);
8585
}
8686

87-
public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
87+
public function getSharedWithList(array $teams, string $userId): array {
8888
if (!$this->hasTeamSupport()) {
8989
return [];
9090
}
9191

92-
$provider = $this->getProvider($providerId);
93-
return array_values(array_filter(array_map(function ($teamId) use ($userId) {
94-
$team = $this->getTeam($teamId, $userId);
95-
if ($team === null) {
96-
return null;
92+
$resources = [];
93+
foreach ($this->getProviders() as $provider) {
94+
if (method_exists($provider, 'getSharedWithList')) {
95+
$resources[] = $provider->getSharedWithList($teams, $userId);
96+
} else {
97+
foreach ($teams as $team) {
98+
$resources[] = [$team->getId() => $provider->getSharedWith($team->getId())];
99+
}
97100
}
101+
}
98102

103+
return array_merge_recursive(...$resources);
104+
}
105+
106+
public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
107+
if (!$this->hasTeamSupport()) {
108+
return [];
109+
}
110+
111+
$provider = $this->getProvider($providerId);
112+
return array_map(function (Circle $team) {
99113
return new Team(
100-
$teamId,
114+
$team->getSingleId(),
101115
$team->getDisplayName(),
102-
$this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $teamId]),
116+
$this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $team->getSingleId()]),
103117
);
104-
}, $provider->getTeamsForResource($resourceId))));
118+
}, $this->getTeams($provider->getTeamsForResource($resourceId), $userId));
105119
}
106120

107121
private function getTeam(string $teamId, string $userId): ?Circle {
@@ -117,4 +131,17 @@ private function getTeam(string $teamId, string $userId): ?Circle {
117131
return null;
118132
}
119133
}
134+
135+
/**
136+
* @return Circle[]
137+
*/
138+
private function getTeams(array $teams, string $userId): array {
139+
if (!$this->hasTeamSupport()) {
140+
return [];
141+
}
142+
143+
$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
144+
$this->circlesManager->startSession($federatedUser);
145+
return $this->circlesManager->getCirclesByIds($teams);
146+
}
120147
}

lib/public/Teams/ITeamManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ public function getSharedWith(string $teamId, string $userId): array;
4040
* @since 29.0.0
4141
*/
4242
public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array;
43+
44+
/**
45+
* @param list<Team> $teams
46+
* @return array<string, list<TeamResource>>
47+
*
48+
* @since 33.0.0
49+
*/
50+
public function getSharedWithList(array $teams, string $userId): array;
4351
}

0 commit comments

Comments
 (0)