from datetime import UTC, datetime from pyrogram.enums import ChatType from pyrogram.types import Chat, User from userbot.modules.avatars import note_avatar from userbot.modules.capture.context import CaptureContext from userbot.modules.groups.repository import record_chat_meta from userbot.modules.profiles.parse import snapshot_from_chat, snapshot_from_high_level from userbot.modules.profiles.repository import write_profile from userbot.modules.raw_json import raw_json async def save_user(ctx: CaptureContext, user: User) -> None: fields, photo_file_id, photo_unique_id = snapshot_from_high_level(user) if not await ctx.peer_identity.changed(ctx.pool, ctx.account_id, user.id, fields): return await write_profile( ctx.pool, ctx.account_id, user.id, fields, raw_json(user), history=ctx.resolve_peer(user.id, is_bot=bool(user.is_bot)).profile_history, ) if photo_file_id and photo_unique_id: await note_avatar( ctx.pool, ctx.account_id, user.id, "peer", photo_unique_id, photo_file_id ) async def save_private(ctx: CaptureContext, chat: Chat) -> bool: chat_id = chat.id or 0 fields, photo_file_id, photo_unique_id = snapshot_from_chat(chat) is_bot = chat.type is ChatType.BOT await write_profile( ctx.pool, ctx.account_id, chat_id, fields, raw_json(chat), history=ctx.resolve_peer(chat_id, is_bot=is_bot).profile_history, ) if photo_file_id and photo_unique_id: await note_avatar( ctx.pool, ctx.account_id, chat_id, "peer", photo_unique_id, photo_file_id ) return bool(fields.first_name or fields.last_name or fields.username) async def save_group(ctx: CaptureContext, chat: Chat) -> None: chat_id = chat.id or 0 photo = chat.photo photo_unique_id = photo.big_photo_unique_id if photo else None photo_file_id = photo.big_file_id if photo else None await record_chat_meta( ctx.pool, ctx.account_id, chat_id, 0, chat.title, photo_unique_id, datetime.now(UTC), raw_json(chat), ) if photo_file_id and photo_unique_id: await note_avatar( ctx.pool, ctx.account_id, chat_id, "chat", photo_unique_id, photo_file_id ) async def save_chat(ctx: CaptureContext, chat: Chat) -> None: if (chat.id or 0) > 0: await save_private(ctx, chat) else: await save_group(ctx, chat)