Browse Source

Use config for default relay

imwald
Nuša Pukšič 9 months ago
parent
commit
0f24c56473
  1. 6
      config/services.yaml
  2. 30
      src/Service/NostrClient.php

6
config/services.yaml

@ -4,8 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed # Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters: parameters:
encryption_key: '%env(APP_ENCRYPTION_KEY)%' default_relay_url: '%env(DEFAULT_RELAY_URL)%'
nsec: '%env(APP_NSEC)%'
services: services:
# default configuration for services in *this* file # default configuration for services in *this* file
@ -27,3 +26,6 @@ services:
Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
arguments: arguments:
- '%env(DATABASE_URL)%' - '%env(DATABASE_URL)%'
App\Service\NostrClient:
arguments:
$defaultRelayUrl: '%default_relay_url%'

30
src/Service/NostrClient.php

@ -23,21 +23,15 @@ class NostrClient
{ {
private RelaySet $defaultRelaySet; private RelaySet $defaultRelaySet;
/**
* List of reputable relays in descending order of reputation
*/
private const REPUTABLE_RELAYS = [
'wss://theforest.nostr1.com',
];
public function __construct(private readonly EntityManagerInterface $entityManager, public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly ManagerRegistry $managerRegistry, private readonly ManagerRegistry $managerRegistry,
private readonly ArticleFactory $articleFactory, private readonly ArticleFactory $articleFactory,
private readonly TokenStorageInterface $tokenStorage, private readonly TokenStorageInterface $tokenStorage,
private readonly LoggerInterface $logger) private readonly LoggerInterface $logger,
private readonly string $defaultRelayUrl)
{ {
$this->defaultRelaySet = new RelaySet(); $this->defaultRelaySet = new RelaySet();
$this->defaultRelaySet->addRelay(new Relay('wss://theforest.nostr1.com')); $this->defaultRelaySet->addRelay(new Relay($this->defaultRelayUrl));
} }
/** /**
@ -68,28 +62,16 @@ class NostrClient
$authorRelays = []; $authorRelays = [];
} }
if (empty($authorRelays)) { if (empty($authorRelays)) {
return [self::REPUTABLE_RELAYS[0]]; // Default to theforest if no author relays return [$this->defaultRelayUrl]; // Default to theforest if no author relays
}
$reputableAuthorRelays = [];
foreach (self::REPUTABLE_RELAYS as $relay) {
if (in_array($relay, $authorRelays) && count($reputableAuthorRelays) < $limit) {
$reputableAuthorRelays[] = $relay;
}
} }
// If no reputable relays found in author's list, take the top 3 from author's list // Can only keep wss relays
// But make sure they start with wss: and are not localhost
if (empty($reputableAuthorRelays)) {
$authorRelays = array_filter($authorRelays, function ($relay) { $authorRelays = array_filter($authorRelays, function ($relay) {
return str_starts_with($relay, 'wss:') && !str_contains($relay, 'localhost'); return str_starts_with($relay, 'wss:') && !str_contains($relay, 'localhost');
}); });
return array_slice($authorRelays, 0, $limit); return array_slice($authorRelays, 0, $limit);
} }
return $reputableAuthorRelays;
}
public function getNpubMetadata($npub): \stdClass public function getNpubMetadata($npub): \stdClass
{ {
$this->logger->info('Getting metadata for npub', ['npub' => $npub]); $this->logger->info('Getting metadata for npub', ['npub' => $npub]);
@ -619,7 +601,7 @@ class NostrClient
// If no author relays found, add default relay // If no author relays found, add default relay
if (empty($relayList)) { if (empty($relayList)) {
$relayList = [self::REPUTABLE_RELAYS[0]]; $relayList = [$this->defaultRelayUrl];
} }
// Ensure we use a RelaySet // Ensure we use a RelaySet

Loading…
Cancel
Save