From e9beed9f31f7974843c8dbf489cb8ab5c9dec3ff Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Sat, 10 Oct 2026 16:02:26 +0000
Subject: [PATCH] fix: формат /labmem; feat: /worldline считается из метрик чата (сообщения/участники/наказания) с полями притяжения

---
 db.py |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/db.py b/db.py
index 058397d..0a83bd8 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, "
@@ -211,6 +212,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 +232,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()

--
Gitblit v1.9.3