Skip to content

Commit

Permalink
v1update
Browse files Browse the repository at this point in the history
  • Loading branch information
arcbtc committed Nov 13, 2024
1 parent edf7914 commit 3c2af76
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 76 deletions.
11 changes: 2 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio

from fastapi import APIRouter
from lnbits.tasks import create_permanent_unique_task
from loguru import logger

from .crud import db
Expand Down Expand Up @@ -33,16 +34,8 @@ def eightball_stop():


def eightball_start():
from lnbits.tasks import create_permanent_unique_task

task = create_permanent_unique_task("ext_eightball", wait_for_paid_invoices)
scheduled_tasks.append(task)


__all__ = [
"db",
"eightball_ext",
"eightball_static_files",
"eightball_start",
"eightball_stop",
]
__all__ = ["db", "eightball_ext", "eightball_static_files"]
39 changes: 15 additions & 24 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
from typing import Optional, Union
from typing import List, Optional, Union

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

from .models import EightBall

db = Database("ext_eightball")


async def create_eightball(data: EightBall) -> EightBall:
await db.execute(
insert_query("eightball.maintable", data),
(*data.dict().values(),),
)
return data
return await db.insert("eightball.maintable", data)


async def get_eightball(eightball_id: str) -> Optional[EightBall]:
row = await db.fetchone(
"SELECT * FROM eightball.maintable WHERE id = ?", (eightball_id,)
return await db.fetchone(
"SELECT * FROM eightball.maintable WHERE id = :id",
{"id": eightball_id},
EightBall,
)
return EightBall(**row) if row else None


async def get_eightballs(wallet_ids: Union[str, list[str]]) -> list[EightBall]:
async def get_eightballs(wallet_ids: Union[str, List[str]]) -> List[EightBall]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM eightball.maintable WHERE wallet IN ({q})", (*wallet_ids,)
q = ",".join([f"'{w}'" for w in wallet_ids])
return await db.fetchall(
f"SELECT * FROM eightball.maintable WHERE wallet IN ({q}) ORDER BY id",
model=EightBall,
)
return [EightBall(**row) for row in rows]


async def update_eightball(eightball: EightBall) -> EightBall:
await db.execute(
update_query("eightball.maintable", eightball),
(
*eightball.dict().values(),
eightball.id,
),
)
await db.update("eightball.maintable", eightball)
return eightball


async def delete_eightball(eightball_id: str) -> None:
await db.execute("DELETE FROM eightball.maintable WHERE id = ?", (eightball_id,))
await db.execute(
"DELETE FROM eightball.maintable WHERE id = :id", {"id": eightball_id}
)
11 changes: 10 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

from fastapi import Request
from lnurl import encode as lnurl_encode
from pydantic import BaseModel


Expand All @@ -16,4 +18,11 @@ class EightBall(BaseModel):
name: str
wordlist: str
lnurlpayamount: int
lnurlpay: Optional[str]

def lnurlpay(self, req: Request) -> str:
url = req.url_for("eightball.api_lnurl_pay", eightball_id=self.id)
url_str = str(url)
if url.netloc.endswith(".onion"):
url_str = url_str.replace("https://", "http://")

return lnurl_encode(url_str)
33 changes: 2 additions & 31 deletions templates/eightball/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ <h5 class="text-subtitle1 q-my-none">Magic 8 Ball</h5>
<q-table
dense
flat
:data="eightb"
:rows="eightb"
row-key="id"
:columns="myexTable.columns"
:pagination.sync="myexTable.pagination"
v-model:pagination="myexTable.pagination"
>
<template v-slot:header="props">
<q-tr :props="props">
Expand Down Expand Up @@ -361,35 +361,6 @@ <h6 class="text-subtitle1 q-my-none">
})
.catch(LNbits.utils.notifyApiError)
},
makeItRain() {
document.getElementById('vue').disabled = true
const end = Date.now() + 2 * 1000
const colors = ['#FFD700', '#ffffff']
const frame = () => {
confetti({
particleCount: 2,
angle: 60,
spread: 55,
origin: {x: 0},
colors,
zIndex: 999999
})
confetti({
particleCount: 2,
angle: 120,
spread: 55,
origin: {x: 1},
colors,
zIndex: 999999
})
if (Date.now() < end) {
requestAnimationFrame(frame)
} else {
document.getElementById('vue').disabled = false
}
}
frame()
},
connectWebocket(wallet_id) {
let localUrl
if (location.protocol !== 'http:') {
Expand Down
14 changes: 3 additions & 11 deletions views_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from http import HTTPStatus

from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi import APIRouter, Depends, HTTPException, Query
from lnbits.core.crud import get_user
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import (
require_admin_key,
require_invoice_key,
)
from lnbits.helpers import urlsafe_short_hash
from lnurl import encode as lnurl_encode

from .crud import (
create_eightball,
Expand Down Expand Up @@ -56,7 +55,7 @@ async def api_eightball_update(
):
if not eightball_id:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="EightBall does not exist."
status_code=HTTPStatus.NOT_FOUND, detail="EightBall has no ID."
)
eightball = await get_eightball(eightball_id)
assert eightball, "EightBall couldn't be retrieved"
Expand All @@ -76,20 +75,13 @@ async def api_eightball_update(
status_code=HTTPStatus.CREATED,
)
async def api_eightball_create(
request: Request,
data: CreateEightBallData,
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> EightBall:
if not data.wallet:
data.wallet = wallet.wallet.id

ball_id = urlsafe_short_hash()
lnurl = lnurl_encode(
str(request.url_for("eightball.api_lnurl_pay", eightball_id=ball_id))
)
eightball = EightBall(
id=ball_id,
lnurlpay=lnurl,
id=urlsafe_short_hash(),
**data.dict(),
)
return await create_eightball(eightball)
Expand Down

0 comments on commit 3c2af76

Please sign in to comment.