diff --git a/app/api/v1/endpoints/notify.py b/app/api/v1/endpoints/notify.py index 3aa6b1f..5d85d6e 100644 --- a/app/api/v1/endpoints/notify.py +++ b/app/api/v1/endpoints/notify.py @@ -1,24 +1,27 @@ from __future__ import annotations import logging +import uuid -from fastapi import APIRouter, Depends, HTTPException +from fastapi import APIRouter, BackgroundTasks, HTTPException from sqlmodel import Session, select from supabase import create_client from app.core.config import settings -from app.core.database import get_session +from app.core.database import engine from app.core.email import send_email from app.models.email_subscriber import EmailSubscriber +from app.models.notification_log import NotificationLog router = APIRouter() logger = logging.getLogger(__name__) -HIGH_RISK_THRESHOLD = 1.0 # TODO: restore to 7.0 after testing +HIGH_RISK_THRESHOLD = 7.0 +MEDIUM_RISK_THRESHOLD = 4.0 -def _build_alert_html(narratives: list[dict]) -> str: - """Build an HTML email body summarizing high-risk narratives.""" +def _build_alert_html(narratives: list[dict], risk_label: str = "High") -> str: + """Build an HTML email body summarizing risk narratives.""" rows = "" td = 'style="padding:8px;border:1px solid #ddd"' td_risk = 'style="padding:8px;border:1px solid #ddd;color:#dc2626;font-weight:bold"' @@ -38,8 +41,8 @@ def _build_alert_html(narratives: list[dict]) -> str: return ( "" - '

New High-Risk Narrative Alerts

' - f"

{len(narratives)} new high-risk narrative(s) detected.

" + f'

New {risk_label}-Risk Narrative Alerts

' + f"

{len(narratives)} new {risk_label.lower()}-risk narrative(s) detected.

" '' '' '' @@ -53,67 +56,144 @@ def _build_alert_html(narratives: list[dict]) -> str: ) -@router.post("/alerts/notify") -def send_alert_notifications(session: Session = Depends(get_session)): - """Called by the pipeline after a run to email high-risk narratives.""" - if not settings.BREVO_API_KEY: - raise HTTPException(status_code=503, detail="BREVO_API_KEY not configured") +def _send_notifications_background() -> None: + """Background task: fetch new narratives and send per-subscriber digest emails.""" + try: + if not settings.SUPABASE_URL or not settings.SUPABASE_SERVICE_ROLE_KEY: + logger.error("Supabase not configured") + return - if not settings.SUPABASE_URL or not settings.SUPABASE_SERVICE_ROLE_KEY: - raise HTTPException(status_code=503, detail="Supabase not configured") + sb = create_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_ROLE_KEY) - sb = create_client(settings.SUPABASE_URL, settings.SUPABASE_SERVICE_ROLE_KEY) + # Fetch all narratives at or above the medium-risk threshold in one query + raw_narratives = ( + sb.table("narratives") + .select( + "narrative_id, narrative_label, narrative_category, " + "narrative_risk_score, narrative_description" + ) + .gte("narrative_risk_score", MEDIUM_RISK_THRESHOLD) + .execute() + .data + or [] + ) - # Fetch all high-risk narratives - narratives = ( - sb.table("narratives") - .select( - "narrative_id, narrative_label, narrative_category, " - "narrative_risk_score, narrative_description" + if not raw_narratives: + logger.info("No medium-or-high-risk narratives found") + return + + # Classify each narrative as "High" or "Medium" + for n in raw_narratives: + score = float(n.get("narrative_risk_score") or 0) + n["risk_level"] = "High" if score >= HIGH_RISK_THRESHOLD else "Medium" + + with Session(engine) as session: + # Fetch all active subscribers + subscribers = session.exec( + select(EmailSubscriber).where(EmailSubscriber.is_active == True) # noqa: E712 + ).all() + + if not subscribers: + logger.info("No active subscribers") + return + + # Bulk-query notification_log for already-sent (narrative_id, email) pairs + narrative_ids = [ + uuid.UUID(n["narrative_id"]) + for n in raw_narratives + if n.get("narrative_id") + ] + + already_sent: set[tuple[str, str]] = set() + if narrative_ids: + already_sent_rows = session.exec( + select(NotificationLog).where( + NotificationLog.narrative_id.in_(narrative_ids) # type: ignore[union-attr] + ) + ).all() + already_sent = { + (str(row.narrative_id), row.recipient_email) + for row in already_sent_rows + } + + sent_count = 0 + for subscriber in subscribers: + # Filter narratives for this subscriber based on preferences and dedup + to_send: list[dict] = [] + for n in raw_narratives: + risk_level = n["risk_level"] + + if risk_level == "High" and not subscriber.notify_high_risk: + continue + if risk_level == "Medium" and not subscriber.notify_medium_risk: + continue + + if (n["narrative_id"], subscriber.email) in already_sent: + continue + + to_send.append(n) + + if not to_send: + continue + + # Build subject line based on risk levels present + levels_present = {n["risk_level"] for n in to_send} + if levels_present == {"High", "Medium"}: + risk_label = "High & Medium" + else: + risk_label = next(iter(levels_present)) + + subject = ( + f"[Alert] {len(to_send)} {risk_label}-Risk Narrative(s) Detected" + ) + html = _build_alert_html(to_send, risk_label=risk_label) + + try: + send_email( + to_email=subscriber.email, + to_name=subscriber.email, + subject=subject, + html_content=html, + sender_email=settings.ALERT_SENDER_EMAIL, + sender_name=settings.ALERT_SENDER_NAME, + ) + + # Log each sent narrative only after a confirmed successful send + for n in to_send: + session.add( + NotificationLog( + narrative_id=uuid.UUID(n["narrative_id"]), + recipient_email=subscriber.email, + ) + ) + session.commit() + sent_count += 1 + + except Exception as exc: + logger.error( + "Failed to send alert email to %s: %s", subscriber.email, exc + ) + session.rollback() + + logger.info( + "Sent digest to %d/%d subscribers (%d narratives in scope)", + sent_count, + len(subscribers), + len(raw_narratives), ) - .gte("narrative_risk_score", HIGH_RISK_THRESHOLD) - .execute() - .data - or [] - ) - if not narratives: - return {"ok": True, "detail": "No high-risk narratives found", "notified": 0} - - # Fetch active subscribers from the database - subscribers = session.exec( - select(EmailSubscriber).where(EmailSubscriber.is_active == True) # noqa: E712 - ).all() - recipients = [s.email for s in subscribers] - - if not recipients: - return {"ok": True, "detail": "No active subscribers", "notified": 0} - - # Build a single digest email - html = _build_alert_html(narratives) - subject = f"[Alert] {len(narratives)} High-Risk Narrative(s) Detected" - - sent_count = 0 - errors = [] - - for email_addr in recipients: - try: - send_email( - to_email=email_addr, - to_name=email_addr, - subject=subject, - html_content=html, - sender_email=settings.ALERT_SENDER_EMAIL, - sender_name=settings.ALERT_SENDER_NAME, - ) - sent_count += 1 - except Exception as exc: - logger.error("Failed to send alert email to %s: %s", email_addr, exc) - errors.append({"email": email_addr, "error": str(exc)}) - - return { - "ok": sent_count > 0, - "detail": f"Sent digest to {sent_count}/{len(recipients)} recipients", - "notified": len(narratives), - "errors": errors, - } + except Exception as exc: + logger.error("Error in background notification task: %s", exc) + + +@router.post("/alerts/notify") +def send_alert_notifications(background_tasks: BackgroundTasks): + """Called by the database webhook to trigger email notifications. + + Returns immediately and sends emails in the background. + """ + if not settings.BREVO_API_KEY: + raise HTTPException(status_code=503, detail="BREVO_API_KEY not configured") + + background_tasks.add_task(_send_notifications_background) + return {"ok": True, "detail": "Notification task queued"} diff --git a/app/core/config.py b/app/core/config.py index 4938986..cc6fce4 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -51,6 +51,10 @@ class Settings(BaseSettings): LLM_PROVIDER: str = "" LLM_MODEL: str = "" + BREVO_API_KEY: str = "" + ALERT_SENDER_EMAIL: str = "noreply@yourdomain.com" + ALERT_SENDER_NAME: str = "YouTube Intelligence" + @property def youtube_api_key(self) -> str: return self.YOUTUBE_API_KEY or self.YOUTUBE_DATA_API_KEY diff --git a/app/models/__init__.py b/app/models/__init__.py index 0fec92d..d1dc4db 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -2,6 +2,7 @@ from app.models.claim_narrative import ClaimNarrative from app.models.email_subscriber import EmailSubscriber from app.models.narrative import Narrative +from app.models.notification_log import NotificationLog from app.models.transcript import Transcript from app.models.video import Video @@ -12,4 +13,5 @@ "Narrative", "ClaimNarrative", "EmailSubscriber", + "NotificationLog", ]
Narrative