From 0f24c56473de2393d15bc641ff66c1ec4a6f8d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Thu, 31 Jul 2025 18:28:21 +0200 Subject: [PATCH] Use config for default relay --- config/services.yaml | 6 +++-- src/Service/NostrClient.php | 44 +++++++++++-------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index e4217eb..cf1cfd4 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -4,8 +4,7 @@ # 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 parameters: - encryption_key: '%env(APP_ENCRYPTION_KEY)%' - nsec: '%env(APP_NSEC)%' + default_relay_url: '%env(DEFAULT_RELAY_URL)%' services: # default configuration for services in *this* file @@ -27,3 +26,6 @@ services: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler: arguments: - '%env(DATABASE_URL)%' + App\Service\NostrClient: + arguments: + $defaultRelayUrl: '%default_relay_url%' diff --git a/src/Service/NostrClient.php b/src/Service/NostrClient.php index 656bcf1..267cfe7 100644 --- a/src/Service/NostrClient.php +++ b/src/Service/NostrClient.php @@ -23,21 +23,15 @@ class NostrClient { 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, - private readonly ManagerRegistry $managerRegistry, - private readonly ArticleFactory $articleFactory, - private readonly TokenStorageInterface $tokenStorage, - private readonly LoggerInterface $logger) + private readonly ManagerRegistry $managerRegistry, + private readonly ArticleFactory $articleFactory, + private readonly TokenStorageInterface $tokenStorage, + private readonly LoggerInterface $logger, + private readonly string $defaultRelayUrl) { $this->defaultRelaySet = new RelaySet(); - $this->defaultRelaySet->addRelay(new Relay('wss://theforest.nostr1.com')); + $this->defaultRelaySet->addRelay(new Relay($this->defaultRelayUrl)); } /** @@ -68,26 +62,14 @@ class NostrClient $authorRelays = []; } if (empty($authorRelays)) { - return [self::REPUTABLE_RELAYS[0]]; // Default to theforest if no author relays - } - - $reputableAuthorRelays = []; - foreach (self::REPUTABLE_RELAYS as $relay) { - if (in_array($relay, $authorRelays) && count($reputableAuthorRelays) < $limit) { - $reputableAuthorRelays[] = $relay; - } + return [$this->defaultRelayUrl]; // Default to theforest if no author relays } - // If no reputable relays found in author's list, take the top 3 from author's list - // But make sure they start with wss: and are not localhost - if (empty($reputableAuthorRelays)) { - $authorRelays = array_filter($authorRelays, function ($relay) { - return str_starts_with($relay, 'wss:') && !str_contains($relay, 'localhost'); - }); - return array_slice($authorRelays, 0, $limit); - } - - return $reputableAuthorRelays; + // Can only keep wss relays + $authorRelays = array_filter($authorRelays, function ($relay) { + return str_starts_with($relay, 'wss:') && !str_contains($relay, 'localhost'); + }); + return array_slice($authorRelays, 0, $limit); } public function getNpubMetadata($npub): \stdClass @@ -619,7 +601,7 @@ class NostrClient // If no author relays found, add default relay if (empty($relayList)) { - $relayList = [self::REPUTABLE_RELAYS[0]]; + $relayList = [$this->defaultRelayUrl]; } // Ensure we use a RelaySet