98 lines
3.9 KiB
Python
98 lines
3.9 KiB
Python
from aiogram import types, Router, F
|
|
from aiogram.filters import Command
|
|
from tonsdk.utils import Address
|
|
|
|
from app.core._blockchain.ton.connect import TonConnect
|
|
from sqlalchemy import select, and_
|
|
from app.core._keyboards import get_inline_keyboard
|
|
from app.core._utils.tg_process_template import tg_process_template
|
|
from app.core.logger import make_log
|
|
from app.core.models.wallet_connection import WalletConnection
|
|
from app.core.models.node_storage import StoredContent
|
|
|
|
main_router = Router()
|
|
|
|
|
|
async def send_home_menu(chat_wrap, user, wallet_connection, **kwargs):
|
|
return await tg_process_template(
|
|
chat_wrap, user.translated('clientHome_menu').format(
|
|
wallet_address=Address(wallet_connection.wallet_address).to_string(1, 1, 1) if wallet_connection else user.translated('p_walletNotConnected'),
|
|
name=user.front_format(plain_text=True),
|
|
), # keyboard=get_inline_keyboard([
|
|
# [{
|
|
# 'text': user.translated('ownedContent_button'),
|
|
# 'callback_data': 'ownedContent'
|
|
# }],
|
|
# [{
|
|
# 'text': user.translated('disconnectWallet_button'),
|
|
# 'callback_data': 'disconnectWallet'
|
|
# }] if wallet_connection else []
|
|
# ]),
|
|
**kwargs
|
|
)
|
|
|
|
|
|
async def send_connect_wallets_list(db_session, chat_wrap, user, **kwargs):
|
|
result = await db_session.execute(select(WalletConnection).where(
|
|
and_(WalletConnection.user_id == user.id, WalletConnection.invalidated == False, WalletConnection.network == 'ton')
|
|
).order_by(WalletConnection.created.desc()))
|
|
ton_connection = result.scalars().first()
|
|
ton_connect = TonConnect.by_key(ton_connection.keys["connection_key"]) if ton_connection else TonConnect()
|
|
if ton_connection:
|
|
await ton_connect.restore_connection()
|
|
wallets = ton_connect._sdk_client.get_wallets()
|
|
message_text = user.translated("connectWalletsList_menu")
|
|
return await tg_process_template(
|
|
chat_wrap, message_text,
|
|
keyboard=get_inline_keyboard([
|
|
[
|
|
{
|
|
'text': f"{wallets[i]['name']}",
|
|
'callback_data': f"initTonconnect_{wallets[i]['app_name']}"
|
|
} if i < len(wallets) else None,
|
|
{
|
|
'text': f"{wallets[i + 1]['name']}",
|
|
'callback_data': f"initTonconnect_{wallets[i + 1]['app_name']}"
|
|
} if i + 1 < len(wallets) else None,
|
|
]
|
|
for i in range(0, len(wallets), 2)
|
|
]), **kwargs
|
|
)
|
|
|
|
|
|
async def t_home_menu(__msg, **extra):
|
|
memory, user, db_session, chat_wrap = extra['memory'], extra['user'], extra['db_session'], extra['chat_wrap']
|
|
if extra.get('state'):
|
|
await extra['state'].clear()
|
|
|
|
if isinstance(__msg, types.CallbackQuery):
|
|
message_id = __msg.message.message_id
|
|
# elif isinstance(__msg, types.Message):
|
|
# message_id = __msg.message_id
|
|
else:
|
|
message_id = None
|
|
|
|
wallet_connection = (await db_session.execute(select(WalletConnection).where(
|
|
and_(WalletConnection.user_id == user.id, WalletConnection.invalidated == False)
|
|
))).scalars().first()
|
|
|
|
# if not wallet_connection:
|
|
# return await send_connect_wallets_list(db_session, chat_wrap, user, message_id=message_id)
|
|
|
|
args = []
|
|
if isinstance(__msg, types.Message):
|
|
args = __msg.text.split(' ')[1:]
|
|
|
|
make_log("Home", f"Home menu args: {args}", level='debug')
|
|
if args:
|
|
if args[0].startswith('C'):
|
|
content = StoredContent.from_cid(db_session, args[0][1:])
|
|
return await chat_wrap.send_content(db_session, content, message_id=message_id)
|
|
|
|
return await send_home_menu(chat_wrap, user, wallet_connection, message_id=message_id)
|
|
|
|
|
|
main_router.message.register(t_home_menu, Command('start'))
|
|
main_router.callback_query.register(t_home_menu, F.data == 'home')
|
|
router = main_router
|