From 25ab024b4c1677d411024b36323a06a18711e28a Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Sun, 11 Oct 2026 15:33:55 +0000
Subject: [PATCH] feat: просмотр последних сообщений чата в отдельном окне и отправка от имени Амадеуса с пометкой автора

---
 db.py |   45 +++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/db.py b/db.py
index 058397d..cf7c711 100644
--- a/db.py
+++ b/db.py
@@ -66,7 +66,8 @@
     "type TEXT, "
     "added_at INTEGER, "
     "updated_at INTEGER, "
-    "active INTEGER DEFAULT 1)",
+    "active INTEGER DEFAULT 1, "
+    "msgs INTEGER DEFAULT 0)",
     "CREATE TABLE IF NOT EXISTS usr_fails ("
     "user_id INTEGER, "
     "chat_id INTEGER, "
@@ -152,6 +153,13 @@
     "CREATE TABLE IF NOT EXISTS dm_users ("
     "user_id INTEGER PRIMARY KEY, "
     "ts INTEGER)",
+    "CREATE TABLE IF NOT EXISTS messages ("
+    "id INTEGER PRIMARY KEY AUTOINCREMENT, "
+    "chat_id INTEGER, "
+    "user_id INTEGER, "
+    "name TEXT, "
+    "text TEXT, "
+    "created_at INTEGER)",
 )
 
 
@@ -211,6 +219,8 @@
             await db.execute("ALTER TABLE reports ADD COLUMN target_msg_id INTEGER DEFAULT 0")
         if not await col_exists(db, "reports", "target_text"):
             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")
 
 
 async def init_db() -> None:
@@ -229,10 +239,11 @@
 async def upsert_chat(chat_id: int, title: Optional[str], ctype: Optional[str], ts: int) -> None:
     async with db_conn() as db:
         await db.execute(
-            "INSERT INTO chats (chat_id, title, type, added_at, updated_at, active) "
-            "VALUES (?, ?, ?, ?, ?, 1) "
+            "INSERT INTO chats (chat_id, title, type, added_at, updated_at, active, msgs) "
+            "VALUES (?, ?, ?, ?, ?, 1, 1) "
             "ON CONFLICT(chat_id) DO UPDATE SET "
-            "title=excluded.title, type=excluded.type, updated_at=excluded.updated_at, active=1",
+            "title=excluded.title, type=excluded.type, updated_at=excluded.updated_at, "
+            "active=1, msgs=msgs+1",
             (chat_id, title, ctype, ts, ts),
         )
         await db.commit()
@@ -807,3 +818,29 @@
     async with db_conn() as db:
         async with db.execute("SELECT 1 FROM dm_users WHERE user_id=?", (user_id,)) as cur:
             return await cur.fetchone() is not None
+
+
+# --- Последние сообщения чата ---
+
+async def add_msg(chat_id: int, user_id: int, name: str, text: str, ts: int, keep: int = 200) -> None:
+    # Храним последние 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),
+        )
+        await db.execute(
+            "DELETE FROM messages WHERE chat_id=? AND id NOT IN "
+            "(SELECT id FROM messages WHERE chat_id=? ORDER BY id DESC LIMIT ?)",
+            (chat_id, chat_id, keep),
+        )
+        await db.commit()
+
+
+async def list_msgs(chat_id: int, limit: int = 100) -> List[dict]:
+    async with db_conn() as db:
+        async with db.execute(
+            "SELECT * FROM messages WHERE chat_id=? ORDER BY id DESC LIMIT ?", (chat_id, limit)
+        ) as cur:
+            rows = [dict(r) for r in await cur.fetchall()]
+    return rows[::-1]

--
Gitblit v1.9.3