Add Ntfy push notifications for super admin events

Sends alerts to a configurable Ntfy topic on: new user registration,
account lockout after 5 failed login attempts, and login attempts on an
already-locked account. Fire-and-forget — never raises if Ntfy is down.

Configure via NTFY_URL and NTFY_TOKEN in .env.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 01:04:12 -07:00
parent 663b506868
commit 090ebc109e
7 changed files with 64 additions and 0 deletions

25
backend/app/utils/ntfy.py Normal file
View File

@@ -0,0 +1,25 @@
import logging
import httpx
from app.config import get_settings
logger = logging.getLogger("homeschool.ntfy")
async def notify(title: str, message: str, priority: str = "default", tags: list[str] | None = None) -> None:
"""Fire-and-forget notification to Ntfy. Silently logs errors — never raises."""
settings = get_settings()
if not settings.ntfy_url:
return
headers = {"Title": title, "Priority": priority}
if tags:
headers["Tags"] = ",".join(tags)
if settings.ntfy_token:
headers["Authorization"] = f"Bearer {settings.ntfy_token}"
try:
async with httpx.AsyncClient(timeout=5) as client:
resp = await client.post(settings.ntfy_url, content=message, headers=headers)
resp.raise_for_status()
except Exception as exc:
logger.warning("Ntfy notification failed: %s", exc)