fix(backend): a bulk deletion is announced in chunks that fit the notify payload limit
This commit is contained in:
1 parent
4ce9c5d340
commit
33ceea3dfc
1 file changed
+10
-2
@@ -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)
|
||||
)
|
||||
Reference in new issue
Block a user