from userbot.modules.capture import capture_message from userbot.modules.jobs.context import JobContext from userbot.modules.jobs.registry import register from utils.policy.models import CaptureToggles SAVE_EVERY = 100 @register("backfill") async def backfill(ctx: JobContext) -> None: client = ctx.client if client is None: return capture = getattr(client, "capture", None) if capture is None: return chat_id = ctx.job.params["chat_id"] toggles = CaptureToggles( messages=True, media=bool(ctx.job.params.get("media")), self_destruct_media=False, ) max_id = (ctx.job.cursor or {}).get("max_id", 0) processed = ctx.job.progress.get("processed", 0) kwargs = {"max_id": max_id} if max_id else {} async for message in client.get_chat_history(chat_id, **kwargs): await capture_message(client, message, capture, toggles) processed += 1 if processed % SAVE_EVERY == 0: next_max = message.id - 1 await ctx.save_cursor({"max_id": next_max}) await ctx.report_progress({"processed": processed, "max_id": next_max}) await ctx.report_progress({"processed": processed, "done": True})