Browse Source

Sort articles

imwald
Nuša Pukšič 7 months ago
parent
commit
8c3e9bd7a1
  1. 16
      src/Controller/AuthorController.php

16
src/Controller/AuthorController.php

@ -7,8 +7,8 @@ namespace App\Controller;
use App\Service\NostrClient; use App\Service\NostrClient;
use App\Service\RedisCacheService; use App\Service\RedisCacheService;
use Elastica\Query\Terms; use Elastica\Query\Terms;
use Exception;
use FOS\ElasticaBundle\Finder\FinderInterface; use FOS\ElasticaBundle\Finder\FinderInterface;
use Psr\Cache\InvalidArgumentException;
use swentel\nostr\Key\Key; use swentel\nostr\Key\Key;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -17,8 +17,7 @@ use Symfony\Component\Routing\Attribute\Route;
class AuthorController extends AbstractController class AuthorController extends AbstractController
{ {
/** /**
* @throws \Exception * @throws Exception
* @throws InvalidArgumentException
*/ */
#[Route('/p/{npub}', name: 'author-profile', requirements: ['npub' => '^npub1.*'])] #[Route('/p/{npub}', name: 'author-profile', requirements: ['npub' => '^npub1.*'])]
public function index($npub, NostrClient $nostrClient, RedisCacheService $redisCacheService, FinderInterface $finder): Response 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 // Retrieve long-form content for the author
try { try {
$list = $nostrClient->getLongFormContentForPubkey($npub); $list = $nostrClient->getLongFormContentForPubkey($npub);
} catch (\Exception $e) { } catch (Exception $e) {
$list = []; $list = [];
} }
// Also look for articles in the Elastica index // Also look for articles in the Elastica index
$query = new Terms('pubkey', [$pubkey]); $query = new Terms('pubkey', [$pubkey]);
$list = array_merge($list, $finder->find($query, 25)); $list = array_merge($list, $finder->find($query, 25));
// deduplicate by slugs
$articles = []; $articles = [];
// Deduplicate by slugs
foreach ($list as $item) { foreach ($list as $item) {
if (!key_exists((string) $item->getSlug(), $articles)) { if (!key_exists((string) $item->getSlug(), $articles)) {
$articles[(string) $item->getSlug()] = $item; $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', [ return $this->render('pages/author.html.twig', [
'author' => $author, 'author' => $author,
'npub' => $npub, 'npub' => $npub,
@ -53,7 +57,7 @@ class AuthorController extends AbstractController
} }
/** /**
* @throws \Exception * @throws Exception
*/ */
#[Route('/p/{pubkey}', name: 'author-redirect')] #[Route('/p/{pubkey}', name: 'author-redirect')]
public function authorRedirect($pubkey): Response public function authorRedirect($pubkey): Response

Loading…
Cancel
Save