2021-08-20 13:44:03 +02:00
|
|
|
from typing import List, Optional, Union
|
|
|
|
|
2023-01-06 10:22:58 +01:00
|
|
|
from lnbits.helpers import urlsafe_short_hash
|
2022-07-16 14:23:03 +02:00
|
|
|
|
2021-08-20 13:44:03 +02:00
|
|
|
from . import db
|
2022-07-16 14:23:03 +02:00
|
|
|
from .models import CreatePayLinkData, PayLink
|
2021-08-20 13:44:03 +02:00
|
|
|
|
|
|
|
|
2021-10-17 19:33:29 +02:00
|
|
|
async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
|
2023-01-06 10:22:58 +01:00
|
|
|
link_id = urlsafe_short_hash()[:6]
|
2022-06-10 15:35:35 +02:00
|
|
|
|
2023-01-06 10:22:58 +01:00
|
|
|
result = await db.execute(
|
2021-08-20 13:44:03 +02:00
|
|
|
f"""
|
|
|
|
INSERT INTO lnurlp.pay_links (
|
2023-01-06 10:22:58 +01:00
|
|
|
id,
|
2021-08-20 13:44:03 +02:00
|
|
|
wallet,
|
|
|
|
description,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
served_meta,
|
|
|
|
served_pr,
|
|
|
|
webhook_url,
|
2022-11-19 21:44:21 +01:00
|
|
|
webhook_headers,
|
|
|
|
webhook_body,
|
2021-08-20 13:44:03 +02:00
|
|
|
success_text,
|
|
|
|
success_url,
|
|
|
|
comment_chars,
|
2022-06-10 16:05:33 +02:00
|
|
|
currency,
|
|
|
|
fiat_base_multiplier
|
2021-08-20 13:44:03 +02:00
|
|
|
)
|
2023-01-06 10:22:58 +01:00
|
|
|
VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?)
|
2021-08-20 13:44:03 +02:00
|
|
|
""",
|
|
|
|
(
|
2023-01-06 10:22:58 +01:00
|
|
|
link_id,
|
2021-08-20 13:44:03 +02:00
|
|
|
wallet_id,
|
2021-10-05 10:19:21 +02:00
|
|
|
data.description,
|
|
|
|
data.min,
|
|
|
|
data.max,
|
|
|
|
data.webhook_url,
|
2022-11-19 21:44:21 +01:00
|
|
|
data.webhook_headers,
|
|
|
|
data.webhook_body,
|
2021-10-05 10:19:21 +02:00
|
|
|
data.success_text,
|
|
|
|
data.success_url,
|
|
|
|
data.comment_chars,
|
|
|
|
data.currency,
|
2022-06-10 16:05:33 +02:00
|
|
|
data.fiat_base_multiplier,
|
2021-08-20 13:44:03 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
assert link, "Newly created link couldn't be retrieved"
|
|
|
|
return link
|
|
|
|
|
|
|
|
|
2023-01-06 10:22:58 +01:00
|
|
|
async def get_pay_link(link_id: str) -> Optional[PayLink]:
|
2021-08-20 13:44:03 +02:00
|
|
|
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
|
|
|
|
return PayLink.from_row(row) if row else None
|
|
|
|
|
|
|
|
|
|
|
|
async def get_pay_links(wallet_ids: Union[str, List[str]]) -> List[PayLink]:
|
|
|
|
if isinstance(wallet_ids, str):
|
|
|
|
wallet_ids = [wallet_ids]
|
|
|
|
|
|
|
|
q = ",".join(["?"] * len(wallet_ids))
|
|
|
|
rows = await db.fetchall(
|
|
|
|
f"""
|
|
|
|
SELECT * FROM lnurlp.pay_links WHERE wallet IN ({q})
|
|
|
|
ORDER BY Id
|
|
|
|
""",
|
|
|
|
(*wallet_ids,),
|
|
|
|
)
|
|
|
|
return [PayLink.from_row(row) for row in rows]
|
|
|
|
|
|
|
|
|
|
|
|
async def update_pay_link(link_id: int, **kwargs) -> Optional[PayLink]:
|
|
|
|
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
|
|
|
|
await db.execute(
|
|
|
|
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
|
|
|
|
)
|
|
|
|
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
|
|
|
|
return PayLink.from_row(row) if row else None
|
|
|
|
|
|
|
|
|
|
|
|
async def increment_pay_link(link_id: int, **kwargs) -> Optional[PayLink]:
|
|
|
|
q = ", ".join([f"{field[0]} = {field[0]} + ?" for field in kwargs.items()])
|
|
|
|
await db.execute(
|
|
|
|
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
|
|
|
|
)
|
|
|
|
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
|
|
|
|
return PayLink.from_row(row) if row else None
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_pay_link(link_id: int) -> None:
|
|
|
|
await db.execute("DELETE FROM lnurlp.pay_links WHERE id = ?", (link_id,))
|