From 33ceea3dfc289e2681c3aa30837fce60075f3578 Mon Sep 17 00:00:00 2001 From: h Date: Mon, 19 Oct 2026 01:59:12 +0000 Subject: [PATCH] fix(backend): a bulk deletion is announced in chunks that fit the notify payload limit --- backend/src/utils/events.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/src/utils/events.py b/backend/src/utils/events.py index 816c815..a65e0b2 100644 --- a/backend/src/utils/events.py +++ b/backend/src/utils/events.py @@ -4,6 +4,7 @@ from typing import Literal import asyncpg BG_EVENTS_CHANNEL = "bg_events" +MESSAGE_IDS_PER_EVENT = 500 EventKind = Literal["message", "edit", "delete", "reaction", "presence", "receipt"] @@ -22,8 +23,15 @@ async def notify_bg_event( # noqa: PLR0913 payload["chat_id"] = chat_id if message_id is not None: payload["message_id"] = message_id - if message_ids is not None: - payload["message_ids"] = message_ids + if message_ids is None: + await _notify(pool, payload) + return + for start in range(0, len(message_ids), MESSAGE_IDS_PER_EVENT): + chunk = message_ids[start : start + MESSAGE_IDS_PER_EVENT] + await _notify(pool, {**payload, "message_ids": chunk}) + + +async def _notify(pool: asyncpg.Pool, payload: dict[str, object]) -> None: await pool.execute( "SELECT pg_notify($1, $2)", BG_EVENTS_CHANNEL, json.dumps(payload) )