2021-04-12 20:31:39 +01:00
|
|
|
import hashlib
|
2021-04-15 09:39:27 +01:00
|
|
|
from quart import g, jsonify, url_for, websocket
|
2021-04-12 20:31:39 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
import httpx
|
|
|
|
|
|
|
|
from lnbits.core.crud import get_user
|
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2021-04-20 10:34:48 +01:00
|
|
|
from .views import updater
|
2021-04-12 20:31:39 +01:00
|
|
|
|
2021-04-14 23:45:28 +01:00
|
|
|
from . import copilot_ext
|
|
|
|
|
2021-04-12 20:31:39 +01:00
|
|
|
from lnbits.extensions.copilot import copilot_ext
|
|
|
|
from .crud import (
|
|
|
|
create_copilot,
|
|
|
|
update_copilot,
|
|
|
|
get_copilot,
|
|
|
|
get_copilots,
|
|
|
|
delete_copilot,
|
|
|
|
)
|
|
|
|
|
2021-04-20 08:50:53 +01:00
|
|
|
#######################COPILOT##########################
|
2021-04-12 20:31:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.route("/api/v1/copilot", methods=["POST"])
|
|
|
|
@copilot_ext.route("/api/v1/copilot/<copilot_id>", methods=["PUT"])
|
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"title": {"type": "string", "empty": False, "required": True},
|
2021-04-15 09:39:27 +01:00
|
|
|
"lnurl_toggle": {"type": "integer", "empty": False, "required": True},
|
|
|
|
"wallet": {"type": "string", "empty": False, "required": False},
|
2021-04-13 19:20:12 +01:00
|
|
|
"animation1": {"type": "string", "required": False},
|
|
|
|
"animation2": {"type": "string", "required": False},
|
|
|
|
"animation3": {"type": "string", "required": False},
|
2021-04-15 09:46:37 +01:00
|
|
|
"animation1threshold": {"type": "string", "required": False},
|
|
|
|
"animation2threshold": {"type": "string", "required": False},
|
|
|
|
"animation3threshold": {"type": "string", "required": False},
|
2021-04-13 19:20:12 +01:00
|
|
|
"animation1webhook": {"type": "string", "required": False},
|
|
|
|
"animation2webhook": {"type": "string", "required": False},
|
|
|
|
"animation3webhook": {"type": "string", "required": False},
|
2021-04-15 09:39:27 +01:00
|
|
|
"lnurl_title": {"type": "string", "empty": False, "required": False},
|
|
|
|
"show_message": {"type": "integer", "empty": False, "required": False},
|
2021-04-13 17:10:25 +01:00
|
|
|
"show_ack": {"type": "integer", "empty": False, "required": True},
|
2021-04-16 11:07:14 +01:00
|
|
|
"show_price": {"type": "integer", "empty": False, "required": True},
|
2021-04-12 20:31:39 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
async def api_copilot_create_or_update(copilot_id=None):
|
|
|
|
if not copilot_id:
|
|
|
|
copilot = await create_copilot(user=g.wallet.user, **g.data)
|
|
|
|
return jsonify(copilot._asdict()), HTTPStatus.CREATED
|
|
|
|
else:
|
|
|
|
copilot = await update_copilot(copilot_id=copilot_id, **g.data)
|
|
|
|
return jsonify(copilot._asdict()), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.route("/api/v1/copilot", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
2021-04-13 19:20:12 +01:00
|
|
|
async def api_copilots_retrieve():
|
|
|
|
try:
|
|
|
|
return (
|
|
|
|
jsonify(
|
2021-04-16 12:20:05 +01:00
|
|
|
[{**copilot._asdict()} for copilot in await get_copilots(g.wallet.user)]
|
2021-04-13 19:20:12 +01:00
|
|
|
),
|
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
return ""
|
2021-04-12 20:31:39 +01:00
|
|
|
|
2021-04-16 12:20:05 +01:00
|
|
|
|
2021-04-12 20:31:39 +01:00
|
|
|
@copilot_ext.route("/api/v1/copilot/<copilot_id>", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_copilot_retrieve(copilot_id):
|
|
|
|
copilot = await get_copilot(copilot_id)
|
|
|
|
|
|
|
|
if not copilot:
|
|
|
|
return jsonify({"message": "copilot does not exist"}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
return (
|
2021-04-20 15:13:29 +01:00
|
|
|
jsonify({**copilot._asdict(), **{"lnurl": copilot.lnurl}}),
|
2021-04-12 20:31:39 +01:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.route("/api/v1/copilot/<copilot_id>", methods=["DELETE"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_copilot_delete(copilot_id):
|
|
|
|
copilot = await get_copilot(copilot_id)
|
|
|
|
|
|
|
|
if not copilot:
|
|
|
|
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
await delete_copilot(copilot_id)
|
|
|
|
|
2021-04-16 22:22:36 +01:00
|
|
|
return "", HTTPStatus.NO_CONTENT
|
2021-04-20 10:34:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
@copilot_ext.route("/api/v1/copilot/ws/<copilot_id>/<comment>/<data>", methods=["GET"])
|
2021-04-20 10:45:54 +01:00
|
|
|
async def api_copilot_ws_relay(copilot_id, comment, data):
|
2021-04-20 10:34:48 +01:00
|
|
|
copilot = await get_copilot(copilot_id)
|
|
|
|
|
|
|
|
if not copilot:
|
|
|
|
return jsonify({"message": "copilot does not exist"}), HTTPStatus.NOT_FOUND
|
|
|
|
await updater(data, comment, copilot_id)
|
2021-04-20 12:31:23 +01:00
|
|
|
print(comment)
|
2021-04-20 10:34:48 +01:00
|
|
|
return "", HTTPStatus.OK
|