Fetch name/nip05/lud16

This commit is contained in:
Believethehype 2023-11-22 10:16:30 +01:00
parent 2286701453
commit d7a101d202
3 changed files with 28 additions and 20 deletions

7
dvm.py
View File

@ -74,18 +74,17 @@ class DVM:
return
def handle_nip90_job_event(nip90_event):
user = get_or_add_user(self.dvm_config.DB, nip90_event.pubkey().to_hex())
user = get_or_add_user(self.dvm_config.DB, nip90_event.pubkey().to_hex(), client=self.client)
task_supported, task, duration = check_task_is_supported(nip90_event, client=self.client,
get_duration=(not user.iswhitelisted),
config=self.dvm_config)
print(task)
if user.isblacklisted:
send_job_status_reaction(nip90_event, "error", client=self.client, dvm_config=self.dvm_config)
print("[" + self.dvm_config.NIP89.name + "] Request by blacklisted user, skipped")
elif task_supported:
print("Received new Task: " + task)
print("Received new Task: " + task + " from " + user.name)
amount = get_amount_per_task(task, self.dvm_config, duration)
if amount is None:
return
@ -155,7 +154,7 @@ class DVM:
else:
anon = True
print("Anonymous Zap received. Unlucky, I don't know from whom, and never will")
user = get_or_add_user(self.dvm_config.DB, sender)
user = get_or_add_user(self.dvm_config.DB, sender, client=self.client)
print(str(user))
if zapped_event is not None:

View File

@ -50,7 +50,7 @@ def admin_make_database_updates(adminconfig: AdminConfig = None, dvmconfig: DVMC
if whitelistuser:
user = get_or_add_user(db, publickey)
user = get_or_add_user(db, publickey, client=client)
update_sql_table(db, user.npub, user.balance, True, False, user.nip05, user.lud16, user.name, user.lastactive)
user = get_from_sql_table(db, publickey)
print(str(user.name) + " is whitelisted: " + str(user.iswhitelisted))

View File

@ -189,20 +189,29 @@ def update_user_balance(db, sender, sats, client, config):
send_event(evt, client=client, dvm_config=config)
def get_or_add_user(db, sender):
user = get_from_sql_table(db, sender)
def get_or_add_user(db, npub, client):
user = get_from_sql_table(db, npub)
if user is None:
print("Adding User")
add_to_sql_table(db, sender, NEW_USER_BALANCE, False, False, None,
None, None, Timestamp.now().as_secs())
user = get_from_sql_table(db, sender)
name, nip05, lud16 = fetch_user_metadata(npub, client)
print("Adding User: " + name + " (" + npub +")")
add_to_sql_table(db, npub, NEW_USER_BALANCE, False, False, nip05,
lud16, name, Timestamp.now().as_secs())
user = get_from_sql_table(db, npub)
print(user)
else:
# update Name, Nip05 and lud16 lnaddress
user.name, user.nip05, user.lud16 = fetch_user_metadata(npub, client)
update_sql_table(db, user.npub, user.balance, user.iswhitelisted, user.isblacklisted, user.nip05, user.lud16,
user.name, Timestamp.now().as_secs())
return user
def update_user_metadata(db, sender, client):
user = get_from_sql_table(db, sender)
def fetch_user_metadata(sender, client) -> (str, str, str):
name = ""
nip05 = ""
lud16 = ""
try:
profile_filter = Filter().kind(0).author(sender).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=3))
@ -211,12 +220,12 @@ def update_user_metadata(db, sender, client):
metadata = Metadata.from_json(ev.content())
name = metadata.get_display_name()
if str(name) == "" or name is None:
user.name = metadata.get_name()
user.nip05 = metadata.get_nip05()
user.lud16 = metadata.get_lud16()
name = metadata.get_name()
nip05 = metadata.get_nip05()
lud16 = metadata.get_lud16()
except:
print("Couldn't get meta information")
update_sql_table(db, user.npub, user.balance, user.iswhitelisted, user.isblacklisted, user.nip05, user.lud16,
user.name, Timestamp.now().as_secs())
user = get_from_sql_table(db, user.npub)
return user
return name, nip05, lud16