make price optional then fix min/max issues.

This commit is contained in:
fiatjaf 2020-11-23 23:30:04 -03:00
parent 42cf70e158
commit 52e7a28fc2
5 changed files with 18 additions and 11 deletions

View File

@ -21,8 +21,8 @@ async def lnurl_response(ls_id):
resp = LnurlPayResponse(
callback=url_for("livestream.lnurl_callback", track_id=track.id, _external=True),
min_sendable=min(100000, track.price_msat),
max_sendable=track.price_msat * 5,
min_sendable=track.min_sendable,
max_sendable=track.max_sendable,
metadata=await track.lnurlpay_metadata(),
)
@ -40,19 +40,19 @@ async def lnurl_callback(track_id):
amount_received = int(request.args.get("amount"))
if amount_received < track.price_msat:
if amount_received < track.min_sendable:
return (
jsonify(
LnurlErrorResponse(
reason=f"Amount {round(amount_received / 1000)} is smaller than minimum {math.floor(track.price_msat / 1000)}."
reason=f"Amount {round(amount_received / 1000)} is smaller than minimum {math.floor(track.min_sendable)}."
).dict()
),
)
elif track.price_msat * 5 < amount_received:
elif track.max_sendable < amount_received:
return (
jsonify(
LnurlErrorResponse(
reason=f"Amount {round(amount_received / 1000)} is greater than maximum {math.floor(track.price_msat * 5 / 1000)}."
reason=f"Amount {round(amount_received / 1000)} is greater than maximum {math.floor(track.max_sendable)}."
).dict()
),
)

View File

@ -25,6 +25,14 @@ class Track(NamedTuple):
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)
async def fullname(self) -> str:
from .crud import get_producer

View File

@ -44,7 +44,6 @@ new Vue({
return (
!this.trackDialog.data.name ||
this.trackDialog.data.name.length === 0 ||
!this.trackDialog.data.price_sat ||
!this.trackDialog.data.producer ||
this.trackDialog.data.producer.length === 0
)
@ -107,7 +106,7 @@ new Vue({
? download_url
: undefined,
name,
price_msat: price_sat * 1000,
price_msat: price_sat * 1000 || 0,
producer_name: typeof producer === 'string' ? producer : undefined,
producer_id: typeof producer === 'object' ? producer.id : undefined
}

View File

@ -252,7 +252,7 @@
type="number"
min="1"
label="Track price (sat)"
hint="This is a minimum price. Payments up to 5x bigger than this will be accepted."
hint="This is the minimum price for buying the track download link. It does nothing for tracks without a download URL."
></q-input>
<q-input
filled

View File

@ -73,7 +73,7 @@ async def api_update_fee(fee_pct):
schema={
"name": {"type": "string", "empty": False, "required": True},
"download_url": {"type": "string", "empty": False, "required": False},
"price_msat": {"type": "number", "min": 1, "required": True},
"price_msat": {"type": "number", "min": 0, "required": False},
"producer_id": {"type": "number", "required": True, "excludes": "producer_name"},
"producer_name": {"type": "string", "required": True, "excludes": "producer_id"},
}
@ -84,7 +84,7 @@ async def api_add_track():
ls.id,
g.data["name"],
g.data.get("download_url"),
g.data["price_msat"],
g.data.get("price_msat", 0),
g.data.get("producer_name"),
g.data.get("producer_id"),
)