subscriptions / sdk update work in progress

This commit is contained in:
Believethehype
2024-03-18 19:44:44 +01:00
parent 53658548e3
commit 41785313a4
45 changed files with 1312 additions and 307 deletions

View File

@@ -7,7 +7,7 @@ from dataclasses import dataclass
from datetime import timedelta
from logging import Filter
from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Filter
from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Filter, Kind
from nostr_dvm.utils.nostr_utils import send_event
@@ -21,6 +21,7 @@ class User:
nip05: str
lud16: str
lastactive: int
subscribed: int
def create_sql_table(db):
@@ -40,7 +41,8 @@ def create_sql_table(db):
nip05 text,
lud16 text,
name text,
lastactive integer
lastactive integer,
subscribed integer
); """)
cur.execute("SELECT name FROM sqlite_master")
con.close()
@@ -53,29 +55,29 @@ def add_sql_table_column(db):
try:
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute(""" ALTER TABLE users ADD COLUMN lastactive 'integer' """)
cur.execute(""" ALTER TABLE users ADD COLUMN subscribed 'integer' """)
con.close()
except Error as e:
print(e)
def add_to_sql_table(db, npub, sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive):
def add_to_sql_table(db, npub, sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, subscribed):
try:
con = sqlite3.connect(db)
cur = con.cursor()
data = (npub, sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive)
cur.execute("INSERT or IGNORE INTO users VALUES(?, ?, ?, ?, ?, ?, ?, ?)", data)
data = (npub, sats, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, subscribed)
cur.execute("INSERT or IGNORE INTO users VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)", data)
con.commit()
con.close()
except Error as e:
print("Error when Adding to DB: " + str(e))
def update_sql_table(db, npub, balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive):
def update_sql_table(db, npub, balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, subscribed):
try:
con = sqlite3.connect(db)
cur = con.cursor()
data = (balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, npub)
data = (balance, iswhitelisted, isblacklisted, nip05, lud16, name, lastactive, subscribed, npub)
cur.execute(""" UPDATE users
SET sats = ? ,
@@ -84,7 +86,8 @@ def update_sql_table(db, npub, balance, iswhitelisted, isblacklisted, nip05, lud
nip05 = ? ,
lud16 = ? ,
name = ? ,
lastactive = ?
lastactive = ?,
subscribed = ?
WHERE npub = ?""", data)
con.commit()
con.close()
@@ -111,6 +114,7 @@ def get_from_sql_table(db, npub):
user.lud16 = row[5]
user.name = row[6]
user.lastactive = row[7]
user.subscribed = row[8]
return user
@@ -162,14 +166,14 @@ def update_user_balance(db, npub, additional_sats, client, config):
if user is None:
name, nip05, lud16 = fetch_user_metadata(npub, client)
add_to_sql_table(db, npub, (int(additional_sats) + config.NEW_USER_BALANCE), False, False,
nip05, lud16, name, Timestamp.now().as_secs())
nip05, lud16, name, Timestamp.now().as_secs(), 0)
print("Adding User: " + npub + " (" + npub + ")")
else:
user = get_from_sql_table(db, npub)
new_balance = int(user.balance) + int(additional_sats)
update_sql_table(db, npub, new_balance, user.iswhitelisted, user.isblacklisted, user.nip05, user.lud16,
user.name,
Timestamp.now().as_secs())
Timestamp.now().as_secs(), user.subscribed)
print("Updated user balance for: " + str(user.name) +
" Zap amount: " + str(additional_sats) + " Sats. New balance: " + str(new_balance) +" Sats")
@@ -184,6 +188,33 @@ def update_user_balance(db, npub, additional_sats, client, config):
send_event(evt, client=client, dvm_config=config)
def update_user_subscription(npub, subscribed_until, client, dvm_config):
user = get_from_sql_table(dvm_config.DB, npub)
if user is None:
name, nip05, lud16 = fetch_user_metadata(npub, client)
add_to_sql_table(dvm_config.DB, npub, dvm_config.NEW_USER_BALANCE, False, False,
nip05, lud16, name, Timestamp.now().as_secs(), 0)
print("Adding User: " + npub + " (" + npub + ")")
else:
user = get_from_sql_table(dvm_config.DB, npub)
update_sql_table(dvm_config.DB, npub, user.balance, user.iswhitelisted, user.isblacklisted, user.nip05, user.lud16,
user.name,
Timestamp.now().as_secs(), subscribed_until)
print("Updated user subscription for: " + str(user.name))
if dvm_config is not None:
keys = Keys.parse(dvm_config.PRIVATE_KEY)
#time.sleep(1.0)
message = ("Subscribed to DVM " + dvm_config.NIP89.NAME + " until: " + str(Timestamp.from_secs(subscribed_until).to_human_datetime().replace("Z", " ").replace("T", " ")))
evt = EventBuilder.encrypted_direct_msg(keys, PublicKey.from_hex(npub), message,
None).to_event(keys)
send_event(evt, client=client, dvm_config=dvm_config)
def get_or_add_user(db, npub, client, config, update=False):
user = get_from_sql_table(db, npub)
if user is None:
@@ -191,7 +222,7 @@ def get_or_add_user(db, npub, client, config, update=False):
name, nip05, lud16 = fetch_user_metadata(npub, client)
print("Adding User: " + npub + " (" + npub + ")")
add_to_sql_table(db, npub, config.NEW_USER_BALANCE, False, False, nip05,
lud16, name, Timestamp.now().as_secs())
lud16, name, Timestamp.now().as_secs(), 0)
user = get_from_sql_table(db, npub)
return user
except Exception as e:
@@ -201,7 +232,7 @@ def get_or_add_user(db, npub, client, config, update=False):
name, nip05, lud16 = fetch_user_metadata(npub, client)
print("Updating User: " + npub + " (" + npub + ")")
update_sql_table(db, user.npub, user.balance, user.iswhitelisted, user.isblacklisted, nip05,
lud16, name, Timestamp.now().as_secs())
lud16, name, Timestamp.now().as_secs(), user.subscribed)
user = get_from_sql_table(db, npub)
return user
except Exception as e:
@@ -214,9 +245,9 @@ def fetch_user_metadata(npub, client):
name = ""
nip05 = ""
lud16 = ""
pk = PublicKey.from_hex(npub)
pk = PublicKey.parse(npub)
print(f"\nGetting profile metadata for {pk.to_bech32()}...")
profile_filter = Filter().kind(0).author(pk).limit(1)
profile_filter = Filter().kind(Kind(0)).author(pk).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=1))
if len(events) > 0:
latest_entry = events[0]