Improve visual
This commit is contained in:
@@ -45,6 +45,7 @@ class TaskStorage:
|
||||
"links_found": params.links_found,
|
||||
"chat_id": params.chat_id,
|
||||
"user_id": params.user_id,
|
||||
"visits_percent": params.visits_percent,
|
||||
"started_at": params.started_at.isoformat() if params.started_at else None,
|
||||
}
|
||||
|
||||
@@ -71,6 +72,7 @@ class TaskStorage:
|
||||
links_found=data.get("links_found", 0),
|
||||
chat_id=data.get("chat_id"),
|
||||
user_id=data.get("user_id"),
|
||||
visits_percent=data.get("visits_percent", 0.0),
|
||||
)
|
||||
if data.get("started_at"):
|
||||
try:
|
||||
@@ -220,6 +222,48 @@ class _IntBalanceStorage:
|
||||
return await asyncio.to_thread(self._load_sync)
|
||||
|
||||
|
||||
class ChatHistoryStorage:
|
||||
"""Хранит ID сообщений бота для очистки чата при рестарте."""
|
||||
|
||||
def __init__(self, file_path: str = "data/chat_history.json"):
|
||||
self.file_path = Path(file_path)
|
||||
self.file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
def _load_sync(self) -> dict:
|
||||
if not self.file_path.exists():
|
||||
return {}
|
||||
try:
|
||||
with open(self.file_path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except (json.JSONDecodeError, IOError):
|
||||
return {}
|
||||
|
||||
def _save_sync(self, data: dict) -> None:
|
||||
with open(self.file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f)
|
||||
|
||||
async def add(self, user_id: int, msg_id: int) -> None:
|
||||
async with self._lock:
|
||||
data = await asyncio.to_thread(self._load_sync)
|
||||
key = str(user_id)
|
||||
ids = data.get(key, [])
|
||||
ids.append(msg_id)
|
||||
data[key] = ids[-200:] # храним последние 200
|
||||
await asyncio.to_thread(self._save_sync, data)
|
||||
|
||||
async def get(self, user_id: int) -> list:
|
||||
async with self._lock:
|
||||
data = await asyncio.to_thread(self._load_sync)
|
||||
return data.get(str(user_id), [])
|
||||
|
||||
async def clear(self, user_id: int) -> None:
|
||||
async with self._lock:
|
||||
data = await asyncio.to_thread(self._load_sync)
|
||||
data.pop(str(user_id), None)
|
||||
await asyncio.to_thread(self._save_sync, data)
|
||||
|
||||
|
||||
class BalanceStorage(_IntBalanceStorage):
|
||||
"""Баланс переходов пользователей."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user