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 lnbits.core.crud import create_account, create_wallet
@ -65,22 +64,36 @@ async def add_track(
name: str,
download_url: Optional[str],
price_msat: int,
producer_name: Optional[str],
producer_id: Optional[int],
producer: Optional[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(
"""
INSERT INTO tracks (livestream, name, download_url, price_msat, producer)
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
@ -120,7 +133,7 @@ async def delete_track_from_livestream(livestream: int, track_id: 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(
"""

View File

@ -93,24 +93,21 @@ new Vue({
)
},
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
.request(
'POST',
'/livestream/api/v1/livestream/tracks',
this.selectedWallet.inkey,
{
.request(method, path, this.selectedWallet.inkey, {
download_url:
download_url && download_url.length > 0
? download_url
: undefined,
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.`,
@ -124,6 +121,21 @@ new Vue({
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?')

View File

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

View File

@ -9,9 +9,11 @@ from .crud import (
get_or_create_livestream_by_wallet,
add_track,
get_tracks,
update_track,
add_producer,
get_producers,
update_livestream_fee,
update_current_track,
update_livestream_fee,
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/<id>", methods=["PUT"])
@api_check_wallet_key("invoice")
@api_validate_post_request(
schema={
@ -90,15 +93,33 @@ 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)
if "producer_id" in g.data:
p_id = g.data["producer_id"]
elif "producer_name" in g.data:
p_id = await add_producer(ls.id, g.data["producer_name"])
else:
raise TypeError("need either producer_id or producer_name arguments")
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),
g.data.get("producer_name"),
g.data.get("producer_id"),
p_id,
)
return "", HTTPStatus.CREATED