from aiogram import F from aiogram.types import Message, CallbackQuery from app import dp from auth import is_admin_msg from keyboards import system_kb from system_checks import security, disks from app import cfg from services.http_checks import get_url_checks, check_url import asyncio from services.queue import enqueue from services.updates import list_updates, apply_updates from state import UPDATES_CACHE from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton @dp.message(F.text == "💽 Disks") async def sd(msg: Message): if is_admin_msg(msg): await msg.answer(disks(), reply_markup=system_kb) @dp.message(F.text == "🔐 Security") async def sec(msg: Message): if is_admin_msg(msg): await msg.answer(security(), reply_markup=system_kb) @dp.message(F.text == "🌐 URLs") async def urls(msg: Message): if not is_admin_msg(msg): return checks = list(get_url_checks(cfg)) if not checks: await msg.answer("⚠️ Нет URL для проверки", reply_markup=system_kb) return await msg.answer("⏳ Проверяю URL…", reply_markup=system_kb) async def worker(): tasks = [asyncio.to_thread(check_url, url) for _, url in checks] results = await asyncio.gather(*tasks) lines = ["🌐 URLs\n"] for (alias, url), (ok, status, ms, err) in zip(checks, results): if ok: lines.append(f"🟢 {alias}: {status} ({ms}ms)") elif status is not None: lines.append(f"🔴 {alias}: {status} ({ms}ms)") else: reason = err or "error" lines.append(f"🔴 {alias}: {reason} ({ms}ms)") await msg.answer("\n".join(lines), reply_markup=system_kb) asyncio.create_task(worker()) @dp.message(F.text == "📦 Updates") async def updates_list(msg: Message): if not is_admin_msg(msg): return async def job(): title, lines = await list_updates() UPDATES_CACHE[msg.from_user.id] = { "title": title, "lines": lines, "page_size": 20, } await send_updates_page(msg, msg.from_user.id, 0, edit=False) pos = await enqueue("pkg-updates", job) await msg.answer(f"🕓 Updates queued (#{pos})", reply_markup=system_kb) @dp.message(F.text == "⬆️ Upgrade") async def updates_apply(msg: Message): if not is_admin_msg(msg): return async def job(): text = await apply_updates() await msg.answer(text, reply_markup=system_kb, parse_mode="Markdown") pos = await enqueue("pkg-upgrade", job) await msg.answer(f"🕓 Upgrade queued (#{pos})", reply_markup=system_kb) def _updates_kb(page: int, total_pages: int) -> InlineKeyboardMarkup: buttons = [] if total_pages > 1: row = [] if page > 0: row.append(InlineKeyboardButton(text="⬅️ Prev", callback_data=f"updpage:{page-1}")) if page < total_pages - 1: row.append(InlineKeyboardButton(text="Next ➡️", callback_data=f"updpage:{page+1}")) if row: buttons.append(row) return InlineKeyboardMarkup(inline_keyboard=buttons) async def send_updates_page(msg: Message, user_id: int, page: int, edit: bool): data = UPDATES_CACHE.get(user_id) if not data: await msg.answer("⚠️ Updates cache empty", reply_markup=system_kb) return lines = data["lines"] page_size = data["page_size"] total_pages = max(1, (len(lines) + page_size - 1) // page_size) page = max(0, min(page, total_pages - 1)) start = page * page_size end = start + page_size body = "\n".join(lines[start:end]) text = f"{data['title']} (page {page+1}/{total_pages})\n```{body}```" if edit: await msg.edit_text(text, reply_markup=_updates_kb(page, total_pages), parse_mode="Markdown") else: await msg.answer(text, reply_markup=_updates_kb(page, total_pages), parse_mode="Markdown") @dp.callback_query(F.data.startswith("updpage:")) async def updates_page(cb: CallbackQuery): if cb.from_user.id not in UPDATES_CACHE: await cb.answer("No cached updates") return try: page = int(cb.data.split(":", 1)[1]) except ValueError: await cb.answer("Bad page") return await cb.answer() await send_updates_page(cb.message, cb.from_user.id, page, edit=True)