Skip to content

Commit 3c2af76

Browse files
committed
v1update
1 parent edf7914 commit 3c2af76

File tree

5 files changed

+32
-76
lines changed

5 files changed

+32
-76
lines changed

__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
from fastapi import APIRouter
4+
from lnbits.tasks import create_permanent_unique_task
45
from loguru import logger
56

67
from .crud import db
@@ -33,16 +34,8 @@ def eightball_stop():
3334

3435

3536
def eightball_start():
36-
from lnbits.tasks import create_permanent_unique_task
37-
3837
task = create_permanent_unique_task("ext_eightball", wait_for_paid_invoices)
3938
scheduled_tasks.append(task)
4039

4140

42-
__all__ = [
43-
"db",
44-
"eightball_ext",
45-
"eightball_static_files",
46-
"eightball_start",
47-
"eightball_stop",
48-
]
41+
__all__ = ["db", "eightball_ext", "eightball_static_files"]

crud.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
1-
from typing import Optional, Union
1+
from typing import List, Optional, Union
22

33
from lnbits.db import Database
4-
from lnbits.helpers import insert_query, update_query
54

65
from .models import EightBall
76

87
db = Database("ext_eightball")
98

109

1110
async def create_eightball(data: EightBall) -> EightBall:
12-
await db.execute(
13-
insert_query("eightball.maintable", data),
14-
(*data.dict().values(),),
15-
)
16-
return data
11+
return await db.insert("eightball.maintable", data)
1712

1813

1914
async def get_eightball(eightball_id: str) -> Optional[EightBall]:
20-
row = await db.fetchone(
21-
"SELECT * FROM eightball.maintable WHERE id = ?", (eightball_id,)
15+
return await db.fetchone(
16+
"SELECT * FROM eightball.maintable WHERE id = :id",
17+
{"id": eightball_id},
18+
EightBall,
2219
)
23-
return EightBall(**row) if row else None
2420

2521

26-
async def get_eightballs(wallet_ids: Union[str, list[str]]) -> list[EightBall]:
22+
async def get_eightballs(wallet_ids: Union[str, List[str]]) -> List[EightBall]:
2723
if isinstance(wallet_ids, str):
2824
wallet_ids = [wallet_ids]
29-
30-
q = ",".join(["?"] * len(wallet_ids))
31-
rows = await db.fetchall(
32-
f"SELECT * FROM eightball.maintable WHERE wallet IN ({q})", (*wallet_ids,)
25+
q = ",".join([f"'{w}'" for w in wallet_ids])
26+
return await db.fetchall(
27+
f"SELECT * FROM eightball.maintable WHERE wallet IN ({q}) ORDER BY id",
28+
model=EightBall,
3329
)
34-
return [EightBall(**row) for row in rows]
3530

3631

3732
async def update_eightball(eightball: EightBall) -> EightBall:
38-
await db.execute(
39-
update_query("eightball.maintable", eightball),
40-
(
41-
*eightball.dict().values(),
42-
eightball.id,
43-
),
44-
)
33+
await db.update("eightball.maintable", eightball)
4534
return eightball
4635

4736

4837
async def delete_eightball(eightball_id: str) -> None:
49-
await db.execute("DELETE FROM eightball.maintable WHERE id = ?", (eightball_id,))
38+
await db.execute(
39+
"DELETE FROM eightball.maintable WHERE id = :id", {"id": eightball_id}
40+
)

models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Optional
22

3+
from fastapi import Request
4+
from lnurl import encode as lnurl_encode
35
from pydantic import BaseModel
46

57

@@ -16,4 +18,11 @@ class EightBall(BaseModel):
1618
name: str
1719
wordlist: str
1820
lnurlpayamount: int
19-
lnurlpay: Optional[str]
21+
22+
def lnurlpay(self, req: Request) -> str:
23+
url = req.url_for("eightball.api_lnurl_pay", eightball_id=self.id)
24+
url_str = str(url)
25+
if url.netloc.endswith(".onion"):
26+
url_str = url_str.replace("https://", "http://")
27+
28+
return lnurl_encode(url_str)

templates/eightball/index.html

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ <h5 class="text-subtitle1 q-my-none">Magic 8 Ball</h5>
2626
<q-table
2727
dense
2828
flat
29-
:data="eightb"
29+
:rows="eightb"
3030
row-key="id"
3131
:columns="myexTable.columns"
32-
:pagination.sync="myexTable.pagination"
32+
v-model:pagination="myexTable.pagination"
3333
>
3434
<template v-slot:header="props">
3535
<q-tr :props="props">
@@ -361,35 +361,6 @@ <h6 class="text-subtitle1 q-my-none">
361361
})
362362
.catch(LNbits.utils.notifyApiError)
363363
},
364-
makeItRain() {
365-
document.getElementById('vue').disabled = true
366-
const end = Date.now() + 2 * 1000
367-
const colors = ['#FFD700', '#ffffff']
368-
const frame = () => {
369-
confetti({
370-
particleCount: 2,
371-
angle: 60,
372-
spread: 55,
373-
origin: {x: 0},
374-
colors,
375-
zIndex: 999999
376-
})
377-
confetti({
378-
particleCount: 2,
379-
angle: 120,
380-
spread: 55,
381-
origin: {x: 1},
382-
colors,
383-
zIndex: 999999
384-
})
385-
if (Date.now() < end) {
386-
requestAnimationFrame(frame)
387-
} else {
388-
document.getElementById('vue').disabled = false
389-
}
390-
}
391-
frame()
392-
},
393364
connectWebocket(wallet_id) {
394365
let localUrl
395366
if (location.protocol !== 'http:') {

views_api.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from http import HTTPStatus
22

3-
from fastapi import APIRouter, Depends, HTTPException, Query, Request
3+
from fastapi import APIRouter, Depends, HTTPException, Query
44
from lnbits.core.crud import get_user
55
from lnbits.core.models import WalletTypeInfo
66
from lnbits.decorators import (
77
require_admin_key,
88
require_invoice_key,
99
)
1010
from lnbits.helpers import urlsafe_short_hash
11-
from lnurl import encode as lnurl_encode
1211

1312
from .crud import (
1413
create_eightball,
@@ -56,7 +55,7 @@ async def api_eightball_update(
5655
):
5756
if not eightball_id:
5857
raise HTTPException(
59-
status_code=HTTPStatus.NOT_FOUND, detail="EightBall does not exist."
58+
status_code=HTTPStatus.NOT_FOUND, detail="EightBall has no ID."
6059
)
6160
eightball = await get_eightball(eightball_id)
6261
assert eightball, "EightBall couldn't be retrieved"
@@ -76,20 +75,13 @@ async def api_eightball_update(
7675
status_code=HTTPStatus.CREATED,
7776
)
7877
async def api_eightball_create(
79-
request: Request,
8078
data: CreateEightBallData,
8179
wallet: WalletTypeInfo = Depends(require_admin_key),
8280
) -> EightBall:
8381
if not data.wallet:
8482
data.wallet = wallet.wallet.id
85-
86-
ball_id = urlsafe_short_hash()
87-
lnurl = lnurl_encode(
88-
str(request.url_for("eightball.api_lnurl_pay", eightball_id=ball_id))
89-
)
9083
eightball = EightBall(
91-
id=ball_id,
92-
lnurlpay=lnurl,
84+
id=urlsafe_short_hash(),
9385
**data.dict(),
9486
)
9587
return await create_eightball(eightball)

0 commit comments

Comments
 (0)