From d083b29d79cf2dea8702e0cf164576f65849b5b8 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Fri, 16 Apr 2021 12:20:05 +0100 Subject: [PATCH] black --- lnbits/extensions/copilot/__init__.py | 2 +- lnbits/extensions/copilot/crud.py | 2 +- lnbits/extensions/copilot/lnurl.py | 20 +++++------ lnbits/extensions/copilot/models.py | 3 +- lnbits/extensions/copilot/views.py | 27 ++++++++++----- lnbits/extensions/copilot/views_api.py | 46 +++++++++++++++----------- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/lnbits/extensions/copilot/__init__.py b/lnbits/extensions/copilot/__init__.py index cefe5064..8654cb36 100644 --- a/lnbits/extensions/copilot/__init__.py +++ b/lnbits/extensions/copilot/__init__.py @@ -9,4 +9,4 @@ copilot_ext: Blueprint = Blueprint( from .views_api import * # noqa from .views import * # noqa -from .lnurl import * # noqa \ No newline at end of file +from .lnurl import * # noqa diff --git a/lnbits/extensions/copilot/crud.py b/lnbits/extensions/copilot/crud.py index 432feb5e..a1179dd8 100644 --- a/lnbits/extensions/copilot/crud.py +++ b/lnbits/extensions/copilot/crud.py @@ -80,7 +80,7 @@ async def create_copilot( show_ack, show_price, lnurl_title, - 0 + 0, ), ) return await get_copilot(copilot_id) diff --git a/lnbits/extensions/copilot/lnurl.py b/lnbits/extensions/copilot/lnurl.py index 7edf35a9..b9f22428 100644 --- a/lnbits/extensions/copilot/lnurl.py +++ b/lnbits/extensions/copilot/lnurl.py @@ -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() @@ -47,7 +45,7 @@ async def lnurl_callback(cp_id): ).dict() ), ) - elif amount_received/1000 > 50000000: + elif amount_received / 1000 > 50000000: return ( jsonify( LnurlErrorResponse( @@ -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())) - - return jsonify(resp.dict()) \ No newline at end of file + print(resp) + + return jsonify(resp.dict()) diff --git a/lnbits/extensions/copilot/models.py b/lnbits/extensions/copilot/models.py index cf33f7a2..aab7c479 100644 --- a/lnbits/extensions/copilot/models.py +++ b/lnbits/extensions/copilot/models.py @@ -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 @@ -38,4 +39,4 @@ class Copilots(NamedTuple): @property def lnurl(self) -> Lnurl: url = url_for("copilot.lnurl_response", cp_id=self.id, _external=True) - return lnurl_encode(url) \ No newline at end of file + return lnurl_encode(url) diff --git a/lnbits/extensions/copilot/views.py b/lnbits/extensions/copilot/views.py index 31246aa9..d25416ff 100644 --- a/lnbits/extensions/copilot/views.py +++ b/lnbits/extensions/copilot/views.py @@ -8,26 +8,29 @@ from .crud import get_copilot from quart import g, abort, render_template, jsonify, websocket from functools import wraps -import trio +import trio import shortuuid from . import copilot_ext connected_websockets = {} -@copilot_ext.websocket('/ws/panel/') + +@copilot_ext.websocket("/ws/panel/") 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_ext.websocket("/ws/compose/") 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,12 +44,20 @@ 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("/") async def panel(copilot_id): copilot = await get_copilot(copilot_id) or abort( HTTPStatus.NOT_FOUND, "Copilot link does not exist." ) - return await render_template("copilot/panel.html", copilot=copilot) \ No newline at end of file + return await render_template("copilot/panel.html", copilot=copilot) diff --git a/lnbits/extensions/copilot/views_api.py b/lnbits/extensions/copilot/views_api.py index e2a60a4d..eb502599 100644 --- a/lnbits/extensions/copilot/views_api.py +++ b/lnbits/extensions/copilot/views_api.py @@ -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/", 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/", 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: + socket_sendererer = app.socket_sendererer() + 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 \ No newline at end of file + + return "", HTTPStatus.OK