Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to lnbits 1.0.0 #21

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Streamer Copilot",
"short_description": "Video tips/animations/webhooks",
"tile": "/copilot/static/bitcoin-streaming.png",
"min_lnbits_version": "0.12.6",
"min_lnbits_version": "1.0.0",
"contributors": [
{
"name": "Ben Arc",
Expand Down
80 changes: 15 additions & 65 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,46 @@
from typing import List, Optional

from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from lnbits.helpers import insert_query, update_query, urlsafe_short_hash

from .models import Copilot, CreateCopilotData

db = Database("ext_copilot")


async def create_copilot(data: CreateCopilotData, inkey: Optional[str] = "") -> Copilot:
async def create_copilot(data: CreateCopilotData) -> Copilot:
copilot_id = urlsafe_short_hash()
copilot = Copilot(id=copilot_id, **data.dict())
await db.execute(
"""
INSERT INTO copilot.newer_copilots (
id,
"user",
lnurl_toggle,
wallet,
title,
animation1,
animation2,
animation3,
animation1threshold,
animation2threshold,
animation3threshold,
animation1webhook,
animation2webhook,
animation3webhook,
lnurl_title,
show_message,
show_ack,
show_price,
fullscreen_cam,
iframe_url,
amount_made
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
copilot_id,
data.user,
int(data.lnurl_toggle),
data.wallet,
data.title,
data.animation1,
data.animation2,
data.animation3,
data.animation1threshold,
data.animation2threshold,
data.animation3threshold,
data.animation1webhook,
data.animation2webhook,
data.animation3webhook,
data.lnurl_title,
int(data.show_message),
int(data.show_ack),
data.show_price,
0,
None,
0,
),
insert_query("copilot.newer_copilots", copilot),
copilot.dict(),
)
copilot = await get_copilot(copilot_id)
assert copilot, "Newly created copilot couldn't be retrieved"
return copilot


async def update_copilot(data: CreateCopilotData, copilot_id: str) -> Copilot:
q = ", ".join([f"{field[0]} = ?" for field in data])
items = [f"{field[1]}" for field in data]
items.append(copilot_id)
await db.execute(f"UPDATE copilot.newer_copilots SET {q} WHERE id = ?", (items,))
row = await db.fetchone(
"SELECT * FROM copilot.newer_copilots WHERE id = ?", (copilot_id,)
async def update_copilot(data: Copilot) -> Copilot:
copilot = await db.execute(
update_query("copilot.newer_copilots", data),
data.dict(),
)
assert row, "Updated copilot couldn't be retrieved"
return Copilot(**row)
return copilot


async def get_copilot(copilot_id: str) -> Optional[Copilot]:
row = await db.fetchone(
"SELECT * FROM copilot.newer_copilots WHERE id = ?", (copilot_id,)
"SELECT * FROM copilot.newer_copilots WHERE id = :id", {"id": copilot_id}
)
return Copilot(**row) if row else None


async def get_copilots(user: str) -> List[Copilot]:
rows = await db.fetchall(
'SELECT * FROM copilot.newer_copilots WHERE "user" = ?', (user,)
'SELECT * FROM copilot.newer_copilots WHERE "user" = :user', {"user": user}
)
return [Copilot(**row) for row in rows]


async def delete_copilot(copilot_id: str) -> None:
await db.execute("DELETE FROM copilot.newer_copilots WHERE id = ?", (copilot_id,))
await db.execute(
"DELETE FROM copilot.newer_copilots WHERE id = :id", {"id": copilot_id}
)
Loading