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 @@ @@ -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: @@ -27,3 +26,6 @@ services:
Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler:
arguments:
- '%env(DATABASE_URL)%'
App\Service\NostrClient:
arguments:
$defaultRelayUrl: '%default_relay_url%'

30
src/Service/NostrClient.php

@ -23,21 +23,15 @@ class NostrClient @@ -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 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,28 +62,16 @@ class NostrClient @@ -68,28 +62,16 @@ 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)) {
// 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);
}
return $reputableAuthorRelays;
}
public function getNpubMetadata($npub): \stdClass
{
$this->logger->info('Getting metadata for npub', ['npub' => $npub]);
@ -619,7 +601,7 @@ class NostrClient @@ -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

Loading…
Cancel
Save