improve work
This commit is contained in:
1 parent
64491e19e1
commit
3511ebd247
23 files changed
+1595
-310
No files matched your search
@@ -9,6 +9,10 @@ from app.core.translation import TranslationCore
|
||||
from ..base import AlchemyBase
|
||||
|
||||
|
||||
_BASE62_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
_BASE62 = len(_BASE62_ALPHABET)
|
||||
|
||||
|
||||
class User(AlchemyBase, DisplayMixin, TranslationCore, AuthenticationMixin_V1, WalletMixin):
|
||||
LOCALE_DOMAIN = 'sanic_telegram_bot'
|
||||
|
||||
@@ -32,3 +36,26 @@ class User(AlchemyBase, DisplayMixin, TranslationCore, AuthenticationMixin_V1, W
|
||||
def __str__(self):
|
||||
return f"User, {self.id}_{self.telegram_id} | Username: {self.username} " + '\\'
|
||||
|
||||
def ensure_ref_id(self):
|
||||
"""Return a short referral identifier, generating it if missing."""
|
||||
meta = self.meta or {}
|
||||
ref_id = meta.get('ref_id')
|
||||
if isinstance(ref_id, str) and ref_id:
|
||||
return ref_id
|
||||
|
||||
ref_id = self._generate_ref_id()
|
||||
self.meta = {**meta, 'ref_id': ref_id}
|
||||
return ref_id
|
||||
|
||||
def _generate_ref_id(self):
|
||||
user_id = int(self.id or 0)
|
||||
if user_id <= 0:
|
||||
return '000'
|
||||
|
||||
value = user_id % (_BASE62 ** 3)
|
||||
chars = []
|
||||
for _ in range(3):
|
||||
chars.append(_BASE62_ALPHABET[value % _BASE62])
|
||||
value //= _BASE62
|
||||
|
||||
return ''.join(reversed(chars)) or '000'
|
||||
@@ -7,6 +7,11 @@ from tonsdk.utils import Address
|
||||
from datetime import datetime, timedelta
|
||||
from app.core.logger import make_log
|
||||
from httpx import AsyncClient
|
||||
from app.core.models.content.indexation_mixins import unpack_item_indexator_data, MIN_ONCHAIN_INDEX
|
||||
|
||||
def _platform_address_str() -> str:
|
||||
from app.core._blockchain.ton.platform import platform
|
||||
return platform.address.to_string(1, 1, 1)
|
||||
|
||||
|
||||
class WalletMixin:
|
||||
@@ -43,6 +48,43 @@ class WalletMixin:
|
||||
item_address = Address(nft_item['address']).to_string(1, 1, 1)
|
||||
owner_address = Address(nft_item['owner']['address']).to_string(1, 1, 1)
|
||||
|
||||
platform_address = _platform_address_str()
|
||||
collection_address = None
|
||||
if isinstance(nft_item, dict):
|
||||
collection_data = nft_item.get('collection')
|
||||
if isinstance(collection_data, dict):
|
||||
collection_address = collection_data.get('address')
|
||||
collection_address = collection_address or nft_item.get('collection_address')
|
||||
if collection_address:
|
||||
try:
|
||||
normalized_collection = 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
|
||||
|
||||
item_index = 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:
|
||||
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']
|
||||
except BaseException as err:
|
||||
make_log(self, f"Failed to fetch indexator data for {item_address}: {err}", level='warning')
|
||||
|
||||
if item_index is None:
|
||||
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')
|
||||
continue
|
||||
|
||||
from sqlalchemy import select
|
||||
user_content = (await db_session.execute(select(UserContent).where(UserContent.onchain_address == item_address))).scalars().first()
|
||||
if user_content:
|
||||
@@ -83,6 +125,33 @@ class WalletMixin:
|
||||
item_address = Address(nft_item['address']).to_string(1, 1, 1)
|
||||
owner_address = Address(nft_item['owner_address']).to_string(1, 1, 1)
|
||||
|
||||
platform_address = _platform_address_str()
|
||||
collection_address = nft_item.get('collection_address') if isinstance(nft_item, dict) else None
|
||||
if collection_address:
|
||||
try:
|
||||
normalized_collection = 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
|
||||
|
||||
item_index = None
|
||||
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']
|
||||
except BaseException as err:
|
||||
make_log(self, f"Failed to fetch indexator data for {item_address}: {err}", level='warning')
|
||||
|
||||
if item_index is None:
|
||||
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')
|
||||
continue
|
||||
|
||||
from sqlalchemy import select
|
||||
user_content = (await db_session.execute(select(UserContent).where(UserContent.onchain_address == item_address))).scalars().first()
|
||||
if user_content:
|
||||
|
||||
Reference in new issue
Block a user