Files
beavergram/backend/src/userbot/handlers/deletes.py
T

39 lines
1.5 KiB
Python

from pyrogram.types import Message
from userbot.modules.capture import repository
from userbot.modules.capture.repository import CHANNEL_ID_THRESHOLD
from userbot.modules.client import PyroClient
from utils.events import notify_bg_event
@PyroClient.on_deleted_messages()
async def on_deleted_messages(client: PyroClient, messages: list[Message]) -> None:
ctx = client.capture
if ctx is None:
return
box: list[int] = []
channels: dict[int, list[int]] = {}
for message in messages:
chat = message.chat
chat_id = chat.id if chat is not None else None
if chat_id is None or chat_id > CHANNEL_ID_THRESHOLD:
box.append(message.id)
else:
channels.setdefault(chat_id, []).append(message.id)
ctx.pending_deletes.remember(None, box)
for chat_id, ids in channels.items():
ctx.pending_deletes.remember(chat_id, ids)
if box:
await repository.mark_deleted_box(ctx.pool, ctx.account_id, box)
await ctx.watches.on_deleted(None, box)
await notify_bg_event(ctx.pool, "delete", ctx.account_id, message_ids=box)
for chat_id, ids in channels.items():
await repository.mark_deleted_channel(ctx.pool, ctx.account_id, chat_id, ids)
await ctx.watches.on_deleted(chat_id, ids)
await notify_bg_event(
ctx.pool, "delete", ctx.account_id, chat_id=chat_id, message_ids=ids
)
handlers = on_deleted_messages.handlers # ty: ignore[unresolved-attribute]