diff --git a/lnbits/extensions/twitchalerts/crud.py b/lnbits/extensions/twitchalerts/crud.py index d4458f953..15b05063d 100644 --- a/lnbits/extensions/twitchalerts/crud.py +++ b/lnbits/extensions/twitchalerts/crud.py @@ -5,6 +5,9 @@ from ..satspay.crud import delete_charge import httpx +from http import HTTPStatus +from quart import jsonify + from typing import Optional from lnbits.helpers import urlsafe_short_hash @@ -28,11 +31,12 @@ async def get_charge_details(service_id): async def create_donation( id: str, - name: str, cur_code: str, sats: int, amount: float, service: int, + name: str = "Anonymous", + message: str = "", posted: bool = False, ) -> Donation: await db.execute( @@ -40,17 +44,19 @@ async def create_donation( INSERT INTO Donations ( id, name, + message, cur_code, sats, amount, service, posted ) - VALUES (?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) """, ( id, name, + message, cur_code, sats, amount, @@ -61,19 +67,51 @@ async def create_donation( return await get_donation(id) -async def post_donation(donation_id: str) -> bool: +async def post_donation(donation_id: str) -> tuple: donation = await get_donation(donation_id) + if not donation: + return ( + jsonify({"message": "Donation not found!"}), + HTTPStatus.BAD_REQUEST + ) if donation.posted: - return False + return ( + jsonify({"message": "Donation has already been posted!"}), + HTTPStatus.BAD_REQUEST + ) service = await get_service(donation.service) - servicename = service.servicename - if servicename == "Streamlabs": - pass + if service.servicename == "Streamlabs": + url = "https://streamlabs.com/api/v1.0/donations" + data = { + "name": donation.name, + "message": donation.message, + "identifier": "LNbits", + "amount": donation.amount, + "currency": donation.cur_code.upper(), + "access_token": service.token, + } + async with httpx.AsyncClient() as client: + response = await client.post(url, data=data) + print(response.json()) + status = [s for s in list(HTTPStatus) if s == response.status_code][0] + elif service.servicename == "StreamElements": + return ( + jsonify({"message": "StreamElements not yet supported!"}), + HTTPStatus.BAD_REQUEST + ) + else: + return ( + jsonify({"message": "Unsopported servicename"}), + HTTPStatus.BAD_REQUEST + ) await db.execute( "UPDATE Donations SET posted = 1 WHERE id = ?", (donation_id,) ) - return True + return ( + jsonify(response.json()), + status + ) async def create_service( diff --git a/lnbits/extensions/twitchalerts/migrations.py b/lnbits/extensions/twitchalerts/migrations.py index 6396ded55..6677d5d20 100644 --- a/lnbits/extensions/twitchalerts/migrations.py +++ b/lnbits/extensions/twitchalerts/migrations.py @@ -22,6 +22,7 @@ async def m001_initial(db): CREATE TABLE IF NOT EXISTS Donations ( id TEXT PRIMARY KEY, name TEXT NOT NULL, + message TEXT NOT NULL, cur_code TEXT NOT NULL, sats INT NOT NULL, amount FLOAT NOT NULL, diff --git a/lnbits/extensions/twitchalerts/models.py b/lnbits/extensions/twitchalerts/models.py index 349ba3beb..32f795ff9 100644 --- a/lnbits/extensions/twitchalerts/models.py +++ b/lnbits/extensions/twitchalerts/models.py @@ -5,6 +5,7 @@ from typing import NamedTuple, Optional class Donation(NamedTuple): id: str name: str + message: str cur_code: str sats: int amount: float diff --git a/lnbits/extensions/twitchalerts/views_api.py b/lnbits/extensions/twitchalerts/views_api.py index b529cab10..453a1b2ff 100644 --- a/lnbits/extensions/twitchalerts/views_api.py +++ b/lnbits/extensions/twitchalerts/views_api.py @@ -133,19 +133,8 @@ async def api_post_donation(): data = await request.get_json(force=True) donation_id = data.get("id", "No ID") charge = await get_charge(donation_id) - print(charge) if charge and charge.paid: - print("This endpoint works!") - if await post_donation(donation_id): - return ( - jsonify({"message": "Posted!"}), - HTTPStatus.OK - ) - else: - return ( - jsonify({"message": "Already posted!"}), - HTTPStatus.BAD_REQUEST - ) + return await post_donation(donation_id) else: return ( jsonify({"message": "Not a paid charge!"}),