Improve Telegram forum onboarding and topic safety
This commit is contained in:
parent
2b56b98697
commit
a1b7a14138
13 changed files with 1101 additions and 376 deletions
|
|
@ -27,18 +27,29 @@ def init_db() -> None:
|
|||
tg_user_id INTEGER PRIMARY KEY,
|
||||
platform_user_id TEXT NOT NULL,
|
||||
display_name TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
forum_group_id INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
chat_id TEXT PRIMARY KEY,
|
||||
tg_user_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
archived_at TIMESTAMP,
|
||||
chat_id TEXT PRIMARY KEY,
|
||||
tg_user_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
archived_at TIMESTAMP,
|
||||
forum_thread_id INTEGER,
|
||||
FOREIGN KEY(tg_user_id) REFERENCES tg_users(tg_user_id)
|
||||
);
|
||||
""")
|
||||
# Миграция для существующих БД
|
||||
try:
|
||||
con.execute("ALTER TABLE tg_users ADD COLUMN forum_group_id INTEGER")
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
con.execute("ALTER TABLE chats ADD COLUMN forum_thread_id INTEGER")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def get_or_create_tg_user(
|
||||
|
|
@ -119,3 +130,38 @@ def archive_chat(chat_id: str) -> None:
|
|||
"UPDATE chats SET archived_at = CURRENT_TIMESTAMP WHERE chat_id = ?",
|
||||
(chat_id,),
|
||||
)
|
||||
|
||||
|
||||
def set_forum_group(tg_user_id: int, group_id: int) -> None:
|
||||
with _conn() as con:
|
||||
con.execute(
|
||||
"UPDATE tg_users SET forum_group_id = ? WHERE tg_user_id = ?",
|
||||
(group_id, tg_user_id),
|
||||
)
|
||||
|
||||
|
||||
def get_forum_group(tg_user_id: int) -> int | None:
|
||||
with _conn() as con:
|
||||
row = con.execute(
|
||||
"SELECT forum_group_id FROM tg_users WHERE tg_user_id = ?",
|
||||
(tg_user_id,),
|
||||
).fetchone()
|
||||
return row["forum_group_id"] if row else None
|
||||
|
||||
|
||||
def set_forum_thread(chat_id: str, thread_id: int) -> None:
|
||||
with _conn() as con:
|
||||
con.execute(
|
||||
"UPDATE chats SET forum_thread_id = ? WHERE chat_id = ?",
|
||||
(thread_id, chat_id),
|
||||
)
|
||||
|
||||
|
||||
def get_chat_by_thread(tg_user_id: int, thread_id: int) -> dict | None:
|
||||
with _conn() as con:
|
||||
row = con.execute(
|
||||
"SELECT * FROM chats WHERE tg_user_id = ? AND forum_thread_id = ? "
|
||||
"AND archived_at IS NULL",
|
||||
(tg_user_id, thread_id),
|
||||
).fetchone()
|
||||
return dict(row) if row else None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue