From 418cdff931f17a113150a67f9aa9548dd2d62c75 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 2 Oct 2024 09:54:30 +0200 Subject: [PATCH] dev: Refactor the NewsPicker API --- src/controllers/News.php | 13 ++++--------- src/services/NewsPicker.php | 23 +++-------------------- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/src/controllers/News.php b/src/controllers/News.php index 337846c5..80ca47ab 100644 --- a/src/controllers/News.php +++ b/src/controllers/News.php @@ -78,10 +78,8 @@ public function create(Request $request): Response ]); } - $news_picker = new services\NewsPicker($user, [ - 'number_links' => 50, - ]); - $links = $news_picker->pick(); + $news_picker = new services\NewsPicker($user); + $links = $news_picker->pick(max: 50); $news = $user->news(); @@ -125,11 +123,8 @@ public function showAvailable(Request $request): Response ]); } - $news_picker = new services\NewsPicker($user, [ - 'number_links' => 1, - 'from' => 'followed', - ]); - $links = $news_picker->pick(); + $news_picker = new services\NewsPicker($user); + $links = $news_picker->pick(max: 1); return Response::json(200, [ 'available' => count($links) > 0, diff --git a/src/services/NewsPicker.php b/src/services/NewsPicker.php index d50b535e..be2a4316 100644 --- a/src/services/NewsPicker.php +++ b/src/services/NewsPicker.php @@ -8,33 +8,16 @@ * The NewsPicker service is a sort of basic artificial intelligence. Its * purpose is to select a bunch of links relevant for a given user. * - * @phpstan-type Options array{ - * 'number_links': int, - * } - * * @author Marien Fressinaud * @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL */ class NewsPicker { - private const DEFAULT_OPTIONS = [ - 'number_links' => 9, - ]; - private models\User $user; - /** @var Options */ - private array $options; - - /** - * @param array{ - * 'number_links'?: int, - * } $options - */ - public function __construct(models\User $user, array $options = []) + public function __construct(models\User $user) { $this->user = $user; - $this->options = array_merge(self::DEFAULT_OPTIONS, $options); } /** @@ -42,7 +25,7 @@ public function __construct(models\User $user, array $options = []) * * @return models\Link[] */ - public function pick(): array + public function pick(int $max = 25): array { $excluded_hashes = models\Link::listHashesExcludedFromNews($this->user->id); $links_from_followed = models\Link::listFromFollowedCollections($this->user->id); @@ -58,7 +41,7 @@ public function pick(): array $links[$hash] = $link; - if (count($links) >= $this->options['number_links']) { + if (count($links) >= $max) { break; } }