Files
silo-server/migrations/sql/20260611150000_web_push_subscriptions.sql
QuickandClaude Fable 5 b091f0c6c1 feat(notifications): in-app inbox, realtime, webhooks, web push + shared SMTP core
Implements the notification system foundation and all v1 delivery channels
that need no external infrastructure (specs 00/01/04/05 in
docs/superpowers/plans/notifications/):

Foundation (spec 01):
- episode_availability seeding + per-library seed markers: "newly available"
  means newly released to this server, so back-catalog imports and first
  scans never flood (verified on dev: 1.13M episodes seeded silently)
- release_events -> profile_series_interest fanout worker with settling
  delay, per-series burst caps, FOR UPDATE SKIP LOCKED multi-node claims,
  and a guarded last-notified cursor
- interest index maintained via a userstore provider decorator so every
  favorites/watchlist/progress mutation path (REST, jellycompat, imports,
  playback) feeds it; progress writes only recompute on state transitions
- durable per-profile inbox + read state, forward-sync cursor API,
  websocket channel with short-lived single-use handshake tickets
- web UI: sidebar badge, inbox page, toasts, per-profile preferences
- startup/daily tasks: availability seeding, interest rebuild, retention

Outbound webhooks (spec 04):
- Discord embeds (text-only per the v1 privacy contract) and generic
  JSON signed Stripe-style with per-webhook secrets
- HTTPS-only + private-destination guard enforced at registration and at
  connect time (DNS-rebinding mitigation); URLs/secrets encrypted at rest
- durable per-target outbox enqueued in the fanout transaction, lease-based
  claims, 24h exponential retry, 3x-consecutive-4xx auto-disable with an
  in-app notice (loop-guarded)

Web push (spec 05):
- VAPID keypair self-provisioned at startup (single atomic JSON setting,
  private half encrypted at rest) — no third-party accounts needed
- payloads E2E-encrypted (RFC 8291); 404/410 treated as unsubscribe
- service worker + subscribe flow in Settings -> Notifications

Shared SMTP core (internal/mail):
- feature-agnostic mail.Sender over live email.* settings, STARTTLS or
  implicit TLS, encrypted password, admin Email settings page with
  synchronous test send; no consumer yet by design (digest is v1.5)

APNs/FCM (specs 02/03) are deferred to v2; the capability endpoint reports
them unavailable so clients render truthfully.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 14:55:46 -04:00

66 lines
2.9 KiB
SQL

-- +goose Up
-- +goose StatementBegin
-- Web Push channel for release notifications. Browser PushSubscriptions are
-- profile-scoped; payloads are end-to-end encrypted (RFC 8291) so the
-- browser vendor's push service never sees notification content. profile_id
-- has no FK: profiles may live in per-user SQLite stores; deletion cleans up
-- in code.
CREATE TABLE public.web_push_subscriptions (
id text PRIMARY KEY,
user_id integer NOT NULL,
profile_id text NOT NULL,
-- The push-service URL is unique per browser registration. A
-- resubscription from the same browser under a different profile
-- reassigns the row (one endpoint notifies exactly one profile).
endpoint text NOT NULL,
p256dh text NOT NULL,
auth text NOT NULL,
device_name varchar(128) NOT NULL DEFAULT '',
enabled boolean NOT NULL DEFAULT true,
consecutive_failures integer NOT NULL DEFAULT 0,
last_success_at timestamptz,
last_failure_at timestamptz,
last_failure_status integer,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT web_push_subscriptions_endpoint_key UNIQUE (endpoint)
);
CREATE INDEX web_push_subscriptions_profile_idx
ON public.web_push_subscriptions (profile_id);
CREATE INDEX web_push_subscriptions_profile_enabled_idx
ON public.web_push_subscriptions (profile_id)
WHERE enabled;
-- Durable dispatch outbox + retry state, mirroring webhook_delivery_attempts:
-- `pending` rows are enqueued in the fanout transaction, claimed post-commit,
-- and swept by the retry worker after a crash.
CREATE TABLE public.web_push_delivery_attempts (
id text PRIMARY KEY,
notification_delivery_id text NOT NULL REFERENCES public.notification_deliveries(id) ON DELETE CASCADE,
subscription_id text NOT NULL REFERENCES public.web_push_subscriptions(id) ON DELETE CASCADE,
attempt_number integer NOT NULL,
attempted_at timestamptz NOT NULL DEFAULT now(),
next_retry_at timestamptz,
http_status integer,
outcome text NOT NULL,
failure_message varchar(256),
CONSTRAINT web_push_delivery_attempts_unique UNIQUE (subscription_id, notification_delivery_id, attempt_number),
CONSTRAINT web_push_delivery_attempts_outcome_check CHECK (outcome IN ('pending', 'delivered', 'retrying', 'failed'))
);
CREATE INDEX web_push_delivery_attempts_retry_idx
ON public.web_push_delivery_attempts (outcome, next_retry_at);
-- Serves the per-delivery claim (ClaimPendingForDelivery) and, critically,
-- the ON DELETE CASCADE from notification_deliveries: without it every
-- retention delete seq-scans this table once per deleted delivery row.
CREATE INDEX web_push_delivery_attempts_delivery_idx
ON public.web_push_delivery_attempts (notification_delivery_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS public.web_push_delivery_attempts;
DROP TABLE IF EXISTS public.web_push_subscriptions;
-- +goose StatementEnd