feat: granular per-mode alert filters (v0.9.3)

Replace coarse milestone/referral suppression with 8 named action
categories (vote, presidential, committee_report, calendar, procedural,
referral, new_document, new_amendment), each independently togglable
per follow mode (Follow / Pocket Veto / Pocket Boost).

- notification_utils: categorize_action() replaces is_milestone_action /
  is_referral_action; _build_payload stores action_category in payload
- congress_poller: use categorize_action() in _update_bill_if_changed
- notification_dispatcher: _should_dispatch() checks per-mode filter dict
  from notification_prefs; follow mode looked up before filter check
- schemas + api: alert_filters (nested dict) wired through settings
  GET/PUT endpoints; no DB migration required
- frontend: tabbed Alert Filters section (Follow / Pocket Veto /
  Pocket Boost), each with independent 8-toggle filter set, milestone
  parent checkbox (indeterminate-aware), Load defaults button, and
  per-tab Save button

Authored-By: Jack Levy
This commit is contained in:
Jack Levy
2026-03-02 19:05:24 -05:00
parent af821dad78
commit a39ae4ccba
7 changed files with 262 additions and 58 deletions

View File

@@ -4,39 +4,31 @@ Centralised here to avoid circular imports.
"""
from datetime import datetime, timedelta, timezone
_MILESTONE_KEYWORDS = [
"passed", "failed", "agreed to",
"signed", "vetoed", "enacted",
"presented to the president",
"ordered to be reported", "ordered reported",
"reported by", "discharged",
"placed on", # placed on calendar
"cloture", "roll call",
"markup", # markup session scheduled/completed
"conference", # conference committee activity
]
# Committee referral — meaningful for pocket_veto/boost but noisy for neutral
_REFERRAL_KEYWORDS = [
"referred to",
]
_VOTE_KW = ["passed", "failed", "agreed to", "roll call"]
_PRES_KW = ["signed", "vetoed", "enacted", "presented to the president"]
_COMMITTEE_KW = ["markup", "ordered to be reported", "ordered reported", "reported by", "discharged"]
_CALENDAR_KW = ["placed on"]
_PROCEDURAL_KW = ["cloture", "conference"]
_REFERRAL_KW = ["referred to"]
# Events created within this window for the same (user, bill, event_type) are suppressed
_DEDUP_MINUTES = 30
def is_milestone_action(action_text: str) -> bool:
def categorize_action(action_text: str) -> str | None:
"""Return the action category string, or None if not notification-worthy."""
t = (action_text or "").lower()
return any(kw in t for kw in _MILESTONE_KEYWORDS)
def is_referral_action(action_text: str) -> bool:
t = (action_text or "").lower()
return any(kw in t for kw in _REFERRAL_KEYWORDS)
if any(kw in t for kw in _VOTE_KW): return "vote"
if any(kw in t for kw in _PRES_KW): return "presidential"
if any(kw in t for kw in _COMMITTEE_KW): return "committee_report"
if any(kw in t for kw in _CALENDAR_KW): return "calendar"
if any(kw in t for kw in _PROCEDURAL_KW): return "procedural"
if any(kw in t for kw in _REFERRAL_KW): return "referral"
return None
def _build_payload(
bill, action_summary: str, milestone_tier: str = "progress", source: str = "bill_follow"
bill, action_summary: str, action_category: str, source: str = "bill_follow"
) -> dict:
from app.config import settings
base_url = (settings.PUBLIC_URL or settings.LOCAL_URL).rstrip("/")
@@ -45,7 +37,9 @@ def _build_payload(
"bill_label": f"{bill.bill_type.upper()} {bill.bill_number}",
"brief_summary": (action_summary or "")[:300],
"bill_url": f"{base_url}/bills/{bill.bill_id}",
"milestone_tier": milestone_tier,
"action_category": action_category,
# kept for RSS/history backwards compat
"milestone_tier": "referral" if action_category == "referral" else "progress",
"source": source,
}
@@ -62,7 +56,7 @@ def _is_duplicate(db, user_id: int, bill_id: str, event_type: str) -> bool:
def emit_bill_notification(
db, bill, event_type: str, action_summary: str, milestone_tier: str = "progress"
db, bill, event_type: str, action_summary: str, action_category: str = "vote"
) -> int:
"""Create NotificationEvent rows for every user following this bill. Returns count."""
from app.models.follow import Follow
@@ -72,7 +66,7 @@ def emit_bill_notification(
if not followers:
return 0
payload = _build_payload(bill, action_summary, milestone_tier, source="bill_follow")
payload = _build_payload(bill, action_summary, action_category, source="bill_follow")
count = 0
for follow in followers:
if _is_duplicate(db, follow.user_id, bill.bill_id, event_type):
@@ -90,7 +84,7 @@ def emit_bill_notification(
def emit_member_follow_notifications(
db, bill, event_type: str, action_summary: str, milestone_tier: str = "progress"
db, bill, event_type: str, action_summary: str, action_category: str = "vote"
) -> int:
"""Notify users following the bill's sponsor (dedup prevents double-alerts for bill+member followers)."""
if not bill.sponsor_id:
@@ -103,7 +97,7 @@ def emit_member_follow_notifications(
if not followers:
return 0
payload = _build_payload(bill, action_summary, milestone_tier, source="member_follow")
payload = _build_payload(bill, action_summary, action_category, source="member_follow")
count = 0
for follow in followers:
if _is_duplicate(db, follow.user_id, bill.bill_id, event_type):
@@ -122,7 +116,7 @@ def emit_member_follow_notifications(
def emit_topic_follow_notifications(
db, bill, event_type: str, action_summary: str, topic_tags: list,
milestone_tier: str = "progress",
action_category: str = "vote",
) -> int:
"""Notify users following any of the bill's topic tags."""
if not topic_tags:
@@ -143,7 +137,7 @@ def emit_topic_follow_notifications(
if not followers:
return 0
payload = _build_payload(bill, action_summary, milestone_tier, source="topic_follow")
payload = _build_payload(bill, action_summary, action_category, source="topic_follow")
count = 0
for follow in followers:
if _is_duplicate(db, follow.user_id, bill.bill_id, event_type):