38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
from pyrogram.types import User
|
|
|
|
from userbot.modules.avatars import note_avatar
|
|
from userbot.modules.jobs.context import JobContext
|
|
from userbot.modules.jobs.registry import register
|
|
from userbot.modules.profiles.parse import snapshot_from_high_level
|
|
from userbot.modules.profiles.repository import write_profile
|
|
from userbot.modules.raw_json import raw_json
|
|
|
|
|
|
@register("sync_contacts")
|
|
async def sync_contacts(ctx: JobContext) -> None:
|
|
client = ctx.client
|
|
if client is None:
|
|
return
|
|
capture = getattr(client, "capture", None)
|
|
if capture is None:
|
|
return
|
|
contacts = await client.get_contacts()
|
|
processed = 0
|
|
for user in contacts:
|
|
if not isinstance(user, User):
|
|
continue
|
|
fields, photo_file_id, photo_unique_id = snapshot_from_high_level(user)
|
|
await write_profile(ctx.pool, ctx.account_id, user.id, fields, raw_json(user))
|
|
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,
|
|
)
|
|
processed += 1
|
|
await capture.contacts.refresh()
|
|
await ctx.report_progress({"processed": processed, "done": True})
|