From 1b1ad4af701ee0256d2ff592a3445157a40d085a Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Sun, 11 Oct 2026 15:39:37 +0000
Subject: [PATCH] feat web: реалтайм-обновление сообщений (polling), реплаи, отправка по Enter (Ctrl+Enter — новая строка), мобильная раскладка

---
 db.py |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/db.py b/db.py
index cf7c711..3051b38 100644
--- a/db.py
+++ b/db.py
@@ -159,7 +159,8 @@
     "user_id INTEGER, "
     "name TEXT, "
     "text TEXT, "
-    "created_at INTEGER)",
+    "created_at INTEGER, "
+    "tg_msg_id INTEGER DEFAULT 0)",
 )
 
 
@@ -221,6 +222,8 @@
             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")
 
 
 async def init_db() -> None:
@@ -822,12 +825,13 @@
 
 # --- Последние сообщения чата ---
 
-async def add_msg(chat_id: int, user_id: int, name: str, text: str, ts: int, keep: int = 200) -> None:
+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:
     # Храним последние keep сообщений на чат.
     async with db_conn() as db:
-        await db.execute(
-            "INSERT INTO messages (chat_id, user_id, name, text, created_at) VALUES (?, ?, ?, ?, ?)",
-            (chat_id, user_id, name, text, ts),
+        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),
         )
         await db.execute(
             "DELETE FROM messages WHERE chat_id=? AND id NOT IN "
@@ -835,6 +839,7 @@
             (chat_id, chat_id, keep),
         )
         await db.commit()
+        return cur.lastrowid
 
 
 async def list_msgs(chat_id: int, limit: int = 100) -> List[dict]:
@@ -844,3 +849,12 @@
         ) as cur:
             rows = [dict(r) for r in await cur.fetchall()]
     return rows[::-1]
+
+
+async def list_msgs_after(chat_id: int, after_id: int, limit: int = 100) -> List[dict]:
+    async with db_conn() as db:
+        async with db.execute(
+            "SELECT * FROM messages WHERE chat_id=? AND id > ? ORDER BY id ASC LIMIT ?",
+            (chat_id, after_id, limit),
+        ) as cur:
+            return [dict(r) for r in await cur.fetchall()]

--
Gitblit v1.9.3