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