import os from aiogram import F, Router from aiogram.fsm.context import FSMContext from aiogram.types import CallbackQuery, InlineKeyboardButton, Message from aiogram.utils.keyboard import InlineKeyboardBuilder from config import ( BALANCE_BUTTON, GIFT_BUTTON, INSTRUCTIONS_BUTTON, NEWS_MESSAGE, REFERRAL_BUTTON, SHOW_START_MENU_ONCE, TRIAL_TIME_DISABLE, ) from core.bootstrap import BUTTONS_CONFIG, MODES_CONFIG from core.cache_config import BALANCE_CACHE_TTL_SEC, KEY_COUNT_CACHE_TTL_SEC, PROFILE_DATA_CACHE_TTL_SEC from core.redis_cache import cache_get, cache_key, cache_set from database import get_balance_trial_key_count from handlers.buttons import ( ABOUT_VPN, ADD_SUB, BACK, BALANCE, GIFTS, INSTRUCTIONS, INVITE, MY_SUB, MY_SUBS, TRIAL_SUB, ADMIN_BTN, ) from handlers.payments.currency_rates import format_for_user from handlers.texts import ADD_SUBSCRIPTION_HINT from hooks.hook_buttons import insert_hook_buttons from hooks.hooks import run_hooks from middlewares.session import release_session_early from .admin.panel.keyboard import AdminPanelCallback from .texts import profile_message_send from .utils import edit_or_send_message, get_username router = Router() @router.callback_query(F.data == "profile") @router.message(F.text == "/profile") async def process_callback_view_profile( callback_query_or_message: Message | CallbackQuery, state: FSMContext, admin: bool, session, ): if isinstance(callback_query_or_message, CallbackQuery): chat = callback_query_or_message.message.chat user = callback_query_or_message.from_user message = callback_query_or_message.message else: chat = callback_query_or_message.chat user = callback_query_or_message.from_user message = callback_query_or_message chat_id = chat.id username = get_username(user or chat) cached = await cache_get(cache_key("profile_data", chat_id)) if isinstance(cached, dict) and "key_count" in cached and "balance_rub" in cached and "trial_status" in cached: key_count = int(cached["key_count"]) balance_rub = float(cached.get("balance_rub") or 0) trial_status = int(cached.get("trial_status") or 0) else: balance_rub, trial_status, key_count = await get_balance_trial_key_count(session, chat_id) balance_rub = balance_rub or 0 await cache_set(cache_key("balance", chat_id), balance_rub, BALANCE_CACHE_TTL_SEC) await cache_set(cache_key("key_count", chat_id), key_count, KEY_COUNT_CACHE_TTL_SEC) await cache_set( cache_key("profile_data", chat_id), {"key_count": key_count, "balance_rub": balance_rub, "trial_status": trial_status}, PROFILE_DATA_CACHE_TTL_SEC, ) balance_text = await format_for_user( session, chat_id, balance_rub, getattr(user, "language_code", None), ) profile_menu_buttons = await run_hooks("profile_menu", chat_id=chat_id, admin=admin, session=session) text_hooks = await run_hooks( "profile_text", username=username, chat_id=chat_id, balance=int(balance_rub), key_count=key_count, session=session, ) profile_message = profile_message_send(username, chat_id, balance_text, key_count) if key_count == 0: profile_message += ADD_SUBSCRIPTION_HINT else: profile_message += f"\n
{NEWS_MESSAGE}
" if text_hooks: profile_message = text_hooks[0] builder = InlineKeyboardBuilder() trial_time_disabled = bool(MODES_CONFIG.get("TRIAL_TIME_DISABLED", TRIAL_TIME_DISABLE)) if key_count > 0: subscriptions_button_text = MY_SUB if key_count == 1 else MY_SUBS builder.row(InlineKeyboardButton(text=subscriptions_button_text, callback_data="view_keys")) elif trial_status == 0 and not trial_time_disabled: builder.row(InlineKeyboardButton(text=TRIAL_SUB, callback_data="create_key")) else: builder.row(InlineKeyboardButton(text=ADD_SUB, callback_data="create_key")) if BUTTONS_CONFIG.get("BALANCE_BUTTON_ENABLE", BALANCE_BUTTON): builder.row(InlineKeyboardButton(text=BALANCE, callback_data="balance")) extra_buttons = [] if BUTTONS_CONFIG.get("REFERRAL_BUTTON_ENABLE", REFERRAL_BUTTON): extra_buttons.append(InlineKeyboardButton(text=INVITE, callback_data="invite")) if BUTTONS_CONFIG.get("GIFT_BUTTON_ENABLE", GIFT_BUTTON): extra_buttons.append(InlineKeyboardButton(text=GIFTS, callback_data="gifts")) if extra_buttons: builder.row(*extra_buttons) builder = insert_hook_buttons(builder, profile_menu_buttons) if BUTTONS_CONFIG.get("INSTRUCTIONS_BUTTON_ENABLE", INSTRUCTIONS_BUTTON): builder.row(InlineKeyboardButton(text=INSTRUCTIONS, callback_data="instructions")) if admin: builder.row( InlineKeyboardButton( text=ADMIN_BTN, callback_data=AdminPanelCallback(action="admin").pack(), ) ) show_start_menu_once = bool(MODES_CONFIG.get("SHOW_START_MENU_ONLY_ONCE", SHOW_START_MENU_ONCE)) if show_start_menu_once: builder.row(InlineKeyboardButton(text=ABOUT_VPN, callback_data="about_vpn")) else: builder.row(InlineKeyboardButton(text=BACK, callback_data="start")) await release_session_early(session) await edit_or_send_message( target_message=message, text=profile_message, reply_markup=builder.as_markup(), media_path=os.path.join("img", "profile.jpg"), disable_web_page_preview=False, force_text=True, )