create scrub link

This commit is contained in:
Tiago vasconcelos 2022-06-17 11:41:12 +01:00
parent 395b4379a4
commit 1a394d0a73
5 changed files with 48 additions and 119 deletions

View file

@ -1,30 +1,31 @@
from typing import List, Optional, Union from typing import List, Optional, Union
from lnbits.db import SQLITE from lnbits.helpers import urlsafe_short_hash
from . import db from . import db
from .models import ScrubLink from .models import CreateScrubLink, ScrubLink
async def create_scrub_link(wallet_id: str, data: ScrubLink) -> ScrubLink: async def create_scrub_link(wallet_id: str, data: CreateScrubLink) -> ScrubLink:
satsdice_id = urlsafe_short_hash() scrub_id = urlsafe_short_hash()
await db.execute( await db.execute(
""" """
INSERT INTO scrub.scrub_links ( INSERT INTO scrub.scrub_links (
id, id,
wallet, wallet,
description, description,
payoraddress, payoraddress
) )
VALUES (?, ?, ?) VALUES (?, ?, ?, ?)
""", """,
( (
satsdice_id, scrub_id,
wallet, data.wallet,
description, data.description,
payoraddress, data.payoraddress,
), ),
) )
link = await get_satsdice_pay(satsdice_id) link = await get_scrub_link(scrub_id)
assert link, "Newly created link couldn't be retrieved" assert link, "Newly created link couldn't be retrieved"
return link return link

View file

@ -1,11 +1,13 @@
import json import json
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult
from starlette.requests import Request
from fastapi.param_functions import Query
from typing import Optional, Dict
from lnbits.lnurl import encode as lnurl_encode # type: ignore
from sqlite3 import Row from sqlite3 import Row
from typing import Dict, Optional
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
from fastapi.param_functions import Query
from pydantic import BaseModel from pydantic import BaseModel
from starlette.requests import Request
from lnbits.lnurl import encode as lnurl_encode # type: ignore
class CreateScrubLink(BaseModel): class CreateScrubLink(BaseModel):
@ -15,7 +17,7 @@ class CreateScrubLink(BaseModel):
class ScrubLink(BaseModel): class ScrubLink(BaseModel):
id: int id: str
wallet: str wallet: str
description: str description: str
payoraddress: str payoraddress: str

View file

@ -106,7 +106,8 @@ new Vue({
const wallet = _.findWhere(this.g.user.wallets, { const wallet = _.findWhere(this.g.user.wallets, {
id: this.formDialog.data.wallet id: this.formDialog.data.wallet
}) })
console.log(wallet) let data = Object.freeze(this.formDialog.data)
console.log(wallet, data)
if (data.id) { if (data.id) {
this.updateScrubLink(wallet, data) this.updateScrubLink(wallet, data)
@ -162,6 +163,7 @@ new Vue({
LNbits.api LNbits.api
.request('POST', '/scrub/api/v1/links', wallet.adminkey, data) .request('POST', '/scrub/api/v1/links', wallet.adminkey, data)
.then(response => { .then(response => {
console.log('RES', response)
this.getScrubLinks() this.getScrubLinks()
this.formDialog.show = false this.formDialog.show = false
this.resetFormData() this.resetFormData()
@ -207,9 +209,9 @@ new Vue({
if (this.g.user.wallets.length) { if (this.g.user.wallets.length) {
var getScrubLinks = this.getScrubLinks var getScrubLinks = this.getScrubLinks
getScrubLinks() getScrubLinks()
this.checker = setInterval(() => { // this.checker = setInterval(() => {
getScrubLinks() // getScrubLinks()
}, 20000) // }, 20000)
} }
LNbits.api LNbits.api
.request('GET', '/scrub/api/v1/currencies') .request('GET', '/scrub/api/v1/currencies')

View file

@ -26,71 +26,18 @@
> >
{% raw %} {% raw %}
<template v-slot:header="props"> <template v-slot:header="props">
<q-tr :props="props"> <q-tr :props="props" style="text-align: left">
<q-th auto-width></q-th> <q-th>Wallet</q-th>
<q-th auto-width>Wallet</q-th> <q-th>Description</q-th>
<q-th auto-width>Description</q-th> <q-th>LNURLPay/Address</q-th>
<q-th auto-width>LNURLPay/Address</q-th>
<q-th auto-width></q-th>
<q-th auto-width></q-th> <q-th auto-width></q-th>
</q-tr> </q-tr>
</template> </template>
<template v-slot:body="props"> <template v-slot:body="props">
<q-tr :props="props"> <q-tr :props="props">
<q-td auto-width> <q-td>{{ props.row.wallet }}</q-td>
<q-btn <q-td>{{ props.row.description }}</q-td>
unelevated <q-td>{{ props.row.payoraddress }}</q-td>
dense
size="xs"
icon="launch"
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
type="a"
:href="props.row.pay_url"
target="_blank"
></q-btn>
<q-btn
unelevated
dense
size="xs"
icon="visibility"
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
@click="openQrCodeDialog(props.row.id)"
></q-btn>
</q-td>
<q-td auto-width>{{ props.row.description }}</q-td>
<q-td auto-width>
<span v-if="props.row.min == props.row.max">
{{ props.row.min }}
</span>
<span v-else>{{ props.row.min }} - {{ props.row.max }}</span>
</q-td>
<q-td>{{ props.row.currency || 'sat' }}</q-td>
<q-td>
<q-icon v-if="props.row.webhook_url" size="14px" name="http">
<q-tooltip>Webhook to {{ props.row.webhook_url}}</q-tooltip>
</q-icon>
<q-icon
v-if="props.row.success_text || props.row.success_url"
size="14px"
name="call_to_action"
>
<q-tooltip>
On success, show message '{{ props.row.success_text }}'
<span v-if="props.row.success_url"
>and URL '{{ props.row.success_url }}'</span
>
</q-tooltip>
</q-icon>
<q-icon
v-if="props.row.comment_chars > 0"
size="14px"
name="insert_comment"
>
<q-tooltip>
{{ props.row.comment_chars }}-char comment allowed
</q-tooltip>
</q-icon>
</q-td>
<q-td auto-width> <q-td auto-width>
<q-btn <q-btn
flat flat

View file

@ -7,7 +7,7 @@ from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from lnbits.core.crud import get_user from lnbits.core.crud import get_user
from lnbits.decorators import WalletTypeInfo, get_key_type from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from lnbits.utils.exchange_rates import currencies, get_fiat_rate_satoshis from lnbits.utils.exchange_rates import currencies, get_fiat_rate_satoshis
from . import scrub_ext from . import scrub_ext
@ -18,7 +18,7 @@ from .crud import (
get_scrub_links, get_scrub_links,
update_scrub_link, update_scrub_link,
) )
from .models import ScrubLink from .models import CreateScrubLink, ScrubLink
@scrub_ext.get("/api/v1/currencies") @scrub_ext.get("/api/v1/currencies")
@ -40,7 +40,7 @@ async def api_links(
try: try:
return [ return [
{**link.dict()} {**link.dict()}
for link in await get_pay_links(wallet_ids) for link in await get_scrub_links(wallet_ids)
] ]
except: except:
@ -54,7 +54,7 @@ async def api_links(
async def api_link_retrieve( async def api_link_retrieve(
r: Request, link_id, wallet: WalletTypeInfo = Depends(get_key_type) r: Request, link_id, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
link = await get_pay_link(link_id) link = await get_scrub_link(link_id)
if not link: if not link:
raise HTTPException( raise HTTPException(
@ -71,37 +71,14 @@ async def api_link_retrieve(
@scrub_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED) @scrub_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
@scrub_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK) @scrub_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
async def api_link_create_or_update( async def api_scrub_create_or_update(
data: ScrubLink, data: CreateScrubLink,
link_id=None, link_id=None,
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(require_admin_key),
): ):
print("WAH") print("WAH")
if data.min < 1:
raise HTTPException(
detail="Min must be more than 1.", status_code=HTTPStatus.BAD_REQUEST
)
if data.min > data.max:
raise HTTPException(
detail="Min is greater than max.", status_code=HTTPStatus.BAD_REQUEST
)
if data.currency == None and (
round(data.min) != data.min or round(data.max) != data.max
):
raise HTTPException(
detail="Must use full satoshis.", status_code=HTTPStatus.BAD_REQUEST
)
if "success_url" in data and data.success_url[:8] != "https://":
raise HTTPException(
detail="Success URL must be secure https://...",
status_code=HTTPStatus.BAD_REQUEST,
)
if link_id: if link_id:
link = await get_pay_link(link_id) link = await get_scrub_link(link_id)
if not link: if not link:
raise HTTPException( raise HTTPException(
@ -113,16 +90,16 @@ async def api_link_create_or_update(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
) )
link = await update_pay_link(**data.dict(), link_id=link_id) link = await update_scrub_link(**data.dict(), link_id=link_id)
else: else:
link = await create_pay_link(data, wallet_id=wallet.wallet.id) link = await create_scrub_link(wallet_id=wallet.wallet.id, data=data)
return {**link.dict(), "lnurl": link.lnurl} return link
@scrub_ext.delete("/api/v1/links/{link_id}") @scrub_ext.delete("/api/v1/links/{link_id}")
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)): async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
link = await get_pay_link(link_id) link = await get_scrub_link(link_id)
if not link: if not link:
raise HTTPException( raise HTTPException(
@ -134,7 +111,7 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
) )
await delete_pay_link(link_id) await delete_scrub_link(link_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT) raise HTTPException(status_code=HTTPStatus.NO_CONTENT)