livestream: allow tracks to be edited.

This commit is contained in:
fiatjaf 2021-04-16 23:36:13 -03:00
parent a3bd36d44f
commit 27e170d304
4 changed files with 100 additions and 43 deletions

View file

@ -1,4 +1,3 @@
import unicodedata
from typing import List, Optional from typing import List, Optional
from lnbits.core.crud import create_account, create_wallet from lnbits.core.crud import create_account, create_wallet
@ -65,22 +64,36 @@ async def add_track(
name: str, name: str,
download_url: Optional[str], download_url: Optional[str],
price_msat: int, price_msat: int,
producer_name: Optional[str], producer: Optional[int],
producer_id: Optional[int],
) -> int: ) -> int:
if producer_id:
p_id = producer_id
elif producer_name:
p_id = await add_producer(livestream, producer_name)
else:
raise TypeError("need either producer_id or producer_name arguments")
result = await db.execute( result = await db.execute(
""" """
INSERT INTO tracks (livestream, name, download_url, price_msat, producer) INSERT INTO tracks (livestream, name, download_url, price_msat, producer)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?)
""", """,
(livestream, name, download_url, price_msat, p_id), (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 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 return result._result_proxy.lastrowid
@ -120,7 +133,7 @@ async def delete_track_from_livestream(livestream: int, track_id: int):
async def add_producer(livestream: int, name: str) -> int: async def add_producer(livestream: int, name: str) -> int:
name = "".join([unicodedata.normalize("NFD", l)[0] for l in name if l]).strip() name = name.strip()
existing = await db.fetchall( existing = await db.fetchall(
""" """

View file

@ -93,24 +93,21 @@ new Vue({
) )
}, },
addTrack() { addTrack() {
let {name, producer, price_sat, download_url} = this.trackDialog.data 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 LNbits.api
.request( .request(method, path, this.selectedWallet.inkey, {
'POST', download_url:
'/livestream/api/v1/livestream/tracks', download_url && download_url.length > 0 ? download_url : undefined,
this.selectedWallet.inkey, name,
{ price_msat: price_sat * 1000 || 0,
download_url: producer_name: typeof producer === 'string' ? producer : undefined,
download_url && download_url.length > 0 producer_id: typeof producer === 'object' ? producer.id : undefined
? 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 => { .then(response => {
this.$q.notify({ this.$q.notify({
message: `Track '${this.trackDialog.data.name}' added.`, message: `Track '${this.trackDialog.data.name}' added.`,
@ -124,6 +121,21 @@ new Vue({
LNbits.utils.notifyApiError(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) { deleteTrack(trackId) {
LNbits.utils LNbits.utils
.confirmDialog('Are you sure you want to delete this track?') .confirmDialog('Are you sure you want to delete this track?')

View file

@ -64,7 +64,7 @@
<q-btn <q-btn
unelevated unelevated
color="deep-purple" color="deep-purple"
@click="trackDialog.show = true" @click="openAddTrackDialog"
>Add new track</q-btn >Add new track</q-btn
> >
</div> </div>
@ -107,12 +107,20 @@
{{ producersMap[props.row.producer].name }} {{ producersMap[props.row.producer].name }}
</q-td> </q-td>
<q-td class="text-right" auto-width <q-td class="text-right" auto-width
>{{ props.row.price_msat }}</q-td >{{ Math.round(props.row.price_msat / 1000) }}</q-td
> >
<q-td class="text-center" auto-width <q-td class="text-center" auto-width
>{{ props.row.download_url }}</q-td >{{ props.row.download_url }}</q-td
> >
<q-td auto-width> <q-td auto-width>
<q-btn
flat
dense
size="xs"
@click="openUpdateDialog(props.row.id)"
icon="edit"
color="light-blue"
></q-btn>
<q-btn <q-btn
unelevated unelevated
dense dense
@ -186,7 +194,7 @@
</q-select> </q-select>
</q-form> </q-form>
<a :href="livestream.url"> <a :href="'lightning:' + livestream.lnurl">
<q-responsive :ratio="1" class="q-mx-sm"> <q-responsive :ratio="1" class="q-mx-sm">
<qrcode <qrcode
:value="livestream.lnurl" :value="livestream.lnurl"
@ -269,7 +277,10 @@
color="deep-purple" color="deep-purple"
:disable="disabledAddTrackButton()" :disable="disabledAddTrackButton()"
type="submit" type="submit"
>Add track</q-btn >
<span v-if="trackDialog.data.id">Update track</span>
<span v-else>Add track</span>
</q-btn
> >
</div> </div>
<div class="col q-ml-lg"> <div class="col q-ml-lg">

View file

@ -9,9 +9,11 @@ from .crud import (
get_or_create_livestream_by_wallet, get_or_create_livestream_by_wallet,
add_track, add_track,
get_tracks, get_tracks,
update_track,
add_producer,
get_producers, get_producers,
update_livestream_fee,
update_current_track, update_current_track,
update_livestream_fee,
delete_track_from_livestream, delete_track_from_livestream,
) )
@ -72,6 +74,7 @@ async def api_update_fee(fee_pct):
@livestream_ext.route("/api/v1/livestream/tracks", methods=["POST"]) @livestream_ext.route("/api/v1/livestream/tracks", methods=["POST"])
@livestream_ext.route("/api/v1/livestream/tracks/<id>", methods=["PUT"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
@api_validate_post_request( @api_validate_post_request(
schema={ schema={
@ -90,17 +93,35 @@ async def api_update_fee(fee_pct):
}, },
} }
) )
async def api_add_track(): async def api_add_track(id=None):
ls = await get_or_create_livestream_by_wallet(g.wallet.id) ls = await get_or_create_livestream_by_wallet(g.wallet.id)
await add_track(
ls.id, if "producer_id" in g.data:
g.data["name"], p_id = g.data["producer_id"]
g.data.get("download_url"), elif "producer_name" in g.data:
g.data.get("price_msat", 0), p_id = await add_producer(ls.id, g.data["producer_name"])
g.data.get("producer_name"), else:
g.data.get("producer_id"), raise TypeError("need either producer_id or producer_name arguments")
)
return "", HTTPStatus.CREATED if id:
await update_track(
ls.id,
id,
g.data["name"],
g.data.get("download_url"),
g.data.get("price_msat", 0),
p_id,
)
return "", HTTPStatus.OK
else:
await add_track(
ls.id,
g.data["name"],
g.data.get("download_url"),
g.data.get("price_msat", 0),
p_id,
)
return "", HTTPStatus.CREATED
@livestream_ext.route("/api/v1/livestream/tracks/<track_id>", methods=["DELETE"]) @livestream_ext.route("/api/v1/livestream/tracks/<track_id>", methods=["DELETE"])