Files
beavergram/backend/src/userbot/handlers/raw/reactions.py
T

58 lines
1.7 KiB
Python

from pyrogram import raw, utils
from userbot.modules.capture import repository
from userbot.modules.capture.chat_meta import meta_from_peer
from userbot.modules.client import PyroClient
from utils.events import notify_bg_event
HANDLES = (raw.types.UpdateMessageReactions,)
def _reaction_key(reaction: raw.base.Reaction) -> str | None:
if isinstance(reaction, raw.types.ReactionEmoji):
return reaction.emoticon
if isinstance(reaction, raw.types.ReactionCustomEmoji):
return f"custom:{reaction.document_id}"
return None
def _parse_recent(reactions: raw.base.MessageReactions) -> list[tuple[int, str]]:
recent = getattr(reactions, "recent_reactions", None) or []
parsed: list[tuple[int, str]] = []
for peer_reaction in recent:
key = _reaction_key(peer_reaction.reaction)
if key is None:
continue
try:
peer_id = utils.get_peer_id(peer_reaction.peer_id)
except (ValueError, AttributeError):
continue
parsed.append((peer_id, key))
return parsed
async def handle(
client: PyroClient,
update: raw.types.UpdateMessageReactions,
users: dict,
chats: dict,
) -> None:
ctx = client.capture
if ctx is None:
return
meta = meta_from_peer(update.peer, chats, users, ctx.contacts.ids)
toggles = ctx.resolve(meta)
if not toggles.reactions:
return
current = _parse_recent(update.reactions)
await repository.sync_reactions(
ctx.pool, ctx.account_id, meta.chat_id, update.msg_id, current
)
await notify_bg_event(
ctx.pool,
"reaction",
ctx.account_id,
chat_id=meta.chat_id,
message_id=update.msg_id,
)