53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
|
|
|
|
import flet as ft
|
|
from typing import Any
|
|
|
|
|
|
def send_browser_notification(page: ft.Page, title: str, body: str) -> None:
|
|
"""Broadcast a browser notification event to all connected sessions.
|
|
|
|
This is typically called from the client (shop) side when a new chat
|
|
is opened or the first message is sent. The actual visual notification
|
|
will be displayed only on admin pages that handle the event.
|
|
"""
|
|
if page is None:
|
|
return
|
|
|
|
payload: dict[str, Any] = {
|
|
"type": "browser_notification",
|
|
"title": title,
|
|
"body": body,
|
|
}
|
|
|
|
# Use PubSub so that admin sessions can react and show a snackbar/toast.
|
|
try:
|
|
# send_all so that any admin pages, and potentially this page, can receive it
|
|
page.pubsub.send_all(payload)
|
|
except Exception:
|
|
# If PubSub is not available, we simply do nothing; email still covers notification.
|
|
pass
|
|
|
|
|
|
def show_local_browser_notification(page: ft.Page, title: str, body: str) -> None:
|
|
"""Show a visual notification (snackbar) on the current page for admin users.
|
|
|
|
This is intended to be called from admin pages when a `browser_notification`
|
|
event is received via PubSub. It will silently do nothing for non-admin users.
|
|
"""
|
|
if page is None:
|
|
return
|
|
|
|
user = page.session.get("user") if hasattr(page, "session") else None
|
|
if not user or user.get("role") != "admin":
|
|
# Only admins see these notifications in the browser
|
|
return
|
|
|
|
message = f"{title}: {body}" if title else body
|
|
|
|
page.snack_bar = ft.SnackBar(
|
|
content=ft.Text(message),
|
|
open=True,
|
|
action="OK",
|
|
)
|
|
page.update() |