try fix stars

This commit is contained in:
user 2025-02-26 20:12:25 +03:00
parent 1dd19f3112
commit 7e39d8db61
2 changed files with 40 additions and 25 deletions

View File

@ -1,5 +1,7 @@
from datetime import datetime, timedelta
from sanic import response from sanic import response
from aiogram import Bot, types from aiogram import Bot, types
from sqlalchemy import and_
from app.core.logger import make_log from app.core.logger import make_log
from app.core.models.node_storage import StoredContent from app.core.models.node_storage import StoredContent
from app.core.models.keys import KnownKey from app.core.models.keys import KnownKey
@ -58,22 +60,19 @@ async def s_api_v1_content_view(request, content_address: str):
) )
if not have_access: if not have_access:
stars_cost = 1 stars_cost = 1
exist_invoice = request.ctx.db_session.query(StarsInvoice).filter(
and_(
StarsInvoice.user_id == request.ctx.user.id,
StarsInvoice.created > datetime.now() - timedelta(minutes=25),
)
).first()
if exist_invoice:
invoice_url = exist_invoice.invoice_url
else:
invoice_id = f"access_{uuid.uuid4().hex}" invoice_id = f"access_{uuid.uuid4().hex}"
request.ctx.db_session.add(
StarsInvoice(
external_id=invoice_id,
type='access',
amount=stars_cost,
content_hash=content['encrypted_content'].hash,
)
)
request.ctx.db_session.commit()
try: try:
opts['invoice'] = { invoice_url = await Bot(token=CLIENT_TELEGRAM_API_KEY).create_invoice_link(
'url': (
await Bot(token=CLIENT_TELEGRAM_API_KEY).create_invoice_link(
'Lifetime access to content', 'Lifetime access to content',
'You will receive NFT with lifetime access to content', 'You will receive NFT with lifetime access to content',
f"access_{invoice_id}", "", "XTR", f"access_{invoice_id}", "", "XTR",
@ -81,12 +80,25 @@ async def s_api_v1_content_view(request, content_address: str):
types.LabeledPrice(label='Lifetime access', amount=stars_cost), types.LabeledPrice(label='Lifetime access', amount=stars_cost),
], ],
) )
), request.ctx.db_session.add(
'amount': stars_cost, StarsInvoice(
} external_id=invoice_id,
type='access',
amount=stars_cost,
user_id=request.ctx.user.id,
content_hash=content['encrypted_content'].hash,
invoice_url=invoice_url
)
)
request.ctx.db_session.commit()
except BaseException as e: except BaseException as e:
make_log("Content", f"Can't create invoice link: {e}", level='warning') make_log("Content", f"Can't create invoice link: {e}", level='warning')
opts['invoice'] = {
'url': invoice_url,
'amount': stars_cost,
}
display_options = { display_options = {
'content_url': content['decrypted_content'].web_url + ( 'content_url': content['decrypted_content'].web_url + (
'?seconds_limit=30' if not have_access else '' '?seconds_limit=30' if not have_access else ''

View File

@ -47,6 +47,9 @@ class StarsInvoice(AlchemyBase):
type = Column(String(256), nullable=False) type = Column(String(256), nullable=False)
amount = Column(Integer, nullable=False) amount = Column(Integer, nullable=False)
user_id = Column(Integer, ForeignKey('users.id'), nullable=True)
content_hash = Column(String(256), nullable=True) content_hash = Column(String(256), nullable=True)
invoice_url = Column(String(256), nullable=True)
created = Column(DateTime, nullable=False, default=datetime.utcnow) created = Column(DateTime, nullable=False, default=datetime.utcnow)