Browse Source

Optimization od naddr fetch

imwald
Nuša Pukšič 7 months ago
parent
commit
302cab24c3
  1. 5
      src/Controller/ArticleController.php
  2. 65
      src/Service/NostrClient.php

5
src/Controller/ArticleController.php

@ -47,11 +47,6 @@ class ArticleController extends AbstractController
throw new \Exception('Not a long form article'); throw new \Exception('Not a long form article');
} }
if (empty($relays)) {
// get author npub relays from their config
$relays = $nostrClient->getNpubRelays($author);
}
$nostrClient->getLongFormFromNaddr($slug, $relays, $author, $kind); $nostrClient->getLongFormFromNaddr($slug, $relays, $author, $kind);
if ($slug) { if ($slug) {
return $this->redirectToRoute('article-slug', ['slug' => $slug]); return $this->redirectToRoute('article-slug', ['slug' => $slug]);

65
src/Service/NostrClient.php

@ -218,15 +218,12 @@ class NostrClient
*/ */
public function getLongFormFromNaddr($slug, $relayList, $author, $kind): void public function getLongFormFromNaddr($slug, $relayList, $author, $kind): void
{ {
if (!empty($relayList)) { if (empty($relayList)) {
// Filter out relays that exist in the REPUTABLE_RELAYS list $topAuthorRelays = $this->getTopReputableRelaysForAuthor($author);
$relayList = array_filter($relayList, function ($relay) { $authorRelaySet = $this->createRelaySet($topAuthorRelays);
// in array REPUTABLE_RELAYS } else {
return str_starts_with($relay, 'wss:') && !str_contains($relay, 'localhost'); $authorRelaySet = $this->createRelaySet($relayList);
});
$relaySet = $this->createRelaySet($relayList);
} }
$hasEvents = false;
try { try {
// Create request using the helper method for forest relay set // Create request using the helper method for forest relay set
@ -236,7 +233,7 @@ class NostrClient
'authors' => [$author], 'authors' => [$author],
'tag' => ['#d', [$slug]] 'tag' => ['#d', [$slug]]
], ],
relaySet: $relaySet ?? $this->defaultRelaySet relaySet: $authorRelaySet
); );
// Process the response // Process the response
@ -245,51 +242,15 @@ class NostrClient
}); });
if (!empty($events)) { if (!empty($events)) {
$this->saveLongFormContent(array_map(function($event) { // Save only the first event (most recent)
$wrapper = new \stdClass(); $event = $events[0];
$wrapper->type = 'EVENT'; $wrapper = new \stdClass();
$wrapper->event = $event; $wrapper->type = 'EVENT';
return $wrapper; $wrapper->event = $event;
}, $events)); $this->saveLongFormContent([$wrapper]);
$hasEvents = true;
}
// If no events found in theforest, try author's reputable relays
if (!$hasEvents) {
$topAuthorRelays = $this->getTopReputableRelaysForAuthor($author);
$authorRelaySet = $this->createRelaySet($topAuthorRelays);
$this->logger->info('No results, trying author relays', [
'relays' => $topAuthorRelays
]);
// Create request using the helper method for author relay set
$request = $this->createNostrRequest(
kinds: [$kind],
filters: [
'authors' => [$author],
'tag' => ['#d', [$slug]]
],
relaySet: $authorRelaySet
);
// Process the response
$events = $this->processResponse($request->send(), function($event) {
return $event;
});
if (!empty($events)) {
$this->saveLongFormContent(array_map(function($event) {
$wrapper = new \stdClass();
$wrapper->type = 'EVENT';
$wrapper->event = $event;
return $wrapper;
}, $events));
}
} }
} catch (\Exception $e) { } catch (\Exception $e) {
// If any error occurs, fall back to default relay set $this->logger->error('Error querying relays', [
$this->logger->error('Error querying relays, falling back to defaults', [
'error' => $e->getMessage() 'error' => $e->getMessage()
]); ]);
throw new \Exception('Error querying relays', 0, $e); throw new \Exception('Error querying relays', 0, $e);

Loading…
Cancel
Save