diff --git a/app/client_bot/routers/stars.py b/app/client_bot/routers/stars.py index fecc175..39d74b8 100644 --- a/app/client_bot/routers/stars.py +++ b/app/client_bot/routers/stars.py @@ -1,4 +1,6 @@ from aiogram import types, Router, F +from sqlalchemy import select + from app.core.logger import make_log from app.core.models import StarsInvoice @@ -12,9 +14,10 @@ async def t_pre_checkout_query_stars_processing(pre_checkout_query: types.PreChe invoice_id = pre_checkout_query.invoice_payload - existing_invoice = db_session.query(StarsInvoice).filter( - StarsInvoice.external_id == invoice_id - ).first() + result = await db_session.execute( + select(StarsInvoice).where(StarsInvoice.external_id == invoice_id) + ) + existing_invoice = result.scalars().first() if not existing_invoice: return await pre_checkout_query.answer(ok=False, error_message="Invoice not found") diff --git a/app/core/models/content/indexation_mixins.py b/app/core/models/content/indexation_mixins.py index 072f027..14d0591 100644 --- a/app/core/models/content/indexation_mixins.py +++ b/app/core/models/content/indexation_mixins.py @@ -59,7 +59,8 @@ class UserContentIndexationMixin: cc_indexator_data = unpack_item_indexator_data(cc_indexator_result) assert cc_indexator_data['type'] == 1, "Type is not a content" assert cc_indexator_data['address'] == self.onchain_address, "Address is not equal" - if cc_indexator_data['index'] < MIN_ONCHAIN_INDEX: + license_type = cc_indexator_data.get('license_type') + if cc_indexator_data['index'] < MIN_ONCHAIN_INDEX and (license_type is None or license_type == 0): make_log( "UserContent", f"Skip license {self.onchain_address} with index {cc_indexator_data['index']} < MIN_ONCHAIN_INDEX={MIN_ONCHAIN_INDEX}", @@ -90,6 +91,7 @@ class UserContentIndexationMixin: self.owner_address = cc_indexator_data['owner_address'] self.type = 'nft/listen' self.content_id = stored_content.id + self.meta = {**(self.meta or {}), 'license_type': license_type} await db_session.commit() except BaseException as e: errored = True diff --git a/app/core/models/user/wallet_mixin.py b/app/core/models/user/wallet_mixin.py index b577587..3337e19 100644 --- a/app/core/models/user/wallet_mixin.py +++ b/app/core/models/user/wallet_mixin.py @@ -57,23 +57,27 @@ class WalletMixin: collection_address = collection_address or nft_item.get('collection_address') if collection_address: try: - normalized_collection = Address(collection_address).to_string(1, 1, 1) + collection_address = Address(collection_address).to_string(1, 1, 1) except Exception: - normalized_collection = collection_address - if normalized_collection != platform_address: - make_log(self, f"Skip foreign NFT {item_address} from collection {normalized_collection}", level='debug') - continue + pass item_index = None + license_type = None # Prefer index from tonapi payload if available raw_index = nft_item.get('index') if isinstance(nft_item, dict) else None if isinstance(raw_index, int): item_index = raw_index - if item_index is None: + + need_chain_probe = item_index is None or item_index < MIN_ONCHAIN_INDEX + platform_address_onchain = None + if need_chain_probe: try: indexator_raw = await toncenter.run_get_method(item_address, 'indexator_data') if indexator_raw.get('exit_code', -1) == 0: - item_index = unpack_item_indexator_data(indexator_raw)['index'] + indexator_data = unpack_item_indexator_data(indexator_raw) + item_index = indexator_data['index'] + license_type = indexator_data.get('license_type') + platform_address_onchain = indexator_data.get('platform_address') except BaseException as err: make_log(self, f"Failed to fetch indexator data for {item_address}: {err}", level='warning') @@ -81,13 +85,32 @@ class WalletMixin: make_log(self, f"Skip NFT {item_address}: unable to resolve on-chain index", level='warning') continue - if item_index is not None and item_index < MIN_ONCHAIN_INDEX: - make_log(self, f"Ignore NFT {item_address} with index {item_index} < MIN_ONCHAIN_INDEX={MIN_ONCHAIN_INDEX}", level='debug') + if platform_address_onchain and platform_address_onchain != platform_address: + make_log( + self, + f"Skip foreign NFT {item_address}: platform mismatch {platform_address_onchain} != {platform_address}", + level='debug' + ) + continue + + if item_index < MIN_ONCHAIN_INDEX and (license_type is None or license_type == 0): + make_log( + self, + f"Ignore NFT {item_address} with index {item_index} < MIN_ONCHAIN_INDEX={MIN_ONCHAIN_INDEX} (license_type={license_type})", + level='debug' + ) continue from sqlalchemy import select user_content = (await db_session.execute(select(UserContent).where(UserContent.onchain_address == item_address))).scalars().first() if user_content: + if license_type is not None and license_type != 0 and user_content.type == 'nft/ignored': + user_content.type = 'nft/unknown' + user_content.meta = {**(user_content.meta or {}), 'license_type': license_type} + user_content.owner_address = owner_address + user_content.status = 'active' + user_content.updated = datetime.fromtimestamp(0) + await db_session.commit() continue user_content = UserContent( @@ -99,7 +122,7 @@ class WalletMixin: updated=datetime.fromtimestamp(0), content_id=None, # not resolved yet created=datetime.now(), - meta={}, + meta={'license_type': license_type} if license_type is not None else {}, user_id=self.id, wallet_connection_id=(await self.wallet_connection_async(db_session)).id, status="active"