fix(backend): a bulk deletion is announced in chunks that fit the notify payload limit

This commit is contained in:
hh committed 2026-10-19 02:29:19 +00:00
1 parent 4ce9c5d340
commit 33ceea3dfc
1 file changed
+10 -2
+10 -2
View File
@@ -4,6 +4,7 @@ from typing import Literal
import asyncpg import asyncpg
BG_EVENTS_CHANNEL = "bg_events" BG_EVENTS_CHANNEL = "bg_events"
MESSAGE_IDS_PER_EVENT = 500
EventKind = Literal["message", "edit", "delete", "reaction", "presence", "receipt"] EventKind = Literal["message", "edit", "delete", "reaction", "presence", "receipt"]
@@ -22,8 +23,15 @@ async def notify_bg_event( # noqa: PLR0913
payload["chat_id"] = chat_id payload["chat_id"] = chat_id
if message_id is not None: if message_id is not None:
payload["message_id"] = message_id payload["message_id"] = message_id
if message_ids is not None: if message_ids is None:
payload["message_ids"] = message_ids 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( await pool.execute(
"SELECT pg_notify($1, $2)", BG_EVENTS_CHANNEL, json.dumps(payload) "SELECT pg_notify($1, $2)", BG_EVENTS_CHANNEL, json.dumps(payload)
) )