This commit is contained in:
Ben Arc 2021-04-16 12:20:05 +01:00
parent f8745cadf0
commit d083b29d79
6 changed files with 58 additions and 42 deletions

View file

@ -80,7 +80,7 @@ async def create_copilot(
show_ack,
show_price,
lnurl_title,
0
0,
),
)
return await get_copilot(copilot_id)

View file

@ -17,12 +17,10 @@ async def lnurl_response(cp_id):
return jsonify({"status": "ERROR", "reason": "Copilot not found."})
resp = LnurlPayResponse(
callback=url_for(
"copilot.lnurl_callback", cp_id=cp_id, _external=True
),
callback=url_for("copilot.lnurl_callback", cp_id=cp_id, _external=True),
min_sendable=10000,
max_sendable=50000000,
metadata=LnurlPayMetadata(json.dumps([["text/plain", cp.lnurl_title]]))
metadata=LnurlPayMetadata(json.dumps([["text/plain", cp.lnurl_title]])),
)
params = resp.dict()
@ -69,18 +67,16 @@ async def lnurl_callback(cp_id):
amount=int(amount_received / 1000),
memo=cp.lnurl_title,
webhook="/copilot/api/v1/copilot/hook/" + cp_id,
description_hash=hashlib.sha256(
(cp.lnurl_title).encode("utf-8")
).digest(),
description_hash=hashlib.sha256((cp.lnurl_title).encode("utf-8")).digest(),
extra={"tag": "copilot", "comment": comment},
)
resp = LnurlPayActionResponse(
pr=payment_request,
success_action=None,
success_action=jsonify({tag: "message", message: "Thank you!"}),
routes=[],
)
print(payment_request)
print(jsonify(resp.dict()))
print(resp)
return jsonify(resp.dict())

View file

@ -6,6 +6,7 @@ from lnurl import Lnurl, encode as lnurl_encode # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore
from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore
class Copilots(NamedTuple):
id: str
user: str

View file

@ -14,20 +14,23 @@ from . import copilot_ext
connected_websockets = {}
@copilot_ext.websocket('/ws/panel/<copilot_id>')
@copilot_ext.websocket("/ws/panel/<copilot_id>")
async def ws_panel(copilot_id):
global connected_websockets
while True:
data = await websocket.receive()
connected_websockets[copilot_id] = shortuuid.uuid() + '-' + data
connected_websockets[copilot_id] = shortuuid.uuid() + "-" + data
@copilot_ext.websocket('/ws/compose/<copilot_id>')
@copilot_ext.websocket("/ws/compose/<copilot_id>")
async def ws_compose(copilot_id):
global connected_websockets
while True:
data = await websocket.receive()
await websocket.send(connected_websockets[copilot_id])
@copilot_ext.route("/")
@validate_uuids(["usr"], required=True)
@check_user_exists()
@ -41,8 +44,16 @@ async def compose(copilot_id):
HTTPStatus.NOT_FOUND, "Copilot link does not exist."
)
if copilot.lnurl_toggle:
return await render_template("copilot/compose.html", copilot=copilot, lnurl=copilot.lnurl, lnurl_toggle=copilot.lnurl_toggle)
return await render_template("copilot/compose.html", copilot=copilot, lnurl_toggle=copilot.lnurl_toggle)
return await render_template(
"copilot/compose.html",
copilot=copilot,
lnurl=copilot.lnurl,
lnurl_toggle=copilot.lnurl_toggle,
)
return await render_template(
"copilot/compose.html", copilot=copilot, lnurl_toggle=copilot.lnurl_toggle
)
@copilot_ext.route("/<copilot_id>")
async def panel(copilot_id):

View file

@ -58,18 +58,14 @@ async def api_copilots_retrieve():
try:
return (
jsonify(
[
{
**copilot._asdict()
}
for copilot in await get_copilots(g.wallet.user)
]
[{**copilot._asdict()} for copilot in await get_copilots(g.wallet.user)]
),
HTTPStatus.OK,
)
except:
return ""
@copilot_ext.route("/api/v1/copilot/<copilot_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_copilot_retrieve(copilot_id):
@ -79,11 +75,7 @@ async def api_copilot_retrieve(copilot_id):
return jsonify({"message": "copilot does not exist"}), HTTPStatus.NOT_FOUND
return (
jsonify(
{
copilot._asdict()
}
),
jsonify({copilot._asdict()}),
HTTPStatus.OK,
)
@ -100,23 +92,39 @@ async def api_copilot_delete(copilot_id):
return "", HTTPStatus.NO_CONTENT
#############################PAYMENTHOOKER##########################
@copilot_ext.route("/api/v1/copilot/hook/<copilot_id>", methods=["POST"])
async def api_copilot_hooker(copilot_id, trigger):
copilot = await get_copilot(copilot_id)
if not copilot:
return jsonify({"message": "Copilot link link does not exist."}), HTTPStatus.NOT_FOUND
return (
jsonify({"message": "Copilot link link does not exist."}),
HTTPStatus.NOT_FOUND,
)
socket_sendererer = app.socket_sendererer()
if copilot.animation1threshold and int(g.data['amount']) > copilot.animation1threshold:
if (
copilot.animation1threshold
and int(g.data["amount"]) > copilot.animation1threshold
):
data = copilot.animation1
if copilot.animation2threshold and int(g.data['amount']) > copilot.animation2threshold:
if (
copilot.animation2threshold
and int(g.data["amount"]) > copilot.animation2threshold
):
data = copilot.animation2
if copilot.animation3threshold and int(g.data['amount']) > copilot.animation3threshold:
if (
copilot.animation3threshold
and int(g.data["amount"]) > copilot.animation3threshold
):
data = copilot.animation3
async with socket_sendererer.websocket('/ws/compose/' + copilot_id) as the_websocket:
async with socket_sendererer.websocket(
"/ws/compose/" + copilot_id
) as the_websocket:
await the_websocket.send(data)
return "", HTTPStatus.OK