From 82a9cba87192f389a3cac8d404a10eabafdae44d Mon Sep 17 00:00:00 2001 From: benarc Date: Wed, 13 Oct 2021 12:37:10 +0100 Subject: [PATCH] Copilot working without websockets --- lnbits/extensions/copilot/crud.py | 18 +++++--- lnbits/extensions/copilot/migrations.py | 11 +++-- lnbits/extensions/copilot/models.py | 57 +++++++++++++------------ lnbits/extensions/copilot/views_api.py | 41 +++++++++--------- 4 files changed, 69 insertions(+), 58 deletions(-) diff --git a/lnbits/extensions/copilot/crud.py b/lnbits/extensions/copilot/crud.py index 7e508900..955561e4 100644 --- a/lnbits/extensions/copilot/crud.py +++ b/lnbits/extensions/copilot/crud.py @@ -32,9 +32,11 @@ async def create_copilot( show_message, show_ack, show_price, + fullscreen_cam, + iframe_url, amount_made ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( copilot_id, @@ -56,16 +58,20 @@ async def create_copilot( int(data.show_ack), data.show_price, 0, + None, + 0, ), ) return await get_copilot(copilot_id) -async def update_copilot(copilot_id: str, **kwargs) -> Optional[Copilots]: - q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) - await db.execute( - f"UPDATE copilot.copilots SET {q} WHERE id = ?", (*kwargs.values(), copilot_id) - ) +async def update_copilot( + data: CreateCopilotData, copilot_id: Optional[str] = "" +) -> Optional[Copilots]: + 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.copilots SET {q} WHERE id = ?", (items)) row = await db.fetchone( "SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,) ) diff --git a/lnbits/extensions/copilot/migrations.py b/lnbits/extensions/copilot/migrations.py index c1fbfc0d..7b23c936 100644 --- a/lnbits/extensions/copilot/migrations.py +++ b/lnbits/extensions/copilot/migrations.py @@ -23,7 +23,7 @@ async def m001_initial(db): lnurl_title TEXT, show_message INTEGER, show_ack INTEGER, - show_price INTEGER, + show_price TEXT, amount_made INTEGER, fullscreen_cam INTEGER, iframe_url TEXT, @@ -32,13 +32,16 @@ async def m001_initial(db): """ ) + async def m002_fix_data_types(db): """ Fix data types. """ - - if(db.type != "SQLITE"): - await db.execute("ALTER TABLE copilot.copilots ALTER COLUMN show_price TYPE TEXT;") + + if db.type != "SQLITE": + await db.execute( + "ALTER TABLE copilot.copilots ALTER COLUMN show_price TYPE TEXT;" + ) # If needed, migration for SQLite (RENAME not working properly) # diff --git a/lnbits/extensions/copilot/models.py b/lnbits/extensions/copilot/models.py index 6c866cc3..ce463d59 100644 --- a/lnbits/extensions/copilot/models.py +++ b/lnbits/extensions/copilot/models.py @@ -10,7 +10,7 @@ from pydantic import BaseModel class CreateCopilotData(BaseModel): user: str = Query(None) title: str = Query(None) - lnurl_toggle: int = Query(None) + lnurl_toggle: int = Query(0) wallet: str = Query(None) animation1: str = Query(None) animation2: str = Query(None) @@ -22,39 +22,40 @@ class CreateCopilotData(BaseModel): animation2webhook: str = Query(None) animation3webhook: str = Query(None) lnurl_title: str = Query(None) - show_message: int = Query(None) - show_ack: int = Query(None) + show_message: int = Query(0) + show_ack: int = Query(0) show_price: str = Query(None) - amount_made: int = Query(None) - timestamp: int = Query(None) - fullscreen_cam: int = Query(None) - iframe_url: int = Query(None) + amount_made: int = Query(0) + timestamp: int = Query(0) + fullscreen_cam: int = Query(0) + iframe_url: str = Query(None) success_url: str = Query(None) class Copilots(BaseModel): id: str - user: str - title: str - lnurl_toggle: int - wallet: str - animation1: str - animation2: str - animation3: str - animation1threshold: int - animation2threshold: int - animation3threshold: int - animation1webhook: str - animation2webhook: str - animation3webhook: str - lnurl_title: str - show_message: int - show_ack: int - show_price: int - amount_made: int - timestamp: int - fullscreen_cam: int - iframe_url: str + user: str = Query(None) + title: str = Query(None) + lnurl_toggle: int = Query(0) + wallet: str = Query(None) + animation1: str = Query(None) + animation2: str = Query(None) + animation3: str = Query(None) + animation1threshold: int = Query(None) + animation2threshold: int = Query(None) + animation3threshold: int = Query(None) + animation1webhook: str = Query(None) + animation2webhook: str = Query(None) + animation3webhook: str = Query(None) + lnurl_title: str = Query(None) + show_message: int = Query(0) + show_ack: int = Query(0) + show_price: str = Query(None) + amount_made: int = Query(0) + timestamp: int = Query(0) + fullscreen_cam: int = Query(0) + iframe_url: str = Query(None) + success_url: str = Query(None) def lnurl(self, req: Request) -> str: url = req.url_for("copilot.lnurl_response", link_id=self.id) diff --git a/lnbits/extensions/copilot/views_api.py b/lnbits/extensions/copilot/views_api.py index bf86ad54..74ce6291 100644 --- a/lnbits/extensions/copilot/views_api.py +++ b/lnbits/extensions/copilot/views_api.py @@ -35,19 +35,22 @@ from .crud import ( #######################COPILOT########################## -@copilot_ext.get("/api/v1/copilot", response_class=HTMLResponse) -async def api_copilots_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): +@copilot_ext.get("/api/v1/copilot") +async def api_copilots_retrieve( + req: Request, wallet: WalletTypeInfo = Depends(get_key_type) +): wallet_user = wallet.wallet.user copilots = [copilot.dict() for copilot in await get_copilots(wallet_user)] - if copilots: + try: return copilots - raise HTTPException( - status_code=HTTPStatus.NO_CONTENT, - detail="No Jukeboxes", - ) + except: + raise HTTPException( + status_code=HTTPStatus.NO_CONTENT, + detail="No copilots", + ) -@copilot_ext.get("/api/v1/copilot/{copilot_id}", response_class=HTMLResponse) +@copilot_ext.get("/api/v1/copilot/{copilot_id}") async def api_copilot_retrieve( copilot_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) ): @@ -62,23 +65,23 @@ async def api_copilot_retrieve( return {**copilot.dict(), **{"lnurl": copilot.lnurl}} -@copilot_ext.post("/api/v1/copilot", response_class=HTMLResponse) -@copilot_ext.put("/api/v1/copilot/{juke_id}", response_class=HTMLResponse) +@copilot_ext.post("/api/v1/copilot") +@copilot_ext.put("/api/v1/copilot/{juke_id}") async def api_copilot_create_or_update( data: CreateCopilotData, copilot_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type), ): - - if not copilot_id: - copilot = await create_copilot(data, inkey=wallet.wallet.inkey) - return copilot, HTTPStatus.CREATED - else: + data.user = wallet.wallet.user + data.wallet = wallet.wallet.id + if copilot_id: copilot = await update_copilot(data, copilot_id=copilot_id) - return copilot, HTTPStatus.NOT_FOUND + else: + copilot = await create_copilot(data, inkey=wallet.wallet.inkey) + return copilot -@copilot_ext.delete("/api/v1/copilot/{copilot_id}", response_class=HTMLResponse) +@copilot_ext.delete("/api/v1/copilot/{copilot_id}") async def api_copilot_delete( copilot_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type), @@ -96,9 +99,7 @@ async def api_copilot_delete( return "", HTTPStatus.NO_CONTENT -@copilot_ext.get( - "/api/v1/copilot/ws/{copilot_id}/{comment}/{data}", response_class=HTMLResponse -) +@copilot_ext.get("/api/v1/copilot/ws/{copilot_id}/{comment}/{data}") async def api_copilot_ws_relay( copilot_id: str = Query(None), comment: str = Query(None),