Corp/backend/notifications/chat_suppression.py
lizerk 14f1e52425
Some checks failed
Deploy production / deploy (push) Failing after 4s
fix(messages): make HTTP authoritative for chat reads
2026-09-10 11:41:59 +03:00

32 lines
847 B
Python

"""Suppress native chat alerts when the recipient is viewing that chat."""
from communications.presence import user_is_viewing_chat
CHAT_NOTIFICATION_VERBS = {
"chat_new_message",
"chat_mention",
"chat_reply",
"announcement_new_message",
}
def notification_chat_id(notification) -> int | None:
if notification.verb not in CHAT_NOTIFICATION_VERBS:
return None
data = notification.data if isinstance(notification.data, dict) else {}
try:
return int(data.get("chat_id"))
except (TypeError, ValueError):
return None
def suppress_native_for_active_chat(notification) -> bool:
chat_id = notification_chat_id(notification)
if chat_id is None or not user_is_viewing_chat(
user_id=notification.recipient_id,
chat_id=chat_id,
):
return False
return True