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

View File

@@ -50,7 +50,7 @@ def admin_make_database_updates(adminconfig: AdminConfig = None, dvmconfig: DVMC
if whitelistuser: 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) 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) user = get_from_sql_table(db, publickey)
print(str(user.name) + " is whitelisted: " + str(user.iswhitelisted)) 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) send_event(evt, client=client, dvm_config=config)
def get_or_add_user(db, sender): def get_or_add_user(db, npub, client):
user = get_from_sql_table(db, sender) user = get_from_sql_table(db, npub)
if user is None: if user is None:
print("Adding User")
add_to_sql_table(db, sender, NEW_USER_BALANCE, False, False, None, name, nip05, lud16 = fetch_user_metadata(npub, client)
None, None, Timestamp.now().as_secs()) print("Adding User: " + name + " (" + npub +")")
user = get_from_sql_table(db, sender) 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) 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 return user
def update_user_metadata(db, sender, client): def fetch_user_metadata(sender, client) -> (str, str, str):
user = get_from_sql_table(db, sender) name = ""
nip05 = ""
lud16 = ""
try: try:
profile_filter = Filter().kind(0).author(sender).limit(1) profile_filter = Filter().kind(0).author(sender).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=3)) 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()) metadata = Metadata.from_json(ev.content())
name = metadata.get_display_name() name = metadata.get_display_name()
if str(name) == "" or name is None: if str(name) == "" or name is None:
user.name = metadata.get_name() name = metadata.get_name()
user.nip05 = metadata.get_nip05() nip05 = metadata.get_nip05()
user.lud16 = metadata.get_lud16() lud16 = metadata.get_lud16()
except: except:
print("Couldn't get meta information") 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()) return name, nip05, lud16
user = get_from_sql_table(db, user.npub)
return user