Copilot working without websockets

This commit is contained in:
benarc 2021-10-13 12:37:10 +01:00
parent 16f9f1612f
commit 82a9cba871
4 changed files with 69 additions and 58 deletions

View file

@ -32,9 +32,11 @@ async def create_copilot(
show_message, show_message,
show_ack, show_ack,
show_price, show_price,
fullscreen_cam,
iframe_url,
amount_made amount_made
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", """,
( (
copilot_id, copilot_id,
@ -56,16 +58,20 @@ async def create_copilot(
int(data.show_ack), int(data.show_ack),
data.show_price, data.show_price,
0, 0,
None,
0,
), ),
) )
return await get_copilot(copilot_id) return await get_copilot(copilot_id)
async def update_copilot(copilot_id: str, **kwargs) -> Optional[Copilots]: async def update_copilot(
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) data: CreateCopilotData, copilot_id: Optional[str] = ""
await db.execute( ) -> Optional[Copilots]:
f"UPDATE copilot.copilots SET {q} WHERE id = ?", (*kwargs.values(), copilot_id) 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( row = await db.fetchone(
"SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,) "SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,)
) )

View file

@ -23,7 +23,7 @@ async def m001_initial(db):
lnurl_title TEXT, lnurl_title TEXT,
show_message INTEGER, show_message INTEGER,
show_ack INTEGER, show_ack INTEGER,
show_price INTEGER, show_price TEXT,
amount_made INTEGER, amount_made INTEGER,
fullscreen_cam INTEGER, fullscreen_cam INTEGER,
iframe_url TEXT, iframe_url TEXT,
@ -32,13 +32,16 @@ async def m001_initial(db):
""" """
) )
async def m002_fix_data_types(db): async def m002_fix_data_types(db):
""" """
Fix data types. Fix data types.
""" """
if(db.type != "SQLITE"): if db.type != "SQLITE":
await db.execute("ALTER TABLE copilot.copilots ALTER COLUMN show_price TYPE TEXT;") await db.execute(
"ALTER TABLE copilot.copilots ALTER COLUMN show_price TYPE TEXT;"
)
# If needed, migration for SQLite (RENAME not working properly) # If needed, migration for SQLite (RENAME not working properly)
# #

View file

@ -10,7 +10,7 @@ from pydantic import BaseModel
class CreateCopilotData(BaseModel): class CreateCopilotData(BaseModel):
user: str = Query(None) user: str = Query(None)
title: str = Query(None) title: str = Query(None)
lnurl_toggle: int = Query(None) lnurl_toggle: int = Query(0)
wallet: str = Query(None) wallet: str = Query(None)
animation1: str = Query(None) animation1: str = Query(None)
animation2: str = Query(None) animation2: str = Query(None)
@ -22,39 +22,40 @@ class CreateCopilotData(BaseModel):
animation2webhook: str = Query(None) animation2webhook: str = Query(None)
animation3webhook: str = Query(None) animation3webhook: str = Query(None)
lnurl_title: str = Query(None) lnurl_title: str = Query(None)
show_message: int = Query(None) show_message: int = Query(0)
show_ack: int = Query(None) show_ack: int = Query(0)
show_price: str = Query(None) show_price: str = Query(None)
amount_made: int = Query(None) amount_made: int = Query(0)
timestamp: int = Query(None) timestamp: int = Query(0)
fullscreen_cam: int = Query(None) fullscreen_cam: int = Query(0)
iframe_url: int = Query(None) iframe_url: str = Query(None)
success_url: str = Query(None) success_url: str = Query(None)
class Copilots(BaseModel): class Copilots(BaseModel):
id: str id: str
user: str user: str = Query(None)
title: str title: str = Query(None)
lnurl_toggle: int lnurl_toggle: int = Query(0)
wallet: str wallet: str = Query(None)
animation1: str animation1: str = Query(None)
animation2: str animation2: str = Query(None)
animation3: str animation3: str = Query(None)
animation1threshold: int animation1threshold: int = Query(None)
animation2threshold: int animation2threshold: int = Query(None)
animation3threshold: int animation3threshold: int = Query(None)
animation1webhook: str animation1webhook: str = Query(None)
animation2webhook: str animation2webhook: str = Query(None)
animation3webhook: str animation3webhook: str = Query(None)
lnurl_title: str lnurl_title: str = Query(None)
show_message: int show_message: int = Query(0)
show_ack: int show_ack: int = Query(0)
show_price: int show_price: str = Query(None)
amount_made: int amount_made: int = Query(0)
timestamp: int timestamp: int = Query(0)
fullscreen_cam: int fullscreen_cam: int = Query(0)
iframe_url: str iframe_url: str = Query(None)
success_url: str = Query(None)
def lnurl(self, req: Request) -> str: def lnurl(self, req: Request) -> str:
url = req.url_for("copilot.lnurl_response", link_id=self.id) url = req.url_for("copilot.lnurl_response", link_id=self.id)

View file

@ -35,19 +35,22 @@ from .crud import (
#######################COPILOT########################## #######################COPILOT##########################
@copilot_ext.get("/api/v1/copilot", response_class=HTMLResponse) @copilot_ext.get("/api/v1/copilot")
async def api_copilots_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): async def api_copilots_retrieve(
req: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
wallet_user = wallet.wallet.user wallet_user = wallet.wallet.user
copilots = [copilot.dict() for copilot in await get_copilots(wallet_user)] copilots = [copilot.dict() for copilot in await get_copilots(wallet_user)]
if copilots: try:
return copilots return copilots
except:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NO_CONTENT, status_code=HTTPStatus.NO_CONTENT,
detail="No Jukeboxes", 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( async def api_copilot_retrieve(
copilot_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) 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}} return {**copilot.dict(), **{"lnurl": copilot.lnurl}}
@copilot_ext.post("/api/v1/copilot", response_class=HTMLResponse) @copilot_ext.post("/api/v1/copilot")
@copilot_ext.put("/api/v1/copilot/{juke_id}", response_class=HTMLResponse) @copilot_ext.put("/api/v1/copilot/{juke_id}")
async def api_copilot_create_or_update( async def api_copilot_create_or_update(
data: CreateCopilotData, data: CreateCopilotData,
copilot_id: str = Query(None), copilot_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
): ):
data.user = wallet.wallet.user
if not copilot_id: data.wallet = wallet.wallet.id
copilot = await create_copilot(data, inkey=wallet.wallet.inkey) if copilot_id:
return copilot, HTTPStatus.CREATED
else:
copilot = await update_copilot(data, copilot_id=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( async def api_copilot_delete(
copilot_id: str = Query(None), copilot_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
@ -96,9 +99,7 @@ async def api_copilot_delete(
return "", HTTPStatus.NO_CONTENT return "", HTTPStatus.NO_CONTENT
@copilot_ext.get( @copilot_ext.get("/api/v1/copilot/ws/{copilot_id}/{comment}/{data}")
"/api/v1/copilot/ws/{copilot_id}/{comment}/{data}", response_class=HTMLResponse
)
async def api_copilot_ws_relay( async def api_copilot_ws_relay(
copilot_id: str = Query(None), copilot_id: str = Query(None),
comment: str = Query(None), comment: str = Query(None),