From 3cb62d1899d6a15895d97cc30ec0ac8483649d5b Mon Sep 17 00:00:00 2001 From: iWarpBTC Date: Tue, 21 Jun 2022 18:03:20 +0200 Subject: [PATCH] recording card tapping --- lnbits/extensions/boltcards/crud.py | 61 +++++++++++++++++-- lnbits/extensions/boltcards/migrations.py | 16 +++++ lnbits/extensions/boltcards/models.py | 20 +++++- .../boltcards/templates/boltcards/index.html | 1 + lnbits/extensions/boltcards/views_api.py | 11 +++- 5 files changed, 102 insertions(+), 7 deletions(-) diff --git a/lnbits/extensions/boltcards/crud.py b/lnbits/extensions/boltcards/crud.py index e8fb5477a..62aad3560 100644 --- a/lnbits/extensions/boltcards/crud.py +++ b/lnbits/extensions/boltcards/crud.py @@ -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), - ) \ No newline at end of file + ) + +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 diff --git a/lnbits/extensions/boltcards/migrations.py b/lnbits/extensions/boltcards/migrations.py index eedbb5d34..e7236ce7a 100644 --- a/lnbits/extensions/boltcards/migrations.py +++ b/lnbits/extensions/boltcards/migrations.py @@ -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 + + """ + ); + """ + ) diff --git a/lnbits/extensions/boltcards/models.py b/lnbits/extensions/boltcards/models.py index 6ef25d0c2..75621269f 100644 --- a/lnbits/extensions/boltcards/models.py +++ b/lnbits/extensions/boltcards/models.py @@ -18,4 +18,22 @@ class CreateCardData(BaseModel): counter: str = Query(...) withdraw: str = Query(...) file_key: str = Query(...) - meta_key: str = Query(...) \ No newline at end of file + 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(...) +''' diff --git a/lnbits/extensions/boltcards/templates/boltcards/index.html b/lnbits/extensions/boltcards/templates/boltcards/index.html index 4910cb66f..a6997a5d1 100644 --- a/lnbits/extensions/boltcards/templates/boltcards/index.html +++ b/lnbits/extensions/boltcards/templates/boltcards/index.html @@ -368,6 +368,7 @@ if (this.g.user.wallets.length) { this.getCards() this.getWithdraws() + this.getHits() } } }) diff --git a/lnbits/extensions/boltcards/views_api.py b/lnbits/extensions/boltcards/views_api.py index 0acfb6858..75cfe129e 100644 --- a/lnbits/extensions/boltcards/views_api.py +++ b/lnbits/extensions/boltcards/views_api.py @@ -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)