From 9318fc842964d6de64b778cd5c617c6859ca56a2 Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Fri, 04 Dec 2026 07:41:15 +0000
Subject: [PATCH] Auto-authenticate Telegram Mini App

---
 track.py |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/track.py b/track.py
index ea757ce..d1b491a 100644
--- a/track.py
+++ b/track.py
@@ -5,15 +5,61 @@
 from util import now_ts
 
 
+def _media_of(event: types.Message):
+    # Тип медиа и file_id для отображения в панели.
+    if event.photo:
+        return "photo", event.photo[-1].file_id
+    if event.sticker:
+        st = event.sticker
+        return "sticker", (st.thumbnail.file_id if st.thumbnail else st.file_id)
+    if event.animation:
+        an = event.animation
+        return "animation", (an.thumbnail.file_id if an.thumbnail else an.file_id)
+    if event.video:
+        v = event.video
+        return "video", (v.thumbnail.file_id if v.thumbnail else "")
+    if event.document and (event.document.mime_type or "").startswith("image/"):
+        return "photo", event.document.file_id
+    return "", ""
+
+
 @dp.message.middleware()
 async def track_msg(handler, event: types.Message, data: dict):
-    # Фиксируем чаты, где присутствует бот, для веб-панели.
+    # Фиксируем чаты и пользователей, чтобы панель и /aperm могли их находить.
     chat = event.chat
+    ts = now_ts()
     if chat and chat.type in ("group", "supergroup", "channel"):
         try:
-            await db.upsert_chat(chat.id, chat.title, chat.type, now_ts())
+            await db.upsert_chat(chat.id, chat.title, chat.type, ts)
         except Exception as e:
             logger.warning("upsert_chat failed chat=%s: %s", chat.id, e)
+    u = event.from_user
+    if u is not None and not u.is_bot:
+        try:
+            await db.upsert_user(u.id, u.username, u.first_name, u.last_name, ts)
+        except Exception as e:
+            logger.warning("upsert_user failed user=%s: %s", u.id, e)
+    # Лог последних сообщений для панели.
+    if chat and chat.type in ("group", "supergroup") and u is not None and not u.is_bot:
+        content = event.text or event.caption or ""
+        media_type, file_id = _media_of(event)
+        if content or media_type:
+            reply_to_tg = 0
+            reply_to_name = ""
+            reply_to_text = ""
+            if event.reply_to_message:
+                rm = event.reply_to_message
+                reply_to_tg = rm.message_id
+                if rm.from_user:
+                    reply_to_name = rm.from_user.full_name
+                reply_to_text = (rm.text or rm.caption or "")[:80]
+            try:
+                await db.add_msg(
+                    chat.id, u.id, u.full_name, content[:1000], ts, event.message_id,
+                    media_type, file_id, reply_to_tg, reply_to_name, reply_to_text,
+                )
+            except Exception as e:
+                logger.warning("add_msg failed chat=%s: %s", chat.id, e)
     return await handler(event, data)
 
 

--
Gitblit v1.9.3