Skip to content

Commit

Permalink
if inbox relays don't work (due to not being writeable, use regular r…
Browse files Browse the repository at this point in the history
…elays)
  • Loading branch information
believethehype committed Aug 29, 2024
1 parent 947b590 commit 42319ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
41 changes: 40 additions & 1 deletion nostr_dvm/utils/nostr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ async def get_inbox_relays(event_to_send: Event, client: Client, dvm_config):
return relays


async def get_main_relays(event_to_send: Event, client: Client, dvm_config):
ptags = []
for tag in event_to_send.tags():
if tag.as_vec()[0] == 'p':
ptag = PublicKey.parse(tag.as_vec()[1])
ptags.append(ptag)

filter = Filter().kinds([EventDefinitions.KIND_FOLLOW_LIST]).authors(ptags)
events = await client.get_events_of([filter], timedelta(dvm_config.RELAY_TIMEOUT))
if len(events) == 0:
return []
else:
followlist = events[0]
content = json.loads(followlist.content())
relays = []
for relay in content:
relays.append(relay)
return relays



async def send_event_outbox(event: Event, client, dvm_config) -> EventId:

# 1. OK, Let's overcomplicate things.
Expand All @@ -167,7 +188,8 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
# 4. If we don't find inbox relays (e.g. because the user didn't announce them, we just send to our default relays
if len(relays) == 0:
print("[" + dvm_config.NIP89.NAME + "] No Inbox found, replying to generic relays")
relays = dvm_config.RELAY_LIST
relays = await get_main_relays(event, client, dvm_config)

#eventid = await send_event(event, client, dvm_config)
#return eventid

Expand Down Expand Up @@ -196,6 +218,23 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
event_id = None
print(e)

# 5. Fallback, if we couldn't send the event to any relay, we try to send to generic relays instead.
if event_id is None:
for relay in relays:
outboxclient.remove_relay(relay)

relays = await get_main_relays(event, client, dvm_config)
for relay in relays:
opts = RelayOptions().ping(False)
await outboxclient.add_relay_with_opts(relay, opts)
try:
event_id = await outboxclient.send_event(event)
except Exception as e:
# Love yourself then.
event_id = None
print(e)


await outboxclient.shutdown()
return event_id

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.8.9'
VERSION = '0.8.10'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. See the github repository for more information')

Expand Down

0 comments on commit 42319ab

Please sign in to comment.