|
|
|
@ -3,78 +3,105 @@ |
|
|
|
namespace App\Twig\Components\Organisms; |
|
|
|
namespace App\Twig\Components\Organisms; |
|
|
|
|
|
|
|
|
|
|
|
use App\Repository\ArticleRepository; |
|
|
|
use App\Repository\ArticleRepository; |
|
|
|
|
|
|
|
use App\Service\NostrClient; |
|
|
|
use Psr\Cache\InvalidArgumentException; |
|
|
|
use Psr\Cache\InvalidArgumentException; |
|
|
|
use swentel\nostr\Event\Event; |
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
|
|
|
|
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
|
|
|
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; |
|
|
|
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; |
|
|
|
|
|
|
|
|
|
|
|
#[AsTwigComponent] |
|
|
|
#[AsTwigComponent] |
|
|
|
final class FeaturedList |
|
|
|
final class FeaturedList |
|
|
|
{ |
|
|
|
{ |
|
|
|
public string $category; |
|
|
|
public string $category = ''; |
|
|
|
public string $title; |
|
|
|
|
|
|
|
|
|
|
|
public string $title = ''; |
|
|
|
|
|
|
|
|
|
|
|
public array $list = []; |
|
|
|
public array $list = []; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
public function __construct( |
|
|
|
private readonly CacheInterface $cache, |
|
|
|
private readonly CacheInterface $cache, |
|
|
|
private readonly ArticleRepository $articleRepository) |
|
|
|
private readonly ArticleRepository $articleRepository, |
|
|
|
{ |
|
|
|
private readonly NostrClient $nostrClient, |
|
|
|
|
|
|
|
private readonly ParameterBagInterface $params, |
|
|
|
|
|
|
|
) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @throws InvalidArgumentException |
|
|
|
* @throws InvalidArgumentException |
|
|
|
* @throws \Exception |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function mount($category): void |
|
|
|
public function mount($category): void |
|
|
|
{ |
|
|
|
{ |
|
|
|
$parts = explode(':', $category[1]); |
|
|
|
$this->list = []; |
|
|
|
/** @var Event $catIndex */ |
|
|
|
$this->title = ''; |
|
|
|
$catIndex = $this->cache->get('magazine-' . $parts[2], function (){ |
|
|
|
|
|
|
|
throw new \Exception('Not found'); |
|
|
|
$coord = $category[1] ?? ''; |
|
|
|
|
|
|
|
$this->category = (string) $coord; |
|
|
|
|
|
|
|
$parts = explode(':', $this->category, 3); |
|
|
|
|
|
|
|
if (\count($parts) < 3) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$slug = $parts[2]; |
|
|
|
|
|
|
|
$npub = (string) $this->params->get('npub'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
$catIndex = $this->cache->get('magazine-' . $slug, function (ItemInterface $item) use ($npub, $slug) { |
|
|
|
|
|
|
|
$item->expiresAfter(300); |
|
|
|
|
|
|
|
$mag = $this->nostrClient->getMagazineIndex($npub, $slug); |
|
|
|
|
|
|
|
if ($mag === null) { |
|
|
|
|
|
|
|
throw new \RuntimeException('Category index not found for '.$slug); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $mag; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
} catch (\Throwable) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$slugs = []; |
|
|
|
$slugs = []; |
|
|
|
foreach ($catIndex->getTags() as $tag) { |
|
|
|
foreach ($catIndex->getTags() as $tag) { |
|
|
|
if ($tag[0] === 'title') { |
|
|
|
if (($tag[0] ?? null) === 'title' && isset($tag[1])) { |
|
|
|
$this->title = $tag[1]; |
|
|
|
$this->title = (string) $tag[1]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (($tag[0] ?? null) === 'a' && isset($tag[1])) { |
|
|
|
|
|
|
|
$segs = explode(':', (string) $tag[1], 3); |
|
|
|
|
|
|
|
$slugs[] = end($segs); |
|
|
|
|
|
|
|
if (\count($slugs) >= 5) { |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if ($tag[0] === 'a') { |
|
|
|
|
|
|
|
$parts = explode(':', $tag[1], 3); |
|
|
|
|
|
|
|
$slugs[] = end($parts); |
|
|
|
|
|
|
|
if (count($slugs) >= 5) { |
|
|
|
|
|
|
|
break; // Limit to 5 items |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->title === '') { |
|
|
|
|
|
|
|
$this->title = $slug; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($slugs === []) { |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Use database query instead of Elasticsearch |
|
|
|
|
|
|
|
if (!empty($slugs)) { |
|
|
|
|
|
|
|
$articles = $this->articleRepository->findBySlugsCriteria($slugs); |
|
|
|
$articles = $this->articleRepository->findBySlugsCriteria($slugs); |
|
|
|
|
|
|
|
|
|
|
|
// Create a map of slug => item to get the latest version of each |
|
|
|
|
|
|
|
$slugMap = []; |
|
|
|
$slugMap = []; |
|
|
|
foreach ($articles as $article) { |
|
|
|
foreach ($articles as $article) { |
|
|
|
$slug = $article->getSlug(); |
|
|
|
$articleSlug = $article->getSlug(); |
|
|
|
if ($slug !== '') { |
|
|
|
if ($articleSlug !== '') { |
|
|
|
if (!isset($slugMap[$slug])) { |
|
|
|
if (!isset($slugMap[$articleSlug])) { |
|
|
|
$slugMap[$slug] = $article; |
|
|
|
$slugMap[$articleSlug] = $article; |
|
|
|
} elseif ($article->getCreatedAt() > $slugMap[$slug]->getCreatedAt()) { |
|
|
|
} elseif ($article->getCreatedAt() > $slugMap[$articleSlug]->getCreatedAt()) { |
|
|
|
$slugMap[$slug] = $article; |
|
|
|
$slugMap[$articleSlug] = $article; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Build ordered list based on original slugs order |
|
|
|
|
|
|
|
$orderedList = []; |
|
|
|
$orderedList = []; |
|
|
|
foreach ($slugs as $slug) { |
|
|
|
foreach ($slugs as $articleSlug) { |
|
|
|
if (isset($slugMap[$slug])) { |
|
|
|
if (isset($slugMap[$articleSlug])) { |
|
|
|
$orderedList[] = $slugMap[$slug]; |
|
|
|
$orderedList[] = $slugMap[$articleSlug]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->list = array_slice($orderedList, 0, 4); |
|
|
|
$this->list = array_slice($orderedList, 0, 4); |
|
|
|
} else { |
|
|
|
|
|
|
|
$this->list = []; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|