From 031dbab6a652eb4bef6882dcdade4420137a4315 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 29 Feb 2024 00:14:34 +0300 Subject: [PATCH] dev@locazia: improve connect wallet --- app/bot/routers/content.py | 35 +++++++++++++++++++++++++++++++++++ app/bot/routers/index.py | 23 ++++++++++++++++------- app/bot/routers/tonconnect.py | 3 ++- 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 app/bot/routers/content.py diff --git a/app/bot/routers/content.py b/app/bot/routers/content.py new file mode 100644 index 0000000..d8d7d8a --- /dev/null +++ b/app/bot/routers/content.py @@ -0,0 +1,35 @@ + +from aiogram import types, Router, F +from aiogram.filters import Command +from app.core._utils.tg_process_template import tg_process_template +from app.core._keyboards import get_inline_keyboard +from app.core.logger import logger +from app.core.models.wallet_connection import WalletConnection +from app.core.models._blockchain.ton.connect import TonConnect, unpack_wallet_info +from app.core._config import WEB_APP_URLS + + +router = Router() + + +async def t_callback_owned_content(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): + message_text = user.translated("ownedContent_menu") + return await tg_process_template( + chat_wrap, message_text, + keyboard=get_inline_keyboard([ + [{ + 'text': user.translated('webApp_uploadContent_button'), + 'web_app': types.WebAppInfo( + url=WEB_APP_URLS['uploadContent'] + ) + }], + [{ + 'text': user.translated('back_button'), + 'callback_data': 'home' + }] + ]) + ) + + +router.callback_query.register(t_callback_owned_content, F.data == 'ownedContent') + diff --git a/app/bot/routers/index.py b/app/bot/routers/index.py index e11ba3b..3de9f73 100644 --- a/app/bot/routers/index.py +++ b/app/bot/routers/index.py @@ -6,6 +6,8 @@ from aiogram import types, Router, F from aiogram.filters import Command from app.bot.routers.tonconnect import router as tonconnect_router +from app.bot.routers.content import router as content_router + from app.core._utils.tg_process_template import tg_process_template from app.core._keyboards import get_inline_keyboard from app.core.logger import logger @@ -21,10 +23,12 @@ async def t_home_menu(__msg, **extra): if extra.get('state'): await extra['state'].clear() - if not db_session.query(WalletConnection).filter( + wallet_connection = db_session.query(WalletConnection).filter( WalletConnection.user_id == user.id, WalletConnection.invalidated == False - ).first(): + ).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() @@ -47,13 +51,17 @@ async def t_home_menu(__msg, **extra): ) return await tg_process_template( - chat_wrap, user.translated('home_menu'), message_id=__msg.message.message_id if isinstance(__msg, types.CallbackQuery) else None, + 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('webApp_uploadContent_button'), - 'web_app': types.WebAppInfo( - url=WEB_APP_URLS['uploadContent'] - ) + 'text': user.translated('ownedContent_button'), + 'callback_data': 'ownedContent' + }] + [{ + 'text': user.translated('disconnectWallet_button'), + 'callback_data': 'disconnectWallet' }] ]) ) @@ -63,6 +71,7 @@ main_router.message.register(t_home_menu, Command('start')) main_router.callback_query.register(t_home_menu, F.data == 'home') main_router.include_routers(tonconnect_router) +main_router.include_routers(content_router) closing_router = Router() diff --git a/app/bot/routers/tonconnect.py b/app/bot/routers/tonconnect.py index 925ecea..c7f9fe9 100644 --- a/app/bot/routers/tonconnect.py +++ b/app/bot/routers/tonconnect.py @@ -67,10 +67,11 @@ Use /dev_tonconnect {wallet_app_name} for connect to wallet.""" async def t_callback_init_tonconnect(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra): wallet_app_name = query.data.split("_")[1] - ton_connect, ton_connection = TonConnect.by_user(db_session, user, callback_fn=()) + ton_connect, ton_connection = TonConnect.by_user(db_session, user) await ton_connect.restore_connection() connection_link = await ton_connect.new_connection(wallet_app_name) ton_connect.connected + memory.add_task(pause_ton_connection, ton_connect, delay_s=60 * 3) make_log("TonConnect_Init", f"New connection link for {wallet_app_name}: {connection_link}", level='debug') message_text = user.translated("tonconnectInit_menu") return await tg_process_template(