Enhance database handling and improve HTML safety across the bot. Added async methods for blacklist checks, updated connection settings for SQLite, and implemented HTML escaping for user inputs and messages to prevent potential issues. Adjusted middleware latency and refactored various handlers for better performance and reliability.

This commit is contained in:
2025-08-26 16:51:28 +03:00
parent 7b6abe2a0e
commit fee22f8ad4
17 changed files with 308 additions and 77 deletions

View File

@@ -1,6 +1,8 @@
import os
import sqlite3
import asyncio
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from logs.custom_logger import logger
@@ -16,10 +18,15 @@ class BotDB:
self.cursor = None
self.logger = logger
self.logger.info(f'Инициация базы данных: {self.db_file}')
# Создаем пул потоков для асинхронных операций
self.executor = ThreadPoolExecutor(max_workers=4)
def connect(self):
"""Создание соединения и курсора."""
self.conn = sqlite3.connect(self.db_file)
# Добавляем таймаут для предотвращения зависаний
self.conn = sqlite3.connect(self.db_file, timeout=10.0)
# Включаем WAL режим для лучшей производительности
self.conn.execute("PRAGMA journal_mode=WAL")
self.cursor = self.conn.cursor()
def create_table(self, sql_script):
@@ -1205,3 +1212,17 @@ class BotDB:
self.cursor.close()
if self.conn:
self.conn.close()
async def check_user_in_blacklist_async(self, user_id: int):
"""
Асинхронная версия проверки пользователя в черном списке.
"""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(self.executor, self.check_user_in_blacklist, user_id)
async def get_blacklist_users_by_id_async(self, user_id: int):
"""
Асинхронная версия получения информации о пользователе из черного списка.
"""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(self.executor, self.get_blacklist_users_by_id, user_id)