From 6278e5357cf2f53d8ffa3b1c0ca42c48c6fffd6c Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Thu, 28 Oct 2021 17:02:07 +0100 Subject: [PATCH] livestreams converted --- lnbits/extensions/livestream/README.md | 45 +++ lnbits/extensions/livestream/__init__.py | 36 ++ lnbits/extensions/livestream/config.json | 10 + lnbits/extensions/livestream/crud.py | 200 +++++++++++ lnbits/extensions/livestream/lnurl.py | 120 +++++++ lnbits/extensions/livestream/migrations.py | 39 +++ lnbits/extensions/livestream/models.py | 89 +++++ .../extensions/livestream/static/js/index.js | 216 ++++++++++++ lnbits/extensions/livestream/tasks.py | 93 +++++ .../templates/livestream/_api_docs.html | 146 ++++++++ .../templates/livestream/index.html | 322 ++++++++++++++++++ lnbits/extensions/livestream/views.py | 41 +++ lnbits/extensions/livestream/views_api.py | 107 ++++++ 13 files changed, 1464 insertions(+) create mode 100644 lnbits/extensions/livestream/README.md create mode 100644 lnbits/extensions/livestream/__init__.py create mode 100644 lnbits/extensions/livestream/config.json create mode 100644 lnbits/extensions/livestream/crud.py create mode 100644 lnbits/extensions/livestream/lnurl.py create mode 100644 lnbits/extensions/livestream/migrations.py create mode 100644 lnbits/extensions/livestream/models.py create mode 100644 lnbits/extensions/livestream/static/js/index.js create mode 100644 lnbits/extensions/livestream/tasks.py create mode 100644 lnbits/extensions/livestream/templates/livestream/_api_docs.html create mode 100644 lnbits/extensions/livestream/templates/livestream/index.html create mode 100644 lnbits/extensions/livestream/views.py create mode 100644 lnbits/extensions/livestream/views_api.py diff --git a/lnbits/extensions/livestream/README.md b/lnbits/extensions/livestream/README.md new file mode 100644 index 000000000..4e88e7bc7 --- /dev/null +++ b/lnbits/extensions/livestream/README.md @@ -0,0 +1,45 @@ +# DJ Livestream + +## Help DJ's and music producers conduct music livestreams + +LNBits Livestream extension produces a static QR code that can be shown on screen while livestreaming a DJ set for example. If someone listening to the livestream likes a song and want to support the DJ and/or the producer he can scan the QR code with a LNURL-pay capable wallet. + +When scanned, the QR code sends up information about the song playing at the moment (name and the producer of that song). Also, if the user likes the song and would like to support the producer, he can send a tip and a message for that specific track. If the user sends an amount over a specific threshold they will be given a link to download it (optional). + +The revenue will be sent to a wallet created specifically for that producer, with optional revenue splitting between the DJ and the producer. + +[**Wallets supporting LNURL**](https://github.com/fiatjaf/awesome-lnurl#wallets) + +[![video tutorial livestream](http://img.youtube.com/vi/zDrSWShKz7k/0.jpg)](https://youtu.be/zDrSWShKz7k 'video tutorial offline shop') + +## Usage + +1. Start by adding a track\ + ![add new track](https://i.imgur.com/Cu0eGrW.jpg) + - set the producer, or choose an existing one + - set the track name + - define a minimum price where a user can download the track + - set the download URL, where user will be redirected if he tips the livestream and the tip is equal or above the set price\ + ![track settings](https://i.imgur.com/HTJYwcW.jpg) +2. Adjust the percentage of the pay you want to take from the user's tips. 10%, the default, means that the DJ will keep 10% of all the tips sent by users. The other 90% will go to an auto generated producer wallet\ + ![adjust percentage](https://i.imgur.com/9weHKAB.jpg) +3. For every different producer added, when adding tracks, a wallet is generated for them\ + ![producer wallet](https://i.imgur.com/YFIZ7Tm.jpg) +4. On the bottom of the LNBits DJ Livestream extension you'll find the static QR code ([LNURL-pay](https://github.com/lnbits/lnbits/blob/master/lnbits/extensions/lnurlp/README.md)) you can add to the livestream or if you're a street performer you can print it and have it displayed +5. After all tracks and producers are added, you can start "playing" songs\ + ![play tracks](https://i.imgur.com/7ytiBkq.jpg) +6. You'll see the current track playing and a green icon indicating active track also\ + ![active track](https://i.imgur.com/W1vBz54.jpg) +7. When a user scans the QR code, and sends a tip, you'll receive 10%, in the example case, in your wallet and the producer's wallet will get the rest. For example someone tips 100 sats, you'll get 10 sats and the producer will get 90 sats + - producer's wallet receiving 18 sats from 20 sats tips\ + ![producer wallet](https://i.imgur.com/OM9LawA.jpg) + +## Use cases + +You can print the QR code and display it on a live gig, a street performance, etc... OR you can use the QR as an overlay in an online stream of you playing music, doing a DJ set, making a podcast. + +You can use the extension's API to trigger updates for the current track, update fees, add tracks... + +## Sponsored by + +[![](https://cdn.shopify.com/s/files/1/0826/9235/files/cryptograffiti_logo_clear_background.png?v=1504730421)](https://cryptograffiti.com/) diff --git a/lnbits/extensions/livestream/__init__.py b/lnbits/extensions/livestream/__init__.py new file mode 100644 index 000000000..6b675b9d8 --- /dev/null +++ b/lnbits/extensions/livestream/__init__.py @@ -0,0 +1,36 @@ +import asyncio + +from fastapi import APIRouter +from fastapi.staticfiles import StaticFiles + +from lnbits.db import Database +from lnbits.helpers import template_renderer +from lnbits.tasks import catch_everything_and_restart + +db = Database("ext_livestream") + +livestream_static_files = [ + { + "path": "/livestream/static", + "app": StaticFiles(directory="lnbits/extensions/livestream/static"), + "name": "livestream_static", + } +] + +livestream_ext: APIRouter = APIRouter( + prefix="/livestream", + tags=["livestream"] +) + +def livestream_renderer(): + return template_renderer(["lnbits/extensions/livestream/templates"]) + +from .lnurl import * # noqa +from .tasks import wait_for_paid_invoices +from .views import * # noqa +from .views_api import * # noqa + + +def lnticket_start(): + loop = asyncio.get_event_loop() + loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) diff --git a/lnbits/extensions/livestream/config.json b/lnbits/extensions/livestream/config.json new file mode 100644 index 000000000..12ba6b797 --- /dev/null +++ b/lnbits/extensions/livestream/config.json @@ -0,0 +1,10 @@ +{ + "name": "DJ Livestream", + "short_description": "Sell tracks and split revenue (lnurl-pay)", + "icon": "speaker", + "contributors": [ + "fiatjaf", + "cryptograffiti" + ], + "hidden": false +} diff --git a/lnbits/extensions/livestream/crud.py b/lnbits/extensions/livestream/crud.py new file mode 100644 index 000000000..1b13bf08f --- /dev/null +++ b/lnbits/extensions/livestream/crud.py @@ -0,0 +1,200 @@ +from typing import List, Optional + +from lnbits.core.crud import create_account, create_wallet +from lnbits.db import SQLITE + +from . import db +from .models import Livestream, Producer, Track + + +async def create_livestream(*, wallet_id: str) -> int: + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await (method)( + f""" + INSERT INTO livestream.livestreams (wallet) + VALUES (?) + {returning} + """, + (wallet_id,), + ) + + if db.type == SQLITE: + return result._result_proxy.lastrowid + else: + return result[0] + + +async def get_livestream(ls_id: int) -> Optional[Livestream]: + row = await db.fetchone( + "SELECT * FROM livestream.livestreams WHERE id = ?", (ls_id,) + ) + return Livestream(**dict(row)) if row else None + + +async def get_livestream_by_track(track_id: int) -> Optional[Livestream]: + row = await db.fetchone( + """ + SELECT livestreams.* FROM livestream.livestreams + INNER JOIN tracks ON tracks.livestream = livestreams.id + WHERE tracks.id = ? + """, + (track_id,), + ) + return Livestream(**dict(row)) if row else None + + +async def get_or_create_livestream_by_wallet(wallet: str) -> Optional[Livestream]: + row = await db.fetchone( + "SELECT * FROM livestream.livestreams WHERE wallet = ?", (wallet,) + ) + + if not row: + # create on the fly + ls_id = await create_livestream(wallet_id=wallet) + return await get_livestream(ls_id) + + return Livestream(**dict(row)) if row else None + + +async def update_current_track(ls_id: int, track_id: Optional[int]): + await db.execute( + "UPDATE livestream.livestreams SET current_track = ? WHERE id = ?", + (track_id, ls_id), + ) + + +async def update_livestream_fee(ls_id: int, fee_pct: int): + await db.execute( + "UPDATE livestream.livestreams SET fee_pct = ? WHERE id = ?", + (fee_pct, ls_id), + ) + + +async def add_track( + livestream: int, + name: str, + download_url: Optional[str], + price_msat: int, + producer: Optional[int], +) -> int: + result = await db.execute( + """ + INSERT INTO livestream.tracks (livestream, name, download_url, price_msat, producer) + VALUES (?, ?, ?, ?, ?) + """, + (livestream, name, download_url, price_msat, producer), + ) + return result._result_proxy.lastrowid + + +async def update_track( + livestream: int, + track_id: int, + name: str, + download_url: Optional[str], + price_msat: int, + producer: int, +) -> int: + result = await db.execute( + """ + UPDATE livestream.tracks SET + name = ?, + download_url = ?, + price_msat = ?, + producer = ? + WHERE livestream = ? AND id = ? + """, + (name, download_url, price_msat, producer, livestream, track_id), + ) + return result._result_proxy.lastrowid + + +async def get_track(track_id: Optional[int]) -> Optional[Track]: + if not track_id: + return None + + row = await db.fetchone( + """ + SELECT id, download_url, price_msat, name, producer + FROM livestream.tracks WHERE id = ? + """, + (track_id,), + ) + return Track(**dict(row)) if row else None + + +async def get_tracks(livestream: int) -> List[Track]: + rows = await db.fetchall( + """ + SELECT id, download_url, price_msat, name, producer + FROM livestream.tracks WHERE livestream = ? + """, + (livestream,), + ) + return [Track(**dict(row)) for row in rows] + + +async def delete_track_from_livestream(livestream: int, track_id: int): + await db.execute( + """ + DELETE FROM livestream.tracks WHERE livestream = ? AND id = ? + """, + (livestream, track_id), + ) + + +async def add_producer(livestream: int, name: str) -> int: + name = name.strip() + + existing = await db.fetchall( + """ + SELECT id FROM livestream.producers + WHERE livestream = ? AND lower(name) = ? + """, + (livestream, name.lower()), + ) + if existing: + return existing[0].id + + user = await create_account() + wallet = await create_wallet(user_id=user.id, wallet_name="livestream: " + name) + + returning = "" if db.type == SQLITE else "RETURNING ID" + method = db.execute if db.type == SQLITE else db.fetchone + + result = await method( + f""" + INSERT INTO livestream.producers (livestream, name, "user", wallet) + VALUES (?, ?, ?, ?) + {returning} + """, + (livestream, name, user.id, wallet.id), + ) + if db.type == SQLITE: + return result._result_proxy.lastrowid + else: + return result[0] + + +async def get_producer(producer_id: int) -> Optional[Producer]: + row = await db.fetchone( + """ + SELECT id, "user", wallet, name + FROM livestream.producers WHERE id = ? + """, + (producer_id,), + ) + return Producer(**dict(row)) if row else None + + +async def get_producers(livestream: int) -> List[Producer]: + rows = await db.fetchall( + """ + SELECT id, "user", wallet, name + FROM livestream.producers WHERE livestream = ? + """, + (livestream,), + ) + return [Producer(**dict(row)) for row in rows] diff --git a/lnbits/extensions/livestream/lnurl.py b/lnbits/extensions/livestream/lnurl.py new file mode 100644 index 000000000..013cd2459 --- /dev/null +++ b/lnbits/extensions/livestream/lnurl.py @@ -0,0 +1,120 @@ +import hashlib +import math +from http import HTTPStatus +from os import name + +from fastapi.exceptions import HTTPException +from fastapi.params import Query +from lnurl import LnurlErrorResponse, LnurlPayActionResponse, LnurlPayResponse +from starlette.requests import Request # type: ignore + +from lnbits.core.services import create_invoice + +from . import livestream_ext +from .crud import get_livestream, get_livestream_by_track, get_track + + +@livestream_ext.get("/lnurl/{ls_id}", name="livestream.lnurl_livestream") +async def lnurl_livestream(ls_id, request: Request): + ls = await get_livestream(ls_id) + if not ls: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Livestream not found." + ) + + track = await get_track(ls.current_track) + if not track: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="This livestream is offline." + ) + + resp = LnurlPayResponse( + callback=request.url_for( + "livestream.lnurl_callback", track_id=track.id + ), + min_sendable=track.min_sendable, + max_sendable=track.max_sendable, + metadata=await track.lnurlpay_metadata(), + ) + + params = resp.dict() + params["commentAllowed"] = 300 + + return params + + +@livestream_ext.get("/lnurl/t/{track_id}", name="livestream.lnurl_track") +async def lnurl_track(track_id, request: Request): + track = await get_track(track_id) + if not track: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Track not found." + ) + + resp = LnurlPayResponse( + callback=request.url_for( + "livestream.lnurl_callback", track_id=track.id + ), + min_sendable=track.min_sendable, + max_sendable=track.max_sendable, + metadata=await track.lnurlpay_metadata(), + ) + + params = resp.dict() + params["commentAllowed"] = 300 + + return params + + +@livestream_ext.get("/lnurl/cb/{track_id}", name="livestream.lnurl_callback") +async def lnurl_callback(track_id, request: Request, amount: int = Query(...), comment: str = Query("")): + track = await get_track(track_id) + if not track: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail="Track not found." + ) + + amount_received = int(amount or 0) + + if amount_received < track.min_sendable: + return LnurlErrorResponse( + reason=f"Amount {round(amount_received / 1000)} is smaller than minimum {math.floor(track.min_sendable)}." + ).dict() + elif track.max_sendable < amount_received: + return LnurlErrorResponse( + reason=f"Amount {round(amount_received / 1000)} is greater than maximum {math.floor(track.max_sendable)}." + ).dict() + + if len(comment or "") > 300: + return LnurlErrorResponse( + reason=f"Got a comment with {len(comment)} characters, but can only accept 300" + ).dict() + + ls = await get_livestream_by_track(track_id) + + payment_hash, payment_request = await create_invoice( + wallet_id=ls.wallet, + amount=int(amount_received / 1000), + memo=await track.fullname(), + description_hash=hashlib.sha256( + (await track.lnurlpay_metadata()).encode("utf-8") + ).digest(), + extra={"tag": "livestream", "track": track.id, "comment": comment}, + ) + + if amount_received < track.price_msat: + success_action = None + else: + success_action = track.success_action(payment_hash, request=request) + + resp = LnurlPayActionResponse( + pr=payment_request, + success_action=success_action, + routes=[], + ) + + return resp.dict() diff --git a/lnbits/extensions/livestream/migrations.py b/lnbits/extensions/livestream/migrations.py new file mode 100644 index 000000000..fb664ab16 --- /dev/null +++ b/lnbits/extensions/livestream/migrations.py @@ -0,0 +1,39 @@ +async def m001_initial(db): + """ + Initial livestream tables. + """ + await db.execute( + f""" + CREATE TABLE livestream.livestreams ( + id {db.serial_primary_key}, + wallet TEXT NOT NULL, + fee_pct INTEGER NOT NULL DEFAULT 10, + current_track INTEGER + ); + """ + ) + + await db.execute( + f""" + CREATE TABLE livestream.producers ( + livestream INTEGER NOT NULL REFERENCES {db.references_schema}livestreams (id), + id {db.serial_primary_key}, + "user" TEXT NOT NULL, + wallet TEXT NOT NULL, + name TEXT NOT NULL + ); + """ + ) + + await db.execute( + f""" + CREATE TABLE livestream.tracks ( + livestream INTEGER NOT NULL REFERENCES {db.references_schema}livestreams (id), + id {db.serial_primary_key}, + download_url TEXT, + price_msat INTEGER NOT NULL DEFAULT 0, + name TEXT, + producer INTEGER REFERENCES {db.references_schema}producers (id) NOT NULL + ); + """ + ) diff --git a/lnbits/extensions/livestream/models.py b/lnbits/extensions/livestream/models.py new file mode 100644 index 000000000..8d3094c0c --- /dev/null +++ b/lnbits/extensions/livestream/models.py @@ -0,0 +1,89 @@ +import json +from typing import Optional + +from fastapi.params import Query +from lnurl import Lnurl +from lnurl import encode as lnurl_encode # type: ignore +from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore +from lnurl.types import LnurlPayMetadata # type: ignore +from pydantic.main import BaseModel +from starlette.requests import Request + + +class CreateTrack(BaseModel): + name: str = Query(...) + download_url: str = Query(None) + price_msat: int = Query(None, ge=0) + producer_id: str = Query(None) + producer_name: str = Query(None) + +class Livestream(BaseModel): + id: int + wallet: str + fee_pct: int + current_track: Optional[int] + + def lnurl(self, request: Request) -> Lnurl: + url = request.url_for("livestream.lnurl_livestream", ls_id=self.id) + return lnurl_encode(url) + + +class Track(BaseModel): + id: int + download_url: Optional[str] + price_msat: Optional[int] + name: str + producer: int + + @property + def min_sendable(self) -> int: + return min(100_000, self.price_msat or 100_000) + + @property + def max_sendable(self) -> int: + return max(50_000_000, self.price_msat * 5) + + def lnurl(self, request: Request) -> Lnurl: + url = request.url_for("livestream.lnurl_track", track_id=self.id) + return lnurl_encode(url) + + async def fullname(self) -> str: + from .crud import get_producer + + producer = await get_producer(self.producer) + if producer: + producer_name = producer.name + else: + producer_name = "unknown author" + + return f"'{self.name}', from {producer_name}." + + async def lnurlpay_metadata(self) -> LnurlPayMetadata: + description = ( + await self.fullname() + ) + " Like this track? Send some sats in appreciation." + + if self.download_url: + description += f" Send {round(self.price_msat/1000)} sats or more and you can download it." + + return LnurlPayMetadata(json.dumps([["text/plain", description]])) + + def success_action(self, payment_hash: str, request: Request) -> Optional[LnurlPaySuccessAction]: + if not self.download_url: + return None + + return UrlAction( + url=request.url_for( + "livestream.track_redirect_download", + track_id=self.id, + p=payment_hash + ), + description=f"Download the track {self.name}!", + ) + + +class Producer(BaseModel): + id: int + user: str + wallet: str + name: str diff --git a/lnbits/extensions/livestream/static/js/index.js b/lnbits/extensions/livestream/static/js/index.js new file mode 100644 index 000000000..c49befce2 --- /dev/null +++ b/lnbits/extensions/livestream/static/js/index.js @@ -0,0 +1,216 @@ +/* globals Quasar, Vue, _, VueQrcode, windowMixin, LNbits, LOCALE */ + +Vue.component(VueQrcode.name, VueQrcode) + +new Vue({ + el: '#vue', + mixins: [windowMixin], + data() { + return { + cancelListener: () => {}, + selectedWallet: null, + nextCurrentTrack: null, + livestream: { + tracks: [], + producers: [] + }, + trackDialog: { + show: false, + data: {} + } + } + }, + computed: { + sortedTracks() { + return this.livestream.tracks.sort((a, b) => a.name - b.name) + }, + tracksMap() { + return Object.fromEntries( + this.livestream.tracks.map(track => [track.id, track]) + ) + }, + producersMap() { + return Object.fromEntries( + this.livestream.producers.map(prod => [prod.id, prod]) + ) + } + }, + methods: { + getTrackLabel(trackId) { + if (!trackId) return + let track = this.tracksMap[trackId] + return `${track.name}, ${this.producersMap[track.producer].name}` + }, + disabledAddTrackButton() { + return ( + !this.trackDialog.data.name || + this.trackDialog.data.name.length === 0 || + !this.trackDialog.data.producer || + this.trackDialog.data.producer.length === 0 + ) + }, + changedWallet(wallet) { + this.selectedWallet = wallet + this.loadLivestream() + this.startPaymentNotifier() + }, + loadLivestream() { + LNbits.api + .request( + 'GET', + '/livestream/api/v1/livestream', + this.selectedWallet.inkey + ) + .then(response => { + this.livestream = response.data + this.nextCurrentTrack = this.livestream.current_track + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + }, + startPaymentNotifier() { + this.cancelListener() + + this.cancelListener = LNbits.events.onInvoicePaid( + this.selectedWallet, + payment => { + let satoshiAmount = Math.round(payment.amount / 1000) + let trackName = ( + this.tracksMap[payment.extra.track] || {name: '[unknown]'} + ).name + + this.$q.notify({ + message: `Someone paid ${satoshiAmount} sat for the track ${trackName}.`, + caption: payment.extra.comment + ? `"${payment.extra.comment}"` + : undefined, + color: 'secondary', + html: true, + timeout: 0, + actions: [{label: 'Dismiss', color: 'white', handler: () => {}}] + }) + } + ) + }, + addTrack() { + let {id, name, producer, price_sat, download_url} = this.trackDialog.data + + const [method, path] = id + ? ['PUT', `/livestream/api/v1/livestream/tracks/${id}`] + : ['POST', '/livestream/api/v1/livestream/tracks'] + + LNbits.api + .request(method, path, this.selectedWallet.inkey, { + download_url: + download_url && download_url.length > 0 ? download_url : undefined, + name, + price_msat: price_sat * 1000 || 0, + producer_name: typeof producer === 'string' ? producer : undefined, + producer_id: typeof producer === 'object' ? producer.id : undefined + }) + .then(response => { + this.$q.notify({ + message: `Track '${this.trackDialog.data.name}' added.`, + timeout: 700 + }) + this.loadLivestream() + this.trackDialog.show = false + this.trackDialog.data = {} + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + }, + openAddTrackDialog() { + this.trackDialog.show = true + this.trackDialog.data = {} + }, + openUpdateDialog(itemId) { + this.trackDialog.show = true + let item = this.livestream.tracks.find(item => item.id === itemId) + this.trackDialog.data = { + ...item, + producer: this.livestream.producers.find( + prod => prod.id === item.producer + ), + price_sat: Math.round(item.price_msat / 1000) + } + }, + deleteTrack(trackId) { + LNbits.utils + .confirmDialog('Are you sure you want to delete this track?') + .onOk(() => { + LNbits.api + .request( + 'DELETE', + '/livestream/api/v1/livestream/tracks/' + trackId, + this.selectedWallet.inkey + ) + .then(response => { + this.$q.notify({ + message: `Track deleted`, + timeout: 700 + }) + this.livestream.tracks.splice( + this.livestream.tracks.findIndex(track => track.id === trackId), + 1 + ) + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + }) + }, + updateCurrentTrack(track) { + console.log(this.nextCurrentTrack, this.livestream) + if (this.livestream.current_track === track) { + // if clicking the same, stop it + track = 0 + } + + LNbits.api + .request( + 'PUT', + '/livestream/api/v1/livestream/track/' + track, + this.selectedWallet.inkey + ) + .then(() => { + this.livestream.current_track = track + this.nextCurrentTrack = track + this.$q.notify({ + message: `Current track updated.`, + timeout: 700 + }) + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + }, + updateFeePct() { + LNbits.api + .request( + 'PUT', + '/livestream/api/v1/livestream/fee/' + this.livestream.fee_pct, + this.selectedWallet.inkey + ) + .then(() => { + this.$q.notify({ + message: `Percentage updated.`, + timeout: 700 + }) + }) + .catch(err => { + LNbits.utils.notifyApiError(err) + }) + }, + producerAdded(added, cb) { + cb(added) + } + }, + created() { + this.selectedWallet = this.g.user.wallets[0] + this.loadLivestream() + this.startPaymentNotifier() + } +}) diff --git a/lnbits/extensions/livestream/tasks.py b/lnbits/extensions/livestream/tasks.py new file mode 100644 index 000000000..1b2b42977 --- /dev/null +++ b/lnbits/extensions/livestream/tasks.py @@ -0,0 +1,93 @@ +import asyncio +import json + +from lnbits.core import db as core_db +from lnbits.core.crud import create_payment +from lnbits.core.models import Payment +from lnbits.helpers import urlsafe_short_hash +from lnbits.tasks import internal_invoice_listener, register_invoice_listener + +from .crud import get_livestream_by_track, get_producer, get_track + + +async def wait_for_paid_invoices(): + invoice_queue = asyncio.Queue() + register_invoice_listener(invoice_queue) + + while True: + payment = await invoice_queue.get() + await on_invoice_paid(payment) + +# async def register_listeners(): +# invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(2) +# register_invoice_listener(invoice_paid_chan_send) +# await wait_for_paid_invoices(invoice_paid_chan_recv) + + +async def on_invoice_paid(payment: Payment) -> None: + if "livestream" != payment.extra.get("tag"): + # not a livestream invoice + return + + track = await get_track(payment.extra.get("track", -1)) + if not track: + print("this should never happen", payment) + return + + if payment.extra.get("shared_with"): + print("payment was shared already", payment) + return + + producer = await get_producer(track.producer) + assert producer, f"track {track.id} is not associated with a producer" + + ls = await get_livestream_by_track(track.id) + assert ls, f"track {track.id} is not associated with a livestream" + + # now we make a special kind of internal transfer + amount = int(payment.amount * (100 - ls.fee_pct) / 100) + + # mark the original payment with two extra keys, "shared_with" and "received" + # (this prevents us from doing this process again and it's informative) + # and reduce it by the amount we're going to send to the producer + await core_db.execute( + """ + UPDATE apipayments + SET extra = ?, amount = ? + WHERE hash = ? + AND checking_id NOT LIKE 'internal_%' + """, + ( + json.dumps( + dict( + **payment.extra, + shared_with=[producer.name, producer.id], + received=payment.amount, + ) + ), + payment.amount - amount, + payment.payment_hash, + ), + ) + + # perform an internal transfer using the same payment_hash to the producer wallet + internal_checking_id = f"internal_{urlsafe_short_hash()}" + await create_payment( + wallet_id=producer.wallet, + checking_id=internal_checking_id, + payment_request="", + payment_hash=payment.payment_hash, + amount=amount, + memo=f"Revenue from '{track.name}'.", + pending=False, + ) + + # manually send this for now + # await internal_invoice_paid.send(internal_checking_id) + await internal_invoice_listener.put(internal_checking_id) + + # so the flow is the following: + # - we receive, say, 1000 satoshis + # - if the fee_pct is, say, 30%, the amount we will send is 700 + # - we change the amount of receiving payment on the database from 1000 to 300 + # - we create a new payment on the producer's wallet with amount 700 diff --git a/lnbits/extensions/livestream/templates/livestream/_api_docs.html b/lnbits/extensions/livestream/templates/livestream/_api_docs.html new file mode 100644 index 000000000..4c497d7f9 --- /dev/null +++ b/lnbits/extensions/livestream/templates/livestream/_api_docs.html @@ -0,0 +1,146 @@ + + + +

Add tracks, profit.

+
+
+
+ + + + + + GET + /livestream/api/v1/livestream +
Headers
+ {"X-Api-Key": <invoice_key>}
+
Body (application/json)
+
+ Returns 200 OK (application/json) +
+ [<livestream_object>, ...] +
Curl example
+ curl -X GET {{ request.url_root }}api/v1/livestream -H "X-Api-Key: {{ + user.wallets[0].inkey }}" + +
+
+
+ + + + PUT + /livestream/api/v1/livestream/track/<track_id> +
Headers
+ {"X-Api-Key": <invoice_key>}
+
Body (application/json)
+
+ Returns 201 CREATED (application/json) +
+
Curl example
+ curl -X PUT {{ request.url_root + }}api/v1/livestream/track/<track_id> -H "X-Api-Key: {{ + user.wallets[0].inkey }}" + +
+
+
+ + + + PUT + /livestream/api/v1/livestream/fee/<fee_pct> +
Headers
+ {"X-Api-Key": <invoice_key>}
+
Body (application/json)
+
+ Returns 201 CREATED (application/json) +
+
Curl example
+ curl -X PUT {{ request.url_root + }}api/v1/livestream/fee/<fee_pct> -H "X-Api-Key: {{ + user.wallets[0].inkey }}" + +
+
+
+ + + + + POST + /livestream/api/v1/livestream/tracks +
Headers
+ {"X-Api-Key": <invoice_key>}
+
Body (application/json)
+ {"name": <string>, "download_url": <string>, + "price_msat": <integer>, "producer_id": <integer>, + "producer_name": <string>} +
+ Returns 201 CREATED (application/json) +
+
Curl example
+ curl -X POST {{ request.url_root }}api/v1/livestream/tracks -d + '{"name": <string>, "download_url": <string>, + "price_msat": <integer>, "producer_id": <integer>, + "producer_name": <string>}' -H "Content-type: application/json" + -H "X-Api-Key: {{ user.wallets[0].adminkey }}" + +
+
+
+ + + + DELETE + /livestream/api/v1/livestream/tracks/<track_id> +
Headers
+ {"X-Api-Key": <invoice_key>}
+
Returns 204 NO CONTENT
+ +
Curl example
+ curl -X DELETE {{ request.url_root + }}api/v1/livestream/tracks/<track_id> -H "X-Api-Key: {{ + user.wallets[0].inkey }}" + +
+
+
+
diff --git a/lnbits/extensions/livestream/templates/livestream/index.html b/lnbits/extensions/livestream/templates/livestream/index.html new file mode 100644 index 000000000..a93bab71e --- /dev/null +++ b/lnbits/extensions/livestream/templates/livestream/index.html @@ -0,0 +1,322 @@ +{% extends "base.html" %} {% from "macros.jinja" import window_vars with context +%} {% block page %} +
+
+ + +
+
+ +
+
+ {% raw %} + + {{ nextCurrentTrack && nextCurrentTrack === + livestream.current_track ? 'Stop' : 'Set' }} current track + + {% endraw %} +
+
+
+ +
+
+ +
+
+ Set percent rate +
+
+
+
+ + + +
+
+
Tracks
+
+
+ Add new track +
+
+ + {% raw %} + + + {% endraw %} + +
+
+ + + +
+
+
Producers
+
+
+ + {% raw %} + + + {% endraw %} + +
+
+ + + + + + + + + + + + + + + Copy LNURL-pay code + + +
+ +
+ + +
+ {{SITE_TITLE}} Livestream extension +
+
+ + + {% include "livestream/_api_docs.html" %} + +
+
+ + + + +

+ Standalone QR Code for this track +

+ + + + + + + Copy LNURL-pay code +
+ + + + + + + +
+
+ + Update track + Add track + +
+
+ Cancel +
+
+
+
+
+
+
+{% endblock %} {% block scripts %} {{ window_vars(user) }} + +{% endblock %} diff --git a/lnbits/extensions/livestream/views.py b/lnbits/extensions/livestream/views.py new file mode 100644 index 000000000..a56fadb17 --- /dev/null +++ b/lnbits/extensions/livestream/views.py @@ -0,0 +1,41 @@ +from http import HTTPStatus +from mmap import MAP_DENYWRITE + +from fastapi.param_functions import Depends +from fastapi.params import Query +from starlette.exceptions import HTTPException +from starlette.requests import Request +from starlette.responses import HTMLResponse, RedirectResponse + +from lnbits.core.crud import get_wallet_payment +from lnbits.core.models import Payment, User +from lnbits.decorators import check_user_exists + +from . import livestream_ext, livestream_renderer +from .crud import get_livestream_by_track, get_track + + +@livestream_ext.get("/", response_class=HTMLResponse) +async def index(request: Request, user: User = Depends(check_user_exists)): + return livestream_renderer().TemplateResponse("livestream/index.html", {"request": request, "user": user.dict()}) + + +@livestream_ext.get("/track/{track_id}", name="livestream.track_redirect_download") +async def track_redirect_download(track_id, request: Request): + payment_hash = request.path_params["p"] + track = await get_track(track_id) + ls = await get_livestream_by_track(track_id) + payment: Payment = await get_wallet_payment(ls.wallet, payment_hash) + + if not payment: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, + detail=f"Couldn't find the payment {payment_hash} or track {track.id}." + ) + + if payment.pending: + raise HTTPException( + status_code=HTTPStatus.PAYMENT_REQUIRED, + detail=f"Payment {payment_hash} wasn't received yet. Please try again in a minute." + ) + return RedirectResponse(url=track.download_url) diff --git a/lnbits/extensions/livestream/views_api.py b/lnbits/extensions/livestream/views_api.py new file mode 100644 index 000000000..9dc9cdcc6 --- /dev/null +++ b/lnbits/extensions/livestream/views_api.py @@ -0,0 +1,107 @@ +from http import HTTPStatus + +from fastapi.param_functions import Depends +from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl +from starlette.exceptions import HTTPException +from starlette.requests import Request # type: ignore + +from lnbits.decorators import WalletTypeInfo, get_key_type +from lnbits.extensions.livestream.models import CreateTrack + +from . import livestream_ext +from .crud import ( + add_producer, + add_track, + delete_track_from_livestream, + get_or_create_livestream_by_wallet, + get_producers, + get_tracks, + update_current_track, + update_livestream_fee, + update_track, +) + + +@livestream_ext.get("/api/v1/livestream") +async def api_livestream_from_wallet(req: Request, g: WalletTypeInfo = Depends(get_key_type)): + ls = await get_or_create_livestream_by_wallet(g.wallet.id) + tracks = await get_tracks(ls.id) + producers = await get_producers(ls.id) + print("INIT", ls, tracks, producers) + try: + return { + **ls.dict(), + **{ + "lnurl": ls.lnurl(request=req), + "tracks": [ + dict(lnurl=track.lnurl(request=req), **track.dict()) + for track in tracks + ], + "producers": [producer.dict() for producer in producers], + }, + } + except LnurlInvalidUrl: + raise HTTPException( + status_code=HTTPStatus.UPGRADE_REQUIRED, + detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor." + ) + + +@livestream_ext.put("/api/v1/livestream/track/{track_id}") +async def api_update_track(track_id, g: WalletTypeInfo = Depends(get_key_type)): + try: + id = int(track_id) + except ValueError: + id = 0 + if id <= 0: + id = None + + ls = await get_or_create_livestream_by_wallet(g.wallet.id) + await update_current_track(ls.id, id) + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + + +@livestream_ext.put("/api/v1/livestream/fee/{fee_pct}") +async def api_update_fee(fee_pct, g: WalletTypeInfo = Depends(get_key_type)): + ls = await get_or_create_livestream_by_wallet(g.wallet.id) + await update_livestream_fee(ls.id, int(fee_pct)) + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + + +@livestream_ext.post("/api/v1/livestream/tracks") +@livestream_ext.put("/api/v1/livestream/tracks/{id}") +async def api_add_track(data: CreateTrack, id=None, g: WalletTypeInfo = Depends(get_key_type)): + ls = await get_or_create_livestream_by_wallet(g.wallet.id) + + if data.producer_id: + p_id = data.producer_id + elif data.producer_name: + p_id = await add_producer(ls.id, data.producer_name) + else: + raise TypeError("need either producer_id or producer_name arguments") + + if id: + await update_track( + ls.id, + id, + data.name, + data.download_url, + data.price_msat or 0, + p_id, + ) + else: + await add_track( + ls.id, + data.name, + data.download_url, + data.price_msat or 0, + p_id, + ) + return + + +@livestream_ext.route("/api/v1/livestream/tracks/{track_id}") +async def api_delete_track(track_id, g: WalletTypeInfo = Depends(get_key_type)): + ls = await get_or_create_livestream_by_wallet(g.wallet.id) + await delete_track_from_livestream(ls.id, track_id) + raise HTTPException(status_code=HTTPStatus.NO_CONTENT)