Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/Db/MembershipRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use OCA\Circles\Exceptions\MembershipNotFoundException;
use OCA\Circles\Model\FederatedUser;
use OCA\Circles\Model\Member;
use OCA\Circles\Model\Membership;
use OCP\DB\QueryBuilder\IQueryBuilder;

Expand Down Expand Up @@ -113,6 +114,36 @@ public function getInherited(string $singleId, int $level = 0): array {
return $this->getItemsFromRequest($qb);
}

/**
* @param string $singleId
* @param int $level
*
* @return list<Membership>
*/
public function getInheritedExcludingCircles(string $singleId, int $level = 0): array {
$qb = $this->getMembershipSelectSql();
$qb->limitToCircleId($singleId);

$expr = $qb->expr();
if ($level > 1) {
$qb->andWhere($expr->gte('level', $qb->createNamedParameter($level, IQueryBuilder::PARAM_INT)));
}
$qb->leftJoin(
CoreQueryBuilder::MEMBERSHIPS,
CoreRequestBuilder::TABLE_MEMBER,
CoreQueryBuilder::MEMBER,
$expr->andX(
$expr->eq(CoreQueryBuilder::MEMBERSHIPS . '.single_id', CoreQueryBuilder::MEMBER . '.single_id'),
$expr->eq(
CoreQueryBuilder::MEMBER . '.user_type',
$qb->createNamedParameter(Member::TYPE_CIRCLE, IQueryBuilder::PARAM_INT)
)
)
);
$qb->andWhere($expr->isNull(CoreQueryBuilder::MEMBER . '.single_id'));

return $this->getItemsFromRequest($qb);
}

/**
* @param string $singleId
Expand Down
6 changes: 6 additions & 0 deletions lib/FederatedItems/CircleDestroy.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ public function manage(FederatedEvent $event): void {

$this->eventService->circleDestroying($event);

$parentCirlces = $this->membershipService->getParentCircles($circle);

$this->circleRequest->delete($circle);
$this->memberRequest->deleteAllFromCircle($circle);
$this->membershipService->onUpdate($circle->getSingleId());

foreach ($parentCirlces as $parentCircle) {
$this->membershipService->updatePopulation($parentCircle);
}
}


Expand Down
1 change: 1 addition & 0 deletions lib/Model/Circle.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ public function jsonSerialize(): array {
'sanitizedName' => $this->getSanitizedName(),
'source' => $this->getSource(),
'population' => $this->getPopulation(),
'populationInherited' => $this->getPopulationInherited(),
'config' => $this->getConfig(),
'description' => $this->getDescription(),
'url' => $this->getUrl(),
Expand Down
53 changes: 46 additions & 7 deletions lib/Service/MembershipService.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,23 +388,62 @@ private function getMembershipsFromList(array $list, string $circleId): Membersh
throw new ItemNotFoundException();
}

/**
* Get all circles that contain a given circle as a member
*
* @param Circle $circle
*
* @return list<Circle>
*/
public function getParentCircles(Circle $circle): array {
$memberships = $this->membershipRequest->getMemberships($circle->getSingleId());
$parentCircles = [];
foreach ($memberships as $membership) {
$parentCircles[] = $this->circleRequest->getCircle($membership->getCircleId());
}

return $parentCircles;
}

/**
* Update population counts for a given circle and all circles that contain it as a member
*
* @param Circle $circle
*/
public function updatePopulation(Circle $circle): void {
$local = $inherited = 0;
$memberships = $this->membershipRequest->getInherited($circle->getSingleId(), Member::LEVEL_MEMBER);
$this->calculateAndSavePopulation($circle);

$parentCircles = $this->getParentCircles($circle);
foreach ($parentCircles as $parentCircle) {
$this->calculateAndSavePopulation($parentCircle);
}
}

/**
* Calculate and save population counts for a given circle
*
* population: direct member count (excluding nested circles)
* populationInherited: total member count (direct + inherited from nested circles)
*
* @param Circle $circle
*/
private function calculateAndSavePopulation(Circle $circle): void {
$population = $populationInherited = 0;

$memberships = $this->membershipRequest->getInheritedExcludingCircles(
$circle->getSingleId(),
Member::LEVEL_MEMBER
);
foreach ($memberships as $membership) {
$inherited++;
if ($membership->getCircleId() === $circle->getSingleId()) {
$local++;
$populationInherited++;
if ($membership->getInheritanceDepth() === 1) {
$population++;
}
}

$settings = $circle->getSettings();
$settings['population'] = $local;
$settings['populationInherited'] = $inherited;
$settings['population'] = $population;
$settings['populationInherited'] = $populationInherited;
$this->circleRequest->updateSettings($circle->setSettings($settings));
}
}
Loading