diff --git a/lnbits/extensions/tipjar/crud.py b/lnbits/extensions/tipjar/crud.py index 1d30fe23c..e9fe53f9c 100644 --- a/lnbits/extensions/tipjar/crud.py +++ b/lnbits/extensions/tipjar/crud.py @@ -8,7 +8,14 @@ from typing import Optional from lnbits.db import SQLITE -async def create_tip(data: createTip) -> Tip: +async def create_tip( + id: int, + wallet: str, + message: str, + name: str, + sats: int, + tipjar: str, +) -> Tip: """Create a new Tip""" await db.execute( """ @@ -22,10 +29,10 @@ async def create_tip(data: createTip) -> Tip: ) VALUES (?, ?, ?, ?, ?, ?) """, - (data.id, data.wallet, data.name, data.message, data.sats, data.tipjar), + (id, wallet, name, message, sats, tipjar), ) - tip = await get_tip(data.id) + tip = await get_tip(id) assert tip, "Newly created tip couldn't be retrieved" return tip @@ -62,7 +69,7 @@ async def create_tipjar(data: createTipJar) -> TipJar: async def get_tipjar(tipjar_id: int) -> Optional[TipJar]: """Return a tipjar by ID""" row = await db.fetchone("SELECT * FROM tipjar.TipJars WHERE id = ?", (tipjar_id,)) - return TipJar.from_row(row) if row else None + return TipJar(**row) if row else None async def get_tipjars(wallet_id: str) -> Optional[list]: @@ -70,7 +77,7 @@ async def get_tipjars(wallet_id: str) -> Optional[list]: rows = await db.fetchall( "SELECT * FROM tipjar.TipJars WHERE wallet = ?", (wallet_id,) ) - return [TipJar.from_row(row) for row in rows] if rows else None + return [TipJar(**row) for row in rows] if rows else None async def delete_tipjar(tipjar_id: int) -> None: @@ -84,13 +91,13 @@ async def delete_tipjar(tipjar_id: int) -> None: async def get_tip(tip_id: str) -> Optional[Tip]: """Return a Tip""" row = await db.fetchone("SELECT * FROM tipjar.Tips WHERE id = ?", (tip_id,)) - return Tip.from_row(row) if row else None + return Tip(**row) if row else None async def get_tips(wallet_id: str) -> Optional[list]: """Return all Tips assigned to wallet_id""" rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE wallet = ?", (wallet_id,)) - return [Tip.from_row(row) for row in rows] if rows else None + return [Tip(**row) for row in rows] if rows else None async def delete_tip(tip_id: str) -> None: diff --git a/lnbits/extensions/tipjar/templates/tipjar/display.html b/lnbits/extensions/tipjar/templates/tipjar/display.html index 87bce0f4f..80e5c6fe3 100644 --- a/lnbits/extensions/tipjar/templates/tipjar/display.html +++ b/lnbits/extensions/tipjar/templates/tipjar/display.html @@ -66,21 +66,23 @@ sats: '', message: '' } - }, + } } }, methods: { Invoice: function () { var self = this + console.log('{{ tipjar }}') axios .post('/tipjar/api/v1/tips', { - tipjar: {{ tipjar }}, + tipjar: '{{ tipjar }}', name: self.tipDialog.data.name, sats: self.tipDialog.data.sats, message: self.tipDialog.data.message }) .then(function (response) { + console.log(response.data) self.redirect_url = response.data.redirect_url console.log(self.redirect_url) window.location.href = self.redirect_url diff --git a/lnbits/extensions/tipjar/views.py b/lnbits/extensions/tipjar/views.py index 03e939934..7038862dc 100644 --- a/lnbits/extensions/tipjar/views.py +++ b/lnbits/extensions/tipjar/views.py @@ -32,10 +32,11 @@ async def index(request: Request, user: User = Depends(check_user_exists)): ) -@tipjar_ext.route("/{id}") -async def tip(request: Request, id: str = Query(None)): +@tipjar_ext.get("/{tipjar_id}") +async def tip(request: Request, tipjar_id: int = Query(None)): """Return the donation form for the Tipjar corresponding to id""" - tipjar = await get_tipjar(id) + tipjar = await get_tipjar(tipjar_id) + print(tipjar_id) if not tipjar: raise HTTPException( status_code=HTTPStatus.NOT_FOUND, detail="TipJar does not exist." diff --git a/lnbits/extensions/tipjar/views_api.py b/lnbits/extensions/tipjar/views_api.py index 1c409db07..3bbbbefac 100644 --- a/lnbits/extensions/tipjar/views_api.py +++ b/lnbits/extensions/tipjar/views_api.py @@ -34,25 +34,30 @@ from .models import createTipJar, createTips, createTip async def api_create_tipjar(data: createTipJar): """Create a tipjar, which holds data about how/where to post tips""" try: - tipjar = await create_tipjar(**data) + tipjar = await create_tipjar(data) except Exception as e: raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e)) return tipjar.dict() +async def user_from_wallet(wallet: WalletTypeInfo = Depends(get_key_type)): + return wallet.wallet.user + + @tipjar_ext.post("/api/v1/tips") -async def api_create_tip(data: createTips, dataCreateTip: createTip): +async def api_create_tip(data: createTips): """Take data from tip form and return satspay charge""" sats = data.sats - message = data.get("message", "")[:144] + message = data.message if not message: message = "No message" tipjar_id = data.tipjar tipjar = await get_tipjar(tipjar_id) + webhook = tipjar.webhook charge_details = await get_charge_details(tipjar.id) - name = data.get("name", "")[:25] + name = data.name # Ensure that description string can be split reliably name = name.replace('"', "''") if not name: @@ -60,18 +65,21 @@ async def api_create_tip(data: createTips, dataCreateTip: createTip): description = f'"{name}": {message}' charge = await create_charge( - amount=sats, - webhook=webhook, - description=description, - **charge_details, + data={ + "amount": sats, + "webhook": webhook, + "description": description, + **charge_details, + }, + ) + await create_tip( + id=charge.id, + wallet=tipjar.wallet, + message=message, + name=name, + sats=data.sats, + tipjar=data.tipjar, ) - dataCreateTip.id = charge.id - dataCreateTip.wallet = tipjar.wallet - dataCreateTip.message = message - dataCreateTip.name = name - dataCreateTip.sats = data.sats - dataCreateTip.tipjar = data.tipjar - await create_tip(dataCreateTip) return {"redirect_url": f"/satspay/{charge.id}"}