Browse Source

Wider net

imwald
Nuša Pukšič 4 days ago
parent
commit
488f9f3a44
  1. 15
      src/Controller/ArticleController.php
  2. 53
      src/Service/NostrClient.php

15
src/Controller/ArticleController.php

@ -51,8 +51,8 @@ class ArticleController extends AbstractController @@ -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 @@ -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.');
}

53
src/Service/NostrClient.php

@ -232,7 +232,7 @@ class NostrClient @@ -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 @@ -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()

Loading…
Cancel
Save