@@ -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