mirror of
https://github.com/lnbits/lnbits.git
synced 2025-04-21 22:14:57 +02:00
recording card tapping
This commit is contained in:
parent
4fab2d3101
commit
3cb62d1899
@ -3,7 +3,7 @@ from typing import List, Optional, Union
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
|
||||
from . import db
|
||||
from .models import Card, CreateCardData
|
||||
from .models import Card, CreateCardData, Hit
|
||||
|
||||
async def create_card(
|
||||
data: CreateCardData, wallet_id: str
|
||||
@ -34,9 +34,9 @@ async def create_card(
|
||||
data.meta_key,
|
||||
),
|
||||
)
|
||||
link = await get_card(card_id, 0)
|
||||
assert link, "Newly created card couldn't be retrieved"
|
||||
return link
|
||||
card = await get_card(card_id, 0)
|
||||
assert card, "Newly created card couldn't be retrieved"
|
||||
return card
|
||||
|
||||
async def update_card(card_id: str, **kwargs) -> Optional[Card]:
|
||||
if "is_unique" in kwargs:
|
||||
@ -88,4 +88,55 @@ async def update_card_counter(counter: int, id: str):
|
||||
await db.execute(
|
||||
"UPDATE boltcards.cards SET counter = ? WHERE id = ?",
|
||||
(counter, id),
|
||||
)
|
||||
)
|
||||
|
||||
async def get_hit(hit_id: str) -> Optional[Hit]:
|
||||
row = await db.fetchone(
|
||||
f"SELECT * FROM boltcards.hits WHERE id = ?", (hit_id)
|
||||
)
|
||||
if not row:
|
||||
return None
|
||||
|
||||
hit = dict(**row)
|
||||
|
||||
return Hit.parse_obj(hit)
|
||||
|
||||
async def get_hits(wallet_ids: Union[str, List[str]]) -> List[Hit]:
|
||||
|
||||
cards = get_cards(wallet_ids)
|
||||
|
||||
q = ",".join(["?"] * len(cards))
|
||||
rows = await db.fetchall(
|
||||
f"SELECT * FROM boltcards.hits WHERE wallet IN ({q})", (*(card.card_id for card in cards),)
|
||||
)
|
||||
|
||||
return [Card(**row) for row in rows]
|
||||
|
||||
async def create_hit(
|
||||
card_id, ip, useragent, old_ctr, new_ctr
|
||||
) -> Hit:
|
||||
hit_id = urlsafe_short_hash()
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO boltcards.hits (
|
||||
id,
|
||||
card_id,
|
||||
ip,
|
||||
useragent,
|
||||
old_ctr,
|
||||
new_ctr
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
hit_id,
|
||||
card_id,
|
||||
ip,
|
||||
useragent,
|
||||
old_ctr,
|
||||
new_ctr,
|
||||
),
|
||||
)
|
||||
hit = await get_hit(hit_id)
|
||||
assert hit, "Newly recorded hit couldn't be retrieved"
|
||||
return hit
|
||||
|
@ -18,3 +18,19 @@ async def m001_initial(db):
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE boltcards.hits (
|
||||
id TEXT PRIMARY KEY,
|
||||
card_id TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
useragent TEXT,
|
||||
old_ctr INT NOT NULL DEFAULT 0,
|
||||
new_ctr INT NOT NULL DEFAULT 0,
|
||||
time TIMESTAMP NOT NULL DEFAULT """
|
||||
+ db.timestamp_now
|
||||
+ """
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
@ -18,4 +18,22 @@ class CreateCardData(BaseModel):
|
||||
counter: str = Query(...)
|
||||
withdraw: str = Query(...)
|
||||
file_key: str = Query(...)
|
||||
meta_key: str = Query(...)
|
||||
meta_key: str = Query(...)
|
||||
|
||||
class Hit(BaseModel):
|
||||
id: str
|
||||
card_id: str
|
||||
ip: str
|
||||
useragent: str
|
||||
old_ctr: int
|
||||
new_ctr: int
|
||||
time: int
|
||||
|
||||
'''
|
||||
class CreateHitData(BaseModel):
|
||||
card_id: str = Query(...)
|
||||
ip: str = Query(...)
|
||||
useragent: str = Query(...)
|
||||
old_ctr: int = Query(...)
|
||||
new_ctr: int = Query(...)
|
||||
'''
|
||||
|
@ -368,6 +368,7 @@
|
||||
if (this.g.user.wallets.length) {
|
||||
this.getCards()
|
||||
this.getWithdraws()
|
||||
this.getHits()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -19,6 +19,7 @@ from lnbits.extensions.withdraw import get_withdraw_link
|
||||
from . import boltcards_ext
|
||||
from .nxp424 import decryptSUN, getSunMAC
|
||||
from .crud import (
|
||||
create_hit,
|
||||
get_all_cards,
|
||||
get_cards,
|
||||
get_card,
|
||||
@ -43,7 +44,7 @@ async def api_cards(
|
||||
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED)
|
||||
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
|
||||
async def api_link_create_or_update(
|
||||
req: Request,
|
||||
# req: Request,
|
||||
data: CreateCardData,
|
||||
card_id: str = None,
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
@ -157,5 +158,13 @@ async def api_scane(
|
||||
|
||||
await update_card_counter(counter_int, card.id)
|
||||
|
||||
ip = request.client.host
|
||||
if request.headers['x-real-ip']:
|
||||
ip = request.headers['x-real-ip']
|
||||
elif request.headers['x-forwarded-for']:
|
||||
ip = request.headers['x-forwarded-for']
|
||||
|
||||
await create_hit(card.id, ip, request.headers['user-agent'], card.counter, counter_int)
|
||||
|
||||
link = await get_withdraw_link(card.withdraw, 0)
|
||||
return link.lnurl_response(request)
|
||||
|
Loading…
x
Reference in New Issue
Block a user