diff --git a/app/api/routes/content.py b/app/api/routes/content.py index 9c93737..302fef7 100644 --- a/app/api/routes/content.py +++ b/app/api/routes/content.py @@ -3,6 +3,7 @@ from aiogram import Bot, types from app.core.logger import make_log from app.core.models.node_storage import StoredContent from app.core.models.keys import KnownKey +from app.core.models import StarsInvoice from app.core.models.content.user_content import UserContent from app.core._config import CLIENT_TELEGRAM_API_KEY import json @@ -56,19 +57,35 @@ async def s_api_v1_content_view(request, content_address: str): content_id=content['encrypted_content'].id).first()) ) if not have_access: - try: - invoice_id = uuid.uuid4().hex - opts['invoice_url'] = ( - await Bot(token=CLIENT_TELEGRAM_API_KEY).create_invoice_link( - description='You will receive NFT with lifetime access to content', - payload=f"access_{invoice_id}", - title='Lifetime access to content', - currency='XTR', - prices=[ - types.LabeledPrice(label='Lifetime access', amount=1) - ] - ) + stars_cost = 1 + 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: + opts['invoice'] = { + 'url': ( + await Bot(token=CLIENT_TELEGRAM_API_KEY).create_invodeice_link( + description='You will receive NFT with lifetime access to content', + payload=f"access_{invoice_id}", + title='Lifetime access to content', + currency='XTR', + prices=[ + types.LabeledPrice(label='Lifetime access', amount=stars_cost), + ], + provider_token="", + ) + ), + 'amount': stars_cost, + } except BaseException as e: make_log("Content", f"Can't create invoice link: {e}", level='warning') diff --git a/app/core/models/__init__.py b/app/core/models/__init__.py index 97ce29f..6db8a02 100644 --- a/app/core/models/__init__.py +++ b/app/core/models/__init__.py @@ -2,7 +2,7 @@ from app.core.models.base import AlchemyBase from app.core.models.keys import KnownKey from app.core.models.memory import Memory from app.core.models.node_storage import StoredContent -from app.core.models.transaction import UserBalance, InternalTransaction +from app.core.models.transaction import UserBalance, InternalTransaction, StarsInvoice from app.core.models.user import User from app.core.models.wallet_connection import WalletConnection from app.core.models.messages import KnownTelegramMessage diff --git a/app/core/models/transaction.py b/app/core/models/transaction.py index a369504..03d8fbe 100644 --- a/app/core/models/transaction.py +++ b/app/core/models/transaction.py @@ -1,5 +1,6 @@ from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, Float from sqlalchemy.orm import relationship +from datetime import datetime from .base import AlchemyBase @@ -36,3 +37,16 @@ class InternalTransaction(AlchemyBase): user = relationship('User', uselist=False, back_populates='internal_transactions', foreign_keys=[user_id]) asset = relationship('Asset', uselist=False, foreign_keys=[asset_id]) spent_transaction = relationship('InternalTransaction', uselist=False, foreign_keys=[spent_transaction_id]) + +class StarsInvoice(AlchemyBase): + __tablename__ = 'stars_invoices' + + id = Column(Integer, autoincrement=True, primary_key=True) + external_id = Column(String(1024), nullable=False) + + type = Column(String(256), nullable=False) + amount = Column(Integer, nullable=False) + + content_hash = Column(String(256), nullable=True) + + created = Column(DateTime, nullable=False, default=datetime.utcnow)