Files
tg_post_min/app/moderation/logger_task.py
Andrey K. Choi c16ec54891 Bot become a Community Guard & Post send manager
added: dictionary support for censore
message/user management with dict triggers
2025-08-22 21:44:14 +09:00

59 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
from typing import Dict, Any, List
from app.db.session import get_session
from app.db.models import ModerationLog, MessageEvent
async def moderation_writer(
queue: asyncio.Queue,
flush_interval: float = 0.3,
max_batch: int = 200,
):
"""
Фоновый воркер: получает события из очереди и записывает в БД пачками.
Ожидаемые элементы очереди — dict со схемой:
- {'type':'log', 'chat_id':..., 'user_id':..., 'message_id':..., 'reason':..., 'action':...}
- {'type':'event', 'chat_id':..., 'user_id':..., 'message_id':..., 'content_hash':...}
"""
buf: List[Dict[str, Any]] = []
while True:
try:
item = await asyncio.wait_for(queue.get(), timeout=flush_interval)
buf.append(item)
except asyncio.TimeoutError:
pass
if not buf:
continue
# ограничим размер пачки
batch, buf = (buf[:max_batch], buf[max_batch:]) if len(buf) > max_batch else (buf[:], [])
try:
with get_session() as s:
for ev in batch:
t = ev.get("type")
if t == "log":
s.add(
ModerationLog(
chat_id=ev.get("chat_id", 0),
tg_user_id=ev.get("user_id", 0),
message_id=ev.get("message_id"),
reason=ev.get("reason", "")[:4000],
action=ev.get("action", "")[:32],
)
)
elif t == "event":
s.add(
MessageEvent(
chat_id=ev.get("chat_id", 0),
tg_user_id=ev.get("user_id", 0),
message_id=ev.get("message_id"),
content_hash=ev.get("content_hash"),
)
)
s.commit()
except Exception:
# на ошибке просто пропускаем пачку, чтобы не зациклиться
pass