From 488f9f3a443df0a39cb7a314c1abdb49b38c73fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Wed, 7 Jan 2026 21:41:47 +0100 Subject: [PATCH] Wider net --- src/Controller/ArticleController.php | 15 ++++++-- src/Service/NostrClient.php | 53 +++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php index d085d86..e5ac03e 100644 --- a/src/Controller/ArticleController.php +++ b/src/Controller/ArticleController.php @@ -51,8 +51,8 @@ class ArticleController extends AbstractController throw new \Exception('Not a long form article'); } - $nostrClient->getLongFormFromNaddr($slug, $relays, $author, $kind); - // It's important to actually find the article + $found = $nostrClient->getLongFormFromNaddr($slug, $relays, $author, $kind); + // Check if anything is in the database now $repository = $em->getRepository(Article::class); $article = $repository->findOneBy(['slug' => $slug, 'pubkey' => $author]); @@ -61,7 +61,16 @@ class ArticleController extends AbstractController return $this->redirectToRoute('article-slug', ['slug' => $slug]); } - throw new \Exception('No article found.'); + // Provide a more informative error message + if (!$found) { + throw new \Exception(sprintf( + 'No article found for slug "%s" by author %s. The article may not exist or the relays may be offline.', + $slug, + substr($author, 0, 8) . '...' + )); + } + + throw new \Exception('Article was retrieved from relays but could not be saved to the database.'); } diff --git a/src/Service/NostrClient.php b/src/Service/NostrClient.php index 707110d..727015b 100644 --- a/src/Service/NostrClient.php +++ b/src/Service/NostrClient.php @@ -232,7 +232,7 @@ class NostrClient /** * @throws \Exception */ - public function getLongFormFromNaddr($slug, $relayList, $author, $kind): void + public function getLongFormFromNaddr($slug, $relayList, $author, $kind): bool { $this->logger->info('Getting long form from ' . $slug, [ 'relay_list' => $relayList, @@ -278,7 +278,58 @@ class NostrClient $wrapper->type = 'EVENT'; $wrapper->event = $event; $this->saveLongFormContent([$wrapper]); + return true; } + + // If no events found in the initial relay set, try fallback with additional reputable relays + $this->logger->info('No events found in initial relay set, trying fallback with additional relays', [ + 'slug' => $slug, + 'author' => $author + ]); + + // Merge the initial relay list with all reputable relays for a comprehensive search + $fallbackRelays = empty($relayList) + ? self::REPUTABLE_RELAYS + : array_unique(array_merge($relayList, self::REPUTABLE_RELAYS)); + + $fallbackRelaySet = $this->createRelaySet($fallbackRelays); + + $fallbackRequest = $this->createNostrRequest( + kinds: [$kind], + filters: [ + 'authors' => [$author], + 'tag' => ['#d', [$slug]] + ], + relaySet: $fallbackRelaySet + ); + + $fallbackEvents = $this->processResponse($fallbackRequest->send(), function($event) { + return $event; + }); + + if (!empty($fallbackEvents)) { + $this->logger->info('Found event in fallback relay search', [ + 'slug' => $slug, + 'author' => $author, + 'event_count' => count($fallbackEvents) + ]); + + // Save only the first event (most recent) + $event = $fallbackEvents[0]; + $wrapper = new \stdClass(); + $wrapper->type = 'EVENT'; + $wrapper->event = $event; + $this->saveLongFormContent([$wrapper]); + return true; + } + + $this->logger->warning('No events found even after fallback relay search', [ + 'slug' => $slug, + 'author' => $author, + 'tried_relays' => $fallbackRelays + ]); + + return false; } catch (\Exception $e) { $this->logger->error('Error querying relays', [ 'error' => $e->getMessage()