From 88c5f16cfd4dc237f1023546927fa7d05d7c3a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Wed, 3 Dec 2025 12:13:41 +0100 Subject: [PATCH] Fix disambiguation --- src/Controller/ArticleController.php | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php index e3314c7..3a43673 100644 --- a/src/Controller/ArticleController.php +++ b/src/Controller/ArticleController.php @@ -75,29 +75,50 @@ class ArticleController extends AbstractController { $slug = urldecode($slug); $repository = $entityManager->getRepository(Article::class); - $articles = $repository->findBy(['slug' => $slug]); + $articles = $repository->findBy(['slug' => $slug], ['createdAt' => 'DESC']); $count = count($articles); if ($count === 0) { throw $this->createNotFoundException('No articles found for this slug'); } - if ($count === 1) { + + // Group articles by author (pubkey) + $articlesByAuthor = []; + foreach ($articles as $article) { + $pubkey = $article->getPubkey(); + if (!isset($articlesByAuthor[$pubkey])) { + $articlesByAuthor[$pubkey] = []; + } + $articlesByAuthor[$pubkey][] = $article; + } + + $uniqueAuthors = count($articlesByAuthor); + + // If only one author, redirect to their most recent article (already sorted by createdAt DESC) + if ($uniqueAuthors === 1) { $key = new Key(); $npub = $key->convertPublicKeyToBech32($articles[0]->getPubkey()); return $this->redirectToRoute('author-article-slug', ['npub' => $npub, 'slug' => $slug]); } + + // Multiple authors: show disambiguation page with one article per author (most recent) $authors = []; $key = new Key(); - foreach ($articles as $article) { + $uniqueArticles = []; + foreach ($articlesByAuthor as $pubkey => $authorArticles) { + // Get the most recent article for this author (first in array due to DESC sort) + $mostRecentArticle = $authorArticles[0]; + $uniqueArticles[] = $mostRecentArticle; $authors[] = [ - 'npub' => $key->convertPublicKeyToBech32($article->getPubkey()), - 'pubkey' => $article->getPubkey(), - 'createdAt' => $article->getCreatedAt(), + 'npub' => $key->convertPublicKeyToBech32($pubkey), + 'pubkey' => $pubkey, + 'createdAt' => $mostRecentArticle->getCreatedAt(), ]; } + return $this->render('pages/article_disambiguation.html.twig', [ 'slug' => $slug, 'authors' => $authors, - 'articles' => $articles + 'articles' => $uniqueArticles ]); }