feat(чат): категории жалоб и attention

This commit is contained in:
anor
2026-11-06 22:45:33 +10:00
parent 45102c3dde
commit 4f3d47926a
6 changed files with 100 additions and 11 deletions
+19 -6
View File
@@ -63,7 +63,10 @@ CREATE TABLE IF NOT EXISTS feedback (
username TEXT NOT NULL,
tab_id TEXT,
comment TEXT,
answer TEXT
answer TEXT,
category TEXT,
source TEXT,
status TEXT NOT NULL DEFAULT 'new'
);
CREATE INDEX IF NOT EXISTS idx_feedback_ts ON feedback(timestamp);
@@ -122,21 +125,27 @@ def ping():
conn.execute("SELECT 1").fetchone()
def save_feedback(username: str, tab_id: str, comment: str, answer: str):
def save_feedback(username: str, tab_id: str, comment: str, answer: str,
category: str | None = None, source: str | None = None):
"""Жалоба пользователя на ответ ассистента (кнопка «сообщить об ошибке»)."""
with _conn() as conn:
conn.execute(
"INSERT INTO feedback (timestamp, username, tab_id, comment, answer) "
"VALUES (?, ?, ?, ?, ?)",
(time.time(), username, tab_id, comment, answer),
"INSERT INTO feedback (timestamp, username, tab_id, comment, answer, category, source) "
"VALUES (?, ?, ?, ?, ?, ?, ?)",
(time.time(), username, tab_id, comment, answer, category, source),
)
def count_feedback_new() -> int:
with _conn() as conn:
return int(conn.execute("SELECT COUNT(*) AS n FROM feedback WHERE status = 'new'").fetchone()["n"])
def get_feedback(limit: int = 200) -> list[dict]:
"""Последние жалобы — для админ-просмотра."""
with _conn() as conn:
rows = conn.execute(
"SELECT id, timestamp, username, tab_id, comment, answer "
"SELECT id, timestamp, username, tab_id, comment, answer, category, source, status "
"FROM feedback ORDER BY id DESC LIMIT ?",
(limit,),
).fetchall()
@@ -162,6 +171,10 @@ def _колонки_нет(conn, таблица: str, колонка: str) -> bo
def init_db():
with _conn() as conn:
conn.executescript(_CREATE)
for _col, _decl in (("category", "TEXT"), ("source", "TEXT"),
("status", "TEXT NOT NULL DEFAULT 'new'")):
if _колонки_нет(conn, "feedback", _col):
conn.execute(f"ALTER TABLE feedback ADD COLUMN {_col} {_decl}")
# Migration: add blocked column if missing
if _колонки_нет(conn, "users", "blocked"):
conn.execute("ALTER TABLE users ADD COLUMN blocked INTEGER NOT NULL DEFAULT 0")