Skip to content

Commit f821e4f

Browse files
authored
Improve MagazinePeopleFrontController (#1957)
1 parent af75cf3 commit f821e4f

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/Controller/Magazine/MagazinePeopleFrontController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function __invoke(
3131
$this->magazineRepository->findByActivity(),
3232
fn ($val) => 'random' !== $val->name && $val !== $magazine
3333
),
34-
'local' => $this->userRepository->findUsersForMagazine($magazine),
35-
'federated' => $this->userRepository->findUsersForMagazine($magazine, true),
34+
'local' => $this->userRepository->findUsersForMagazine($magazine, limit: 28, limitTime: $magazine->getContentCount() > 1000),
35+
'federated' => $this->userRepository->findUsersForMagazine($magazine, true, limit: 28, limitTime: $magazine->getContentCount() > 1000),
3636
]
3737
);
3838
}

src/Entity/Magazine.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,9 @@ public function isActorPostingRestricted(Magazine|User $actor): bool
530530

531531
return true;
532532
}
533+
534+
public function getContentCount(): int
535+
{
536+
return $this->entryCount + $this->entryCommentCount + $this->postCount + $this->postCommentCount;
537+
}
533538
}

src/Repository/UserRepository.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -534,22 +534,23 @@ public function findUsersSuggestions(string $query): array
534534
->getResult();
535535
}
536536

537-
public function findUsersForMagazine(Magazine $magazine, ?bool $federated = false, $limit = 200, bool $limitTime = false, bool $requireAvatar = false): array
537+
public function findUsersForMagazine(Magazine $magazine, ?bool $federated = false, int $limit = 200, bool $limitTime = false, bool $requireAvatar = false): array
538538
{
539539
$conn = $this->_em->getConnection();
540540
$timeWhere = $limitTime ? "AND created_at > now() - '30 days'::interval" : '';
541541
$sql = "
542-
(SELECT count(id), user_id FROM entry WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT 50)
543-
UNION
544-
(SELECT count(id), user_id FROM entry_comment WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT 50)
545-
UNION
546-
(SELECT count(id), user_id FROM post WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT 50)
547-
UNION
548-
(SELECT count(id), user_id FROM post_comment WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT 50)
549-
ORDER BY count DESC";
542+
(SELECT count(id), user_id FROM entry WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT :limit)
543+
UNION ALL
544+
(SELECT count(id), user_id FROM entry_comment WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT :limit)
545+
UNION ALL
546+
(SELECT count(id), user_id FROM post WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT :limit)
547+
UNION ALL
548+
(SELECT count(id), user_id FROM post_comment WHERE magazine_id = :magazineId $timeWhere GROUP BY user_id ORDER BY count DESC LIMIT :limit)
549+
";
550550

551551
$stmt = $conn->prepare($sql);
552552
$stmt->bindValue('magazineId', $magazine->getId());
553+
$stmt->bindValue('limit', $limit);
553554
$counter = $stmt->executeQuery()->fetchAllAssociative();
554555

555556
$output = [];
@@ -563,6 +564,9 @@ public function findUsersForMagazine(Magazine $magazine, ?bool $federated = fals
563564
}
564565
}
565566

567+
// sort the array after the counts from the different table are added up
568+
usort($output, fn ($a, $b) => $b['count'] - $a['count']);
569+
566570
$user = array_map(fn ($item) => $item['user_id'], $output);
567571

568572
$qb = $this->createQueryBuilder('u', 'u.id');
@@ -581,8 +585,7 @@ public function findUsersForMagazine(Magazine $magazine, ?bool $federated = fals
581585

582586
if (null !== $federated) {
583587
if ($federated) {
584-
$qb->andWhere('u.apId IS NOT NULL')
585-
->andWhere('u.apDiscoverable = true');
588+
$qb->andWhere('u.apId IS NOT NULL');
586589
} else {
587590
$qb->andWhere('u.apId IS NULL');
588591
}
@@ -603,7 +606,7 @@ public function findUsersForMagazine(Magazine $magazine, ?bool $federated = fals
603606
if (isset($users[$item['user_id']])) {
604607
$res[] = $users[$item['user_id']];
605608
}
606-
if (\count($res) >= 35) {
609+
if (\count($res) >= $limit) {
607610
break;
608611
}
609612
}

0 commit comments

Comments
 (0)