diff --git a/app/bot/routers/content.py b/app/bot/routers/content.py index d8d7d8a..336c882 100644 --- a/app/bot/routers/content.py +++ b/app/bot/routers/content.py @@ -27,7 +27,7 @@ async def t_callback_owned_content(query: types.CallbackQuery, memory=None, user 'text': user.translated('back_button'), 'callback_data': 'home' }] - ]) + ]), message_id=query.message.message_id ) diff --git a/app/bot/routers/index.py b/app/bot/routers/index.py index 4b250f7..3aa7c07 100644 --- a/app/bot/routers/index.py +++ b/app/bot/routers/index.py @@ -18,6 +18,46 @@ from app.core._config import WEB_APP_URLS main_router = Router() +async def send_home_menu(chat_wrap, user, wallet_connection): + return await tg_process_template( + chat_wrap, user.translated('home_menu').format( + wallet_address=wallet_connection.wallet_address + ), keyboard=get_inline_keyboard([ + [{ + 'text': user.translated('ownedContent_button'), + 'callback_data': 'ownedContent' + }], + [{ + 'text': user.translated('disconnectWallet_button'), + 'callback_data': 'disconnectWallet' + }] + ]) + ) + + +async def send_connect_wallets_list(db_session, chat_wrap, user): + ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) + 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) + ]) + ) + + 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'): @@ -29,42 +69,9 @@ async def t_home_menu(__msg, **extra): ).first() if not wallet_connection: - ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) - 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) - ]) - ) + return await send_connect_wallets_list(db_session, chat_wrap, user) - return await tg_process_template( - chat_wrap, user.translated('home_menu').format( - wallet_address=wallet_connection.wallet_address - ), message_id=__msg.message.message_id if isinstance(__msg, types.CallbackQuery) else None, - keyboard=get_inline_keyboard([ - [{ - 'text': user.translated('ownedContent_button'), - 'callback_data': 'ownedContent' - }], - [{ - 'text': user.translated('disconnectWallet_button'), - 'callback_data': 'disconnectWallet' - }] - ]) - ) + return await send_home_menu(chat_wrap, user, wallet_connection) main_router.message.register(t_home_menu, Command('start')) diff --git a/app/bot/routers/tonconnect.py b/app/bot/routers/tonconnect.py index c7f9fe9..e0a1f4a 100644 --- a/app/bot/routers/tonconnect.py +++ b/app/bot/routers/tonconnect.py @@ -7,6 +7,8 @@ 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._blockchain.ton.connect import TonConnect, unpack_wallet_info +from app.core.models.wallet_connection import WalletConnection +from app.bot.routers.index import send_connect_wallets_list router = Router() @@ -83,9 +85,21 @@ async def t_callback_init_tonconnect(query: types.CallbackQuery, memory=None, us 'url': connection_link } ] - ]) + ]), message_id=query.message.message_id ) +async def t_callback_disconnect_wallet(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): + wallet_connections = db_session.query(WalletConnection).filter( + WalletConnection.user_id == user.id, + WalletConnection.invalidated == False + ).all() + for wallet_connection in wallet_connections: + wallet_connection.invalidated = True + + db_session.commit() + return await send_connect_wallets_list(db_session, chat_wrap, user) + router.message.register(t_tonconnect_dev_menu, Command('dev_tonconnect')) router.callback_query.register(t_callback_init_tonconnect, F.data.startswith('initTonconnect_')) +router.callback_query.register(t_callback_disconnect_wallet, F.data == 'disconnectWallet')