Files
ai-agent/backend/users/push_style.py
gabrielvidalandClaude Opus 5.5 c80c9e2323 feat(notifications): notification channels a plugin contributes; Home Assistant delivery leaves the core
A plugin declaring provides: {"notify.channel": "<action>"} gets every
push, ask and clear of the person who installed it (notifications/channels.py),
the payload the HA webhook always got. Tried once, no retry; with no channel
a push is still in the app (an in_app record, ok) and on the browsers, which
stay core. Guests' own channel installs get their wording and guest link.

Removed: the webhook list (Settings → Webhooks, /api/webhooks*, forward(),
the seeded HA webhook) and integrations/home-assistant, now in the
home-assistant-push plugin (ai-agent-plugins). The old webhook's URL and PWA
package are carried over once into the plugin's data. /settings/webhooks
redirects to the plugin. Forms ha-push-as-a-plugin_yjj2n0,
browser-push-as-a-plugin_7id9we.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 17:50:55 +01:00

86 lines
3.4 KiB
Python

"""
How a web push looks and whether it buzzes — the Web Push twin of the Home
Assistant integration's ``TYPE_STYLES`` (the ``home-assistant-push`` plugin's
``integration/.../const.py``, in ai-agent-plugins). A push's ``type`` picks a **style**: its large icon and
status-bar badge (``frontend/public/push/<style>.png`` / ``<style>-badge.png``,
rendered by ``frontend/scripts/push-icons.mjs`` in the HA colours).
A browser gives a site **one** Android notification channel, so HA's
per-type channels live here instead, as **groups** of styles each user sets to
``buzz`` / ``silent`` / ``off`` (``users/webpush.py`` keeps the choice; the
defaults follow the HA importance: ``high``/``max`` buzz, ``default`` is
silent).
"""
from __future__ import annotations
from core.i18n import msg, tr
# Every notify-done --type the HA map knows, folded onto its style.
ALIASES = {
"config": "code", "commit": "git", "container": "service", "mcp": "ai",
"download": "music", "jellyfin": "media", "metrics": "logs",
"auth": "security", "done": "success", "fail": "error", "warning": "warn",
}
# style → HA importance.
IMPORTANCE = {
"deploy": "high", "code": "default", "build": "high", "git": "default",
"service": "default", "ai": "high", "music": "default", "media": "default",
"research": "high", "logs": "default", "dns": "default", "security": "max",
"mail": "high", "success": "high", "backup": "default", "error": "max",
"warn": "high", "ask": "max", "form": "max", "default": "high",
}
def style_of(kind: str, type_: str) -> str:
"""The style of a push: an ask's or a form's own, else its type's."""
if kind in ("ask", "form"):
return kind
t = (type_ or "").strip().lower()
t = ALIASES.get(t, t)
return t if t in IMPORTANCE else "default"
def look(style: str) -> dict:
"""The payload fields that draw ``style``."""
return {"icon": f"/push/{style}.png", "badge": f"/push/{style}-badge.png"}
LEVELS = ("buzz", "silent", "off")
# The groups a user tunes — (id, label, styles, default level).
GROUPS = (
("questions", msg("Questions & forms"), ("ask", "form"), "buzz"),
("deploy", msg("Deploys"), ("deploy",), "buzz"),
("ai", msg("AI & research"), ("ai", "research"), "buzz"),
("code", msg("Code, commits & builds"), ("code", "git", "build"), "silent"),
("infra", msg("Services & infra"), ("service", "dns", "logs", "backup"), "silent"),
("media", msg("Media & music"), ("media", "music"), "silent"),
("mail", msg("Mail"), ("mail",), "buzz"),
("success", msg("Success"), ("success",), "buzz"),
("alerts", msg("Warnings, errors & security"), ("warn", "error", "security"), "buzz"),
("other", msg("Everything else"), ("default",), "buzz"),
)
_GROUP_OF = {style: g[0] for g in GROUPS for style in g[2]}
DEFAULTS = {g[0]: g[3] for g in GROUPS}
def group_of(style: str) -> str:
return _GROUP_OF.get(style, "other")
def clean_levels(raw) -> dict:
"""A user's group → level choices, unknown groups and levels dropped."""
if not isinstance(raw, dict):
return {}
return {g: v for g, v in raw.items() if g in DEFAULTS and v in LEVELS}
def prefs_view(levels: dict, lang: str = "en") -> list[dict]:
"""Every group with the user's level (or the default), labelled in
``lang``."""
return [{"id": gid, "label": tr(lang, label), "level": levels.get(gid, default),
"default": default}
for gid, label, _styles, default in GROUPS]