Skip to content

Commit acce8f4

Browse files
authored
Revert "feat: use lnurl lib for views_lnurl (#24)" (#25)
This reverts commit b034078.
1 parent b034078 commit acce8f4

File tree

5 files changed

+81
-68
lines changed

5 files changed

+81
-68
lines changed

config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"name": "Streamer Copilot",
33
"short_description": "Video tips/animations/webhooks",
44
"tile": "/copilot/static/bitcoin-streaming.png",
5-
"version": "1.1.0",
6-
"min_lnbits_version": "1.3.0",
5+
"min_lnbits_version": "1.0.0",
76
"contributors": [
87
{
98
"name": "Ben Arc",

models.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from fastapi import Query
1+
from fastapi import Query, Request
2+
from lnurl import encode as lnurl_encode
23
from pydantic import BaseModel
34

45

@@ -31,7 +32,7 @@ class Copilot(BaseModel):
3132
user: str | None
3233
title: str
3334
lnurl_toggle: int
34-
wallet: str
35+
wallet: str | None
3536
animation1: str | None
3637
animation2: str | None
3738
animation3: str | None
@@ -49,3 +50,7 @@ class Copilot(BaseModel):
4950
timestamp: int
5051
fullscreen_cam: int
5152
iframe_url: str | None
53+
54+
def lnurl(self, req: Request) -> str:
55+
url = str(req.url_for("copilot.lnurl_response", cp_id=self.id))
56+
return lnurl_encode(url)

templates/copilot/compose.html

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
<q-card-section>
2323
<div class="row">
2424
<div class="col" style="max-width: 100px">
25-
<lnbits-qrcode :value="chatUrl"></lnbits-qrcode>
25+
<lnbits-qrcode
26+
:value="chatUrl"
27+
class="rounded-borders"
28+
></lnbits-qrcode>
2629
</div>
2730
<div class="col">
2831
<div class="text-h6 q-ml-md">Trollbox</div>
@@ -71,15 +74,17 @@
7174
"
7275
>
7376
<div class="col">
74-
<lnbits-qrcode-lnurl
75-
:url="url"
76-
:show-buttons="false"
77-
></lnbits-qrcode-lnurl>
78-
<center
79-
class="absolute-bottom"
80-
style="color: black; font-size: 20px"
81-
v-text="copilot.lnurl_title"
82-
></center>
77+
<a class="text-secondary" :href="'lightning:' + copilot.lnurl">
78+
<lnbits-qrcode
79+
:value="'lightning:' + copilot.lnurl"
80+
class="rounded-borders"
81+
></lnbits-qrcode>
82+
<center
83+
class="absolute-bottom"
84+
style="color: black; font-size: 20px"
85+
v-text="copilot.lnurl_title"
86+
></center>
87+
</a>
8388
</div>
8489
</div>
8590

@@ -160,7 +165,7 @@
160165
copilot: {},
161166
animQueue: [],
162167
queue: false,
163-
url: '',
168+
lnurl: '',
164169
troll_box: false,
165170
trollbox: [],
166171
chatUrl: '',
@@ -313,8 +318,6 @@
313318
)
314319
.then(response => {
315320
this.copilot = response.data
316-
this.url =
317-
window.location.origin + '/copilot/lnurl/' + this.copilot.id
318321
})
319322
.catch(err => {
320323
LNbits.utils.notifyApiError(err)

views_api.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from http import HTTPStatus
22

3-
from fastapi import APIRouter, Depends
3+
from fastapi import APIRouter, Depends, Request
44
from fastapi.exceptions import HTTPException
55
from lnbits.core.models import WalletTypeInfo
66
from lnbits.core.services import websocket_updater
@@ -28,14 +28,18 @@ async def api_copilots_retrieve(wallet: WalletTypeInfo = Depends(require_invoice
2828
@copilot_api_router.get(
2929
"/api/v1/copilot/{copilot_id}", dependencies=[Depends(require_invoice_key)]
3030
)
31-
async def api_copilot_retrieve(copilot_id: str) -> Copilot:
31+
async def api_copilot_retrieve(
32+
req: Request,
33+
copilot_id: str,
34+
):
3235
copilot = await get_copilot(copilot_id)
3336
if not copilot:
3437
raise HTTPException(
35-
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found."
38+
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
3639
)
37-
38-
return copilot
40+
if not copilot.lnurl_toggle:
41+
return copilot
42+
return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}}
3943

4044

4145
@copilot_api_router.post("/api/v1/copilot")

views_lnurl.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,80 @@
11
import json
2+
from http import HTTPStatus
23

34
from fastapi import APIRouter, Query, Request
5+
from fastapi.exceptions import HTTPException
6+
from fastapi.responses import HTMLResponse
47
from lnbits.core.services import create_invoice
5-
from lnurl import (
6-
CallbackUrl,
7-
LightningInvoice,
8-
LnurlErrorResponse,
9-
LnurlPayActionResponse,
10-
LnurlPayMetadata,
11-
LnurlPayResponse,
12-
MilliSatoshi,
13-
)
14-
from pydantic import parse_obj_as
8+
from lnurl.types import LnurlPayMetadata
159

1610
from .crud import get_copilot
1711

1812
copilot_lnurl_router = APIRouter()
1913

2014

21-
@copilot_lnurl_router.get("/lnurl/{cp_id}", name="copilot.lnurl_response")
22-
async def lnurl_response(
23-
req: Request, cp_id: str
24-
) -> LnurlPayResponse | LnurlErrorResponse:
15+
@copilot_lnurl_router.get(
16+
"/lnurl/{cp_id}", response_class=HTMLResponse, name="copilot.lnurl_response"
17+
)
18+
async def lnurl_response(req: Request, cp_id: str):
2519
cp = await get_copilot(cp_id)
2620
if not cp:
27-
return LnurlErrorResponse(reason="Copilot not found.")
28-
29-
callback_url = parse_obj_as(
30-
CallbackUrl, str(req.url_for("copilot.lnurl_callback", cp_id=cp_id))
31-
)
21+
raise HTTPException(
22+
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
23+
)
3224

33-
pay_response = LnurlPayResponse(
34-
callback=callback_url,
35-
metadata=LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]])),
36-
minSendable=MilliSatoshi(10000),
37-
maxSendable=MilliSatoshi(50000000),
38-
)
25+
pay_response = {
26+
"tag": "payRequest",
27+
"callback": str(req.url_for("copilot.lnurl_callback", cp_id=cp_id)),
28+
"metadata": LnurlPayMetadata(json.dumps([["text/plain", str(cp.lnurl_title)]])),
29+
"maxSendable": 50000000,
30+
"minSendable": 10000,
31+
}
3932

4033
if cp.show_message:
41-
pay_response.commentAllowed = 300
42-
43-
return pay_response
34+
pay_response["commentAllowed"] = 300
35+
return json.dumps(pay_response)
4436

4537

4638
@copilot_lnurl_router.get("/lnurl/cb/{cp_id}", name="copilot.lnurl_callback")
4739
async def lnurl_callback(
4840
cp_id: str, amount: str = Query(None), comment: str = Query(None)
49-
) -> LnurlPayActionResponse | LnurlErrorResponse:
41+
):
5042
cp = await get_copilot(cp_id)
5143
if not cp:
52-
return LnurlErrorResponse(reason="Copilot not found.")
53-
44+
raise HTTPException(
45+
status_code=HTTPStatus.NOT_FOUND, detail="Copilot not found"
46+
)
5447
amount_received = int(amount)
55-
amount_rounded = round(amount_received / 1000)
48+
5649
if amount_received < 10000:
57-
return LnurlErrorResponse(
58-
reason=f"Amount {amount_rounded} is smaller than minimum 10 sats."
50+
raise HTTPException(
51+
status_code=HTTPStatus.FORBIDDEN,
52+
detail=(
53+
"Amount {round(amount_received / 1000)} "
54+
"is smaller than minimum 10 sats."
55+
),
5956
)
6057
elif amount_received / 1000 > 10000000:
61-
return LnurlErrorResponse(
62-
reason=f"Amount {amount_rounded} is greater than maximum 10000000 sats."
58+
raise HTTPException(
59+
status_code=HTTPStatus.FORBIDDEN,
60+
detail=(
61+
"Amount {round(amount_received / 1000)} "
62+
"is greater than maximum 50000."
63+
),
6364
)
64-
65+
comment = ""
6566
if comment:
66-
if len(comment) > 300:
67-
return LnurlErrorResponse(
68-
reason=(
69-
f"Got a comment with {len(comment)} characters, "
67+
if len(comment or "") > 300:
68+
raise HTTPException(
69+
status_code=HTTPStatus.FORBIDDEN,
70+
detail=(
71+
"Got a comment with {len(comment)} characters, "
7072
"but can only accept 300"
71-
)
73+
),
7274
)
73-
75+
if len(comment) < 1:
76+
comment = "none"
77+
assert cp.wallet, "Copilot wallet not found"
7478
payment = await create_invoice(
7579
wallet_id=cp.wallet,
7680
amount=int(amount_received / 1000),
@@ -80,6 +84,4 @@ async def lnurl_callback(
8084
).encode(),
8185
extra={"tag": "copilot", "copilotid": cp.id, "comment": comment},
8286
)
83-
84-
invoice = parse_obj_as(LightningInvoice, payment.bolt11)
85-
return LnurlPayActionResponse(pr=invoice)
87+
return {"pr": payment.bolt11, "routes": []}

0 commit comments

Comments
 (0)