diff --git a/src/Controller/Administration/ArticleManagementController.php b/src/Controller/Administration/ArticleManagementController.php index 738da1a..4feb260 100644 --- a/src/Controller/Administration/ArticleManagementController.php +++ b/src/Controller/Administration/ArticleManagementController.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Controller\Administration; use App\Service\RedisCacheService; +use Elastica\Query; use FOS\ElasticaBundle\Finder\PaginatedFinderInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -20,6 +21,7 @@ class ArticleManagementController extends AbstractController public function listArticles( #[Autowire(service: 'fos_elastica.finder.articles')] PaginatedFinderInterface $finder, + #[Autowire(service: 'fos_elastica.index.articles')] \Elastica\Index $index, RedisCacheService $redisCacheService ): Response { @@ -41,9 +43,24 @@ class ArticleManagementController extends AbstractController if (count($articles) >= 50) break; } } + // Separate query for aggregations + $aggQuery = new Query([ + 'size' => 0, + 'aggs' => [ + 'tags' => [ + 'terms' => [ + 'field' => 'topics', + 'size' => 50 + ] + ] + ] + ]); + $aggResults = $index->search($aggQuery); + $tagCounts = $aggResults->getAggregations()['tags']['buckets'] ?? []; return $this->render('admin/articles.html.twig', [ 'articles' => $articles, 'indexes' => [], + 'tagCounts' => $tagCounts, ]); } diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php index 266c580..4389353 100644 --- a/src/Controller/ArticleController.php +++ b/src/Controller/ArticleController.php @@ -9,6 +9,7 @@ use App\Service\NostrClient; use App\Service\RedisCacheService; use App\Util\CommonMark\Converter; use Doctrine\ORM\EntityManagerInterface; +use FOS\ElasticaBundle\Finder\PaginatedFinderInterface; use League\CommonMark\Exception\CommonMarkException; use nostriphant\NIP19\Bech32; use nostriphant\NIP19\Data\NAddr; @@ -17,6 +18,7 @@ use Psr\Cache\InvalidArgumentException; use swentel\nostr\Event\Event; use swentel\nostr\Key\Key; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -356,4 +358,68 @@ class ArticleController extends AbstractController return $data; } + #[Route('/topics', name: 'topics')] + public function topics( + Request $request, + #[Autowire(service: 'fos_elastica.finder.articles')] PaginatedFinderInterface $finder + ): Response { + $topics = [ + 'bitcoin-crypto' => [ + 'name' => 'Bitcoin & Crypto', + 'tags' => ['bitcoin', 'bitkoin', 'lightning', 'decentralization', 'freedom', 'privacy', 'sovereignty'] + ], + 'nostr' => [ + 'name' => 'Nostr', + 'tags' => ['nostr', 'grownostr'] + ], + 'christianity' => [ + 'name' => 'Christianity', + 'tags' => ['jesus', 'christian', 'christianity', 'bible', 'trustjesus', 'biblestr'] + ], + 'travel' => [ + 'name' => 'Travel', + 'tags' => ['travel', 'europe'] + ], + 'photography' => [ + 'name' => 'Photography', + 'tags' => ['photography'] + ], + 'ai' => [ + 'name' => 'AI', + 'tags' => ['ai'] + ], + 'philosophy' => [ + 'name' => 'Philosophy', + 'tags' => ['philosophy'] + ], + 'art' => [ + 'name' => 'Art', + 'tags' => ['art'] + ] + ]; + + $selectedTopic = $request->query->get('topic'); + $articles = []; + if ($selectedTopic && isset($topics[$selectedTopic])) { + $tags = $topics[$selectedTopic]['tags']; + $query = [ + 'size' => 10, + 'sort' => [['createdAt' => ['order' => 'desc']]], + 'query' => [ + 'terms' => [ + 'topics' => $tags + ] + ] + ]; + $results = $finder->find($query); + $articles = array_slice($results, 0, 10); + } + + return $this->render('pages/topics.html.twig', [ + 'topics' => $topics, + 'selectedTopic' => $selectedTopic, + 'articles' => $articles, + ]); + } + } diff --git a/templates/admin/articles.html.twig b/templates/admin/articles.html.twig index a3c0f94..26db0bc 100644 --- a/templates/admin/articles.html.twig +++ b/templates/admin/articles.html.twig @@ -42,4 +42,13 @@ {% endfor %} + +
Select a topic to view the 10 most recent articles related to it.
+ +{{ article.summary|slice(0, 150) }}{% if article.summary|length > 150 %}...{% endif %}
+ Published: {{ article.createdAt|date('Y-m-d H:i') }} +No articles found for this topic.
+{% endif %} +{% endblock %}