feat: create message capture policies
This commit is contained in:
@@ -7,9 +7,14 @@ import asyncpg
|
||||
import uvloop
|
||||
|
||||
from dependencies.container import container
|
||||
from userbot.folders import FolderCache
|
||||
from userbot.handlers import dialog_filter_handler
|
||||
from userbot.modules import PyroClient
|
||||
from utils.env import env
|
||||
from utils.logging import logger, setup_logging
|
||||
from utils.policy.models import ChatKind, ChatMeta
|
||||
from utils.policy.repository import load_policy_set
|
||||
from utils.policy.resolver import resolve
|
||||
|
||||
setup_logging()
|
||||
|
||||
@@ -24,6 +29,7 @@ ON CONFLICT (tg_user_id) DO UPDATE SET
|
||||
is_active = TRUE,
|
||||
raw = EXCLUDED.raw,
|
||||
updated_at = now()
|
||||
RETURNING account_id
|
||||
"""
|
||||
|
||||
|
||||
@@ -34,10 +40,10 @@ def _discover_sessions(sessions_dir: Path) -> list[Path]:
|
||||
|
||||
async def _sync_account(
|
||||
pool: asyncpg.Pool, client: PyroClient, session_name: str
|
||||
) -> None:
|
||||
) -> int | None:
|
||||
me = client.me
|
||||
if not me:
|
||||
return
|
||||
return None
|
||||
raw = json.dumps(
|
||||
{
|
||||
"id": me.id,
|
||||
@@ -48,10 +54,25 @@ async def _sync_account(
|
||||
}
|
||||
)
|
||||
label = " ".join(filter(None, [me.first_name, me.last_name])) or me.username
|
||||
await pool.execute(
|
||||
account_id = await pool.fetchval(
|
||||
_UPSERT_ACCOUNT, me.id, label, me.phone_number, session_name, raw
|
||||
)
|
||||
logger.info(f"[green]Account synced:[/] {label} ({me.id})")
|
||||
return account_id
|
||||
|
||||
|
||||
async def _setup_policy(
|
||||
pool: asyncpg.Pool, client: PyroClient, account_id: int
|
||||
) -> None:
|
||||
cache = FolderCache(client, pool, account_id)
|
||||
await cache.refresh()
|
||||
client.add_handler(dialog_filter_handler(cache))
|
||||
if client.me:
|
||||
policies = await load_policy_set(pool, account_id)
|
||||
sample = resolve(
|
||||
ChatMeta(chat_id=client.me.id, kind=ChatKind.DM), cache.folders, policies
|
||||
)
|
||||
logger.info(f"[green]Sample resolve (self DM):[/] {sample.model_dump()}")
|
||||
|
||||
|
||||
async def runner() -> None:
|
||||
@@ -78,10 +99,12 @@ async def runner() -> None:
|
||||
f"{client.me.full_name if client.me else 'unknown'} "
|
||||
f"{client.me.id if client.me else 'unknown'}"
|
||||
)
|
||||
await _sync_account(pool, client, session_name)
|
||||
account_id = await _sync_account(pool, client, session_name)
|
||||
if account_id is not None:
|
||||
await _setup_policy(pool, client, account_id)
|
||||
|
||||
if clients:
|
||||
logger.info("[green]Userbot running. Idle (no handlers until phase 3).[/]")
|
||||
logger.info("[green]Userbot running.[/]")
|
||||
await asyncio.Event().wait()
|
||||
finally:
|
||||
for client in clients:
|
||||
|
||||
Reference in New Issue
Block a user