From 8c3e9bd7a1c592485c976c2d30e5657378155fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Sun, 8 Jun 2025 11:39:15 +0200 Subject: [PATCH] Sort articles --- src/Controller/AuthorController.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Controller/AuthorController.php b/src/Controller/AuthorController.php index 8b77307..11b91e9 100644 --- a/src/Controller/AuthorController.php +++ b/src/Controller/AuthorController.php @@ -7,8 +7,8 @@ namespace App\Controller; use App\Service\NostrClient; use App\Service\RedisCacheService; use Elastica\Query\Terms; +use Exception; use FOS\ElasticaBundle\Finder\FinderInterface; -use Psr\Cache\InvalidArgumentException; use swentel\nostr\Key\Key; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; @@ -17,8 +17,7 @@ use Symfony\Component\Routing\Attribute\Route; class AuthorController extends AbstractController { /** - * @throws \Exception - * @throws InvalidArgumentException + * @throws Exception */ #[Route('/p/{npub}', name: 'author-profile', requirements: ['npub' => '^npub1.*'])] public function index($npub, NostrClient $nostrClient, RedisCacheService $redisCacheService, FinderInterface $finder): Response @@ -30,21 +29,26 @@ class AuthorController extends AbstractController // Retrieve long-form content for the author try { $list = $nostrClient->getLongFormContentForPubkey($npub); - } catch (\Exception $e) { + } catch (Exception $e) { $list = []; } // Also look for articles in the Elastica index $query = new Terms('pubkey', [$pubkey]); $list = array_merge($list, $finder->find($query, 25)); - // deduplicate by slugs $articles = []; + // Deduplicate by slugs foreach ($list as $item) { if (!key_exists((string) $item->getSlug(), $articles)) { $articles[(string) $item->getSlug()] = $item; } } + // Sort articles by date + usort($articles, function ($a, $b) { + return $b->getCreatedAt() <=> $a->getCreatedAt(); + }); + return $this->render('pages/author.html.twig', [ 'author' => $author, 'npub' => $npub, @@ -53,7 +57,7 @@ class AuthorController extends AbstractController } /** - * @throws \Exception + * @throws Exception */ #[Route('/p/{pubkey}', name: 'author-redirect')] public function authorRedirect($pubkey): Response