Skip to content

Commit

Permalink
nostr receive split content by words
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 15, 2023
1 parent d2689bf commit a853c80
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions cashu/wallet/nostr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import threading
import time

import click
from loguru import logger
Expand Down Expand Up @@ -91,7 +90,7 @@ async def send_nostr(

client.dm(token, pubkey_to)
print(f"Token sent to {pubkey_to.bech32()}")
await asyncio.sleep(5)
await asyncio.sleep(1)
client.close()
return token, pubkey_to.bech32()

Expand All @@ -113,30 +112,41 @@ async def receive_nostr(
# print(f"Your nostr private key (do not share!): {client.private_key.bech32()}")
await asyncio.sleep(2)

def get_token_callback(event: Event, decrypted_content):
def get_token_callback(event: Event, decrypted_content: str):
logger.debug(
f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}"
)
try:
# call the receive method
tokenObj = deserialize_token_from_string(decrypted_content)
asyncio.run(
receive(
wallet,
tokenObj,
# split the content into words
words = decrypted_content.split(" ")
for w in words:
try:
logger.trace(
f"Nostr: setting last check timestamp to {event.created_at}"
)
)
except Exception as e:
logger.error(e)
pass
# call the receive method
tokenObj = deserialize_token_from_string(w)
asyncio.run(
receive(
wallet,
tokenObj,
)
)
asyncio.run(
set_nostr_last_check_timestamp(
timestamp=event.created_at, db=wallet.db
)
)

except Exception as e:
logger.debug(e)
pass

# determine timestamp of last check so we don't scan all historical DMs
last_check = await get_nostr_last_check_timestamp(db=wallet.db)
logger.debug(f"Last check: {last_check}")
if last_check:
last_check -= 60 * 60 # 1 hour tolerance

await set_nostr_last_check_timestamp(timestamp=int(time.time()), db=wallet.db)
logger.debug("Starting Nostr DM thread")
t = threading.Thread(
target=client.get_dm,
Expand Down

0 comments on commit a853c80

Please sign in to comment.