From d9c4c369924d1874e68b19ed93aa1aefa439b0cd Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Fri, 04 Dec 2026 08:26:53 +0000
Subject: [PATCH] Add /panel command with deep link to open Mini App with chat context

---
 web/views.py |   63 +++++++++++++++----------------
 1 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/web/views.py b/web/views.py
index 27853b6..dfc0e6c 100644
--- a/web/views.py
+++ b/web/views.py
@@ -63,7 +63,11 @@
 
 
 async def dashboard(request) -> web.Response:
-    user = await _user(request)
+    user = request["user"]
+    if user is None:
+        if request.headers.get("X-Telegram-Web-App-Platform"):
+            raise web.HTTPFound("/miniapp")
+        raise web.HTTPFound("/login")
     chats = await allowed_chats(user["user_id"])
     ids = {c["chat_id"] for c in chats}
     names = {c["chat_id"]: c["title"] for c in chats}
@@ -88,7 +92,11 @@
 
 
 async def chats_page(request) -> web.Response:
-    user = await _user(request)
+    user = request["user"]
+    if user is None:
+        if request.headers.get("X-Telegram-Web-App-Platform"):
+            raise web.HTTPFound("/miniapp")
+        raise web.HTTPFound("/login")
     chats = await allowed_chats(user["user_id"])
     cfgs = {c["chat_id"]: await db.get_chat_cfg(c["chat_id"]) for c in chats}
     alarms = {r["chat_id"]: r for r in await db.list_alarm_chats()}
@@ -96,42 +104,33 @@
 
 
 async def miniapp_page(request) -> web.Response:
-    """Выбирает стартовый экран Mini App по проверенному initData Telegram."""
-    user = await _user(request)
-    chats = await allowed_chats(user["user_id"])
-    available_ids = {c["chat_id"] for c in chats}
-    init_data = request.query.get("tgWebAppData", "")
-    context = None
-    if init_data:
-        from web.auth import verify_webapp_init_data
+    """Выбирает стартовый экран Mini App по chat_id из Telegram launch URL."""
+    if request["user"] is None:
+        raise web.HTTPFound("/miniapp/login")
 
-        verified = verify_webapp_init_data(init_data)
-        if verified:
-            try:
-                tg_user = json.loads(verified["user"])
-                if int(tg_user["id"]) == user["user_id"]:
-                    chat = json.loads(verified.get("chat", "{}"))
-                    candidate = int(chat.get("id", 0)) if chat else 0
-                    if not candidate and verified.get("chat_instance"):
-                        instance = verified["chat_instance"]
-                        candidate = int(instance) if instance.lstrip("-").isdigit() else 0
-                    if candidate in available_ids:
-                        context = candidate
-            except (KeyError, TypeError, ValueError):
-                pass
-    if context is None:
-        context = request.query.get("chat_id", "")
-        try:
-            context = int(context)
-        except (TypeError, ValueError):
-            context = 0
-        if context not in available_ids:
-            context = 0
+    user_id = request["user"]["user_id"]
+    chats = await allowed_chats(user_id)
+    available_ids = {c["chat_id"] for c in chats}
+    try:
+        context = int(request.query.get("chat_id", "") or 0)
+    except (TypeError, ValueError):
+        context = 0
+    if context not in available_ids:
+        context = 0
     if context:
         raise web.HTTPFound(f"/chats/{context}")
     raise web.HTTPFound("/chats")
 
 
+async def miniapp_login_page(request) -> web.Response:
+    """Стартовая HTML-страница: получает initData через Telegram WebApp JS API."""
+    if request["user"] is not None:
+        raise web.HTTPFound("/miniapp")
+    response = render(request, "miniapp_login.html")
+    response.headers["X-Amadeus-MiniApp-Version"] = "5387fa6"
+    return response
+
+
 async def chat_page(request) -> web.Response:
     user = await _user(request)
     chat_id = int(request.match_info["chat_id"])

--
Gitblit v1.9.3