Fix relay URL handling for nostr-sdk 0.43

This commit is contained in:
thePR0M3TH3AN
2025-07-28 13:55:55 -04:00
parent 2d39d7a5bd
commit a3d45a117c
2 changed files with 18 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ from nostr_sdk import (
Kind,
KindStandard,
Tag,
RelayUrl,
)
from datetime import timedelta
from nostr_sdk import EventId, Timestamp
@@ -173,14 +174,26 @@ class NostrClient:
async def _initialize_client_pool(self) -> None:
if self.offline_mode or not self.relays:
return
formatted = []
for relay in self.relays:
if isinstance(relay, str):
try:
formatted.append(RelayUrl.parse(relay))
except Exception:
logger.error("Invalid relay URL: %s", relay)
else:
formatted.append(relay)
if hasattr(self.client, "add_relays"):
await self.client.add_relays(self.relays)
await self.client.add_relays(formatted)
else:
for relay in self.relays:
for relay in formatted:
await self.client.add_relay(relay)
await self.client.connect()
self._connected = True
logger.info(f"NostrClient connected to relays: {self.relays}")
logger.info("NostrClient connected to relays: %s", formatted)
async def _ping_relay(self, relay: str, timeout: float) -> bool:
"""Attempt to retrieve the latest event from a single relay."""

View File

@@ -91,7 +91,7 @@ def test_initialize_client_pool_add_relays_used(tmp_path):
client = _setup_client(tmp_path, FakeAddRelaysClient)
fc = client.client
client.connect()
assert fc.added == [client.relays]
assert [[str(r) for r in relays] for relays in fc.added] == [client.relays]
assert fc.connected is True
@@ -99,7 +99,7 @@ def test_initialize_client_pool_add_relay_fallback(tmp_path):
client = _setup_client(tmp_path, FakeAddRelayClient)
fc = client.client
client.connect()
assert fc.added == client.relays
assert [str(r) for r in fc.added] == client.relays
assert fc.connected is True