| | |
| | | "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, " |
| | |
| | | "target_id INTEGER, " |
| | | "text TEXT, " |
| | | "created_at INTEGER, " |
| | | "status TEXT DEFAULT 'new')", |
| | | "status TEXT DEFAULT 'new', " |
| | | "target_msg_id INTEGER DEFAULT 0, " |
| | | "target_text TEXT DEFAULT '')", |
| | | "CREATE TABLE IF NOT EXISTS report_block (" |
| | | "user_id INTEGER, " |
| | | "chat_id INTEGER, " |
| | |
| | | "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, " |
| | | "tg_msg_id INTEGER DEFAULT 0, " |
| | | "media_type TEXT DEFAULT '', " |
| | | "media_file_id TEXT DEFAULT '', " |
| | | "reply_to_tg INTEGER DEFAULT 0, " |
| | | "reply_to_name TEXT DEFAULT '', " |
| | | "reply_to_text TEXT DEFAULT '')", |
| | | "CREATE TABLE IF NOT EXISTS rp_actions (" |
| | | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| | | "chat_id INTEGER, " |
| | | "trigger TEXT COLLATE NOCASE, " |
| | | "response_text TEXT, " |
| | | "gif_file_id TEXT, " |
| | | "enabled INTEGER DEFAULT 1, " |
| | | "created_at INTEGER, " |
| | | "UNIQUE(chat_id, trigger))", |
| | | ) |
| | | |
| | | |
| | |
| | | await db.execute(f"ALTER TABLE chat_cfg ADD COLUMN {col} {ddl}") |
| | | if await tbl_exists(db, "alarm") and not await col_exists(db, "alarm", "until"): |
| | | await db.execute("ALTER TABLE alarm ADD COLUMN until INTEGER DEFAULT 0") |
| | | if await tbl_exists(db, "reports"): |
| | | if not await col_exists(db, "reports", "target_msg_id"): |
| | | 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") |
| | | if await tbl_exists(db, "messages"): |
| | | msg_cols = { |
| | | "tg_msg_id": "INTEGER DEFAULT 0", |
| | | "media_type": "TEXT DEFAULT ''", |
| | | "media_file_id": "TEXT DEFAULT ''", |
| | | "reply_to_tg": "INTEGER DEFAULT 0", |
| | | "reply_to_name": "TEXT DEFAULT ''", |
| | | "reply_to_text": "TEXT DEFAULT ''", |
| | | } |
| | | for col, ddl in msg_cols.items(): |
| | | if not await col_exists(db, "messages", col): |
| | | await db.execute(f"ALTER TABLE messages ADD COLUMN {col} {ddl}") |
| | | |
| | | |
| | | async def init_db() -> None: |
| | |
| | | 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() |
| | |
| | | |
| | | # --- Репорты --- |
| | | |
| | | async def add_report(chat_id: int, user_id: int, target_id: int, text: str, ts: int) -> int: |
| | | async def add_report(chat_id: int, user_id: int, target_id: int, text: str, ts: int, target_msg_id: int = 0, target_text: str = "") -> int: |
| | | async with db_conn() as db: |
| | | cur = await db.execute( |
| | | "INSERT INTO reports (chat_id, user_id, target_id, text, created_at, status) " |
| | | "VALUES (?, ?, ?, ?, ?, 'new')", |
| | | (chat_id, user_id, target_id, text, ts), |
| | | "INSERT INTO reports (chat_id, user_id, target_id, text, created_at, status, target_msg_id, target_text) " |
| | | "VALUES (?, ?, ?, ?, ?, 'new', ?, ?)", |
| | | (chat_id, user_id, target_id, text, ts, target_msg_id, target_text), |
| | | ) |
| | | await db.commit() |
| | | return cur.lastrowid |
| | | |
| | | |
| | | async def get_report(report_id: int) -> Optional[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute("SELECT * FROM reports WHERE id=?", (report_id,)) as cur: |
| | | row = await cur.fetchone() |
| | | return dict(row) if row else None |
| | | |
| | | |
| | | async def clr_reports(chat_id: int) -> None: |
| | | async with db_conn() as db: |
| | | await db.execute("DELETE FROM reports WHERE chat_id=?", (chat_id,)) |
| | | await db.commit() |
| | | |
| | | |
| | | async def list_reports(chat_id: int, limit: int = 200) -> List[dict]: |
| | |
| | | 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, |
| | | tg_msg_id: int = 0, |
| | | media_type: str = "", |
| | | media_file_id: str = "", |
| | | reply_to_tg: int = 0, |
| | | reply_to_name: str = "", |
| | | reply_to_text: str = "", |
| | | keep: int = 200, |
| | | ) -> int: |
| | | # Храним последние keep сообщений на чат. |
| | | async with db_conn() as db: |
| | | cur = await db.execute( |
| | | "INSERT INTO messages " |
| | | "(chat_id, user_id, name, text, created_at, tg_msg_id, media_type, media_file_id, reply_to_tg, reply_to_name, reply_to_text) " |
| | | "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| | | (chat_id, user_id, name, text, ts, tg_msg_id, media_type, media_file_id, reply_to_tg, reply_to_name, reply_to_text), |
| | | ) |
| | | 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() |
| | | return cur.lastrowid |
| | | |
| | | |
| | | async def get_msg(msg_id: int) -> Optional[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute("SELECT * FROM messages WHERE id=?", (msg_id,)) as cur: |
| | | row = await cur.fetchone() |
| | | return dict(row) if row else None |
| | | |
| | | |
| | | async def find_msg_by_tg(chat_id: int, tg_msg_id: int) -> Optional[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute( |
| | | "SELECT * FROM messages WHERE chat_id=? AND tg_msg_id=? ORDER BY id DESC LIMIT 1", |
| | | (chat_id, tg_msg_id), |
| | | ) as cur: |
| | | row = await cur.fetchone() |
| | | return dict(row) if row else None |
| | | |
| | | |
| | | 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] |
| | | |
| | | |
| | | async def list_msgs_after(chat_id: int, after_id: int, limit: int = 100) -> List[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute( |
| | | "SELECT * FROM messages WHERE chat_id=? AND id > ? ORDER BY id ASC LIMIT ?", |
| | | (chat_id, after_id, limit), |
| | | ) as cur: |
| | | return [dict(r) for r in await cur.fetchall()] |
| | | |
| | | |
| | | # --- RP Actions --- |
| | | |
| | | async def add_rp_action(chat_id: int, trigger: str, response_text: str, gif_file_id: Optional[str], ts: int) -> Optional[int]: |
| | | async with db_conn() as db: |
| | | try: |
| | | await db.execute( |
| | | "INSERT INTO rp_actions (chat_id, trigger, response_text, gif_file_id, enabled, created_at) " |
| | | "VALUES (?, ?, ?, ?, 1, ?)", |
| | | (chat_id, trigger, response_text, gif_file_id, ts), |
| | | ) |
| | | await db.commit() |
| | | return (await db.execute("SELECT last_insert_rowid()")).fetchone()[0] |
| | | except Exception: |
| | | return None |
| | | |
| | | |
| | | async def list_rp_actions(chat_id: int) -> List[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute( |
| | | "SELECT * FROM rp_actions WHERE chat_id=? AND enabled=1 ORDER BY created_at", |
| | | (chat_id,), |
| | | ) as cur: |
| | | return [dict(r) for r in await cur.fetchall()] |
| | | |
| | | |
| | | async def get_rp_action(action_id: int) -> Optional[dict]: |
| | | async with db_conn() as db: |
| | | async with db.execute("SELECT * FROM rp_actions WHERE id=?", (action_id,)) as cur: |
| | | row = await cur.fetchone() |
| | | return dict(row) if row else None |
| | | |
| | | |
| | | async def delete_rp_action(action_id: int) -> bool: |
| | | async with db_conn() as db: |
| | | await db.execute("DELETE FROM rp_actions WHERE id=?", (action_id,)) |
| | | await db.commit() |
| | | return True |
| | | |
| | | |
| | | async def check_rp_trigger(chat_id: int, text: str) -> Optional[dict]: |
| | | text_upper = text.upper() |
| | | async with db_conn() as db: |
| | | async with db.execute( |
| | | "SELECT * FROM rp_actions WHERE chat_id=? AND enabled=1", |
| | | (chat_id,), |
| | | ) as cur: |
| | | rows = await cur.fetchall() |
| | | for row in rows: |
| | | if row["trigger"].upper() in text_upper: |
| | | return dict(row) |
| | | return None |