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

---
 db.py |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 53 insertions(+), 7 deletions(-)

diff --git a/db.py b/db.py
index 3051b38..8f66bb2 100644
--- a/db.py
+++ b/db.py
@@ -160,7 +160,12 @@
     "name TEXT, "
     "text TEXT, "
     "created_at INTEGER, "
-    "tg_msg_id INTEGER DEFAULT 0)",
+    "tg_msg_id INTEGER DEFAULT 0, "
+    "media_type TEXT DEFAULT '', "
+    "media_file_id TEXT DEFAULT '', "
+    "reply_to_tg INTEGER DEFAULT 0, "
+    "reply_to_name TEXT DEFAULT '', "
+    "reply_to_text TEXT DEFAULT '')",
 )
 
 
@@ -222,8 +227,18 @@
             await db.execute("ALTER TABLE reports ADD COLUMN target_text TEXT DEFAULT ''")
     if await tbl_exists(db, "chats") and not await col_exists(db, "chats", "msgs"):
         await db.execute("ALTER TABLE chats ADD COLUMN msgs INTEGER DEFAULT 0")
-    if await tbl_exists(db, "messages") and not await col_exists(db, "messages", "tg_msg_id"):
-        await db.execute("ALTER TABLE messages ADD COLUMN tg_msg_id INTEGER DEFAULT 0")
+    if await tbl_exists(db, "messages"):
+        msg_cols = {
+            "tg_msg_id": "INTEGER DEFAULT 0",
+            "media_type": "TEXT DEFAULT ''",
+            "media_file_id": "TEXT DEFAULT ''",
+            "reply_to_tg": "INTEGER DEFAULT 0",
+            "reply_to_name": "TEXT DEFAULT ''",
+            "reply_to_text": "TEXT DEFAULT ''",
+        }
+        for col, ddl in msg_cols.items():
+            if not await col_exists(db, "messages", col):
+                await db.execute(f"ALTER TABLE messages ADD COLUMN {col} {ddl}")
 
 
 async def init_db() -> None:
@@ -825,13 +840,27 @@
 
 # --- Последние сообщения чата ---
 
-async def add_msg(chat_id: int, user_id: int, name: str, text: str, ts: int, tg_msg_id: int = 0, keep: int = 200) -> int:
+async def add_msg(
+    chat_id: int,
+    user_id: int,
+    name: str,
+    text: str,
+    ts: int,
+    tg_msg_id: int = 0,
+    media_type: str = "",
+    media_file_id: str = "",
+    reply_to_tg: int = 0,
+    reply_to_name: str = "",
+    reply_to_text: str = "",
+    keep: int = 200,
+) -> int:
     # Храним последние keep сообщений на чат.
     async with db_conn() as db:
         cur = await db.execute(
-            "INSERT INTO messages (chat_id, user_id, name, text, created_at, tg_msg_id) "
-            "VALUES (?, ?, ?, ?, ?, ?)",
-            (chat_id, user_id, name, text, ts, tg_msg_id),
+            "INSERT INTO messages "
+            "(chat_id, user_id, name, text, created_at, tg_msg_id, media_type, media_file_id, reply_to_tg, reply_to_name, reply_to_text) "
+            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+            (chat_id, user_id, name, text, ts, tg_msg_id, media_type, media_file_id, reply_to_tg, reply_to_name, reply_to_text),
         )
         await db.execute(
             "DELETE FROM messages WHERE chat_id=? AND id NOT IN "
@@ -842,6 +871,23 @@
         return cur.lastrowid
 
 
+async def get_msg(msg_id: int) -> Optional[dict]:
+    async with db_conn() as db:
+        async with db.execute("SELECT * FROM messages WHERE id=?", (msg_id,)) as cur:
+            row = await cur.fetchone()
+    return dict(row) if row else None
+
+
+async def find_msg_by_tg(chat_id: int, tg_msg_id: int) -> Optional[dict]:
+    async with db_conn() as db:
+        async with db.execute(
+            "SELECT * FROM messages WHERE chat_id=? AND tg_msg_id=? ORDER BY id DESC LIMIT 1",
+            (chat_id, tg_msg_id),
+        ) as cur:
+            row = await cur.fetchone()
+    return dict(row) if row else None
+
+
 async def list_msgs(chat_id: int, limit: int = 100) -> List[dict]:
     async with db_conn() as db:
         async with db.execute(

--
Gitblit v1.9.3