|
1 | 1 | import random |
2 | 2 | from datetime import datetime as dt, timedelta as td |
| 3 | +from io import BytesIO |
3 | 4 |
|
4 | 5 | from aiogram import Router, F |
5 | | -from aiogram.types import CallbackQuery, Message, InlineQuery, InlineQueryResultArticle, InputTextMessageContent |
| 6 | +from aiogram.types import ( |
| 7 | + BufferedInputFile, |
| 8 | + CallbackQuery, |
| 9 | + InlineQuery, |
| 10 | + InlineQueryResultArticle, |
| 11 | + InputTextMessageContent, |
| 12 | + Message, |
| 13 | +) |
6 | 14 | from aiogram.fsm.context import FSMContext |
7 | 15 | from sqlalchemy.ext.asyncio import AsyncSession |
8 | 16 | from aiogram.exceptions import TelegramBadRequest |
9 | 17 |
|
10 | 18 | from app.db.models import UserStatus |
| 19 | +from app.models.settings import ConfigFormat |
11 | 20 | from app.models.user import UserCreate, UserModify, UserStatusModify, CreateUserFromTemplate, ModifyUserByTemplate |
12 | 21 | from app.models.validators import UserValidator |
13 | 22 | from app.operation import OperatorType |
| 23 | +from app.operation.subscription import SubscriptionOperation |
14 | 24 | from app.operation.user import UserOperation |
15 | 25 | from app.operation.group import GroupOperation |
16 | 26 | from app.operation.user_template import UserTemplateOperation |
|
24 | 34 | from app.telegram.utils.shared import add_to_messages_to_delete, delete_messages |
25 | 35 |
|
26 | 36 | user_operations = UserOperation(OperatorType.TELEGRAM) |
| 37 | +subscription_operations = SubscriptionOperation(OperatorType.TELEGRAM) |
27 | 38 | group_operations = GroupOperation(OperatorType.TELEGRAM) |
28 | 39 | user_templates = UserTemplateOperation(OperatorType.TELEGRAM) |
29 | 40 |
|
@@ -568,6 +579,35 @@ async def get_user_by_sub(event: Message, db: AsyncSession, admin: AdminDetails) |
568 | 579 | await event.reply(Texts.user_details(user, groups), reply_markup=UserPanel(user).as_markup()) |
569 | 580 |
|
570 | 581 |
|
| 582 | +@router.callback_query(UserPanel.Callback.filter(UserPanelAction.v2ray_links == F.action)) |
| 583 | +async def get_v2ray_links( |
| 584 | + event: CallbackQuery, db: AsyncSession, admin: AdminDetails, callback_data: UserPanel.Callback |
| 585 | +): |
| 586 | + try: |
| 587 | + db_user = await user_operations.get_validated_user_by_id(db, callback_data.user_id, admin) |
| 588 | + user = await user_operations.validate_user(db_user) |
| 589 | + user_with_inbounds = await subscription_operations.validated_user(db_user) |
| 590 | + links, _ = await subscription_operations.fetch_config(user_with_inbounds, ConfigFormat.links) |
| 591 | + except ValueError as exc: |
| 592 | + return await event.answer(str(exc), show_alert=True) |
| 593 | + |
| 594 | + if not links or not links.strip(): |
| 595 | + return await event.answer(Texts.v2ray_links_unavailable, show_alert=True) |
| 596 | + |
| 597 | + max_message_length = 4085 # Telegram message limit (including formatting) |
| 598 | + if len(links) < max_message_length: |
| 599 | + await event.message.answer(Texts.client_user_details(user)) |
| 600 | + await event.message.answer(f"<pre>{links}</pre>") |
| 601 | + else: |
| 602 | + file = BytesIO(links.encode("utf-8")) |
| 603 | + await event.message.answer_document( |
| 604 | + BufferedInputFile(file.read(), f"{user.username}-v2ray-links.txt"), |
| 605 | + caption=Texts.client_user_details(user), |
| 606 | + ) |
| 607 | + |
| 608 | + await event.answer() |
| 609 | + |
| 610 | + |
571 | 611 | @router.message(F.text) |
572 | 612 | @router.callback_query(UserPanel.Callback.filter(UserPanelAction.show == F.action)) |
573 | 613 | async def get_user(event: Message | CallbackQuery, admin: AdminDetails, db: AsyncSession, **kwargs): |
|
0 commit comments