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) )