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, Kind,
KindStandard, KindStandard,
Tag, Tag,
RelayUrl,
) )
from datetime import timedelta from datetime import timedelta
from nostr_sdk import EventId, Timestamp from nostr_sdk import EventId, Timestamp
@@ -173,14 +174,26 @@ class NostrClient:
async def _initialize_client_pool(self) -> None: async def _initialize_client_pool(self) -> None:
if self.offline_mode or not self.relays: if self.offline_mode or not self.relays:
return 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"): if hasattr(self.client, "add_relays"):
await self.client.add_relays(self.relays) await self.client.add_relays(formatted)
else: else:
for relay in self.relays: for relay in formatted:
await self.client.add_relay(relay) await self.client.add_relay(relay)
await self.client.connect() await self.client.connect()
self._connected = True 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: async def _ping_relay(self, relay: str, timeout: float) -> bool:
"""Attempt to retrieve the latest event from a single relay.""" """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) client = _setup_client(tmp_path, FakeAddRelaysClient)
fc = client.client fc = client.client
client.connect() 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 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) client = _setup_client(tmp_path, FakeAddRelayClient)
fc = client.client fc = client.client
client.connect() client.connect()
assert fc.added == client.relays assert [str(r) for r in fc.added] == client.relays
assert fc.connected is True assert fc.connected is True