Enhance bot functionality and refactor database interactions

- Added `ca-certificates` installation to Dockerfile for improved network security.
- Updated health check command in Dockerfile to include better timeout handling.
- Refactored `run_helper.py` to implement proper signal handling and logging during shutdown.
- Transitioned database operations to an asynchronous model in `async_db.py`, improving performance and responsiveness.
- Updated database schema to support new foreign key relationships and optimized indexing for better query performance.
- Enhanced various bot handlers to utilize async database methods, improving overall efficiency and user experience.
- Removed obsolete database and fix scripts to streamline the project structure.
This commit is contained in:
2025-09-02 18:22:02 +03:00
parent 013892dcb7
commit 1c6a37bc12
59 changed files with 5682 additions and 4204 deletions

View File

@@ -1,14 +1,18 @@
-- Telegram Helper Bot Database Schema
-- Compatible with Docker container deployment
-- IMPORTANT: Enable foreign key support after each database connection
-- PRAGMA foreign_keys = ON;
-- Note: sqlite_sequence table is automatically created by SQLite for AUTOINCREMENT fields
-- No need to create it manually
-- Users who have listened to audio messages
CREATE TABLE IF NOT EXISTS listen_audio_users (
CREATE TABLE IF NOT EXISTS user_audio_listens (
file_name TEXT NOT NULL,
user_id INTEGER NOT NULL,
is_listen BOOLEAN NOT NULL DEFAULT 0
PRIMARY KEY (file_name, user_id),
FOREIGN KEY (user_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- Reference table for audio messages
@@ -16,29 +20,24 @@ CREATE TABLE IF NOT EXISTS audio_message_reference (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
file_name TEXT NOT NULL UNIQUE,
author_id INTEGER NOT NULL,
date_added DATE NOT NULL,
listen_count INTEGER NOT NULL DEFAULT 0
);
-- Database migrations tracking
CREATE TABLE IF NOT EXISTS migrations (
version INTEGER NOT NULL PRIMARY KEY,
script_name TEXT NOT NULL,
created_at TEXT NOT NULL
date_added INTEGER NOT NULL,
FOREIGN KEY (author_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- Bot administrators
CREATE TABLE IF NOT EXISTS admins (
user_id INTEGER NOT NULL PRIMARY KEY,
role TEXT
role TEXT,
FOREIGN KEY (user_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- User blacklist for banned users
CREATE TABLE IF NOT EXISTS blacklist (
user_id INTEGER NOT NULL PRIMARY KEY,
user_name TEXT,
message_for_user TEXT,
date_to_unban INTEGER
date_to_unban INTEGER,
created_at INTEGER DEFAULT (strftime('%s', 'now')),
FOREIGN KEY (user_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- User message history
@@ -46,8 +45,9 @@ CREATE TABLE IF NOT EXISTS user_messages (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
message_text TEXT,
user_id INTEGER,
message_id INTEGER NOT NULL,
date TEXT NOT NULL
telegram_message_id INTEGER NOT NULL,
date INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- Suggested posts from Telegram
@@ -56,14 +56,16 @@ CREATE TABLE IF NOT EXISTS post_from_telegram_suggest (
text TEXT,
helper_text_message_id INTEGER,
author_id INTEGER,
created_at TEXT NOT NULL
created_at INTEGER NOT NULL,
FOREIGN KEY (author_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- Links between posts and content
CREATE TABLE IF NOT EXISTS message_link_to_content (
post_id INTEGER NOT NULL,
message_id INTEGER NOT NULL,
PRIMARY KEY (post_id, message_id)
PRIMARY KEY (post_id, message_id),
FOREIGN KEY (post_id) REFERENCES post_from_telegram_suggest(message_id) ON DELETE CASCADE
);
-- Content associated with Telegram posts
@@ -71,22 +73,22 @@ CREATE TABLE IF NOT EXISTS content_post_from_telegram (
message_id INTEGER NOT NULL,
content_name TEXT NOT NULL,
content_type TEXT,
PRIMARY KEY (message_id, content_name)
PRIMARY KEY (message_id, content_name),
FOREIGN KEY (message_id) REFERENCES post_from_telegram_suggest(message_id) ON DELETE CASCADE
);
-- Bot users information
-- Bot users information (user_id is now PRIMARY KEY)
CREATE TABLE IF NOT EXISTS our_users (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL UNIQUE,
user_id INTEGER NOT NULL PRIMARY KEY,
first_name TEXT,
full_name TEXT,
username TEXT,
is_bot BOOLEAN DEFAULT 0,
language_code TEXT,
has_stickers INTEGER DEFAULT 0 NOT NULL,
has_stickers BOOLEAN DEFAULT 0 NOT NULL,
emoji TEXT,
date_added DATE NOT NULL,
date_changed DATE NOT NULL,
date_added INTEGER NOT NULL,
date_changed INTEGER NOT NULL,
voice_bot_welcome_received BOOLEAN DEFAULT 0
);
@@ -94,14 +96,18 @@ CREATE TABLE IF NOT EXISTS our_users (
CREATE TABLE IF NOT EXISTS audio_moderate (
user_id INTEGER NOT NULL,
message_id INTEGER,
PRIMARY KEY (user_id, message_id)
PRIMARY KEY (user_id, message_id),
FOREIGN KEY (user_id) REFERENCES our_users(user_id) ON DELETE CASCADE
);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_listen_audio_users_file_name ON listen_audio_users(file_name);
CREATE INDEX IF NOT EXISTS idx_listen_audio_users_user_id ON listen_audio_users(user_id);
-- Optimized index for user_audio_listens - only user_id for "show all audio listened by user X"
CREATE INDEX IF NOT EXISTS idx_user_audio_listens_user_id ON user_audio_listens(user_id);
CREATE INDEX IF NOT EXISTS idx_audio_message_reference_author_id ON audio_message_reference(author_id);
CREATE INDEX IF NOT EXISTS idx_user_messages_user_id ON user_messages(user_id);
CREATE INDEX IF NOT EXISTS idx_post_from_telegram_suggest_author_id ON post_from_telegram_suggest(author_id);
CREATE INDEX IF NOT EXISTS idx_our_users_user_id ON our_users(user_id);
CREATE INDEX IF NOT EXISTS idx_audio_moderate_user_id ON audio_moderate(user_id);
CREATE INDEX IF NOT EXISTS idx_blacklist_date_to_unban ON blacklist(date_to_unban);
CREATE INDEX IF NOT EXISTS idx_user_messages_date ON user_messages(date);
CREATE INDEX IF NOT EXISTS idx_audio_message_reference_date ON audio_message_reference(date_added);
CREATE INDEX IF NOT EXISTS idx_post_from_telegram_suggest_date ON post_from_telegram_suggest(created_at);
CREATE INDEX IF NOT EXISTS idx_our_users_date_changed ON our_users(date_changed);