Compare commits
33
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e006553335 | ||
|
|
2947d58836 | ||
|
|
13c5e0ba2f | ||
|
|
88aa769fe6 | ||
|
|
5afe56cfc0 | ||
|
|
3e77f39d24 | ||
|
|
6f9427d613 | ||
|
|
bcf0253c09 | ||
|
|
91c7eb364d | ||
|
|
f4594e902d | ||
|
|
9508449859 | ||
|
|
4e521ed2e8 | ||
|
|
1e3780d4fb | ||
|
|
91e987bcda | ||
|
|
22af261786 | ||
|
|
799754dde7 | ||
|
|
d2ccbcb1a6 | ||
|
|
3d2368aed7 | ||
|
|
beb6b880fc | ||
|
|
b83df36636 | ||
|
|
88ddd2a406 | ||
|
|
ebf3352bda | ||
|
|
5f05374a1d | ||
|
|
27b9006ef5 | ||
|
|
df95e3cb95 | ||
|
|
e5b210589d | ||
|
|
4274220266 | ||
|
|
d9e27da59e | ||
|
|
4fd21c7aee | ||
|
|
b091f0c6c1 | ||
|
|
a24279e3e3 | ||
|
|
a0f7810481 | ||
|
|
adeda3c87c |
@@ -42,6 +42,7 @@ POSTGRES_SHM_SIZE=8gb
|
||||
|
||||
# Optional host port overrides for docker-compose.yml.
|
||||
# PORT=8090
|
||||
# Used only after Jellyfin-compatible app support is enabled by an admin.
|
||||
# JF_PORT=8096
|
||||
# PROXY_PORT=8083
|
||||
# TRANSCODE_PORT=8082
|
||||
@@ -59,6 +60,9 @@ POSTGRES_SHM_SIZE=8gb
|
||||
# docs/architecture/secret-encryption.md.
|
||||
# SECRET_KEY=replace-with-output-of-openssl-rand-base64-48
|
||||
|
||||
# Optional public URL for Silo. If not set, the server will use the IP address of the container.
|
||||
# SILO_PUBLIC_URL=https://silo.example.com
|
||||
|
||||
# Run from source / advanced overrides
|
||||
# Only DATABASE_URL is required when running Silo outside the default docker compose stack.
|
||||
# DATABASE_URL=postgres://silo:password@localhost:5432/silo
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# v1 scope lock — changes to the lock artifact require maintainer review
|
||||
/docs/architecture/v1-scope.md @Quick104
|
||||
@@ -0,0 +1,56 @@
|
||||
name: v1 capability proposal
|
||||
description: Propose a user-facing capability for the Silo v1 scope lock
|
||||
title: "[v1] <capability name>"
|
||||
labels: ["v1-proposed"]
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
One proposal per **user-facing capability** (e.g. "Manga library type", "Transcoded playback"),
|
||||
not per PR or per endpoint. Locked capabilities are what the client teams plan against —
|
||||
the **API surface** section is mandatory for a capability to be lockable.
|
||||
Scope state and lock rules: `docs/architecture/v1-scope.md`.
|
||||
- type: textarea
|
||||
id: summary
|
||||
attributes:
|
||||
label: Summary
|
||||
description: One paragraph, user-facing language. What can a user do when this ships?
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: value
|
||||
attributes:
|
||||
label: User value / why v1
|
||||
description: Why does v1 need this rather than v1.1?
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: api-surface
|
||||
attributes:
|
||||
label: API surface
|
||||
description: >
|
||||
Endpoints/payloads clients will call (method + path + notable request/response fields).
|
||||
Plain list now; OpenAPI pointers once the #135 tooling lands. A capability cannot be
|
||||
Locked without this section filled in.
|
||||
placeholder: |
|
||||
GET /api/v1/manga/{seriesId}/chapters — list chapters (id, index, volume, read_state)
|
||||
POST /api/v1/watch-progress — records reading progress (existing endpoint, new item type)
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
id: client-impact
|
||||
attributes:
|
||||
label: Client impact (android / apple / web)
|
||||
description: What must each client build to surface this capability? "None" is a valid answer per client.
|
||||
validations:
|
||||
required: true
|
||||
- type: dropdown
|
||||
id: size
|
||||
attributes:
|
||||
label: Rough size (server-side)
|
||||
options:
|
||||
- S
|
||||
- M
|
||||
- L
|
||||
validations:
|
||||
required: true
|
||||
@@ -0,0 +1,26 @@
|
||||
name: v1 proposal labeler
|
||||
|
||||
# The issue form applies `v1-proposed` automatically, but only for submissions
|
||||
# through the web form. Contributors filing via the API/CLI (or from a fork)
|
||||
# cannot apply labels without triage/write access, so their proposals would land
|
||||
# unlabeled and never auto-add to the Silo v1 project. This stamps `v1-proposed`
|
||||
# on any issue whose title starts with `[v1]`, regardless of who filed it.
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [opened]
|
||||
|
||||
permissions:
|
||||
issues: write
|
||||
|
||||
jobs:
|
||||
label:
|
||||
if: startsWith(github.event.issue.title, '[v1]')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Apply v1-proposed
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
ISSUE_NUMBER: ${{ github.event.issue.number }}
|
||||
REPO: ${{ github.repository }}
|
||||
run: gh issue edit "$ISSUE_NUMBER" --repo "$REPO" --add-label v1-proposed
|
||||
+2
-1
@@ -20,6 +20,7 @@ coverage.html
|
||||
# Temp
|
||||
/tmp/
|
||||
*.tmp
|
||||
.local/
|
||||
|
||||
# Worktrees
|
||||
.worktrees/
|
||||
@@ -40,6 +41,7 @@ go.work.sum
|
||||
|
||||
# Frontend
|
||||
web/node_modules/
|
||||
.pnpm-store/
|
||||
web/dist/
|
||||
plugins/metadb/dist/
|
||||
web/*.tsbuildinfo
|
||||
@@ -57,6 +59,5 @@ node_modules/
|
||||
logs/
|
||||
.playwright-mcp/
|
||||
demo/
|
||||
docker-compose.dev.yml
|
||||
.playwright-cli/
|
||||
output/
|
||||
|
||||
@@ -88,3 +88,21 @@ Recent history follows Conventional Commit-style subjects such as `feat(playback
|
||||
- Silo separates login accounts (`users`) from household profiles; multiple profiles on one account share the same `user_id`.
|
||||
- Profile `is_primary` marks the household parent for that account; it is not the same as server-wide `admin` role on the user account.
|
||||
- Point the Vite dev frontend at a remote backend with `VITE_API_PROXY_TARGET` in `web/.env.local` (or inline) when running `make dev-frontend`; the frontend calls relative `/api` URLs proxied by Vite.
|
||||
|
||||
## Silo v1 Process (scope lock & API rules)
|
||||
|
||||
Silo is working toward a locked v1 feature set so client teams can plan against a stable core. These rules apply to every change.
|
||||
|
||||
**Scope gate.** Before implementing a new user-facing capability, check `docs/architecture/v1-scope.md`. If the capability is not listed there — or the file says scope is not yet locked — do NOT open a feature PR. File a **v1 capability proposal** issue (template in this repo) and wait for triage. Bug fixes, refactors, and work on already-locked capabilities proceed normally.
|
||||
|
||||
**API rules — additive-only within `/api/v1`.**
|
||||
- Never rename or remove a response field, change a field's type, or repurpose a status code on an existing endpoint.
|
||||
- New functionality adds new fields/endpoints; removals go through the existing Deprecation/Sunset header flow only.
|
||||
- New features expose capability endpoints (feature detection) rather than relying on version sniffing.
|
||||
- Contract strategy and tooling: issue #135.
|
||||
|
||||
**PR requirements.**
|
||||
- Link the capability epic or sub-issue the PR serves (`Part of #NNN`). PRs with no linked scope item will be questioned at review.
|
||||
- One concern per PR; Conventional Commit subject; AI-use disclosure in the PR body.
|
||||
|
||||
**Pre-push checklist.** `make lint` · `cd web && pnpm run lint && pnpm run format:check` · `make verify-local-paths` · Go tests in a libvips-capable container (a bare-host `go test ./...` silently skips CGO packages, including `internal/api/handlers`).
|
||||
|
||||
+6
-3
@@ -50,12 +50,15 @@ RUN apt-get update && \
|
||||
echo "deb [signed-by=/usr/share/keyrings/jellyfin.gpg arch=${TARGETARCH}] https://repo.jellyfin.org/debian bookworm main" \
|
||||
> /etc/apt/sources.list.d/jellyfin.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y --no-install-recommends jellyfin-ffmpeg7 libvips42 fonts-noto-core fonts-noto-cjk && \
|
||||
apt-get install -y --no-install-recommends jellyfin-ffmpeg7 git libvips42 fonts-noto-core fonts-noto-cjk && \
|
||||
apt-get purge -y gnupg && apt-get autoremove -y && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
RUN mkdir -p /tmp/silo-transcode
|
||||
RUN mkdir -p /tmp/silo-transcode /var/lib/silo/compat/jellyfin-web
|
||||
COPY --from=frontend /usr/local/bin/node /usr/local/bin/node
|
||||
COPY --from=frontend /usr/local/lib/node_modules/npm /usr/local/lib/node_modules/npm
|
||||
RUN ln -sf ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
|
||||
ln -sf ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
|
||||
COPY --from=build /silo /usr/local/bin/silo
|
||||
COPY third_party/jellyfin-web/ /srv/jellyfin-web/
|
||||
EXPOSE 8080 8096 13378
|
||||
HEALTHCHECK --interval=15s --timeout=5s --start-period=10s --retries=3 \
|
||||
CMD curl -f http://localhost:${PORT:-8080}/api/v1/health || exit 1
|
||||
|
||||
+7
-4
@@ -55,13 +55,16 @@ RUN apt-get update && \
|
||||
echo "deb [signed-by=/usr/share/keyrings/jellyfin.gpg arch=${TARGETARCH}] https://repo.jellyfin.org/debian bookworm main" \
|
||||
> /etc/apt/sources.list.d/jellyfin.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y --no-install-recommends jellyfin-ffmpeg7 libvips42 fonts-noto-core fonts-noto-cjk && \
|
||||
apt-get install -y --no-install-recommends jellyfin-ffmpeg7 git libvips42 fonts-noto-core fonts-noto-cjk && \
|
||||
apt-get purge -y gnupg && apt-get autoremove -y && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
RUN mkdir -p /tmp/silo-transcode
|
||||
RUN mkdir -p /tmp/silo-transcode /var/lib/silo/compat/jellyfin-web
|
||||
COPY --from=frontend /usr/local/bin/node /usr/local/bin/node
|
||||
COPY --from=frontend /usr/local/lib/node_modules/npm /usr/local/lib/node_modules/npm
|
||||
RUN ln -sf ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
|
||||
ln -sf ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
|
||||
COPY --from=build /silo /usr/local/bin/silo
|
||||
COPY third_party/jellyfin-web/ /srv/jellyfin-web/
|
||||
EXPOSE 8080 8080 8096
|
||||
EXPOSE 8080 8096 13378
|
||||
HEALTHCHECK --interval=15s --timeout=5s --start-period=10s --retries=3 \
|
||||
CMD curl -f http://localhost:${PORT:-8080}/api/v1/health || exit 1
|
||||
ENTRYPOINT ["silo"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.PHONY: frontend build dev-frontend dev-backend dev-proxy dev-transcode lint clean jellyfin-web-bundle migrate-continuum-check verify-local-paths install-hooks migrate-create migrate-validate migrate-status migrate-up
|
||||
.PHONY: frontend build dev-frontend dev-backend dev-proxy dev-transcode lint clean jellyfin-web migrate-continuum-check verify-local-paths install-hooks migrate-create migrate-validate migrate-status migrate-up
|
||||
|
||||
GIT_COMMON_DIR := $(strip $(shell git rev-parse --git-common-dir 2>/dev/null))
|
||||
MAIN_CHECKOUT_ROOT := $(if $(GIT_COMMON_DIR),$(abspath $(GIT_COMMON_DIR)/..))
|
||||
@@ -15,7 +15,8 @@ else ifneq ($(wildcard $(SHARED_PLUGIN_SDK_DIR)),)
|
||||
DEV_PLUGIN_SDK_DIR ?= $(SHARED_PLUGIN_SDK_DIR)
|
||||
endif
|
||||
|
||||
JELLYFIN_WEB_OUTPUT_DIR ?= third_party/jellyfin-web
|
||||
JELLYFIN_WEB_INSTALL_DIR ?= .local/compat/jellyfin-web
|
||||
JELLYFIN_WEB_VERSION ?= 10.11.6
|
||||
|
||||
# Build version stamping: inject the git revision so the admin Build panel shows a
|
||||
# version even when Go's VCS metadata isn't embedded (mirrors the Dockerfile ldflags).
|
||||
@@ -82,9 +83,9 @@ install-hooks:
|
||||
fi
|
||||
git config core.hooksPath .githooks
|
||||
|
||||
# Fetch and build the pinned Jellyfin Web bundle
|
||||
jellyfin-web-bundle:
|
||||
JELLYFIN_WEB_OUTPUT_DIR=$(JELLYFIN_WEB_OUTPUT_DIR) scripts/fetch-jellyfin-web.sh
|
||||
# Fetch and build the pinned Jellyfin Web component into a gitignored local cache.
|
||||
jellyfin-web:
|
||||
go run ./cmd/silo/ compat-web install --dir "$(JELLYFIN_WEB_INSTALL_DIR)" --version "$(JELLYFIN_WEB_VERSION)"
|
||||
|
||||
# Read-only preflight for Continuum Docker installs moving to Silo.
|
||||
migrate-continuum-check:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Silo
|
||||
|
||||
A self-hosted media streaming server with a React frontend and Go backend. Supports direct play, remuxing, and hardware-accelerated transcoding. Compatible with Jellyfin/Emby clients (VidHub, Findroid) via a built-in compatibility layer.
|
||||
A self-hosted media streaming server with a React frontend and Go backend. Supports direct play, remuxing, and hardware-accelerated transcoding. Includes optional Jellyfin/Emby-compatible app support for clients such as VidHub and Findroid.
|
||||
|
||||
Join the community on [Discord](https://discord.gg/4RxuUQAEnW).
|
||||
|
||||
@@ -30,7 +30,7 @@ The easiest way to run Silo is with Docker Compose. The default stack assumes yo
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
This starts PostgreSQL, Redis, and the integrated Silo server. The app is available at `http://localhost:8090` and the Jellyfin-compatible endpoint at `http://localhost:8096`.
|
||||
This starts PostgreSQL, Redis, and the integrated Silo server. The app is available at `http://localhost:8090`. Jellyfin-compatible app support is disabled until an administrator enables it in onboarding or admin settings.
|
||||
|
||||
If you already have PostgreSQL and Redis available, omit those bundled service examples from compose and point Silo at your existing `DATABASE_URL` and `REDIS_URL` instead.
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ material needed for Silo server builds.
|
||||
|
||||
## Jellyfin Web
|
||||
|
||||
`third_party/jellyfin-web/10.11.6/` contains a vendored Jellyfin Web build used
|
||||
for Jellyfin compatibility surfaces. Upstream package notices generated by that
|
||||
build are preserved next to the corresponding bundles as `*.LICENSE.txt` files.
|
||||
Keep those notices with the vendored files when updating or redistributing this
|
||||
directory.
|
||||
Silo does not bundle Jellyfin Web in this repository or in the default runtime
|
||||
image. Administrators can explicitly install Jellyfin Web as a separate
|
||||
compatibility component with `silo compat-web install` or the admin settings UI.
|
||||
The installer records the upstream source URL, tag, commit SHA, checksum,
|
||||
license, and source/provenance metadata beside the installed assets.
|
||||
|
||||
## Collection Template Posters
|
||||
|
||||
@@ -17,4 +17,3 @@ directory.
|
||||
poster artwork. The artwork is intended to use generic original scenes and
|
||||
avoid copyrighted movie/show posters, recognizable actors, franchise
|
||||
characters, provider logos, and readable in-image third-party branding.
|
||||
|
||||
|
||||
+144
-49
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"log/slog"
|
||||
@@ -40,6 +42,7 @@ import (
|
||||
"github.com/Silo-Server/silo-server/internal/audiobooks/podcastfeed"
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
"github.com/Silo-Server/silo-server/internal/autoscan"
|
||||
"github.com/Silo-Server/silo-server/internal/branding"
|
||||
"github.com/Silo-Server/silo-server/internal/cache"
|
||||
"github.com/Silo-Server/silo-server/internal/catalog"
|
||||
"github.com/Silo-Server/silo-server/internal/catalogseed"
|
||||
@@ -56,6 +59,7 @@ import (
|
||||
"github.com/Silo-Server/silo-server/internal/libraryingest"
|
||||
"github.com/Silo-Server/silo-server/internal/logfilter"
|
||||
"github.com/Silo-Server/silo-server/internal/logstream"
|
||||
"github.com/Silo-Server/silo-server/internal/mail"
|
||||
"github.com/Silo-Server/silo-server/internal/markers"
|
||||
"github.com/Silo-Server/silo-server/internal/mdblist"
|
||||
"github.com/Silo-Server/silo-server/internal/metadata"
|
||||
@@ -69,9 +73,7 @@ import (
|
||||
"github.com/Silo-Server/silo-server/internal/playback"
|
||||
"github.com/Silo-Server/silo-server/internal/pluginhost"
|
||||
"github.com/Silo-Server/silo-server/internal/plugins"
|
||||
"github.com/Silo-Server/silo-server/internal/presence"
|
||||
"github.com/Silo-Server/silo-server/internal/proxy"
|
||||
"github.com/Silo-Server/silo-server/internal/push"
|
||||
"github.com/Silo-Server/silo-server/internal/ratelimit"
|
||||
"github.com/Silo-Server/silo-server/internal/recommendations"
|
||||
mediarequests "github.com/Silo-Server/silo-server/internal/requests"
|
||||
@@ -167,7 +169,10 @@ func configureOperationalLogging(
|
||||
}
|
||||
opsPM := partman.NewManager(pool, "operational_logs", partman.Daily, 3)
|
||||
if err := opsPM.EnsureFuturePartitions(ctx); err != nil {
|
||||
log.Fatalf("ensure operational log partitions: %v", err)
|
||||
// Non-fatal: a partition hiccup must not crash-loop the server (see the
|
||||
// operational_logs partition incident). Writes fall back to the default
|
||||
// partition and the periodic cleanup retries EnsureFuturePartitions.
|
||||
slog.Warn("ensure operational log partitions; continuing in degraded mode", "error", err)
|
||||
}
|
||||
|
||||
var operationalWriter opslog.Writer
|
||||
@@ -297,7 +302,56 @@ func runCredentialBackfills(ctx context.Context, pool *pgxpool.Pool, cipher *sec
|
||||
}
|
||||
}
|
||||
|
||||
func runCompatWebCommand(ctx context.Context, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("usage: silo compat-web {status|install|update|remove}")
|
||||
}
|
||||
command := args[0]
|
||||
flags := flag.NewFlagSet("compat-web "+command, flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
root := flags.String("dir", config.DefaultJellyfinWebInstallDir, "Jellyfin Web component install root")
|
||||
version := flags.String("version", config.DefaultJellyfinWebVersion, "Jellyfin Web version without leading v")
|
||||
source := flags.String("source", jellycompat.DefaultWebSourceURL, "upstream jellyfin-web git repository")
|
||||
if err := flags.Parse(args[1:]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch command {
|
||||
case "status":
|
||||
status := jellycompat.WebComponentStatusForConfig(&config.Config{
|
||||
JellyfinCompat: config.JellyfinCompatConfig{
|
||||
Enabled: false,
|
||||
WebVersion: *version,
|
||||
WebInstallDir: *root,
|
||||
WebDir: filepath.Join(*root, "current"),
|
||||
},
|
||||
}, map[string]string{
|
||||
"jellyfin_compat.web_source_url": *source,
|
||||
})
|
||||
return json.NewEncoder(os.Stdout).Encode(status)
|
||||
case "install", "update":
|
||||
status, err := jellycompat.InstallWebComponent(ctx, jellycompat.WebComponentInstallOptions{
|
||||
InstallRoot: *root,
|
||||
SourceURL: *source,
|
||||
Version: *version,
|
||||
})
|
||||
_ = json.NewEncoder(os.Stdout).Encode(status)
|
||||
return err
|
||||
case "remove":
|
||||
return jellycompat.RemoveWebComponent(*root)
|
||||
default:
|
||||
return fmt.Errorf("unknown compat-web command %q", command)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "compat-web" {
|
||||
if err := runCompatWebCommand(context.Background(), os.Args[2:]); err != nil {
|
||||
log.Fatalf("compat-web: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
envFile := flag.String("env", ".env", "path to .env bootstrap file")
|
||||
migrateOnly := flag.Bool("migrate-only", false, "apply database migrations and exit")
|
||||
migrateStatus := flag.Bool("migrate-status", false, "show database migration status and exit")
|
||||
@@ -330,7 +384,7 @@ func main() {
|
||||
slog.Info("connected to PostgreSQL")
|
||||
|
||||
if *migrateStatus {
|
||||
migCtx, migCancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
migCtx, migCancel := database.MigrationContext(ctx)
|
||||
statuses, statusErr := database.MigrationStatuses(migCtx, pool, migrations.FS, "sql")
|
||||
migCancel()
|
||||
if statusErr != nil {
|
||||
@@ -355,7 +409,7 @@ func main() {
|
||||
}
|
||||
|
||||
if *migrateOnly {
|
||||
migCtx, migCancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
migCtx, migCancel := database.MigrationContext(ctx)
|
||||
migErr := database.RunMigrations(migCtx, pool, migrations.FS, "sql")
|
||||
migCancel()
|
||||
if migErr != nil {
|
||||
@@ -373,7 +427,7 @@ func main() {
|
||||
// ciphertext; secondary nodes read whatever the primary encrypted.
|
||||
isPrimaryNode := bc.Mode == "integrated" || bc.Mode == "api" || bc.Mode == ""
|
||||
if isPrimaryNode {
|
||||
migCtx, migCancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
migCtx, migCancel := database.MigrationContext(ctx)
|
||||
if migErr := database.RunMigrations(migCtx, pool, migrations.FS, "sql"); migErr != nil {
|
||||
migCancel()
|
||||
log.Fatalf("failed to run migrations: %v", migErr)
|
||||
@@ -498,17 +552,6 @@ func main() {
|
||||
log.Fatalf("realtime hub start: %v", err)
|
||||
}
|
||||
eventsHub := realtimeHub.EventsHub()
|
||||
var presenceRegistry presence.Registry
|
||||
if cfg.Redis.URL != "" {
|
||||
if rc, err := cache.NewRedisClient(cfg.Redis); err == nil {
|
||||
presenceRegistry = presence.NewRedisRegistry(rc, 60*time.Second)
|
||||
} else {
|
||||
slog.Warn("presence: redis unavailable, falling back to in-memory registry", "error", err)
|
||||
}
|
||||
}
|
||||
if presenceRegistry == nil {
|
||||
presenceRegistry = presence.NewMemoryRegistry()
|
||||
}
|
||||
scanRegistry := evt.NewScanRegistry()
|
||||
operationalWriter, opsRepo, opsPM := configureOperationalLogging(appCtx, pool, settingsRepo, cfg.Redis, logStreamHub, quietFilter, nodeID)
|
||||
defer func() {
|
||||
@@ -608,6 +651,16 @@ func main() {
|
||||
bootstrapSensitiveValues["redis.url"] = bc.RedisURL
|
||||
}
|
||||
|
||||
// Shared Redis client for components needing raw Redis beyond the event
|
||||
// bus (websocket handshake tickets, session listing). Nil on Redis-less
|
||||
// deployments; consumers fall back to in-process implementations.
|
||||
apiRedisClient, apiRedisErr := cache.NewRedisClient(cfg.Redis)
|
||||
if apiRedisErr != nil {
|
||||
slog.Warn("redis client init failed; multi-node websocket tickets disabled", "error", apiRedisErr)
|
||||
} else if apiRedisClient != nil {
|
||||
defer func() { _ = apiRedisClient.Close() }()
|
||||
}
|
||||
|
||||
deps := api.Dependencies{
|
||||
Config: cfg,
|
||||
LiveConfig: configWatcher.Config,
|
||||
@@ -618,6 +671,7 @@ func main() {
|
||||
DB: pool,
|
||||
SecretCipher: dataCipher,
|
||||
EventBus: eventBus,
|
||||
RedisClient: apiRedisClient,
|
||||
LogStreamHub: logStreamHub,
|
||||
RealtimeHub: realtimeHub,
|
||||
EventsHub: eventsHub,
|
||||
@@ -639,30 +693,6 @@ func main() {
|
||||
configWatcher.RequestReload()
|
||||
},
|
||||
}
|
||||
// Notifications subsystem: store → service → materializer.
|
||||
// pool is always non-nil here (proxy/transcode returned early above).
|
||||
notificationsStore := notifications.NewRepository(pool)
|
||||
notificationsSvc := notifications.NewService(notificationsStore, eventsHub)
|
||||
notificationsMaterializer := notifications.NewMaterializer(eventsHub, notificationsSvc, notifications.NewContentResolverRepo(pool))
|
||||
if err := notificationsMaterializer.Start(appCtx); err != nil {
|
||||
log.Fatalf("notifications materializer start: %v", err)
|
||||
}
|
||||
deps.NotificationsService = notificationsSvc
|
||||
deps.PresenceRegistry = presenceRegistry
|
||||
|
||||
// Push subsystem: store → config → enqueuer → transports → worker.
|
||||
pushStore := push.NewStore(pool)
|
||||
pushConfig := push.NewConfig(settingsRepo)
|
||||
notificationsSvc.SetPushEnqueuer(push.NewEnqueuer(pushStore, 30*time.Second, time.Now))
|
||||
pushTransports := []push.Transport{
|
||||
push.NewWebPushTransport(pushConfig.WebPush),
|
||||
push.NewAPNsTransport(pushConfig.APNs),
|
||||
push.NewFCMTransport(pushConfig.FCM),
|
||||
}
|
||||
pushWorker := push.NewWorker(push.NewStoreQueue(pushStore), presenceRegistry, pushTransports, time.Now)
|
||||
deps.PushStore = pushStore
|
||||
deps.PushConfig = pushConfig
|
||||
|
||||
audiobooksService := audiobooks.New(&audiobooksSettingsAdapter{repo: settingsRepo})
|
||||
absCompatEnabled, err := audiobooksService.ABSCompatEnabled(appCtx)
|
||||
if err != nil {
|
||||
@@ -1245,8 +1275,10 @@ func main() {
|
||||
cfg.Scanner.MaxConcurrentLibraries,
|
||||
cfg.Scanner.MaxConcurrentScoped,
|
||||
)
|
||||
libraryScanQueue.Start()
|
||||
defer libraryScanQueue.Stop()
|
||||
// Started below, after the notification system has attached its
|
||||
// availability detector to the executor: a scan resumed by the
|
||||
// workers before that wiring would complete without recording
|
||||
// episode availability, silently losing release notifications.
|
||||
deps.LibraryScanQueue = libraryScanQueue
|
||||
}
|
||||
if deps.DB != nil && deps.FileRepo != nil && metadataService != nil {
|
||||
@@ -1308,6 +1340,48 @@ func main() {
|
||||
}
|
||||
defer userStoreProvider.Close()
|
||||
}
|
||||
|
||||
// User-facing release notifications. The system reads user state through
|
||||
// the raw store provider; the provider handed to everything downstream is
|
||||
// wrapped so every favorites/watchlist/progress mutation (REST handlers,
|
||||
// jellycompat, imports, playback) feeds the interest index.
|
||||
var notificationSystem *notifications.System
|
||||
if deps.DB != nil && userStoreProvider != nil {
|
||||
notificationScopes := access.NewResolver(
|
||||
auth.NewUserRepository(deps.DB),
|
||||
userStoreProvider,
|
||||
access.NewProfileTokenService(cfg.Auth.JWTSecret, 0),
|
||||
)
|
||||
notificationSystem = notifications.NewSystem(
|
||||
deps.DB,
|
||||
settingsRepo,
|
||||
userStoreProvider,
|
||||
notificationScopes,
|
||||
auth.NewUserRepository(deps.DB),
|
||||
deps.EventsHub,
|
||||
deps.RedisClient,
|
||||
deps.SecretCipher,
|
||||
mail.NewSMTPSender(settingsRepo),
|
||||
)
|
||||
userStoreProvider = notifications.WrapUserStoreProvider(userStoreProvider, notificationSystem)
|
||||
deps.Notifications = notificationSystem
|
||||
if libraryIngestExecutor != nil {
|
||||
libraryIngestExecutor.SetAvailabilityDetector(notificationSystem.Detector)
|
||||
}
|
||||
if needsWorkers {
|
||||
notificationSystem.Start(appCtx)
|
||||
defer notificationSystem.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
// Start the scan queue only now that the availability detector (when
|
||||
// notifications are enabled) is attached to the ingest executor, so scans
|
||||
// resumed at startup cannot complete before the detector exists.
|
||||
if libraryScanQueue != nil {
|
||||
libraryScanQueue.Start()
|
||||
defer libraryScanQueue.Stop()
|
||||
}
|
||||
|
||||
if userStoreProvider != nil && pluginService != nil {
|
||||
deps.PluginUserConfig = plugins.NewUserConfigStore(userStoreProvider, pluginService)
|
||||
}
|
||||
@@ -1510,7 +1584,9 @@ func main() {
|
||||
}
|
||||
activityPM := partman.NewManager(pool, "activity_log", partman.Weekly, 2)
|
||||
if err := activityPM.EnsureFuturePartitions(appCtx); err != nil {
|
||||
log.Fatalf("ensure activity log partitions: %v", err)
|
||||
// Non-fatal: see the operational_logs partition incident. Writes fall
|
||||
// back to the default partition and periodic cleanup retries.
|
||||
slog.Warn("ensure activity log partitions; continuing in degraded mode", "error", err)
|
||||
}
|
||||
var activityWriter activitylog.Writer
|
||||
activityConsumer := activitylog.NewConsumer(pool, nil, logStreamHub)
|
||||
@@ -1612,6 +1688,11 @@ func main() {
|
||||
}
|
||||
taskMgr.Register(tasks.NewActivityLogCleanupTask(deps.DB, settingsRepo, activityPM))
|
||||
taskMgr.Register(tasks.NewOperationalLogCleanupTask(deps.DB, settingsRepo, opsPM))
|
||||
if notificationSystem != nil {
|
||||
taskMgr.Register(tasks.NewSeedContentAvailabilityTask(notificationSystem))
|
||||
taskMgr.Register(tasks.NewRebuildReleaseInterestTask(notificationSystem))
|
||||
taskMgr.Register(tasks.NewNotificationsRetentionTask(notificationSystem))
|
||||
}
|
||||
if matchWorker != nil {
|
||||
taskMgr.Register(tasks.NewMatchMediaTask(matchWorker))
|
||||
}
|
||||
@@ -1642,7 +1723,6 @@ func main() {
|
||||
),
|
||||
)
|
||||
requestReconcileSvc.SetRequesterIdentityResolver(plugins.RequesterIdentityFromLookup(plugins.NewPgUserIdentityLookup(deps.DB)))
|
||||
requestReconcileSvc.SetEventsHub(eventsHub)
|
||||
api.AttachRequestRouter(requestReconcileSvc, pluginService)
|
||||
if userStoreProvider != nil {
|
||||
reconcileResolver := access.NewResolver(
|
||||
@@ -1652,6 +1732,9 @@ func main() {
|
||||
)
|
||||
requestReconcileSvc.SetEntitlementResolver(mediarequests.NewAccessEntitlements(reconcileResolver))
|
||||
}
|
||||
if notificationSystem != nil {
|
||||
requestReconcileSvc.SetFulfillmentNotifier(notifications.NewRequestFulfillmentNotifier(notificationSystem))
|
||||
}
|
||||
taskMgr.Register(tasks.NewReconcileRequestsTask(requestReconcileSvc, 100))
|
||||
if deps.FolderRepo != nil && deps.LibraryScanQueue != nil && pluginService != nil && pluginInstallationStore != nil {
|
||||
autoscanRepo := autoscan.NewRepository(deps.DB, deps.SecretCipher)
|
||||
@@ -1701,9 +1784,6 @@ func main() {
|
||||
taskMgr.Register(pluginTask)
|
||||
}
|
||||
}
|
||||
taskMgr.Register(tasks.NewNotificationsRetentionTask(notificationsStore, pushStore))
|
||||
taskMgr.Register(tasks.NewNotificationsDigestTask(notificationsSvc))
|
||||
taskMgr.Register(push.NewPushDeliveryTask(pushWorker))
|
||||
|
||||
taskMgr.Start(appCtx)
|
||||
defer taskMgr.Stop()
|
||||
@@ -1880,6 +1960,21 @@ func main() {
|
||||
}
|
||||
deps.FrontendFS = distFS
|
||||
server.WebDistFS = distFS
|
||||
|
||||
// White-label branding: one service shared by the API (public read + admin
|
||||
// upload) and the frontend handler (index.html title, favicon, manifest).
|
||||
// S3 is optional — pass a nil AssetStore (not the typed-nil *s3client.Client)
|
||||
// when it isn't configured so text branding still works without it.
|
||||
if settingsRepo != nil {
|
||||
var brandingStore branding.AssetStore
|
||||
if deps.S3Public != nil {
|
||||
brandingStore = deps.S3Public
|
||||
}
|
||||
brandingSvc := branding.NewService(settingsRepo, brandingStore)
|
||||
deps.BrandingService = brandingSvc
|
||||
server.Branding = brandingSvc
|
||||
}
|
||||
|
||||
router := api.NewRouter(deps)
|
||||
|
||||
// Step 8: Expose Prometheus metrics endpoint (not behind auth).
|
||||
@@ -1987,7 +2082,7 @@ func main() {
|
||||
}
|
||||
|
||||
var compatSrv *http.Server
|
||||
if (mode == "integrated" || mode == "api") && cfg.JellyfinCompat.Listen != "" {
|
||||
if (mode == "integrated" || mode == "api") && cfg.JellyfinCompat.Enabled && cfg.JellyfinCompat.Listen != "" {
|
||||
compatDeps := jellycompat.Dependencies{
|
||||
Config: cfg,
|
||||
LiveConfig: configWatcher.Config,
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
# Development stack:
|
||||
# docker compose -f docker-compose.dev.yml up --build
|
||||
#
|
||||
# Vite serves the UI at http://localhost:${PORT:-8090} and proxies API
|
||||
# requests to the Air-reloaded Go backend on the compose network.
|
||||
name: silo-dev
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: pgvector/pgvector:pg18
|
||||
restart: unless-stopped
|
||||
shm_size: ${POSTGRES_SHM_SIZE:-2gb}
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-silo}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-silo}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-silo}
|
||||
ports:
|
||||
- "${POSTGRES_PORT:-5432}:5432"
|
||||
volumes:
|
||||
- silo-dev-postgres:/var/lib/postgresql
|
||||
command: ["postgres", "-c", "listen_addresses=*"]
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-silo}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
redis:
|
||||
image: redis:alpine
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "${REDIS_PORT:-6379}:6379"
|
||||
volumes:
|
||||
- silo-dev-redis:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
|
||||
silo:
|
||||
build:
|
||||
context: .
|
||||
dockerfile_inline: |
|
||||
FROM node:22-slim AS node_runtime
|
||||
|
||||
FROM golang:1.26
|
||||
|
||||
ENV CGO_ENABLED=1
|
||||
ENV GOPROXY=direct
|
||||
ENV GOPRIVATE=github.com/Silo-Server/*
|
||||
ENV GONOSUMDB=github.com/Silo-Server/*
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates \
|
||||
curl \
|
||||
ffmpeg \
|
||||
git \
|
||||
libvips-dev \
|
||||
pkg-config \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=node_runtime /usr/local/bin/node /usr/local/bin/node
|
||||
COPY --from=node_runtime /usr/local/lib/node_modules/npm /usr/local/lib/node_modules/npm
|
||||
RUN ln -sf ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
|
||||
ln -sf ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
|
||||
|
||||
RUN go install github.com/air-verse/air@latest
|
||||
|
||||
WORKDIR /app
|
||||
restart: unless-stopped
|
||||
working_dir: /app
|
||||
command:
|
||||
- sh
|
||||
- -c
|
||||
- |
|
||||
mkdir -p tmp web/dist
|
||||
if [ ! -f web/dist/index.html ]; then
|
||||
printf '%s\n' '<!doctype html><html><body><div id="root"></div></body></html>' > web/dist/index.html
|
||||
fi
|
||||
|
||||
cat >/tmp/air.docker.toml <<'AIR'
|
||||
root = "."
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
cmd = "go build -o ./tmp/silo ./cmd/silo/"
|
||||
bin = "./tmp/silo"
|
||||
args_bin = []
|
||||
include_ext = ["go", "sql", "json"]
|
||||
include_dir = ["cmd", "internal", "migrations"]
|
||||
exclude_dir = ["tmp", "vendor", "third_party", "docs", "scripts", "web"]
|
||||
exclude_regex = ["_test\\.go$"]
|
||||
delay = 500
|
||||
stop_on_error = true
|
||||
kill_delay = 500
|
||||
send_interrupt = true
|
||||
|
||||
[log]
|
||||
time = true
|
||||
|
||||
[misc]
|
||||
clean_on_exit = true
|
||||
AIR
|
||||
|
||||
air -c /tmp/air.docker.toml
|
||||
environment:
|
||||
MODE: integrated
|
||||
PORT: "8080"
|
||||
DATABASE_URL: postgres://${POSTGRES_USER:-silo}:${POSTGRES_PASSWORD:-silo}@postgres:5432/${POSTGRES_DB:-silo}?sslmode=disable
|
||||
REDIS_URL: redis://redis:6379
|
||||
SILO_PLUGIN_CACHE_DIR: /var/lib/silo/plugins
|
||||
POSTGRES_TUNE: "off"
|
||||
ports:
|
||||
- "${BACKEND_PORT:-18080}:8080"
|
||||
- "${JF_PORT:-8096}:8096"
|
||||
- "${ABS_PORT:-13378}:13378"
|
||||
volumes:
|
||||
- .:/app
|
||||
- silo-dev-air-tmp:/app/tmp
|
||||
- silo-dev-go-build:/root/.cache/go-build
|
||||
- silo-dev-go-mod:/go/pkg/mod
|
||||
- silo-dev-plugins:/var/lib/silo/plugins
|
||||
- silo-dev-compat:/var/lib/silo/compat
|
||||
- silo-dev-transcode:/tmp/silo-transcode
|
||||
- silo-dev-audiobook-covers:/var/lib/silo/audiobook-covers
|
||||
- ${MEDIA_ROOT:-/tmp/silo-dev-media}:${MEDIA_CONTAINER_ROOT:-/mnt/media}:ro
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fsS", "http://localhost:8080/api/v1/health"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 30
|
||||
start_period: 10s
|
||||
env_file:
|
||||
- path: .env
|
||||
required: false
|
||||
|
||||
web:
|
||||
build:
|
||||
context: .
|
||||
dockerfile_inline: |
|
||||
FROM node:22-slim
|
||||
|
||||
RUN corepack enable && corepack prepare pnpm@10.32.1 --activate
|
||||
|
||||
WORKDIR /app/web
|
||||
restart: unless-stopped
|
||||
working_dir: /app/web
|
||||
command: sh -c "pnpm install --frozen-lockfile --store-dir /pnpm/store && pnpm run dev -- --host 0.0.0.0"
|
||||
environment:
|
||||
VITE_API_PROXY_TARGET: http://silo:8080
|
||||
VITE_HMR_CLIENT_PORT: "${PORT:-8090}"
|
||||
PNPM_STORE_DIR: /pnpm/store
|
||||
CI: "true"
|
||||
ports:
|
||||
- "${PORT:-8090}:5173"
|
||||
volumes:
|
||||
- .:/app
|
||||
- silo-dev-web-node-modules:/app/web/node_modules
|
||||
- silo-dev-pnpm-store:/pnpm/store
|
||||
depends_on:
|
||||
silo:
|
||||
condition: service_healthy
|
||||
|
||||
volumes:
|
||||
silo-dev-air-tmp:
|
||||
silo-dev-audiobook-covers:
|
||||
silo-dev-compat:
|
||||
silo-dev-go-build:
|
||||
silo-dev-go-mod:
|
||||
silo-dev-plugins:
|
||||
silo-dev-pnpm-store:
|
||||
silo-dev-postgres:
|
||||
silo-dev-redis:
|
||||
silo-dev-transcode:
|
||||
silo-dev-web-node-modules:
|
||||
@@ -53,6 +53,7 @@ services:
|
||||
- ${MEDIA_ROOT:?Set MEDIA_ROOT in .env to the host media path}:${MEDIA_CONTAINER_ROOT:-/mnt/media}:ro
|
||||
- ${MEDIA_BOOKS_ROOT:-${MEDIA_ROOT}}:${MEDIA_BOOKS_CONTAINER_ROOT:-${MEDIA_CONTAINER_ROOT:-/mnt/media}/books}:ro
|
||||
- ${SILO_DATA_ROOT:-/opt/silo}/plugins:/var/lib/silo/plugins
|
||||
- ${SILO_DATA_ROOT:-/opt/silo}/compat:/var/lib/silo/compat
|
||||
- ${SILO_DATA_ROOT:-/opt/silo}/transcode:/tmp/silo-transcode
|
||||
- ${SILO_DATA_ROOT:-/opt/silo}/catalog-seeds:/catalog-seeds:ro
|
||||
- ${SILO_DATA_ROOT:-/opt/silo}/audiobook-covers:/var/lib/silo/audiobook-covers
|
||||
|
||||
@@ -0,0 +1,376 @@
|
||||
# Deterministic, Cross-Server `content_id`
|
||||
|
||||
**Status:** Implemented (branch `feat/deterministic-content-id`)
|
||||
**Scope:** catalog / scanner / metadata
|
||||
**Key code:** `internal/contentid`, `internal/metadata/service.go`,
|
||||
`internal/catalog/history_source.go`,
|
||||
`migrations/sql/20260612130000_deterministic_content_id.sql`,
|
||||
`migrations/sql/20260614120000_content_id_online_reid.sql`
|
||||
|
||||
This is the dev-facing reference for *why* `content_id` is what it is and *how*
|
||||
it works. It merges the original design spec, the implementation notes, and the
|
||||
PR write-up into one place.
|
||||
|
||||
---
|
||||
|
||||
## 1. The problem
|
||||
|
||||
`content_id` is the anchor almost everything hangs off: artwork, metadata, watch
|
||||
history, progress, favorites, ratings, collections, credits. Historically it was
|
||||
a **Sonyflake** — a locally generated, time-ordered random number minted at scan
|
||||
time.
|
||||
|
||||
That means the *same* movie matched on two servers (or re-imported into a fresh
|
||||
instance) gets a **different** id each time. Consequence: two servers holding the
|
||||
identical title share *none* of that state. Run more than one Silo, or try to
|
||||
sync a profile's history between servers, and the same film shows up as a
|
||||
stranger on the other side — no resume point, no watched flags, no ratings. There
|
||||
was no stable identity to build any cross-server feature on.
|
||||
|
||||
We want: **the same logical title produces the same `content_id` on every server,
|
||||
every time**, derived from a globally stable anchor (the provider IDs already in
|
||||
the library), and **stable across file renames/re-encodes** so linked state
|
||||
survives.
|
||||
|
||||
---
|
||||
|
||||
## 2. The decision
|
||||
|
||||
Make `content_id` a **deterministic, provider-derived, structured natural key**,
|
||||
stored as **`text COLLATE "C"`**:
|
||||
|
||||
| Item | `content_id` |
|
||||
| ---- | ------------ |
|
||||
| Movie | `movie-tmdb-228064` (fallback `movie-imdb-tt2413338`) |
|
||||
| Series | `series-tvdb-296762` |
|
||||
| Season | `season-tvdb-296762-1` |
|
||||
| Episode | `episode-tvdb-296762-1-5` |
|
||||
| Unmatched / local | `local-<128-bit path hash, hex>` |
|
||||
|
||||
Why this shape:
|
||||
|
||||
- **Collision-free with no hashing.** Provider IDs are unique by construction, so
|
||||
this is a natural key, not a surrogate — there is no "how many bits" question
|
||||
and no birthday bound.
|
||||
- **No column-type migration.** It stays `text`, so the wide soft-reference graph
|
||||
keeps working; only the *values* are remapped, not the type.
|
||||
- **Debuggable.** A row's identity is legible in logs and queries, and the
|
||||
scanner's match step becomes a direct PK lookup ("do I already have
|
||||
`movie-tmdb-228064`?") instead of a join through `media_item_provider_ids`.
|
||||
- **Stable** across renames/re-encodes — a movie keeps its tmdb id.
|
||||
|
||||
### Why `content_id`, not a file-level id
|
||||
|
||||
Two distinct identities at different granularities, not to be conflated:
|
||||
|
||||
- **`content_id`** — the *logical* item (movie/series/season/episode). One id,
|
||||
*many* files (1080p + 4K + cuts; a series owns thousands of episode files).
|
||||
- **`media_files.id`** — one *physical* file; a per-server serial that only
|
||||
answers "which file on disk."
|
||||
|
||||
`content_id` is the high-value target (35 FKs + ~25 unconstrained soft references
|
||||
point at it). Its stability requirement is the *opposite* of a file id's: a file
|
||||
id *should* change on rename; a `content_id` must *not*, or every server would
|
||||
drop a title's watch history on a routine rename. That's why it's anchored on
|
||||
rename-invariant provider IDs, never a path or file hash. File-level dedup is a
|
||||
separate, deferred concern.
|
||||
|
||||
---
|
||||
|
||||
## 3. How the id is derived
|
||||
|
||||
### Format (SchemeVersion 1)
|
||||
|
||||
```text
|
||||
movie-<provider>-<id> movie-tmdb-228064
|
||||
series-<provider>-<id> series-tvdb-296762
|
||||
season-<provider>-<seriesId>-<n> season-tvdb-296762-1
|
||||
episode-<provider>-<seriesId>-<s>-<e> episode-tvdb-296762-1-5
|
||||
local-<hex112> local-<sha256(path)[:14]>
|
||||
```
|
||||
|
||||
The component separator is `-`, an [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-2.3)
|
||||
*unreserved* character. Every component is `[a-z0-9]+` (or `tt` + digits for
|
||||
IMDb), so `-` is an unambiguous delimiter, and because it never needs
|
||||
percent-encoding the id doubles as its own tidy URL path segment
|
||||
(`/item/series-tvdb-296762`) — identical to what is stored in the database, so
|
||||
there is no encode/decode boundary and an operator can grep the value straight
|
||||
out of a URL or log. (`:` would be legal in a path too, but `encodeURIComponent`
|
||||
escapes it to `%3A`; an unreserved separator sidesteps that entirely.)
|
||||
|
||||
- The leading entity-type token domain-separates namespaces, so a movie and an
|
||||
episode can never alias on a coincidental number.
|
||||
- IDs are normalized: lowercase scheme tokens, whitespace trimmed, IMDb keeps its
|
||||
`tt` prefix verbatim.
|
||||
|
||||
### Frozen provider precedence
|
||||
|
||||
Each item anchors on **one** canonical provider, chosen by fixed precedence so
|
||||
two servers that see the same tags pick the same anchor:
|
||||
|
||||
- **Movies:** `tmdb → imdb → tvdb`
|
||||
- **Series / Season / Episode:** `tvdb → tmdb → imdb` (TVDB is episode-canonical)
|
||||
|
||||
Precedence **and** exact string format are frozen behind a code constant
|
||||
(`contentid.SchemeVersion = 1`), *not* a DB column — any scheme change forces a
|
||||
full remap that normalizes every row at once, so there is never a mixed-version
|
||||
population. Staleness is recoverable by recomputing the expected id from the
|
||||
provider columns and comparing.
|
||||
|
||||
### The load-bearing invariant: the show falls out of the episode id
|
||||
|
||||
Season/episode ids compose from the **series anchor + season/episode numbers**
|
||||
(both present in filenames and universally available), *not* their own provider
|
||||
episode IDs (often missing). Because `episode-<provider>-<seriesId>-<s>-<e>`
|
||||
embeds the series anchor, the series id is a pure **string transform** of the
|
||||
episode id — no catalog lookup:
|
||||
|
||||
```text
|
||||
episode-tvdb-296762-1-5 → series-tvdb-296762
|
||||
movie-tmdb-228064 → movie-tmdb-228064 (movies/series: unchanged)
|
||||
```
|
||||
|
||||
This is relied on by the hot watch-history query (§5). The format must never
|
||||
break it. (`contentid.SeriesIDFromContentID` is the canonical transform.)
|
||||
|
||||
### Where the anchor comes from
|
||||
|
||||
For tagged libraries the IDs are in the folder/file name
|
||||
(`{imdb-tt2413338} {tmdb-228064}`), so both servers derive the same id **from the
|
||||
path string at scan time, with no API call**. The denormalized
|
||||
`media_items.{tmdb,imdb,tvdb}_id` columns and the `media_item_provider_ids` link
|
||||
table are the other sources. `content_id` is *derived from* the link table, not a
|
||||
replacement — keep the full provider-ID set indexed.
|
||||
|
||||
---
|
||||
|
||||
## 4. What was built
|
||||
|
||||
| Area | File(s) | Summary |
|
||||
| ---- | ------- | ------- |
|
||||
| Derivation core | `internal/contentid/contentid.go` | `ForMovie`/`ForSeries`/`ForSeason`/`ForEpisode`/`ForLocal`, `SeriesIDFromContentID`, `IsProviderAnchored`/`IsLocal`, `SchemeVersion`, frozen precedence, normalization. |
|
||||
| Generation wiring | `internal/metadata/service.go` | `deriveLogicalContentID`/`deriveSeasonContentID`/`deriveEpisodeContentID` replace `idgen.NextID()` at every mint site (skeleton, provider-match, explicit/implicit seasons, episodes, scanner fallback). |
|
||||
| Re-ID on match | `internal/metadata/canonicalize.go` | Promotes a `local-` placeholder to its deterministic id at first confirmed match. |
|
||||
| Hot query | `internal/catalog/history_source.go` | Watch-history `display_id` resolves the show via the string transform for anchored episodes; skips the `episodes_pkey` probe. |
|
||||
| Value-remap migration | `migrations/sql/20260612130000_deterministic_content_id.sql` | Collision-safe Sonyflake → structured remap across the whole reference graph + `COLLATE "C"`, with rollback. |
|
||||
| Online re-ID migration | `migrations/sql/20260614120000_content_id_online_reid.sql` | Adds `silo_rename_content_id` + `ON UPDATE CASCADE` to the content-id FK family. |
|
||||
|
||||
### Generation: determinism at scan time
|
||||
|
||||
The scanner already parses provider tags from names, so `createOrFindSkeleton`
|
||||
derives `movie-tmdb-…`/`series-tvdb-…` immediately — two servers seeing the same
|
||||
tags mint the same id with no provider API call. This is the primary target
|
||||
(Radarr/Sonarr/Plex-tagged libraries). Untagged new items get a path-derived
|
||||
`local-` id keyed on the **file path** (not the folder), so distinct pre-merge
|
||||
skeletons in a shared folder stay separate while being stable across rescans.
|
||||
|
||||
Same-server convergence is preserved by the existing dedup
|
||||
(`findExistingByProviderIDs`) plus `UpsertTx`'s
|
||||
`ON CONFLICT (content_id) DO UPDATE` backstop.
|
||||
|
||||
Non-movie/series items (audiobook/ebook/podcast) and anchorless items **stay on
|
||||
Sonyflake** — there's no stable cross-server anchor to derive from, so changing
|
||||
them gains nothing.
|
||||
|
||||
### Re-ID on match: untagged items converge later
|
||||
|
||||
A tagged file is born with the right id; an *untagged* file gets a `local-` id
|
||||
and only learns its provider IDs later when the match worker confirms a result. A
|
||||
single gate in `mergeAndPersist` (`canonicalizeLocalContentID`) promotes the
|
||||
placeholder at first confirmed match:
|
||||
|
||||
```text
|
||||
SCAN a new file ← unchanged, still no network call
|
||||
▼
|
||||
createOrFindSkeleton ──► tag? YES ► movie-tmdb-123 NO ► local-<hash>
|
||||
▼
|
||||
(later) match worker → mergeAndPersist → canonicalizeLocalContentID
|
||||
│ Is the id still local- AND do we now have provider IDs?
|
||||
│ no → do nothing
|
||||
│ yes → Does a row already sit at movie-tmdb-123?
|
||||
│ yes → MERGE this row into it (rebindItemToExistingItem)
|
||||
│ no → RENAME local-<hash> → movie-tmdb-123
|
||||
▼ └─ DB auto-moves child rows (ON UPDATE CASCADE)
|
||||
rest of mergeAndPersist runs with the corrected id
|
||||
```
|
||||
|
||||
The guard is a single `IsLocal` prefix check, so tagged content and all refreshes
|
||||
pay nothing; the promotion is self-healing (a partial run re-runs on the next
|
||||
match). The rename primitive `silo_rename_content_id` reuses the migration's
|
||||
catalog-driven column enumeration so the two stay in lockstep.
|
||||
`ON UPDATE CASCADE` only fires when a `content_id` actually changes (essentially
|
||||
never outside this promotion), so there is no steady-state cost.
|
||||
|
||||
### The migration
|
||||
|
||||
The column **type does not change** (`text` → `text COLLATE "C"`); the **values**
|
||||
are remapped via add-then-swap, not in-place PK mutation. It dynamically
|
||||
enumerates the reference graph (the three PKs, every FK child from
|
||||
`pg_constraint`, and soft-reference columns by name — **65 columns** on the real
|
||||
schema), drops/recreates FKs and triggers, and retains a `content_id_migration_map`
|
||||
audit table for rollback.
|
||||
|
||||
**Collisions are never merged.** If two items derive to the same key, or a key is
|
||||
already taken, those rows keep their Sonyflake id and are flagged `collision` in
|
||||
the map — the migration can never violate a PK or silently drop a row. Collision
|
||||
status **cascades** from a series to its seasons/episodes (otherwise a uniquely
|
||||
numbered episode under a colliding series would get an `episode-…` key whose
|
||||
embedded series anchor points at a non-existent row).
|
||||
|
||||
`COLLATE "C"` is applied to every referencing column in the same migration so
|
||||
join keys match on both type and collation.
|
||||
|
||||
---
|
||||
|
||||
## 5. Why `COLLATE "C"` is mandatory
|
||||
|
||||
Provider-derived IDs are pure ASCII, but `content_id` inherits the DB default
|
||||
`en_US.utf8` collation. The design **regresses without `COLLATE "C"`**:
|
||||
|
||||
- For **equality / hash joins** (the watch-history join), `en_US.utf8` is
|
||||
deterministic so PostgreSQL already compares raw bytes — collation is
|
||||
irrelevant.
|
||||
- For **ordering** comparisons (B-tree descent on PK probes, `ORDER BY`, range
|
||||
scans), `en_US.utf8` invokes libc `strcoll` while `C` is a plain `memcmp`.
|
||||
- Structured keys share long prefixes (`episode-tvdb-296762-…`), which makes
|
||||
`strcoll` *worse* than on 18-digit Sonyflake strings — so a structured key
|
||||
under `en_US.utf8` is *slower* than the status quo. Under `C` it's ~3–5×
|
||||
faster.
|
||||
|
||||
So `COLLATE "C"` is load-bearing, not a free upgrade.
|
||||
|
||||
### The hot query
|
||||
|
||||
The watch-history page query (`history_source.go`) resolves each history row's
|
||||
episode key to its series, then inner-joins `media_items`, deduped over the
|
||||
profile's *entire* history. Two facts:
|
||||
|
||||
- `DISTINCT ON` can't short-circuit on `LIMIT` — cost is **O(history size)** with
|
||||
PK probes per row, not O(page).
|
||||
- The structured id lets us **delete the `LEFT JOIN episodes`** that existed only
|
||||
to fetch `series_id`: for anchored episode ids the show is a string transform.
|
||||
The join is null-poisoned for anchored ids (`= NULL` is an unsatisfiable b-tree
|
||||
scan key) so the planner never descends `episodes_pkey` for them. Legacy/`local-`
|
||||
ids still fall through the join.
|
||||
|
||||
The remaining `JOIN media_items` stays — it's for access/library filtering, not
|
||||
the show id. Reducing full-history scans to O(page) for the heaviest profiles is
|
||||
follow-up work (a per-`(profile, display_id)` summary table), not part of this
|
||||
change.
|
||||
|
||||
---
|
||||
|
||||
## 6. Performance — measured at production scale
|
||||
|
||||
Measured on an isolated, prod-tuned PostgreSQL 18 instance (`shared_buffers=4GB`)
|
||||
loaded with an **exact-cardinality copy of production**: 184k items, 1.93M
|
||||
episodes, 775k watch-history rows / 1,622 profiles. "Before" reproduces
|
||||
production today (Sonyflake, `en_US.utf8`); "after" applies the structured-key
|
||||
remap + `COLLATE "C"`. **Every metric improves.**
|
||||
|
||||
**`content_id` index-probe cost** — 300k forced point lookups over 1.9M keys:
|
||||
|
||||
The last three columns are *why* the faster schemes were rejected: speed alone
|
||||
isn't the goal — the scheme has to carry the properties the whole feature exists
|
||||
for. ✓ = has the property, ✗ = lacks it.
|
||||
|
||||
| Key scheme | Index size | µs / probe | vs today | Cross-server deterministic | Zero-join show transform | Human-readable |
|
||||
| ---------- | ---------- | ---------- | -------- | :------------------------: | :----------------------: | :------------: |
|
||||
| Sonyflake text, `en_US.utf8` *(today)* | 74 MB | 5.05 | 1.00× | ✗ | ✗ | ✗ |
|
||||
| 128-bit hash `bytea(16)` | 74 MB | 1.80 | 2.81× | ✓ | ✗ | ✗ |
|
||||
| `bigint` surrogate | 41 MB | 1.23 | 4.11× | ✗ | ✗ | ✗ |
|
||||
| **Structured + `COLLATE "C"` (this PR)** | 84 MB | **1.86** | **2.71×** | ✓ | ✓ | ✓ |
|
||||
|
||||
The structured key lands within 3% of a 128-bit hash and is the **only all-✓
|
||||
row**. The two faster schemes each give up something load-bearing:
|
||||
|
||||
- **128-bit hash** is cross-server deterministic (it hashes the same natural
|
||||
key), but an opaque digest can't be string-transformed, so episode→series
|
||||
needs a catalog join (no zero-join hot path), and it isn't legible in logs,
|
||||
URLs, or `psql`.
|
||||
- **`bigint` surrogate** is just the status quo's narrow integer: fast because
|
||||
it's 8 bytes, but per-server (not reproducible elsewhere), no embedded anchor
|
||||
to transform, and not meaningful to a human.
|
||||
|
||||
Index grows ~14% (84 vs 74 MB), immaterial.
|
||||
|
||||
**Real history-page query** — heaviest profile (25,384 rows):
|
||||
|
||||
| State | Latency | `episodes_pkey` probes | Speedup |
|
||||
| ----- | ------- | ---------------------- | ------- |
|
||||
| Before | 385 ms | 25,348 | 1.00× |
|
||||
| **This PR** | **150 ms** | **2,775** | **2.57×** |
|
||||
|
||||
**Concurrency** (`pgbench`, 150 real profiles): at 100 concurrent users,
|
||||
**311 ms / 321 tps → 181 ms / 551 tps** — 1.7× throughput, 42% lower tail
|
||||
latency. Comparison cost is pure CPU, so cheaper `COLLATE "C"` comparisons raise
|
||||
the throughput ceiling under load.
|
||||
|
||||
*Caveat: warmed (dataset fits in `shared_buffers`); the before/after **ratios**
|
||||
are the robust result, not absolute tps.*
|
||||
|
||||
---
|
||||
|
||||
## 7. Known limitations / follow-ups
|
||||
|
||||
- **Untagged series children.** A series that accumulated season/episode rows
|
||||
*before* it matched keeps those children on Sonyflake until a follow-up
|
||||
`recomposeSeriesChildIDs` pass re-derives them. Rare at first match (children
|
||||
usually don't exist yet); not yet wired.
|
||||
- **Cross-provider reconciliation.** A series tagged tmdb-only on one server and
|
||||
tvdb-only on another still diverges — both paths derive from the denormalized
|
||||
provider columns. Proper fix: consult `media_item_provider_ids` before walking
|
||||
the precedence list.
|
||||
- **O(history) query shape.** The watch-history `DISTINCT ON` over full history is
|
||||
the real liability at concurrency; the string transform removes a probe but not
|
||||
the full scan. The O(page) summary table is the fix, deferred.
|
||||
- **Migration at scale.** Runs in one transaction holding `AccessExclusive` locks
|
||||
for the COLLATE rewrite + remap — run off-peak; very large installs (10–50M
|
||||
rows) should use the batched procedure noted in the migration header.
|
||||
- **`local-` uses the absolute path,** so a same-server remount re-IDs local
|
||||
items. Acceptable for the best-effort local namespace. The hash is 112 bits
|
||||
(`sha256(path)[:14]`) so a `local-` id packs losslessly into a compat UUID
|
||||
(see below).
|
||||
- **Client-visible id churn.** `jellycompat` encodes `content_id` into the
|
||||
client-facing UUID, so item ids seen by Android/Apple/Jellyfin clients change at
|
||||
migration (caches/resume keyed on item id reset once). **silo-android** and
|
||||
**silo-apple** should be made aware; server-side state is fully remapped. The
|
||||
encoding is *reversible* — a structured or local `content_id` is bit-packed
|
||||
into the UUID (`contentid.Pack`/`Unpack`), not hashed into a lookup table — so
|
||||
a compat item id decodes by pure computation and is stable across server
|
||||
restarts and across instances. Only arbitrary names (genres, studios) and the
|
||||
rare `content_id` too large to pack still use the in-memory hash map.
|
||||
|
||||
---
|
||||
|
||||
## 8. Prior art
|
||||
|
||||
- **Plex:** logical identity is a provider-namespaced GUID (`plex://movie/…`,
|
||||
`com.plexapp.agents.imdb://tt…`) — exactly this shape. (Its per-server integer
|
||||
`ratingKey` is the non-portable counterexample we avoid.)
|
||||
- **Jellyfin / Emby:** store namespaced provider IDs and match on them; item GUIDs
|
||||
are deterministic. Same principle — anchor identity on the provider, not a
|
||||
per-server sequence.
|
||||
|
||||
---
|
||||
|
||||
## Appendix — alternatives rejected
|
||||
|
||||
Retained so the decision isn't re-litigated:
|
||||
|
||||
- **Keep Sonyflake.** Non-deterministic per server — the entire problem.
|
||||
- **Hash provider tuple → `uuid` (128-bit).** Collision resistance is unnecessary
|
||||
(input is already unique), the id becomes opaque, and it forces a `text → uuid`
|
||||
migration across 35 FKs + ~25 soft refs whose failure mode is a seq-scan cliff
|
||||
(miss one column → `uuid = text` per-row cast → index disabled). Pays a large
|
||||
migration cost for an ~8-byte width win that only matters at a 50M-row ceiling.
|
||||
- **Hash provider tuple → `text` hex.** Opaque like the uuid, large like the
|
||||
natural key — no reason to pick it over the legible structured key.
|
||||
- **Path or content hash.** A title spans many files at many paths (no single path
|
||||
to hash), and the hash changes on rename — the opposite of the stability
|
||||
requirement. Only appropriate for *file-level* identity.
|
||||
- **Pack provider + numeric id into `bigint`.** Variable-width provider numbers
|
||||
and S/E packing make the encoding brittle; not worth it over a legible key.
|
||||
- **File-level deterministic id (`media_files`).** Deferred, not rejected — lower
|
||||
value (no metadata/watch state hangs off it), with open problems (server-local
|
||||
mount anchors, same-file-in-two-libraries). A complement, out of scope here.
|
||||
@@ -0,0 +1,49 @@
|
||||
# Outbound Email (`internal/mail`)
|
||||
|
||||
**Status:** Implemented 2026-06-11
|
||||
|
||||
Silo's shared outbound email facility. It is deliberately feature-agnostic: any
|
||||
feature that sends mail (notification emails, account flows, invites) composes
|
||||
a `mail.Message` and hands it to the shared `mail.Sender`, so SMTP
|
||||
configuration, security policy, and diagnostics live in exactly one place.
|
||||
|
||||
## Abstraction
|
||||
|
||||
```go
|
||||
type Sender interface {
|
||||
Enabled(ctx context.Context) bool
|
||||
Send(ctx context.Context, msg Message) error
|
||||
}
|
||||
```
|
||||
|
||||
`Message` carries recipients, subject, and text and/or HTML bodies (both set →
|
||||
multipart/alternative). `Send` returns `mail.ErrNotConfigured` when email is
|
||||
disabled or incomplete, so features treat email as an optional transport and
|
||||
degrade gracefully. The SMTP implementation (`mail.NewSMTPSender`) is backed by
|
||||
`github.com/wneessen/go-mail`.
|
||||
|
||||
## Configuration
|
||||
|
||||
Live server settings (no restart required; read on every send — volume is
|
||||
low):
|
||||
|
||||
| Key | Default | Notes |
|
||||
|---|---|---|
|
||||
| `email.enabled` | `false` | master switch |
|
||||
| `email.smtp_host` | — | required |
|
||||
| `email.smtp_port` | `587` | |
|
||||
| `email.smtp_security` | `starttls` | `starttls` \| `tls` (implicit, port 465) \| `none` |
|
||||
| `email.smtp_username` | — | empty = no auth |
|
||||
| `email.smtp_password` | — | encrypted at rest (`SensitiveSettingKeys`) |
|
||||
| `email.from_address` | — | required |
|
||||
| `email.from_name` | `Silo` | |
|
||||
|
||||
Admin UI: Admin Settings → Connections → Email, including a synchronous test
|
||||
send (`POST /api/v1/admin/email/test`).
|
||||
|
||||
## Adding a consumer
|
||||
|
||||
Construct messages in the feature package and send through a `mail.Sender`
|
||||
dependency. Do not read `email.*` settings from feature code, and check
|
||||
`Enabled` (or branch on `ErrNotConfigured`) rather than treating a missing
|
||||
SMTP configuration as an error.
|
||||
@@ -0,0 +1,19 @@
|
||||
# Silo v1 Scope
|
||||
|
||||
**Status: NOT LOCKED — proposal window open.**
|
||||
|
||||
Propose capabilities with the **v1 capability proposal** issue template; triage happens on the
|
||||
[Silo v1 project](https://github.com/orgs/Silo-Server/projects/5).
|
||||
|
||||
When the scope locks, this file becomes the source of truth and will contain:
|
||||
|
||||
1. **Locked capabilities** — a table of capability epics (issue links) with one-line scope statements.
|
||||
2. **API policy** — additive-only within `/api/v1` (no field renames/removals, no type changes,
|
||||
no status-code repurposing; removals only via the Deprecation/Sunset header flow; capability
|
||||
endpoints for feature detection). Contract tooling: #135.
|
||||
3. **Amendment rules** — after lock, this file changes only via PR with code-owner review.
|
||||
An amendment PR is the exception process: it must say what changes, why it cannot wait
|
||||
for v1.1, and what it displaces.
|
||||
|
||||
Until lock: treat any capability not tracked as `Proposed`/`Locked` on the project as out of scope
|
||||
for feature PRs (see the scope gate in `CLAUDE.md`).
|
||||
@@ -1,100 +0,0 @@
|
||||
# Notifications & Push — Test Checklist
|
||||
|
||||
A repeatable regression pass for the core notifications + push subsystems. Organized by layer: automated (fast, run anytime), server pipeline (API, no browser), real browser (human-in-the-loop), and not-yet-testable (future work). Run the automated layer on every change; run the browser layer before shipping a user-facing change.
|
||||
|
||||
Specs/plans this exercises live under `docs/superpowers/specs|plans/2026-06-10-core-notifications-*` and `*-push-*`.
|
||||
|
||||
---
|
||||
|
||||
## 1. Automated tests (fast, no live instance)
|
||||
|
||||
Run from the repo root. `export GOWORK=off` first (or run inside the worktree).
|
||||
|
||||
```bash
|
||||
# Go — materializer, matchers, worker state machine, request events, presence, handlers, tasks
|
||||
GOWORK=off go test ./internal/notifications/ ./internal/push/ ./internal/presence/ \
|
||||
./internal/requests/ ./internal/api/handlers/ ./internal/taskmanager/tasks/
|
||||
|
||||
# Web — bell, inbox, settings, admin page, usePushDevice, SW logic
|
||||
cd web && pnpm exec vitest run
|
||||
```
|
||||
|
||||
Expected: all green. The Go suites cover the outcome state machine (sent/skipped/failed-with-backoff/dead + dead-token prune), child-suppression SQL, the `notifications.send` plugin contract, request lifecycle publishing, and presence refcounting. The web suites cover the UI and the `usePushDevice` lifecycle/race-guard.
|
||||
|
||||
What this does NOT cover: real WebSocket delivery, real push banners, real migrations against production-shaped data. Use layers 2–3 for those.
|
||||
|
||||
---
|
||||
|
||||
## 2. Server pipeline (API — proves source → materializer → enqueuer → worker, no browser)
|
||||
|
||||
Needs an admin API key and the instance reachable (on silo-new the API is `http://localhost:8090`; create a temp key, delete it after — see the snippet at the bottom). Observe results in the `notifications` and `push_deliveries` tables.
|
||||
|
||||
For each notification **source**, trigger it and confirm a row lands (and, if push is configured + a device registered, a `push_deliveries` row):
|
||||
|
||||
- [ ] **Announcement** — `POST /api/v1/admin/announcements {title, body, audience:{all:true}}` → confirm a `notifications` row (category `announcement`) for each targeted user. ⚠️ `audience:{all:true}` fans out to **every** user's inbox — use `{user_ids:[<your id>]}` when testing on a live instance.
|
||||
- [ ] **Request lifecycle** — submit a media request, then `POST /api/v1/requests/{id}/approve` (admin) → confirm a `request.approved` notification for the requester. Decline/complete/fail similarly produce `request.declined` / `request.completed` / `request.failed`.
|
||||
- [ ] **System** — admin user-update that changes a password → confirm a `system.password_changed` notification for that user.
|
||||
- [ ] **Content** — add an item to a profile's watchlist/favorites, then add/scan a matching library item → confirm a `content.added` notification for that profile (respects the 48h recency gate + per-day burst dedup).
|
||||
- [ ] **Admin alert** — force a failed job/scan → confirm an `admin`-category notification fans out to admin users only (throttled 1/hour per source).
|
||||
- [ ] **Digest** — opt a user into `content_digest`, then run the `notifications_digest` task (or wait for 08:00) → one digest notification if there were additions.
|
||||
- [ ] **Push test** — `POST /api/v1/admin/push/test` → 202; a `system.test_push` notification + a `push_deliveries` row per the caller's registered push devices; the worker transitions it within ~30–45s (grace + tick). With a real subscription it sends; with a bogus one it fails-with-backoff (still proves the worker path).
|
||||
|
||||
Push config endpoints to verify:
|
||||
- [ ] `GET /api/v1/admin/push/status` → `{apns,fcm,webpush}` booleans (flip after saving creds).
|
||||
- [ ] `POST /api/v1/admin/push/generate-vapid-keys` → `{vapid_public, vapid_private}` (admin only).
|
||||
- [ ] `GET /api/v1/notifications/push/webpush-key` → the configured public key (empty when unconfigured).
|
||||
- [ ] `PUT /api/v1/notifications/push/device {transport:"webpush", token:<subscription JSON>}` with `X-Profile-Id` → 204 (400 without the profile header — the route is profile-gated).
|
||||
- [ ] `GET /api/v1/notifications/push/devices` → lists the user's push devices; `PUT .../devices/{id} {enabled}` toggles.
|
||||
|
||||
---
|
||||
|
||||
## 3. Real browser (the only way to validate the UI + actual push banner)
|
||||
|
||||
Open the web app over a secure context (`localhost` or HTTPS — service workers require it). On silo-new that's `http://localhost:8090` (tunnel if remote).
|
||||
|
||||
In-app notifications:
|
||||
- [ ] **Bell + badge** — top of the app; unread count badge.
|
||||
- [ ] **Dropdown** — latest notifications, "Mark all read", deep links.
|
||||
- [ ] **Inbox page** (`/notifications`) — category tabs, dismiss, mark-on-view, "Load more".
|
||||
- [ ] **Live toast** — with the app open, trigger a notification (announcement to yourself, or the push-test) → a toast appears in real time (via the WebSocket), unless that category is muted.
|
||||
- [ ] **Preferences** — Settings → Notifications → per-category mute toggles persist; "Daily digest" defaults off; the admin category shows only for admins.
|
||||
|
||||
Push (the headline manual test):
|
||||
- [ ] **Admin config** — Admin → Settings → Push Notifications → "Generate keys", save the VAPID public/private + a `mailto:` subject → the Web Push badge flips to "Ready".
|
||||
- [ ] **Enable on this device** — Settings → Notifications → "Push on this device" → grant the browser permission prompt → a device row appears in the list.
|
||||
- [ ] **Background banner** — **close the app tab**, then `POST /admin/push/test` (or trigger any notification) → within ~30–45s an OS notification banner appears.
|
||||
- [ ] **Click-through** — click the banner → the app opens/focuses and navigates to the notification's deep link.
|
||||
- [ ] **Presence gating** — with the app tab open and focused, the same notification should NOT push a redundant banner (you saw it in-app); after closing the tab for >30s it does.
|
||||
- [ ] **Per-device toggle / revoke** — disable the device in the list → no more banners; re-enable restores.
|
||||
|
||||
---
|
||||
|
||||
## 4. Not yet testable (tracked future work)
|
||||
|
||||
- **APNs (Apple) / FCM (Android)** — no mobile clients exist to register tokens, so these transports are only unit-tested (pure result-mappers). End-to-end needs silo-apple / silo-android (Phase 3 mobile).
|
||||
- **Plugin `notifications.send`** — the ebook/audiobook request plugins don't publish it yet. Until those one-line `PublishEvent("notifications.send", …)` calls are added, plugin-sourced notifications can't be exercised end to end (the contract itself is unit-tested).
|
||||
- **iOS Safari web push** — only works for an installed PWA, not a normal tab; the client degrades to an "unsupported" state otherwise.
|
||||
|
||||
---
|
||||
|
||||
## Appendix — temp admin key on silo-new (create, use, delete)
|
||||
|
||||
```bash
|
||||
# create
|
||||
KEY="sa_$(openssl rand -hex 32)"
|
||||
docker exec silo-server-postgres-1 psql -U silo -d silo -tAc \
|
||||
"INSERT INTO api_keys (user_id,label,api_key) VALUES (1,'manual-test','$KEY');"
|
||||
echo "$KEY" # use as: Authorization: Bearer $KEY against http://localhost:8090/api/v1/...
|
||||
|
||||
# observe
|
||||
docker exec silo-server-postgres-1 psql -U silo -d silo -tAc \
|
||||
"SELECT id,category,type,title FROM notifications ORDER BY id DESC LIMIT 10;"
|
||||
docker exec silo-server-postgres-1 psql -U silo -d silo -tAc \
|
||||
"SELECT status,transport,attempts,left(last_error,40) FROM push_deliveries ORDER BY id DESC LIMIT 10;"
|
||||
|
||||
# clean up (delete the key + any test artifacts you created)
|
||||
docker exec silo-server-postgres-1 psql -U silo -d silo -tAc \
|
||||
"DELETE FROM api_keys WHERE label='manual-test';"
|
||||
```
|
||||
|
||||
Always remove test notifications/devices you create on a real user's account; `push_deliveries` cascades when its `notifications` row is deleted.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,809 +0,0 @@
|
||||
# Core Notifications — Web UI Implementation Plan (Phase 2 of 3)
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Notifications UI in the silo-server web app: unread badge (sidebar + mobile header bell with dropdown), live toasts from the events WebSocket, an inbox page, per-category preference toggles in settings, and an admin announcements page.
|
||||
|
||||
**Architecture:** A `notifications` query-hook module talks to the Phase-1 REST API; one live-wiring hook subscribes to the `notifications` WS channel via the existing `RealtimeEventsProvider` ref-counted `useEventChannel`, firing sonner toasts and patching React Query caches. UI components consume only the hooks. Desktop surface is an `AppSidebar` item with badge (no desktop top bar exists); mobile surface is a bell + popover in the `Layout` header.
|
||||
|
||||
**Tech Stack:** React 19, React Router v7, TanStack Query, sonner, lucide-react, shadcn/ui primitives (Popover, Switch, Dialog), Vitest + RTL.
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-06-10-core-notifications-design.md` (Web UI section). Server API shipped in Phase 1.
|
||||
|
||||
Commands assume the repository root is the cwd; web commands run in `web/`. `pnpm` is the package manager.
|
||||
|
||||
---
|
||||
|
||||
## Server API contract (frozen, Phase 1)
|
||||
|
||||
The `api()` client prefixes `/api/v1` and adds auth + `X-Profile-Id` headers automatically.
|
||||
|
||||
```
|
||||
GET /notifications?unread=1&category=&cursor=&limit= → {"items": Notification[], "next_cursor": number|null}
|
||||
GET /notifications/unread-count → {"count": number}
|
||||
POST /notifications/read {"ids":[...]} | {"all":true} → 204
|
||||
POST /notifications/{id}/dismiss → 204 | 404
|
||||
GET /notifications/preferences → {"preferences":[{"category","enabled"}]}
|
||||
PUT /notifications/preferences {"preferences":[...]} → 204 | 400
|
||||
GET /admin/announcements → {"items": Announcement[]}
|
||||
POST /admin/announcements {title, body, audience:{all|user_ids|library_ids}, expires_at?} → 201
|
||||
DELETE /admin/announcements/{id} → 204 | 404
|
||||
```
|
||||
|
||||
Notification JSON: `{id, user_id, profile_id?, category, type, title, body, link?, item_id?, created_at, read_at?, expires_at?}`. Categories: `request | content | announcement | system | admin`; preference categories additionally `content_digest` (never a notification category). WS: channel `"notifications"`, event `"notification.created"`, `data` = the Notification; subscribe snapshot `data` = `{"unread_count": number}`.
|
||||
|
||||
## File map
|
||||
|
||||
| File | Action | Responsibility |
|
||||
|---|---|---|
|
||||
| `web/src/api/types.ts` | modify | `AppNotification`, `NotificationPreference`, `Announcement`, `AnnouncementAudience` types; add `"notifications"` to `EventChannel` |
|
||||
| `web/src/hooks/queries/keys.ts` | modify | `notificationKeys` factory |
|
||||
| `web/src/hooks/queries/notifications.ts` | create | all query/mutation hooks (user + admin) |
|
||||
| `web/src/hooks/useNotificationsLive.ts` | create | WS subscription → toast + cache patching |
|
||||
| `web/src/hooks/useNotificationsLive.test.ts` | create | live-hook tests |
|
||||
| `web/src/components/NotificationBell.tsx` | create | badge + popover dropdown (mobile header) |
|
||||
| `web/src/components/NotificationBell.test.tsx` | create | bell tests |
|
||||
| `web/src/components/Layout.tsx` | modify | mount bell in mobile header; mount live hook |
|
||||
| `web/src/components/AppSidebar.tsx` | modify | Notifications nav item with badge |
|
||||
| `web/src/pages/Notifications.tsx` | create | inbox page |
|
||||
| `web/src/pages/Notifications.test.tsx` | create | inbox tests |
|
||||
| `web/src/pages/settings/NotificationsSettings.tsx` | create | preference toggles |
|
||||
| `web/src/pages/SettingsLayout.tsx` | modify | nav section entry |
|
||||
| `web/src/pages/AdminAnnouncements.tsx` | create | admin CRUD page |
|
||||
| `web/src/components/AdminSidebar.tsx` | modify | announcements nav item |
|
||||
| `web/src/App.tsx` | modify | routes: `/notifications`, admin `announcements`, settings `notifications` |
|
||||
|
||||
---
|
||||
|
||||
### Task 1: API types and event channel
|
||||
|
||||
**Files:**
|
||||
- Modify: `web/src/api/types.ts` (EventChannel union ~line 2198; types appended near other domain types)
|
||||
|
||||
- [ ] **Step 1: Add the types**
|
||||
|
||||
Add `"notifications"` to the `EventChannel` union. Then add (named `AppNotification` to avoid colliding with the DOM `Notification` type — verify no existing `AppNotification` symbol first with grep):
|
||||
|
||||
```typescript
|
||||
export type NotificationCategory = "request" | "content" | "announcement" | "system" | "admin";
|
||||
|
||||
export type NotificationPreferenceCategory = NotificationCategory | "content_digest";
|
||||
|
||||
export interface AppNotification {
|
||||
id: number;
|
||||
user_id: number;
|
||||
profile_id?: string;
|
||||
category: NotificationCategory;
|
||||
type: string;
|
||||
title: string;
|
||||
body: string;
|
||||
link?: string;
|
||||
item_id?: string;
|
||||
created_at: string;
|
||||
read_at?: string;
|
||||
expires_at?: string;
|
||||
}
|
||||
|
||||
export interface NotificationListResponse {
|
||||
items: AppNotification[] | null;
|
||||
next_cursor: number | null;
|
||||
}
|
||||
|
||||
export interface NotificationPreference {
|
||||
category: NotificationPreferenceCategory;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface AnnouncementAudience {
|
||||
all?: boolean;
|
||||
user_ids?: number[];
|
||||
library_ids?: number[];
|
||||
}
|
||||
|
||||
export interface Announcement {
|
||||
id: number;
|
||||
title: string;
|
||||
body: string;
|
||||
audience: AnnouncementAudience;
|
||||
created_by?: number;
|
||||
created_at: string;
|
||||
expires_at?: string;
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Typecheck and commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -5
|
||||
git add src/api/types.ts && git commit -m "feat(web): notification API types and event channel"
|
||||
```
|
||||
|
||||
(If the repo's typecheck command differs — check package.json scripts for `typecheck`/`build` — use that; `pnpm run build` also typechecks via `tsc -b`.)
|
||||
|
||||
---
|
||||
|
||||
### Task 2: Query keys and hooks
|
||||
|
||||
**Files:**
|
||||
- Modify: `web/src/hooks/queries/keys.ts`
|
||||
- Create: `web/src/hooks/queries/notifications.ts`
|
||||
|
||||
- [ ] **Step 1: Keys** — follow the existing factory style in keys.ts:
|
||||
|
||||
```typescript
|
||||
export const notificationKeys = {
|
||||
all: ["notifications"] as const,
|
||||
list: (filters: { unread?: boolean; category?: string }) =>
|
||||
[...notificationKeys.all, "list", filters] as const,
|
||||
unreadCount: () => [...notificationKeys.all, "unread-count"] as const,
|
||||
preferences: () => [...notificationKeys.all, "preferences"] as const,
|
||||
announcements: () => ["admin", "announcements"] as const,
|
||||
};
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Hooks** — `web/src/hooks/queries/notifications.ts`. Mirror the conventions in `web/src/hooks/queries/admin/users.ts` (api client import path, toast usage, invalidation):
|
||||
|
||||
```typescript
|
||||
import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { api } from "@/api/client";
|
||||
import type {
|
||||
Announcement,
|
||||
AnnouncementAudience,
|
||||
AppNotification,
|
||||
NotificationListResponse,
|
||||
NotificationPreference,
|
||||
} from "@/api/types";
|
||||
import { notificationKeys } from "@/hooks/queries/keys";
|
||||
|
||||
const PAGE_SIZE = 25;
|
||||
|
||||
export function useNotificationsList(filters: { unread?: boolean; category?: string } = {}) {
|
||||
return useInfiniteQuery({
|
||||
queryKey: notificationKeys.list(filters),
|
||||
queryFn: async ({ pageParam }) => {
|
||||
const params = new URLSearchParams();
|
||||
params.set("limit", String(PAGE_SIZE));
|
||||
if (filters.unread) params.set("unread", "1");
|
||||
if (filters.category) params.set("category", filters.category);
|
||||
if (pageParam) params.set("cursor", String(pageParam));
|
||||
return api<NotificationListResponse>(`/notifications?${params.toString()}`);
|
||||
},
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
export function useUnreadCount() {
|
||||
return useQuery({
|
||||
queryKey: notificationKeys.unreadCount(),
|
||||
queryFn: () => api<{ count: number }>("/notifications/unread-count"),
|
||||
select: (d) => d.count,
|
||||
});
|
||||
}
|
||||
|
||||
export function useMarkRead() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: { ids?: number[]; all?: boolean }) =>
|
||||
api("/notifications/read", { method: "POST", body: JSON.stringify(body) }),
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: notificationKeys.all });
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : "Failed to mark read");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDismissNotification() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => api(`/notifications/${id}/dismiss`, { method: "POST" }),
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: notificationKeys.all });
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : "Failed to dismiss");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useNotificationPreferences() {
|
||||
return useQuery({
|
||||
queryKey: notificationKeys.preferences(),
|
||||
queryFn: () => api<{ preferences: NotificationPreference[] }>("/notifications/preferences"),
|
||||
select: (d) => d.preferences,
|
||||
});
|
||||
}
|
||||
|
||||
export function useSetNotificationPreferences() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (preferences: NotificationPreference[]) =>
|
||||
api("/notifications/preferences", { method: "PUT", body: JSON.stringify({ preferences }) }),
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: notificationKeys.preferences() });
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : "Failed to save preferences");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useAnnouncements() {
|
||||
return useQuery({
|
||||
queryKey: notificationKeys.announcements(),
|
||||
queryFn: () => api<{ items: Announcement[] | null }>("/admin/announcements"),
|
||||
select: (d) => d.items ?? [],
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateAnnouncement() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: {
|
||||
title: string;
|
||||
body: string;
|
||||
audience: AnnouncementAudience;
|
||||
expires_at?: string;
|
||||
}) => api<Announcement>("/admin/announcements", { method: "POST", body: JSON.stringify(body) }),
|
||||
onSuccess: () => {
|
||||
toast.success("Announcement published");
|
||||
void queryClient.invalidateQueries({ queryKey: notificationKeys.announcements() });
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : "Failed to publish");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteAnnouncement() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => api(`/admin/announcements/${id}`, { method: "DELETE" }),
|
||||
onSuccess: () => {
|
||||
toast.success("Announcement deleted");
|
||||
void queryClient.invalidateQueries({ queryKey: notificationKeys.announcements() });
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err instanceof Error ? err.message : "Failed to delete");
|
||||
},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
Check `api()`'s actual error behavior (does it throw on non-2xx with a message? read `web/src/api/client.ts:343-380`) and the import alias style (`@/` vs relative — match the queries directory's existing imports).
|
||||
|
||||
- [ ] **Step 3: Typecheck + commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -5
|
||||
git add src/hooks/queries/ && git commit -m "feat(web): notification query hooks"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 3: Live wiring hook (WS → toasts + cache)
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/hooks/useNotificationsLive.ts`
|
||||
- Test: `web/src/hooks/useNotificationsLive.test.ts`
|
||||
|
||||
- [ ] **Step 1: Write the failing test**
|
||||
|
||||
Mock `useEventChannel` (from `@/components/realtimeEventsContext`) to capture handlers, mock `sonner`'s toast, wrap in a QueryClientProvider. Follow the mocking style of `web/src/components/RealtimeEventsProvider.test.tsx` (vi.mock + vi.hoisted).
|
||||
|
||||
```typescript
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { renderHook } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { capturedHandlers, toastMock } = vi.hoisted(() => ({
|
||||
capturedHandlers: {} as { onEvent?: (m: unknown) => void; onSnapshot?: (m: unknown) => void },
|
||||
toastMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/components/realtimeEventsContext", () => ({
|
||||
useEventChannel: (_channel: string, handlers: typeof capturedHandlers) => {
|
||||
capturedHandlers.onEvent = handlers?.onEvent;
|
||||
capturedHandlers.onSnapshot = handlers?.onSnapshot;
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("sonner", () => ({ toast: toastMock }));
|
||||
|
||||
const profileState = vi.hoisted(() => ({ profile: { id: "p-1", is_child: false } }));
|
||||
vi.mock("@/hooks/useAuth", () => ({ useAuth: () => profileState }));
|
||||
|
||||
import { useNotificationsLive } from "./useNotificationsLive";
|
||||
import { notificationKeys } from "@/hooks/queries/keys";
|
||||
|
||||
function setup() {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
queryClient.setQueryData(notificationKeys.unreadCount(), { count: 1 });
|
||||
renderHook(() => useNotificationsLive(), {
|
||||
wrapper: ({ children }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
),
|
||||
});
|
||||
return queryClient;
|
||||
}
|
||||
|
||||
function frame(data: object) {
|
||||
return { type: "event", channel: "notifications", event: "notification.created", data };
|
||||
}
|
||||
|
||||
describe("useNotificationsLive", () => {
|
||||
it("toasts and bumps unread count on notification.created", () => {
|
||||
const qc = setup();
|
||||
capturedHandlers.onEvent?.(
|
||||
frame({ id: 5, category: "request", title: "Request approved", body: "Dune", profile_id: "p-1" }),
|
||||
);
|
||||
expect(toastMock).toHaveBeenCalledWith("Request approved", expect.anything());
|
||||
expect(qc.getQueryData(notificationKeys.unreadCount())).toEqual({ count: 2 });
|
||||
});
|
||||
|
||||
it("ignores frames for another profile", () => {
|
||||
setup();
|
||||
capturedHandlers.onEvent?.(
|
||||
frame({ id: 6, category: "content", title: "New content", body: "", profile_id: "p-other" }),
|
||||
);
|
||||
expect(toastMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("suppresses restricted categories on child profiles", () => {
|
||||
profileState.profile = { id: "p-1", is_child: true };
|
||||
setup();
|
||||
capturedHandlers.onEvent?.(
|
||||
frame({ id: 7, category: "request", title: "Request approved", body: "", profile_id: undefined }),
|
||||
);
|
||||
expect(toastMock).not.toHaveBeenCalled();
|
||||
profileState.profile = { id: "p-1", is_child: false };
|
||||
});
|
||||
|
||||
it("seeds unread count from snapshot", () => {
|
||||
const qc = setup();
|
||||
capturedHandlers.onSnapshot?.({ type: "snapshot", channel: "notifications", data: { unread_count: 9 } });
|
||||
expect(qc.getQueryData(notificationKeys.unreadCount())).toEqual({ count: 9 });
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
(The test file needs `.tsx` extension if JSX is used in the wrapper — name it `useNotificationsLive.test.tsx`.)
|
||||
|
||||
- [ ] **Step 2: Run to verify failure**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/hooks/useNotificationsLive.test.tsx 2>&1 | tail -5
|
||||
```
|
||||
|
||||
Expected: FAIL — module `./useNotificationsLive` not found.
|
||||
|
||||
- [ ] **Step 3: Implement**
|
||||
|
||||
```typescript
|
||||
import { useCallback } from "react";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import type { AppNotification } from "@/api/types";
|
||||
import { useEventChannel } from "@/components/realtimeEventsContext";
|
||||
import { notificationKeys } from "@/hooks/queries/keys";
|
||||
import { useAuth } from "@/hooks/useAuth";
|
||||
|
||||
const CHILD_HIDDEN_CATEGORIES = new Set(["request", "system", "admin"]);
|
||||
|
||||
/**
|
||||
* Subscribes to the notifications WS channel: fires a toast for live
|
||||
* notifications addressed to the active profile, keeps the unread-count
|
||||
* cache current, and seeds it from the subscribe snapshot.
|
||||
* Mount exactly once inside the authenticated layout.
|
||||
*/
|
||||
export function useNotificationsLive() {
|
||||
const queryClient = useQueryClient();
|
||||
const { profile } = useAuth();
|
||||
const profileId = profile?.id;
|
||||
const isChild = profile?.is_child ?? false;
|
||||
|
||||
const onEvent = useCallback(
|
||||
(message: unknown) => {
|
||||
const frame = message as { event?: string; data?: AppNotification };
|
||||
if (frame.event !== "notification.created" || !frame.data) return;
|
||||
const n = frame.data;
|
||||
if (n.profile_id && n.profile_id !== profileId) return;
|
||||
if (isChild && CHILD_HIDDEN_CATEGORIES.has(n.category)) return;
|
||||
|
||||
queryClient.setQueryData<{ count: number }>(notificationKeys.unreadCount(), (prev) => ({
|
||||
count: (prev?.count ?? 0) + 1,
|
||||
}));
|
||||
void queryClient.invalidateQueries({
|
||||
queryKey: [...notificationKeys.all, "list"],
|
||||
refetchType: "none",
|
||||
});
|
||||
toast(n.title, { description: n.body });
|
||||
},
|
||||
[queryClient, profileId, isChild],
|
||||
);
|
||||
|
||||
const onSnapshot = useCallback(
|
||||
(message: unknown) => {
|
||||
const frame = message as { data?: { unread_count?: number } };
|
||||
if (typeof frame.data?.unread_count !== "number") return;
|
||||
queryClient.setQueryData(notificationKeys.unreadCount(), { count: frame.data.unread_count });
|
||||
},
|
||||
[queryClient],
|
||||
);
|
||||
|
||||
useEventChannel("notifications", { onEvent, onSnapshot });
|
||||
}
|
||||
```
|
||||
|
||||
NOTE on handler identity: `useEventChannel`'s effect re-subscribes when the handlers object changes — pass a stable object. Check how existing consumers handle this (grep `useEventChannel(` usages); if they memoize the handlers object, do the same: `useMemo(() => ({ onEvent, onSnapshot }), [onEvent, onSnapshot])`.
|
||||
|
||||
The toast call shape (`toast(title, { description })`) must match the test's `expect.anything()` second arg. If `useAuth` returns a different profile field shape, adapt (read `useAuth.tsx`).
|
||||
|
||||
- [ ] **Step 4: Run tests until green**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/hooks/useNotificationsLive.test.tsx 2>&1 | tail -5
|
||||
```
|
||||
|
||||
- [ ] **Step 5: Commit**
|
||||
|
||||
```bash
|
||||
git add web/src/hooks/useNotificationsLive.* && git commit -m "feat(web): live notifications wiring (toasts, unread cache)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 4: NotificationBell component + Layout mount
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/components/NotificationBell.tsx`
|
||||
- Test: `web/src/components/NotificationBell.test.tsx`
|
||||
- Modify: `web/src/components/Layout.tsx` (mobile header actions ~lines 151-168; also mount `useNotificationsLive()` once in the Layout component body)
|
||||
|
||||
- [ ] **Step 1: Failing test**
|
||||
|
||||
```typescript
|
||||
// Mocks: @/hooks/queries/notifications (useUnreadCount → 3, useNotificationsList → two items,
|
||||
// useMarkRead → mutate spy), react-router (MemoryRouter wrapper).
|
||||
// Asserts: badge renders "3"; opening the popover (click bell button, role="button" name /notifications/i)
|
||||
// shows both item titles; "Mark all read" calls markRead with {all:true};
|
||||
// item with link renders an anchor/Link to that link.
|
||||
```
|
||||
|
||||
Write it concretely following an existing component test's structure (see `web/src/components/AppSidebar.test.tsx` for provider wrapping conventions). Badge cap: 4+ digits render "99+" when count > 99 — include one assertion with count 120.
|
||||
|
||||
- [ ] **Step 2: Implement NotificationBell**
|
||||
|
||||
```tsx
|
||||
import { Bell } from "lucide-react";
|
||||
import { Link } from "react-router";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
||||
import {
|
||||
useMarkRead,
|
||||
useNotificationsList,
|
||||
useUnreadCount,
|
||||
} from "@/hooks/queries/notifications";
|
||||
|
||||
export function NotificationBell() {
|
||||
const { data: count = 0 } = useUnreadCount();
|
||||
const list = useNotificationsList({});
|
||||
const markRead = useMarkRead();
|
||||
const items = (list.data?.pages ?? []).flatMap((p) => p.items ?? []).slice(0, 10);
|
||||
|
||||
return (
|
||||
<Popover>
|
||||
<PopoverTrigger asChild>
|
||||
<Button variant="ghost" size="icon" aria-label="Notifications" className="relative">
|
||||
<Bell className="h-5 w-5" />
|
||||
{count > 0 && (
|
||||
<Badge className="absolute -top-1 -right-1 h-4 min-w-4 px-1 text-[10px]">
|
||||
{count > 99 ? "99+" : count}
|
||||
</Badge>
|
||||
)}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent align="end" className="w-80 p-0">
|
||||
<div className="flex items-center justify-between border-b px-3 py-2">
|
||||
<span className="text-sm font-medium">Notifications</span>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={count === 0}
|
||||
onClick={() => markRead.mutate({ all: true })}
|
||||
>
|
||||
Mark all read
|
||||
</Button>
|
||||
</div>
|
||||
<ul className="max-h-96 list-none overflow-y-auto">
|
||||
{items.length === 0 && (
|
||||
<li className="text-muted-foreground px-3 py-6 text-center text-sm">
|
||||
You're all caught up
|
||||
</li>
|
||||
)}
|
||||
{items.map((n) => (
|
||||
<li key={n.id} className={n.read_at ? "opacity-60" : ""}>
|
||||
<Link to={n.link ?? "/notifications"} className="hover:bg-accent block px-3 py-2">
|
||||
<div className="text-sm font-medium">{n.title}</div>
|
||||
{n.body && <div className="text-muted-foreground truncate text-xs">{n.body}</div>}
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<div className="border-t px-3 py-2 text-center">
|
||||
<Link to="/notifications" className="text-primary text-sm">
|
||||
View all
|
||||
</Link>
|
||||
</div>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
Verify the ui primitives exist with these export names (`badge.tsx`, `button.tsx`, `popover.tsx` under `components/ui/`); adjust imports/props to the actual variants. Match `react-router` import name used elsewhere (the repo uses `react-router` v7 — check whether components import `Link` from `react-router` or `react-router-dom`).
|
||||
|
||||
Dropdown-open toast suppression (spec requirement): export a tiny module-level flag from NotificationBell —
|
||||
|
||||
```tsx
|
||||
let bellOpenFlag = false;
|
||||
export function isNotificationDropdownOpen() {
|
||||
return bellOpenFlag;
|
||||
}
|
||||
// in the component: <Popover onOpenChange={(open) => { bellOpenFlag = open; }}>
|
||||
```
|
||||
|
||||
and in `useNotificationsLive`'s onEvent, before toasting: `if (isNotificationDropdownOpen()) return;` (cache updates still run — only the toast is suppressed; the open dropdown shows the new item via the list invalidation). Add one live-hook test: set the flag via a popover-open simulation or export a test-only setter, assert no toast while open.
|
||||
|
||||
- [ ] **Step 3: Mount in Layout**
|
||||
|
||||
In `Layout.tsx`: call `useNotificationsLive()` at the top of the component (single mount point for the live hook), and render `<NotificationBell />` in the mobile header's right-side action cluster (next to the search icon button).
|
||||
|
||||
- [ ] **Step 4: Tests green, typecheck, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/components/NotificationBell.test.tsx 2>&1 | tail -5 && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add src/components/NotificationBell.* src/components/Layout.tsx src/hooks/
|
||||
git commit -m "feat(web): notification bell with live badge in mobile header"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 5: Sidebar nav item with badge (desktop)
|
||||
|
||||
**Files:**
|
||||
- Modify: `web/src/components/AppSidebar.tsx` (nav list — insert after the History item, ~line 645)
|
||||
- Test: extend `web/src/components/AppSidebar.test.tsx`
|
||||
|
||||
- [ ] **Step 1: Add the nav item**
|
||||
|
||||
Follow the exact ViewTransitionLink pattern of the History item (active indicator span, icon sizing, `SidebarLabel`):
|
||||
|
||||
```tsx
|
||||
<li>
|
||||
<ViewTransitionLink
|
||||
to="/notifications"
|
||||
onClick={onNavigate}
|
||||
className={navLinkClass("/notifications")}
|
||||
aria-current={isActive("/notifications") ? "page" : undefined}
|
||||
>
|
||||
{isActive("/notifications") && (
|
||||
<span
|
||||
className="absolute top-1/2 left-0 h-[18px] w-[3px] -translate-y-1/2 rounded-r-sm"
|
||||
style={{ background: "var(--primary)" }}
|
||||
/>
|
||||
)}
|
||||
<Bell className="h-[18px] w-[18px] shrink-0" />
|
||||
{unreadCount > 0 && (
|
||||
<span className="bg-primary text-primary-foreground absolute top-1 left-6 flex h-4 min-w-4 items-center justify-center rounded-full px-1 text-[10px]">
|
||||
{unreadCount > 99 ? "99+" : unreadCount}
|
||||
</span>
|
||||
)}
|
||||
<SidebarLabel show={showLabels}>Notifications</SidebarLabel>
|
||||
</ViewTransitionLink>
|
||||
</li>
|
||||
```
|
||||
|
||||
`const { data: unreadCount = 0 } = useUnreadCount();` at the top of AppSidebar (import from `@/hooks/queries/notifications`; `Bell` from lucide-react — add to the existing lucide import list at line ~56). Badge positioning: verify visually against the collapsed-sidebar state (`showLabels` false) — the absolute chip must remain visible on the icon in both states; tweak offsets if the icon container differs.
|
||||
|
||||
- [ ] **Step 2: Test**
|
||||
|
||||
Extend AppSidebar.test.tsx: mock `@/hooks/queries/notifications` `useUnreadCount` to return `{ data: 5 }`; assert the rendered sidebar contains a link to `/notifications` showing "5". Follow the file's existing mock arrangement for other hooks.
|
||||
|
||||
- [ ] **Step 3: Run, typecheck, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/components/AppSidebar.test.tsx 2>&1 | tail -4
|
||||
git add src/components/AppSidebar.* && git commit -m "feat(web): notifications sidebar item with unread badge"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 6: Inbox page + route
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/pages/Notifications.tsx`
|
||||
- Test: `web/src/pages/Notifications.test.tsx`
|
||||
- Modify: `web/src/App.tsx` (user routes block ~line 331+)
|
||||
|
||||
- [ ] **Step 1: Failing test**
|
||||
|
||||
Mock the notifications query hooks. Assertions:
|
||||
- renders rows for items across categories with title/body/relative time;
|
||||
- category filter tabs (All, Requests, Content, Announcements, System — admin tab only when `useAuth().user?.role === "admin"`) change the `category` filter passed to `useNotificationsList` (capture via mock);
|
||||
- unread rows visually distinct (e.g. a `data-unread="true"` attribute — assert on it);
|
||||
- per-row dismiss button calls `useDismissNotification().mutate(id)`;
|
||||
- "Load more" appears when `hasNextPage` and calls `fetchNextPage`;
|
||||
- on mount, visible unread ids are marked read: `useMarkRead().mutate({ ids: [...] })` (assert called once with the unread ids from page one).
|
||||
|
||||
- [ ] **Step 2: Implement**
|
||||
|
||||
Page structure (use the repo's page conventions — check an existing simple page like the History/Collections page for header/container classes):
|
||||
|
||||
```tsx
|
||||
const CATEGORY_TABS: { key: string; label: string; adminOnly?: boolean }[] = [
|
||||
{ key: "", label: "All" },
|
||||
{ key: "request", label: "Requests" },
|
||||
{ key: "content", label: "Content" },
|
||||
{ key: "announcement", label: "Announcements" },
|
||||
{ key: "system", label: "System" },
|
||||
{ key: "admin", label: "Admin", adminOnly: true },
|
||||
];
|
||||
```
|
||||
|
||||
- `useState` for active category; `useNotificationsList({ category })` (omit when "");
|
||||
- mark-on-view effect:
|
||||
|
||||
```tsx
|
||||
const firstPage = list.data?.pages?.[0];
|
||||
const markRead = useMarkRead();
|
||||
const markedRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (markedRef.current || !firstPage) return;
|
||||
const unreadIds = (firstPage.items ?? []).filter((n) => !n.read_at).map((n) => n.id);
|
||||
if (unreadIds.length > 0) markRead.mutate({ ids: unreadIds });
|
||||
markedRef.current = true;
|
||||
}, [firstPage, markRead]);
|
||||
```
|
||||
|
||||
- rows: title, body, `created_at` rendered with the repo's existing relative-time helper (grep `formatDistance\|relativeTime\|timeAgo` in web/src/lib — use what exists; if nothing, use `Intl.RelativeTimeFormat` inline helper in the page);
|
||||
- dismiss button (X icon) per row; "Mark all read" header button; "Load more" via `fetchNextPage` when `hasNextPage`;
|
||||
- empty state per tab.
|
||||
|
||||
- [ ] **Step 3: Route**
|
||||
|
||||
In `App.tsx`, register alongside the other authenticated user pages (match the guard used by e.g. the favorites/history page — likely inside the layout route with profile required):
|
||||
|
||||
```tsx
|
||||
<Route path="/notifications" element={<Notifications />} />
|
||||
```
|
||||
|
||||
(Lazy-load if neighboring pages are lazy — match the file's import pattern.)
|
||||
|
||||
- [ ] **Step 4: Tests, typecheck, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/pages/Notifications.test.tsx 2>&1 | tail -5 && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add src/pages/Notifications.* src/App.tsx && git commit -m "feat(web): notifications inbox page"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 7: Settings preferences section
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/pages/settings/NotificationsSettings.tsx`
|
||||
- Modify: `web/src/pages/SettingsLayout.tsx` (NAV_SECTIONS), `web/src/App.tsx` (settings child route)
|
||||
|
||||
- [ ] **Step 1: Implement the settings page**
|
||||
|
||||
Template: `web/src/pages/settings/PlaybackSettings.tsx` (SettingsGroup/SettingRow/Switch composition). Content:
|
||||
|
||||
```tsx
|
||||
const PREF_LABELS: Record<string, { label: string; description: string; adminOnly?: boolean }> = {
|
||||
request: { label: "Requests", description: "Updates when your requests are approved, declined, or ready" },
|
||||
content: { label: "New content", description: "New arrivals matching your watchlist, favorites, and shows in progress" },
|
||||
system: { label: "Account & security", description: "Password changes and account notices" },
|
||||
admin: { label: "Admin alerts", description: "Failed jobs and scans (admins only)", adminOnly: true },
|
||||
content_digest: { label: "Daily digest", description: "One daily summary of everything added to your libraries" },
|
||||
};
|
||||
```
|
||||
|
||||
- `useNotificationPreferences()` for current values; render a `Switch` per category in PREF_LABELS order, skipping `adminOnly` entries when `useAuth().user?.role !== "admin"`;
|
||||
- on toggle: `useSetNotificationPreferences().mutate([{ category, enabled }])` (single-category PUT — server upserts per row);
|
||||
- note under the group: "Announcements from your server admin can't be turned off."
|
||||
|
||||
- [ ] **Step 2: Register in SettingsLayout NAV_SECTIONS**
|
||||
|
||||
Add a "Notifications" section (icon `Bell`) pointing at path `notifications`, following the structure at SettingsLayout.tsx lines 39-100. Register the child route in App.tsx where other settings pages are routed (find `PlaybackSettings` route registration and mirror it).
|
||||
|
||||
- [ ] **Step 3: Test**
|
||||
|
||||
Extend `web/src/pages/SettingsLayout.test.tsx` if it asserts nav items (check); add `NotificationsSettings` rendering test only if sibling settings pages have tests (check `ls web/src/pages/settings/*.test.tsx`) — match local convention; if none exist, rely on the layout test + typecheck (note this in the commit message).
|
||||
|
||||
- [ ] **Step 4: Typecheck, test, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -3 && pnpm exec vitest run src/pages/SettingsLayout.test.tsx 2>&1 | tail -4
|
||||
git add src/pages/settings/NotificationsSettings.tsx src/pages/SettingsLayout.tsx src/App.tsx
|
||||
git commit -m "feat(web): notification preference toggles in settings"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 8: Admin announcements page
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/pages/AdminAnnouncements.tsx`
|
||||
- Modify: `web/src/components/AdminSidebar.tsx` (nav items ~lines 81-134), `web/src/App.tsx` (admin routes ~lines 363-398)
|
||||
|
||||
- [ ] **Step 1: Implement the page**
|
||||
|
||||
Template: `web/src/pages/AdminUsers.tsx` for the table + dialog patterns (read it first; reuse its Table/Dialog/Button composition). Features:
|
||||
|
||||
- Table of announcements: title, audience summary ("Everyone" / "N users" / "Libraries: a, b"), created date, expires date, delete button with confirm dialog ("Deleting dismisses unread copies from user inboxes.").
|
||||
- "New announcement" button → Dialog with: title (Input, required), body (Textarea), audience radio group (Everyone / Specific users / Libraries) — for "Specific users" a multi-select of users (check how AdminUsers fetches the user list — reuse that hook; if a multi-select primitive is missing, comma-separated ids input is NOT acceptable — use checkboxes in a scrollable list), for Libraries the same against the libraries list hook (grep `useLibraries` in hooks/queries) — optional `expires_at` datetime-local input.
|
||||
- Submit via `useCreateAnnouncement()`; disable submit until title non-empty and a valid audience selection exists.
|
||||
|
||||
- [ ] **Step 2: Nav + route**
|
||||
|
||||
AdminSidebar: add item (icon `Megaphone` from lucide-react) labeled "Announcements" → `/admin/announcements`, following the existing items' structure. App.tsx admin routes: `<Route path="announcements" element={<AdminAnnouncements />} />`.
|
||||
|
||||
- [ ] **Step 3: Test**
|
||||
|
||||
`web/src/pages/AdminAnnouncements.test.tsx` — mock the three admin hooks; assert: table renders rows incl. audience summaries; create dialog validates (submit disabled with empty title); submitting with Everyone calls `useCreateAnnouncement().mutate` with `{audience: {all: true}, ...}`; delete confirm calls `useDeleteAnnouncement().mutate(id)`.
|
||||
|
||||
- [ ] **Step 4: Tests, typecheck, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/pages/AdminAnnouncements.test.tsx 2>&1 | tail -5 && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add src/pages/AdminAnnouncements.* src/components/AdminSidebar.tsx src/App.tsx
|
||||
git commit -m "feat(web): admin announcements management page"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 9: Full verification
|
||||
|
||||
- [ ] **Step 1: Web checks**
|
||||
|
||||
```bash
|
||||
cd web && pnpm run lint && pnpm run format:check && pnpm exec vitest run 2>&1 | tail -6 && pnpm run build 2>&1 | tail -4
|
||||
```
|
||||
|
||||
All green; build also refreshes `web/dist` so the Go embed keeps working.
|
||||
|
||||
- [ ] **Step 2: Backend still green**
|
||||
|
||||
```bash
|
||||
GOWORK=off go build ./... && GOWORK=off go test ./internal/api/handlers/ 2>&1 | tail -2
|
||||
make verify-local-paths
|
||||
```
|
||||
|
||||
- [ ] **Step 3: E2E smoke against the dev stack**
|
||||
|
||||
```bash
|
||||
docker compose up -d postgres redis
|
||||
GOWORK=off go build -o /tmp/silo-web-e2e ./cmd/silo && /tmp/silo-web-e2e &
|
||||
```
|
||||
|
||||
In a browser (or report for manual check): log in, confirm the sidebar Notifications item + mobile bell render; create an announcement in /admin/announcements; verify toast fires live (WS) and badge increments without reload; open inbox, rows mark read, badge clears; toggle a preference off and verify it persists after reload.
|
||||
|
||||
- [ ] **Step 4: Commit anything outstanding; report**
|
||||
|
||||
---
|
||||
|
||||
## Self-review notes
|
||||
|
||||
- Desktop bell+dropdown from the spec adapted to a sidebar nav item + badge (no desktop top bar exists in this app); the dropdown ships on the mobile header bell. Spec intent (always-visible unread indicator + 1-click access) preserved.
|
||||
- Toast child/profile filtering is client-side by necessity (WS frames are user-scoped; see spec Future work on claims enrichment) — `useNotificationsLive` suppresses other-profile and child-restricted frames.
|
||||
- `content_digest` rows arrive as category `content` (server stores digest as content/content.digest) — the inbox Content tab includes them; the preferences page exposes the separate `content_digest` toggle. No UI filters by `content_digest` category (final server review noted this trap).
|
||||
- Mark-on-view marks only page one's unread ids once per mount — deliberate; deeper pages mark via "Mark all read".
|
||||
- Several steps include verify-greps (ui primitive names, Link import source, relative-time helper, settings test conventions, multi-select availability) — implementers must align with what exists rather than invent.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,838 +0,0 @@
|
||||
# Core Push — Web Client & Admin Config — Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Make push operable end-to-end from the browser: a Web Push client (service worker, subscribe, receive, per-device control, device list) plus an admin provider-config page (APNs/FCM/WebPush credentials, VAPID keygen, readiness), on top of the frozen push API.
|
||||
|
||||
**Architecture:** Five web units + two small Go endpoints. A static service worker receives pushes; a `usePushDevice` hook owns the browser subscription lifecycle; the existing notifications-settings page gains a push control + device list; a new admin page reuses the existing encrypted-settings write path for credentials. Two net-new server endpoints: VAPID keygen and a self-targeted test push.
|
||||
|
||||
**Tech Stack:** React 19, TanStack Query, sonner, the existing api client (auto device/profile headers), browser Push API / Service Worker / Notifications API, Go (chi), webpush-go (already a dep). Vitest + RTL.
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-06-10-core-push-web-client-design.md`. The push registration API + delivery worker are frozen (Phase-3-server).
|
||||
|
||||
Commands assume the repository root is the cwd; web commands run in `web/` with `pnpm`. Go: `export GOWORK=off`.
|
||||
|
||||
## Frozen API the client calls
|
||||
|
||||
```
|
||||
GET /api/v1/notifications/push/webpush-key → {vapid_public_key}
|
||||
PUT /api/v1/notifications/push/device {transport, token} → 204 (needs X-Profile-Id; api client sends it)
|
||||
DELETE /api/v1/notifications/push/device → 204
|
||||
GET /api/v1/notifications/push/devices → {devices:[{device_id,name,platform,transport,push_enabled,registered_at}]}
|
||||
PUT /api/v1/notifications/push/devices/{device_id} {enabled} → 204
|
||||
GET /api/v1/admin/push/status → {apns,fcm,webpush: bool}
|
||||
PUT /api/v1/admin/settings/{key} {value} → writes an encrypted push.* key (existing)
|
||||
GET /api/v1/admin/settings/sensitive-status → {configured:[keys]} (existing)
|
||||
```
|
||||
|
||||
Net-new this plan: `POST /api/v1/admin/push/generate-vapid-keys` and `POST /api/v1/admin/push/test`.
|
||||
|
||||
## File map
|
||||
|
||||
| File | Action | Responsibility |
|
||||
|---|---|---|
|
||||
| `internal/api/handlers/push.go` | modify | add `HandleGenerateVAPIDKeys`, `HandleSendTestPush`; add a `notifier` dep |
|
||||
| `internal/api/router.go` | modify | register the two admin routes |
|
||||
| `cmd/silo/main.go` | modify | pass the notifications service into the push handler |
|
||||
| `web/public/sw.js` | create | service worker: push, notificationclick, pushsubscriptionchange |
|
||||
| `web/src/lib/pushSw.ts` | create | pure SW logic (notification options, click URL) — tested; mirrored in sw.js |
|
||||
| `web/src/lib/push.ts` | create | `urlBase64ToUint8Array`, VAPID-key cache helpers |
|
||||
| `web/src/main.tsx` | modify | register `/sw.js` when supported |
|
||||
| `web/src/api/types.ts` | modify | `PushDeviceInfo`, `VapidKeyPair` |
|
||||
| `web/src/hooks/queries/push.ts` | create | query/mutation hooks (webpush-key, devices, toggle, register/revoke, admin keygen/test) |
|
||||
| `web/src/hooks/usePushDevice.ts` | create | browser subscription lifecycle state machine |
|
||||
| `web/src/hooks/usePushDevice.test.tsx` | create | hook tests |
|
||||
| `web/src/pages/settings/NotificationsSettings.tsx` | modify | push control + device list |
|
||||
| `web/src/pages/admin-settings/PushSettings.tsx` | create | three credential cards + keygen + test |
|
||||
| `web/src/pages/admin-settings/AdminSettingsLayout.tsx` | modify | nav entry |
|
||||
|
||||
---
|
||||
|
||||
### Task 1: Server endpoints — VAPID keygen + test push
|
||||
|
||||
**Files:**
|
||||
- Modify: `internal/api/handlers/push.go`, `internal/api/router.go`, `cmd/silo/main.go`
|
||||
- Test: `internal/api/handlers/push_test.go`
|
||||
|
||||
- [ ] **Step 1: Add a notifier dep + the two handlers**
|
||||
|
||||
In `push.go`, add a narrow interface and a field on `PushHandler`:
|
||||
|
||||
```go
|
||||
// pushNotifier creates a notification for the calling user (test push).
|
||||
type pushNotifier interface {
|
||||
CreateSystem(ctx context.Context, userID int, typ, title, body string)
|
||||
}
|
||||
```
|
||||
|
||||
Add `notifier pushNotifier` to `PushHandler` and a parameter to `NewPushHandler` (nil-tolerant — test-push 503s if absent). `*notifications.Service` satisfies `pushNotifier` (it has `CreateSystem(ctx, userID int, typ, title, body string)` from Phase-1).
|
||||
|
||||
Handlers:
|
||||
|
||||
```go
|
||||
func (h *PushHandler) HandleGenerateVAPIDKeys(w http.ResponseWriter, r *http.Request) {
|
||||
priv, pub, err := webpush.GenerateVAPIDKeys()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "failed to generate keys")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"vapid_public": pub, "vapid_private": priv})
|
||||
}
|
||||
|
||||
func (h *PushHandler) HandleSendTestPush(w http.ResponseWriter, r *http.Request) {
|
||||
if h.notifier == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "notifications unavailable")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
if userID == 0 {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "auth required")
|
||||
return
|
||||
}
|
||||
h.notifier.CreateSystem(r.Context(), userID,
|
||||
"system.test_push", "Test push",
|
||||
"If you can see this on a device, push delivery is working.")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
```
|
||||
|
||||
Import `webpush "github.com/SherClockHolmes/webpush-go"`. **Verify the `GenerateVAPIDKeys()` return order** against the installed version — webpush-go returns `(privateKey, publicKey string, err error)`. Confirm by reading the vendored signature or `go doc`; if it's `(public, private, err)` swap accordingly. Get this right — swapping the keys silently breaks push.
|
||||
|
||||
- [ ] **Step 2: Routes**
|
||||
|
||||
In `router.go`, in the admin push block (next to `r.Get("/push/status", ...)`, ~line 2295, behind `deps.PushStore != nil`):
|
||||
|
||||
```go
|
||||
r.Post("/push/generate-vapid-keys", pushHandler.HandleGenerateVAPIDKeys)
|
||||
r.Post("/push/test", pushHandler.HandleSendTestPush)
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Wire the notifier**
|
||||
|
||||
Update every `NewPushHandler(...)` call site (find with grep) to pass the notifications service. In `cmd/silo/main.go` and/or `router.go` where the push handler is constructed, add `deps.NotificationsService` (or the in-scope `notificationsSvc`) as the third arg. Build will tell you the sites.
|
||||
|
||||
- [ ] **Step 4: Tests**
|
||||
|
||||
Add to `push_test.go` (reuse the package's fakes / chi-request helper from Task-13 push tests):
|
||||
|
||||
```go
|
||||
func TestPushHandleGenerateVAPIDKeys_ReturnsPair(t *testing.T) {
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.HandleGenerateVAPIDKeys(rr, httptest.NewRequest(http.MethodPost, "/", nil))
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("code = %d", rr.Code)
|
||||
}
|
||||
var body map[string]string
|
||||
_ = json.Unmarshal(rr.Body.Bytes(), &body)
|
||||
if body["vapid_public"] == "" || body["vapid_private"] == "" || body["vapid_public"] == body["vapid_private"] {
|
||||
t.Fatalf("bad key pair: %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeNotifier struct{ called bool; userID int; typ string }
|
||||
|
||||
func (f *fakeNotifier) CreateSystem(_ context.Context, userID int, typ, _, _ string) {
|
||||
f.called = true
|
||||
f.userID = userID
|
||||
f.typ = typ
|
||||
}
|
||||
|
||||
func TestPushHandleSendTestPush_CreatesForCaller(t *testing.T) {
|
||||
n := &fakeNotifier{}
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, n)
|
||||
r := httptest.NewRequest(http.MethodPost, "/", nil)
|
||||
r = r.WithContext(apimw.SetUserID(r.Context(), 7)) // use the package's claims-injection helper if SetUserID differs
|
||||
rr := httptest.NewRecorder()
|
||||
h.HandleSendTestPush(rr, r)
|
||||
if rr.Code != http.StatusAccepted || !n.called || n.userID != 7 || n.typ != "system.test_push" {
|
||||
t.Fatalf("unexpected: code=%d notifier=%+v", rr.Code, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleSendTestPush_NilNotifier503(t *testing.T) {
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, nil)
|
||||
rr := httptest.NewRecorder()
|
||||
h.HandleSendTestPush(rr, httptest.NewRequest(http.MethodPost, "/", nil))
|
||||
if rr.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("code = %d", rr.Code)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Check how the existing push tests inject the user id into context (Task-13 used a claims/`apimw` helper — mirror it; `apimw.SetUserID` may not exist, use whatever the package's tests already use to set `GetUserID`).
|
||||
|
||||
- [ ] **Step 5: Build, test, commit**
|
||||
|
||||
```bash
|
||||
GOWORK=off go build ./... && GOWORK=off go test ./internal/api/handlers/ -run TestPush -v 2>&1 | tail -8 && GOWORK=off go vet ./internal/api/handlers/
|
||||
git add internal/api/handlers/push.go internal/api/handlers/push_test.go internal/api/router.go cmd/silo/main.go
|
||||
git commit -m "feat(push): admin VAPID keygen and self-test endpoints"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 2: Service worker + registration + helpers
|
||||
|
||||
**Files:**
|
||||
- Create: `web/public/sw.js`, `web/src/lib/pushSw.ts`, `web/src/lib/push.ts`
|
||||
- Test: `web/src/lib/pushSw.test.ts`, `web/src/lib/push.test.ts`
|
||||
- Modify: `web/src/main.tsx`
|
||||
|
||||
- [ ] **Step 1: Pure SW logic (tested) — `web/src/lib/pushSw.ts`**
|
||||
|
||||
```ts
|
||||
export interface PushPayload {
|
||||
id?: number;
|
||||
title?: string;
|
||||
body?: string;
|
||||
link?: string;
|
||||
category?: string;
|
||||
}
|
||||
|
||||
export interface SwNotification {
|
||||
title: string;
|
||||
options: { body: string; data: { link: string }; tag: string };
|
||||
}
|
||||
|
||||
/** Maps a raw push payload to showNotification args; tolerant of partial/missing fields. */
|
||||
export function buildNotification(payload: PushPayload | null): SwNotification {
|
||||
const p = payload ?? {};
|
||||
const link = p.link && p.link.length > 0 ? p.link : "/notifications";
|
||||
const tag = p.id != null ? `n${p.id}` : "n";
|
||||
return {
|
||||
title: p.title && p.title.length > 0 ? p.title : "Silo",
|
||||
options: { body: p.body ?? "", data: { link }, tag },
|
||||
};
|
||||
}
|
||||
|
||||
/** The URL a notification click should open/focus. */
|
||||
export function clickUrl(data: unknown): string {
|
||||
const d = (data ?? {}) as { link?: string };
|
||||
return d.link && d.link.length > 0 ? d.link : "/notifications";
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Test it — `web/src/lib/pushSw.test.ts`**
|
||||
|
||||
```ts
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildNotification, clickUrl } from "./pushSw";
|
||||
|
||||
describe("buildNotification", () => {
|
||||
it("maps a full payload", () => {
|
||||
const n = buildNotification({ id: 5, title: "Approved", body: "Dune", link: "/requests" });
|
||||
expect(n.title).toBe("Approved");
|
||||
expect(n.options).toEqual({ body: "Dune", data: { link: "/requests" }, tag: "n5" });
|
||||
});
|
||||
it("falls back on missing fields", () => {
|
||||
const n = buildNotification(null);
|
||||
expect(n.title).toBe("Silo");
|
||||
expect(n.options.body).toBe("");
|
||||
expect(n.options.data.link).toBe("/notifications");
|
||||
expect(n.options.tag).toBe("n");
|
||||
});
|
||||
});
|
||||
|
||||
describe("clickUrl", () => {
|
||||
it("uses link when present", () => expect(clickUrl({ link: "/items/9" })).toBe("/items/9"));
|
||||
it("falls back to inbox", () => expect(clickUrl({})).toBe("/notifications"));
|
||||
});
|
||||
```
|
||||
|
||||
Run: `pnpm exec vitest run src/lib/pushSw.test.ts` → green.
|
||||
|
||||
- [ ] **Step 3: The static service worker — `web/public/sw.js`**
|
||||
|
||||
Self-contained (served statically, not bundled). Mirror the pure logic exactly (the canonical, tested source is `pushSw.ts`):
|
||||
|
||||
```js
|
||||
// web/public/sw.js — static service worker for Web Push.
|
||||
// NOTE: the buildNotification/clickUrl logic mirrors src/lib/pushSw.ts (the
|
||||
// tested source of truth). Keep them in sync.
|
||||
|
||||
function buildNotification(payload) {
|
||||
const p = payload || {};
|
||||
const link = p.link && p.link.length > 0 ? p.link : "/notifications";
|
||||
const tag = p.id != null ? "n" + p.id : "n";
|
||||
return {
|
||||
title: p.title && p.title.length > 0 ? p.title : "Silo",
|
||||
options: { body: p.body || "", data: { link: link }, tag: tag },
|
||||
};
|
||||
}
|
||||
function clickUrl(data) {
|
||||
const d = data || {};
|
||||
return d.link && d.link.length > 0 ? d.link : "/notifications";
|
||||
}
|
||||
|
||||
self.addEventListener("push", (event) => {
|
||||
let payload = null;
|
||||
try {
|
||||
payload = event.data ? event.data.json() : null;
|
||||
} catch (e) {
|
||||
payload = null;
|
||||
}
|
||||
const n = buildNotification(payload);
|
||||
event.waitUntil(self.registration.showNotification(n.title, n.options));
|
||||
});
|
||||
|
||||
self.addEventListener("notificationclick", (event) => {
|
||||
event.notification.close();
|
||||
const url = clickUrl(event.notification.data);
|
||||
event.waitUntil(
|
||||
self.clients.matchAll({ type: "window", includeUncontrolled: true }).then((wins) => {
|
||||
for (const c of wins) {
|
||||
if ("focus" in c) {
|
||||
c.postMessage({ type: "notification-click", link: url });
|
||||
return c.focus();
|
||||
}
|
||||
}
|
||||
return self.clients.openWindow(url);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("pushsubscriptionchange", (event) => {
|
||||
// Best-effort re-subscribe using the cached VAPID key; the app re-registers
|
||||
// the new subscription on next load if this fails.
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
try {
|
||||
const cache = await caches.open("silo-push");
|
||||
const res = await cache.match("vapid-public-key");
|
||||
if (!res) return;
|
||||
const vapid = await res.text();
|
||||
const sub = await self.registration.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: vapid,
|
||||
});
|
||||
await fetch("/api/v1/notifications/push/device", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ transport: "webpush", token: JSON.stringify(sub.toJSON()) }),
|
||||
});
|
||||
} catch (e) {
|
||||
// swallow; app re-registers on next load
|
||||
}
|
||||
})(),
|
||||
);
|
||||
});
|
||||
```
|
||||
|
||||
Note: the `pushsubscriptionchange` re-register relies on cookie auth (`credentials: include`) and the device/profile headers it can't set from the SW — it is genuinely best-effort and the app heals it on next load (documented in the spec). `applicationServerKey` accepts a base64url string in modern browsers; if the target browsers need a Uint8Array here, the app-side path (Task 4) converts it — the SW path is the fallback and may no-op on stricter browsers, which is acceptable.
|
||||
|
||||
- [ ] **Step 4: App-side helpers — `web/src/lib/push.ts`**
|
||||
|
||||
```ts
|
||||
/** Convert a base64url VAPID public key to the Uint8Array applicationServerKey wants. */
|
||||
export function urlBase64ToUint8Array(base64: string): Uint8Array {
|
||||
const padding = "=".repeat((4 - (base64.length % 4)) % 4);
|
||||
const b64 = (base64 + padding).replace(/-/g, "+").replace(/_/g, "/");
|
||||
const raw = atob(b64);
|
||||
const out = new Uint8Array(raw.length);
|
||||
for (let i = 0; i < raw.length; i++) out[i] = raw.charCodeAt(i);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Cache the raw VAPID public key so the SW can re-subscribe without the app. */
|
||||
export async function cacheVapidKey(base64: string): Promise<void> {
|
||||
if (!("caches" in self)) return;
|
||||
const cache = await caches.open("silo-push");
|
||||
await cache.put("vapid-public-key", new Response(base64));
|
||||
}
|
||||
|
||||
export function pushSupported(): boolean {
|
||||
return (
|
||||
typeof navigator !== "undefined" &&
|
||||
"serviceWorker" in navigator &&
|
||||
typeof window !== "undefined" &&
|
||||
"PushManager" in window &&
|
||||
"Notification" in window
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 5: Test the converter — `web/src/lib/push.test.ts`**
|
||||
|
||||
```ts
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { urlBase64ToUint8Array } from "./push";
|
||||
|
||||
describe("urlBase64ToUint8Array", () => {
|
||||
it("decodes a url-safe base64 key to bytes", () => {
|
||||
// "AQID" → [1,2,3]
|
||||
expect(Array.from(urlBase64ToUint8Array("AQID"))).toEqual([1, 2, 3]);
|
||||
});
|
||||
it("handles url-safe chars and missing padding", () => {
|
||||
const out = urlBase64ToUint8Array("a-_9"); // '-'→'+', '_'→'/'
|
||||
expect(out.length).toBe(3);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
- [ ] **Step 6: Register the SW — `web/src/main.tsx`**
|
||||
|
||||
After `createRoot(...).render(...)`, add:
|
||||
|
||||
```tsx
|
||||
if ("serviceWorker" in navigator && "PushManager" in window) {
|
||||
window.addEventListener("load", () => {
|
||||
navigator.serviceWorker.register("/sw.js").catch(() => {
|
||||
// registration failure is non-fatal; push just won't be available
|
||||
});
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 7: Run, build, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/lib/pushSw.test.ts src/lib/push.test.ts 2>&1 | tail -5 && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add web/public/sw.js web/src/lib/pushSw.ts web/src/lib/pushSw.test.ts web/src/lib/push.ts web/src/lib/push.test.ts web/src/main.tsx
|
||||
git commit -m "feat(web): web push service worker, registration, and helpers"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 3: Push query hooks + types
|
||||
|
||||
**Files:**
|
||||
- Modify: `web/src/api/types.ts`
|
||||
- Create: `web/src/hooks/queries/push.ts`
|
||||
- Modify: `web/src/hooks/queries/keys.ts` (add `pushKeys`)
|
||||
|
||||
- [ ] **Step 1: Types** — append to `types.ts`:
|
||||
|
||||
```ts
|
||||
export interface PushDeviceInfo {
|
||||
device_id: string;
|
||||
name: string;
|
||||
platform: string;
|
||||
transport: string;
|
||||
push_enabled: boolean;
|
||||
registered_at?: string;
|
||||
}
|
||||
|
||||
export interface VapidKeyPair {
|
||||
vapid_public: string;
|
||||
vapid_private: string;
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Keys** — in `keys.ts`:
|
||||
|
||||
```ts
|
||||
export const pushKeys = {
|
||||
all: ["push"] as const,
|
||||
webpushKey: () => [...pushKeys.all, "webpush-key"] as const,
|
||||
devices: () => [...pushKeys.all, "devices"] as const,
|
||||
status: () => [...pushKeys.all, "status"] as const,
|
||||
};
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Hooks — `web/src/hooks/queries/push.ts`**
|
||||
|
||||
Mirror `admin/users.ts` conventions (api import, toast, invalidation).
|
||||
|
||||
```ts
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { api } from "@/api/client";
|
||||
import type { PushDeviceInfo, VapidKeyPair } from "@/api/types";
|
||||
import { pushKeys } from "@/hooks/queries/keys";
|
||||
|
||||
export function useWebPushPublicKey() {
|
||||
return useQuery({
|
||||
queryKey: pushKeys.webpushKey(),
|
||||
queryFn: () => api<{ vapid_public_key: string }>("/notifications/push/webpush-key"),
|
||||
select: (d) => d.vapid_public_key,
|
||||
staleTime: 5 * 60 * 1000,
|
||||
});
|
||||
}
|
||||
|
||||
export function usePushDevices() {
|
||||
return useQuery({
|
||||
queryKey: pushKeys.devices(),
|
||||
queryFn: () => api<{ devices: PushDeviceInfo[] | null }>("/notifications/push/devices"),
|
||||
select: (d) => d.devices ?? [],
|
||||
});
|
||||
}
|
||||
|
||||
export function useTogglePushDevice() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ deviceId, enabled }: { deviceId: string; enabled: boolean }) =>
|
||||
api(`/notifications/push/devices/${deviceId}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ enabled }),
|
||||
}),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: pushKeys.devices() }),
|
||||
onError: (err) => toast.error(err instanceof Error ? err.message : "Failed to update device"),
|
||||
});
|
||||
}
|
||||
|
||||
export function usePushStatus() {
|
||||
return useQuery({
|
||||
queryKey: pushKeys.status(),
|
||||
queryFn: () => api<{ apns: boolean; fcm: boolean; webpush: boolean }>("/admin/push/status"),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGenerateVapidKeys() {
|
||||
return useMutation({
|
||||
mutationFn: () => api<VapidKeyPair>("/admin/push/generate-vapid-keys", { method: "POST" }),
|
||||
onError: (err) => toast.error(err instanceof Error ? err.message : "Failed to generate keys"),
|
||||
});
|
||||
}
|
||||
|
||||
export function useSendTestPush() {
|
||||
return useMutation({
|
||||
mutationFn: () => api("/admin/push/test", { method: "POST" }),
|
||||
onSuccess: () => toast.success("Test push queued — watch for a banner"),
|
||||
onError: (err) => toast.error(err instanceof Error ? err.message : "Failed to send test"),
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
(Register/revoke device calls live inside `usePushDevice` (Task 4) since they're tied to the subscription, not standalone query hooks.)
|
||||
|
||||
- [ ] **Step 4: Typecheck + commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add web/src/api/types.ts web/src/hooks/queries/keys.ts web/src/hooks/queries/push.ts
|
||||
git commit -m "feat(web): push query hooks and types"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 4: `usePushDevice` lifecycle hook
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/hooks/usePushDevice.ts`, `web/src/hooks/usePushDevice.test.tsx`
|
||||
|
||||
- [ ] **Step 1: Failing tests**
|
||||
|
||||
Mock `navigator.serviceWorker`, `window.PushManager`, `Notification`, the api client, and the push helpers. Assert the status machine and the enable/disable flows.
|
||||
|
||||
```tsx
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { apiMock, cacheVapidMock } = vi.hoisted(() => ({
|
||||
apiMock: vi.fn(),
|
||||
cacheVapidMock: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/api/client", () => ({ api: apiMock }));
|
||||
vi.mock("@/lib/push", async (orig) => ({
|
||||
...(await orig<typeof import("@/lib/push")>()),
|
||||
cacheVapidKey: cacheVapidMock,
|
||||
}));
|
||||
|
||||
import { usePushDevice } from "./usePushDevice";
|
||||
|
||||
function wrap() {
|
||||
const qc = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={qc}>{children}</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
||||
let subscribeMock: ReturnType<typeof vi.fn>;
|
||||
let getSubMock: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
apiMock.mockReset();
|
||||
cacheVapidMock.mockReset();
|
||||
subscribeMock = vi.fn(async () => ({ toJSON: () => ({ endpoint: "https://x", keys: {} }) }));
|
||||
getSubMock = vi.fn(async () => null);
|
||||
(globalThis as any).PushManager = function () {};
|
||||
(navigator as any).serviceWorker = {
|
||||
register: vi.fn(),
|
||||
ready: Promise.resolve({ pushManager: { subscribe: subscribeMock, getSubscription: getSubMock } }),
|
||||
};
|
||||
(globalThis as any).Notification = { permission: "default", requestPermission: vi.fn(async () => "granted") };
|
||||
});
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
describe("usePushDevice", () => {
|
||||
it("reports unsupported when PushManager missing", () => {
|
||||
delete (globalThis as any).PushManager;
|
||||
const { result } = renderHook(() => usePushDevice(), { wrapper: wrap() });
|
||||
expect(result.current.status).toBe("unsupported");
|
||||
});
|
||||
|
||||
it("reports blocked when permission denied", () => {
|
||||
(globalThis as any).Notification.permission = "denied";
|
||||
const { result } = renderHook(() => usePushDevice(), { wrapper: wrap() });
|
||||
expect(result.current.status).toBe("blocked");
|
||||
});
|
||||
|
||||
it("enable(): permission → key → subscribe → PUT device, caches key, status on", async () => {
|
||||
apiMock.mockImplementation((path: string) => {
|
||||
if (path.includes("webpush-key")) return Promise.resolve({ vapid_public_key: "AQID" });
|
||||
return Promise.resolve(undefined); // PUT device
|
||||
});
|
||||
const { result } = renderHook(() => usePushDevice(), { wrapper: wrap() });
|
||||
await act(async () => { await result.current.enable(); });
|
||||
expect(subscribeMock).toHaveBeenCalledOnce();
|
||||
expect(cacheVapidMock).toHaveBeenCalledWith("AQID");
|
||||
const putCall = apiMock.mock.calls.find((c) => c[0] === "/notifications/push/device");
|
||||
expect(putCall?.[1]).toMatchObject({ method: "PUT" });
|
||||
expect(JSON.parse(putCall![1].body)).toMatchObject({ transport: "webpush" });
|
||||
await waitFor(() => expect(result.current.status).toBe("on"));
|
||||
});
|
||||
|
||||
it("enable(): permission denied short-circuits, no subscribe", async () => {
|
||||
(globalThis as any).Notification.requestPermission = vi.fn(async () => "denied");
|
||||
const { result } = renderHook(() => usePushDevice(), { wrapper: wrap() });
|
||||
await act(async () => { await result.current.enable(); });
|
||||
expect(subscribeMock).not.toHaveBeenCalled();
|
||||
expect(result.current.status).toBe("blocked");
|
||||
});
|
||||
|
||||
it("disable(): unsubscribe + DELETE, status off", async () => {
|
||||
const unsub = vi.fn(async () => true);
|
||||
getSubMock.mockResolvedValue({ unsubscribe: unsub });
|
||||
apiMock.mockResolvedValue(undefined);
|
||||
const { result } = renderHook(() => usePushDevice(), { wrapper: wrap() });
|
||||
await act(async () => { await result.current.disable(); });
|
||||
expect(unsub).toHaveBeenCalled();
|
||||
const del = apiMock.mock.calls.find((c) => c[0] === "/notifications/push/device" && c[1]?.method === "DELETE");
|
||||
expect(del).toBeTruthy();
|
||||
await waitFor(() => expect(result.current.status).toBe("off"));
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run, verify fail**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/hooks/usePushDevice.test.tsx 2>&1 | tail -6
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Implement — `web/src/hooks/usePushDevice.ts`**
|
||||
|
||||
```ts
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { api } from "@/api/client";
|
||||
import { cacheVapidKey, pushSupported, urlBase64ToUint8Array } from "@/lib/push";
|
||||
|
||||
export type PushDeviceStatus = "unsupported" | "blocked" | "off" | "on" | "pending";
|
||||
|
||||
export function usePushDevice() {
|
||||
const [status, setStatus] = useState<PushDeviceStatus>("pending");
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
if (!pushSupported()) {
|
||||
setStatus("unsupported");
|
||||
return;
|
||||
}
|
||||
if (Notification.permission === "denied") {
|
||||
setStatus("blocked");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
const sub = await reg.pushManager.getSubscription();
|
||||
setStatus(sub ? "on" : "off");
|
||||
} catch {
|
||||
setStatus("off");
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void refresh();
|
||||
}, [refresh]);
|
||||
|
||||
const enable = useCallback(async () => {
|
||||
if (!pushSupported()) {
|
||||
setStatus("unsupported");
|
||||
return;
|
||||
}
|
||||
setStatus("pending");
|
||||
const perm = await Notification.requestPermission();
|
||||
if (perm !== "granted") {
|
||||
setStatus(perm === "denied" ? "blocked" : "off");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
const { vapid_public_key } = await api<{ vapid_public_key: string }>(
|
||||
"/notifications/push/webpush-key",
|
||||
);
|
||||
if (!vapid_public_key) {
|
||||
setStatus("off");
|
||||
return;
|
||||
}
|
||||
await cacheVapidKey(vapid_public_key);
|
||||
const sub = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(vapid_public_key),
|
||||
});
|
||||
await api("/notifications/push/device", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ transport: "webpush", token: JSON.stringify(sub.toJSON()) }),
|
||||
});
|
||||
setStatus("on");
|
||||
} catch {
|
||||
setStatus("off");
|
||||
}
|
||||
}, []);
|
||||
|
||||
const disable = useCallback(async () => {
|
||||
setStatus("pending");
|
||||
try {
|
||||
const reg = await navigator.serviceWorker.ready;
|
||||
const sub = await reg.pushManager.getSubscription();
|
||||
if (sub) await sub.unsubscribe();
|
||||
} catch {
|
||||
/* best-effort */
|
||||
}
|
||||
try {
|
||||
await api("/notifications/push/device", { method: "DELETE" });
|
||||
} catch {
|
||||
/* server may already lack the row */
|
||||
}
|
||||
setStatus("off");
|
||||
}, []);
|
||||
|
||||
return { status, enable, disable, refresh };
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Run green, typecheck, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec vitest run src/hooks/usePushDevice.test.tsx 2>&1 | tail -6 && pnpm exec tsc -b --noEmit 2>&1 | head -3
|
||||
git add web/src/hooks/usePushDevice.ts web/src/hooks/usePushDevice.test.tsx
|
||||
git commit -m "feat(web): usePushDevice subscription lifecycle hook"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 5: NotificationsSettings — push control + device list
|
||||
|
||||
**Files:**
|
||||
- Modify: `web/src/pages/settings/NotificationsSettings.tsx`
|
||||
- Test: extend the page's test if one exists (check `ls web/src/pages/settings/*.test.tsx`)
|
||||
|
||||
- [ ] **Step 1: Add a push section**
|
||||
|
||||
Read the current `NotificationsSettings.tsx` (it uses `SettingsGroup`/`SettingRow`/`Switch`). Add, below the category-preference group, a new "Push notifications" group:
|
||||
|
||||
- A row driven by `usePushDevice()`:
|
||||
- `status === "unsupported"` → static text "This browser doesn't support push notifications."
|
||||
- `status === "blocked"` → text "Notifications are blocked for this site. Re-enable them in your browser's site settings."
|
||||
- else → a `Switch` checked when `status === "on"`, disabled when `status === "pending"`, `onCheckedChange` calls `enable()` / `disable()`.
|
||||
- Below it, a device list from `usePushDevices()`: one row per device (`name || device_id`, `platform`/`transport`, `registered_at`), each with a `Switch` bound to `useTogglePushDevice().mutate({ deviceId, enabled })` reflecting `push_enabled`. Empty state: "No devices registered for push yet."
|
||||
|
||||
Match the page's existing component composition and classes. Keep `content_digest` and the existing toggles untouched.
|
||||
|
||||
- [ ] **Step 2: Test (if the page is tested)**
|
||||
|
||||
If `NotificationsSettings.test.tsx` exists, mock `usePushDevice` (return `{status:"off", enable, disable}`) and `usePushDevices` (two devices); assert: toggling the device-level switch calls `useTogglePushDevice().mutate` with the device id; the main push switch calls `enable` when off. If no sibling test exists, rely on typecheck + the hook tests and note it in the commit.
|
||||
|
||||
- [ ] **Step 3: Typecheck, test, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -3 && pnpm exec vitest run src/pages/settings 2>&1 | tail -4
|
||||
git add web/src/pages/settings/NotificationsSettings.tsx web/src/pages/settings/*.test.tsx
|
||||
git commit -m "feat(web): push on-this-device control and device list in settings"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 6: Admin PushSettings page + nav
|
||||
|
||||
**Files:**
|
||||
- Create: `web/src/pages/admin-settings/PushSettings.tsx`
|
||||
- Modify: `web/src/pages/admin-settings/AdminSettingsLayout.tsx`
|
||||
- Test: `web/src/pages/admin-settings/PushSettings.test.tsx`
|
||||
|
||||
- [ ] **Step 1: Build the page**
|
||||
|
||||
Template: `IntegrationsSettings.tsx` (credential card = `CredentialStatus` badge + `SettingField` + `useUpdateServerSetting` + `useAdminSensitiveStatus`). Three cards. Header badge per card from `usePushStatus()`.
|
||||
|
||||
- **Web Push card** (leads): fields `push.webpush.vapid_public`, `push.webpush.vapid_private`, `push.webpush.subject`. A "Generate keys" button → `useGenerateVapidKeys().mutateAsync()` fills the public + private field state (local `useState`, not auto-saved). Subject field hint: `mailto:you@example.com`. Each field saves via `updateSetting.mutateAsync({ key, value })`. A "Send test push" button → `useSendTestPush().mutate()`.
|
||||
- **APNs card**: `push.apns.p8_key` (textarea via `SettingField` multiline if supported, else a `<textarea>`), `push.apns.key_id`, `push.apns.team_id`, `push.apns.bundle_id`.
|
||||
- **FCM card**: `push.fcm.service_account_json` (textarea).
|
||||
|
||||
`configured` per field from `new Set(sensitive?.configured ?? []).has(key)`. Page header + intro like IntegrationsSettings.
|
||||
|
||||
- [ ] **Step 2: Register nav**
|
||||
|
||||
In `AdminSettingsLayout.tsx`, add to the "Connections" group's `items` (import the component + a lucide icon, e.g. `BellRing`):
|
||||
|
||||
```tsx
|
||||
{ id: "push", label: "Push Notifications", icon: BellRing, component: PushSettings },
|
||||
```
|
||||
|
||||
(`id: "push"` is stable URL state.)
|
||||
|
||||
- [ ] **Step 3: Test — `PushSettings.test.tsx`**
|
||||
|
||||
Mock `useUpdateServerSetting` (capture mutate args), `useAdminSensitiveStatus` (some keys configured), `usePushStatus` (webpush true), `useGenerateVapidKeys` (returns a pair), `useSendTestPush`. Assert: three cards render; webpush badge shows ready; saving the VAPID public field calls `updateSetting.mutateAsync({ key: "push.webpush.vapid_public", value })`; "Generate keys" populates the public/private fields; "Send test push" calls the mutation. Follow the admin-settings test conventions (check a sibling `*.test.tsx` in that dir; if none, write a minimal RTL test with the mocks).
|
||||
|
||||
- [ ] **Step 4: Typecheck, test, build, commit**
|
||||
|
||||
```bash
|
||||
cd web && pnpm exec tsc -b --noEmit 2>&1 | head -3 && pnpm exec vitest run src/pages/admin-settings 2>&1 | tail -5
|
||||
git add web/src/pages/admin-settings/PushSettings.tsx web/src/pages/admin-settings/PushSettings.test.tsx web/src/pages/admin-settings/AdminSettingsLayout.tsx
|
||||
git commit -m "feat(web): admin push provider config page"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Task 7: Full verification + manual smoke
|
||||
|
||||
- [ ] **Step 1: Web gates**
|
||||
|
||||
```bash
|
||||
cd web && pnpm run lint 2>&1 | tail -3 && pnpm run format:check 2>&1 | tail -2 && pnpm exec vitest run 2>&1 | tail -5 && pnpm run build 2>&1 | tail -3
|
||||
```
|
||||
|
||||
All green (lint warnings allowed, errors not). Fix any new-code lint/format issues (prettier --write the new files if format:check flags them). `build` also confirms `public/sw.js` is copied to `dist/` (Vite copies `public/` verbatim).
|
||||
|
||||
- [ ] **Step 2: Backend gates**
|
||||
|
||||
```bash
|
||||
GOWORK=off go build ./... && GOWORK=off go test ./internal/api/handlers/ -run TestPush 2>&1 | tail -3 && make verify-local-paths
|
||||
```
|
||||
|
||||
- [ ] **Step 3: Manual browser smoke (documented; requires a real browser over HTTPS or localhost)**
|
||||
|
||||
Service workers + Push require a secure context — `localhost` qualifies. Build the server with the web assets and run on a localhost port.
|
||||
Commands assume the repository root is the cwd.
|
||||
|
||||
```bash
|
||||
cd web && pnpm run build && cd ..
|
||||
GOWORK=off go build -o ./bin/silo-pushweb ./cmd/silo && (PORT=18080 JF_PORT=18096 ./bin/silo-pushweb > ./silo-pushweb.log 2>&1 &)
|
||||
```
|
||||
|
||||
Then in a Chromium browser at `http://localhost:18080`:
|
||||
1. Log in as admin. Go to Admin → Settings → Push Notifications. Click "Generate keys", Save the VAPID public/private + a `mailto:` subject. Confirm the Web Push badge flips to "Ready" (and `GET /admin/push/status` → webpush:true).
|
||||
2. Go to Settings → Notifications. Toggle "Push on this device" on → grant the browser permission prompt. Confirm a row appears in the device list and the toggle stays on.
|
||||
3. Click "Send test push" on the admin page (or create an announcement). Within ~30–45s (grace + worker tick) a desktop notification banner appears. Close the app tab first to prove background delivery.
|
||||
4. Click the banner → the app opens/focuses at the deep link.
|
||||
5. Toggle push off → confirm `DELETE` and the device drops from the list.
|
||||
|
||||
Record the outcome. (If a real browser isn't available in the harness, run steps 1–2 via curl to confirm the API wiring — register a webpush device, confirm `push_deliveries` row + status — and note the visual steps as operator-verified.)
|
||||
|
||||
- [ ] **Step 4: Commit any verification fixes; report.** Work stays on `feat/core-notifications-server` (or a dedicated branch off it).
|
||||
|
||||
---
|
||||
|
||||
## Self-review notes (resolved during writing)
|
||||
|
||||
- `webpush.GenerateVAPIDKeys()` returns `(privateKey, publicKey, err)` in webpush-go — Task 1 Step 1 flags verifying the order so the keys aren't swapped.
|
||||
- The SW's pure logic is duplicated between `public/sw.js` (untestable static file) and the tested `src/lib/pushSw.ts`; the duplication is small and intentional, with sw.js annotated as mirroring the tested source. The reviewer should diff the two functions for drift.
|
||||
- `usePushDevice` owns register/revoke (tied to the live subscription); standalone query hooks (`push.ts`) cover read/list/toggle/admin. No overlap.
|
||||
- Test push reuses `notifications.Service.CreateSystem` (Phase-1) → a real system notification → exercises the full enqueue→worker→transport pipeline to the caller's own devices. No bespoke push path.
|
||||
- Admin credential writes use the existing `PUT /admin/settings/{key}` (encrypted, redacted) — no new write endpoint; only keygen + test are net-new server surface.
|
||||
- iOS Safari web push (installed-PWA only) is handled by `pushSupported()` returning false in a normal tab → `unsupported` state; documented, not specially coded.
|
||||
- Several steps embed verify-greps (NewPushHandler call sites, GenerateVAPIDKeys order, SettingField multiline support, sibling test conventions, the user-id context injection helper in push_test.go) — align with reality.
|
||||
@@ -0,0 +1,276 @@
|
||||
# Plan: Personal + Server sections on the user Collections tab
|
||||
|
||||
Status: Draft
|
||||
Branch: `feat/collections-server-section`
|
||||
Owner: TBD
|
||||
Commands assume the repository root is the cwd.
|
||||
|
||||
## Problem
|
||||
|
||||
The user-facing **Collections** tab on the home screen (`web/src/pages/Collections.tsx`)
|
||||
only shows the signed-in profile's *personal* collections. For most users this list is
|
||||
empty or nearly empty, so the tab looks broken or pointless.
|
||||
|
||||
Meanwhile, server-curated collections (admin-created "library collections") only appear
|
||||
*inside each individual library's* Collections tab (`web/src/pages/LibraryCollections.tsx`,
|
||||
served by `GET /library/{id}/collections`). There is no place in the app where a user can
|
||||
see the collections curated across the whole server — they are effectively hidden behind
|
||||
per-library navigation.
|
||||
|
||||
We want the top-level Collections tab to become the single home for collections:
|
||||
|
||||
1. **Your collections** — the user's personal/shared collections, with a large section
|
||||
title, shown at the top (current behavior, kept intact: grouping, drag-reorder, create,
|
||||
edit, sync, delete).
|
||||
2. **Server collections** — below that, a clearly titled section aggregating the visible
|
||||
server (library) collections across every library the user can access, so they are no
|
||||
longer buried per-library.
|
||||
|
||||
## Current state (what exists today)
|
||||
|
||||
### Frontend
|
||||
- `web/src/pages/Collections.tsx` — the top-level tab. Renders the page header + a
|
||||
`GroupedCollectionsBoard` of personal collections. Data via `useCollections()` /
|
||||
`useCollectionGroups()` (`web/src/hooks/queries/collections.ts`), both backed by
|
||||
`GET /collections` → `CollectionsListResponse { collections, groups }`.
|
||||
- `web/src/pages/LibraryCollections.tsx` — per-library tab. Renders a poster grid
|
||||
(`CollectionPosterCard`) grouped by admin groups + ungrouped. Data via
|
||||
`useLibraryCollections(libraryId)` → `GET /library/{id}/collections` →
|
||||
`LibraryTabResponse { groups, ungrouped }`. **This is the visual we want to reuse for the
|
||||
server section** (poster cards, item-count badge, navigate to catalog href).
|
||||
- `web/src/api/types.ts` — `Collection`, `CollectionGroup`, `CollectionsListResponse`,
|
||||
`LibraryTabResponse`, `LibraryTabGroup`, `LibraryTabCollection`, `LibraryTabUngrouped`.
|
||||
- Catalog hrefs: `buildLibraryCollectionCatalogHref(id, title)` and
|
||||
`buildUserCollectionCatalogHref(id, title)` in `web/src/pages/catalogSearchParams.ts`.
|
||||
|
||||
### Backend
|
||||
- `internal/api/handlers/collections.go` — personal collection handlers under `/collections`
|
||||
(registered in `internal/api/router.go` ~lines 1700-1726, behind `RequireProfile`).
|
||||
- `internal/api/handlers/library_collections.go`:
|
||||
- `HandleListLibraryCollections` (line 813) — builds the per-library `LibraryTabResponse`.
|
||||
- `requestCanAccessLibrary` (line 3311) — checks `access.GetScope(ctx).AllowedLibraryIDs`
|
||||
(nil ⇒ access to all libraries).
|
||||
- `LibraryCollectionRepository.ListAll(ctx, libraryID *int, opts)`
|
||||
(`internal/catalog/library_collection_repo.go:368`) — **with `libraryID == nil` it
|
||||
already returns server collections across all libraries** (uses
|
||||
`libraryCollectionScopeFallbackJoin`), filtering `visibility = 'visible'` unless
|
||||
`IncludeHidden`. This is the aggregation primitive we need.
|
||||
- `presignGPURL` / `toLibraryCollectionResponses` — turn stored poster paths into signed
|
||||
URLs for the response.
|
||||
|
||||
## Design decision: aggregate on the server, new endpoint
|
||||
|
||||
We add a new **user-facing aggregate endpoint** rather than fanning out N per-library calls
|
||||
from the client (avoids N round-trips, keeps access/visibility filtering server-side, and
|
||||
matches the "Performance first / Reliability first" priorities in `CLAUDE.md`).
|
||||
|
||||
### New endpoint
|
||||
|
||||
`GET /collections/server` (registered next to the other `/collections` routes in
|
||||
`internal/api/router.go`, behind `RequireProfile`).
|
||||
|
||||
Response shape (new types, mirrors the poster-card needs without per-library group noise):
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"libraries": [
|
||||
{
|
||||
"library_id": 3,
|
||||
"library_name": "Movies",
|
||||
"total_count": 80, // total visible server collections in this library
|
||||
"collections": [ // CAPPED teaser slice (see "Scale & pagination")
|
||||
{
|
||||
"id": "...",
|
||||
"title": "...",
|
||||
"poster_url": "https://signed...",
|
||||
"poster_thumbhash": "...",
|
||||
"item_count": 42,
|
||||
"featured": false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Grouping by library (with `library_name`) is the recommended default: it lets the frontend
|
||||
render "Server collections" as one **horizontal scrollable row per library**, which keeps a
|
||||
large catalog legible. (Alternative: a single flat list — simpler UI, but loses provenance
|
||||
and the per-library "See all" affordance. Recommend per-library grouping.)
|
||||
|
||||
### Scale & pagination (many collections per library)
|
||||
|
||||
The server section is a **discovery** surface aggregating across *all* accessible libraries
|
||||
at once, so a full wrapping grid per library does not scale (e.g. 5 libraries × 80
|
||||
collections = 400 stacked tiles). Decisions:
|
||||
|
||||
- **Render each library as a horizontal carousel row**, not a wrapping grid. Heading = the
|
||||
library name + a **"See all →"** link.
|
||||
- **Cap the response** at ~20 collections per library and include `total_count`. Ordering of
|
||||
the capped slice follows the repo's existing `featured DESC, sort_order ASC, title ASC`
|
||||
("best first"), so the teaser row surfaces featured/curated collections.
|
||||
- **"See all" links to the existing per-library Collections tab**
|
||||
(`/library/{id}` Collections tab, served by the unchanged
|
||||
`GET /library/{id}/collections`), which already renders the full wrapping grid with admin
|
||||
grouping. We do **not** build a new full-aggregate view — reuse the canonical
|
||||
"show everything for this library" surface.
|
||||
- This caps payload size and the per-library query cost, and keeps the home tab fast
|
||||
("Performance first").
|
||||
|
||||
### API contract impact
|
||||
|
||||
- `GET /collections` (personal) — **unchanged**.
|
||||
- `GET /library/{id}/collections` (per-library tab, "See all" target) — **unchanged**;
|
||||
reused as-is.
|
||||
- `GET /collections/server` — **new, additive**. No existing response changes; no
|
||||
Android/Apple contract break.
|
||||
|
||||
### Why a separate endpoint, not extending `GET /collections`
|
||||
|
||||
It is tempting to make `GET /collections` return both personal and server collections in one
|
||||
call. We deliberately do **not**, for three reasons:
|
||||
|
||||
- **Client safety / semantics.** `GET /collections`'s `collections[]` array is the user's
|
||||
*editable, personal* shelves — web and the Android/Apple clients render reorder/edit/
|
||||
delete/sync affordances on it. Mixing read-only server collections into that same array is
|
||||
a *silent semantic break*: the JSON still parses, so nothing errors, but clients would
|
||||
offer "delete"/"reorder" on collections the user can't mutate (→ 403/404). A new sibling
|
||||
field (`{ collections, groups, server }`) would be additive and parse-safe, but still
|
||||
couples discovery into the personal-management contract.
|
||||
- **Cache / refetch lifecycle.** `GET /collections` is the heavily-mutated query —
|
||||
invalidated on every create, edit, reorder, group change, and sync (via
|
||||
`invalidateUserCollectionQueries`). Server collections don't change on a personal reorder,
|
||||
so folding them in would refetch the whole server-wide catalog on every personal mutation
|
||||
("Performance first" violation). Separate endpoints ⇒ independent cache keys + lifecycles.
|
||||
- **Zero downstream risk.** A brand-new `/collections/server` endpoint cannot break existing
|
||||
clients, because nothing calls it yet. Consolidating into one call later is a *deliberate
|
||||
follow-up requiring coordinated Android/Apple work* — and must use a distinct field, never
|
||||
overload `collections[]`.
|
||||
|
||||
### Handler logic (`HandleListServerCollections`)
|
||||
|
||||
1. Resolve accessible library IDs: read `access.GetScope(ctx)`. If `AllowedLibraryIDs ==
|
||||
nil`, the user can see all libraries → list all of them; else restrict to that set.
|
||||
2. List visible server collections:
|
||||
- Simplest correct approach: call `repo.ListAll(ctx, nil, ListLibraryCollectionsOptions{})`
|
||||
once (server-wide, visible-only), then **filter each collection to libraries the user
|
||||
can access** and bucket by library. This requires knowing each collection's
|
||||
library membership (`library_collection_libraries`). If `ListAll` does not already
|
||||
return membership, either (a) add a per-library loop calling
|
||||
`repo.ListByLibrary(ctx, libID, opts)` for each accessible library (bounded by library
|
||||
count, each query already does the grouping/sort), or (b) extend the repo to return
|
||||
library membership. **Recommended: per-accessible-library loop** — reuses the existing,
|
||||
tested `ListByLibrary` path and naturally yields the per-library buckets and respects
|
||||
scope. Deduplicate collections that span multiple libraries if we choose flat output;
|
||||
for per-library output, a collection legitimately appears under each of its libraries.
|
||||
3. For each library, set `total_count` to the full visible count, then **cap
|
||||
`collections` to ~20** (keep the repo's `featured DESC, sort_order ASC, title ASC`
|
||||
ordering so featured/curated collections lead). Define the cap as a single named
|
||||
constant.
|
||||
4. Resolve library display names (catalog/library store lookup) for `library_name`.
|
||||
5. Presign poster URLs via the existing `presignGPURL` helper — only for the capped slice,
|
||||
so we never presign 100 posters we won't send.
|
||||
6. Skip empty libraries. Return `200` with `{ libraries: [] }` when nothing is visible.
|
||||
|
||||
Reuse existing helpers (`presignGPURL`, sort helpers like `applyCollectionSort`) rather than
|
||||
duplicating logic. Add the new response structs next to the existing
|
||||
`libraryTabResponse` types in `library_collections.go`, or in `collections.go` if cleaner —
|
||||
keep them in the package that owns the behavior.
|
||||
|
||||
## Frontend changes
|
||||
|
||||
### Types (`web/src/api/types.ts`)
|
||||
Add:
|
||||
```ts
|
||||
export interface ServerCollectionsLibrary {
|
||||
library_id: number;
|
||||
library_name: string;
|
||||
total_count: number; // total visible; collections[] is a capped teaser slice
|
||||
collections: LibraryTabCollection[]; // reuse existing shape
|
||||
}
|
||||
export interface ServerCollectionsResponse {
|
||||
libraries: ServerCollectionsLibrary[];
|
||||
}
|
||||
```
|
||||
|
||||
### Query hook (`web/src/hooks/queries/collections.ts` + `keys.ts`)
|
||||
- Add `collectionKeys.server()` to `web/src/hooks/queries/keys.ts`.
|
||||
- Add `useServerCollections()` → `GET /collections/server` →
|
||||
`ServerCollectionsResponse`. Independent query key from `/collections`, so the personal
|
||||
section keeps its single round-trip and the server section loads in parallel.
|
||||
|
||||
### Page (`web/src/pages/Collections.tsx`)
|
||||
Restructure into two titled sections inside the existing `page-shell`:
|
||||
|
||||
1. **"Your collections"** section
|
||||
- Add a large section title (e.g. `<h2 class="page-title ...">Your collections</h2>`)
|
||||
above the existing `GroupedCollectionsBoard` / empty-state block. Keep all current
|
||||
personal-collection behavior unchanged (create, group, reorder, sync, delete).
|
||||
- The page-level `<h1>Collections</h1>` header + subtitle + action buttons stay at the
|
||||
very top.
|
||||
|
||||
2. **"Server collections"** section (new)
|
||||
- Title `<h2>Server collections</h2>` with a short subtitle ("Curated across every
|
||||
library on this server").
|
||||
- Use `useServerCollections()`. Render **one horizontal scrollable row per library**
|
||||
(carousel), not a wrapping grid. Each poster card reuses the **same visual as
|
||||
`LibraryCollections.tsx`** — extract `CollectionPosterCard` into a shared component so
|
||||
both pages use it (avoids duplicate logic per `CLAUDE.md` maintainability rule).
|
||||
Proposed location: `web/src/components/collections/CollectionPosterCard.tsx`.
|
||||
- For each `library` bucket: a sub-heading row with `library_name` and a **"See all →"**
|
||||
link routing to that library's existing Collections tab (the unchanged
|
||||
`GET /library/{id}/collections` surface). Show "See all" only when
|
||||
`total_count > collections.length`; optionally include the count, e.g. "See all (80)".
|
||||
- Cards navigate via `buildLibraryCollectionCatalogHref(id, title)` and show the
|
||||
item-count badge. These are admin/library collections (`kind="regular"`); the
|
||||
sidebar-pin affordance can remain — pass `library.library_id` as `libraryId`.
|
||||
- Use the project's existing horizontal-row/carousel pattern if one exists (check
|
||||
`web/src/components` for a shelf/row component used on the home screen) rather than
|
||||
hand-rolling overflow scrolling; reuse it for consistent snap/scroll behavior.
|
||||
- Loading: skeleton row(s) (reuse the existing skeleton block pattern).
|
||||
- Empty: if `libraries` is empty, render nothing (or a muted "No server collections yet"
|
||||
line) — do not show a scary empty state, since the personal section above may have
|
||||
content.
|
||||
|
||||
### Refactor note
|
||||
Extracting `CollectionPosterCard` is the only shared-logic extraction required. Keep the
|
||||
extraction behavior-identical for `LibraryCollections.tsx` (props: `collection`, `kind`,
|
||||
`libraryId`). The per-library tab keeps its wrapping grid; only the new server section uses
|
||||
horizontal rows.
|
||||
|
||||
## Out of scope / non-goals
|
||||
- No changes to how admin/library collections are created or to per-library tabs.
|
||||
- No reordering/editing of server collections from the user Collections tab (read-only).
|
||||
- No new visibility model — we honor existing `visibility = 'visible'` + access scope.
|
||||
|
||||
## Risks / follow-ups
|
||||
- **Access scope correctness**: must not leak collections from libraries outside
|
||||
`AllowedLibraryIDs`. Mitigate by driving the query from accessible library IDs, and add a
|
||||
test for a restricted-scope user.
|
||||
- **Collections spanning multiple libraries** appear once per library in per-library output;
|
||||
confirm this is the desired UX (vs. dedup). Decide before implementation.
|
||||
- **Performance**: per-accessible-library loop is N queries. Acceptable for typical library
|
||||
counts; if N grows large, revisit with a single membership-aware query. Add an index check
|
||||
on `library_collection_libraries(library_id)` (likely already present).
|
||||
- **Client parity**: this is a new server endpoint and a web-only UI change. Android/Apple
|
||||
clients have their own collections surfaces — file follow-up issues if they should mirror
|
||||
the personal+server split. No API contract break (purely additive endpoint).
|
||||
- **Empty-state UX** for users with no personal AND no server collections — ensure the page
|
||||
still reads sensibly.
|
||||
|
||||
## Verification
|
||||
- Backend: `go build ./...`, `go vet ./...`, and a handler/unit test for
|
||||
`HandleListServerCollections` covering (a) full access, (b) restricted scope, (c) empty.
|
||||
- Frontend: `cd web && pnpm run lint && pnpm run format:check`; `make verify-local-paths`.
|
||||
- Manual: log in as a normal profile with empty personal collections; confirm the
|
||||
"Your collections" section shows the empty/create state and the "Server collections"
|
||||
section lists library collections grouped by library with correct posters, counts, and
|
||||
working navigation into each collection's catalog view.
|
||||
|
||||
## Implementation order
|
||||
1. Backend: response types + `HandleListServerCollections` + route registration.
|
||||
2. Backend test for access-scope filtering.
|
||||
3. Frontend: types + `useServerCollections` hook + `collectionKeys.server()`.
|
||||
4. Frontend: extract shared `CollectionPosterCard`; refactor `LibraryCollections.tsx`.
|
||||
5. Frontend: restructure `Collections.tsx` into the two titled sections.
|
||||
6. Lint/format/build/manual verification.
|
||||
@@ -0,0 +1,388 @@
|
||||
# Silo Notifications — Architecture Overview
|
||||
|
||||
**Date:** 2026-04-28
|
||||
**Status:** Draft
|
||||
**Scope:** Cross-cutting design covering all delivery channels: durable in-app inbox, realtime websocket, Apple Push (APNs), Android Push (FCM), and outbound webhooks.
|
||||
**Companion docs:**
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md) — Foundation
|
||||
- [`02-apns-relay.md`](./02-apns-relay.md) — Apple push
|
||||
- [`03-fcm-relay.md`](./03-fcm-relay.md) — Android push
|
||||
- [`04-outbound-webhooks.md`](./04-outbound-webhooks.md) — Discord and generic webhooks
|
||||
|
||||
## Goal
|
||||
|
||||
Silo should notify a profile when a new episode of a series they care about becomes available in their library. "Care about" means at least one of: the series is favorited, watchlisted, in continue-watching, or has a profile-eligible next-up cursor.
|
||||
|
||||
The notification should reach the profile through whichever delivery channels the profile has configured: an in-app inbox with realtime push to connected clients, mobile push notifications to Apple and Android devices, and outbound webhooks to user-chosen destinations like a Discord channel.
|
||||
|
||||
## Why this is hard
|
||||
|
||||
Three constraints pull in different directions:
|
||||
|
||||
1. **Self-hosted privacy.** Silo is self-hosted media. Notification content (episode titles, posters, profile names, library identity) must never leave the user's server unless the user has explicitly opted into a destination that requires it.
|
||||
2. **Mobile push gatekeeping.** Apple and Google require pushes to the official App Store / Play Store builds to be authenticated by the credentials of the developer account that signed and published the app. There is no fully self-hosted, zero-Silo-infrastructure path to push to the official mobile apps.
|
||||
3. **Multi-profile fairness.** A single user account on a Silo server can have multiple profiles (kid, adult, parent, etc.). Notifications must be scoped to the active profile. Devices, push tokens, webhooks, and preferences are all profile-scoped.
|
||||
|
||||
The design resolves these by:
|
||||
|
||||
- Routing all notification work through a durable per-profile inbox that lives on the user's own server. Everything else is a transport on top of this inbox.
|
||||
- Operating a privacy-preserving relay for APNs and FCM that holds the official-app credentials and accepts only opaque push requests. Self-hosted servers opt in. The relay never sees notification content.
|
||||
- Profile-scoping every database row, every API surface, every websocket subscription, and every push registration.
|
||||
|
||||
## Mental model
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────┐
|
||||
│ │
|
||||
ingest/scan ────► release_events (one row per newly-available │
|
||||
│ episode in a library) │
|
||||
│ │ │
|
||||
│ ▼ │
|
||||
│ fanout worker │
|
||||
│ │ │
|
||||
│ │ loads profile_series_interest │
|
||||
│ │ applies eligibility + preferences │
|
||||
│ ▼ │
|
||||
│ notification_deliveries (one row per │
|
||||
│ eligible profile per release) │
|
||||
│ │ │
|
||||
│ │ AFTER COMMIT │
|
||||
│ ▼ │
|
||||
│ channel dispatcher ◄── fans out to channels: │
|
||||
│ │
|
||||
│ ┌───────────────┬───────────────┬─────────┐ │
|
||||
│ │ │ │ │ │
|
||||
│ ▼ ▼ ▼ ▼ │
|
||||
│ websocket apple push android webhooks│
|
||||
│ (in-app) (APNs relay push (per │
|
||||
│ or custom) (FCM) profile)│
|
||||
│ │ │ │ │ │
|
||||
└───┼───────────────┼───────────────┼─────────┼───┘
|
||||
▼ ▼ ▼ ▼
|
||||
connected iOS / tvOS / Android Discord /
|
||||
browser tabs macOS apps apps user URL
|
||||
and apps (wake then (wake) (content
|
||||
fetch metadata) included)
|
||||
```
|
||||
|
||||
Three invariants make this work:
|
||||
|
||||
- **The durable row is the source of truth.** Realtime websocket events, mobile pushes, and webhooks are all triggered *after* the `notification_deliveries` row commits. If any transport fails, the inbox row still exists and shows up on next reconnect or refresh.
|
||||
- **The fanout worker decides recipients exactly once.** Recipients are derived from a compact `profile_series_interest` index keyed by `(library_id, series_id)`. Per-channel transports are purely about delivery — they never make recipient decisions.
|
||||
- **Dispatch enqueue is durable (outbox).** The same fanout transaction that inserts `notification_deliveries` also inserts `pending` per-target attempt rows for the push and webhook channels (`push_delivery_attempts` / `webhook_delivery_attempts`). The post-commit dispatcher *claims* those rows for immediate delivery; recovery workers sweep stale `pending` rows. A crash between delivery commit and dispatch therefore delays pushes/webhooks instead of silently dropping them. The websocket channel deliberately has no outbox row — it is best-effort because the inbox snapshot covers reconnect. See `01-release-events-and-inbox.md` "Transaction and Concurrency Rules".
|
||||
|
||||
## Channels at a glance
|
||||
|
||||
| Channel | Where it lives | Trust model | Content visibility |
|
||||
|---|---|---|---|
|
||||
| **In-app inbox** | User's own server, accessed via authenticated REST API | Same auth as the rest of Silo | Full content |
|
||||
| **Realtime websocket** | User's own server, `/events/ws` channel `notifications` | Same auth, profile-scoped | Full content |
|
||||
| **Apple Push (APNs)** | Silo-operated relay (or admin-supplied custom APNs credentials) | Opt-in. Self-hosters trust the relay to forward opaque requests. | Opaque payload only — app fetches content from user server after wake |
|
||||
| **Android Push (FCM)** | Silo-operated relay (or admin-supplied custom FCM credentials) | Same as APNs | Opaque data-only message — app fetches content from user server after wake |
|
||||
| **Outbound webhooks** | User-chosen destination URL (e.g., Discord, Slack, custom service) | Profile chose the destination; trust is implicit | Full content included by default. Discord type renders native embeds; generic type sends signed JSON |
|
||||
|
||||
## Triggers (what generates a notification)
|
||||
|
||||
A notification is generated when a new episode becomes available in a library and at least one profile has interest in the series:
|
||||
|
||||
- **Favorite** — the series is favorited by the profile.
|
||||
- **Watchlist** — the series is on the profile's watchlist.
|
||||
- **Continue watching** — the profile has in-progress watch state for the series.
|
||||
- **Next up** — the new episode is at or beyond the profile's `next_expected_episode_key` cursor and the profile has progression state for the series.
|
||||
|
||||
A profile may match multiple reasons for a single release event. The `notification_deliveries` row records all matching reasons in a `reason_flags` JSONB column; the fanout produces exactly one delivery per `(profile_id, release_event_id)` regardless of how many reasons matched. A second uniqueness rule spans libraries: because Silo media items are catalog-level (`media_item_libraries` is a junction), the same episode landing in two libraries (e.g., "TV" and "TV 4K") shares one `episode_id`, and at most one `episode.available` delivery is created per `(profile_id, episode_id)` — dual-quality library setups do not double-notify.
|
||||
|
||||
**Back-catalog protection:** "newly available" must mean *newly released to this server*, not *newly seen by the notifications feature*. The first scan of a newly created library and the one-time feature-enable backfill **seed** `episode_availability` without creating release events, and the fanout worker applies a per-series burst cap for bulk additions to existing libraries. Without this, importing a 200-episode back-catalog of a series with hundreds of interested profiles would generate tens of thousands of deliveries and pushes in a single scan. Full rules in `01-release-events-and-inbox.md` "Seeding and Burst Suppression".
|
||||
|
||||
**v1 explicitly does not support:**
|
||||
|
||||
- Movie additions. Silo has no movie-availability detector parallel to the per-episode one in this design; without it, there's nothing to fan out from for movies. Users *can* watchlist movies that don't yet exist in their library (via TMDB lookups), but the missing piece is the ingest-time signal that a watchlisted movie has now arrived. v2 can add a `movie_availability` table and analogous release events; the fanout/inbox/channel layers in this design are movie-ready by construction.
|
||||
- Metadata-only updates (poster refreshes, title corrections).
|
||||
- Generic library-changed notifications.
|
||||
- Aggregated notifications like "3 new episodes available." Per-episode deliveries first; aggregation is a presentation concern that can stack on top.
|
||||
|
||||
## Mode terminology
|
||||
|
||||
The design uses three vocabularies that correspond to different layers; they are intentionally not the same word. This is the canonical mapping:
|
||||
|
||||
| Layer | Terms | Where it lives |
|
||||
|---|---|---|
|
||||
| **Profile-level push mode** (UI-facing) | `off`, `in_app_only`, `private_push` | `push_devices.push_mode` |
|
||||
| **APNs wire mode** (relay request `mode` field) | `private_alert`, `background_wake` | Sent to APNs relay or used in custom APNs payload builder |
|
||||
| **FCM wire mode** (relay request `mode` field) | `private_data`, `background_wake` | Sent to FCM relay or used in custom FCM payload builder |
|
||||
|
||||
Mapping:
|
||||
|
||||
- Profile `private_push` → APNs `private_alert` for visible wakes, FCM `private_data` for visible wakes.
|
||||
- Profile `in_app_only` → no remote push at all (websocket / inbox only).
|
||||
- Profile `off` → no notifications for this device whatsoever.
|
||||
- The `background_wake` wire mode is reserved for low-noise sync (per-channel spec details). It is not exposed at profile level in v1.
|
||||
|
||||
## Preference model
|
||||
|
||||
Preferences are profile-scoped and flat. There is no per-channel × per-reason matrix.
|
||||
|
||||
```
|
||||
notification_preferences (per profile)
|
||||
├── enabled bool — global kill switch
|
||||
├── notify_favorites bool
|
||||
├── notify_watchlist bool
|
||||
├── notify_continue_watching bool
|
||||
└── notify_next_up bool
|
||||
```
|
||||
|
||||
Per-webhook reason gating uses **the same four boolean columns** on `notification_webhooks` (`notify_favorites`, `notify_watchlist`, `notify_continue_watching`, `notify_next_up`) — not a single structured `reason_filters` JSON field.
|
||||
|
||||
**Precedence:** profile preferences are a **hard gate** applied during fanout (a delivery row is not created if all matching reasons are disabled at the profile level). Per-webhook flags and per-device push mode are **additional filters** applied during dispatch — they can narrow what fires for a specific destination but cannot re-enable a reason the profile has globally disabled.
|
||||
|
||||
Each push device has a `push_mode` (`off`, `in_app_only`, `private_push`) that gates whether mobile push fires for that specific device. Each webhook has its own `enabled` flag and per-webhook `notify_favorites` / `notify_watchlist` / `notify_continue_watching` / `notify_next_up` booleans for finer control.
|
||||
|
||||
The fanout worker uses `notification_preferences` to decide whether to *create* a `notification_delivery` at all. The channel dispatcher uses per-device and per-webhook flags to decide which transports fire after the row commits.
|
||||
|
||||
This means: if a profile turns off `notify_continue_watching`, a delivery is never created for a continue-watching-only match, and no transports fire. If a profile leaves the reason flags on but disables a specific Discord webhook, the inbox row + websocket + push still fire normally — the webhook just doesn't.
|
||||
|
||||
## Mobile push: why a relay is necessary
|
||||
|
||||
Apple and Google operate the only push infrastructure that can wake a closed app on iOS and Android. To send a push to the official Silo app on the App Store or Play Store, the request must be authenticated by the credentials of the developer account that signed and published that app build:
|
||||
|
||||
- **APNs:** Apple Developer Team ID + Auth Key ID + `.p8` private key. The auth key is account-scoped (not topic-scoped); it can sign JWTs that authorize push to any bundle topic the team owns. The `apns-topic` request header selects which topic the push targets, and Apple rejects requests whose topic is not owned by the signing team.
|
||||
- **FCM:** Firebase service account JSON whose private key signs a JWT exchanged for an OAuth2 access token. The service account is scoped to a specific Firebase project, and the project owns one or more registered Android app package names. (Package + SHA-1 cert fingerprint registration matters for Firebase Authentication and App Check, but for plain FCM v1 push the service account + package name + matching FCM registration token are what's required.)
|
||||
|
||||
Distributing these credentials to thousands of self-hosted Silo installations is not viable:
|
||||
|
||||
1. It violates Apple's and Google's developer agreements, which prohibit sharing credentials outside the team.
|
||||
2. Anyone with a copy could push to every Silo app worldwide.
|
||||
3. Apple actively monitors and revokes leaked keys.
|
||||
|
||||
Therefore the design separates two paths:
|
||||
|
||||
### Hosted relay (`silo_relay`) — the default for official-app users
|
||||
|
||||
A small Silo-operated service holds the official APNs `.p8` key and FCM service account JSON. Self-hosted servers opt in by configuring a relay API key, then call the relay with opaque push requests. The relay signs and forwards to APNs / FCM. The relay never sees notification content.
|
||||
|
||||
The relay is **stateless on the request path**. Its database holds only:
|
||||
|
||||
- relay accounts (one per opt-in self-hosted server installation)
|
||||
- relay API keys (with `last_used_at`, revocable)
|
||||
- minimal per-account allowlists (which app topics this account may push to)
|
||||
- redacted operational logs (no APNs/FCM tokens, no notification content)
|
||||
|
||||
It does **not** store user identities, profile data, device-to-user mappings, or notification content. Apple and Google do the actual addressing — when a push request goes out, the relay just forwards `{token, opaque_ids}` to APNs/FCM, and APNs/FCM deliver based on the device token.
|
||||
|
||||
### Custom credentials (`custom_apns` / `custom_fcm`) — escape hatch
|
||||
|
||||
Power users, white-label deployments, or anyone running a fork that ships its own resigned mobile app can configure their own Apple/Google developer credentials. The user's Silo server signs requests directly using those credentials and sends to APNs/FCM without going through the Silo relay.
|
||||
|
||||
Custom mode is **only useful if the admin has shipped a custom-signed app build under their own developer account** with their own bundle/package ID. It does not let a self-hoster push to the official App Store / Play Store app — that's not technically possible without the official developer credentials.
|
||||
|
||||
### Addressing flow walkthrough
|
||||
|
||||
The most common question on this design is "how does the relay know which user/device to send to with thousands of servers and thousands of users?" The answer is: it doesn't, because Apple and Google handle addressing.
|
||||
|
||||
```
|
||||
1. User installs official Silo app on iPhone from App Store.
|
||||
2. App calls iOS push registration; Apple returns an opaque APNs device token
|
||||
(~100 bytes, unique per app install per device).
|
||||
3. App POSTs that token to the user's OWN server:
|
||||
POST https://my-silo.example.com/api/v1/devices/push/apple
|
||||
{ device_id, apns_token, ... }
|
||||
4. User's server stores in its push_devices table, scoped to the active profile.
|
||||
5. New episode lands. Fanout worker creates a notification_delivery row.
|
||||
6. Channel dispatcher loads the profile's enabled push_devices and POSTs
|
||||
to the relay (or signs directly via custom_apns):
|
||||
POST https://relay.silo.app/v1/apple/send
|
||||
Authorization: Bearer rk_thisServersRelayApiKey
|
||||
{
|
||||
"token": "740f...", ← APNs device token from step 3
|
||||
"topic": "com.continuum.app.ios",
|
||||
"environment": "production",
|
||||
"mode": "private_alert",
|
||||
"server_device_id": "01JOPAQUE...", ← lets app find right local server
|
||||
"delivery_id": "01JDELIVERY..." ← lets app fetch notification meta
|
||||
}
|
||||
7. Relay signs JWT with Silo's .p8, forwards to APNs:
|
||||
POST https://api.push.apple.com/3/device/740f...
|
||||
apns-topic: com.continuum.app.ios
|
||||
authorization: bearer <signed JWT>
|
||||
{generic opaque payload with silo.* fields}
|
||||
8. Apple delivers to the iPhone holding token 740f...
|
||||
9. App wakes, reads server_device_id, finds matching local server account,
|
||||
GETs full notification content from the user's own server.
|
||||
```
|
||||
|
||||
The relay knows: a relay API key was used (so: which self-hosted server installation), the server's egress IP address (inherent to any hosted relay — the server connects to it directly), an APNs device token, a timestamp, and opaque IDs. The relay does not know: which user, which profile, what notification content, what server URL, what library, or what media item. Even a fully compromised relay leaks only timing + device tokens + egress IPs, not notification content or identity. The egress IP is the closest thing to an identity leak in this design — for home hosting it identifies the household's connection — and is listed here so the privacy claims stay exhaustive rather than over-strong.
|
||||
|
||||
## Threat model summary
|
||||
|
||||
Detailed per-channel threat models live in each channel's spec. The cross-cutting summary:
|
||||
|
||||
| Channel | Adversary | Worst case |
|
||||
|---|---|---|
|
||||
| In-app inbox + websocket | Network attacker | Existing Silo auth threat model. Notifications add no new surface. |
|
||||
| APNs / FCM relay (operator) | Compromised relay or hostile relay operator | Can learn device tokens, push timing, and the server's egress IP for opted-in servers. Cannot learn user identity, notification content, server URL, or library data. Cannot fabricate meaningful pushes (only generic opaque payloads). |
|
||||
| APNs / FCM (Apple / Google) | Apple / Google as platform operator | Can see app topic, device token, generic payload, timing. No user identity, no media metadata, no server URL. |
|
||||
| Outbound webhooks | Webhook destination operator (e.g., Discord) | Sees full notification content (titles, posters, episode info) for the profile that configured the webhook. The profile chose this destination; this is the explicit trade. |
|
||||
| Outbound webhooks | Network attacker who owns a URL submitted by the user | Can receive the profile's notifications until the webhook is removed. Mitigated by HMAC for generic webhooks, HTTPS-only, host blocklist (no localhost / RFC1918 by default), and a profile-visible "last seen" / failure status. |
|
||||
|
||||
## Channel dispatcher boundary
|
||||
|
||||
A single internal `Dispatcher` interface fans the durable row out to channels. Each channel implements the same minimal contract:
|
||||
|
||||
```go
|
||||
type Dispatcher interface {
|
||||
// Called once per notification_deliveries row, AFTER the row commits.
|
||||
// Per-target dispatchers (push, webhooks) do not decide their own work:
|
||||
// the fanout transaction already enqueued one `pending` attempt row per
|
||||
// (delivery_id, target_id), where target_id is push_device.id or
|
||||
// notification_webhook.id. Dispatch claims and sends those rows; a
|
||||
// recovery worker sweeps `pending` rows whose claim never happened
|
||||
// (crash between commit and dispatch). Idempotency is therefore
|
||||
// by (delivery_id, target_id, attempt_number). The websocket dispatcher
|
||||
// has no per-target row and is instead idempotent by delivery_id alone —
|
||||
// re-publishing the same delivery_id is a no-op for connected clients.
|
||||
Dispatch(ctx context.Context, delivery NotificationDelivery) error
|
||||
}
|
||||
```
|
||||
|
||||
Implementations:
|
||||
|
||||
- `WebsocketDispatcher` — publishes a `notification.created` event on the `notifications` channel, scoped to the delivery's `(user_id, profile_id)`. Best-effort; durable row is the source of truth.
|
||||
- `ApplePushDispatcher` — selects enabled `push_devices` for the profile where `platform = 'apple'` and routes through the configured provider (`silo_relay` or `custom_apns`). Records each attempt in `push_delivery_attempts`.
|
||||
- `AndroidPushDispatcher` — same but `platform = 'android'`, routes through `silo_relay` or `custom_fcm`.
|
||||
- `WebhookDispatcher` — selects enabled `notification_webhooks` for the profile, applies per-webhook reason filters, and POSTs the channel-specific payload (Discord embed or generic JSON+HMAC). Records each attempt in `webhook_delivery_attempts`.
|
||||
|
||||
Channel failures are isolated: if APNs is down, webhooks and websocket still fire. If a webhook destination is unreachable, push still fires. The fanout worker does **not** wait for any dispatcher to succeed before marking the release event processed; the durable delivery rows plus the `pending` outbox attempt rows (enqueued in the same transaction) guarantee eventual delivery for push and webhooks even across a crash. The relay-bound dispatchers additionally pace their sends client-side (token bucket) so a burst never trips the relay's rate limits — see `02`/`03` "Relay pacing".
|
||||
|
||||
## Capability surface
|
||||
|
||||
The frontend and mobile clients need to know what's available without guessing. A single capability endpoint returns the truth:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/capability
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"in_app": { "enabled": true },
|
||||
"apple_push": {
|
||||
"available": true,
|
||||
"provider": "silo_relay",
|
||||
"supported_modes": ["private_push", "in_app_only"]
|
||||
},
|
||||
"android_push": {
|
||||
"available": false,
|
||||
"provider": "off",
|
||||
"supported_modes": ["in_app_only"]
|
||||
},
|
||||
"webhooks": {
|
||||
"available": true,
|
||||
"max_per_profile": 10,
|
||||
"supported_types": ["discord", "generic"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Clients render setup UI from this response. They never have to introspect admin settings.
|
||||
|
||||
## Data model overview
|
||||
|
||||
The full schemas live in the per-channel specs. The shared shape:
|
||||
|
||||
| Table | Purpose | Spec |
|
||||
|---|---|---|
|
||||
| `episode_availability` | "Episode E became available in library L." Idempotent. Seeded silently for back-catalog. | 01 |
|
||||
| `release_events` | One per `(library_id, episode_id)` newly-available event. | 01 |
|
||||
| `notification_library_seed_state` | Per-library marker: availability has been seeded; release events may now be emitted. | 01 |
|
||||
| `profile_series_interest` | Compact recipient index keyed by `(profile_id, library_id, series_id)`. | 01 |
|
||||
| `notification_deliveries` | Per-profile durable inbox row. Cross-library dedupe via partial unique `(profile_id, episode_id)`. | 01 |
|
||||
| `notification_preferences` | Per-profile reason toggles + master enable. | 01 |
|
||||
| `push_devices` | Per-profile-per-device push registration. Platform-tagged (`apple` / `android`). | 02 / 03 |
|
||||
| `push_delivery_attempts` | Per-attempt push log. No notification content. | 02 / 03 |
|
||||
| `notification_webhooks` | Per-profile webhook destinations (Discord or generic). | 04 |
|
||||
| `webhook_delivery_attempts` | Per-attempt webhook log. | 04 |
|
||||
|
||||
A single migration block lands the foundation tables. APNs/FCM/webhook tables can land in separate migrations as those channels ship — they don't block the v1 in-app inbox.
|
||||
|
||||
## API surface overview
|
||||
|
||||
Profile-scoped (require `X-Profile-Id` header):
|
||||
|
||||
- `GET /api/v1/notifications` — paginated inbox (newest-first, `before` cursor).
|
||||
- `GET /api/v1/notifications/sync` — forward sync (`since` cursor); the endpoint mobile clients call after a push wake.
|
||||
- `GET /api/v1/notifications/{id}` — single delivery by ID.
|
||||
- `GET /api/v1/notifications/unread-count` — for the badge.
|
||||
- `POST /api/v1/notifications/{id}/read` — mark single read.
|
||||
- `POST /api/v1/notifications/read-all` — mark all read.
|
||||
- `POST /api/v1/events/ws-ticket` — mint a short-lived single-use websocket ticket bound to `(user_id, profile_id)`.
|
||||
- `GET /api/v1/notifications/preferences` — read profile prefs.
|
||||
- `PUT /api/v1/notifications/preferences` — update profile prefs.
|
||||
- `GET /api/v1/notifications/capability` — what channels are available.
|
||||
- `POST /api/v1/devices/push/apple` — register an APNs device.
|
||||
- `POST /api/v1/devices/push/fcm` — register an FCM device.
|
||||
- `DELETE /api/v1/devices/push/{id}` — disable a push device.
|
||||
- `GET /api/v1/notifications/webhooks` — list profile's webhooks.
|
||||
- `POST /api/v1/notifications/webhooks` — create a webhook.
|
||||
- `PUT /api/v1/notifications/webhooks/{id}` — update a webhook.
|
||||
- `DELETE /api/v1/notifications/webhooks/{id}` — delete a webhook.
|
||||
- `POST /api/v1/notifications/webhooks/{id}/test` — fire a test event.
|
||||
|
||||
Admin-scoped:
|
||||
|
||||
- Settings registry entries for APNs and FCM provider configuration (admin-only via existing `internal/api/handlers/settings.go` patterns).
|
||||
|
||||
## Realtime channel
|
||||
|
||||
A new event channel `notifications` joins the existing `catalog`, `jobs`, `sessions`, `tasks`, `scans`, `history_import`, `user_state`, `plugins` channels in `internal/events/types.go`. The websocket handler must validate active profile identity and must reject `notifications` events whose `profile_id` doesn't match the connection's bound profile.
|
||||
|
||||
Because browsers can't set custom headers on WebSocket connections, profile identity is carried by a **short-lived single-use ticket**: the client calls `POST /api/v1/events/ws-ticket` (normal auth + `X-Profile-Id`), receives an opaque ticket valid for ~30 seconds, and passes it as a `ticket` query parameter on the handshake. The server consumes the ticket and binds the connection to the `(user_id, profile_id)` it was minted for. A long-lived profile token must **not** be passed in the query string — self-hosted servers commonly sit behind reverse proxies whose access logs capture query strings, and a logged ticket that expired 30 seconds after minting is harmless where a logged profile token is not.
|
||||
|
||||
Event types on this channel:
|
||||
|
||||
- `notification.created` — a new delivery for the bound profile.
|
||||
- `notification.read` — a delivery was marked read (allows multi-tab coherence).
|
||||
|
||||
Snapshot on subscribe: recent unread deliveries for the bound profile, so reconnecting clients hydrate without a separate REST call.
|
||||
|
||||
## Out of scope for v1
|
||||
|
||||
- Movie release notifications (no trigger surface; v2).
|
||||
- Request-fulfilled notifications ("your requested item is now available"). Silo's requests system postdates this design; it is the most obvious next notification type and slots into the extensible `notification_deliveries.type` registry without schema changes. v2.
|
||||
- Aggregated notifications ("3 new episodes available"; presentation layer, can stack on per-episode rows later). The per-series burst cap in `01` bounds bulk-addition volume until an aggregated delivery type exists.
|
||||
- Web Push (browser PWA notifications via Push API). Possible v2 — would mirror APNs/FCM as a third push platform.
|
||||
- Email or SMS delivery.
|
||||
- Cross-profile / household-wide notification views.
|
||||
- Admin-level system notifications ("scan complete", "library health alert"). The existing realtime events hub (`internal/events/`, with the operational publisher wrapper at `internal/notifications/hub.go`) already handles operational events; this folder's design is strictly user-facing media notifications.
|
||||
- Notification templates / customization beyond Discord-vs-generic.
|
||||
- Time-window quiet hours. Could be added to `notification_preferences` later; not v1.
|
||||
- Locale negotiation for push payloads. APNs / FCM payloads use generic localization keys (`SILO_NOTIFICATION_GENERIC_BODY` etc.) so the OS picks language from device locale.
|
||||
|
||||
## Open questions
|
||||
|
||||
These are the few remaining items not yet decided. None block writing the per-channel specs.
|
||||
|
||||
1. **Service-level rate limits per relay account.** Per-second and per-day caps need real numbers. Suggest 10 req/sec burst, 50,000 req/day initial quota; tune from logs. The server-side dispatchers pace their relay calls client-side (default 5 req/sec token bucket, see `02`/`03`) so a hundreds-of-users server drains bursts smoothly instead of slamming into 429s; the per-series burst cap in `01` bounds worst-case burst size.
|
||||
2. **Webhook destination domain blocklist edge cases.** RFC ranges are defined in `04-outbound-webhooks.md`; remaining open question is whether IPv6 v4-mapped addresses (`::ffff:0:0/96`) and CGNAT (`100.64.0.0/10`) need any admin escape hatch beyond the global `allow_private_destinations` flag.
|
||||
3. **Discord image proxy timing.** Ship in v1 (build `media.discord-cdn-proxy.silo.app`) or defer to v1.5 with text-only embeds? Recommendation in `04`: defer to v1.5; v1 ships text-only embeds.
|
||||
|
||||
Resolved during review (no longer open):
|
||||
|
||||
- **Relay service code lives in a separate repo** (provisional `silo-push-relay`). This folder defines the contract.
|
||||
- **Migration ordering.** Foundation schema lands as one migration; per-channel schemas can land separately as their channels ship.
|
||||
- **`server_device_id` lifecycle.** Stable across token rotation; rotates only when a device is removed and re-registered.
|
||||
|
||||
## Phasing
|
||||
|
||||
Implementation phases gated by feature flags:
|
||||
|
||||
| Phase | Flag | Capability |
|
||||
|---|---|---|
|
||||
| 1 | `notifications.schema_enabled` | Tables exist; no writes. |
|
||||
| 2 | `notifications.interest_updaters_enabled` | `profile_series_interest` updates land on favorites/watchlist/playback events. Backfill task runs. |
|
||||
| 3 | `notifications.release_events_enabled` | Availability seeding backfill runs first, then ingest writes `release_events`. No fanout yet. |
|
||||
| 4 | `notifications.fanout_enabled` | Fanout worker runs; `notification_deliveries` rows materialize. Websocket channel published. |
|
||||
| 5 | `notifications.ui_enabled` | Inbox API + frontend badge + page exposed. |
|
||||
| 6 | `notifications.webhooks_enabled` | Outbound webhooks + Discord/generic dispatchers. v1 ships **text-only** Discord embeds (no `image` or `avatar_url` fields) to avoid leaking the user's server origin to Discord. v1.5 adds the optional `media.discord-cdn-proxy.silo.app` proxy and re-enables embed images. |
|
||||
| 7 | `notifications.apple_push_enabled` | APNs registration + dispatcher. Requires relay infra (or admin-supplied custom credentials). |
|
||||
| 8 | `notifications.android_push_enabled` | FCM registration + dispatcher. Requires relay infra (or admin-supplied custom credentials). |
|
||||
|
||||
Phases 1-6 ship without any new external infrastructure. Phases 7-8 require Silo to provision Apple Developer + Firebase developer accounts and operate the relay service.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,891 @@
|
||||
# Privacy-Preserving APNs Relay Spec
|
||||
|
||||
**Date:** 2026-04-27 (refined 2026-04-28)
|
||||
**Status:** Draft (refined; open questions resolved)
|
||||
**Scope:** Silo server push integration, Apple clients, opt-in central APNs relay, custom APNs provider configuration
|
||||
**Depends On:**
|
||||
- [`00-architecture-overview.md`](./00-architecture-overview.md) — read first for cross-cutting context
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md) — foundational durable inbox + fanout
|
||||
**Sibling spec:**
|
||||
- [`03-fcm-relay.md`](./03-fcm-relay.md) — Android push using the same threat model and architecture
|
||||
|
||||
## Summary
|
||||
|
||||
Silo should support remote Apple notifications through either an opt-in
|
||||
central APNs relay or admin-configured APNs credentials, while keeping
|
||||
notification metadata on the user's own Silo server.
|
||||
|
||||
The hosted relay should be a constrained transport service. It should receive
|
||||
only the minimum fields needed to submit an APNs request:
|
||||
|
||||
- an APNs device token
|
||||
- APNs environment and app topic
|
||||
- a push mode
|
||||
- an opaque server/device correlation key
|
||||
- an opaque delivery identifier
|
||||
- optional badge count
|
||||
|
||||
Neither provider path may send titles, item names, usernames, profile names,
|
||||
library names, server URLs, artwork URLs, watched-state metadata, or
|
||||
notification body text through APNs.
|
||||
|
||||
When the Apple device wakes, the app should fetch real notification metadata
|
||||
directly from the user's Silo server using the existing authenticated
|
||||
server connection.
|
||||
|
||||
## Problem
|
||||
|
||||
Reliable Apple remote notifications require APNs. A self-hosted Silo server
|
||||
cannot directly wake an iPhone, iPad, or Apple TV app in the background without
|
||||
going through Apple's push infrastructure.
|
||||
|
||||
That creates two product concerns:
|
||||
|
||||
1. Privacy: notification contents should not be exposed to Apple or a Silo
|
||||
operated relay.
|
||||
2. Operational friction: most server admins should not need to create and manage
|
||||
their own Apple developer account, APNs key, bundle topic, and push service.
|
||||
|
||||
Silo should offer a practical default that preserves the self-hosted trust
|
||||
model as much as the platform allows.
|
||||
|
||||
## Decision
|
||||
|
||||
Add configurable Apple push providers for the official Apple app:
|
||||
|
||||
- `off`: no remote Apple push
|
||||
- `silo_relay`: Silo's hosted APNs relay
|
||||
- `custom_apns`: admin-supplied APNs credentials used directly by the server
|
||||
|
||||
The relay submits APNs requests using Silo-controlled Apple credentials for
|
||||
the official app bundle. A self-hosted server admin may opt in to use this relay.
|
||||
The relay's API should intentionally prevent notification content from being
|
||||
sent through the relay.
|
||||
|
||||
If `custom_apns` is selected, the user's Silo server submits directly to
|
||||
APNs using admin-provided Apple developer credentials. This avoids the Silo
|
||||
relay entirely, but it must use the same minimal payload and metadata-fetch
|
||||
rules as the hosted relay path.
|
||||
|
||||
The default push mode should be **Private Alert**:
|
||||
|
||||
- APNs displays a generic Silo notification.
|
||||
- The APNs payload contains only opaque identifiers.
|
||||
- The app fetches the actual notification row from the user's server when it is
|
||||
opened or granted background execution.
|
||||
|
||||
The implementation should also support **Background Wake** for low-noise sync,
|
||||
with the explicit understanding that iOS background pushes are opportunistic and
|
||||
not a reliable user-visible notification mechanism.
|
||||
|
||||
## Goals
|
||||
|
||||
- Make Apple push possible without every admin managing APNs credentials.
|
||||
- Keep actual notification metadata on the user's Silo server.
|
||||
- Make relay participation explicit and disabled by default.
|
||||
- Make APNs credentials configurable for admins who want to avoid the hosted
|
||||
relay.
|
||||
- Keep the relay stateless for device subscriptions where possible.
|
||||
- Keep APNs payloads opaque and generic.
|
||||
- Keep the hosted relay and custom APNs paths behaviorally equivalent from the
|
||||
app's point of view.
|
||||
- Fit on top of the durable notification inbox design.
|
||||
|
||||
## Non-Goals
|
||||
|
||||
- Replacing APNs for Apple remote push.
|
||||
- Shipping notification content through the central relay.
|
||||
- Requiring Firebase or Google services for this Apple path.
|
||||
- Solving Android push in this spec.
|
||||
- Adding marketing, analytics, or delivery-tracking exports to the relay.
|
||||
- Hiding APNs itself from Apple devices. Custom APNs still uses Apple APNs.
|
||||
- Making silent background delivery as reliable as visible push.
|
||||
|
||||
## Threat Model
|
||||
|
||||
### What The User's Server Knows
|
||||
|
||||
The user's Silo server knows:
|
||||
|
||||
- the notification content
|
||||
- the profile and user recipients
|
||||
- the registered local devices
|
||||
- APNs device tokens for those devices
|
||||
- push delivery attempts and failures
|
||||
|
||||
This is acceptable because the server is already the authority for the user's
|
||||
media, profiles, sessions, and notification inbox.
|
||||
|
||||
### What The Central Relay May Know
|
||||
|
||||
The relay may see:
|
||||
|
||||
- an opt-in relay account or install identifier
|
||||
- the server's egress IP address (inherent to any hosted relay — the server
|
||||
connects to it directly; for home hosting this identifies the household's
|
||||
connection, making it the closest thing to an identity leak in this design)
|
||||
- APNs device tokens submitted in send requests
|
||||
- request timestamps
|
||||
- coarse push mode, such as `private_alert` or `background_wake`
|
||||
- opaque `collapse_id` values (per-server-keyed HMACs of series identity; the
|
||||
relay can group one server's pushes into series-equivalence classes but
|
||||
cannot recover which series, nor correlate across servers — see "Send Apple
|
||||
Push" validation)
|
||||
- APNs response status and APNs request IDs
|
||||
- opaque delivery identifiers
|
||||
|
||||
The relay must not receive enough information to know what media item,
|
||||
notification type, profile, user, library, server hostname, or server URL caused
|
||||
the push.
|
||||
|
||||
### What Apple May Know
|
||||
|
||||
Apple/APNs may see:
|
||||
|
||||
- the official app topic
|
||||
- the target device token
|
||||
- APNs headers
|
||||
- request timing
|
||||
- the generic APNs payload
|
||||
|
||||
Apple must not receive media titles, usernames, library names, server URLs,
|
||||
artwork URLs, or other notification metadata in the payload.
|
||||
|
||||
### Residual Metadata Leakage
|
||||
|
||||
This design cannot hide that a device received a Silo push at a particular
|
||||
time, nor the egress IP of the server sending relay requests. It can only hide
|
||||
the meaning and content of the push. Admins for whom IP exposure matters can
|
||||
route relay traffic through their existing VPN/proxy egress; the relay path is
|
||||
plain HTTPS and needs no special handling.
|
||||
|
||||
## User And Admin Controls
|
||||
|
||||
### Server Admin Control
|
||||
|
||||
The server admin must explicitly choose an Apple push provider.
|
||||
|
||||
Suggested setting:
|
||||
|
||||
```text
|
||||
notifications.apple_push.provider = off | silo_relay | custom_apns
|
||||
```
|
||||
|
||||
Default:
|
||||
|
||||
```text
|
||||
off
|
||||
```
|
||||
|
||||
If `silo_relay` is selected, the admin configures a relay API key and
|
||||
accepts a clear privacy notice:
|
||||
|
||||
```text
|
||||
Silo Relay can wake Apple devices through APNs, but notification details
|
||||
stay on this server. The relay receives APNs device tokens, timestamps, opaque
|
||||
delivery IDs, and generic push mode only.
|
||||
```
|
||||
|
||||
If `custom_apns` is selected, the admin configures their own Apple developer
|
||||
APNs credentials:
|
||||
|
||||
```text
|
||||
notifications.apple_push.custom.team_id
|
||||
notifications.apple_push.custom.key_id
|
||||
notifications.apple_push.custom.private_key
|
||||
notifications.apple_push.custom.default_environment = production | sandbox
|
||||
notifications.apple_push.custom.allowed_topics = [...]
|
||||
```
|
||||
|
||||
The server then sends directly to APNs and does not call the Silo relay.
|
||||
The same private payload rules still apply.
|
||||
|
||||
### Device User Control
|
||||
|
||||
Each Apple device must opt in independently:
|
||||
|
||||
- the OS notification permission must be granted
|
||||
- the app must be signed into a Silo server
|
||||
- the active profile must enable push notifications for that device
|
||||
- the server admin must have enabled an Apple push provider
|
||||
|
||||
Admins must not be able to silently enroll every profile/device into remote push
|
||||
without the device having granted OS notification permission.
|
||||
|
||||
### Profile Control
|
||||
|
||||
Push preferences should sit on top of the durable notification preferences from
|
||||
the inbox design.
|
||||
|
||||
Suggested profile/device modes:
|
||||
|
||||
- `off`: never send remote push to this device
|
||||
- `in_app_only`: websocket and inbox only
|
||||
- `private_push`: generic APNs wake/alert, fetch details from server
|
||||
- `full_preview`: out of scope for v1; would require explicit user consent and
|
||||
a separate privacy warning
|
||||
|
||||
V1 should ship only `off`, `in_app_only`, and `private_push`.
|
||||
|
||||
## System Architecture
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant App as Apple App
|
||||
participant Server as User Silo Server
|
||||
participant Relay as Silo APNs Relay
|
||||
participant APNs as Apple APNs
|
||||
|
||||
App->>App: Request OS notification permission
|
||||
App->>App: Register for remote notifications
|
||||
App->>Server: Register device token and opaque server_device_id
|
||||
Server->>Server: Store encrypted token and device preferences
|
||||
Server->>Server: Create durable notification_delivery
|
||||
Server->>Relay: Send minimal APNs relay request
|
||||
Relay->>APNs: Submit generic APNs payload
|
||||
APNs->>App: Deliver private alert or background wake
|
||||
App->>Server: Fetch notification metadata directly
|
||||
Server->>App: Return inbox rows for active profile
|
||||
```
|
||||
|
||||
## Ownership Boundaries
|
||||
|
||||
### This Repository
|
||||
|
||||
Silo owns:
|
||||
|
||||
- admin settings for selecting a push provider
|
||||
- profile and device notification preferences
|
||||
- APNs token registration from Apple clients
|
||||
- durable storage of device registrations
|
||||
- push fanout from `notification_deliveries`
|
||||
- relay client integration
|
||||
- retry, backoff, and APNs error handling
|
||||
- inbox sync APIs used after wake
|
||||
|
||||
### Apple Client Repository
|
||||
|
||||
The Apple app owns:
|
||||
|
||||
- APNs registration
|
||||
- notification permission UX
|
||||
- device registration API calls
|
||||
- local server/device correlation
|
||||
- wake handling
|
||||
- fetching metadata from the user's server
|
||||
- rendering local/in-app notification details
|
||||
|
||||
### Central Relay Service
|
||||
|
||||
The central relay owns:
|
||||
|
||||
- APNs provider credentials for official Silo bundle topics
|
||||
- relay API authentication
|
||||
- request validation and rate limiting
|
||||
- APNs request submission
|
||||
- redacted operational logs
|
||||
- minimal delivery status reporting
|
||||
|
||||
The relay should live outside this repo unless the project later decides to
|
||||
co-locate the service code. This repo should define the contract and implement
|
||||
the self-hosted server side.
|
||||
|
||||
### Custom APNs Mode
|
||||
|
||||
When `custom_apns` is selected, there is no central relay in the send path.
|
||||
|
||||
The user's Silo server owns:
|
||||
|
||||
- APNs provider authentication using the configured team ID, key ID, and private
|
||||
key
|
||||
- topic allowlisting for the official Silo app bundle IDs it intends to
|
||||
support
|
||||
- sandbox vs production environment selection
|
||||
- APNs request construction
|
||||
- APNs response handling
|
||||
- APNs credential rotation and secret storage
|
||||
|
||||
Custom APNs mode must not unlock richer payloads. It exists to let admins avoid
|
||||
the Silo relay, not to bypass the privacy-preserving notification contract.
|
||||
|
||||
## Data Model
|
||||
|
||||
This spec assumes `notification_deliveries` exists from the durable notification
|
||||
inbox design.
|
||||
|
||||
### `push_devices`
|
||||
|
||||
Purpose: profile/device-scoped remote push registration. Shared across Apple and Android — see [`03-fcm-relay.md`](./03-fcm-relay.md) for the FCM-specific columns.
|
||||
|
||||
Columns:
|
||||
|
||||
- `id text primary key`
|
||||
- `user_id integer not null` (matches `users.id integer`)
|
||||
- `profile_id text not null`
|
||||
- `device_id text not null`
|
||||
- `platform text not null` — `'apple'` or `'android'`. Required; the row is invalid without it.
|
||||
- `provider text not null` — for Apple rows: `'silo_relay'` or `'custom_apns'`. For Android rows: `'silo_relay'` or `'custom_fcm'`. (Effectively the active configured provider at registration time; updates rewrite the row.)
|
||||
- `apns_environment text` — `'production'` / `'sandbox'`. Nullable; populated only for `platform = 'apple'`.
|
||||
- `apns_topic text` — bundle topic. Nullable; populated only for `platform = 'apple'`.
|
||||
- `apns_token_ciphertext bytea` — nullable; populated only for `platform = 'apple'`.
|
||||
- `apns_token_hash text` — nullable; populated only for `platform = 'apple'`.
|
||||
- `fcm_token_ciphertext bytea` — nullable; populated only for `platform = 'android'`. Defined here so the table is forward-compatible with [`03-fcm-relay.md`](./03-fcm-relay.md); the FCM spec adds the rest of the Android-specific columns.
|
||||
- `fcm_token_hash text` — nullable; Android only.
|
||||
- `fcm_project_id text` — nullable; Android only.
|
||||
- `fcm_package_name text` — nullable; Android only.
|
||||
- `server_device_id text not null` — random opaque value, generated server-side at registration. Stable across token rotation; rotates only when the device is removed and re-registered.
|
||||
- `push_mode text not null default 'private_push'` — profile-level mode (see [`00-architecture-overview.md`](./00-architecture-overview.md) "Mode terminology").
|
||||
- `enabled boolean not null default true`
|
||||
- `last_seen_at timestamptz`
|
||||
- `last_success_at timestamptz`
|
||||
- `last_failure_at timestamptz`
|
||||
- `last_failure_code text`
|
||||
- `created_at timestamptz not null default now()`
|
||||
- `updated_at timestamptz not null default now()`
|
||||
|
||||
Constraints:
|
||||
|
||||
- unique on `(profile_id, device_id, platform)` — a device can register exactly once per platform per profile, regardless of which provider the admin happens to have configured. This matters because admins may flip between `silo_relay` and `custom_apns` mid-rotation; a `provider`-scoped unique key would leave stale rows behind.
|
||||
- unique on `(server_device_id)`
|
||||
- `CHECK ((platform = 'apple' AND apns_token_hash IS NOT NULL AND fcm_token_hash IS NULL) OR (platform = 'android' AND fcm_token_hash IS NOT NULL AND apns_token_hash IS NULL))` — exactly the platform-appropriate token columns are populated.
|
||||
|
||||
Notes:
|
||||
|
||||
- `server_device_id` must be random and opaque. It is the value included in the
|
||||
APNs payload so the app can map the push back to the correct local server
|
||||
account.
|
||||
- Store APNs tokens encrypted at rest when the local encryption facility exists.
|
||||
- Store `apns_token_hash` for dedupe and diagnostics without logging raw tokens.
|
||||
|
||||
### `push_delivery_attempts`
|
||||
|
||||
Purpose: operational record of push attempts without notification content.
|
||||
|
||||
Columns:
|
||||
|
||||
- `id text primary key`
|
||||
- `notification_delivery_id text not null`
|
||||
- `push_device_id text not null`
|
||||
- `provider text not null`
|
||||
- `attempt_number integer not null` — 1-based counter incremented on each retry. Used as the `Idempotency-Key` suffix for relay calls (see Send Apple Push), and lets diagnostics distinguish "10 attempts of one delivery" from "10 deliveries each first-attempted".
|
||||
- `relay_request_id text`
|
||||
- `apns_id text` — Apple's APNs ID returned in the response. Nullable; null for FCM rows.
|
||||
- `fcm_message_name text` — FCM v1 message name (see [`03-fcm-relay.md`](./03-fcm-relay.md)). Nullable; null for APNs rows.
|
||||
- `status text not null` — `'pending'`, `'delivered'`, `'retrying'`, `'failed'`, `'device_disabled'`. `pending` rows are the dispatch outbox: the fanout transaction in [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md) inserts attempt 1 as `pending` in the same transaction as the delivery; the push dispatcher claims and sends them post-commit.
|
||||
- `failure_code text`
|
||||
- `attempted_at timestamptz not null default now()`
|
||||
- `next_retry_at timestamptz`
|
||||
|
||||
Constraints:
|
||||
|
||||
- unique on `(notification_delivery_id, push_device_id, attempt_number)` to make the relay's `Idempotency-Key` deterministic and prevent double-claims of an attempt.
|
||||
- add an index on `(status, next_retry_at)` for retry workers. The same index serves outbox recovery: a sweeper claims `pending` rows older than ~60s (crash between delivery commit and dispatch) with `FOR UPDATE SKIP LOCKED`.
|
||||
|
||||
Retention:
|
||||
|
||||
- keep successful attempts for a short operational window, such as 14 days
|
||||
- keep failed attempts longer, such as 30 to 90 days, to diagnose device issues
|
||||
|
||||
## Server API Surface
|
||||
|
||||
### Register Apple Push Device
|
||||
|
||||
```http
|
||||
POST /api/v1/devices/push/apple
|
||||
```
|
||||
|
||||
Profile-scoped. Requires normal auth plus active profile context.
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"device_id": "apple-device-local-id",
|
||||
"apns_token": "hex-or-base64-token",
|
||||
"apns_environment": "production",
|
||||
"apns_topic": "com.continuum.app.ios",
|
||||
"push_mode": "private_push"
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "01J...",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"enabled": true,
|
||||
"push_mode": "private_push"
|
||||
}
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- `apns_topic` must be allowlisted by app build/channel.
|
||||
- `apns_environment` must be `sandbox` or `production`.
|
||||
- registration is idempotent by `(profile_id, device_id, provider)`.
|
||||
- token rotation updates the encrypted token and token hash.
|
||||
- a disabled server push provider should still allow registration, but should
|
||||
report that remote push is unavailable in a capability field if the frontend
|
||||
needs it.
|
||||
|
||||
### Disable Push Device
|
||||
|
||||
```http
|
||||
DELETE /api/v1/devices/push/{id}
|
||||
```
|
||||
|
||||
Profile-scoped and idempotent.
|
||||
|
||||
The server may either delete the row or mark `enabled = false`. Marking disabled
|
||||
is preferred for diagnostics and future re-enable flows.
|
||||
|
||||
### Push Capability
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/capability
|
||||
```
|
||||
|
||||
The full envelope is defined in [`00-architecture-overview.md`](./00-architecture-overview.md). The Apple-relevant slice is:
|
||||
|
||||
```json
|
||||
{
|
||||
"apple_push": {
|
||||
"available": true,
|
||||
"provider": "silo_relay",
|
||||
"available_providers": ["silo_relay", "custom_apns"],
|
||||
"supported_modes": ["private_push", "in_app_only"],
|
||||
"privacy_mode": "metadata_fetch"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The full response also includes `in_app`, `android_push` (see [`03-fcm-relay.md`](./03-fcm-relay.md)), and `webhooks` (see [`04-outbound-webhooks.md`](./04-outbound-webhooks.md)).
|
||||
|
||||
This lets clients present truthful setup UI without guessing whether the admin
|
||||
has enabled remote push.
|
||||
|
||||
## Relay API Contract
|
||||
|
||||
The relay API should be intentionally narrow. The user server should not send an
|
||||
arbitrary APNs payload.
|
||||
|
||||
### Send Apple Push
|
||||
|
||||
```http
|
||||
POST /v1/apple/send
|
||||
Authorization: Bearer <relay_api_key>
|
||||
Idempotency-Key: <notification_delivery_id>:<push_device_id>:<attempt_number>
|
||||
```
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "apns-token",
|
||||
"environment": "production",
|
||||
"topic": "com.continuum.app.ios",
|
||||
"mode": "private_alert",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_id": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"request_id": "01JRELAY...",
|
||||
"apns_id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"status": "accepted"
|
||||
}
|
||||
```
|
||||
|
||||
Validation:
|
||||
|
||||
- `token` is required and must be plausible for APNs.
|
||||
- `environment` must be `sandbox` or `production`. The relay forwards `production` requests to `https://api.push.apple.com` and `sandbox` requests to `https://api.development.push.apple.com` (Apple's current canonical hostnames; older docs/libraries also reference `api.sandbox.push.apple.com`, but `api.development.push.apple.com` is the current Apple-documented form).
|
||||
- `topic` must be allowlisted for the relay account.
|
||||
- `mode` must be `private_alert` or `background_wake`.
|
||||
- `server_device_id` and `delivery_id` must be opaque values with length limits (recommend ≤128 chars, ULID-shaped).
|
||||
- `collapse_id` must be opaque and ≤64 bytes — Apple enforces a 64-byte cap on `apns-collapse-id`. Reject longer values with HTTP 400.
|
||||
- `collapse_id` derivation (server-side rule, not relay-enforced): the server computes `collapse_id = base32(HMAC-SHA256(server_collapse_secret, series_id))` truncated to 26 chars, where `server_collapse_secret` is a random per-server secret generated at first use. Never send raw or plainly-hashed `series_id` — an unkeyed hash would let the relay (or Apple) dictionary-match popular series IDs. Residual: the relay can still group one server's pushes into per-series equivalence classes (that is what collapse is for); the per-server key prevents recovering the series or correlating across servers.
|
||||
- `badge` is optional. Default should be omitted to avoid leaking unread counts through APNs unless the user/admin explicitly enables badge sync.
|
||||
- no free-form notification title or body fields are accepted.
|
||||
- no image URL, media ID, username, server hostname, or server URL field is accepted.
|
||||
|
||||
### Relay-Built APNs Payloads
|
||||
|
||||
For `private_alert`, the relay constructs:
|
||||
|
||||
```json
|
||||
{
|
||||
"aps": {
|
||||
"alert": {
|
||||
"title-loc-key": "SILO_NOTIFICATION_TITLE",
|
||||
"loc-key": "SILO_NOTIFICATION_GENERIC_BODY"
|
||||
},
|
||||
"sound": "default"
|
||||
},
|
||||
"silo": {
|
||||
"v": 1,
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For `background_wake`, the relay constructs:
|
||||
|
||||
```json
|
||||
{
|
||||
"aps": {
|
||||
"content-available": 1
|
||||
},
|
||||
"silo": {
|
||||
"v": 1,
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Headers:
|
||||
|
||||
- `apns-topic`: selected from the allowlisted `topic`
|
||||
- `apns-push-type`: `alert` for `private_alert`, `background` for
|
||||
`background_wake`
|
||||
- `apns-priority`: `10` for `private_alert`, `5` for `background_wake`
|
||||
- `apns-collapse-id`: optional opaque `collapse_id`
|
||||
|
||||
The relay should not allow callers to override this payload shape in v1.
|
||||
|
||||
## Custom APNs Contract
|
||||
|
||||
`custom_apns` should share the same internal send model as `silo_relay`,
|
||||
but replace the relay HTTP call with a direct APNs provider request.
|
||||
|
||||
The server-side push sender should accept only a structured internal request:
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "apns-token",
|
||||
"environment": "production",
|
||||
"topic": "com.continuum.app.ios",
|
||||
"mode": "private_alert",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_id": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
The sender builds the APNs payload locally using the exact same payload shapes
|
||||
defined above for `private_alert` and `background_wake`.
|
||||
|
||||
Configuration:
|
||||
|
||||
- `team_id`: Apple developer team ID.
|
||||
- `key_id`: APNs auth key ID.
|
||||
- `private_key`: APNs `.p8` private key, stored as a secret.
|
||||
- `default_environment`: `production` or `sandbox`.
|
||||
- `allowed_topics`: explicit bundle topics this server is allowed to send for.
|
||||
|
||||
Rules:
|
||||
|
||||
- The admin may configure credentials, environment, and topics.
|
||||
- The server must not expose a free-form APNs JSON payload setting.
|
||||
- The server must not expose custom title/body templates for remote push in v1.
|
||||
- Topic values must match the device registration topic and the configured
|
||||
allowlist.
|
||||
- Sandbox tokens must be sent to the sandbox endpoint and production tokens to
|
||||
the production endpoint.
|
||||
- APNs auth tokens should be cached briefly and regenerated before expiry.
|
||||
- Credential validation should provide a test-send or dry-run diagnostic that
|
||||
does not include notification content.
|
||||
|
||||
## Wake And Metadata Fetch
|
||||
|
||||
When the app receives a push:
|
||||
|
||||
1. Read `silo.v`, `server_device_id`, and `delivery_id`.
|
||||
2. Find the local server account that owns `server_device_id`.
|
||||
3. If background execution is available, call the server immediately.
|
||||
4. If the app is opened from the notification, call the server before rendering
|
||||
the target screen.
|
||||
5. Fetch notification metadata from the user's server.
|
||||
6. Render the real notification from server data.
|
||||
|
||||
Suggested fetch:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/sync?since=<last_cursor>
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/{delivery_id}
|
||||
```
|
||||
|
||||
The sync endpoint is preferred because push delivery is not guaranteed and
|
||||
multiple notification deliveries can be coalesced.
|
||||
|
||||
If the app cannot reach the server, it should keep the generic notification and
|
||||
retry the inbox sync later.
|
||||
|
||||
## Fanout Rules
|
||||
|
||||
Push fanout should happen after durable inbox delivery commits.
|
||||
|
||||
Flow:
|
||||
|
||||
1. `notification_deliveries` row is created. **In the same transaction**, the
|
||||
fanout worker enqueues one `pending` `push_delivery_attempts` row per
|
||||
enabled `push_device` of the recipient profile (the dispatch outbox — see
|
||||
[`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)
|
||||
"Transaction and Concurrency Rules").
|
||||
2. The realtime websocket event is published for connected clients.
|
||||
3. The push dispatcher claims the `pending` attempt rows post-commit
|
||||
(`FOR UPDATE SKIP LOCKED`); a recovery sweeper claims rows older than ~60s
|
||||
whose dispatch never ran.
|
||||
4. Each claimed attempt sends through the configured provider:
|
||||
- `silo_relay`: send the minimal relay request
|
||||
- `custom_apns`: build the same minimal APNs payload locally and send it
|
||||
directly to APNs
|
||||
5. APNs errors update device state and attempt status.
|
||||
|
||||
Do not block notification delivery creation on APNs or relay availability.
|
||||
|
||||
### Relay pacing
|
||||
|
||||
The dispatcher paces relay calls client-side with a token bucket (default 5
|
||||
req/sec, setting `notifications.push.relay_send_rate`), shared across the APNs
|
||||
and FCM dispatchers since both consume the same relay account quota. On a
|
||||
server with hundreds of users, a popular release can enqueue several hundred
|
||||
push attempts at once; pacing drains the queue smoothly instead of slamming
|
||||
into the relay's rate limit and burning retries on 429s. `TooManyRequests`
|
||||
from the relay is still honored with backoff — pacing is the steady-state
|
||||
mechanism, backoff is the safety net. The per-series burst cap in `01` bounds
|
||||
worst-case queue depth. Custom APNs sends are paced separately and more
|
||||
generously (Apple's own limits are far higher than any single-server relay
|
||||
quota).
|
||||
|
||||
## APNs Error Handling
|
||||
|
||||
The server should handle relay/APNs failures without exposing notification
|
||||
content.
|
||||
|
||||
Recommended handling:
|
||||
|
||||
- `BadDeviceToken` / `InvalidToken`: disable the token and require re-registration. (Apple has migrated some responses from `BadDeviceToken` to `InvalidToken`; treat them equivalently.)
|
||||
- `Unregistered`: disable the token.
|
||||
- `DeviceTokenNotForTopic`: disable and log a build/topic mismatch.
|
||||
- `TooManyRequests`: retry with backoff.
|
||||
- `ExpiredProviderToken`: applies only to `custom_apns`. The cached APNs JWT has expired. Regenerate the JWT immediately and retry the request once before backing off. The relay path never surfaces this code because the relay manages its own JWTs.
|
||||
- `TooManyProviderTokenUpdates`: applies only to `custom_apns`. Apple has rate-limited JWT regeneration. Stop regenerating; reuse the existing token; retry after backoff. Indicates a bug in the JWT cache (regenerating more often than every 20 minutes).
|
||||
- relay unavailable, APNs unavailable, or network timeout: retry with backoff.
|
||||
- malformed request: mark failed and surface admin diagnostics.
|
||||
|
||||
Errors should be visible to admins as operational status, not to normal users
|
||||
unless their device needs reauthorization.
|
||||
|
||||
## Privacy Requirements
|
||||
|
||||
The implementation must satisfy these requirements before shipping:
|
||||
|
||||
- Relay requests contain no notification title or body.
|
||||
- Relay requests contain no media identifiers.
|
||||
- Relay requests contain no server hostname or base URL.
|
||||
- Relay requests contain no profile, username, library, collection, or item names.
|
||||
- Relay requests *do* carry `apns-topic` (e.g., `com.continuum.app.ios`) and `mode` (`private_alert` / `background_wake`). These are platform/build identifiers that Apple already sees; documenting them here so the privacy claim list is exhaustive rather than over-strong.
|
||||
- Direct APNs payloads contain no notification title or body beyond generic
|
||||
localizable keys.
|
||||
- Direct APNs payloads contain no media identifiers, server hostname, server URL,
|
||||
profile name, username, library name, collection name, or item name.
|
||||
- Relay logs redact raw APNs tokens.
|
||||
- Relay logs redact authorization headers.
|
||||
- Relay logs do not persist full request payloads.
|
||||
- Server logs redact APNs tokens and relay API keys.
|
||||
- Badge count is disabled by default.
|
||||
- Push registration and remote push provider use are opt-in.
|
||||
- Device unregister disables further push attempts for that device.
|
||||
|
||||
## Security Requirements
|
||||
|
||||
- Relay API uses TLS only.
|
||||
- Relay API keys are scoped to a relay account or install identifier.
|
||||
- Relay API keys can be revoked without changing the user's Silo server
|
||||
authentication.
|
||||
- Relay requests are rate limited by API key and coarse token hash.
|
||||
- Relay supports idempotency keys to prevent duplicate APNs sends during retry.
|
||||
- Relay does not accept arbitrary APNs payload JSON in v1.
|
||||
- Server stores relay API keys as secrets.
|
||||
- Server stores custom APNs private keys as secrets.
|
||||
- Server never logs custom APNs private keys, generated provider tokens, or APNs
|
||||
auth headers.
|
||||
- Server stores APNs tokens encrypted at rest where local secret storage exists.
|
||||
- Admin diagnostics must not print raw APNs tokens.
|
||||
|
||||
## Settings And UX
|
||||
|
||||
Admin settings should explain the tradeoff plainly:
|
||||
|
||||
```text
|
||||
Apple Push Provider
|
||||
|
||||
Off
|
||||
No Apple remote push. Devices still receive in-app realtime updates while open.
|
||||
|
||||
Silo Relay
|
||||
Uses Silo's APNs relay to wake Apple devices. Notification details stay on
|
||||
this server. The relay receives APNs device tokens, timestamps, and opaque
|
||||
delivery IDs.
|
||||
|
||||
Custom APNs
|
||||
Advanced. Send directly to Apple APNs using your own Apple developer
|
||||
credentials. Notification details still stay on this server.
|
||||
```
|
||||
|
||||
Device settings should describe the user-visible mode:
|
||||
|
||||
```text
|
||||
Private Push
|
||||
Show a generic Silo notification, then fetch details from your server when
|
||||
this device wakes.
|
||||
```
|
||||
|
||||
Do not claim the central relay is fully self-hosted. The truthful claim is:
|
||||
|
||||
```text
|
||||
Notification content stays on your server. Apple, and the Silo relay if
|
||||
selected, may still process generic wake messages needed for Apple push
|
||||
delivery.
|
||||
```
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### Task 1: Add Push Provider Settings
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/api/handlers/settings.go`
|
||||
- `web/src/lib/settingsManifest.ts`
|
||||
- settings UI files as needed
|
||||
|
||||
Add server/admin settings for:
|
||||
|
||||
- provider selection
|
||||
- relay endpoint
|
||||
- relay API key
|
||||
- custom APNs team ID
|
||||
- custom APNs key ID
|
||||
- custom APNs private key
|
||||
- custom APNs environment
|
||||
- custom APNs topic allowlist
|
||||
- badge sync enabled or disabled
|
||||
- relay send rate (`notifications.push.relay_send_rate`, shared with FCM pacing)
|
||||
|
||||
Default provider must be `off`.
|
||||
|
||||
### Task 2: Add Push Device Registration
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- new migration under `migrations/`
|
||||
- new package or service under `internal/notifications`
|
||||
- `internal/api/handlers/notifications.go`
|
||||
- `internal/api/router.go`
|
||||
|
||||
Add profile-scoped Apple push device registration, token rotation, and disable
|
||||
APIs.
|
||||
|
||||
### Task 3: Add Apple Push Provider Clients
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/notifications/apple_relay.go`
|
||||
- `internal/notifications/apple_apns.go`
|
||||
- config/settings accessors
|
||||
|
||||
Implement the narrow `/v1/apple/send` client. The client should not accept a
|
||||
free-form title/body/content payload.
|
||||
|
||||
Implement the direct APNs client for `custom_apns` using the same internal
|
||||
request shape and payload builder.
|
||||
|
||||
### Task 4: Add Push Fanout Worker
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/notifications/push_fanout.go`
|
||||
- notification delivery creation path from the durable inbox implementation
|
||||
|
||||
Trigger push fanout after `notification_deliveries` commit. Use retries and
|
||||
record `push_delivery_attempts`.
|
||||
|
||||
### Task 5: Add Apple Client Registration And Wake Handling
|
||||
|
||||
Files live in the Apple client repository, not this server repo.
|
||||
|
||||
Required client behavior:
|
||||
|
||||
- request notification permission
|
||||
- register APNs token with the user's server
|
||||
- store `server_device_id` locally
|
||||
- handle token rotation
|
||||
- fetch notification metadata after wake/open
|
||||
- unregister or disable on sign-out/profile removal
|
||||
|
||||
### Task 6: Add Admin Diagnostics
|
||||
|
||||
Expose high-level status:
|
||||
|
||||
- provider enabled/disabled
|
||||
- number of registered Apple devices
|
||||
- last relay success
|
||||
- last relay failure code
|
||||
- custom APNs credential presence and topic/environment status
|
||||
- last custom APNs success
|
||||
- last custom APNs failure code
|
||||
- token/topic mismatch warnings
|
||||
|
||||
Do not expose raw APNs tokens.
|
||||
|
||||
## Validation Plan
|
||||
|
||||
Use minimal verification while the work is still design-only. When implemented,
|
||||
verify:
|
||||
|
||||
- relay-disabled servers never call the relay
|
||||
- `custom_apns` servers never call the relay
|
||||
- device registration is profile-scoped and idempotent
|
||||
- APNs tokens are redacted in logs
|
||||
- relay request bodies contain no titles, body text, item IDs, server URLs, or
|
||||
profile names
|
||||
- direct APNs payloads contain no titles, body text, item IDs, server URLs, or
|
||||
profile names beyond the generic localizable notification keys
|
||||
- custom APNs private keys and generated provider tokens are redacted in logs
|
||||
- creating one `notification_delivery` sends at most one push per enabled device
|
||||
- APNs token rotation updates the stored token without duplicating devices
|
||||
- `Unregistered` and `BadDeviceToken` disable the affected device
|
||||
- app wake fetches notification metadata from the user's server
|
||||
- server offline after push leaves only the generic notification visible
|
||||
|
||||
## Resolved Questions (was: Open Questions)
|
||||
|
||||
These four open questions from the original draft were resolved during the multi-channel refinement:
|
||||
|
||||
- **Topics: per-platform.** Official Silo builds use separate APNs topics for iOS, tvOS, and macOS. Each is a different bundle ID with its own provisioning. The `apns_topic` allowlist on a relay account enumerates the per-platform topics the account may push to.
|
||||
- **Badge counts: disabled in v1.** Badge count leaks unread volume through APNs. v1 ships without badge updates; an explicit per-profile opt-in to badge sync can land in v2 alongside a privacy notice.
|
||||
- **Relay statefulness: stateless v1.** No stored token aliases. The relay accepts raw APNs tokens on every request. Token aliases add complexity for a benefit (reduced repeat-token exposure to relay logs) that is small compared to "redact tokens from logs." Add aliases later if abuse-pattern analysis or rate-limit pressure justifies it.
|
||||
- **Relay deployment repo: out of scope for this spec.** The relay implementation lives in a separate repo (provisional name `silo-push-relay`). This document defines the contract the user-facing Silo server implements; the relay's internal architecture (deployment platform, dependency tree, etc.) is the relay repo's concern.
|
||||
|
||||
## Remaining Open Questions
|
||||
|
||||
- **Multi-bundle topic allowlist UX.** When an admin configures custom APNs, how do they declare allowlisted topics — free-text textarea, or a structured list? Recommendation: structured list with platform tags (iOS / tvOS / macOS) so the UI can show which platforms have working push.
|
||||
- **APNs JWT cache lifetime.** APNs JWTs are valid for 60 minutes per Apple's docs but may be reused across requests. Should the custom APNs path cache and reuse, or sign per-request? Recommendation: cache with regeneration at 50 minutes to leave headroom, matching Apple's recommendation.
|
||||
@@ -0,0 +1,748 @@
|
||||
# Privacy-Preserving FCM Relay Spec
|
||||
|
||||
**Date:** 2026-04-28
|
||||
**Status:** Draft
|
||||
**Scope:** Silo server push integration for Android, opt-in central FCM relay, custom FCM provider configuration
|
||||
**Depends On:**
|
||||
- [`00-architecture-overview.md`](./00-architecture-overview.md)
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)
|
||||
- [`02-apns-relay.md`](./02-apns-relay.md) — sibling spec; structurally identical with FCM-specific differences
|
||||
|
||||
## Summary
|
||||
|
||||
Silo should support remote Android notifications through either an opt-in central FCM relay or admin-configured Firebase Cloud Messaging credentials, while keeping notification metadata on the user's own Silo server.
|
||||
|
||||
The hosted relay should be a constrained transport service. It should receive only the minimum fields needed to submit a Firebase Cloud Messaging v1 API request:
|
||||
|
||||
- an FCM registration token
|
||||
- the target Firebase project ID and Android package name (allowlisted)
|
||||
- a push mode
|
||||
- an opaque server/device correlation key
|
||||
- an opaque delivery identifier
|
||||
- optional badge count (disabled by default)
|
||||
|
||||
Neither provider path may send titles, item names, usernames, profile names, library names, server URLs, artwork URLs, watched-state metadata, or notification body text through FCM.
|
||||
|
||||
When the Android device wakes, the app should fetch real notification metadata directly from the user's Silo server using the existing authenticated server connection.
|
||||
|
||||
## Why this spec mirrors APNs
|
||||
|
||||
The reasoning, threat model, and fanout flow are identical to the APNs case. Apple and Google both gatekeep mobile push at the app-bundle level: only the developer who signed and published the app can authenticate pushes to it. The same arguments for a hosted relay apply unchanged. The differences are entirely about FCM's protocol and credentials:
|
||||
|
||||
- **Auth:** FCM v1 API uses **OAuth2 with a Google service account**, not a JWT signed with a `.p8` key.
|
||||
- **Endpoint:** Pushes go to `https://fcm.googleapis.com/v1/projects/{project_id}/messages:send`.
|
||||
- **Project model:** A Firebase project owns the Android app's package name + SHA-1 cert fingerprint mapping. The relay holds one service account JSON per official Firebase project.
|
||||
- **Topics vs tokens:** FCM supports both topic broadcasts and per-token sends. Silo uses **per-token sends only** — topic broadcasts would leak audience information.
|
||||
- **Message types:** FCM has `notification` and `data` messages. The privacy design uses **data-only messages** so the OS can't render content the relay shouldn't have constructed.
|
||||
|
||||
## Decision
|
||||
|
||||
Add configurable Android push providers for the official Silo Android app:
|
||||
|
||||
- `off`: no remote Android push
|
||||
- `silo_relay`: Silo's hosted FCM relay
|
||||
- `custom_fcm`: admin-supplied FCM service account JSON used directly by the server
|
||||
|
||||
The relay submits FCM v1 requests using Silo-controlled Firebase credentials for the official app package. A self-hosted server admin may opt in to use this relay. The relay's API should intentionally prevent notification content from being sent through the relay.
|
||||
|
||||
If `custom_fcm` is selected, the user's Silo server calls FCM v1 directly using admin-provided service account JSON. This avoids the Silo relay entirely, but it must use the same minimal payload and metadata-fetch rules as the hosted relay path.
|
||||
|
||||
The default push mode should be **Private Data Wake**:
|
||||
|
||||
- FCM delivers a data-only message.
|
||||
- The data payload contains only opaque identifiers.
|
||||
- The Android app handles the message via `FirebaseMessagingService.onMessageReceived` and constructs a generic local notification while it fetches the actual notification row from the user's server.
|
||||
|
||||
The implementation should also support **Background Wake** for low-noise sync, with the explicit understanding that Android background data messages are subject to Doze, App Standby, and battery-optimization restrictions and are not a reliable user-visible notification mechanism.
|
||||
|
||||
## Goals
|
||||
|
||||
- Make Android push possible without every admin managing their own Firebase project.
|
||||
- Keep actual notification metadata on the user's Silo server.
|
||||
- Make relay participation explicit and disabled by default.
|
||||
- Make FCM credentials configurable for admins who want to avoid the hosted relay.
|
||||
- Keep the relay stateless for device subscriptions where possible.
|
||||
- Keep FCM payloads opaque and generic (data-only).
|
||||
- Keep the hosted relay and custom FCM paths behaviorally equivalent from the app's point of view.
|
||||
- Fit on top of the durable notification inbox design.
|
||||
|
||||
## Non-Goals
|
||||
|
||||
- Replacing FCM for Android remote push.
|
||||
- Shipping notification content through the central relay.
|
||||
- Requiring Apple push services for this Android path.
|
||||
- Supporting Huawei Mobile Services (HMS) push, Amazon Device Messaging, or other Android-adjacent push systems in v1. Could be added later as parallel providers.
|
||||
- Adding marketing, analytics, or delivery-tracking exports to the relay.
|
||||
- Hiding FCM itself from Android devices. Custom FCM still uses Google FCM.
|
||||
- Making silent background delivery as reliable as visible notifications.
|
||||
|
||||
## Threat Model
|
||||
|
||||
### What The User's Server Knows
|
||||
|
||||
The user's Silo server knows:
|
||||
|
||||
- the notification content
|
||||
- the profile and user recipients
|
||||
- the registered local devices
|
||||
- FCM registration tokens for those devices
|
||||
- push delivery attempts and failures
|
||||
|
||||
This is acceptable because the server is already the authority for the user's media, profiles, sessions, and notification inbox.
|
||||
|
||||
### What The Central Relay May Know
|
||||
|
||||
The relay may see:
|
||||
|
||||
- an opt-in relay account or install identifier
|
||||
- the server's egress IP address (inherent to any hosted relay — the server connects to it directly; see the identical note in [`02-apns-relay.md`](./02-apns-relay.md))
|
||||
- FCM registration tokens submitted in send requests
|
||||
- request timestamps
|
||||
- coarse push mode, such as `private_data` or `background_wake`
|
||||
- opaque `collapse_key` values (per-server-keyed HMACs of series identity; same derivation rule as APNs `collapse_id`)
|
||||
- FCM response status and FCM message names
|
||||
- opaque delivery identifiers
|
||||
|
||||
The relay must not receive enough information to know what media item, notification type, profile, user, library, server hostname, or server URL caused the push.
|
||||
|
||||
### What Google May Know
|
||||
|
||||
Google / FCM may see:
|
||||
|
||||
- the official app package name and Firebase project
|
||||
- the target FCM registration token
|
||||
- FCM v1 request headers
|
||||
- request timing
|
||||
- the generic FCM data payload (which contains only opaque identifiers)
|
||||
|
||||
Google must not receive media titles, usernames, library names, server URLs, artwork URLs, or other notification metadata in the payload. In particular, the design uses **data-only messages**, never `notification` messages, so Google's servers don't carry rendering content.
|
||||
|
||||
### Residual Metadata Leakage
|
||||
|
||||
This design cannot hide that a device received a Silo push at a particular time, nor the egress IP of the server sending relay requests. It can only hide the meaning and content of the push. Additionally, on Android, FCM delivery may be delayed by Doze / App Standby; an adversary observing wake patterns may infer rough activity but not content.
|
||||
|
||||
## User And Admin Controls
|
||||
|
||||
### Server Admin Control
|
||||
|
||||
The server admin must explicitly choose an Android push provider.
|
||||
|
||||
Suggested setting:
|
||||
|
||||
```text
|
||||
notifications.android_push.provider = off | silo_relay | custom_fcm
|
||||
```
|
||||
|
||||
Default:
|
||||
|
||||
```text
|
||||
off
|
||||
```
|
||||
|
||||
If `silo_relay` is selected, the admin configures a relay API key and accepts a clear privacy notice:
|
||||
|
||||
```text
|
||||
Silo Relay can wake Android devices through Firebase Cloud Messaging,
|
||||
but notification details stay on this server. The relay receives FCM
|
||||
registration tokens, timestamps, opaque delivery IDs, and generic push mode
|
||||
only.
|
||||
```
|
||||
|
||||
If `custom_fcm` is selected, the admin configures their own Firebase service account JSON:
|
||||
|
||||
```text
|
||||
notifications.android_push.custom.service_account_json
|
||||
notifications.android_push.custom.project_id
|
||||
notifications.android_push.custom.allowed_packages = [...]
|
||||
```
|
||||
|
||||
The server then sends directly to FCM and does not call the Silo relay. The same private payload rules still apply.
|
||||
|
||||
### Device User Control
|
||||
|
||||
Each Android device must opt in independently:
|
||||
|
||||
- the OS notification permission must be granted (Android 13+ requires `POST_NOTIFICATIONS` runtime permission)
|
||||
- the app must be signed into a Silo server
|
||||
- the active profile must enable push notifications for that device
|
||||
- the server admin must have enabled an Android push provider
|
||||
|
||||
Admins must not be able to silently enroll every profile/device into remote push without the device having granted OS notification permission.
|
||||
|
||||
### Profile Control
|
||||
|
||||
Push preferences sit on top of the durable notification preferences from the inbox design. The push-mode field on a `push_devices` row is shared across platforms (`apple` and `android`) so the same `push_mode` enum applies.
|
||||
|
||||
Suggested profile/device modes:
|
||||
|
||||
- `off`: never send remote push to this device
|
||||
- `in_app_only`: websocket and inbox only
|
||||
- `private_push`: generic FCM data wake, fetch details from server
|
||||
- `full_preview`: out of scope for v1
|
||||
|
||||
V1 should ship only `off`, `in_app_only`, and `private_push`.
|
||||
|
||||
## System Architecture
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant App as Android App
|
||||
participant Server as User Silo Server
|
||||
participant Relay as Silo FCM Relay
|
||||
participant FCM as Google FCM
|
||||
|
||||
App->>App: Request POST_NOTIFICATIONS permission (API 33+)
|
||||
App->>App: Get FCM registration token via FirebaseMessaging.getToken()
|
||||
App->>Server: Register registration token and opaque server_device_id
|
||||
Server->>Server: Store encrypted token and device preferences
|
||||
Server->>Server: Create durable notification_delivery
|
||||
Server->>Relay: Send minimal FCM relay request
|
||||
Relay->>FCM: Submit data-only message via FCM v1 API
|
||||
FCM->>App: Deliver data message; FirebaseMessagingService.onMessageReceived
|
||||
App->>Server: Fetch notification metadata directly
|
||||
Server->>App: Return inbox rows for active profile
|
||||
```
|
||||
|
||||
## Ownership Boundaries
|
||||
|
||||
### This Repository
|
||||
|
||||
Silo owns:
|
||||
|
||||
- admin settings for selecting an Android push provider
|
||||
- profile and device notification preferences
|
||||
- FCM token registration from Android clients
|
||||
- durable storage of device registrations
|
||||
- push fanout from `notification_deliveries`
|
||||
- relay client integration
|
||||
- retry, backoff, and FCM error handling
|
||||
- inbox sync APIs used after wake
|
||||
|
||||
### Android Client Repository
|
||||
|
||||
The Android app owns:
|
||||
|
||||
- FCM registration via Firebase SDK
|
||||
- notification permission UX (`POST_NOTIFICATIONS` on API 33+)
|
||||
- device registration API calls
|
||||
- local server/device correlation
|
||||
- `FirebaseMessagingService.onMessageReceived` handling
|
||||
- fetching metadata from the user's server
|
||||
- rendering local notifications via `NotificationManager` after the wake
|
||||
|
||||
### Central Relay Service
|
||||
|
||||
The central relay owns:
|
||||
|
||||
- FCM service account credentials for the official Silo Firebase project(s)
|
||||
- relay API authentication
|
||||
- request validation and rate limiting
|
||||
- FCM v1 request submission with OAuth2 access token caching
|
||||
- redacted operational logs
|
||||
- minimal delivery status reporting
|
||||
|
||||
The relay should live outside this repo unless the project later decides to co-locate the service code. This repo defines the contract and implements the self-hosted server side. The relay implementation can share most code with the APNs relay; only the upstream provider client differs.
|
||||
|
||||
### Custom FCM Mode
|
||||
|
||||
When `custom_fcm` is selected, there is no central relay in the send path.
|
||||
|
||||
The user's Silo server owns:
|
||||
|
||||
- FCM v1 OAuth2 token generation from the configured service account JSON
|
||||
- access token caching (Google's OAuth2 access tokens are short-lived; cache and refresh on expiry)
|
||||
- package allowlisting for the official Silo app package(s) it intends to support
|
||||
- FCM v1 request construction (data-only)
|
||||
- FCM response handling
|
||||
- FCM credential rotation and secret storage
|
||||
|
||||
Custom FCM mode must not unlock richer payloads. It exists to let admins avoid the Silo relay, not to bypass the privacy-preserving notification contract.
|
||||
|
||||
## Data Model
|
||||
|
||||
The `push_devices` and `push_delivery_attempts` schemas are owned by [`02-apns-relay.md`](./02-apns-relay.md) and are platform-tagged from the start (`platform = 'apple' | 'android'`) so this spec adds no new columns. The Android-relevant columns on `push_devices` are:
|
||||
|
||||
- `platform text not null` — must be `'android'` for FCM rows.
|
||||
- `provider text not null` — `'silo_relay'` or `'custom_fcm'`.
|
||||
- `fcm_token_ciphertext bytea` — required for Android rows.
|
||||
- `fcm_token_hash text` — required for Android rows; used for dedupe/diagnostics without logging raw tokens.
|
||||
- `fcm_project_id text` — the Firebase project the token was minted under.
|
||||
- `fcm_package_name text` — the Android package name (e.g., `com.continuum.app.android`).
|
||||
- All `apns_*` columns must be null for Android rows. The `CHECK` constraint defined in `02-apns-relay.md` enforces this exclusivity.
|
||||
|
||||
Unique constraints (defined in `02-apns-relay.md`): `(profile_id, device_id, platform)` and `(server_device_id)`. Both are intentionally platform-scoped, not provider-scoped — the same Android device cannot register twice even if the admin flips between `silo_relay` and `custom_fcm`.
|
||||
|
||||
`push_delivery_attempts` columns relevant to FCM (also owned by 02):
|
||||
|
||||
- `provider text not null` — for FCM rows: `'silo_relay'` or `'custom_fcm'`.
|
||||
- `apns_id text` — null for FCM rows.
|
||||
- `fcm_message_name text` — populated on success with FCM v1's response `name` (e.g., `"projects/continuum-prod-android/messages/0:..."`); null for APNs rows.
|
||||
- `attempt_number integer not null` — 1-based; participates in the unique constraint and in the relay `Idempotency-Key`.
|
||||
|
||||
Retention is identical to APNs: ~14 days for successes, 30-90 for failures.
|
||||
|
||||
## Server API Surface
|
||||
|
||||
### Register Android Push Device
|
||||
|
||||
```http
|
||||
POST /api/v1/devices/push/fcm
|
||||
```
|
||||
|
||||
Profile-scoped. Requires normal auth plus active profile context.
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"device_id": "android-device-local-id",
|
||||
"fcm_token": "long-fcm-registration-token",
|
||||
"fcm_project_id": "continuum-prod-android",
|
||||
"fcm_package_name": "com.continuum.app.android",
|
||||
"push_mode": "private_push"
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "01J...",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"enabled": true,
|
||||
"push_mode": "private_push"
|
||||
}
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- `fcm_package_name` must be allowlisted for the configured provider's allowed_packages list.
|
||||
- `fcm_project_id` must match the configured Firebase project for the active provider.
|
||||
- registration is idempotent by `(profile_id, device_id, platform)` where platform is implied as `android`.
|
||||
- token rotation updates the encrypted token and token hash. FCM tokens rotate periodically; the app should call this endpoint whenever `FirebaseMessagingService.onNewToken` fires.
|
||||
- a disabled server push provider should still allow registration, but should report that remote push is unavailable in the capability response.
|
||||
|
||||
### Disable Push Device
|
||||
|
||||
Shared with APNs, see [`02-apns-relay.md`](./02-apns-relay.md):
|
||||
|
||||
```http
|
||||
DELETE /api/v1/devices/push/{id}
|
||||
```
|
||||
|
||||
### Push Capability
|
||||
|
||||
Extended from the APNs spec to include Android:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/capability
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"in_app": { "enabled": true },
|
||||
"apple_push": {
|
||||
"available": true,
|
||||
"provider": "silo_relay",
|
||||
"available_providers": ["silo_relay", "custom_apns"],
|
||||
"supported_modes": ["private_push", "in_app_only"]
|
||||
},
|
||||
"android_push": {
|
||||
"available": true,
|
||||
"provider": "silo_relay",
|
||||
"available_providers": ["silo_relay", "custom_fcm"],
|
||||
"supported_modes": ["private_push", "in_app_only"]
|
||||
},
|
||||
"webhooks": {
|
||||
"available": true,
|
||||
"max_per_profile": 10,
|
||||
"supported_types": ["discord", "generic"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Relay API Contract
|
||||
|
||||
The relay API should be intentionally narrow. The user server should not send an arbitrary FCM payload.
|
||||
|
||||
### Send Android Push
|
||||
|
||||
```http
|
||||
POST /v1/fcm/send
|
||||
Authorization: Bearer <relay_api_key>
|
||||
Idempotency-Key: <notification_delivery_id>:<push_device_id>:<attempt_number>
|
||||
```
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "fcm-registration-token",
|
||||
"project_id": "continuum-prod-android",
|
||||
"package_name": "com.continuum.app.android",
|
||||
"mode": "private_data",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_key": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"request_id": "01JRELAY...",
|
||||
"fcm_message_name": "projects/continuum-prod-android/messages/0:1234567890123456%abcdef",
|
||||
"status": "accepted"
|
||||
}
|
||||
```
|
||||
|
||||
Validation:
|
||||
|
||||
- `token` is required and must be plausible for FCM (FCM tokens are typically 152-180+ chars, base64url-ish).
|
||||
- `project_id` must match the configured Firebase project for this relay account's allowed packages.
|
||||
- `package_name` must be allowlisted for the relay account.
|
||||
- `mode` must be `private_data` or `background_wake`. Note: this is the **wire mode** (FCM-side); the profile-level mode is `private_push`. See [`00-architecture-overview.md`](./00-architecture-overview.md) "Mode terminology" for the mapping.
|
||||
- `server_device_id`, `delivery_id`, and `collapse_key` must be opaque values with length limits.
|
||||
- `collapse_key` derivation (server-side rule, not relay-enforced): identical to the APNs `collapse_id` rule in [`02-apns-relay.md`](./02-apns-relay.md) — `base32(HMAC-SHA256(server_collapse_secret, series_id))` truncated, using the same per-server secret. Never send raw or plainly-hashed `series_id`.
|
||||
- `badge` is optional. Default should be omitted to avoid leaking unread counts unless the user/admin explicitly enables badge sync.
|
||||
- no free-form notification title or body fields are accepted.
|
||||
- no image URL, media ID, username, server hostname, or server URL field is accepted.
|
||||
|
||||
### Relay-Built FCM Payloads
|
||||
|
||||
For `private_data`, the relay constructs:
|
||||
|
||||
```json
|
||||
{
|
||||
"message": {
|
||||
"token": "fcm-registration-token",
|
||||
"data": {
|
||||
"v": "1",
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
},
|
||||
"android": {
|
||||
"priority": "high",
|
||||
"collapse_key": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For `background_wake`, the relay constructs:
|
||||
|
||||
```json
|
||||
{
|
||||
"message": {
|
||||
"token": "fcm-registration-token",
|
||||
"data": {
|
||||
"v": "1",
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
},
|
||||
"android": {
|
||||
"priority": "normal",
|
||||
"collapse_key": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- The relay sends **data-only messages** (no top-level `notification` field) so Google never carries rendering content. The Android app constructs the local notification in `onMessageReceived`.
|
||||
- `priority: high` is used for `private_data` so the message wakes the app even under Doze. High-priority data messages are subject to FCM's anti-abuse quotas — if a server hits the quota, deliveries fall back to normal priority and may be deferred.
|
||||
- `collapse_key` allows multiple pushes for the same series to coalesce when the device is offline, mirroring `apns-collapse-id`.
|
||||
- The relay does not allow callers to override this payload shape in v1.
|
||||
|
||||
## Custom FCM Contract
|
||||
|
||||
`custom_fcm` should share the same internal send model as `silo_relay`, but replace the relay HTTP call with a direct FCM v1 API request.
|
||||
|
||||
The server-side push sender should accept only a structured internal request (same shape as the relay request body above):
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "fcm-registration-token",
|
||||
"project_id": "continuum-prod-android",
|
||||
"package_name": "com.continuum.app.android",
|
||||
"mode": "private_data",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_key": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
The sender builds the FCM v1 payload locally using the exact same payload shapes defined above for `private_data` and `background_wake`.
|
||||
|
||||
Configuration:
|
||||
|
||||
- `service_account_json`: full Firebase service account JSON, stored as a secret.
|
||||
- `project_id`: Firebase project ID.
|
||||
- `allowed_packages`: explicit Android package names this server is allowed to send for.
|
||||
|
||||
Rules:
|
||||
|
||||
- The admin may configure credentials, project ID, and allowed packages.
|
||||
- The server must not expose a free-form FCM JSON payload setting.
|
||||
- The server must not expose custom title/body templates for remote push in v1.
|
||||
- Package values must match the device registration package and the configured allowlist.
|
||||
- OAuth2 access tokens should be cached and regenerated before expiry (Google access tokens are typically valid for 1 hour).
|
||||
- Credential validation should provide a test-send or dry-run diagnostic that does not include notification content. FCM v1 supports `validate_only: true` requests for this purpose.
|
||||
|
||||
## Wake And Metadata Fetch
|
||||
|
||||
When the app receives an FCM data message:
|
||||
|
||||
1. `FirebaseMessagingService.onMessageReceived(remoteMessage)` fires.
|
||||
2. Read `data.v`, `data.server_device_id`, and `data.delivery_id`.
|
||||
3. Find the local server account that owns `server_device_id`.
|
||||
4. If the app is in the foreground, update in-app inbox state directly.
|
||||
5. If background:
|
||||
- Construct a generic local notification using `NotificationManager` with a localized "New Silo notification" title.
|
||||
- Schedule a `WorkManager` job to fetch the actual metadata from the user's server.
|
||||
- When the fetch returns, update the visible notification with real content (or replace it).
|
||||
6. If the app is opened from the notification, call the server before rendering the target screen.
|
||||
|
||||
Suggested fetch:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/sync?since=<last_cursor>
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/{delivery_id}
|
||||
```
|
||||
|
||||
The sync endpoint is preferred because push delivery is not guaranteed and multiple notification deliveries can be coalesced.
|
||||
|
||||
If the app cannot reach the server, it should keep the generic notification and retry the inbox sync when connectivity returns.
|
||||
|
||||
## Fanout Rules
|
||||
|
||||
Push fanout should happen after durable inbox delivery commits. Same flow as APNs:
|
||||
|
||||
1. `notification_deliveries` row is created. In the same transaction, the fanout worker enqueues one `pending` `push_delivery_attempts` row per enabled Android `push_device` (the dispatch outbox — see [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)).
|
||||
2. The realtime websocket event is published for connected clients.
|
||||
3. The push dispatcher claims the `pending` attempt rows post-commit, partitioned by platform; a recovery sweeper claims rows older than ~60s whose dispatch never ran.
|
||||
4. Each claimed attempt sends through the configured Android provider:
|
||||
- `silo_relay`: send the minimal relay request.
|
||||
- `custom_fcm`: build the same minimal FCM v1 payload locally and send directly.
|
||||
5. FCM errors update device state and attempt status.
|
||||
|
||||
Do not block notification delivery creation on FCM or relay availability.
|
||||
|
||||
Relay pacing is shared with APNs: one token bucket (`notifications.push.relay_send_rate`, default 5 req/sec) covers both dispatchers because both consume the same relay account quota. See [`02-apns-relay.md`](./02-apns-relay.md) "Relay pacing".
|
||||
|
||||
## FCM Error Handling
|
||||
|
||||
The server should handle relay/FCM failures without exposing notification content.
|
||||
|
||||
FCM v1 error codes worth handling explicitly:
|
||||
|
||||
- `UNREGISTERED` (404): the token is no longer valid (app uninstalled or data cleared). Disable the device and require re-registration.
|
||||
- `INVALID_ARGUMENT` (400): malformed request. Mark failed and surface admin diagnostics. Should be rare given the constrained payload.
|
||||
- `SENDER_ID_MISMATCH` (403): the token was minted under a different Firebase project. Disable and log.
|
||||
- `QUOTA_EXCEEDED` (429): retry with backoff, **honoring the `Retry-After` response header** if present (FCM uses standard HTTP semantics here). Common when high-priority data-message quotas are hit; FCM specifically throttles high-priority data messages that don't surface a user-visible notification on the device. Consider falling back to `priority: normal` after repeated 429s within a cooldown window.
|
||||
- `UNAVAILABLE` (503): FCM is temporarily down. Retry with backoff.
|
||||
- `INTERNAL` (500): retry with backoff.
|
||||
- `THIRD_PARTY_AUTH_ERROR` (401): credential failure (relay or custom_fcm). Surface in admin diagnostics; do not retry until the admin fixes it.
|
||||
- relay unavailable, FCM unavailable, or network timeout: retry with backoff.
|
||||
|
||||
Errors should be visible to admins as operational status, not to normal users unless their device needs reauthorization.
|
||||
|
||||
## Privacy Requirements
|
||||
|
||||
The implementation must satisfy these requirements before shipping:
|
||||
|
||||
- Relay requests contain no notification title or body.
|
||||
- Relay requests contain no media identifiers.
|
||||
- Relay requests contain no server hostname or base URL.
|
||||
- Relay requests contain no profile, username, library, collection, or item names.
|
||||
- Direct FCM payloads contain no notification title or body. Specifically: **no top-level `notification` object** in the FCM v1 message — data-only messages only.
|
||||
- Direct FCM payloads contain no media identifiers, server hostname, server URL, profile name, username, library name, collection name, or item name.
|
||||
- Relay logs redact raw FCM tokens.
|
||||
- Relay logs redact authorization headers and OAuth2 access tokens.
|
||||
- Relay logs do not persist full request payloads.
|
||||
- Server logs redact FCM tokens, custom service account JSON contents, and generated OAuth2 access tokens.
|
||||
- Badge count is disabled by default.
|
||||
- Push registration and remote push provider use are opt-in.
|
||||
- Device unregister disables further push attempts for that device.
|
||||
|
||||
## Security Requirements
|
||||
|
||||
- Relay API uses TLS only.
|
||||
- Relay API keys are scoped to a relay account or install identifier.
|
||||
- Relay API keys can be revoked without changing the user's Silo server authentication.
|
||||
- Relay requests are rate limited by API key and coarse token hash.
|
||||
- Relay supports idempotency keys to prevent duplicate FCM sends during retry.
|
||||
- Relay does not accept arbitrary FCM payload JSON in v1.
|
||||
- Server stores relay API keys as secrets.
|
||||
- Server stores custom FCM service account JSON as a secret.
|
||||
- Server never logs custom FCM service account JSON contents, generated OAuth2 access tokens, or FCM authorization headers.
|
||||
- Server stores FCM registration tokens encrypted at rest where local secret storage exists.
|
||||
- Admin diagnostics must not print raw FCM tokens or service account JSON.
|
||||
- Custom FCM service account JSON must validate as a real Google service account JSON before being accepted (parse + sanity check `type: "service_account"` and required key fields).
|
||||
|
||||
## Settings And UX
|
||||
|
||||
Admin settings should explain the tradeoff plainly:
|
||||
|
||||
```text
|
||||
Android Push Provider
|
||||
|
||||
Off
|
||||
No Android remote push. Devices still receive in-app realtime updates while open.
|
||||
|
||||
Silo Relay
|
||||
Uses Silo's FCM relay to wake Android devices. Notification details stay on
|
||||
this server. The relay receives FCM tokens, timestamps, and opaque delivery IDs.
|
||||
|
||||
Custom FCM
|
||||
Advanced. Send directly to Google FCM using your own Firebase service account.
|
||||
Notification details still stay on this server. Only useful if you've published
|
||||
your own signed Android app variant under your own Firebase project.
|
||||
```
|
||||
|
||||
Device settings (shared with APNs) should describe the user-visible mode:
|
||||
|
||||
```text
|
||||
Private Push
|
||||
Show a generic Silo notification, then fetch details from your server when
|
||||
this device wakes.
|
||||
```
|
||||
|
||||
Do not claim the central relay is fully self-hosted. The truthful claim is:
|
||||
|
||||
```text
|
||||
Notification content stays on your server. Google FCM, and the Silo relay
|
||||
if selected, may still process generic wake messages needed for Android push
|
||||
delivery.
|
||||
```
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### Task 1: Add Android Push Provider Settings
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/api/handlers/settings.go`
|
||||
- `web/src/lib/settingsManifest.ts` (or admin-scope equivalent)
|
||||
- settings UI files as needed
|
||||
|
||||
Add server/admin settings for:
|
||||
|
||||
- provider selection
|
||||
- relay endpoint
|
||||
- relay API key
|
||||
- custom FCM service account JSON
|
||||
- custom FCM project ID
|
||||
- custom FCM allowed packages
|
||||
- badge sync enabled or disabled (shared with Apple settings)
|
||||
|
||||
Default provider must be `off`.
|
||||
|
||||
### Task 2: Extend Push Device Registration
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- migration extending `push_devices` from the APNs spec to add FCM-specific columns and a check constraint
|
||||
- `internal/notifications` package
|
||||
- `internal/api/handlers/notifications.go`
|
||||
- `internal/api/router.go`
|
||||
|
||||
Add profile-scoped Android push device registration, token rotation, and disable APIs.
|
||||
|
||||
If the APNs spec lands first, this task is purely additive (new columns nullable, new endpoint, expanded `platform` enum).
|
||||
|
||||
### Task 3: Add Android Push Provider Clients
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/notifications/fcm_relay.go`
|
||||
- `internal/notifications/fcm_direct.go`
|
||||
- config/settings accessors
|
||||
|
||||
Implement the narrow `/v1/fcm/send` relay client. The client should not accept a free-form notification payload.
|
||||
|
||||
Implement the direct FCM v1 client for `custom_fcm` using:
|
||||
|
||||
- `golang.org/x/oauth2/google` for service-account-based OAuth2 token generation, or an equivalent that doesn't add a heavy Google SDK dependency.
|
||||
- Cached access token with refresh-before-expiry.
|
||||
- Standard `net/http` for the FCM v1 endpoint.
|
||||
|
||||
Both clients use the same internal request shape and payload builder so the dispatcher code is uniform.
|
||||
|
||||
### Task 4: Extend Push Fanout Worker
|
||||
|
||||
Files likely involved:
|
||||
|
||||
- `internal/notifications/push_fanout.go` (shared with APNs)
|
||||
|
||||
Trigger Android push fanout for devices with `platform = 'android'`. Use retries and record `push_delivery_attempts` with `fcm_message_name` populated on success.
|
||||
|
||||
### Task 5: Add Android Client Registration And Wake Handling
|
||||
|
||||
Files live in the Android client repository, not this server repo.
|
||||
|
||||
Required client behavior:
|
||||
|
||||
- request `POST_NOTIFICATIONS` permission on Android 13+
|
||||
- get FCM registration token via `FirebaseMessaging.getInstance().getToken()`
|
||||
- register token with the user's server
|
||||
- handle `FirebaseMessagingService.onNewToken` for token rotation
|
||||
- store `server_device_id` locally, scoped to the active server account
|
||||
- handle `onMessageReceived` for both foreground and background data messages
|
||||
- fetch notification metadata after wake/open
|
||||
- unregister or disable on sign-out / profile removal / app data clear
|
||||
|
||||
### Task 6: Add Admin Diagnostics
|
||||
|
||||
Expose high-level status (extending the APNs diagnostics surface):
|
||||
|
||||
- provider enabled/disabled
|
||||
- number of registered Android devices
|
||||
- last relay success
|
||||
- last relay failure code
|
||||
- custom FCM credential presence and project/package status
|
||||
- last custom FCM success
|
||||
- last custom FCM failure code
|
||||
- token/package/project mismatch warnings
|
||||
|
||||
Do not expose raw FCM tokens or service account JSON.
|
||||
|
||||
## Validation Plan
|
||||
|
||||
Use minimal verification while the work is still design-only. When implemented, verify:
|
||||
|
||||
- relay-disabled servers never call the relay
|
||||
- `custom_fcm` servers never call the relay
|
||||
- device registration is profile-scoped and idempotent
|
||||
- FCM tokens are redacted in logs
|
||||
- relay request bodies contain no titles, body text, item IDs, server URLs, or profile names
|
||||
- direct FCM payloads contain no `notification` object and no titles, body text, item IDs, server URLs, or profile names
|
||||
- custom FCM service account JSON is redacted in logs
|
||||
- generated OAuth2 access tokens are redacted in logs
|
||||
- creating one `notification_delivery` sends at most one push per enabled device
|
||||
- FCM token rotation updates the stored token without duplicating devices
|
||||
- `UNREGISTERED` and `SENDER_ID_MISMATCH` disable the affected device
|
||||
- `QUOTA_EXCEEDED` triggers backoff
|
||||
- app wake fetches notification metadata from the user's server
|
||||
- server offline after push leaves only the generic notification visible
|
||||
|
||||
## Open Questions
|
||||
|
||||
- Should official Silo Android builds use one Firebase project per build channel (debug / staging / prod), or share?
|
||||
- Should badge counts (Android notification dots / numbers) be allowed in `private_push`, or deferred?
|
||||
- Should the relay store hashed FCM token aliases to avoid sending raw tokens on every request? Defer to v2 unless rate-limit pressure justifies it.
|
||||
- Should the server fall back to `priority: normal` after repeated `QUOTA_EXCEEDED` to keep deliveries flowing at the cost of latency? Recommendation: yes, with a cooldown timer.
|
||||
- Should we also add a Huawei Mobile Services (HMS) push path for non-Google Android devices? Out of scope v1; could mirror this spec as `04-hms-relay.md` later.
|
||||
@@ -0,0 +1,731 @@
|
||||
# Outbound Webhooks Spec
|
||||
|
||||
**Date:** 2026-04-28
|
||||
**Status:** Draft
|
||||
**Scope:** Profile-scoped outbound webhook destinations for Silo notifications. Native Discord embed type and generic JSON+HMAC type.
|
||||
**Depends On:**
|
||||
- [`00-architecture-overview.md`](./00-architecture-overview.md)
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)
|
||||
|
||||
## Summary
|
||||
|
||||
Silo should let each profile send their notifications to user-chosen webhook destinations: a Discord channel, a Slack incoming webhook, a personal automation service, or any HTTP endpoint that accepts JSON. Webhooks are configured per profile, support per-webhook reason filters, and include full notification content by default — the profile chose the destination, so trust is implicit.
|
||||
|
||||
Two webhook types ship in v1:
|
||||
|
||||
- **Discord** — native Discord embed payload. The user pastes a Discord channel webhook URL; Silo POSTs Discord-formatted embeds.
|
||||
- **Generic** — canonical Silo JSON, HMAC-SHA256 signed via `X-Silo-Signature`. Suitable for Slack-incoming-webhook-style targets, custom user automations, or any HTTP endpoint.
|
||||
|
||||
## Why this is different from APNs / FCM
|
||||
|
||||
The privacy model inverts. APNs and FCM relay paths route through infrastructure operators (Apple, Google, Silo's relay) that the user did not directly choose. Therefore those paths carry no notification content — only opaque IDs.
|
||||
|
||||
Outbound webhooks go to a destination the **profile explicitly configured**. By choosing the URL, the profile is consenting to send notification content to that endpoint. There is no privacy benefit to gating content out of webhooks the profile set up themselves; doing so would just make the feature useless ("New episode! [check Silo]" is no better than what's already in the app).
|
||||
|
||||
The trust model is therefore:
|
||||
|
||||
- Profile chose the URL → notification content is included by default.
|
||||
- Discord embeds get series title, episode title, season/episode numbers, and poster.
|
||||
- Generic webhooks get the same data in canonical JSON, plus an HMAC signature derived from a per-webhook signing secret.
|
||||
- Silo still enforces privacy guardrails the profile cannot opt out of: HTTPS only, no localhost / RFC1918 destinations by default, redacted logs, retry/disable on persistent failure.
|
||||
|
||||
## Decision
|
||||
|
||||
Add per-profile webhook destinations, configurable through the profile's notification preferences UI:
|
||||
|
||||
- Profile creates a webhook by pasting a URL and optionally a name and reason filters.
|
||||
- Webhook type is detected from the URL or selected explicitly: `discord` (matches Discord webhook URL pattern) or `generic`.
|
||||
- Silo POSTs a typed payload to the URL whenever a `notification_deliveries` row commits for that profile and the webhook's reason filters allow it.
|
||||
- Failed deliveries are retried with exponential backoff and disabled after a configurable consecutive-failure threshold.
|
||||
- Each webhook has a profile-visible status: enabled, last success, last failure code, consecutive failure count.
|
||||
|
||||
## Goals
|
||||
|
||||
- Let a profile send Silo notifications to Discord, Slack, or any user-chosen URL.
|
||||
- Render natively in Discord without the user copy-pasting JSON templates.
|
||||
- Provide a stable, signed, well-documented generic format for everything else.
|
||||
- Keep the feature profile-scoped: a profile only sees and controls their own webhooks.
|
||||
- Add no new infrastructure dependencies (no relay needed; webhooks are direct outbound HTTP from the user's Silo server).
|
||||
- Make failure modes legible to the profile so they can fix a broken webhook themselves.
|
||||
|
||||
## Non-Goals
|
||||
|
||||
- Server-wide / admin-level webhooks for system events (scan complete, library health, etc.). Out of scope v1; Silo's existing realtime hub already handles operational events.
|
||||
- Webhook destinations that require OAuth (Slack apps, Discord bots, Microsoft Teams Adaptive Cards). Webhook-style endpoints with an unauth'd POST URL are sufficient for v1.
|
||||
- Per-event-type custom payload templates. The Discord embed shape and the generic JSON shape are fixed in v1; richer customization is v2.
|
||||
- Webhook retry windows beyond 24 hours. After ~24h of consecutive failures, the webhook is auto-disabled and the profile is notified in-app.
|
||||
- Multiple webhook destinations per webhook row (e.g., one row firing to two URLs). One URL per row.
|
||||
- Two-way webhooks. Silo only POSTs out; it does not consume webhook responses for state.
|
||||
|
||||
## Trust Model
|
||||
|
||||
The profile chose the destination URL. By creating a webhook, the profile consents to send full notification content (titles, posters, episode metadata) to that URL.
|
||||
|
||||
What we still don't trust the profile about:
|
||||
|
||||
- **HTTPS:** Required. `http://` URLs are rejected.
|
||||
- **Local / private destinations:** Rejected by default. Specifically, the URL host must resolve to an address outside all of the following ranges:
|
||||
- **IPv4 private/special:** `0.0.0.0/8`, `10.0.0.0/8`, `100.64.0.0/10` (CGNAT / RFC6598), `127.0.0.0/8`, `169.254.0.0/16`, `172.16.0.0/12`, `192.0.0.0/24` (IETF protocol assignments), `192.0.2.0/24` / `198.51.100.0/24` / `203.0.113.0/24` (TEST-NET-1/2/3), `192.88.99.0/24` (deprecated 6to4 anycast), `192.168.0.0/16`, `198.18.0.0/15` (benchmarking, RFC 2544), `224.0.0.0/4` (multicast), `240.0.0.0/4` (reserved future use)
|
||||
- **IPv6 private/special:** `::/128` (unspecified), `::1/128` (loopback), `fc00::/7` (ULA), `fe80::/10` (link-local), `2001:db8::/32` (documentation), `64:ff9b::/96` (NAT64)
|
||||
- **IPv4-mapped IPv6:** `::ffff:0:0/96` — the most likely real-world bypass. A literal `::ffff:127.0.0.1` resolves to loopback when the connection is established but bypasses naive IPv4-only checks. The validator must unwrap v4-mapped addresses and re-check against the IPv4 deny set.
|
||||
- **DNS names that resolve to any of the above** at registration time *and* at delivery time (DNS rebinding mitigation; see below).
|
||||
|
||||
An admin-only setting `notifications.webhooks.allow_private_destinations = true` may be set for dev environments. It applies globally to the server and is intended only for development.
|
||||
- **Server URL leakage:** Webhook payloads must **not** include the user's Silo server hostname, base URL, or absolute artwork URLs that include the server origin. Posters in Discord embeds use a profile-token-signed proxy URL (or, for v1, the embed omits images if the server URL would leak). Generic webhooks include only relative paths and let the receiver fetch from the server using their own knowledge of the server URL.
|
||||
- **Relay leakage:** The webhook delivery path runs entirely on the user's own Silo server; no Silo-operated infrastructure is involved.
|
||||
|
||||
## User Flow
|
||||
|
||||
```
|
||||
1. Profile opens Settings -> Notifications -> Webhooks.
|
||||
2. Profile clicks "Add webhook".
|
||||
3. Pastes a URL like https://discord.com/api/webhooks/123/abc.
|
||||
4. Silo auto-detects Discord type from URL pattern.
|
||||
5. Profile names the webhook ("Family Discord") and toggles
|
||||
reason filters: favorites on, watchlist on, continue_watching off, next_up off.
|
||||
6. Profile clicks "Test", Silo POSTs a sample notification.
|
||||
7. Discord channel shows the test embed; profile clicks "Save".
|
||||
8. From now on, matching new-episode notifications post to Discord.
|
||||
9. If the webhook URL becomes invalid (Discord deletes it), Silo:
|
||||
a. Marks each 404'd delivery failed immediately (non-retryable 4xx);
|
||||
transient network failures instead retry with backoff for ~24h.
|
||||
b. After 3 consecutive non-retryable-4xx deliveries (or the retry
|
||||
threshold for persistent network failures), auto-disables the webhook.
|
||||
c. Posts an in-app notification to the profile: "Your 'Family Discord'
|
||||
webhook stopped working. Last error: 404. Edit settings to fix."
|
||||
```
|
||||
|
||||
## Data Model
|
||||
|
||||
### `notification_webhooks`
|
||||
|
||||
Purpose: profile-scoped webhook destination.
|
||||
|
||||
Columns:
|
||||
|
||||
- `id text primary key`
|
||||
- `user_id integer not null` (matches `users.id integer`)
|
||||
- `profile_id text not null`
|
||||
- `name varchar(64) not null` — user-friendly label, capped to bound row size
|
||||
- `type text not null` — `'discord'` or `'generic'`
|
||||
- `url_ciphertext bytea not null` — destination URL, encrypted at rest
|
||||
- `url_host varchar(253) not null` — denormalized host (no path) for diagnostics and validation logs (253 = max DNS host length)
|
||||
- `signing_secret_ciphertext bytea` — null for `discord` (Discord webhooks don't sign); required for `generic`
|
||||
- `enabled boolean not null default true`
|
||||
- `notify_favorites boolean not null default true`
|
||||
- `notify_watchlist boolean not null default true`
|
||||
- `notify_continue_watching boolean not null default true`
|
||||
- `notify_next_up boolean not null default true`
|
||||
- `consecutive_failures integer not null default 0`
|
||||
- `disabled_reason varchar(256)` — populated when auto-disabled
|
||||
- `last_success_at timestamptz`
|
||||
- `last_failure_at timestamptz`
|
||||
- `last_failure_status integer`
|
||||
- `last_failure_message varchar(256)` — short, non-sensitive diagnostic, e.g., "404 Not Found"
|
||||
- `created_at timestamptz not null default now()`
|
||||
- `updated_at timestamptz not null default now()`
|
||||
|
||||
Constraints:
|
||||
|
||||
- `(profile_id, name)` is unique per profile to prevent duplicate-name confusion.
|
||||
- `CHECK (type IN ('discord', 'generic'))`.
|
||||
- `CHECK (type = 'discord' OR signing_secret_ciphertext IS NOT NULL)` — generic webhooks must have a signing secret.
|
||||
|
||||
Indexes:
|
||||
|
||||
- `(profile_id)` for the listing endpoint.
|
||||
- `(profile_id, enabled)` partial index where `enabled = true` for the dispatcher hot path.
|
||||
|
||||
Notes:
|
||||
|
||||
- URLs and signing secrets are encrypted using the same local secret-storage facility as other sensitive columns (e.g., APNs tokens). If that facility doesn't exist yet, this spec lands the columns as plain `bytea` containing UTF-8 bytes for v1; an explicit follow-up adds the encryption layer.
|
||||
- `disabled_reason` is set when the system auto-disables; cleared when the profile re-enables.
|
||||
|
||||
### `webhook_delivery_attempts`
|
||||
|
||||
Purpose: operational record of webhook attempts.
|
||||
|
||||
Columns:
|
||||
|
||||
- `id text primary key`
|
||||
- `notification_delivery_id text not null references notification_deliveries(id) on delete cascade`
|
||||
- `webhook_id text not null references notification_webhooks(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` — `'pending'`, `'delivered'`, `'retrying'`, `'failed'`, `'auto_disabled'`
|
||||
- `failure_message varchar(256)` — short diagnostic (e.g., HTTP status text, DNS error class). Must not include payload contents.
|
||||
|
||||
Constraints:
|
||||
|
||||
- `(webhook_id, notification_delivery_id, attempt_number)` is unique to prevent double-claims under retry concurrency.
|
||||
- `CHECK (outcome IN ('pending', 'delivered', 'retrying', 'failed', 'auto_disabled'))`.
|
||||
- index on `(webhook_id, attempted_at desc)` for per-webhook history listing.
|
||||
- index on `(outcome, next_retry_at)` for the retry worker.
|
||||
|
||||
Retention:
|
||||
|
||||
- keep `delivered` rows for ~7 days
|
||||
- keep `failed`/`auto_disabled` rows for ~30 days for profile-visible debugging
|
||||
- keep `pending`/`retrying` rows until they resolve
|
||||
|
||||
## Server API Surface
|
||||
|
||||
All endpoints are profile-scoped and require `X-Profile-Id` middleware.
|
||||
|
||||
### List Webhooks
|
||||
|
||||
```http
|
||||
GET /api/v1/notifications/webhooks
|
||||
```
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"webhooks": [
|
||||
{
|
||||
"id": "01J...",
|
||||
"name": "Family Discord",
|
||||
"type": "discord",
|
||||
"url_host": "discord.com",
|
||||
"enabled": true,
|
||||
"notify_favorites": true,
|
||||
"notify_watchlist": true,
|
||||
"notify_continue_watching": false,
|
||||
"notify_next_up": false,
|
||||
"last_success_at": "2026-04-27T18:32:11Z",
|
||||
"last_failure_at": null,
|
||||
"last_failure_status": null,
|
||||
"last_failure_message": null,
|
||||
"consecutive_failures": 0,
|
||||
"disabled_reason": null
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Note the response **never includes the full URL or signing secret**. It returns only `url_host` so the profile can identify which Discord/Slack/etc. the webhook points at without leaking the secret URL token (which functions as the auth credential for Discord webhooks).
|
||||
|
||||
### Create Webhook
|
||||
|
||||
```http
|
||||
POST /api/v1/notifications/webhooks
|
||||
```
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Family Discord",
|
||||
"url": "https://discord.com/api/webhooks/123456/abcdef-token",
|
||||
"type": "discord",
|
||||
"notify_favorites": true,
|
||||
"notify_watchlist": true,
|
||||
"notify_continue_watching": false,
|
||||
"notify_next_up": false
|
||||
}
|
||||
```
|
||||
|
||||
Rules:
|
||||
|
||||
- `type` is optional; if omitted, the server detects Discord URLs via pattern (`https://discord.com/api/webhooks/{id}/{token}` or `https://discordapp.com/...`). All other URLs default to `generic`.
|
||||
- For `generic`, the server generates a random signing secret (32 bytes, base64) and returns it **once** in the response. The profile is responsible for storing it on the receiving service.
|
||||
- URL must pass private-destination guards.
|
||||
- URL must use `https`.
|
||||
- `name` length capped at 64 chars; reject empty or whitespace-only.
|
||||
- Per-profile cap: max 10 webhooks (configurable via admin setting; documented as `notifications.webhooks.max_per_profile`).
|
||||
|
||||
Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "01J...",
|
||||
"name": "Family Discord",
|
||||
"type": "discord",
|
||||
"url_host": "discord.com",
|
||||
"enabled": true,
|
||||
"notify_favorites": true,
|
||||
"notify_watchlist": true,
|
||||
"notify_continue_watching": false,
|
||||
"notify_next_up": false,
|
||||
"signing_secret": null
|
||||
}
|
||||
```
|
||||
|
||||
For `generic`:
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "01J...",
|
||||
"name": "My Slack Webhook",
|
||||
"type": "generic",
|
||||
"url_host": "hooks.slack.com",
|
||||
"enabled": true,
|
||||
"notify_favorites": true,
|
||||
"notify_watchlist": true,
|
||||
"notify_continue_watching": true,
|
||||
"notify_next_up": true,
|
||||
"signing_secret": "base64-encoded-secret-shown-once"
|
||||
}
|
||||
```
|
||||
|
||||
`signing_secret` is **only** returned at create time and is never re-fetchable. Rotation requires a separate endpoint.
|
||||
|
||||
### Update Webhook
|
||||
|
||||
```http
|
||||
PUT /api/v1/notifications/webhooks/{id}
|
||||
```
|
||||
|
||||
Request fields are all optional; included fields are updated:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Renamed",
|
||||
"enabled": true,
|
||||
"notify_favorites": false,
|
||||
"url": "https://discord.com/api/webhooks/.../new-token"
|
||||
}
|
||||
```
|
||||
|
||||
Updating the URL re-validates against the private-destination guard and resets `consecutive_failures` to 0.
|
||||
|
||||
### Delete Webhook
|
||||
|
||||
```http
|
||||
DELETE /api/v1/notifications/webhooks/{id}
|
||||
```
|
||||
|
||||
Idempotent. Cascades to `webhook_delivery_attempts`.
|
||||
|
||||
### Rotate Signing Secret (generic only)
|
||||
|
||||
```http
|
||||
POST /api/v1/notifications/webhooks/{id}/rotate-secret
|
||||
```
|
||||
|
||||
Generates and returns a new signing secret. The profile must update the receiving service to use the new secret.
|
||||
|
||||
### Test Webhook
|
||||
|
||||
```http
|
||||
POST /api/v1/notifications/webhooks/{id}/test
|
||||
```
|
||||
|
||||
Synchronously POSTs a sample payload to the destination and returns the result:
|
||||
|
||||
```json
|
||||
{
|
||||
"ok": true,
|
||||
"http_status": 204,
|
||||
"duration_ms": 187
|
||||
}
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```json
|
||||
{
|
||||
"ok": false,
|
||||
"http_status": 404,
|
||||
"duration_ms": 234,
|
||||
"message": "404 Not Found"
|
||||
}
|
||||
```
|
||||
|
||||
The test payload is identical in shape to a real notification but is clearly marked (e.g., embed footer text "Silo test notification" for Discord; `"test": true` field in generic). Test sends do not consume the retry/auto-disable counters.
|
||||
|
||||
## Payload Formats
|
||||
|
||||
### Discord
|
||||
|
||||
The Discord webhook API documents the `POST /api/webhooks/{id}/{token}` endpoint, which accepts a JSON body with optional `content` (plain text) and `embeds` (rich cards). Silo sends embeds only; no `content` field, so the message renders cleanly without a leading text line.
|
||||
|
||||
**v1 payload (text-only, no images):**
|
||||
|
||||
```json
|
||||
{
|
||||
"embeds": [
|
||||
{
|
||||
"title": "Severance — S2 E1: Hello, Ms. Cobel",
|
||||
"description": "New episode available on Silo",
|
||||
"color": 5814783,
|
||||
"footer": {
|
||||
"text": "Silo • Severance"
|
||||
},
|
||||
"timestamp": "2026-04-28T12:34:56Z",
|
||||
"fields": [
|
||||
{ "name": "Reason", "value": "Favorited & Continue Watching", "inline": true },
|
||||
{ "name": "Season", "value": "2", "inline": true },
|
||||
{ "name": "Episode", "value": "1", "inline": true }
|
||||
]
|
||||
}
|
||||
],
|
||||
"username": "Silo"
|
||||
}
|
||||
```
|
||||
|
||||
**v1.5 payload (with image proxy, additive):**
|
||||
|
||||
In v1.5, the builder adds `image` (poster) and `avatar_url` (Silo mark) fields, both pointing at `media.discord-cdn-proxy.silo.app` URLs. **In v1, these fields MUST be omitted entirely** — the builder must not fall back to absolute URLs that include the user's server origin. The privacy contract is broken if Discord sees the user's server URL in any embed field.
|
||||
|
||||
Notes on the Discord payload:
|
||||
|
||||
- **v1 builder rule:** the `image` field, the embed's `url` field, and the top-level `avatar_url` field must be omitted. Discord renders the embed without an image; the channel webhook's configured avatar is used. There is no fallback to the user's server URL.
|
||||
- **v1.5 builder rule:** `image.url` and `avatar_url` use a CDN proxy hosted at `media.discord-cdn-proxy.silo.app`. The proxy is a small Silo-operated service that takes a short-lived signed URL token (issued by the user's server when constructing the webhook payload) and server-side streams the corresponding asset from the user's server. The leak vector being mitigated is *Discord's own infrastructure* fetching and caching the embed image — Discord's CDN (`media.discordapp.net`) fetches once from whatever origin the embed names; end-users only ever see the cached `media.discordapp.net` URL. The proxy ensures the origin Discord fetches from is the proxy, not the user's server.
|
||||
- **Discord embed limits** (apply to both v1 and v1.5; payload builder must enforce):
|
||||
- 6,000 char total across `title` + `description` + `field.name` + `field.value` + `footer.text` per embed
|
||||
- `title`: 256 chars
|
||||
- `description`: 4,096 chars
|
||||
- `fields`: 25 max; `field.name` 256 chars, `field.value` 1,024 chars
|
||||
- `footer.text`: 2,048 chars
|
||||
- 10 embeds per message (Silo sends one)
|
||||
- **Truncation policy:** prefer to truncate `description` first (with ellipsis), then field values. Never truncate `title`. If even truncated content exceeds the 6,000-char total, drop fields right-to-left until under cap.
|
||||
- `color` is a Discord embed accent color encoded as decimal RGB. Silo picks per-reason: favorite=5814783 (Silo brand purple), watchlist=3066993 (green), continue_watching=15844367 (yellow), next_up=15158332 (red).
|
||||
- `username` overrides the Discord webhook's default name to "Silo".
|
||||
- The webhook URL itself is the auth — Discord's webhook tokens are bearer credentials in the URL path. This is why we never return the URL on read.
|
||||
|
||||
The Discord embed structure follows Discord's webhook API. See [Discord embed object docs](https://discord.com/developers/docs/resources/channel#embed-object) for the full schema.
|
||||
|
||||
### Generic
|
||||
|
||||
JSON body, signed via HMAC-SHA256 over the raw bytes Silo sends. Receivers verify against the literal bytes they received — no canonicalization required on either side.
|
||||
|
||||
Headers:
|
||||
|
||||
- `Content-Type: application/json`
|
||||
- `User-Agent: Silo-Webhook/1.0`
|
||||
- `X-Silo-Event: notification.created`
|
||||
- `X-Silo-Webhook-Id: 01J...` — the webhook row ID
|
||||
- `X-Silo-Delivery-Id: 01J...` — the underlying `notification_deliveries.id`
|
||||
- `X-Silo-Timestamp: 1714299296` — Unix epoch seconds, integer
|
||||
- `X-Silo-Signature: t=1714299296,v1=<hex-hmac-sha256>`
|
||||
- HMAC-SHA256 of the byte string `{X-Silo-Timestamp}.{request body bytes}` using the per-webhook signing secret. The result is hex-encoded.
|
||||
- Format follows Stripe's signing convention so receivers can use existing libraries (Stripe Go SDK, `stripe-signature` ports, etc.).
|
||||
- **The timestamp value in the body's `timestamp` field is informational and may be RFC3339 for human readability; only the `X-Silo-Timestamp` header value (Unix epoch integer) participates in the HMAC computation.**
|
||||
|
||||
Body:
|
||||
|
||||
```json
|
||||
{
|
||||
"event": "notification.created",
|
||||
"delivery_id": "01J...",
|
||||
"webhook_id": "01J...",
|
||||
"timestamp": "2026-04-28T12:34:56Z",
|
||||
"version": 1,
|
||||
"test": false,
|
||||
"profile_id": "profile-1",
|
||||
"library_id": 7,
|
||||
"type": "episode.available",
|
||||
"reason_flags": {
|
||||
"favorite": true,
|
||||
"watchlist": false,
|
||||
"continue_watching": true,
|
||||
"next_up": true
|
||||
},
|
||||
"series": {
|
||||
"id": "series-123",
|
||||
"title": "Severance"
|
||||
},
|
||||
"episode": {
|
||||
"id": "episode-456",
|
||||
"title": "Hello, Ms. Cobel",
|
||||
"season_number": 2,
|
||||
"episode_number": 1
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Notes on the generic payload:
|
||||
|
||||
- The body is canonicalized (sorted keys, UTF-8 encoded) before HMAC computation so the receiver can verify deterministically.
|
||||
- `profile_id` is included so the receiver can route by profile (one Slack channel per profile, etc.). It is the profile's own ID and is acceptable to share with the destination the profile chose.
|
||||
- No server URL, no absolute poster URLs, no library name. The receiver cannot link this back to the user's server hostname unless the profile has separately told them.
|
||||
- `version: 1` allows future schema changes without breaking existing receivers.
|
||||
- `test: true` distinguishes test sends from real sends.
|
||||
|
||||
### Signature Verification (generic)
|
||||
|
||||
Receivers verify by:
|
||||
|
||||
```
|
||||
parts = X-Silo-Signature.split(",")
|
||||
ts = parts.find("t=").value // Unix epoch integer (string form)
|
||||
v1 = parts.find("v1=").value // hex string
|
||||
|
||||
raw_body = literal request body bytes (do NOT re-parse and re-serialize JSON)
|
||||
expected = hex(hmac_sha256(secret, ts + "." + raw_body))
|
||||
|
||||
if constant_time_compare(v1, expected) and abs(now_epoch - parse_int(ts)) < 300:
|
||||
accept
|
||||
else:
|
||||
reject
|
||||
```
|
||||
|
||||
Key rules:
|
||||
|
||||
- Sign and verify the **literal request body bytes**, not a re-canonicalized form. This eliminates JSON-canonicalization ambiguity (number formatting, escape rules, key ordering) and matches Stripe's pattern.
|
||||
- `ts` and `now_epoch` are both Unix epoch seconds (integer). The timestamp window of 300 seconds (5 minutes) prevents replay; receivers may tighten if they wish.
|
||||
- Use a constant-time comparator (`hmac.equal`, `crypto/subtle.ConstantTimeCompare`, etc.) to avoid timing attacks.
|
||||
|
||||
### Signing Secret Rotation
|
||||
|
||||
`POST /api/v1/notifications/webhooks/{id}/rotate-secret` generates a new signing secret and returns it once. After rotation:
|
||||
|
||||
- The new secret takes effect immediately for all subsequent deliveries.
|
||||
- **Pending and retrying delivery attempts re-sign with the current secret on each retry**, not the secret that was active when the attempt was first enqueued. This means receivers who have updated to the new secret will accept the retried delivery; receivers still on the old secret will reject it. The expected operational pattern is: profile rotates the secret and updates their receiver atomically (or near-atomically); brief retry-window mismatches are acceptable.
|
||||
- Silo does **not** keep the old secret around after rotation. There is no dual-acceptance window.
|
||||
|
||||
## Delivery Semantics
|
||||
|
||||
### Send path
|
||||
|
||||
1. The fanout transaction in [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md) commits the `notification_deliveries` row **and**, in the same transaction, one `webhook_delivery_attempts` row with `outcome = 'pending'` per enabled, reason-matching webhook (the dispatch outbox). Two filters apply at enqueue time:
|
||||
- **Type deny list.** `webhook.auto_disabled` deliveries never enqueue webhook attempts — a webhook auto-disable notice must never be re-dispatched as a webhook, otherwise a broken webhook would generate an auto-disable notification that fires another webhook attempt that fails again, looping. Other notification types may join this list as they're added.
|
||||
- **Per-webhook reason filter** (`notify_favorites`, etc.). No matching reason, no attempt row.
|
||||
2. `WebhookDispatcher.Dispatch(delivery)` runs post-commit and claims the delivery's `pending` attempt rows (`FOR UPDATE SKIP LOCKED`).
|
||||
3. For each claimed attempt:
|
||||
- Construct the payload (Discord embed or generic JSON).
|
||||
- POST the payload with a 10-second total timeout.
|
||||
- On success (HTTP 2xx): mark attempt `delivered`, update webhook `last_success_at`, reset `consecutive_failures = 0`.
|
||||
- On failure: mark attempt `retrying` with `next_retry_at`, increment `consecutive_failures` on the webhook.
|
||||
4. The retry worker reads `webhook_delivery_attempts WHERE (outcome = 'retrying' AND next_retry_at <= now()) OR (outcome = 'pending' AND attempted_at <= now() - interval '60 seconds')` — the second arm is outbox recovery for attempts whose post-commit dispatch never ran (process crash).
|
||||
5. When `consecutive_failures` exceeds the threshold (default 10) over a span > 24h, the webhook is auto-disabled (`enabled = false`, `disabled_reason = '...'`), and an in-app notification is posted to the profile via the in-app inbox.
|
||||
|
||||
### Preference precedence (relationship to `notification_preferences`)
|
||||
|
||||
Profile-level `notification_preferences.notify_*` flags are a **hard gate** applied during fanout (see [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)). A delivery row is never created if all matching reasons are disabled at the profile level — so the dispatcher never sees those events.
|
||||
|
||||
Per-webhook `notify_*` flags are an **additional filter** applied during dispatch. They can narrow what fires for a specific webhook, but they cannot re-enable a reason the profile has globally disabled. The frontend must reflect this: if a profile turns off `notify_continue_watching` globally, the per-webhook continue-watching checkbox should appear disabled with explanatory text rather than implying the user can re-enable it for one destination.
|
||||
|
||||
### Retry schedule
|
||||
|
||||
Exponential backoff, capped at 24h total:
|
||||
|
||||
| Attempt | Delay since first attempt |
|
||||
|---|---|
|
||||
| 1 | 0 (immediate) |
|
||||
| 2 | 30s |
|
||||
| 3 | 2m |
|
||||
| 4 | 10m |
|
||||
| 5 | 30m |
|
||||
| 6 | 2h |
|
||||
| 7 | 6h |
|
||||
| 8 | 12h |
|
||||
| 9 | 18h |
|
||||
| 10 | 24h, then auto-disable |
|
||||
|
||||
Each attempt has a 10s total timeout.
|
||||
|
||||
**4xx skip-retry:** 4xx HTTP responses (except `408 Request Timeout`, `425 Too Early`, `429 Too Many Requests`) are deterministic destination-side rejections — retrying *this delivery* won't help, so the attempt is marked `failed` immediately without walking the 10-attempt schedule. Auto-disable, however, requires **3 consecutive deliveries** to fail with a non-retryable 4xx (tracked via `consecutive_failures` plus the failure-status class) before the webhook is disabled. A single 4xx is not proof the webhook is dead: destination-side WAF/CDN blips (Discord behind Cloudflare) intermittently return 403/400 for valid webhooks, and instant disable on one such blip would force pointless manual re-enables. Three consecutive deterministic rejections is strong evidence the URL is actually gone. The profile is notified in-app per "Auto-Disable Notification" below.
|
||||
|
||||
`429 Too Many Requests` honors the `Retry-After` header if present (overriding the schedule above for that attempt).
|
||||
|
||||
### Concurrency
|
||||
|
||||
The dispatcher fans webhook deliveries in parallel within a small bounded pool (e.g., 16 workers per server process). One slow webhook destination cannot block other deliveries.
|
||||
|
||||
### Test delivery isolation
|
||||
|
||||
`POST /webhooks/{id}/test` sends synchronously, does not write to `webhook_delivery_attempts`, does not affect `consecutive_failures`, and returns the HTTP result inline.
|
||||
|
||||
## Privacy Requirements
|
||||
|
||||
- Webhook URLs are encrypted at rest where the local encryption facility supports it.
|
||||
- Webhook URLs are never returned in API responses (only `url_host`).
|
||||
- Generic signing secrets are returned only at create / rotate time; never readable after.
|
||||
- Logs redact full URLs (host only) and full signing secrets.
|
||||
- Logs do not persist webhook payload bodies. Failure logs may include HTTP status text.
|
||||
- Webhook payloads must not include the Silo server URL or absolute artwork URLs that include the server origin.
|
||||
- An admin must not be able to read another profile's webhook URLs or signing secrets.
|
||||
|
||||
## Security Requirements
|
||||
|
||||
- HTTPS-only destinations.
|
||||
- Private-destination guard on URL submission and on each delivery (re-resolve to catch DNS rebinding to private IPs).
|
||||
- HMAC-SHA256 with per-webhook 32-byte signing secrets for `generic`.
|
||||
- Signing secret returned only at create / rotate; rotation immediately invalidates the previous secret.
|
||||
- Per-profile cap on webhook count (default 10).
|
||||
- Per-profile rate limit on webhook delivery (e.g., 60 deliveries / 60 seconds / profile across all webhooks). Notifications that exceed the limit are still durably stored in the inbox; webhooks just don't fire for them. Logged so admins can spot runaway scenarios.
|
||||
- TLS verification is enforced and not user-overridable.
|
||||
- HTTP redirects are followed (max 3 hops) but the final URL must still pass the private-destination guard at each hop.
|
||||
|
||||
## DNS Rebinding Mitigation
|
||||
|
||||
A naive implementation that resolves the URL once at registration time is vulnerable to DNS rebinding: the host could resolve to a public IP at registration and a private IP at delivery time. Mitigation:
|
||||
|
||||
- At delivery time, resolve the host yourself, validate the resolved IPs against the private deny set, then connect using the validated IP (or a controlled `net.Dialer` that re-validates the address it's about to connect to).
|
||||
- Do not let the standard library transparently re-resolve on each request.
|
||||
- The `internal/notifications/webhook_http.go` HTTP client should use a custom `Dialer.Control` callback that inspects the resolved address and refuses connections to private ranges.
|
||||
|
||||
## Settings And UX
|
||||
|
||||
Profile-level settings:
|
||||
|
||||
```text
|
||||
Webhooks
|
||||
|
||||
Add webhook
|
||||
Send Silo notifications to a webhook URL.
|
||||
Discord URLs render as native Discord embeds.
|
||||
Other URLs receive signed JSON.
|
||||
|
||||
[Family Discord] ✓ enabled
|
||||
discord.com
|
||||
✓ Favorites ✓ Watchlist ☐ Continue watching ☐ Next up
|
||||
Last success: 2 hours ago
|
||||
[ Test ] [ Edit ] [ Delete ]
|
||||
|
||||
[Slack: #media] ✓ enabled
|
||||
hooks.slack.com
|
||||
✓ all reasons
|
||||
Last failure: 3 minutes ago — 404 Not Found
|
||||
⚠ This webhook is failing. Check the destination URL.
|
||||
[ Test ] [ Edit ] [ Delete ]
|
||||
|
||||
+ Add webhook
|
||||
```
|
||||
|
||||
Admin-level settings (server-wide guards, default values):
|
||||
|
||||
- `notifications.webhooks.max_per_profile` (default 10)
|
||||
- `notifications.webhooks.allow_private_destinations` (default false; for dev environments)
|
||||
- `notifications.webhooks.deliveries_per_minute_per_profile` (default 60)
|
||||
|
||||
## Implementation Plan
|
||||
|
||||
### Task 1: Schema
|
||||
|
||||
Files:
|
||||
|
||||
- new migration under `migrations/` adding `notification_webhooks` and `webhook_delivery_attempts` tables.
|
||||
|
||||
### Task 2: Webhook Repository And Service
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/notifications/webhook_repo.go`
|
||||
- `internal/notifications/webhook_service.go` (CRUD, validation, signing-secret handling)
|
||||
|
||||
Includes:
|
||||
|
||||
- URL validation (https, private-destination guard)
|
||||
- Discord URL detection
|
||||
- signing secret generation
|
||||
- per-profile count enforcement
|
||||
|
||||
### Task 3: HTTP Client With Address Guard
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/notifications/webhook_http.go`
|
||||
|
||||
Custom HTTP client with:
|
||||
|
||||
- 10-second timeout
|
||||
- TLS verification non-overridable
|
||||
- `Dialer.Control` callback that re-validates the resolved IP at connect time
|
||||
- bounded redirect handling
|
||||
- structured error reporting (DNS error class, TCP, TLS, HTTP status) for `failure_message`
|
||||
|
||||
### Task 4: Payload Builders
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/notifications/webhook_payload_discord.go`
|
||||
- `internal/notifications/webhook_payload_generic.go`
|
||||
|
||||
Each pure function `func Build(delivery NotificationDelivery, hook Webhook) ([]byte, error)`. No side effects, easy to unit test.
|
||||
|
||||
### Task 5: Dispatcher Integration
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/notifications/webhook_dispatcher.go`
|
||||
|
||||
Implements the channel `Dispatcher` interface from `00-architecture-overview.md`. Loads enabled webhooks for the delivery's profile, applies reason filters, posts payloads, records attempts.
|
||||
|
||||
### Task 6: Retry Worker
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/notifications/webhook_retry_worker.go`
|
||||
|
||||
Polls `webhook_delivery_attempts WHERE outcome = 'retrying' AND next_retry_at <= now()` with `FOR UPDATE SKIP LOCKED`. Re-dispatches and updates state. Handles auto-disable.
|
||||
|
||||
### Task 7: API Handlers And Routes
|
||||
|
||||
Files:
|
||||
|
||||
- `internal/api/handlers/notifications_webhooks.go`
|
||||
- `internal/api/router.go`
|
||||
|
||||
Routes:
|
||||
|
||||
- `GET /api/v1/notifications/webhooks`
|
||||
- `POST /api/v1/notifications/webhooks`
|
||||
- `PUT /api/v1/notifications/webhooks/{id}`
|
||||
- `DELETE /api/v1/notifications/webhooks/{id}`
|
||||
- `POST /api/v1/notifications/webhooks/{id}/test`
|
||||
- `POST /api/v1/notifications/webhooks/{id}/rotate-secret`
|
||||
|
||||
All require `RequireProfile` middleware.
|
||||
|
||||
### Task 8: Frontend
|
||||
|
||||
Files:
|
||||
|
||||
- `web/src/pages/settings/NotificationWebhooksSettings.tsx`
|
||||
- `web/src/hooks/queries/notification-webhooks.ts`
|
||||
- form components, status pills, test-result display
|
||||
|
||||
Includes:
|
||||
|
||||
- "Show signing secret once" UX with explicit "I've saved it" confirmation.
|
||||
- Show secret-rotation flow.
|
||||
- Last-failure status with admin-level message ("404 Not Found"), not stack traces.
|
||||
- Test button.
|
||||
- Per-reason filter checkboxes.
|
||||
|
||||
### Task 9: Auto-Disable Notification
|
||||
|
||||
When auto-disable fires, create a `notification_deliveries` row with `type = "webhook.auto_disabled"`, including the webhook name and last failure code. The profile sees this in their inbox so a broken webhook doesn't fail silently.
|
||||
|
||||
### Task 10: Optional CDN Proxy For Discord Images
|
||||
|
||||
Files:
|
||||
|
||||
- separate repo (e.g., `silo-discord-cdn-proxy`)
|
||||
- `internal/notifications/webhook_image_signer.go` for issuing short-lived signed URL tokens
|
||||
|
||||
Out of scope for v1 unless Discord image rendering is wanted in the first release. v1 ships embeds without images; v1.5 adds the proxy and image URLs.
|
||||
|
||||
## Validation Plan
|
||||
|
||||
- Unit tests for URL validators (HTTPS, private-IP guard, DNS resolution).
|
||||
- Unit tests for payload builders (Discord shape, generic canonicalization, HMAC determinism).
|
||||
- Integration test: round-trip a generic webhook against a stubbed receiver that verifies the signature.
|
||||
- Integration test: Discord embed against `https://discord.com/api/webhooks/.../test-only` (or recorded fixture) — manual verification only since Discord doesn't offer a sandbox.
|
||||
- Concurrency test: 100 deliveries in parallel; verify retry rows aren't double-counted.
|
||||
- Auto-disable test: simulate 3 consecutive deliveries returning 404 and verify the webhook is disabled with the correct in-app notification; verify 1-2 isolated 4xx blips do **not** disable it; verify persistent timeouts exhaust the 10-attempt schedule before disabling.
|
||||
- Outbox recovery test: commit deliveries with `pending` attempt rows, skip the inline dispatcher, and verify the retry worker sends them after the 60s recovery window.
|
||||
- Manual verification:
|
||||
- Create a Discord webhook URL.
|
||||
- Add it to a profile.
|
||||
- Trigger a release event.
|
||||
- Confirm the embed renders correctly in Discord.
|
||||
- Disable the Discord webhook on Discord's side.
|
||||
- Trigger another event.
|
||||
- Verify the webhook auto-disables after retries.
|
||||
- Verify the profile receives an in-app "webhook auto-disabled" notification.
|
||||
|
||||
## Open Questions
|
||||
|
||||
- **Discord image proxy scope.** Build it in v1, or ship v1 without images and add v1.5? Recommendation: ship without images in v1 — text-only embeds are useful and require no new infra. Add the proxy in v1.5 if user feedback wants images.
|
||||
- **Slack and Microsoft Teams native types.** Should we add `slack` and `teams` types alongside `discord` and `generic`? Both Slack incoming webhooks and Teams Adaptive Cards have their own JSON shapes. Recommendation: `generic` works for Slack (Slack incoming webhooks accept arbitrary JSON; users can build their own message format with a Workflow). Teams requires Adaptive Card JSON specifically. Defer both to v2 unless concrete user demand emerges.
|
||||
- **Per-event-type templating.** Some users will want to customize the embed text. Recommendation: defer to v2; v1 fixed shapes ship faster and most users will accept them.
|
||||
- **Webhook "channels" beyond episode.available.** Should webhook payloads cover other event types (release-aggregated, server announcements)? V1 only fires on `notification.created` for `episode.available`. Other types are forward-compatible via the `event` and `type` fields.
|
||||
- **Outgoing IP.** Webhook deliveries originate from the user's Silo server IP. Do we need an option to route through a proxy? Recommendation: not v1; document that deliveries come from the server's egress IP.
|
||||
- **Backpressure.** What happens when a profile's webhook is slow but reachable, blocking delivery for an hour at a time? The 10s timeout caps individual deliveries; bounded worker pool caps fanout concurrency. A single bad webhook shouldn't degrade other webhooks because each delivery runs independently.
|
||||
@@ -0,0 +1,54 @@
|
||||
# Web Push Spec
|
||||
|
||||
**Date:** 2026-06-11
|
||||
**Status:** Implemented
|
||||
**Scope:** Browser push notifications (Push API + VAPID) as a third push platform alongside the deferred APNs/FCM channels.
|
||||
**Depends On:**
|
||||
- [`00-architecture-overview.md`](./00-architecture-overview.md)
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)
|
||||
|
||||
## Why Web Push ships before APNs/FCM
|
||||
|
||||
The architecture overview deferred mobile push because Apple and Google require pushes to official store builds to be signed by the publisher's credentials, forcing a Silo-operated relay. Web Push has neither problem:
|
||||
|
||||
- **No accounts, no relay.** The server self-provisions a VAPID keypair on first use. Any standards-compliant browser push service (Chrome, Firefox, Edge, Safari 16+) accepts VAPID-signed requests from any origin.
|
||||
- **Content-safe by protocol.** Payloads are encrypted end-to-end (RFC 8291, `aes128gcm`) to keys held only by the subscribed browser. The vendor push service relays ciphertext. Unlike the APNs/FCM design, payloads can therefore carry full display content (titles, episode numbers, poster URLs) without violating the self-hosted privacy model — there is no opaque-wake/fetch dance.
|
||||
|
||||
The residual leak matches the relay threat model: the push service sees the user server's egress IP, delivery timing, and payload size. It never sees content or identity.
|
||||
|
||||
## Data model
|
||||
|
||||
- `web_push_subscriptions` — profile-scoped browser registrations: `endpoint` (unique; a resubscription from the same browser under a different profile reassigns the row), `p256dh`, `auth`, `device_name`, failure bookkeeping. No FK to profiles (per-user SQLite stores); profile deletion purges in code.
|
||||
- `web_push_delivery_attempts` — the durable dispatch outbox, mirroring `webhook_delivery_attempts`: `pending` rows enqueued in the fanout transaction, claimed post-commit with a lease, swept by the retry loop after a crash.
|
||||
|
||||
## VAPID identity
|
||||
|
||||
Generated once and persisted in `server_settings`:
|
||||
|
||||
- `notifications.web_push.vapid_public_key` — served to clients via the capability endpoint.
|
||||
- `notifications.web_push.vapid_private_key` — encrypted at rest (`SensitiveSettingKeys`).
|
||||
|
||||
The private key is persisted before the public key so a crash between writes regenerates the pair instead of stranding clients with an unusable public key. The pair must never be rotated casually: browsers bind subscriptions to it.
|
||||
|
||||
## API surface (profile-scoped)
|
||||
|
||||
- `GET /api/v1/notifications/capability` — `web_push: { available, public_key }`.
|
||||
- `POST /api/v1/notifications/web-push/subscriptions` — body is `PushSubscription.toJSON()` plus `device_name`.
|
||||
- `GET /api/v1/notifications/web-push/subscriptions` — for the settings UI device list.
|
||||
- `DELETE /api/v1/notifications/web-push/subscriptions/{id}`
|
||||
- `POST /api/v1/notifications/web-push/unsubscribe` — by endpoint (browsers don't know row IDs).
|
||||
|
||||
Subscription endpoints are attacker-controllable URLs the server will POST to, so they pass the same HTTPS + private-destination guard as webhooks, both at registration and at connect time (guarded dialer).
|
||||
|
||||
## Delivery semantics
|
||||
|
||||
- Fanout enqueues one `pending` attempt per enabled subscription of each recipient profile, in the same transaction as the delivery rows. No per-reason filters: profile preferences already gate delivery creation.
|
||||
- Retry schedule is short (30s/2m/10m/30m, 5 attempts): vendor push services queue messages for offline devices themselves (TTL 12h), so server-side retries only ride out transient push-service errors.
|
||||
- `404`/`410` from the push service is the protocol's unsubscribe signal: the subscription row is deleted, not retried.
|
||||
- `notifications.web_push_enabled` is the kill switch (default on).
|
||||
|
||||
## Client
|
||||
|
||||
- `web/public/sw.js` — displays notifications and routes clicks (episode deep link, or the inbox).
|
||||
- `web/src/lib/webPush.ts` — permission + subscribe/unsubscribe flows.
|
||||
- Settings → Notifications → "Browser Notifications" — this-browser toggle plus a revocable list of the profile's other subscribed devices.
|
||||
@@ -0,0 +1,169 @@
|
||||
# Notifications v1.5 Roadmap
|
||||
|
||||
**Date:** 2026-06-11
|
||||
**Status:** Draft (work not started)
|
||||
**Scope:** The remaining notification work between the shipped v1 and the deferred v2 push channels. Each item is independent and sized to land as its own PR.
|
||||
**Depends On:**
|
||||
- [`00-architecture-overview.md`](./00-architecture-overview.md)
|
||||
- [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md) — implemented
|
||||
- [`04-outbound-webhooks.md`](./04-outbound-webhooks.md) — implemented
|
||||
- [`05-web-push.md`](./05-web-push.md) — implemented
|
||||
|
||||
## Where v1 landed (context for this doc)
|
||||
|
||||
Implemented 2026-06-11: the full foundation (availability seeding, release events,
|
||||
interest index, fanout worker with burst caps, websocket channel with ticket
|
||||
handshake, inbox/sync/preferences/capability APIs, web inbox + badge + settings),
|
||||
outbound webhooks (Discord + generic HMAC, SSRF guard, durable outbox, retry +
|
||||
auto-disable), Web Push (VAPID self-provisioned, E2E-encrypted payloads, service
|
||||
worker), and the shared SMTP core (`internal/mail`, see
|
||||
`docs/architecture/email.md`) with an admin Email settings page — but no feature
|
||||
consuming email yet.
|
||||
|
||||
**Deferred to v2 by explicit decision:** APNs (`02`) and FCM (`03`) — they
|
||||
require Silo-operated relay infrastructure and developer accounts. Also v2 per
|
||||
the original plans: movie availability, aggregated notifications ("3 new
|
||||
episodes"), quiet hours, cross-profile views.
|
||||
|
||||
---
|
||||
|
||||
## 1. Admin settings UI for notification controls
|
||||
|
||||
**Why:** every `notifications.*` setting works today but is reachable only
|
||||
through the raw admin settings API. Admins should not need `curl` to find the
|
||||
kill switches.
|
||||
|
||||
**What:** an admin settings page ("Notifications", next to the Email page added
|
||||
in v1) exposing:
|
||||
|
||||
| Group | Keys |
|
||||
|---|---|
|
||||
| Kill switches | `notifications.release_events_enabled`, `notifications.fanout_enabled`, `notifications.ui_enabled`, `notifications.webhooks_enabled`, `notifications.web_push_enabled` |
|
||||
| Fanout tuning | `notifications.fanout.settle_seconds` (default 30), `notifications.fanout.max_series_burst` (default 3) |
|
||||
| Webhook guards | `notifications.webhooks.max_per_profile` (10), `notifications.webhooks.allow_private_destinations` (false; dev only — label it loudly), `notifications.webhooks.deliveries_per_minute_per_profile` (60) |
|
||||
| Retention | `notifications.retention.read_days` (90), `notifications.retention.unread_days` (180), `notifications.retention.event_days` (30) |
|
||||
|
||||
**Files:** add `web/src/pages/admin-settings/NotificationsAdminSettings.tsx`
|
||||
(follow `EmailSettings.tsx` / `useSettingsForm`), register in
|
||||
`web/src/pages/admin-settings/AdminSettingsLayout.tsx`. No backend work — all
|
||||
keys are live-read.
|
||||
|
||||
**Effort:** small (one page, no migrations, no Go changes).
|
||||
|
||||
---
|
||||
|
||||
## 2. Request-fulfilled notifications (`request.fulfilled`)
|
||||
|
||||
**Why:** `00-architecture-overview.md` calls this "the most obvious next
|
||||
notification type." Users who request media currently learn it arrived by
|
||||
checking manually; every delivery channel they configured should tell them.
|
||||
|
||||
**Design:** the `notification_deliveries.type` registry is extensible by
|
||||
construction — no schema change.
|
||||
|
||||
- New type `request.fulfilled`. `reason_flags` carries the operational shape
|
||||
(like `webhook.auto_disabled` does), e.g.
|
||||
`{"request_id": "...", "tmdb_id": 123, "media_type": "movie"}` — never the
|
||||
four reason booleans.
|
||||
- **Hook point:** the request reconciliation service (`internal/mediarequests`)
|
||||
is where a request transitions to available/fulfilled. On that transition,
|
||||
insert a delivery via `DeliveryRepository.InsertOperational` (the path the
|
||||
webhook auto-disable notice already uses) and publish through the system's
|
||||
dispatchers so websocket, web push, and webhooks all fire.
|
||||
- **Recipient:** the requesting profile (requests are profile-attributed). No
|
||||
`profile_series_interest` involvement — this is a direct, not fanned-out,
|
||||
notification.
|
||||
- **Webhook enqueue:** operational inserts bypass the fanout outbox, so either
|
||||
(a) extend `InsertOperational` to optionally enqueue per-target attempt rows,
|
||||
or (b) add a small shared "dispatch one delivery durably" helper used by both
|
||||
this and the auto-disable notice. Prefer (b); the auto-disable notice
|
||||
deliberately skips webhooks (loop guard) but request notices should not.
|
||||
- **Per-reason preferences:** add nothing in v1.5. The profile master toggle
|
||||
(`notification_preferences.enabled`) gates it; a dedicated
|
||||
`notify_requests` flag can come later if users ask.
|
||||
- **Clients:** the web inbox/toast/web-push renderers fall back to a generic
|
||||
card for unknown types; add a `request.fulfilled` case with the media title,
|
||||
poster, and a deep link to the item (or the request page until matched).
|
||||
|
||||
**Effort:** medium-small. The delivery/dispatch machinery all exists.
|
||||
|
||||
---
|
||||
|
||||
## 3. Email digest channel
|
||||
|
||||
**Why:** first real consumer of `internal/mail`; reaches users who don't keep a
|
||||
browser open and have no webhook.
|
||||
|
||||
**Open design decisions (resolve before building):**
|
||||
|
||||
- **Account-level, not profile-level.** Email addresses live on `users`;
|
||||
profiles have none. A digest therefore aggregates across the account's
|
||||
profiles (group by profile inside the email body).
|
||||
- **Digest, not per-episode.** Per-episode email is spam at hundreds-of-users
|
||||
scale and duplicates the realtime channels. Recommend: opt-in daily digest of
|
||||
unread deliveries, sent by a taskmanager task (reuse the checkpointed
|
||||
iteration pattern from the interest backfill), with a per-user
|
||||
enable + cadence setting.
|
||||
- **Unsubscribe / preference surface:** account settings, not profile
|
||||
notification preferences.
|
||||
|
||||
**Files (sketch):** `internal/notifications/email_digest.go` (compose from
|
||||
`DeliveryRepository`, send via `mail.Sender`, branch on
|
||||
`mail.ErrNotConfigured`), a `taskmanager` task, a small user-settings surface.
|
||||
|
||||
**Effort:** medium. Blocked on the design decisions above, not on plumbing.
|
||||
|
||||
---
|
||||
|
||||
## 4. Native client adoption (no push required)
|
||||
|
||||
**Why:** the Android and Apple apps gain a full notification experience today —
|
||||
APNs/FCM only add closed-app wake-ups later.
|
||||
|
||||
Server surfaces ready for clients (`silo-android`, `silo-apple`):
|
||||
|
||||
- `GET /api/v1/notifications` + `unread-count` + read endpoints — inbox UI.
|
||||
- `GET /api/v1/notifications/sync` — opaque forward cursor for
|
||||
reconnect/foreground catch-up (this is also the wake-fetch endpoint the v2
|
||||
push specs assume, so client work done now is reused).
|
||||
- `POST /api/v1/events/ws-ticket` + `ticket` query param on `/api/v1/events/ws`,
|
||||
`notifications` channel — realtime while the app is open. Snapshot on
|
||||
subscribe hydrates recent unread.
|
||||
- `GET /api/v1/notifications/capability` — drive setup UI from this, never from
|
||||
admin settings.
|
||||
- `GET/PUT /api/v1/notifications/preferences` — per-profile reason toggles.
|
||||
|
||||
**Effort:** client-repo work; the server side is done. Coordinate per the
|
||||
multi-repo guidance in the repo root `CLAUDE.md`.
|
||||
|
||||
---
|
||||
|
||||
## 5. Hardening backlog (defer freely)
|
||||
|
||||
- **DB-backed integration tests** from the `01` verification plan: idempotent
|
||||
availability/event inserts, cross-library delivery dedupe, per-series burst
|
||||
cap, outbox recovery (pending rows with no dispatch → retry worker sends),
|
||||
multi-node claim safety. The behaviors shipped and were exercised manually on
|
||||
dev; they are not yet pinned by automated tests because the repo has no
|
||||
Postgres test harness for this package.
|
||||
- **Metrics:** `01` names Prometheus-style counters
|
||||
(`release_events_suppressed_total`, etc.); v1 ships them as structured log
|
||||
fields. Revisit when the repo grows a metrics registry — keep the names.
|
||||
- **Discord embed images** via the `media.discord-cdn-proxy` service
|
||||
(see `04`, "v1.5 payload"). Requires a new Silo-operated repo/service plus
|
||||
`webhook_image_signer.go`; v1 deliberately ships text-only embeds so the
|
||||
user's server origin never reaches Discord.
|
||||
- **Webhook delivery history endpoint:** `webhook_delivery_attempts` already
|
||||
has the listing index; a `GET /api/v1/notifications/webhooks/{id}/attempts`
|
||||
endpoint + UI table would make failures self-diagnosable beyond the
|
||||
last-failure summary.
|
||||
|
||||
---
|
||||
|
||||
## Suggested order
|
||||
|
||||
1. Admin settings UI (#1) — smallest, completes operability.
|
||||
2. Request-fulfilled (#2) — highest product value per effort.
|
||||
3. Webhook history endpoint (#5, last bullet) — pairs naturally with #1.
|
||||
4. Email digest (#3) — after its design decisions are made.
|
||||
5. Native clients (#4) — parallel track in the client repos.
|
||||
@@ -0,0 +1,109 @@
|
||||
# Notifications: Email Channel
|
||||
|
||||
**Date:** 2026-06-11
|
||||
**Status:** Implemented (written post-implementation)
|
||||
**Scope:** Item 3 of [`06-v1.5-roadmap.md`](./06-v1.5-roadmap.md) — the first real consumer of the shared SMTP core (`internal/mail`, `docs/architecture/email.md`).
|
||||
**Depends On:** [`00-architecture-overview.md`](./00-architecture-overview.md), [`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)
|
||||
|
||||
## Decisions (resolving the roadmap's open questions)
|
||||
|
||||
- **Account-level, as planned.** Email addresses live on `users`; one mode
|
||||
covers every profile on the account and one email aggregates across them.
|
||||
- **Per-episode AND digest, not digest-only.** The roadmap recommended
|
||||
digest-only; product direction chose to offer per-episode alerts too, gated
|
||||
by an admin allowance (`notifications.email.allow_per_episode`). When the
|
||||
admin disallows it, accounts set to per-episode are **coerced to the daily
|
||||
digest** rather than silenced.
|
||||
- **Interest-scoped by construction.** Email consumes existing
|
||||
`notification_deliveries` rows, which the fanout only creates for profiles
|
||||
with series interest (favorites, watchlist, continue-watching, next-up) and
|
||||
for direct notices (`request.fulfilled`, `webhook.auto_disabled`). The
|
||||
channel adds no targeting of its own — it is never "all new content".
|
||||
- **Opt-in, default off.** Users enable it per account in Settings →
|
||||
Notifications; enabling initializes the watermark to now so history never
|
||||
floods a fresh opt-in.
|
||||
|
||||
## Architecture: watermark sweep, not a third outbox
|
||||
|
||||
Webhooks and web push use per-target outbox attempt rows. Email deliberately
|
||||
does not:
|
||||
|
||||
- Deliveries already carry `user_id`, and an account whose profiles follow the
|
||||
same series gets one row per profile — a per-row outbox would email the same
|
||||
episode several times. The sweep collapses them (dedupe by `episode_id`, by
|
||||
`request_id` for requests).
|
||||
- A per-account watermark over `(created_at, id)` that advances **only after a
|
||||
successful SMTP send** gives durability for free: a crash or SMTP outage
|
||||
re-sends on the next pass instead of dropping.
|
||||
- Both cadences are the same mechanism: per-episode sweeps every minute (and
|
||||
is nudged by the dispatcher seconds after fanout commits); the digest is the
|
||||
same sweep gated on "today's send hour passed and not yet stamped today".
|
||||
|
||||
State lives in `notification_email_prefs` (`migrations/sql/`,
|
||||
`email_notification_channel`): mode, watermark, `last_digest_at`, and failure
|
||||
backoff counters (`last_attempt_at`, `consecutive_failures`; 1m doubling,
|
||||
capped at 6h). No FK to `users` per the notification-tables rule; deleted or
|
||||
disabled accounts drop out of the recipient join. A supporting index
|
||||
`notification_deliveries_user_created_idx (user_id, created_at, id)` serves
|
||||
the sweep.
|
||||
|
||||
**Multi-node safety:** each account is processed inside one transaction that
|
||||
claims the prefs row `FOR UPDATE SKIP LOCKED`, re-derives eligibility from the
|
||||
locked row (mode flips and another node's digest stamp are both re-checked),
|
||||
sends, then commits the watermark/stamp. Failed sends commit only the backoff
|
||||
counters. `mail.ErrNotConfigured` aborts the whole pass; three consecutive
|
||||
send failures end it early (SMTP trouble is global, not per-recipient).
|
||||
|
||||
**Flood bounds:** one email renders at most 30 lines (`…and N more in your
|
||||
Silo inbox`), one pass fetches at most 200 rows per account, and upstream the
|
||||
per-series burst cap already limits fanout volume. Digest emails include only
|
||||
rows still unread at compose time; the watermark passes read rows silently.
|
||||
|
||||
## Files
|
||||
|
||||
| Piece | Location |
|
||||
|---|---|
|
||||
| Modes, prefs repo (`notification_email_prefs`) | `internal/notifications/email_prefs_repo.go` |
|
||||
| Worker, dispatcher nudge, System service methods | `internal/notifications/email_digest.go` |
|
||||
| Subject/text/HTML rendering | `internal/notifications/email_compose.go` |
|
||||
| Account sweep query | `DeliveryRepository.ListForUserSince` (`internal/notifications/delivery_repo.go`) |
|
||||
| Settings accessors | `internal/notifications/settings.go` |
|
||||
| API handlers | `internal/api/handlers/notifications_email.go` (+ capability in `notifications.go`) |
|
||||
| Web UI (user) | `EmailSection` in `web/src/pages/settings/NotificationsSettings.tsx` |
|
||||
| Web UI (admin) | Email group in `web/src/pages/admin-settings/NotificationsAdminSettings.tsx` |
|
||||
| Logic tests | `internal/notifications/email_logic_test.go` |
|
||||
|
||||
The worker is wired in `notifications.NewSystem` (new `mail.Sender` parameter,
|
||||
passed from `cmd/silo/main.go`); its dispatcher joins the `MultiDispatcher`,
|
||||
so operational deliveries (`request.fulfilled`, `webhook.auto_disabled`) nudge
|
||||
it exactly like fanout rows do.
|
||||
|
||||
## Settings
|
||||
|
||||
| Key | Default | Meaning |
|
||||
|---|---|---|
|
||||
| `notifications.email_enabled` | `true` | Channel kill switch (availability still requires SMTP configured via `email.*`) |
|
||||
| `notifications.email.allow_per_episode` | `true` | Admin allowance for the per-episode cadence |
|
||||
| `notifications.email.digest_hour` | `8` | Hour (0–23, server-local) daily digests go out |
|
||||
| `notifications.email.external_url` | empty | Public base URL for deep links in emails; empty sends link-free emails (the server origin is never leaked implicitly) |
|
||||
|
||||
## API
|
||||
|
||||
- `GET /api/v1/notifications/email-preferences` → `{"mode": "off" | "per_episode" | "daily_digest"}`
|
||||
- `PUT /api/v1/notifications/email-preferences` `{"mode": ...}` — 400 codes:
|
||||
`bad_request` (unknown mode), `not_allowed` (per-episode disallowed),
|
||||
`no_email` (account has no address). Any profile on the account may set it.
|
||||
- `GET /api/v1/notifications/capability` gained
|
||||
`"email": {"available", "modes", "digest_hour"}`; clients gate setup UI on
|
||||
it as usual. `available` requires the kill switch on **and**
|
||||
`mail.Sender.Enabled()` — never read `email.*` settings directly.
|
||||
|
||||
## Deliberately not in v1
|
||||
|
||||
- Posters/images in emails (would require externally reachable presigned URLs).
|
||||
- `List-Unsubscribe` headers / tokenized unsubscribe endpoint (self-hosted,
|
||||
opt-in; revisit if servers grow beyond household scale).
|
||||
- Per-user digest hour (admin-global for now).
|
||||
- DB-backed integration tests for the sweep (same Postgres-harness gap as the
|
||||
rest of `01`'s verification backlog; pure logic is covered by
|
||||
`email_logic_test.go`).
|
||||
@@ -0,0 +1,88 @@
|
||||
# Silo Notifications — Design Index
|
||||
|
||||
> **Provenance:** Imported 2026-06-11 from `ContinuumApp/continuum` (`docs/superpowers/plans/notifications/` at `04aa9266`), written 2026-04-28 before the project was renamed to Silo. Reviewed and amended 2026-06-11: product naming and all wire contracts (payload keys, headers, settings enums, localization keys, provisional domains/repo names) are normalized to Silo. Bundle topics, Android package names, and Firebase project IDs intentionally keep their current official client-build values (`com.continuum.app.*`, `continuum-prod-android`) — they are allowlist *config*, not part of this design's contract, and the shipped client builds still use them. Relay/proxy hostnames (`relay.silo.app`, `media.discord-cdn-proxy.silo.app`) are provisional. See "2026-06-11 review amendments" below for the design-level changes made during review.
|
||||
|
||||
This folder collects the design work for Silo's notification system, covering durable in-app inbox + websocket realtime, Apple Push (APNs), Android Push (FCM), and outbound webhooks (Discord-native + generic).
|
||||
|
||||
A self-contained visual summary of the design decisions (who/what/where/why per decision, pipeline, trust model) lives at [`design-decisions.html`](./design-decisions.html) — open it in any browser; no build or network access required.
|
||||
|
||||
## Reading order
|
||||
|
||||
1. **[`00-architecture-overview.md`](./00-architecture-overview.md)** — Cross-cutting overview. Read this first. Explains the channel model, fanout pipeline, preference shape, threat model, and addressing model (why a relay is required for mobile push). Links into the per-channel specs.
|
||||
2. **[`01-release-events-and-inbox.md`](./01-release-events-and-inbox.md)** — Foundation. Durable `release_events` -> per-profile `notification_deliveries` fanout, the in-app inbox API, the websocket channel, and per-profile preferences. Vendor-agnostic; nothing else works without this.
|
||||
3. **[`02-apns-relay.md`](./02-apns-relay.md)** — Apple push. Privacy-preserving design with three provider modes (`off` / `silo_relay` / `custom_apns`).
|
||||
4. **[`03-fcm-relay.md`](./03-fcm-relay.md)** — Android push. Mirrors the APNs spec with `off` / `silo_relay` / `custom_fcm` modes using FCM v1 (OAuth2 service-account JSON).
|
||||
5. **[`04-outbound-webhooks.md`](./04-outbound-webhooks.md)** — Profile-scoped outbound webhooks. Native Discord embed type and generic JSON+HMAC type. Content included by default because the profile chose the destination.
|
||||
|
||||
## Status
|
||||
|
||||
| Doc | Status |
|
||||
|---|---|
|
||||
| 00-architecture-overview | Draft (amended 2026-06-11) |
|
||||
| 01-release-events-and-inbox | Draft (refined, amended 2026-06-11) |
|
||||
| 02-apns-relay | Draft (refined, amended 2026-06-11) |
|
||||
| 03-fcm-relay | Draft (amended 2026-06-11) |
|
||||
| 04-outbound-webhooks | Draft (amended 2026-06-11) |
|
||||
|
||||
Nothing in this folder has been implemented. The codebase has zero notification-related code beyond the existing realtime events hub (`internal/events/`, with the operational publisher wrapper at `internal/notifications/hub.go`), which publishes catalog/jobs events and is unrelated to the user-facing notification system designed here.
|
||||
|
||||
## 2026-06-11 review amendments
|
||||
|
||||
A scaling and privacy review (self-hosted servers with hundreds of users) resolved four design-level gaps. The detailed designs live in the per-doc sections; this is the index:
|
||||
|
||||
1. **Back-catalog seeding + burst suppression** (`01`). First scan of a new library and the feature-enable backfill seed `episode_availability` *without* creating release events; bulk additions to existing libraries are bounded by a per-series fanout burst cap. Without this, importing a 200-episode back-catalog of a popular series would generate tens of thousands of deliveries and pushes in one scan.
|
||||
2. **Durable dispatch outbox** (`01`, referenced by `02`/`03`/`04`). The fanout transaction that inserts `notification_deliveries` also inserts `pending` per-target attempt rows for the push and webhook channels. A crash between delivery commit and dispatch no longer silently loses pushes/webhooks; recovery workers drain stale pending rows.
|
||||
3. **Cross-library episode dedupe** (`01`). Media items in Silo are catalog-level (`media_item_libraries` junction), so the same episode landing in "TV" and "TV 4K" shares one `episode_id`. A partial unique index on `(profile_id, episode_id)` guarantees at most one `episode.available` delivery per profile per episode across libraries.
|
||||
4. **Mobile wake-fetch endpoints defined in the foundation** (`01`). `GET /api/v1/notifications/sync` (forward cursor) and `GET /api/v1/notifications/{id}` — previously referenced by `02`/`03` but defined nowhere.
|
||||
|
||||
Smaller amendments: relay threat models now list the server's egress IP as residual leakage (`00`/`02`/`03`); `collapse_id`/`collapse_key` derivation is specified as per-server-keyed HMAC so the relay can't read series identity out of it (`02`/`03`); the websocket handshake uses a short-lived single-use ticket instead of a long-lived profile token in the query string (`00`/`01`); client-side relay pacing (`02`/`03`); webhook auto-disable requires 3 consecutive non-retryable 4xx instead of 1 (`04`); SSRF deny list gains `198.18.0.0/15` and `192.88.99.0/24` (`04`); interest recompute fires on watch-state transitions, not every progress tick (`01`); profile-deletion cascades and `release_events.dedupe_key` composition are specified (`01`); migration tasks now follow the repo's timestamped Goose convention (`01`).
|
||||
|
||||
## Scope summary
|
||||
|
||||
**In scope (v1):**
|
||||
|
||||
- Episode-availability notifications (a new episode lands in a library and is relevant to a profile via favorites / watchlist / continue-watching / next-up).
|
||||
- Durable per-profile inbox with read/unread state.
|
||||
- Realtime websocket delivery for connected clients.
|
||||
- Apple Push via opt-in Silo-operated relay or admin-supplied custom APNs credentials.
|
||||
- Android Push via opt-in Silo-operated relay or admin-supplied custom FCM credentials.
|
||||
- Outbound webhooks per profile, with native Discord embed rendering and generic JSON+HMAC.
|
||||
|
||||
**Out of scope (v1):**
|
||||
|
||||
- Movie release notifications (the trigger surface — favoriting a movie that doesn't yet exist — isn't a flow Silo supports).
|
||||
- Email, SMS, or other delivery channels.
|
||||
- Browser Web Push (PWA) notifications.
|
||||
- Cross-profile administrator dashboards.
|
||||
- Marketing or promotional notifications.
|
||||
- Third-party ingest events (Plex/Sonarr/Radarr) as notification sources.
|
||||
|
||||
## Cross-cutting decisions
|
||||
|
||||
These hold across all channels and were resolved during spec review:
|
||||
|
||||
- **Episode-only triggers in v1.** Confirmed: there's no UX path for a user to favorite/watchlist a movie before it exists in their library.
|
||||
- **Per-profile webhooks include content by default.** The profile chose the destination; trust is implicit. Discord-native embeds show series and episode details; generic webhooks include the same.
|
||||
- **Mobile push payloads stay opaque by default.** APNs and FCM relay paths never see notification content. The app fetches metadata from the user's own server after wake. No badge counts in v1 (deferred to user-opt-in in v2).
|
||||
- **Preference shape is flat:** per-profile reason flags (favorites / watchlist / continue-watching / next-up) plus a master enable toggle on each push device and each webhook. Not a per-channel × per-reason matrix.
|
||||
- **Per-platform APNs topics.** iOS, tvOS, and macOS each get their own bundle topic. Single-build collapse is a v2 concern.
|
||||
- **Stateless relay v1.** No stored token aliases. Add only if abuse/rate-limit pressure justifies it.
|
||||
- **Relay services live in their own repos.** This folder defines the contracts the user-server side implements; the actual relay deployment (APNs / FCM) is a separate operational repo.
|
||||
- **Back-catalog never floods.** Initial library scans and the feature-enable backfill seed availability silently; per-series burst caps bound fanout for bulk additions (see `01`).
|
||||
- **One delivery per episode per profile, across libraries.** Dual-quality library setups (e.g., "TV" + "TV 4K") do not double-notify.
|
||||
- **Dispatch enqueue is durable (outbox).** Push and webhook sends survive a crash between delivery commit and dispatch; the websocket channel stays best-effort because the inbox snapshot covers reconnect.
|
||||
|
||||
## Phasing
|
||||
|
||||
Implementation should land in this order, gated by feature flags so partial deploys are safe:
|
||||
|
||||
1. Schema (Phase 1 of `01-release-events-and-inbox.md`).
|
||||
2. `profile_series_interest` updaters and backfill task (no fanout yet).
|
||||
3. Availability seeding backfill, then release event creation (still no fanout). Seeding **must** complete before release events are enabled, or the first scan after enablement emits an event per back-catalog episode.
|
||||
4. In-app fanout worker, websocket channel, inbox APIs.
|
||||
5. Frontend inbox + badge + preference UI.
|
||||
6. Outbound webhooks (no new infra; can ship before mobile push).
|
||||
7. APNs relay + custom APNs.
|
||||
8. FCM relay + custom FCM.
|
||||
|
||||
The relay services (steps 7-8) require Silo to provision Apple Developer + Firebase developer accounts and host the relay services. Steps 1-6 require no new external infrastructure.
|
||||
@@ -0,0 +1,281 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Silo notifications — design decisions</title>
|
||||
<style>
|
||||
:root {
|
||||
color-scheme: light dark;
|
||||
--background: #f4f4f6;
|
||||
--card: #ffffff;
|
||||
--border: #d8d8de;
|
||||
--fg: #1a1a1e;
|
||||
--fg-2: #3a3a42;
|
||||
--muted: #63636b;
|
||||
--chip: #e8e8ec;
|
||||
--green: #22c55e;
|
||||
--purple: #a855f7;
|
||||
--amber: #f59e0b;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: #141417;
|
||||
--card: #1c1c20;
|
||||
--border: #28282e;
|
||||
--fg: #e8e8ec;
|
||||
--fg-2: #d0d0d6;
|
||||
--muted: #9696a0;
|
||||
--chip: #232328;
|
||||
--green: #81c995;
|
||||
--purple: #c78dbd;
|
||||
--amber: #e8a87c;
|
||||
}
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
background: var(--background);
|
||||
color: var(--fg);
|
||||
font-family: "Outfit", ui-sans-serif, system-ui, "Helvetica Neue", sans-serif;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
}
|
||||
main { max-width: 880px; margin: 0 auto; padding: 52px 28px 48px; }
|
||||
h1 { font-size: 24px; font-weight: 600; margin: 0 0 6px; letter-spacing: -0.01em; }
|
||||
.sub { color: var(--muted); font-size: 14px; margin: 0 0 8px; }
|
||||
.sub code { font-size: 12.5px; }
|
||||
h2 {
|
||||
font-size: 17px; font-weight: 600; margin: 44px 0 14px;
|
||||
padding-bottom: 8px; border-bottom: 1px solid var(--border);
|
||||
}
|
||||
p { color: var(--fg-2); margin: 0 0 12px; max-width: 72ch; }
|
||||
code {
|
||||
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
|
||||
font-size: 12.5px; background: var(--chip); border-radius: 4px; padding: 1px 5px;
|
||||
}
|
||||
.legend { display: flex; flex-wrap: wrap; gap: 8px 22px; margin: 10px 0 0; font-size: 13px; color: var(--muted); }
|
||||
.legend span { display: inline-flex; align-items: center; gap: 7px; }
|
||||
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
|
||||
table { width: 100%; border-collapse: collapse; margin: 4px 0 0; }
|
||||
th { text-align: left; font-size: 13px; font-weight: 600; color: var(--fg); padding: 8px 14px 8px 0; border-bottom: 1px solid var(--border); }
|
||||
td { font-size: 13.5px; color: var(--fg-2); padding: 9px 14px 9px 0; border-bottom: 1px solid var(--border); vertical-align: top; }
|
||||
td:first-child, th:first-child { white-space: nowrap; }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 14px; }
|
||||
.card {
|
||||
background: var(--card); border: 1px solid var(--border);
|
||||
border-radius: 12px; padding: 16px 18px;
|
||||
}
|
||||
.card h3 { font-size: 14.5px; font-weight: 600; margin: 0 0 10px; }
|
||||
.kv { display: grid; grid-template-columns: 54px 1fr; row-gap: 7px; column-gap: 10px; }
|
||||
.kv dt { font-size: 13px; font-weight: 500; color: var(--muted); margin: 0; }
|
||||
.kv dd { font-size: 13.5px; color: var(--fg-2); margin: 0; line-height: 1.55; }
|
||||
.svgwrap { border: 1px solid var(--border); border-radius: 12px; background: var(--card); padding: 18px 14px 10px; }
|
||||
svg text { font-family: "Outfit", ui-sans-serif, system-ui, sans-serif; }
|
||||
.bt { font-size: 13px; font-weight: 600; fill: var(--fg); }
|
||||
.bm { font-size: 12.5px; font-weight: 600; fill: var(--fg); font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
|
||||
.bs { font-size: 11.5px; fill: var(--muted); }
|
||||
.bx { fill: var(--card); stroke: var(--border); }
|
||||
ul.docs { padding-left: 20px; margin: 0; color: var(--fg-2); }
|
||||
ul.docs li { margin: 4px 0; }
|
||||
footer { margin-top: 40px; font-size: 13px; color: var(--muted); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
|
||||
<h1>Silo notifications — design decisions</h1>
|
||||
<p class="sub">Episode-availability notifications for a self-hosted, privacy-focused media server, designed to hold up with hundreds of users on one install.</p>
|
||||
<p class="sub">Companion to the specs in <code>docs/superpowers/plans/notifications/</code> · drafts amended 2026-06-11</p>
|
||||
|
||||
<h2>How a notification flows</h2>
|
||||
<p>Ingest records availability, a worker fans out to interested profiles, and a durable inbox row is written before any channel fires. Every channel below the row is a transport, never a source of truth.</p>
|
||||
<div class="svgwrap">
|
||||
<svg width="100%" viewBox="0 0 760 452" role="img" aria-label="Pipeline from library scan to release events to the fanout worker, into a durable notification_deliveries row, which dispatches to websocket, Apple push, Android push, and webhook channels after commit. The push channels continue through the opt-in Silo push relay, which sees only a device token and opaque IDs, then through APNs or FCM, and the woken device fetches notification details from the user's own server. A dashed path shows custom credentials skipping the relay.">
|
||||
<defs>
|
||||
<marker id="a" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
|
||||
<path d="M2 1L8 5L2 9" fill="none" stroke="var(--muted)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect class="bx" x="20" y="24" width="190" height="52" rx="8"/>
|
||||
<text class="bt" x="115" y="45" text-anchor="middle">Scan and ingest</text>
|
||||
<text class="bs" x="115" y="63" text-anchor="middle">new episode lands</text>
|
||||
<rect class="bx" x="280" y="24" width="190" height="52" rx="8"/>
|
||||
<text class="bm" x="375" y="45" text-anchor="middle">release_events</text>
|
||||
<text class="bs" x="375" y="63" text-anchor="middle">one row per episode</text>
|
||||
<rect class="bx" x="540" y="24" width="190" height="52" rx="8"/>
|
||||
<text class="bt" x="635" y="45" text-anchor="middle">Fanout worker</text>
|
||||
<text class="bs" x="635" y="63" text-anchor="middle">burst caps + eligibility</text>
|
||||
<line x1="214" y1="50" x2="274" y2="50" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<line x1="474" y1="50" x2="534" y2="50" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<path d="M635 76 L635 98 L380 98 L380 114" fill="none" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<rect class="bx" x="250" y="120" width="260" height="52" rx="8"/>
|
||||
<text class="bm" x="380" y="141" text-anchor="middle">notification_deliveries</text>
|
||||
<text class="bs" x="380" y="159" text-anchor="middle">durable row, dispatched after commit</text>
|
||||
<line x1="380" y1="174" x2="97" y2="206" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<line x1="380" y1="174" x2="287" y2="206" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<line x1="380" y1="174" x2="473" y2="206" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<line x1="380" y1="174" x2="663" y2="206" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<rect class="bx" x="10" y="212" width="170" height="52" rx="8" stroke="var(--green)"/>
|
||||
<text class="bt" x="95" y="233" text-anchor="middle">Websocket</text>
|
||||
<text class="bs" x="95" y="251" text-anchor="middle">full content</text>
|
||||
<rect class="bx" x="200" y="212" width="170" height="52" rx="8" stroke="var(--purple)"/>
|
||||
<text class="bt" x="285" y="233" text-anchor="middle">Apple push</text>
|
||||
<text class="bs" x="285" y="251" text-anchor="middle">opaque wake</text>
|
||||
<rect class="bx" x="390" y="212" width="170" height="52" rx="8" stroke="var(--purple)"/>
|
||||
<text class="bt" x="475" y="233" text-anchor="middle">Android push</text>
|
||||
<text class="bs" x="475" y="251" text-anchor="middle">opaque wake</text>
|
||||
<rect class="bx" x="580" y="212" width="170" height="52" rx="8" stroke="var(--amber)"/>
|
||||
<text class="bt" x="665" y="233" text-anchor="middle">Webhooks</text>
|
||||
<text class="bs" x="665" y="251" text-anchor="middle">your chosen URL</text>
|
||||
<line x1="285" y1="268" x2="345" y2="292" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<line x1="475" y1="268" x2="415" y2="292" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<rect class="bx" x="290" y="296" width="180" height="52" rx="8" stroke="var(--purple)"/>
|
||||
<text class="bt" x="380" y="317" text-anchor="middle">Silo push relay</text>
|
||||
<text class="bs" x="380" y="335" text-anchor="middle">sees token + opaque IDs</text>
|
||||
<line x1="380" y1="352" x2="380" y2="380" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<path d="M230 268 L230 410 L284 410" fill="none" stroke="var(--muted)" stroke-width="1" stroke-dasharray="4 4" marker-end="url(#a)"/>
|
||||
<text class="bs" x="222" y="352" text-anchor="end">custom credentials</text>
|
||||
<text class="bs" x="222" y="368" text-anchor="end">skip the relay</text>
|
||||
<rect class="bx" x="290" y="384" width="180" height="52" rx="8" stroke="var(--purple)"/>
|
||||
<text class="bt" x="380" y="405" text-anchor="middle">APNs / FCM</text>
|
||||
<text class="bs" x="380" y="423" text-anchor="middle">deliver the generic wake</text>
|
||||
<line x1="474" y1="410" x2="536" y2="410" stroke="var(--muted)" stroke-width="1" marker-end="url(#a)"/>
|
||||
<rect class="bx" x="540" y="384" width="190" height="52" rx="8" stroke="var(--green)"/>
|
||||
<text class="bt" x="635" y="405" text-anchor="middle">Device wakes</text>
|
||||
<text class="bs" x="635" y="423" text-anchor="middle">fetches details from home</text>
|
||||
</svg>
|
||||
<div class="legend" style="padding: 8px 6px 8px;">
|
||||
<span><i class="sw" style="background: var(--green);"></i>content stays in Silo</span>
|
||||
<span><i class="sw" style="background: var(--purple);"></i>opaque wake only, device fetches from home</span>
|
||||
<span><i class="sw" style="background: var(--amber);"></i>full content to a destination the profile chose</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Trust model</h2>
|
||||
<table>
|
||||
<thead><tr><th>Channel</th><th>Who sees content</th><th>Why that's acceptable</th></tr></thead>
|
||||
<tbody>
|
||||
<tr><td><i class="sw" style="background: var(--green); margin-right: 8px;"></i>Inbox + websocket</td><td>Only the user's own server and signed-in clients</td><td>Same auth boundary as the rest of Silo</td></tr>
|
||||
<tr><td><i class="sw" style="background: var(--purple); margin-right: 8px;"></i>Apple / Android push</td><td>Nobody — relay and Apple/Google see a device token, timestamps, and opaque IDs</td><td>The device fetches real content from the user's server after waking</td></tr>
|
||||
<tr><td><i class="sw" style="background: var(--amber); margin-right: 8px;"></i>Webhooks</td><td>The destination operator (e.g., Discord)</td><td>The profile pasted that URL; consent is explicit</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>Foundation</h2>
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<h3>Durable inbox is the source of truth</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Every profile on the server</dd>
|
||||
<dt>What</dt><dd>One delivery row per event; every channel is just a transport on top</dd>
|
||||
<dt>Where</dt><dd>The server's own PostgreSQL</dd>
|
||||
<dt>Why</dt><dd>Transports fail — the row survives, so reconnect or refresh always recovers</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Preferences are flat, not a matrix</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Profiles tuning what they hear about</dd>
|
||||
<dt>What</dt><dd>Four reason toggles plus a master switch; per-device and per-webhook filters can only narrow further</dd>
|
||||
<dt>Where</dt><dd>Hard gate at fanout, extra filters at dispatch</dd>
|
||||
<dt>Why</dt><dd>A per-channel by per-reason grid is unexplainable in UI and impossible to debug</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Privacy boundaries</h2>
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<h3>Opaque push through a hosted relay</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Admins who opt in; official store app builds</dd>
|
||||
<dt>What</dt><dd>The relay holds the Apple and Google signing credentials and forwards a device token plus opaque IDs only</dd>
|
||||
<dt>Where</dt><dd>Silo-operated service in its own repo, stateless on the request path</dd>
|
||||
<dt>Why</dt><dd>Only the publisher's credentials can wake store builds; content never leaves the server — the device fetches details from home after waking</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Custom credentials escape hatch</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Power users and forks shipping their own signed app builds</dd>
|
||||
<dt>What</dt><dd><code>custom_apns</code> / <code>custom_fcm</code> send straight to Apple or Google with the exact same minimal payload</dd>
|
||||
<dt>Where</dt><dd>Entirely on the user's server</dd>
|
||||
<dt>Why</dt><dd>Zero Silo infrastructure in the path, without loosening the privacy contract</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Profiles, not accounts, are the unit</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Households sharing one login across several profiles</dd>
|
||||
<dt>What</dt><dd>Preferences, devices, webhooks, inbox rows, and websocket bindings are all profile-keyed</dd>
|
||||
<dt>Where</dt><dd>Every table, API route, and event envelope</dd>
|
||||
<dt>Why</dt><dd>A kid's profile and a parent's profile must not see each other's activity</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Webhooks carry full content, by choice</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Each profile, up to 10 destinations</dd>
|
||||
<dt>What</dt><dd>Discord embeds or HMAC-signed JSON with series and episode details</dd>
|
||||
<dt>Where</dt><dd>Direct HTTPS from the server to the URL the profile pasted</dd>
|
||||
<dt>Why</dt><dd>Choosing the destination is the consent; HTTPS-only, the SSRF deny list, and no-server-origin rules stay mandatory</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Scale and reliability</h2>
|
||||
<div class="grid">
|
||||
<div class="card">
|
||||
<h3>Fanout reads a compact interest index</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Servers with hundreds of users, roughly 1,000 profiles</dd>
|
||||
<dt>What</dt><dd><code>profile_series_interest</code> keyed by library and series, updated on favorite, watchlist, and watch transitions</dd>
|
||||
<dt>Where</dt><dd>Written at interaction time, read once per release event</dd>
|
||||
<dt>Why</dt><dd>Cost scales with profiles interested in one series, not with a million-episode catalog</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Back-catalog imports never flood</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Anyone adding a new library or a full season pack</dd>
|
||||
<dt>What</dt><dd>First scans seed availability silently; bulk additions cap at 3 notifications per series per scan</dd>
|
||||
<dt>Where</dt><dd>Scanner seeding plus the fanout worker burst cap</dd>
|
||||
<dt>Why</dt><dd>One 200-episode import with 300 interested profiles would otherwise mean about 60,000 pushes in a single scan</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>Dispatch enqueue is durable (outbox)</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Operators who care what a crash costs</dd>
|
||||
<dt>What</dt><dd>Pending push and webhook attempt rows commit in the same transaction as the inbox row</dd>
|
||||
<dt>Where</dt><dd>The fanout transaction; recovery workers sweep stale rows</dd>
|
||||
<dt>Why</dt><dd>A crash between commit and dispatch delays sends instead of silently losing them</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="card">
|
||||
<h3>One delivery per episode, across libraries</h3>
|
||||
<dl class="kv">
|
||||
<dt>Who</dt><dd>Dual-quality setups like TV plus TV 4K</dd>
|
||||
<dt>What</dt><dd>A partial unique index on profile and episode spans libraries; the first event processed wins</dd>
|
||||
<dt>Where</dt><dd>The <code>notification_deliveries</code> schema</dd>
|
||||
<dt>Why</dt><dd>Media items are catalog-level in Silo, so the same episode landing twice must not notify twice</dd>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Source documents</h2>
|
||||
<ul class="docs">
|
||||
<li><code>00-architecture-overview.md</code> — channel model, fanout pipeline, threat model, relay addressing</li>
|
||||
<li><code>01-release-events-and-inbox.md</code> — durable inbox, fanout worker, seeding and burst suppression, sync API</li>
|
||||
<li><code>02-apns-relay.md</code> — Apple push: hosted relay and custom APNs</li>
|
||||
<li><code>03-fcm-relay.md</code> — Android push: hosted relay and custom FCM</li>
|
||||
<li><code>04-outbound-webhooks.md</code> — Discord and generic JSON+HMAC webhooks</li>
|
||||
<li><code>README.md</code> — reading order, status, and the 2026-06-11 amendment index</li>
|
||||
</ul>
|
||||
|
||||
<footer>Generated 2026-06-11 from the amended notification specs. Commands and paths assume the repository root is the cwd.</footer>
|
||||
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,967 @@
|
||||
# Silo Push Relay — Engineering Spec
|
||||
|
||||
**Date:** 2026-06-13
|
||||
**Status:** Draft (implementation-grade)
|
||||
**Service:** `silo-push-relay` (provisional repo name; the relay code lives in a **separate repository**)
|
||||
**Provisional hostname:** `relay.silo.app`
|
||||
**Stack:** Go 1.25+, PostgreSQL, Redis
|
||||
|
||||
**Provenance / source contracts (read these first):**
|
||||
|
||||
- [`../00-architecture-overview.md`](../00-architecture-overview.md) — cross-cutting design; "Mobile push: why a relay is necessary", addressing-flow walkthrough, mode terminology, cross-channel threat model.
|
||||
- [`../02-apns-relay.md`](../02-apns-relay.md) — **the external APNs relay contract** (`POST /v1/apple/send`): request/response/validation, relay-built payloads, headers, error handling. The self-hosted Silo server is already designed against this; the relay is its counterpart and must honor it exactly.
|
||||
- [`../03-fcm-relay.md`](../03-fcm-relay.md) — **the external FCM relay contract** (`POST /v1/fcm/send`): request/response/validation, relay-built data-only payloads, Android config, error handling.
|
||||
- [`./02-apns-fcm-2026-reference.md`](./02-apns-fcm-2026-reference.md) — the 2026-current Apple/Google/Go reference this service is built on. Where the older contract docs (authored 2026-04) glossed an upstream-provider mechanic, **this reference wins for the upstream side**; the **external relay contract from 02/03 is never changed** — reconciliations are called out explicitly in §5.2 (FCM upstream field casing), §6.2 (sandbox host), §6.4 (APNs error mapping), and §7.5.
|
||||
|
||||
> **Commands assume the repository root is the cwd.** No local absolute filesystem paths or transient worktree IDs appear in this document; all repo references are repository-relative.
|
||||
|
||||
---
|
||||
|
||||
## 1. Title, Provenance, Status, Scope
|
||||
|
||||
### 1.1 What this service is
|
||||
|
||||
`silo-push-relay` is a small, **stateless-on-the-request-path** HTTP service that:
|
||||
|
||||
1. Holds the official Silo Apple (`.p8` APNs auth key) and Google (Firebase service-account JSON) push credentials.
|
||||
2. Accepts authenticated, **opaque, content-free** push requests from opted-in self-hosted Silo servers on two endpoints: `POST /v1/apple/send` and `POST /v1/fcm/send`.
|
||||
3. Builds a fixed, generic APNs / FCM payload from those opaque fields and forwards it to APNs / FCM on the caller's behalf.
|
||||
4. Maps the upstream provider response back to a narrow caller-facing result.
|
||||
|
||||
It is the **server counterpart** to the relay-client integration in `02-apns-relay.md` and `03-fcm-relay.md`. Those two documents define the wire contract from the self-hosted Silo server's perspective; this document specifies the service that answers it. **The external request/response contract in 02/03 is authoritative and is reproduced here byte-for-byte; this spec adds the server-internal design (upstream clients, storage, auth, rate limiting, idempotency, ops) that 02/03 deliberately left to "a separate repo".**
|
||||
|
||||
### 1.2 What this service is NOT
|
||||
|
||||
- It is **not** a Silo media server, and it shares no database, auth system, or deployment with `silo-server`.
|
||||
- It is **not** a general-purpose push gateway: it accepts only the narrow JSON shape in §5; it never accepts a free-form APNs/FCM payload, title, body, or media identifier.
|
||||
- It is **not** an addressing service. Apple and Google do the addressing from the device token. The relay never resolves a token to a user, profile, device-to-user mapping, or server.
|
||||
- It does **not** store notification content, user/profile identity, server URLs, or token→user aliases (v1 is stateless on the request path; see `02-apns-relay.md` "Relay statefulness: stateless v1").
|
||||
- It is **not** in the `custom_apns` / `custom_fcm` path. When a self-hosted admin configures their own credentials, the Silo server talks to APNs/FCM directly and never touches this relay (see `00-architecture-overview.md` "Custom credentials").
|
||||
|
||||
### 1.3 Relationship to the contract docs and the separate repo
|
||||
|
||||
| Concern | Owner | Document |
|
||||
|---|---|---|
|
||||
| External relay request/response/validation/payloads | `02`/`03` (authoritative) | `../02-apns-relay.md`, `../03-fcm-relay.md` |
|
||||
| Self-hosted server's relay-client, dispatcher, pacing, retry | `silo-server` | `../02`, `../03`, `../00` |
|
||||
| Relay service internals (this doc): upstream clients, DB, auth, rate limit, idempotency, deploy | `silo-push-relay` (separate repo) | **this file** |
|
||||
| 2026-current upstream provider mechanics (JWT, OAuth2, endpoints, error tables) | reference | `./02-apns-fcm-2026-reference.md` |
|
||||
|
||||
These design docs are authored **here in `silo-server`** alongside the `02`/`03` contracts purely for co-location. The relay **code** lives elsewhere (`silo-push-relay`).
|
||||
|
||||
---
|
||||
|
||||
## 2. Goals and Non-Goals
|
||||
|
||||
### 2.1 Goals
|
||||
|
||||
- **G1 — Faithful contract.** Implement `POST /v1/apple/send` and `POST /v1/fcm/send` exactly as `02`/`03` specify: same request fields, same validation rules, same response shape, same relay-built payloads, same headers.
|
||||
- **G2 — Content-free by construction.** The request schema admits **only** the opaque fields in §5. Any unexpected/free-form field is rejected with `400`. The relay cannot forward content even if a caller tries.
|
||||
- **G3 — Credential custody.** Hold the official `.p8` and SA JSON in a secret manager, never on disk in the repo/image, never in env vars where avoidable (§11).
|
||||
- **G4 — Stateless request path.** No per-request DB writes other than throttled `last_used_at` and redacted op-logs. Idempotency + rate-limit state live in Redis. The relay can be horizontally scaled and a single instance can be killed mid-flight without data loss (the server-side outbox in `../01-release-events-and-inbox.md` guarantees re-delivery).
|
||||
- **G5 — Correct upstream mechanics (2026).** One cached ES256 JWT per APNs team (regenerated ~50 min, never < 20 min apart); one cached OAuth2 access token per FCM project (~1 h, refresh with margin); HTTP/2 connection reuse with PING health checks; correct reason→error mapping. Per `./02-apns-fcm-2026-reference.md`.
|
||||
- **G6 — Multi-tenant isolation.** Every API key, rate-limit bucket, idempotency key, and log line is scoped by `account_id`. Per-account allowlists bound which APNs topics / FCM (project, package) an account may push to.
|
||||
- **G7 — Minimal attack surface for admin ops.** No public admin HTTP API in v1. Account/key/allowlist administration is a CLI (`relayctl`) writing directly to the DB (§5.6).
|
||||
- **G8 — Operable.** Structured redacted logs, Prometheus metrics, `/healthz` + `/readyz`, graceful shutdown.
|
||||
|
||||
### 2.2 Non-goals
|
||||
|
||||
- **NG1** — Storing token→user aliases, device subscriptions, or any user/profile identity (deferred; `02` "stateless v1").
|
||||
- **NG2** — Accepting arbitrary APNs/FCM JSON, custom titles/bodies, badge by default, or topic broadcasts (FCM `topic`/`condition` are never used — per-token only, per `03`).
|
||||
- **NG3** — Delivery receipts, analytics, marketing exports, or open-tracking.
|
||||
- **NG4** — APNs broadcast / Live Activity channels (`/4/broadcasts/...`) — out of scope; see `./02-apns-fcm-2026-reference.md` §1.10. v1 uses only `/3/device/<token>`.
|
||||
- **NG5** — Web Push, HMS, ADM, or any non-APNs/non-FCM transport.
|
||||
- **NG6** — A public self-service signup / billing surface. Accounts are provisioned by Silo operators via `relayctl`.
|
||||
- **NG7** — The `custom_apns` / `custom_fcm` direct paths (those run inside `silo-server`, not here).
|
||||
|
||||
---
|
||||
|
||||
## 3. Trust and Threat Model
|
||||
|
||||
### 3.1 What the relay sees (restated from `00`/`02`/`03`)
|
||||
|
||||
The relay, even fully compromised, sees only:
|
||||
|
||||
- **Which self-hosted installation** is calling — the relay API key (`rk_…`) maps 1:1 to a relay account.
|
||||
- **The server's egress IP** — inherent to any hosted relay; the server connects directly. For home hosting this identifies the household connection. **This is the closest thing to an identity leak in the design** and is listed so the privacy claim stays exhaustive, not over-strong (`00` addressing-flow note).
|
||||
- **A device token** — APNs device token (~100 bytes hex) or FCM registration token (~152–180+ chars).
|
||||
- **Request timing.**
|
||||
- **Coarse wire mode** — `private_alert`/`background_wake` (APNs) or `private_data`/`background_wake` (FCM).
|
||||
- **Opaque `collapse_id` / `collapse_key`** — per-server-keyed HMACs (see §3.3).
|
||||
- **Opaque correlation IDs** — `server_device_id`, `delivery_id`.
|
||||
- **Upstream response status** — APNs `apns-id` / FCM message name, status code, reason string.
|
||||
- **`apns-topic` / FCM (`project_id`, `package_name`)** — platform/build identifiers Apple/Google already see (`02` privacy note).
|
||||
|
||||
### 3.2 What the relay must NEVER see or store
|
||||
|
||||
Per `00`/`02`/`03`: no notification title or body, no media identifiers, no profile/user/username/library/collection/item names, no server hostname or base URL, no artwork URLs, no watched-state metadata, no device→user mapping, and **no token aliases** (stateless v1). The request schema (§5) has no field that could carry any of these, and an unknown field is a hard `400` (§12).
|
||||
|
||||
### 3.3 Collapse-ID equivalence classes (opaque to the relay)
|
||||
|
||||
The Silo server computes `collapse_id = base32(HMAC-SHA256(server_collapse_secret, series_id))` truncated to 26 chars (APNs) / equivalent for FCM `collapse_key`, where `server_collapse_secret` is a random per-server secret (`02`/`03`). The relay treats this as **opaque** and never derives it. Residual leakage: the relay can group **one server's** pushes into per-series equivalence classes (that is what collapse is *for*), but the per-server key prevents recovering the series identity or correlating the same series across two different servers.
|
||||
|
||||
### 3.4 Residual leakage (exhaustive)
|
||||
|
||||
| Leak | To whom | Why it's irreducible | Mitigation |
|
||||
|---|---|---|---|
|
||||
| Egress IP | Relay operator | Server connects directly over HTTPS | Admin may route relay traffic through their own VPN/proxy egress (`02` residual-leakage note) |
|
||||
| Push timing | Relay operator + Apple/Google | A push is an observable network event | None — only meaning/content is hidden |
|
||||
| Device token | Relay operator + Apple/Google | Required to address the device | Tokens never logged raw; truncated/hashed (§11/§12) |
|
||||
| Collapse equivalence class | Relay operator | Collapse must be a stable per-series key | Per-server HMAC key prevents series recovery & cross-server correlation (§3.3) |
|
||||
| App topic / Firebase project + package | Relay operator + Apple/Google | Required by the upstream APIs | Platform/build identifiers Apple/Google already hold |
|
||||
|
||||
The relay **cannot** learn user identity, notification content, server URL, or library data even when fully compromised (`00` threat-model summary). It **cannot fabricate meaningful pushes** — only generic opaque wake payloads.
|
||||
|
||||
### 3.5 Adversaries
|
||||
|
||||
| Adversary | Capability | Worst case | Control |
|
||||
|---|---|---|---|
|
||||
| Hostile/compromised relay operator | Full DB + request stream | Learns device tokens, timing, egress IPs of opted-in servers; can send generic wakes | No content/identity; per-server collapse keying; opt-in & disabled by default |
|
||||
| Stolen relay API key | Can send opaque pushes as that account | Generic wakes to tokens the thief already holds; bounded by per-account allowlist + rate limits | Revoke key (`relayctl key revoke`); rotate; per-account quota caps spend (OWASP API4:2023) |
|
||||
| Cross-tenant attacker | Tries to push to another account's topic/token | Rejected: topic/project/package not on attacker's allowlist | Per-account allowlist enforcement (§8.4); namespaced Redis keys |
|
||||
| Apple / Google | Platform operator | Sees topic, token, generic payload, timing | No identity/content (data-only FCM; loc-key-only APNs alert) |
|
||||
|
||||
---
|
||||
|
||||
## 4. Architecture
|
||||
|
||||
### 4.1 Request path (stateless)
|
||||
|
||||
```
|
||||
1. TLS terminate (LB or in-process) → HTTP/2 inbound.
|
||||
2. Auth middleware: parse "Authorization: Bearer rk_<prefix>_<secret>";
|
||||
SELECT row by indexed prefix; constant-time-compare hash; reject revoked/expired.
|
||||
3. Rate-limit middleware: per-account Redis token bucket (Lua: refill→check→consume).
|
||||
Over limit → 429 + Retry-After.
|
||||
4. Body decode with DisallowUnknownFields (any unexpected field → 400).
|
||||
5. Validate fields (§5) incl. allowlist check (topic / project+package).
|
||||
6. Idempotency: Idempotency-Key header → Redis SET NX lock; replay / 409 / 422.
|
||||
7. Build the fixed relay payload (§5) + headers; forward to upstream APNs/FCM
|
||||
client (cached JWT / OAuth2 token, reused HTTP/2 conn).
|
||||
8. Map upstream response → caller-facing JSON (§5).
|
||||
9. Store the {status, body} under the idempotency key (TTL ~24h);
|
||||
throttled last_used_at update; emit redacted op-log + metrics.
|
||||
```
|
||||
|
||||
The only synchronous DB touch on the hot path is the **API-key lookup** (indexed, O(1) by prefix) and, at most once per minute per key, a `last_used_at` write. Idempotency and rate-limit state are in Redis. No notification content, no token alias, ever persisted.
|
||||
|
||||
### 4.2 Component diagram
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph Server["Self-hosted Silo server (caller)"]
|
||||
DISP[Push dispatcher + client-side pacing\n5 req/s token bucket]
|
||||
end
|
||||
|
||||
DISP -->|"POST /v1/apple/send Bearer rk_… Idempotency-Key"| LB[(TLS / Load balancer)]
|
||||
DISP -->|"POST /v1/fcm/send"| LB
|
||||
|
||||
subgraph Relay["silo-push-relay (stateless replicas)"]
|
||||
LB --> API[HTTP server\nnet/http, HTTP/2, timeouts]
|
||||
API --> AUTH[Auth middleware\nprefix lookup + ConstantTimeCompare]
|
||||
AUTH --> RL[Rate-limit middleware\nRedis Lua token bucket]
|
||||
RL --> IDEM[Idempotency\nRedis SET NX / replay / 409 / 422]
|
||||
IDEM --> VAL[Validate + allowlist\nDisallowUnknownFields]
|
||||
VAL --> AC[APNs client\ncached ES256 JWT ~50m\nper-team HTTP/2 pool]
|
||||
VAL --> FC[FCM client\ncached OAuth2 token ~1h\nHTTP/2]
|
||||
API --> OPS[/healthz /readyz /metrics/]
|
||||
AUTH -. redacted .-> LOG[slog JSON]
|
||||
end
|
||||
|
||||
AC -->|"POST /3/device/<token>"| APNS[(api.push.apple.com\n/ api.sandbox.push.apple.com)]
|
||||
FC -->|"POST /v1/projects/{id}/messages:send"| FCM[(fcm.googleapis.com)]
|
||||
|
||||
AUTH --> PG[(PostgreSQL\naccounts / api_keys /\nallowlists / op_logs)]
|
||||
RL --> REDIS[(Redis\nidempotency + rate buckets)]
|
||||
IDEM --> REDIS
|
||||
|
||||
CLI[relayctl CLI\naccount/key/allowlist admin] --> PG
|
||||
SM[(Secret manager)] -.->|.p8 / SA JSON / pepper| AC
|
||||
SM -.-> FC
|
||||
```
|
||||
|
||||
### 4.3 Data stores
|
||||
|
||||
| Store | Holds | Why | Notes |
|
||||
|---|---|---|---|
|
||||
| **PostgreSQL** | `relay_accounts`, `relay_api_keys`, `relay_apns_allowlist`, `relay_fcm_allowlist`, `relay_op_logs` | Durable, low-write config + redacted audit | `jackc/pgx/v5` + `pgxpool` (`./02-apns-fcm-2026-reference.md` §3.4). Never stores tokens, content, or identity. |
|
||||
| **Redis** | `idem:{account}:{key}` (idempotency lock/result), `rl:{account}` (token-bucket state) | Fast, TTL'd, shared across replicas | `redis/go-redis/v9`. Atomic Lua for rate-limit; `SET NX` for idempotency. Idle keys TTL out. |
|
||||
| **Secret manager** | APNs `.p8`, FCM SA JSON, API-key HMAC pepper, DB/Redis creds | Credential custody | KMS/Vault/secret-manager preferred over mounted file over env var (OWASP, `./02-apns-fcm-2026-reference.md` §4.5/§5). |
|
||||
|
||||
### 4.4 Credential holding
|
||||
|
||||
- **APNs:** one (or a few, scoped) `.p8` ES256 keys + Team ID + Key ID per official team. Prefer team-scoped + topic-specific keys (Feb-2025 hardening) for least privilege (`./02-apns-fcm-2026-reference.md` §1.2). A connection pool is **per APNs team** (a connection cannot serve multiple teams — §1.1 of the reference).
|
||||
- **FCM:** one service-account JSON per official Firebase project (`continuum-prod-android`). On GCP, prefer keyless Workload Identity / attached SA; off-GCP, the JSON lives in the secret manager (`./02-apns-fcm-2026-reference.md` §2.2/§4.5).
|
||||
- Credentials are loaded **once at startup** from the secret manager into memory; never written to disk, never logged, never returned by any endpoint. Reload requires a process restart (v1) or SIGHUP-triggered re-read (optional).
|
||||
|
||||
---
|
||||
|
||||
## 5. Public API Contract
|
||||
|
||||
All endpoints are HTTPS-only over HTTP/2. Authentication is `Authorization: Bearer rk_<key>` (§8.2). `Content-Type: application/json` for send endpoints. Bodies are decoded with `DisallowUnknownFields` — **any unrecognized field is a `400`** (§12). Unless stated otherwise, error bodies use the shape in §5.5.
|
||||
|
||||
### 5.1 `POST /v1/apple/send`
|
||||
|
||||
Authoritative source: `02-apns-relay.md` "Send Apple Push". Reproduced exactly.
|
||||
|
||||
**Request**
|
||||
|
||||
```http
|
||||
POST /v1/apple/send HTTP/2
|
||||
Host: relay.silo.app
|
||||
Authorization: Bearer rk_live_3Qw9...secret...
|
||||
Idempotency-Key: 01JDELIVERY...:01JDEVICE...:1
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "apns-token",
|
||||
"environment": "production",
|
||||
"topic": "com.continuum.app.ios",
|
||||
"mode": "private_alert",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_id": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
**Field table**
|
||||
|
||||
| Field | Type | Required | Rule |
|
||||
|---|---|---|---|
|
||||
| `token` | string | yes | Required; must be plausible for APNs — non-empty, hex-shaped (`^[0-9a-fA-F]+$`). Apple device tokens are commonly **64 hex chars (32 bytes)** but Apple does not guarantee a fixed length and has lengthened them historically, so the relay enforces a **generous bound (e.g. ≥ 64 and ≤ 256 hex chars)** rather than a tight guess that could reject valid future-length tokens. Reject non-hex / out-of-bound → `400 invalid_token`. |
|
||||
| `environment` | string | yes | `production` or `sandbox`. Selects upstream host (§6.2). Any other value → `400 invalid_environment`. |
|
||||
| `topic` | string | yes | Must be on the account's APNs topic allowlist (§8.4). Not allowlisted → `403 topic_not_allowed`. |
|
||||
| `mode` | string | yes | `private_alert` or `background_wake`. Other → `400 invalid_mode`. |
|
||||
| `server_device_id` | string | yes | Opaque; length-limited (≤128 chars, ULID-shaped recommended). Over limit → `400 invalid_field`. |
|
||||
| `delivery_id` | string | yes | Opaque; length-limited (≤128). |
|
||||
| `badge` | integer \| null | no | Default omitted. When present, must be a **bounded non-negative integer `0 ≤ badge ≤ 9999`** — out of range → `400 invalid_field`. This is the one numeric field copied verbatim into `aps.badge`, so it is bounded to keep it from becoming an unbounded caller-controlled channel visible to Apple. Badge is **disabled by default** to avoid leaking unread counts (`02` privacy); the relay forwards it only if provided. |
|
||||
| `collapse_id` | string \| null | no | Opaque; **≤ 64 bytes** (Apple's `apns-collapse-id` cap). Over 64 bytes → `400 invalid_collapse_id`. |
|
||||
|
||||
No other fields are accepted: **no title, body, image URL, media ID, username, server hostname, or server URL** field exists (`02`). An unknown key → `400 unexpected_field`.
|
||||
|
||||
**`Idempotency-Key` header** (required for sends): format `<notification_delivery_id>:<push_device_id>:<attempt_number>` (`02`/`03`). See §10.
|
||||
|
||||
**Response — 200**
|
||||
|
||||
```json
|
||||
{
|
||||
"request_id": "01JRELAY...",
|
||||
"apns_id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"status": "accepted"
|
||||
}
|
||||
```
|
||||
|
||||
- `request_id` — relay-generated ULID, also echoed as the `X-Request-Id` response header and used in logs.
|
||||
- `apns_id` — Apple's `apns-id` response header (UUID).
|
||||
- `status` — `accepted` on a 200 from APNs.
|
||||
|
||||
> **Sandbox `apns-unique-id` (additive observability).** On a **200 in the sandbox/DEVELOPMENT environment only**, APNs additionally returns an `apns-unique-id` response header used by the Push Notifications Console Delivery Log (reference §1.6). It is the single most useful field for diagnosing "accepted-but-not-delivered" on sandbox. The relay **captures `apns-unique-id` (sandbox only)** into the op-log and the `relayctl ping-upstream` output, and may echo it in the sandbox 200 body, to aid delivery-log correlation during client QA. It is **never** present or logged for production sends.
|
||||
|
||||
**Relay-built APNs payloads** (the relay constructs these; callers cannot override — `02`).
|
||||
|
||||
`private_alert`:
|
||||
|
||||
```json
|
||||
{
|
||||
"aps": {
|
||||
"alert": {
|
||||
"title-loc-key": "SILO_NOTIFICATION_TITLE",
|
||||
"loc-key": "SILO_NOTIFICATION_GENERIC_BODY"
|
||||
},
|
||||
"sound": "default"
|
||||
},
|
||||
"silo": {
|
||||
"v": 1,
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
When `badge` is present and non-null, the relay adds `"badge": <n>` inside `aps`. Otherwise it is omitted entirely (no `badge` key).
|
||||
|
||||
`background_wake`:
|
||||
|
||||
```json
|
||||
{
|
||||
"aps": { "content-available": 1 },
|
||||
"silo": {
|
||||
"v": 1,
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Exact APNs request headers the relay sets** (`02` Headers + reconciled with `./02-apns-fcm-2026-reference.md` §1.4):
|
||||
|
||||
| Upstream header | `private_alert` | `background_wake` | Source |
|
||||
|---|---|---|---|
|
||||
| `:method` | `POST` | `POST` | fixed |
|
||||
| `:path` | `/3/device/<token>` | `/3/device/<token>` | `token` field |
|
||||
| `authorization` | `bearer <cached ES256 JWT>` | same | relay JWT (§6.1) |
|
||||
| `apns-topic` | the allowlisted `topic` | same | `topic` field |
|
||||
| `apns-push-type` | `alert` | `background` | `02` |
|
||||
| `apns-priority` | `10` | `5` | `02` (and reference §1.4.1: background **must** be 5; 10 is an error) |
|
||||
| `apns-collapse-id` | `collapse_id` if present (≤64B) | same | `02` |
|
||||
| `apns-id` | omitted (Apple auto-generates; echoed back) | same | reference §1.4 |
|
||||
| `apns-expiration` | **finite default** (e.g. `now + 4 h`) | **short TTL** (e.g. `now + 1 h`) | reference §1.4. **Configurable**; ships a sane non-30-day default — see below. |
|
||||
|
||||
> **`apns-expiration` policy (v1 ships a finite default, not the 30-day store-and-retry).** With `apns-expiration` omitted, Apple stores and retries an undelivered push for up to **30 days** (reference §1.4) — a wake fired days later is useless or confusing for content that is fetched live from the user's server on wake (the underlying notification may already be read/deleted server-side). The relay therefore sets a **finite, configurable `apns-expiration`**: a few hours for `private_alert` (default ~4 h) and a **short** TTL for `background_wake` (default ~1 h), so a device offline for days does not get a stale wake. Both defaults are config values (per-deployment); the relay never ships the 30-day default. The FCM path sets the parallel `android.ttl` (§5.2) for parity. (Resolved from former open question §15 #5; values are tunable from product input.)
|
||||
|
||||
Payload is **uncompressed JSON**, ≤ 4 KB (reference §1.5). The relay-built payloads are far under 4 KB; the relay still rejects with `400 payload_too_large` defensively if a (future) field pushes it over, and maps an upstream `413` per §6.4.
|
||||
|
||||
**Error mapping back to the caller** — see §6.4.
|
||||
|
||||
### 5.2 `POST /v1/fcm/send`
|
||||
|
||||
Authoritative source: `03-fcm-relay.md` "Send Android Push". Reproduced exactly.
|
||||
|
||||
**Request**
|
||||
|
||||
```http
|
||||
POST /v1/fcm/send HTTP/2
|
||||
Host: relay.silo.app
|
||||
Authorization: Bearer rk_live_3Qw9...secret...
|
||||
Idempotency-Key: 01JDELIVERY...:01JDEVICE...:1
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"token": "fcm-registration-token",
|
||||
"project_id": "continuum-prod-android",
|
||||
"package_name": "com.continuum.app.android",
|
||||
"mode": "private_data",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY...",
|
||||
"badge": null,
|
||||
"collapse_key": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
```
|
||||
|
||||
**Field table**
|
||||
|
||||
| Field | Type | Required | Rule |
|
||||
|---|---|---|---|
|
||||
| `token` | string | yes | Plausible FCM token: non-empty, base64url-ish, **length 100 ≤ len ≤ 4096** (FCM tokens are typically 152–180+ chars — `03`; the upper bound guards against oversized-token abuse without rejecting legitimate longer tokens). Implausible → `400 invalid_token`. |
|
||||
| `project_id` | string | yes | Must match the configured Firebase project for this account's allowlist (§8.4). Mismatch → `403 project_not_allowed`. |
|
||||
| `package_name` | string | yes | Must be on the account's FCM package allowlist, paired with `project_id` (§8.4). Not allowlisted → `403 package_not_allowed`. |
|
||||
| `mode` | string | yes | `private_data` or `background_wake` (the **wire** mode; profile-level is `private_push` — `00`/`03`). Other → `400 invalid_mode`. |
|
||||
| `server_device_id` | string | yes | Opaque; ≤128 chars. |
|
||||
| `delivery_id` | string | yes | Opaque; ≤128 chars. |
|
||||
| `badge` | integer \| null | no | Accepted for caller-contract parity with the APNs endpoint, but **FCM has no data-only home for it** (the relay never builds a `notification` block). v1 **ignores `badge` on the FCM path** — it is neither rendered nor smuggled into the `data` map (§5.2 construction rules, §12). When present it must still pass the same bound as APNs (`0 ≤ badge ≤ 9999`); otherwise → `400 invalid_field`. |
|
||||
| `collapse_key` | string \| null | no | Opaque; **≤ 64 bytes**. This is the **inbound caller field name** (snake_case, per `03`); the relay maps it to the FCM REST field `android.collapseKey` (camelCase — see §5.2 construction rules). Over 64 bytes → `400 invalid_collapse_key`. |
|
||||
|
||||
No free-form `notification`, title, body, image URL, media ID, username, or server URL field exists (`03`). Unknown key → `400 unexpected_field`.
|
||||
|
||||
**Response — 200**
|
||||
|
||||
```json
|
||||
{
|
||||
"request_id": "01JRELAY...",
|
||||
"fcm_message_name": "projects/continuum-prod-android/messages/0:1234567890123456%abcdef",
|
||||
"status": "accepted"
|
||||
}
|
||||
```
|
||||
|
||||
- `fcm_message_name` — FCM v1 response `name`.
|
||||
|
||||
**Relay-built FCM payloads — data-only, no `notification` block** (`03`).
|
||||
|
||||
`private_data`:
|
||||
|
||||
```json
|
||||
{
|
||||
"message": {
|
||||
"token": "fcm-registration-token",
|
||||
"data": {
|
||||
"v": "1",
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
},
|
||||
"android": {
|
||||
"priority": "HIGH",
|
||||
"collapseKey": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`background_wake`:
|
||||
|
||||
```json
|
||||
{
|
||||
"message": {
|
||||
"token": "fcm-registration-token",
|
||||
"data": {
|
||||
"v": "1",
|
||||
"wake": "notifications.changed",
|
||||
"server_device_id": "01JOPAQUE...",
|
||||
"delivery_id": "01JDELIVERY..."
|
||||
},
|
||||
"android": {
|
||||
"priority": "NORMAL",
|
||||
"collapseKey": "01JOPAQUE_COLLAPSE"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Construction rules (reconciled with `./02-apns-fcm-2026-reference.md` §2.3/§2.4):**
|
||||
|
||||
- `message.data` is `map<string,string>` — **all values are strings** (`"v": "1"`, not `1`). It is a **fixed, closed set of keys** — exactly `{v, wake, server_device_id, delivery_id}` — with **no caller-controlled key or value** beyond the already-opaque IDs (§12). Reserved keys (`from`, `message_type`, `notification`, `google.*`, `gcm.*`) are never used, and `badge` is never injected into `data`.
|
||||
- No top-level `message.notification` — **data-only** so Google carries no rendering content (`03`).
|
||||
- `android.priority`: `"HIGH"` for `private_data` (wakes under Doze), `"NORMAL"` for `background_wake`. The relay emits the **canonical uppercase enum** values documented for FCM REST `AndroidConfig.priority` (reference §2.4); FCM also accepts lowercase, but the canonical form is used to avoid relying on a parenthetical allowance. **Reconciliation:** the `03` contract reproduces lowercase `high`/`normal`; the wire field accepts both and `priority` is relay-set (not a caller field), so this is an internal-upstream normalization with no external-contract impact.
|
||||
- `android.collapseKey` (REST **camelCase** — reference §2.4): set to the caller's `collapse_key` when present, omitted otherwise. **Maps to FCM `android.collapseKey`** — the relay POSTs raw JSON to the FCM HTTP v1 REST endpoint, which rejects unknown fields (including a snake_case `collapse_key` inside `android`) with `INVALID_ARGUMENT`. **Reconciliation (parallel to the sandbox-host one in §6.2):** the inbound caller field stays `collapse_key` per `03`/§5.2; only the relay-built upstream FCM field is `collapseKey`.
|
||||
- `android.ttl` (a duration string, e.g. `"14400s"`): set to a **finite, configurable** TTL for parity with the APNs `apns-expiration` policy (FCM otherwise defaults to **4 weeks** of storage). Default ~4 h for `private_data`, a shorter TTL for `background_wake`, so a device offline for days does not get a stale wake. Configurable per deployment.
|
||||
- `validateOnly` (top-level, **camelCase** — reference §2.5) is the dry-run flag for the `relayctl` test path only (§7.3, §7.5); never set on production sends.
|
||||
- The relay never sets FCM `topic`/`condition`; only `message.token` (per-token only — `03`).
|
||||
- Payload ≤ 4096 bytes for token sends (reference §2.3); the fixed payload is far under.
|
||||
|
||||
> **Reconciliation note (priority vs. reference §2.8).** The reference warns that high-priority data messages that never produce a user-facing notification can be deprioritized to normal by FCM over a ~7-day window. The `03` contract nonetheless specifies `priority: high` for `private_data` (the app *does* surface a notification after wake), and the relay **honors the `03` contract** — it does not second-guess priority. Deprioritization is an upstream behavior, not a contract change; the `03` doc already documents the quota fallback (caller may switch to normal after repeated 429s — §7.4).
|
||||
|
||||
**Exact FCM request** the relay sends:
|
||||
|
||||
```http
|
||||
POST /v1/projects/continuum-prod-android/messages:send HTTP/2
|
||||
Host: fcm.googleapis.com
|
||||
Authorization: Bearer <cached OAuth2 access token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
**Error mapping** — see §7.4.
|
||||
|
||||
### 5.3 `GET /healthz`
|
||||
|
||||
Liveness. Returns `200 {"status":"ok"}` if the process is up. No dependency checks. Used by the orchestrator's liveness probe. Unauthenticated.
|
||||
|
||||
### 5.4 `GET /readyz` and `GET /metrics`
|
||||
|
||||
- **`GET /readyz`** — readiness. Readiness gates on **core shared dependencies only**: Postgres reachable (`SELECT 1` via pool) and Redis reachable (`PING`). Per-provider health (APNs signer initialized; FCM token source initialized) is reported **separately, not as a binary gate**, so that a **single-provider** credential/init failure does **not** pull the whole replica from the LB and kill the healthy provider too (APNs and FCM are independent upstreams; the "channel failures are isolated" principle from `00` carried into the relay's own health model). The body is `200 {"status":"ready","providers":{"apns":"healthy|degraded","fcm":"healthy|degraded"}}` when core deps are up, or `503 {"status":"not_ready","checks":{...}}` only when **PG or Redis** is down. When a provider is `degraded`, the **send handler for that provider** returns `503 upstream_unavailable` for that provider only (the other provider keeps serving); a per-provider gauge and circuit-breaker state make `APNs degraded, FCM healthy` visible to operators rather than a binary not-ready. See §13.2 (`relay_provider_healthy`).
|
||||
- **`GET /metrics`** — Prometheus exposition (`promhttp`). Bound to the internal network / scrape-only. See §13.2 for the catalog.
|
||||
|
||||
### 5.5 Standard error body
|
||||
|
||||
All 4xx/5xx (except health endpoints) return:
|
||||
|
||||
```json
|
||||
{
|
||||
"error": {
|
||||
"code": "topic_not_allowed",
|
||||
"message": "topic is not on this account's allowlist",
|
||||
"request_id": "01JRELAY..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`code` is a stable machine string (see §6.4 / §7.4 tables). `request_id` matches `X-Request-Id`. `Retry-After` is set on `429`/`503` (§9, §10).
|
||||
|
||||
### 5.6 Admin surface — `relayctl` CLI (no public admin HTTP API)
|
||||
|
||||
To minimize attack surface (G7), there is **no HTTP admin API** in v1. Administration is a Go CLI, `relayctl`, that connects **directly to the relay's PostgreSQL** (via a privileged DSN held by operators only) and performs the operations below. It runs from an operator workstation or a jump host — never exposed to the public internet.
|
||||
|
||||
| Command | Action | DB effect |
|
||||
|---|---|---|
|
||||
| `relayctl account create --name <label> [--note <text>]` | Provision a relay account (one per opt-in installation). Prints the new `account_id`. | INSERT `relay_accounts`. |
|
||||
| `relayctl account list [--active]` | List accounts with status, key count, last activity. | SELECT. |
|
||||
| `relayctl account disable --account <id>` | Suspend an account (all keys rejected, but rows retained). | UPDATE `relay_accounts.status='disabled'`. |
|
||||
| `relayctl key issue --account <id> [--env live\|test] [--expires <dur>]` | Generate a new API key. **Prints the full `rk_…` token exactly once** (only the hash is stored). | INSERT `relay_api_keys`. |
|
||||
| `relayctl key list --account <id>` | List keys (prefix, created, last_used, revoked) — never the secret. | SELECT. |
|
||||
| `relayctl key revoke --key-prefix <prefix>` | Revoke a key immediately. | UPDATE `relay_api_keys.revoked_at=now()`. |
|
||||
| `relayctl allowlist apns set --account <id> --topics com.continuum.app.ios,.tvos,.macos` | Replace the account's APNs topic allowlist. | UPSERT `relay_apns_allowlist`. |
|
||||
| `relayctl allowlist fcm set --account <id> --project continuum-prod-android --packages com.continuum.app.android` | Replace the account's FCM (project, packages) allowlist. | UPSERT `relay_fcm_allowlist`. |
|
||||
| `relayctl allowlist show --account <id>` | Print effective allowlists. | SELECT. |
|
||||
|
||||
`relayctl` operations are themselves audited into `relay_op_logs` (event `admin.*`) with the operator identity (OS user / `--actor`) but never the secret token. The CLI shares the relay's data-access package so schema and constraints stay in one place.
|
||||
|
||||
---
|
||||
|
||||
## 6. Upstream APNs Client
|
||||
|
||||
Built per `./02-apns-fcm-2026-reference.md` §1 and §3.1. Library choice: `github.com/sideshow/apns2 v0.25.0` **or** a hand-rolled `net/http` + `golang.org/x/net/http2` + `golang-jwt/jwt/v5` client (reference §3.1; bus-factor caveat in §15). Either way, the mechanics below are required.
|
||||
|
||||
### 6.1 Auth — JWT ES256 lifecycle
|
||||
|
||||
- **Algorithm:** ES256 (ECDSA P-256) **only** — APNs accepts no other (reference §1.1).
|
||||
- **Header:** `{"alg":"ES256","kid":"<10-char Key ID>"}`. **Claims:** `{"iss":"<10-char Team ID>","iat":<epoch seconds>}`.
|
||||
- **Signing key:** the official `.p8` private key from the secret manager.
|
||||
- **Caching:** generate **one** JWT, hold it in memory behind a mutex, reuse across all senders/connections/topics for that team (reference §1.1/§4.4).
|
||||
- **Refresh cadence:** regenerate on a **~50-minute timer** (well inside Apple's 1-hour `iat` window), **never more often than once per 20 minutes**, and eagerly on a `403 ExpiredProviderToken` (then retry the request once). Over-frequent refresh → `429 TooManyProviderTokenUpdates`; back off and slow refresh (reference §1.1 lifecycle table).
|
||||
- **Single-flight regeneration (stampede guard).** Under a burst of concurrent sends sharing one cached JWT, an expiry or a `403 ExpiredProviderToken` is observed by **many in-flight goroutines at once**. JWT regeneration **must be single-flighted** — e.g. `golang.org/x/sync/singleflight` keyed by team, or a generation counter behind the mutex: a goroutine seeing `ExpiredProviderToken` checks whether the JWT was **already refreshed since its request started** (compare its captured generation/`iat` to the current one) and, if so, **reuses the new token** instead of triggering another refresh. Only **one** refresh happens per ~20-min window regardless of how many concurrent 403s arrive. A naive per-goroutine regenerate-on-403 would trip `429 TooManyProviderTokenUpdates`; the 20-minute floor is honored **by this concurrency mechanism**, not just asserted. (Mirrors risk R3.)
|
||||
- **Per-team pools.** A connection cannot map to multiple teams; open a separate JWT + HTTP/2 connection pool per APNs team (reference §1.1). v1 has one official team, so one pool — but the code keys pools by team to stay correct.
|
||||
- **Key selection — `(environment, topic) → .p8 key`.** With Apple's Feb-2025 team-scoped/topic-specific keys (max 2 team-scoped keys per environment), more than one `.p8` may be loaded (e.g. a topic-specific key for `com.continuum.app.ios` plus a team key; or per-environment scoped keys). A **sandbox send must use a sandbox-scoped key and a production send a production-scoped key** — using the wrong one yields `403 BadEnvironmentKeyIdInToken` / `UnrelatedKeyIdInToken`. The relay therefore holds an explicit **key-selection map `(environment, topic) → key id`** in config, falling back to an unscoped team key only where no scoped key exists. **Startup validation:** every allowlisted `(topic, environment)` must resolve to a usable key, or startup fails. A `BadEnvironmentKeyIdInToken`/`UnrelatedKeyIdInToken` at runtime maps to `upstream_auth_error` (§6.4) and is treated as a **key-selection misconfiguration** (ops alert).
|
||||
|
||||
### 6.2 Endpoint selection
|
||||
|
||||
| `environment` field | Upstream host | Port |
|
||||
|---|---|---|
|
||||
| `production` | `https://api.push.apple.com` | 443 (2197 fallback) |
|
||||
| `sandbox` | `https://api.sandbox.push.apple.com` | 443 (2197 fallback) |
|
||||
|
||||
Both hosts are **hardcoded constants** selected only by the `environment` enum — never derived from any other request field (§11 egress/SSRF posture).
|
||||
|
||||
> **Reconciliation (host name).** `02-apns-relay.md` validation text names the sandbox host `https://api.development.push.apple.com`. **Both `api.sandbox.push.apple.com` and `api.development.push.apple.com` are valid, functionally-equivalent Apple sandbox aliases — both resolve.** The relay **standardizes on `api.sandbox.push.apple.com`** (reference §1.3; it also matches `sideshow/apns2 v0.25.0`'s `HostDevelopment` constant, which points at `api.sandbox.push.apple.com`). This is an *internal upstream* detail and does **not** change the external contract: callers still send `"environment": "sandbox"` exactly as `02` specifies. (See reference open question §5 item 4 / this spec §15 — now a note that the two hosts are interchangeable, not a discrepancy to resolve at build time.)
|
||||
|
||||
### 6.3 Connection management
|
||||
|
||||
- HTTP/2 + TLS 1.2+ (1.3 acceptable). **Reuse one client / connection pool per process per environment per team**; never create a client per push (TLS setup is slow — reference §3.1). One well-tuned connection can do thousands of pushes/sec.
|
||||
- Set a non-zero `ReadIdleTimeout` (~15 s) + `PingTimeout` (~15 s) on the `http2.Transport` to reap half-open connections (reference §3.3 — "the single most important tuning for a long-lived relay").
|
||||
- Do **not** hardcode `SETTINGS_MAX_CONCURRENT_STREAMS`; read it from the server (reference §1.3/§3.3).
|
||||
- Handle `GOAWAY`: drop the connection from the pool and reconnect; finish in-flight streams ≤ `Last-Stream-ID` (RFC 9113 §6.8). Go's `net/http2` transport does most of this when one client is reused (reference §1.3). **A stream severed by `GOAWAY`/reset before a final APNs response is mapped to `503 upstream_unavailable` (retryable) and is NOT retried in-process** — see §6.4 "Connection-severed / GOAWAY" for the exact caller-facing rule and the at-most-once rationale.
|
||||
- Ensure the TLS trust store includes **USERTrust RSA** (SHA-2 root) — already in standard OS/Go pools; only matters if the relay pins or ships a minimal trust store (reference §1.7).
|
||||
|
||||
### 6.4 Response / reason mapping → caller-facing error
|
||||
|
||||
The relay parses the APNs JSON `{"reason":"…"}` (and the `timestamp` on 410), not just the status code (reference §1.6). It maps to a caller-facing `error.code` and decides retryability. **The relay does not itself retry transient upstream errors across separate caller requests** — it returns a retryable status so the caller's outbox/backoff (in `../01-release-events-and-inbox.md`/`02`) drives retries; the one exception is the in-process JWT-expiry retry (§6.1).
|
||||
|
||||
| APNs status | APNs `reason` | Caller HTTP | Caller `error.code` | Retryable (caller)? |
|
||||
|---|---|---|---|---|
|
||||
| 200 | — | 200 | — | success |
|
||||
| 400 | `BadDeviceToken`, `InvalidToken`, `DeviceTokenNotForTopic`, `MissingDeviceToken` | 422 | `bad_device_token` | **No — purge token** (caller disables device, per `02`). `02` treats `BadDeviceToken`/`InvalidToken` equivalently: "Apple has migrated some responses from `BadDeviceToken` to `InvalidToken`; treat them equivalently." See the environment-mismatch caveat below. |
|
||||
| 400 | `BadTopic`, `MissingTopic`, `TopicDisallowed` | 400 | `bad_topic` | No (config error) |
|
||||
| 400 | `BadCollapseId` | 400 | `invalid_collapse_id` | No |
|
||||
| 400 | `BadExpirationDate`, `BadPriority`, `BadMessageId`, `InvalidPushType`, `PayloadEmpty`, `DuplicateHeaders`, `IdleTimeout` | 400 | `bad_request` | No |
|
||||
| 403 | `ExpiredProviderToken` | (internal) | — | **Relay regenerates JWT, retries once** (§6.1), then maps result |
|
||||
| 403 | `InvalidProviderToken`, `MissingProviderToken`, `UnrelatedKeyIdInToken`, `BadEnvironmentKeyIdInToken`, `Forbidden`, `BadCertificate*` | 502 | `upstream_auth_error` | No (relay misconfig — **ops alert**, not caller-fixable) |
|
||||
| 404 | `BadPath` | 502 | `upstream_error` | No |
|
||||
| 405 | `MethodNotAllowed` | 502 | `upstream_error` | No |
|
||||
| 410 | `Unregistered`, `ExpiredToken` | 410 | `unregistered` | **No — purge token** (caller disables device). `timestamp` (ms) passed through in `error.message` so caller can compare against its registration time. |
|
||||
| 413 | `PayloadTooLarge` | 400 | `payload_too_large` | No |
|
||||
| 429 | `TooManyProviderTokenUpdates` | 503 | `upstream_throttled` | Yes — relay slows JWT refresh; caller backs off |
|
||||
| 429 | `TooManyRequests` | 429 | `rate_limited` | Yes — per-device backoff; `Retry-After` set |
|
||||
| 500 | `InternalServerError` | 503 | `upstream_unavailable` | Yes (caller backoff ~15 min) |
|
||||
| 503 | `ServiceUnavailable`, `Shutdown` | 503 | `upstream_unavailable` | Yes (caller backoff) |
|
||||
|
||||
**Default / fallback for unrecognized reasons.** Any **unmapped** `reason` string on a `400`/`403`/`410` (e.g. a future Apple reason the relay does not know) maps to `502 upstream_error`, **non-retryable, no token purge** — a safe default that surfaces the anomaly for investigation rather than silently dropping it or purging a possibly-live token. An unmapped `5xx` maps to `503 upstream_unavailable` (retryable). The exact unmapped reason is recorded in the op-log `upstream_reason` for triage.
|
||||
|
||||
**Connection-severed / GOAWAY (no final response).** A request whose HTTP/2 stream is terminated by a `GOAWAY`, connection reset, or any transport drop **before a final APNs response arrives** is mapped to `503 upstream_unavailable` (retryable) — the same as a network timeout. The relay does **NOT** transparently retry such a request in-process; the caller's outbox (`01-release-events-and-inbox.md`) re-dispatches it under the same idempotency key, preserving at-most-once semantics (a request that may have been delivered must not be silently re-sent by the relay). Apple emits `GOAWAY` routinely for maintenance/overload, so this is a normal, expected outcome, distinguished in the op-log/metrics with a `connection_severed` outcome label.
|
||||
|
||||
**Environment / sandbox-vs-production mismatch caveat.** A caller that sends `environment=sandbox` with a production token (or vice-versa) gets `BadDeviceToken`/`InvalidToken` from APNs for a token that may be perfectly valid in the other environment. The relay still returns `422 bad_device_token`, but **the caller should NOT treat the first such error from a newly-onboarded device as conclusive grounds to purge** when an environment mismatch is plausible. To let operators distinguish a systemic misconfiguration from genuinely dead tokens, the relay tags these op-logs/metrics so a burst of `bad_device_token` concentrated on **one account** (a likely env mixup) is visible separately from scattered dead tokens across many accounts. See §5.1 (token rules) and §8.4.
|
||||
|
||||
Mapping to the caller's own error handling in `02`: `bad_device_token`/`unregistered` → the caller disables the device (`02` "BadDeviceToken / Unregistered: disable the token"); `upstream_throttled`/`rate_limited`/`upstream_unavailable` → the caller retries with backoff. The relay never surfaces `ExpiredProviderToken`/`TooManyProviderTokenUpdates` as caller-facing codes (those are the relay's own JWT lifecycle — `02` notes they apply only to `custom_apns`, never the relay path).
|
||||
|
||||
Network timeout / connection failure to APNs → `503 upstream_unavailable` (retryable).
|
||||
|
||||
---
|
||||
|
||||
## 7. Upstream FCM Client
|
||||
|
||||
Built per `./02-apns-fcm-2026-reference.md` §2 and §3.2. Preferred path: `golang.org/x/oauth2/google` `TokenSource` (auto-caches + refreshes) + a custom HTTP/2 client posting the relay-built JSON (reference §3.2 — lighter than the Admin SDK and gives direct control of connection reuse and error classification). The Admin SDK (`firebase.google.com/go/v4/messaging`) is an acceptable alternative; pick exactly one.
|
||||
|
||||
### 7.1 OAuth2 service-account token source + caching
|
||||
|
||||
- Build a `TokenSource` from the SA JSON with `google.CredentialsFromJSONWithType(ctx, jsonKey, google.ServiceAccount, "https://www.googleapis.com/auth/firebase.messaging")` (least-privilege scope; `CredentialsFromJSON` is deprecated — reference §2.2/§3.2). The `TokenSource` caches and auto-refreshes.
|
||||
- Access tokens are valid **1 hour**; never mint per request. The library re-mints before expiry; if hand-rolling the JWT-bearer grant, refresh with a ~5-minute safety margin (reference §2.2).
|
||||
- **Credential resolution:** prefer keyless **Workload Identity / attached SA** when the relay runs on GCP; off-GCP, load the SA JSON from the secret manager (reference §2.2/§4.5; §11 here).
|
||||
- One token source **per Firebase project**. v1 has one official project (`continuum-prod-android`).
|
||||
|
||||
### 7.2 Endpoint
|
||||
|
||||
```http
|
||||
POST https://fcm.googleapis.com/v1/projects/{PROJECT_ID}/messages:send
|
||||
Authorization: Bearer <oauth2_access_token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
Global host only — **no regional/data-residency send endpoints exist** as of June 2026 (reference §2.3). The host `fcm.googleapis.com` is a **hardcoded constant**, never derived from request input. `{PROJECT_ID}` is the validated `project_id` from the request: it is checked against a **strict charset (`^[a-z0-9-]+$`) before** allowlist lookup and before URL interpolation, so an allowlist row can never inject path-traversal or override the host (§11 egress/SSRF posture).
|
||||
|
||||
### 7.3 Data-only message construction
|
||||
|
||||
As in §5.2: `message.token` set to the caller's `token`; `message.data` is string-valued only (a fixed, closed key set — §5.2/§12); `message.android.priority` is the canonical `"HIGH"`/`"NORMAL"` enum by mode; the caller's `collapse_key` is mapped to the REST field `message.android.collapseKey` (camelCase) when present; **no `notification` block**. The relay can optionally use `validateOnly: true` (top-level, camelCase) for the `relayctl`-driven test path (reference §2.5) but **never** on production sends.
|
||||
|
||||
### 7.4 Error mapping → caller-facing error + Retry-After
|
||||
|
||||
The relay classifies on `error.status` / the `FcmError.errorCode` in `details[]`, not the bare HTTP code (reference §2.6).
|
||||
|
||||
| FCM HTTP | `error.status` | Caller HTTP | Caller `error.code` | Retryable (caller)? |
|
||||
|---|---|---|---|---|
|
||||
| 200 | — | 200 | — | success |
|
||||
| 400 | `INVALID_ARGUMENT` | 422 | `invalid_argument` | No. Ambiguous (bad payload **or** bad token); since the relay payload is independently valid, the caller may treat it as a dead token (`03` / reference §2.6). |
|
||||
| 401 | `THIRD_PARTY_AUTH_ERROR` | 502 | `upstream_error` | **Not expected on this pure-Android path** — see note below. No auto-purge; surface for investigation. Do **NOT** page as a relay-credential failure. |
|
||||
| 403 | `SENDER_ID_MISMATCH` | 403 | `sender_id_mismatch` | **No — disable device** (token minted under a different project; `03`). |
|
||||
| 404 | `UNREGISTERED` | 410 | `unregistered` | **No — purge token** (`03`). Caller-facing **410** is an **intentional cross-provider normalization** — see note below. |
|
||||
| 429 | `QUOTA_EXCEEDED` (RESOURCE_EXHAUSTED) | 429 | `rate_limited` | Yes — **honor `Retry-After`** if present; else default **60 s** (reference §2.7). |
|
||||
| 429 / 400 | per-device rate (`DeviceMessageRateExceeded` — see note) | 429 | `rate_limited` | Yes — **retryable; NOT a token purge**. Conservative default `Retry-After` (60 s) if none provided. |
|
||||
| 401 / 403 | (HTTP **status**, not `FcmError` code) `UNAUTHENTICATED` / `PERMISSION_DENIED` on `messages:send`, **or** a non-200 from `oauth2.googleapis.com/token` at mint time | 502 | `upstream_auth_error` | No — **relay's own OAuth2 / service-account credential failure** (**ops alert**). See note below. |
|
||||
| 500 | `INTERNAL` | 503 | `upstream_unavailable` | Yes (backoff; no documented `Retry-After`). |
|
||||
| 503 | `UNAVAILABLE` | 503 | `upstream_unavailable` | Yes — **honor `Retry-After`** if present (reference §2.7). |
|
||||
| — | `UNSPECIFIED_ERROR` / any unmapped `errorCode` | 502 | `upstream_error` | No auto-purge; surface for investigation (reference §2.6). |
|
||||
|
||||
**Note — `THIRD_PARTY_AUTH_ERROR` is not a relay-credential signal on this path.** Per Firebase's error-codes page, `THIRD_PARTY_AUTH_ERROR` means *"APNs certificate or web-push auth key was invalid or missing"* and is returned **only** for messages targeting **iOS-via-APNs or Web Push** registration tokens — i.e. when FCM itself must authenticate downstream to Apple/VAPID. The relay's `/v1/fcm/send` sends **only Android registration tokens** (data-only `message.token`, package `com.continuum.app.android`), so a legitimate Android send can never produce `THIRD_PARTY_AUTH_ERROR`. If it ever appears it indicates a **per-token/registration anomaly** (an Android token that is somehow APNs/web-bound — a client/registration bug), not a relay `.p8`/SA misconfiguration; treat it as `502 upstream_error`, surface for investigation, and do **not** page on-call as a credential failure.
|
||||
|
||||
**Note — the relay's OWN FCM credential failure surfaces differently.** A failure of the relay's OAuth2 service-account auth shows up as a non-200 from the Google token endpoint (`oauth2.googleapis.com/token`) at mint time, or as an HTTP **status** `401 UNAUTHENTICATED` / `403 PERMISSION_DENIED` on `messages:send` (the gRPC/REST status, **not** an `FcmError.errorCode`). **These** are the signals that map to `upstream_auth_error` and warrant an ops alert (the alerting condition in `01-implementation-plan.md` keys on this `upstream_reason`, not on `THIRD_PARTY_AUTH_ERROR`).
|
||||
|
||||
**Note — `UNREGISTERED` is deliberately re-mapped from FCM's native 404 to caller-facing 410.** FCM natively returns `UNREGISTERED` as HTTP **404**; the relay normalizes it to caller-facing **410** so it matches the same "dead token, purge" semantic the APNs path uses for `Unregistered` (where APNs natively uses 410). This is an **intentional cross-provider normalization** (not a bug) so the caller's device-disable logic is uniform across APNs and FCM. Similarly `SENDER_ID_MISMATCH` keeps FCM's native 403.
|
||||
|
||||
**Note — per-device rate exceedance.** FCM enforces per-device Android caps (~240 msg/min, ~5000 msg/hr) that surface as a `DeviceMessageRateExceeded`-class error, **distinct from project-level `QUOTA_EXCEEDED`** (reference §2.7). Because the relay fans out bursty per-device wakes, this is a plausible real outcome. The exact error code/string for per-device exceedance is **not firmly documented**, so the relay detects it **defensively** by classifying on `error.status`/`errorCode` and maps it to a **retryable `429 rate_limited`** with a conservative default `Retry-After` — **never a token purge** (the device is fine, just rate-limited). The relay's coarse per-`(account, token_hash)` limiter (§9.1) is the local guard, but an upstream per-device error must still map to `429`, not fall through to `502`.
|
||||
|
||||
**Retry-After honoring (reference §2.7):** on FCM `429`, the relay reads the upstream `Retry-After`; if absent it defaults to 60 s. It forwards this as the caller-facing `Retry-After` header on the `429 rate_limited` response (relative `delay-seconds`, not an absolute epoch — reference §4.2). On `503 UNAVAILABLE` it forwards any upstream `Retry-After`.
|
||||
|
||||
Mapping to `03` caller handling: `unregistered`/`sender_id_mismatch`/`invalid_argument` → caller disables the device; `rate_limited` → caller backs off and may fall back to `priority: NORMAL` after repeated 429s (`03` open question, resolved "yes with cooldown"); `upstream_auth_error` → caller surfaces admin diagnostics, no retry. Note that `upstream_auth_error` is now sourced from the **relay's own** OAuth2/SA credential signals (HTTP `401 UNAUTHENTICATED` / `403 PERMISSION_DENIED` on `messages:send`, or a token-mint failure), **not** from `THIRD_PARTY_AUTH_ERROR` — which on this Android-only path is a registration anomaly, not a relay-credential problem (see the notes above and §7.5).
|
||||
|
||||
### 7.5 Reconciliations with the older `03` contract
|
||||
|
||||
- **`Retry-After` on 429.** `03` says "honoring the `Retry-After` response header if present (FCM uses standard HTTP semantics here)". The reference (§2.6/§2.7) refines this: FCM's error page promises only `google.rpc.QuotaFailure` (not `RetryInfo`); treat `Retry-After`/`RetryInfo` as optional and fall back to 60 s. The relay implements the **reference's** refinement while still surfacing a `Retry-After` to the caller exactly as `03` expects.
|
||||
- **No batch endpoint.** The reference (§2.1) confirms FCM's batch endpoint is discontinued; the relay sends **one POST per request** (it already does — one caller request = one device).
|
||||
- **Upstream field casing — `collapseKey`, `priority`, `validateOnly`.** `03` writes the upstream Android fields in snake_case (`collapse_key`) and lowercase priority (`high`/`normal`), and writes the dry-run flag as `validate_only`. The correct **FCM HTTP v1 REST** field names are `android.collapseKey` (camelCase), the canonical `priority` enum `"HIGH"`/`"NORMAL"` (uppercase; lowercase also accepted), and top-level `validateOnly` (camelCase) (reference §2.4/§2.5). The relay uses the **REST-correct names on the upstream wire** while leaving the **inbound caller field `collapse_key` unchanged** per `03`. FCM v1 rejects unknown fields with `INVALID_ARGUMENT`, so this reconciliation is mandatory, not cosmetic.
|
||||
- **FCM 4-distinct-collapse-key-per-device cap.** FCM stores at most **4 distinct collapse keys per device while offline**; a 5th distinct key evicts one nondeterministically (reference §2.7 — a delivery/storage behavior, **not** a per-message validation limit). Because `collapse_key` is a per-series HMAC and a device may follow **more than 4 series**, collapsible coalescing is **best-effort**: excess distinct keys evict nondeterministically and a collapsible wake may be dropped. This is **acceptable** because the durable inbox + sync (`01-release-events-and-inbox.md`) recovers any dropped wake; the relay treats `collapse_key` opaquely and takes no action. **Asymmetry note:** APNs has no equivalent documented per-device `collapse-id` cap, so the FCM path alone carries this best-effort caveat.
|
||||
- **External contract unchanged.** None of the above alters the `03` request/response shapes; only the relay's internal upstream behavior is specified more precisely.
|
||||
|
||||
---
|
||||
|
||||
## 8. Account, Key and Allowlist Model
|
||||
|
||||
### 8.1 DB schema (PostgreSQL)
|
||||
|
||||
Goose-style migration. All IDs are ULIDs stored as `text` for consistency with the Silo convention; timestamps are `timestamptz`.
|
||||
|
||||
```sql
|
||||
-- relay_accounts: one per opt-in self-hosted installation.
|
||||
CREATE TABLE relay_accounts (
|
||||
id text PRIMARY KEY, -- ULID
|
||||
name text NOT NULL, -- operator-facing label
|
||||
status text NOT NULL DEFAULT 'active'
|
||||
CHECK (status IN ('active','disabled')),
|
||||
note text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- relay_api_keys: revocable bearer keys (rk_ prefix). Secret never stored.
|
||||
CREATE TABLE relay_api_keys (
|
||||
id text PRIMARY KEY, -- ULID
|
||||
account_id text NOT NULL REFERENCES relay_accounts(id) ON DELETE CASCADE,
|
||||
key_prefix text NOT NULL, -- non-secret, indexed lookup handle (e.g. rk_live_3Qw9XYZ)
|
||||
key_hash bytea NOT NULL, -- HMAC-SHA256(pepper, secret), 32 bytes
|
||||
env text NOT NULL DEFAULT 'live'
|
||||
CHECK (env IN ('live','test')),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
last_used_at timestamptz, -- throttled write (<= 1/min)
|
||||
expires_at timestamptz, -- optional
|
||||
revoked_at timestamptz -- non-null = revoked
|
||||
);
|
||||
CREATE UNIQUE INDEX relay_api_keys_prefix_uidx ON relay_api_keys (key_prefix);
|
||||
CREATE INDEX relay_api_keys_account_idx ON relay_api_keys (account_id);
|
||||
|
||||
-- relay_apns_allowlist: which APNs topics an account may push to.
|
||||
CREATE TABLE relay_apns_allowlist (
|
||||
account_id text NOT NULL REFERENCES relay_accounts(id) ON DELETE CASCADE,
|
||||
topic text NOT NULL, -- e.g. com.continuum.app.ios
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (account_id, topic)
|
||||
);
|
||||
|
||||
-- relay_fcm_allowlist: which (project, package) pairs an account may push to.
|
||||
CREATE TABLE relay_fcm_allowlist (
|
||||
account_id text NOT NULL REFERENCES relay_accounts(id) ON DELETE CASCADE,
|
||||
project_id text NOT NULL, -- e.g. continuum-prod-android
|
||||
package_name text NOT NULL, -- e.g. com.continuum.app.android
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (account_id, project_id, package_name)
|
||||
);
|
||||
|
||||
-- relay_op_logs: redacted operational/audit log. No tokens, no content.
|
||||
CREATE TABLE relay_op_logs (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
occurred_at timestamptz NOT NULL DEFAULT now(),
|
||||
account_id text REFERENCES relay_accounts(id) ON DELETE SET NULL,
|
||||
request_id text, -- ULID echoed to caller
|
||||
event text NOT NULL, -- e.g. send.apns, send.fcm, auth.reject, admin.key_issue
|
||||
provider text, -- 'apns' | 'fcm' | null
|
||||
outcome text, -- 'accepted' | 'rejected' | 'upstream_error' | ...
|
||||
status_code int, -- caller-facing HTTP status
|
||||
error_code text, -- caller-facing error.code
|
||||
upstream_reason text, -- APNs reason / FCM status (non-sensitive)
|
||||
token_hash text, -- SHA-256(token) hex, NOT the raw token
|
||||
egress_ip inet, -- caller egress IP
|
||||
latency_ms int
|
||||
);
|
||||
CREATE INDEX relay_op_logs_account_time_idx ON relay_op_logs (account_id, occurred_at DESC);
|
||||
CREATE INDEX relay_op_logs_time_idx ON relay_op_logs (occurred_at DESC);
|
||||
```
|
||||
|
||||
Notes:
|
||||
- `relay_op_logs` is the only table written on the hot path besides the throttled `last_used_at`. It carries **no raw token, no payload, no identity** — only a `token_hash` (SHA-256 hex) for correlating repeated failures to one device without storing the token (reference §4.6). Retention: see §13.
|
||||
- No table stores APNs/FCM device tokens, notification content, user/profile IDs, or token aliases — by design (`00` "Its database holds only…").
|
||||
|
||||
### 8.2 API-key format, hashing, lookup, compare
|
||||
|
||||
- **Format:** `rk_<env>_<random>`. The **prefix** = `rk_<env>_<first 6–8 chars of the random>` is **non-secret**, stored in cleartext, and uniquely indexed (`key_prefix`). The full random part is 32 bytes from `crypto/rand`, base62/base64url-encoded. GitHub/Stripe-style design — lets a request `SELECT` by indexed prefix in O(1), then constant-time-compare (reference §4.1).
|
||||
- **Hashing:** store `HMAC-SHA256(pepper, secret)` (32 bytes) where `pepper` is a server-side secret in the secret manager. A fast keyed hash is appropriate for **high-entropy 256-bit random keys** — OWASP's slow-KDF (argon2id) guidance is scoped to low-entropy passwords and would needlessly throttle per-request auth (reference §4.1). Never store plaintext.
|
||||
- **Lookup + compare on each request (constant-time regardless of whether the prefix exists):**
|
||||
1. Parse `Authorization: Bearer rk_<env>_<rest>`; reject malformed → `401 unauthorized`.
|
||||
2. Derive `key_prefix` from the token; `SELECT … FROM relay_api_keys WHERE key_prefix = $1` (unique index).
|
||||
3. If no row, or `revoked_at IS NOT NULL`, or `expires_at < now()`, or the account is `disabled` → still perform a **dummy `HMAC-SHA256` against a fixed decoy hash with `crypto/subtle.ConstantTimeCompare`**, then return the uniform `401 unauthorized` (do not distinguish reasons to the caller). **Do not short-circuit before the HMAC+compare** — an unknown prefix must take the same time as a known prefix with a wrong secret, or the timing difference is a key-enumeration oracle (a known/live prefix would otherwise reject measurably faster). The fine-grained `reason` (`unknown_prefix`/`hash_mismatch`/…) is kept **only in internal logs and the internal-only `/metrics`** (§13.2), never surfaced to a tenant or an untrusted scraper.
|
||||
4. Compute `HMAC-SHA256(pepper, presented_secret)` and `crypto/subtle.ConstantTimeCompare` against `key_hash` (or the decoy). Mismatch → `401`. Never use `==`/`bytes.Equal` on secret material (reference §4.1).
|
||||
5. Throttle-update `last_used_at` (at most once per minute per key). **The throttle clock lives in Redis** (`lastused:{key_prefix}` set `NX EX 60`) so the "≤1/min/key" bound holds **cross-replica** (a per-instance clock would allow N writes/min/key across N replicas); only when that `NX` succeeds does the relay issue the Postgres `UPDATE`. The Redis key carries no secret. This avoids a Postgres write per request (reference §4.1) without leaking a per-key validity side channel through DB write cadence.
|
||||
- **Rotation:** multiple live keys per account are supported (issue a new key, deploy it on the server, then revoke the old) for zero-downtime rotation (reference §4.1).
|
||||
- **Client-IP resolution (trusted-proxy requirement).** The relay runs **behind a TLS load balancer** (§4.2, §14.1), so Go's `http.Request.RemoteAddr` is the LB/proxy address, not the caller. The relay derives the client IP as follows: **use `RemoteAddr` unless the immediate peer is in the allowlisted trusted-LB CIDR set, in which case take exactly the right-most untrusted hop from the LB-set forwarding header (`X-Forwarded-For`)**. **Caller-supplied forwarding headers are ignored** — a caller cannot set `X-Forwarded-For`/`X-Real-IP` to forge an IP. The trusted-proxy CIDR list is a **config value**. Both the per-IP auth-failure limiter and the op-log `egress_ip` consume this single resolved IP. (Without this, a caller could either spread brute-force across unlimited fake IPs to defeat the per-IP throttle (OWASP API2:2023) and poison `egress_ip`, or — if `RemoteAddr` were trusted blindly behind the LB — collapse every tenant onto one IP and trip the throttle for all at once.)
|
||||
- **Anti-brute-force — per-IP auth-failure limiter (specified).** Auth failures are rate-limited **per resolved client IP** (independent of the per-account bucket) to blunt key-guessing (OWASP API2:2023, reference §4.1). Concretely:
|
||||
- **Store:** Redis, key `authfail:{client_ip}`, sliding/fixed window with `EXPIRE`.
|
||||
- **Window / threshold:** e.g. **20 failures / 5 min** per IP (tunable).
|
||||
- **Response on breach — *soft* lockout, not a hard block:** added latency / a `429` with `Retry-After` (e.g. 60 s backoff) rather than dropping the connection. This is deliberate because the doc anticipates **VPN/CGNAT shared egress** (§3.4): multiple self-hosted servers can share one egress IP, so a hard per-IP block would be collateral damage from a noisy/compromised neighbor. Optionally scope tracking by `(key_prefix, IP)` to further isolate neighbors.
|
||||
- **Fail-open/closed stance:** this limiter **fails open** if Redis is unreachable (availability over a perfectly-enforced anti-guessing cap), logging `authfail_limiter.degraded`. The hard auth boundary (HMAC compare, allowlist) is always enforced from Postgres regardless.
|
||||
- **Metric + runbook:** `relay_auth_failures_total{reason=…}` (internal only) plus a documented response runbook for the key-guessing alert (R8, §6.4 monitoring): inspect the offending IP/prefix, confirm whether a single account or a guessing pattern, revoke/rotate as needed.
|
||||
|
||||
### 8.3 The `rk_` token is shown exactly once
|
||||
|
||||
`relayctl key issue` prints the full `rk_…` token to stdout **once**; only the HMAC hash and prefix are persisted. There is no endpoint or command that can recover a secret. Lost keys are revoked and re-issued.
|
||||
|
||||
### 8.4 Allowlist enforcement
|
||||
|
||||
Enforced **after** auth, **before** building the upstream payload:
|
||||
|
||||
- **APNs (`/v1/apple/send`):** the request `topic` must exist in `relay_apns_allowlist` for the caller's `account_id`. Not present → `403 topic_not_allowed`. The official build config allowlist (allowlist config, **not** part of the contract) is the set `com.continuum.app.ios`, `com.continuum.app.tvos`, `com.continuum.app.macos` (`02` resolved-questions: per-platform topics).
|
||||
- **FCM (`/v1/fcm/send`):** the `(project_id, package_name)` pair must exist in `relay_fcm_allowlist` for the caller's `account_id`. Missing project → `403 project_not_allowed`; project present but package not → `403 package_not_allowed`. Official build config: project `continuum-prod-android`, package `com.continuum.app.android`.
|
||||
- Allowlists are loaded per request from Postgres (cheap, small) or from a short-TTL in-memory cache keyed by `account_id` (invalidated on `relayctl allowlist … set`). The cache must be per-account-scoped so one account's allowlist can never authorize another's push.
|
||||
|
||||
These are **allowlist config values**, set by `relayctl`, not part of the external wire contract — a different deployment (e.g., a fork) would set different topics/packages.
|
||||
|
||||
> **Environment ⇄ token consistency (operational caveat).** The allowlist does **not** (and cannot) verify that the request `environment` matches the environment the device `token` was minted for. A `sandbox`-vs-`production` mixup presents downstream as APNs `BadDeviceToken`/`InvalidToken` → `422 bad_device_token` (§6.4), which would otherwise tell the caller to purge a perfectly good token. To let operators catch a **systemic** misconfiguration, the relay emits a distinct op-log/metric tag for these so a burst of `bad_device_token` concentrated on **one account** (likely an env mixup) is distinguishable from scattered genuinely-dead tokens across many accounts. The cross-field rule is documented in §5.1 and §6.4.
|
||||
|
||||
---
|
||||
|
||||
## 9. Rate Limiting and Quotas
|
||||
|
||||
### 9.1 Model
|
||||
|
||||
Per-account **token bucket** enforced in Redis with an atomic Lua script (refill → check → consume in one `EVAL`), state stored as a HASH `rl:{account_id} = {tokens, last_refill_ts}` with a TTL for idle-account eviction (reference §4.2). A Redis-backed limiter (not per-instance `x/time/rate`) is required because the relay runs multiple replicas behind a load balancer — per-instance limiters would not enforce a global cap.
|
||||
|
||||
A second, coarser limiter keys on `(account_id, token_hash)` to blunt hammering a single device (the `02`/`03` "rate limited by API key and coarse token hash" requirement).
|
||||
|
||||
**Redis operational requirements (eviction, HA, sizing).** Redis backs both idempotency (fail-closed) and rate limiting (fail-open), so its configuration is correctness-relevant, not just performance:
|
||||
|
||||
- **Non-evicting policy for idempotency.** The idempotency keyspace **must** run under `maxmemory-policy noeviction` (or live in a **dedicated Redis/db** separate from the rate-limit keyspace) so that memory pressure can **never silently evict an in-flight idempotency lock** — an evicted lock would silently violate at-most-once (a concurrent retry would get `NX`-success and double-send). Rate-limit keys may be evicted harmlessly; idempotency keys must not. This is called out in risks R4/R5.
|
||||
- **HA topology.** Run Redis with an HA topology (Sentinel or Cluster) with prompt failover. A Redis failover/restart makes **every** send `503` while idempotency fails closed (§10) — a push-delaying, not push-losing, event (the caller outbox retries), **but only as long as idempotency stays fail-closed**. A Redis outage should **page ops**.
|
||||
- **Keyspace sizing.** Size the idempotency keyspace from the quota math: ~`50k/day/account × N accounts`, each entry holding `{status, body, upstream_id, canonical_hash}` under a ~24 h TTL. Provision `maxmemory` with headroom above that working set so `noeviction` never starts rejecting writes.
|
||||
|
||||
### 9.2 Suggested limits (ties to `00` open question #1)
|
||||
|
||||
| Limit | Default | Source / rationale |
|
||||
|---|---|---|
|
||||
| Per-account burst | **~10 req/s** | `00` open question #1 ("Suggest 10 req/sec burst") |
|
||||
| Per-account daily quota | **50,000 req/day** | `00` open question #1 ("50,000 req/day initial quota") |
|
||||
| Bucket refill rate | 10 tokens/s, burst capacity 20 | derived from the 10 req/s target with a small burst allowance |
|
||||
| Per-(account, token) coarse cap | e.g. 1 req/3 s sustained | abuse guard on a single device |
|
||||
|
||||
These are tunable per account (future column on `relay_accounts`); v1 ships a single global default and tunes from logs (`00`). The Silo server already paces its calls client-side (default 5 req/s shared token bucket — `02`/`03` "Relay pacing"), so a well-behaved caller stays comfortably under the 10 req/s burst; the relay limit is the backstop against a misbehaving or compromised caller (OWASP API4:2023 — cap per-tenant downstream spend).
|
||||
|
||||
**Daily-quota mechanics (store, reset, fail-open).** The per-second token bucket (§9.1) does **not** model the 50,000 req/day cap; the daily counter is a **separate Redis counter** keyed `rl:day:{account_id}:{yyyymmdd}` with an `EXPIRE` past the day boundary, incremented atomically per accepted send:
|
||||
|
||||
- **Reset semantics:** a **fixed UTC calendar day** (the `{yyyymmdd}` is UTC), not a rolling 24 h — simple, predictable for operators, and the key self-expires.
|
||||
- **Breach:** when the counter reaches 50,000 the account's sends return `429 rate_limited` for the rest of the UTC day with a `Retry-After` = seconds until 00:00 UTC. Daily exhaustion blocks **all** of an account's pushes until reset — a notable availability event, so it is **alerted** (see below), not silent.
|
||||
- **Fail-open during a Redis outage:** like the per-second bucket, the daily counter **fails open** if Redis is unreachable — but this is exactly the abuse-amplification window, so it is **bounded** by the in-process fallback limiter in §9.4 (a compromised key cannot burn unlimited upstream sends during the outage) and the outage pages ops. The daily quota **cannot be enforced cross-replica while Redis is down**; that limitation is explicit.
|
||||
- **Alerting:** a metric/alert fires for accounts **approaching** (e.g. ≥80%) or **hitting** the daily cap, since a legitimate hundreds-of-users server can plausibly hit 50k/day on a big release burst — operators should see this and raise the cap rather than have the account silently throttle. Reconcile the 50k/day default against the `02`/`03` burst-cap math when tuning.
|
||||
|
||||
### 9.3 429 response
|
||||
|
||||
On limit exceeded the relay returns:
|
||||
|
||||
```http
|
||||
HTTP/2 429
|
||||
Retry-After: 3
|
||||
Content-Type: application/json
|
||||
X-Request-Id: 01JRELAY...
|
||||
|
||||
{ "error": { "code": "rate_limited",
|
||||
"message": "account rate limit exceeded",
|
||||
"request_id": "01JRELAY..." } }
|
||||
```
|
||||
|
||||
`Retry-After` is a **relative `delay-seconds`** integer (never an absolute Unix epoch — reference §4.2), computed from the bucket's time-to-next-token. The daily-quota breach also returns `429 rate_limited` with a longer `Retry-After` (seconds until the daily window rolls).
|
||||
|
||||
### 9.4 Fail-open vs fail-closed (bounded, not unlimited)
|
||||
|
||||
If Redis is unreachable, the rate-limit middleware **fails open** for availability (a push is more valuable than a perfectly enforced cap), but **bounded** — it does **not** simply remove the cap. When Redis is down the relay falls back to a **conservative per-instance in-process limiter** (`golang.org/x/time/rate`, keyed per `account_id`) that caps each account to a small multiple of its normal rate. This matters because the per-account quota is the primary control bounding a **stolen-key** attacker's spend and per-tenant downstream APNs/FCM cost (OWASP API4:2023, §3.5 "Stolen relay API key", G6): an unbounded fail-open would let a compromised `rk_` key send without limit against the official Apple/Google credentials — including burning the shared FCM project quota (§9.5) common to all tenants — for the entire outage. The **allowlist (always enforced from Postgres)** remains the hard boundary. A `rate_limit.degraded` op-log + metric fires; the **daily quota cannot be enforced cross-replica** during the outage, and an outage **pages ops**. The idempotency layer (§10), by contrast, **fails closed** to avoid double-sends. (Reference §4.2 — explicitly a per-endpoint decision.)
|
||||
|
||||
### 9.5 Shared FCM project quota — global limiter / circuit breaker
|
||||
|
||||
The per-account limits above are **local to each account**, but the **FCM project quota (~600k messages/min, reference §2.7) is SHARED across ALL relay accounts** sending to `continuum-prod-android`. Without a relay-side guard, one noisy/compromised account can exhaust the shared project quota and every other account's sends start returning `429 QUOTA_EXCEEDED` — a **multi-tenant fairness** failure (one noisy tenant degrades all). The relay therefore adds a **global (per-project) outbound limiter / circuit breaker** at the aggregation point — the only place that *can* see aggregate FCM rate across accounts:
|
||||
|
||||
- **(a) Track aggregate FCM send rate** against the documented project quota (a Redis counter keyed per project, e.g. `fcmq:{project_id}:{yyyymmddhhmm}` with a per-minute window).
|
||||
- **(b) Shed load before hammering FCM:** when near the quota, return `503 upstream_throttled` (retryable) **before** forwarding, rather than spending quota on calls that FCM will reject anyway (some errors still count against quota).
|
||||
- **(c) Anti-spike smoothing (optional):** honor the reference §2.7 guidance to avoid concentrating traffic in the 2-minute windows around `:00/:15/:30/:45` — the relay, as the cross-account aggregation point, is the only component that can actually implement this ramp.
|
||||
- **Observability:** a `relay_fcm_project_quota_headroom` gauge plus an alert when headroom is low. This is explicitly a **multi-tenant fairness** control (a single account must not exhaust the shared project quota for all).
|
||||
|
||||
---
|
||||
|
||||
## 10. Idempotency
|
||||
|
||||
### 10.1 Key format
|
||||
|
||||
`Idempotency-Key: <notification_delivery_id>:<push_device_id>:<attempt_number>` (`02`/`03`). This is **scoped per account** in storage (so two accounts cannot collide). The relay validates the header is present and non-empty on send endpoints; missing → `400 missing_idempotency_key`. The three-part shape is not strictly parsed (it is opaque to the relay), but it is length-limited (≤ 255 chars, Stripe-style — reference §4.3).
|
||||
|
||||
### 10.2 Store, TTL, locking, duplicate semantics
|
||||
|
||||
Redis, namespaced `idem:{account_id}:{key}` (reference §4.3):
|
||||
|
||||
1. `SET idem:{account}:{key} <in_flight_marker> NX EX <lock_ttl>` (`lock_ttl` = 30 s). The `<in_flight_marker>` carries a **unique per-attempt nonce** so the completing request can prove ownership (see invariant below).
|
||||
2. **NX success** → this is the first request. Process it; on completion, **compare-and-set**: overwrite the key with the serialized `{status, body, upstream_id, canonical_hash}` and a **~24 h TTL** **only if the stored value is still this request's own in-flight marker** (i.e. the lock was not stolen by a concurrent request after a TTL expiry). Success *and* error are both recorded (reference §4.3 / Stripe model).
|
||||
3. **NX fail + stored completed result** → **replay** it: return the original status code and body verbatim (so a retried send returns the same `apns_id`/`fcm_message_name`, never a second upstream send).
|
||||
4. **NX fail + still the in-flight marker** → the original is mid-flight → **`409 conflict`** (`{"error":{"code":"idempotency_conflict",…}}`) so the caller retries later.
|
||||
5. **Same key, different request payload** → **`422 unprocessable`** (`idempotency_key_reuse`). The relay stores a `canonical_hash` of the request alongside the result and compares (reference §4.3). **The canonical hash is computed over the validated fields with the device `token` replaced by its already-computed `token_hash` (SHA-256) — the raw token is NEVER part of the canonical form.** The stored idempotency value contains only `{status, body, upstream_id, canonical_hash}`; it holds **no raw token** (matching the op-log guarantee §12, NG1). This keeps the "no token aliases / stateless v1" privacy guarantee intact even though the value lives in Redis under `idem:{account}:{key}` for ~24 h.
|
||||
|
||||
> **Lock-TTL invariant (no double-send, no result-clobber).** The upstream APNs/FCM call **must run under a hard context deadline strictly shorter than `lock_ttl`** — e.g. **upstream timeout 10 s, `lock_ttl` 30 s** — and the handler **must refuse to send upstream if its own deadline would exceed the lock TTL**. Otherwise a slow first attempt (HTTP/2 setup + JWT/OAuth refresh + GOAWAY-reconnect can stack up) could outlive the 30 s lock; the lock would expire mid-flight, a concurrent retry of the same `Idempotency-Key` would get `NX`-success and fire a **second** upstream send (defeating at-most-once), and when the slow first attempt finally completed it would clobber the stored result with a different `apns_id`. The **compare-and-set on completion** (step 2) prevents the clobber — a lock that was stolen by a concurrent request cannot have its result overwritten by the original — and the **deadline-shorter-than-TTL** rule prevents the double-send in the first place. (Optionally, a lock heartbeat may renew the marker while genuinely in flight, but the deadline rule is the primary guarantee.) This invariant is the whole reason the idempotency store exists (R4).
|
||||
|
||||
This guarantees **at-most-once upstream send** under the caller's concurrent retries (the `../01-release-events-and-inbox.md` outbox can re-claim and re-dispatch the same `(delivery_id, device_id, attempt_number)`), which is exactly why the key includes `attempt_number`: attempt 2 is a *new* idempotency key (intended re-send after a failure), while two concurrent dispatches of attempt 1 collapse to one upstream call.
|
||||
|
||||
### 10.3 What a duplicate returns (summary)
|
||||
|
||||
| Situation | Response |
|
||||
|---|---|
|
||||
| First request | Process; record `{status, body, upstream_id, canonical_hash}` (no raw token — §10.2) for ~24 h. |
|
||||
| Duplicate, original completed | Replay stored status + body (same `apns_id`/`fcm_message_name`). |
|
||||
| Duplicate, original in-flight | `409 idempotency_conflict`. |
|
||||
| Same key, different payload | `422 idempotency_key_reuse`. |
|
||||
| Missing key | `400 missing_idempotency_key`. |
|
||||
|
||||
Idempotency **fails closed**: if Redis is unavailable, send endpoints return `503 upstream_unavailable` (retryable) rather than risk a double-send (contrast §9.4).
|
||||
|
||||
---
|
||||
|
||||
## 11. Security Requirements
|
||||
|
||||
- **TLS only.** Public endpoints are HTTPS over HTTP/2; no plaintext listener. TLS 1.2+ (prefer 1.3). HSTS on the public hostname. (`02`/`03` "Relay API uses TLS only".)
|
||||
- **Secret management.** APNs `.p8`, FCM SA JSON, the API-key HMAC pepper, and DB/Redis credentials live in a **secret manager** (KMS/Vault/cloud secret-manager). Ranking: secret-manager > tightly-permissioned mounted file > env var (**discouraged**) > in-image/repo (**forbidden**) (reference §4.5). On GCP, prefer keyless Workload Identity for FCM and avoid a downloadable SA JSON entirely (reference §4.5). Never commit or bake credentials into the image.
|
||||
- **Key storage.** API keys stored only as `HMAC-SHA256(pepper, secret)` + non-secret prefix; constant-time compare; revocable; optional expiry; multiple live keys per account for rotation (§8.2). The relay's own `rk_` secret is shown once and never recoverable.
|
||||
- **No raw secrets in transit out.** No endpoint returns the `.p8`, SA JSON, pepper, or any `rk_` secret. `/metrics` and op-logs carry none.
|
||||
- **Redaction (enforced in middleware so it cannot be bypassed).** Never log: `Authorization`/bearer header, raw API key (log only the non-secret prefix or a hash), raw APNs/FCM device tokens (log `SHA-256` truncated hash only), notification payload bodies, the relay JWT, or OAuth2 access tokens (reference §4.6; `02`/`03` privacy/security requirements). See §12/§13.
|
||||
- **Isolation / multi-tenancy.** Every API key, rate-limit bucket, idempotency key, allowlist cache entry, and log line is scoped by `account_id`. Shared-store keys are namespaced (`idem:{account}:…`, `rl:{account}`) to prevent cross-tenant collision or replay (reference §4.7). For APNs, connection pools are per-team (§6.1).
|
||||
- **Network posture.** `/metrics` and `/readyz` bound to the internal/scrape network; only `/v1/*` and `/healthz` are public. `relayctl` reaches Postgres over a private network with a privileged DSN held by operators only — there is no public admin API (§5.6).
|
||||
- **Input hardening.** `DisallowUnknownFields`, body size cap (e.g. 16 KiB — far above the ~1 KB legitimate body), explicit server timeouts (`ReadHeaderTimeout`, `ReadTimeout`, `WriteTimeout`, `IdleTimeout`, `MaxHeaderBytes`) since the zero value means no timeout (reference §3.3). Graceful shutdown via `srv.Shutdown(ctx)` on SIGTERM.
|
||||
- **Egress / SSRF posture.** Both upstream hosts are **hardcoded constants** — `api.push.apple.com` / `api.sandbox.push.apple.com` (APNs) and `fcm.googleapis.com` (FCM) — **never derived from any request field**. The only request-derived value interpolated into an upstream URL is the FCM `project_id` (into `/v1/projects/{project_id}/messages:send`); it is **allowlist-checked** (§8.4) **and** validated against a strict charset **before** allowlist lookup and URL construction: `project_id` and `package_name` against `^[a-z0-9-]+$` / reverse-DNS, `topic` against the APNs reverse-DNS shape. This ensures an allowlist row can never introduce a path-traversal/host-override and the host can never be templated from caller input (SSRF/path-injection prevention).
|
||||
- **Anti-brute-force** on auth failures per resolved client IP, with trusted-proxy IP resolution (§8.2).
|
||||
|
||||
---
|
||||
|
||||
## 12. Privacy Requirements
|
||||
|
||||
The relay's privacy guarantee is **structural**, not policy-based: the request schema has no field that can carry content, and unknown fields are rejected.
|
||||
|
||||
- **Content-free guarantee.** The send-endpoint structs contain only the fields in §5.1/§5.2. There is **no** title, body, image URL, media ID, username, server hostname, server URL, library/collection/item name, or watched-state field (`02`/`03` privacy requirements). The relay-built payloads use only generic localization keys (`SILO_NOTIFICATION_TITLE` / `SILO_NOTIFICATION_GENERIC_BODY`) and opaque IDs.
|
||||
- **Reject any unexpected field.** JSON decoding uses `DisallowUnknownFields`; any field not in the schema → `400 unexpected_field`. This is the enforcement point for the content-free guarantee — a caller (buggy or hostile) cannot smuggle content through an extra key.
|
||||
- **Badge disabled by default.** `badge` is omitted unless explicitly provided, so unread counts don't leak through APNs/FCM by default (`02`/`03`).
|
||||
- **FCM is data-only with a closed key set.** No top-level `notification` block is ever constructed, so Google carries no rendering content (`03`). The `message.data` map is a **fixed, closed set of keys** — exactly `{v, wake, server_device_id, delivery_id}` — with **no caller-controlled key or value** beyond the already-opaque IDs (§5.2). `badge` is **never** injected into `data` (it has no data-only home and is ignored on the FCM path — §5.2), so the data-map allowlist cannot be widened by a provided badge or any extra key.
|
||||
- **No raw token in any store.** Neither the op-log nor the idempotency store holds a raw device token: the op-log carries only `token_hash` (SHA-256), and the idempotency value's `canonical_hash` is computed with the token replaced by `token_hash` (§10.2). The stateless-v1 "no token aliases" guarantee (NG1) holds across both stores.
|
||||
- **Redacted logging.** Op-logs and structured logs **never** contain: raw device tokens (only `SHA-256` hash), notification content (none exists), the `Authorization` header, the relay JWT, OAuth2 tokens, or the raw API key (only the prefix). Full request payloads are never persisted (`02`/`03` "Relay logs do not persist full request payloads"). What **is** logged for triage: `request_id`, `account_id`, status code, caller `error.code`, upstream reason, `token_hash`, egress IP, latency (reference §4.6).
|
||||
- **No identity storage.** The relay stores no user/profile identity, no device→user mapping, no server URL, and no token aliases (`00`).
|
||||
|
||||
---
|
||||
|
||||
## 13. Observability
|
||||
|
||||
### 13.1 Structured logs (with redaction)
|
||||
|
||||
`log/slog` JSON handler (reference §3.5). Every request emits one structured line through a redaction middleware that strips the disallowed fields (§12). Example (a rejected, allowlist-failed APNs send):
|
||||
|
||||
```json
|
||||
{
|
||||
"time": "2026-06-13T10:00:00Z",
|
||||
"level": "WARN",
|
||||
"msg": "send.apns rejected",
|
||||
"request_id": "01JRELAY...",
|
||||
"account_id": "01JACCOUNT...",
|
||||
"provider": "apns",
|
||||
"event": "send.apns",
|
||||
"outcome": "rejected",
|
||||
"status_code": 403,
|
||||
"error_code": "topic_not_allowed",
|
||||
"token_hash": "9f86d081...c2b5",
|
||||
"egress_ip": "203.0.113.7",
|
||||
"latency_ms": 2
|
||||
}
|
||||
```
|
||||
|
||||
Redaction is implemented in middleware so no code path can log a raw token, the bearer header, the JWT, or an OAuth2 token. The redacted audit subset also lands in `relay_op_logs` (§8.1) for queryable history. **Op-log retention:** successful sends **>14 days** are deleted; failures/upstream errors **>90 days** are deleted (mirrors the `02`/`03` `push_delivery_attempts` retention guidance). At 50k/day/account this table grows unbounded without active GC, so retention is a **shipped deliverable, not a deferral** — enforced by a scheduled sweeper / `relayctl logs --gc` (specified in `01-implementation-plan.md` Phase 5), with a disk-usage / table-growth alert. Time-partitioning of `relay_op_logs` is the recommended store strategy; see §15 #8.
|
||||
|
||||
### 13.2 Metrics catalog (Prometheus, `/metrics`)
|
||||
|
||||
`prometheus/client_golang` (reference §3.5). Labels avoid high-cardinality fields (no token, no `request_id`).
|
||||
|
||||
| Metric | Type | Labels | Meaning |
|
||||
|---|---|---|---|
|
||||
| `relay_requests_total` | counter | `provider`, `endpoint`, `status_code`, `error_code` | All inbound send requests by outcome. |
|
||||
| `relay_request_duration_seconds` | histogram | `provider`, `endpoint` | End-to-end relay latency. |
|
||||
| `relay_upstream_requests_total` | counter | `provider`, `upstream_status`, `upstream_reason` | APNs/FCM calls by upstream result. |
|
||||
| `relay_upstream_duration_seconds` | histogram | `provider` | Upstream call latency. |
|
||||
| `relay_auth_failures_total` | counter | `reason` (`bad_format`,`unknown_prefix`,`hash_mismatch`,`revoked`,`expired`,`account_disabled`) | Auth rejections. **`/metrics` is internal/scrape-only** — the `reason` split (which distinguishes `unknown_prefix` from `hash_mismatch`) must **never** be exposed to a tenant or untrusted scraper, or it becomes a key-enumeration oracle paired with the constant-time auth path (§8.2). |
|
||||
| `relay_rate_limited_total` | counter | `account_id`(low-card, hashed/bucketed if needed), `kind` (`account`,`token`) | 429s emitted. |
|
||||
| `relay_rate_limit_degraded_total` | counter | — | Fail-open events when Redis is down (§9.4). |
|
||||
| `relay_idempotency_total` | counter | `result` (`first`,`replay`,`conflict`,`reuse`) | Idempotency decisions. |
|
||||
| `relay_apns_jwt_refresh_total` | counter | `result` (`scheduled`,`expired_retry`,`throttled`) | JWT lifecycle (§6.1). |
|
||||
| `relay_fcm_token_refresh_total` | counter | `result` | OAuth2 token refreshes (§7.1). |
|
||||
| `relay_redis_up` / `relay_pg_up` | gauge | — | Core dependency health (drives `/readyz` — §5.4). |
|
||||
| `relay_provider_healthy` | gauge | `provider` | Per-provider init/circuit-breaker health (`1` healthy / `0` degraded) — does **not** gate `/readyz` (§5.4). |
|
||||
| `relay_provider_credential_healthy` | gauge | `provider` | Synthetic credential-validation result (§13.4); `0` triggers the credential alert. |
|
||||
| `relay_fcm_project_quota_headroom` | gauge | `project_id` | Remaining headroom against the shared FCM project quota (§9.5). |
|
||||
| `relay_daily_quota_used_ratio` | gauge | `account_id`(low-card/bucketed) | Fraction of the 50k/day cap consumed; alerts ≥80% (§9.2). |
|
||||
| `relay_connection_severed_total` | counter | `provider` | Sends mapped to `503` by GOAWAY/connection reset before a final response (§6.4). |
|
||||
| `relay_authfail_limiter_degraded_total` | counter | — | Per-IP auth-failure limiter fail-open events (§8.2). |
|
||||
|
||||
### 13.3 Admin diagnostics
|
||||
|
||||
`relayctl` provides read paths for operators (no raw secrets/tokens): `account list`, `key list`, `allowlist show`, and a `relayctl logs --account <id> [--since …] [--outcome rejected]` that queries `relay_op_logs` (redacted by construction). A `relayctl ping-upstream --provider apns|fcm --env sandbox` performs a credential-only health check (JWT sign / token mint, optionally an FCM `validateOnly:true` send) without sending real content — useful after a credential rotation. The same check captures the sandbox `apns-unique-id` (§5.1) when APNs returns one.
|
||||
|
||||
### 13.4 Credential health, validation and rotation alerting
|
||||
|
||||
Credentials can be revoked or rotated **out from under the relay** at any time — Apple "actively monitors and revokes leaked `.p8` keys" (`00`), and FCM SA keys / Workload-Identity bindings can be disabled. Relying only on production sends starting to fail (upstream `403 InvalidProviderToken` / the relay's own `401 UNAUTHENTICATED` / `403 PERMISSION_DENIED`) is too late. The relay therefore validates credentials **proactively**, distinguishing an **auth-layer** failure from a mere token-layer rejection:
|
||||
|
||||
- **Startup + periodic synthetic validation.** On startup and on a periodic timer, the relay exercises the auth layer end-to-end: an FCM `validateOnly:true` send, and an APNs send to a **known-bad token** that still forces APNs to authenticate the JWT (so an auth-layer `403 InvalidProviderToken`/`BadCertificate*` is distinguishable from a token-layer `400 BadDeviceToken` — the latter proves the credential is *accepted*). Note `/readyz` alone is **insufficient**: it only proves a JWT was **signed locally** / a token **minted**, not that APNs/FCM **accept** the credential.
|
||||
- **`credential healthy` gauge.** Wire the synthetic-validation result into a per-provider `relay_provider_credential_healthy` gauge (§13.2) and surface it on the per-provider readiness signal (§5.4).
|
||||
- **Continuous health via cron.** Schedule `relayctl ping-upstream` (e.g. a cron / scheduled run) for continuous credential health between restarts.
|
||||
- **Alert on first auth failure.** Alert on the **first** `upstream_auth_error` (the relay's own credential signals — §6.4/§7.4), not after a sustained failure rate, since a revoked credential fails *all* sends immediately.
|
||||
|
||||
---
|
||||
|
||||
## 14. Deployment and Operations
|
||||
|
||||
### 14.1 Public endpoint & TLS
|
||||
|
||||
- One public hostname, `relay.silo.app`, terminating TLS (1.2+, prefer 1.3) at a load balancer or in-process. HTTP/2 end to end. Only `/v1/apple/send`, `/v1/fcm/send`, and `/healthz` are public; `/readyz` and `/metrics` are internal-only.
|
||||
- Set explicit server timeouts and graceful shutdown (§11).
|
||||
|
||||
### 14.2 High availability — "relay-down delays, never loses"
|
||||
|
||||
The relay being down does **not** lose pushes: the self-hosted server's dispatch is an **outbox**. The fanout transaction in `../01-release-events-and-inbox.md` inserts `pending` `push_delivery_attempts` rows in the same transaction as the `notification_deliveries` row; the dispatcher claims them post-commit and a recovery sweeper re-claims stale `pending` rows (`00`/`02`/`03`). So if the relay returns `503`/times out, the caller marks the attempt `retrying` and retries with backoff — the durable inbox row and the outbox attempt row guarantee eventual delivery. **This means the relay can be operated with ordinary HA (multiple stateless replicas, rolling deploys) rather than five-nines durability.** A short relay outage only delays pushes.
|
||||
|
||||
- **Stateless replicas.** No in-memory request state that can't be reconstructed; idempotency and rate-limit state are in Redis, shared across replicas. Scale horizontally behind the LB.
|
||||
- **Rolling deploys / kill-safety.** A replica can be drained (`srv.Shutdown`) or killed mid-request; the caller retries the same idempotency key, which either replays the recorded result or re-sends at-most-once (§10).
|
||||
- **Drain ordering (no orphaned idempotency locks).** A clean drain must not turn into a 30 s stall per retry. On `SIGTERM`: (1) flip `/readyz` to `503` so the LB **stops routing new requests** to this replica; (2) keep serving **in-flight** handlers; (3) bound the `srv.Shutdown` grace period to **less than the idempotency `lock_ttl`** (§10.2) — and/or **release/downgrade the in-flight idempotency marker on handler context-cancellation** — so that a send killed mid-flight does **not** leave a stale 30 s `in_flight` marker in Redis. Otherwise the caller's immediate retry hits §10.2 case 4 and gets `409 idempotency_conflict` for up to `lock_ttl` seconds. The relationship between the shutdown grace period and `lock_ttl` is load-bearing and is asserted by a drain test (no orphaned in-flight markers after a rolling restart — see `01-implementation-plan.md`).
|
||||
|
||||
### 14.3 Secret provisioning
|
||||
|
||||
- Provision the APNs `.p8` (+ Team ID, Key ID) and FCM SA JSON (or Workload Identity binding) into the secret manager out-of-band. The HMAC pepper and DB/Redis creds likewise. Never in the image or repo (§11).
|
||||
- **Credential rotation runbook (overlap window, dual-key).** Apple permits **multiple active `.p8` keys per team**, and the relay already keys connection pools by team (§6.1), so rotation is **make-before-break**: (1) provision the new credential into the secret manager alongside the old; (2) load both — the relay supports an **overlap window** where old and new keys are both valid and the key-selection map (§6.1) can resolve to either; (3) run `relayctl ping-upstream` / the synthetic validation (§13.4) to confirm the **new** credential authenticates against APNs/FCM; (4) cut sends over to the new key (rolling restart picks up the new secret); (5) retire/revoke the old credential only after the new one is confirmed healthy. The same overlap pattern applies to FCM SA keys / Workload-Identity bindings. This avoids the failure mode where a single-key rotation revokes the old credential before the new one is proven, taking all pushes down.
|
||||
|
||||
### 14.4 Sandbox vs production
|
||||
|
||||
- The relay supports both APNs environments via the request `environment` field, routing to `api.push.apple.com` (production) or `api.sandbox.push.apple.com` (sandbox) (§6.2). FCM has a single global host (no sandbox split — reference §2.3); test sends use `validateOnly:true` (reference §2.5).
|
||||
- A **staging relay deployment** (separate hostname, separate accounts) is recommended for client QA so test traffic never touches production accounts/quotas.
|
||||
|
||||
### 14.5 Scaling & regions
|
||||
|
||||
- **Scaling.** One well-tuned APNs HTTP/2 connection handles thousands of pushes/sec (reference §1.3/§3.1); one FCM token source serves the project. The bottleneck at relay scale is inbound throughput and Redis, both horizontally scalable. Keep upstream clients **process-singletons** (one per env/team/project), not per-request.
|
||||
- **Regions.** APNs and FCM are global; FCM has no data-residency send endpoints (reference §2.3). v1 runs a single region close to the operator's Postgres/Redis. Multi-region is possible later (the request path is stateless; Redis/Postgres would need regional strategy) but is out of scope for v1.
|
||||
- **Quota awareness.** FCM's default project quota is ~600k messages/min (reference §2.7); the relay's per-account 50k/day default (§9.2) is far under that even summed across many accounts. The **shared** project quota is nonetheless guarded by the relay-side global limiter / circuit breaker (§9.5) so one noisy account cannot exhaust it for all tenants; operators monitor `relay_fcm_project_quota_headroom` (§13.2), the project quota in Cloud Console, and `relay_upstream_requests_total{upstream_reason="QUOTA_EXCEEDED"}`.
|
||||
|
||||
---
|
||||
|
||||
## 15. Open Questions
|
||||
|
||||
1. **Per-account rate limits — real numbers.** v1 ships the `00`-suggested ~10 req/s burst + 50k/day defaults (§9.2); tune from production logs. Should limits become per-account columns on `relay_accounts` with `relayctl quota set`? (Ties to `00` open question #1.)
|
||||
2. **APNs sandbox hostname (resolved — note only).** `api.sandbox.push.apple.com` and `api.development.push.apple.com` are **interchangeable, both-resolving** Apple sandbox aliases; the relay standardizes on `api.sandbox.push.apple.com` (reference §1.3, reference open question §5 item 4). The external contract (`"environment":"sandbox"`) is unaffected either way. No build-time decision required.
|
||||
3. **`apns2` vs hand-rolled APNs client.** `sideshow/apns2` has had no release since v0.25.0 (Oct 2024) — bus-factor risk. Decide before implementation: adopt `apns2` or hand-roll `net/http2` + `golang-jwt/jwt/v5` (reference §3.1, §5.2).
|
||||
4. **FCM credentials: GCP-keyless vs off-GCP JSON.** If the relay runs on GCP, use Workload Identity / attached SA (no downloadable key); off-GCP, the SA JSON lives in the secret manager. Resolve in the deployment design (reference §4.5 / §2.2).
|
||||
5. **`apns-expiration` / `android.ttl` default values (resolved; tune from product input).** v1 **ships finite defaults** — `apns-expiration` ~4 h for `private_alert` / ~1 h for `background_wake`, and the parallel FCM `android.ttl` (§5.1/§5.2) — rather than the 30-day APNs / 4-week FCM store-and-retry, so stale wakes don't fire days later. The exact durations are configurable and may be tuned with product input (reference §1.4).
|
||||
6. **API-key hashing: HMAC pepper vs plain salted SHA-256.** This spec chooses HMAC-SHA-256 with a secret-manager pepper for defense-in-depth. Pepper rotation is operationally heavier (re-hash all keys or support dual peppers). Confirm the tradeoff (reference §4.1, open question §12).
|
||||
7. **Idempotency-Key spec status.** The IETF draft expired 2026-04-18 without becoming an RFC; the header name + Stripe-style semantics are de-facto standard. Re-check for an RFC before relying on its exact status-code semantics (reference §4.3).
|
||||
8. **Op-log volume & retention.** At 50k/day/account across many accounts, `relay_op_logs` grows unbounded without GC. v1 **ships** the retention sweep (delete success >14 d, failure >90 d — §13.1) as a scheduled `relayctl logs --gc` / built-in sweeper (`01-implementation-plan.md` Phase 5), with a table-growth alert. Open: whether to **time-partition** `relay_op_logs` (recommended) or offload to a log pipeline rather than Postgres at higher volume.
|
||||
9. **Multi-region.** Out of scope for v1 (single region). If latency to distant self-hosters matters, design a regional Redis/Postgres strategy for the otherwise-stateless request path.
|
||||
10. **FCM priority deprioritization.** The relay honors `03`'s `priority: high` for `private_data`. If FCM deprioritizes Silo's app-instance traffic over the 7-day window (reference §2.8), is any relay-side change warranted, or is this purely the caller's `priority: normal` fallback concern? Lean: caller's concern; revisit if metrics show systematic deprioritization.
|
||||
@@ -0,0 +1,738 @@
|
||||
# Silo Push Relay — Implementation Plan
|
||||
|
||||
**Date:** 2026-06-13
|
||||
**Status:** Draft (implementation-grade, phased)
|
||||
**Service:** `silo-push-relay` (provisional repo name; the relay code lives in a **separate repository**)
|
||||
**Provisional hostname:** `relay.silo.app`
|
||||
**Stack:** Go 1.25+, PostgreSQL, Redis
|
||||
|
||||
**Source documents (read before executing any phase):**
|
||||
|
||||
- [`./00-relay-spec.md`](./00-relay-spec.md) — the engineering spec this plan implements. Section numbers below (e.g. "spec §6.4") refer to it.
|
||||
- [`./02-apns-fcm-2026-reference.md`](./02-apns-fcm-2026-reference.md) — 2026-current Apple/Google/Go reference (cited as "reference §N").
|
||||
- [`../02-apns-relay.md`](../02-apns-relay.md) and [`../03-fcm-relay.md`](../03-fcm-relay.md) — the **authoritative external wire contract** (the `02`/`03` request/response shapes). The relay must honor these byte-for-byte.
|
||||
|
||||
> **Commands assume the repository root is the cwd.** This document contains no local absolute filesystem paths or transient worktree IDs; all repo references are repository-relative.
|
||||
|
||||
> **Repo boundary.** This plan describes a **separate repository** (`silo-push-relay`). The design docs are authored here in `silo-server` purely for co-location with the `02`/`03` contracts. The relay **code** lives elsewhere. The one deliberate coupling is the shared `pushwire` payload-builder package, mirrored into both repos (see §3 and Phase 3/4).
|
||||
|
||||
---
|
||||
|
||||
## 1. Overview
|
||||
|
||||
### 1.1 What is being built
|
||||
|
||||
A small, **stateless-on-the-request-path** Go HTTP service that holds the official Silo Apple (`.p8` APNs auth key) and Google (Firebase service-account JSON) push credentials, accepts authenticated **content-free** push requests from opted-in self-hosted Silo servers on two endpoints, builds a fixed generic APNs/FCM payload, forwards it upstream, and maps the upstream result back to a narrow caller-facing response.
|
||||
|
||||
The two send endpoints and their contract are fixed by `02`/`03` and reproduced in spec §5:
|
||||
|
||||
| Endpoint | Purpose | Authoritative contract |
|
||||
|---|---|---|
|
||||
| `POST /v1/apple/send` | Forward one opaque push to APNs for one device token | `../02-apns-relay.md`, spec §5.1 |
|
||||
| `POST /v1/fcm/send` | Forward one opaque data-only push to FCM for one device token | `../03-fcm-relay.md`, spec §5.2 |
|
||||
| `GET /healthz` | Liveness (public) | spec §5.3 |
|
||||
| `GET /readyz` | Readiness incl. PG/Redis core deps + per-provider credential health (internal) | spec §5.4 |
|
||||
| `GET /metrics` | Prometheus exposition (internal) | endpoint spec §5.4; metric catalog §13.2 |
|
||||
|
||||
Administration is a CLI (`relayctl`) that writes directly to the relay's PostgreSQL — **there is no public admin HTTP API** in v1 (spec §5.6).
|
||||
|
||||
### 1.2 The v1 cut line
|
||||
|
||||
**In scope (v1):**
|
||||
|
||||
- `POST /v1/apple/send` and `POST /v1/fcm/send` exactly per `02`/`03`.
|
||||
- Bearer auth (`rk_` keys), per-account allowlists, Redis token-bucket rate limiting, Redis idempotency.
|
||||
- APNs token-based (ES256 `.p8`) client with cached JWT; FCM HTTP v1 client with cached OAuth2 token.
|
||||
- PostgreSQL storage of accounts/keys/allowlists/redacted op-logs only; Goose-style migrations.
|
||||
- `relayctl` admin CLI; structured redacted logs; Prometheus metrics; `/healthz` + `/readyz`; graceful shutdown.
|
||||
- Single region, multiple stateless replicas behind a TLS load balancer.
|
||||
|
||||
**Explicitly out of scope (v1)** — from spec §2.2 / NG1–NG7:
|
||||
|
||||
- Token→user aliases, device subscriptions, any user/profile identity (stateless v1).
|
||||
- Arbitrary APNs/FCM JSON, custom titles/bodies, topic/condition broadcasts, badge-by-default.
|
||||
- Delivery receipts, analytics, open-tracking.
|
||||
- APNs broadcast / Live Activity channels (`/4/broadcasts/...`).
|
||||
- Web Push, HMS, ADM, or any non-APNs/non-FCM transport.
|
||||
- A public self-service signup / billing surface.
|
||||
- The `custom_apns` / `custom_fcm` direct paths (those run inside `silo-server`).
|
||||
- Multi-region (the request path is stateless, but Redis/Postgres regional strategy is deferred).
|
||||
|
||||
### 1.3 The real long pole: provider account provisioning
|
||||
|
||||
**Apple/Firebase account provisioning is the critical-path dependency, not the code.** Obtaining the official Apple Developer team, generating a `.p8` APNs auth key (Key ID + Team ID), registering per-platform bundle topics, standing up the Firebase project(s), and minting a service-account JSON can take days-to-weeks of organizational/approval lead time and is **fully decoupled from coding**.
|
||||
|
||||
> **Action: start the §7 provisioning checklist on day 1, in parallel with Phase 0.** Phases 0–2 (scaffold, storage, auth/rate-limit/idempotency) need **no** live provider credentials and can complete entirely against fakes and contract tests. Only Phase 3 (APNs integration tests against sandbox) and Phase 4 (FCM `validateOnly`) are gated on credentials. If provisioning slips, the code still reaches "everything but live upstream" without blocking.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph Track A — Code
|
||||
P0[Phase 0\nscaffold] --> P1[Phase 1\nstorage] --> P2[Phase 2\nauth/RL/idem]
|
||||
P2 --> P3[Phase 3\nAPNs]
|
||||
P2 --> P4[Phase 4\nFCM]
|
||||
P3 --> P5[Phase 5\nobservability]
|
||||
P4 --> P5
|
||||
P5 --> P6[Phase 6\nharden+deploy]
|
||||
end
|
||||
subgraph Track B — Provisioning (parallel, long pole)
|
||||
A1[Apple team + .p8 + topics]
|
||||
G1[Firebase project + SA JSON + package]
|
||||
end
|
||||
A1 -. gates .-> P3
|
||||
G1 -. gates .-> P4
|
||||
A1 -. gates .-> P6
|
||||
G1 -. gates .-> P6
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Repository Bootstrap
|
||||
|
||||
### 2.1 Go module path
|
||||
|
||||
```
|
||||
module github.com/silo-app/silo-push-relay
|
||||
```
|
||||
|
||||
Target **Go 1.25+** (required by `jackc/pgx v5.10` and `firebase.google.com/go/v4 v4.20`; reference §3 version-targeting note). Pin in `go.mod` with `go 1.25`.
|
||||
|
||||
### 2.2 Directory layout
|
||||
|
||||
```
|
||||
silo-push-relay/
|
||||
├── cmd/
|
||||
│ ├── relay/ # main HTTP service entrypoint (wires config → stores → clients → server)
|
||||
│ │ └── main.go
|
||||
│ └── relayctl/ # admin CLI (account/key/allowlist/logs/ping-upstream); writes directly to PG
|
||||
│ └── main.go
|
||||
├── internal/
|
||||
│ ├── config/ # env + secret-manager config loading, validation, defaults
|
||||
│ ├── httpapi/ # router, handlers, middleware (auth, rate limit, idempotency, redaction), error bodies
|
||||
│ ├── apns/ # APNs upstream client: ES256 JWT lifecycle, HTTP/2 pool, reason→error mapping
|
||||
│ ├── fcm/ # FCM upstream client: OAuth2 token source, HTTP/2 client, status→error mapping
|
||||
│ ├── accounts/ # data-access for accounts/keys/allowlists (shared by httpapi + relayctl)
|
||||
│ ├── ratelimit/ # Redis Lua token-bucket limiter (per-account + coarse per-token)
|
||||
│ ├── idempotency/ # Redis SET NX lock / replay / 409 / 422 store
|
||||
│ ├── pushwire/ # SHARED payload builders (APNs + FCM payload/header construction) — mirrored into silo-server
|
||||
│ ├── oplog/ # redacted op-log writer (PG) + token hashing helper
|
||||
│ ├── observability/ # slog JSON handler + redaction, Prometheus registry/metrics, /healthz /readyz /metrics
|
||||
│ └── store/ # pgxpool + go-redis bootstrapping, health pings, migration runner hook
|
||||
├── migrations/
|
||||
│ └── sql/ # Goose-style timestamped SQL migrations (see §2.6)
|
||||
├── deploy/
|
||||
│ ├── Dockerfile
|
||||
│ └── (k8s / compose manifests as applicable)
|
||||
├── Makefile
|
||||
├── go.mod
|
||||
├── go.sum
|
||||
├── .golangci.yml
|
||||
├── .github/workflows/ci.yml # or equivalent CI config
|
||||
└── README.md
|
||||
```
|
||||
|
||||
Package ownership mirrors the Silo convention (CLAUDE.md "keep new code in the package that owns the behavior"): no catch-all `utils` package; shared payload logic lives in `pushwire`; shared data-access lives in `accounts`/`store` so `relayctl` and `httpapi` share schema and constraints (spec §5.6 "The CLI shares the relay's data-access package").
|
||||
|
||||
### 2.3 The shared `pushwire` package (maintainability win)
|
||||
|
||||
`internal/pushwire` is the single source of truth for **how an opaque request becomes an upstream payload + headers** — the APNs `aps`/`silo` dictionaries and `apns-*` headers (spec §5.1), and the FCM data-only `message` body + `android` config (spec §5.2). It is intentionally **dependency-light** (pure Go structs + JSON, no network) so it can be **mirrored into `silo-server`** under its `custom_apns` / `custom_fcm` paths, where the self-hosted server builds the *same* payloads when an admin uses their own credentials. One package, two repos: the relay and the direct-credential path can never drift on payload shape. (See §3 dependency table and Phase 3/4.)
|
||||
|
||||
> **Mirroring mechanism & sync runbook (drift/ownership hazard — make it explicit).** Keep `pushwire` import-cycle-free and free of relay-only types (no DB, no config). **Designate one repo as the canonical source of truth** for `pushwire` (the relay repo `silo-push-relay`, since it owns the upstream payload contract). The copy in `silo-server` is a **checked-in generated artifact**, not a hand-edited file: commit a **content checksum** of the canonical package alongside it, and have **both** repos' CI verify their local copy against that checksum (a copied package in repo A cannot fail repo B's CI on its own, so a passive golden-file test in one repo is insufficient). Bump a **versioned payload-schema constant** on any payload change so a mismatch is unambiguous. Add a golden-file + closed-allowlist-key contract test on both sides (Phase 3/4 / §5.2). Document the ownership/sync runbook (who regenerates, in what order, how the checksum is bumped) so a payload change cannot silently diverge until the other repo happens to update. The preferred end state is a small shared module both repos import rather than a manual copy.
|
||||
|
||||
### 2.4 Config strategy
|
||||
|
||||
Twelve-factor-ish, but credentials come from a **secret manager**, not env vars (spec §11, reference §4.5 ranking: secret-manager > mounted file > env var > in-image). `internal/config` resolves, in order of preference per secret:
|
||||
|
||||
| Config | Source (preferred → fallback) | Notes |
|
||||
|---|---|---|
|
||||
| APNs `.p8` private key, Key ID, Team ID | secret manager → mounted file | never env var, never in image |
|
||||
| FCM SA JSON **or** Workload Identity binding | GCP attached SA / Workload Identity → secret manager JSON | off-GCP uses JSON; on-GCP keyless (reference §4.5) |
|
||||
| API-key HMAC pepper | secret manager | rotation is heavier (spec open Q#6) |
|
||||
| Postgres DSN, Redis URL | secret manager → env | pool-tuned |
|
||||
| Non-secret tuning (timeouts, rate defaults, listen addrs, log level, environment label) | env vars / flags | safe to log |
|
||||
|
||||
`config.Load(ctx)` returns a validated, fully-populated struct or a hard error; the process refuses to start with a missing/invalid credential. Provide a `config.Validate()` that `cmd/relay` calls before opening any listener. Non-secret defaults (rate limits from spec §9.2, timeouts from spec §11) live as constants with env overrides.
|
||||
|
||||
### 2.5 Makefile (mirror Silo's Makefile-driven workflow)
|
||||
|
||||
`make` targets, modeled on `silo-server`'s Makefile (CLAUDE.md §Build):
|
||||
|
||||
```make
|
||||
make build # go build ./cmd/relay ./cmd/relayctl
|
||||
make run # run cmd/relay locally against docker-compose PG+Redis
|
||||
make test # go test ./... (unit + contract; no live providers)
|
||||
make test-integration # go test -tags=integration ./... (APNs sandbox + FCM validateOnly; needs creds)
|
||||
make lint # golangci-lint run
|
||||
make fmt # gofmt -w + goimports
|
||||
make migrate-create NAME=add_thing # timestamped Goose migration scaffold
|
||||
make migrate-up # apply migrations
|
||||
make migrate-status # list migration state
|
||||
make relayctl ARGS="account list" # build+run the admin CLI
|
||||
make docker # build deploy/Dockerfile
|
||||
make loadtest # run the k6/vegeta burst scenario (Phase 6)
|
||||
```
|
||||
|
||||
`make migrate-create NAME=...` must produce a **timestamped** filename (CLAUDE.md: "New migrations must use timestamped filenames created with `make migrate-create`; do not run `goose fix` or create paired `.up.sql`/`.down.sql` files"). The relay uses single-file Goose SQL migrations with `-- +goose Up` / `-- +goose Down`.
|
||||
|
||||
### 2.6 Migrations (Goose-style)
|
||||
|
||||
`migrations/sql/<timestamp>_<name>.sql`, run by a Goose runner wired in `internal/store` and invokable via `make migrate-up` and on `cmd/relay` startup (behind a flag). The v1 schema is exactly spec §8.1 (`relay_accounts`, `relay_api_keys`, `relay_apns_allowlist`, `relay_fcm_allowlist`, `relay_op_logs`). IDs are ULIDs stored as `text`; timestamps are `timestamptz` (Silo convention).
|
||||
|
||||
### 2.7 CI outline
|
||||
|
||||
CI (GitHub Actions or equivalent) runs on every push/PR:
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
build-test:
|
||||
steps:
|
||||
- setup-go 1.25
|
||||
- make lint # golangci-lint
|
||||
- make build
|
||||
- make test # unit + contract tests (no provider creds)
|
||||
- upload coverage
|
||||
migrate-check:
|
||||
services: [postgres, redis]
|
||||
steps:
|
||||
- make migrate-up # migrations apply cleanly on a fresh DB
|
||||
- make migrate-status
|
||||
integration: # gated: only when provider creds are available (Phase 3+)
|
||||
if: secrets.APNS_P8 != '' && secrets.FCM_SA_JSON != ''
|
||||
services: [postgres, redis]
|
||||
steps:
|
||||
- make test-integration # APNs sandbox + FCM validateOnly contract tests
|
||||
```
|
||||
|
||||
The `integration` job is **conditional on secrets being present** so Phases 0–2 are green long before any credential exists.
|
||||
|
||||
---
|
||||
|
||||
## 3. Dependency Choices
|
||||
|
||||
All versions are from the reference §3.7 pinned list. Pin exact versions in `go.mod`; bump deliberately.
|
||||
|
||||
| Concern | Library | Version | One-line rationale |
|
||||
|---|---|---|---|
|
||||
| APNs client | `github.com/sideshow/apns2` **or** hand-rolled | `v0.25.0` / `x/net/http2 v0.56.0` + `golang-jwt/jwt/v5 v5.3.1` | De-facto Go APNs HTTP/2 client (auto JWT + conn reuse); **decide vs. hand-rolled before Phase 3** because `apns2` has had no release since Oct 2024 (bus-factor; reference §3.1, spec open Q#3). |
|
||||
| FCM auth | `golang.org/x/oauth2/google` | `v0.36.0` | `CredentialsFromJSONWithType(..., google.ServiceAccount, firebase.messaging scope)` gives an auto-caching/-refreshing `TokenSource`; lighter than the Admin SDK and gives direct control of conn reuse + error classification (reference §3.2 — preferred for a fixed payload). |
|
||||
| FCM (alt, batteries-included) | `firebase.google.com/go/v4/messaging` | `v4.20.0` | Acceptable alternative with `IsUnregistered`/`IsQuotaExceeded` helpers; **pick exactly one** FCM path, not both (reference §3.2). Plan defaults to the `x/oauth2` path. |
|
||||
| JWT (ES256 sign for APNs; RS256 for hand-rolled FCM grant) | `github.com/golang-jwt/jwt/v5` | `v5.3.1` | Maintained JWT lib; ES256 for APNs, RS256 if hand-rolling the FCM token exchange (reference §3.1). |
|
||||
| HTTP/2 transport tuning | `golang.org/x/net/http2` | `v0.56.0` | `ReadIdleTimeout`+`PingTimeout` to reap half-open upstream conns — "the single most important tuning for a long-lived relay" (reference §3.3). |
|
||||
| Postgres | `github.com/jackc/pgx/v5` + `pgxpool` | `v5.10.0` | Recommended driver/pool; `pgxpool.New` is concurrency-safe + health-checked (reference §3.4). |
|
||||
| Redis | `github.com/redis/go-redis/v9` | `v9.20.1` | Context-first client with pooling; atomic Lua for rate-limit, `SET NX` for idempotency (reference §3.4). |
|
||||
| Router | stdlib `net/http` (Go 1.22+ `http.ServeMux` patterns) | stdlib | Two routes + health/metrics need no framework; stdlib `ServeMux` supports method+path patterns and gets HTTP/2 over TLS automatically. Avoids a dependency; middleware is plain `http.Handler` wrapping. |
|
||||
| Structured logging | `log/slog` | stdlib (Go 1.21+) | `slog.NewJSONHandler` for production; redaction middleware so secrets can't leak (reference §3.5, spec §13.1). |
|
||||
| Metrics | `github.com/prometheus/client_golang` | `v1.23.2` | Custom registry + `promhttp.HandlerFor` for `/metrics`; per-provider latency histograms + outcome counters (reference §3.5, spec §13.2). |
|
||||
| Per-instance rate (outbound pacing helper) | `golang.org/x/time/rate` | current | `Reservation.Delay()` to compute relative `Retry-After`; used for in-process pacing, **not** the cross-replica cap (that is Redis — reference §4.2). |
|
||||
| ULID generation | `github.com/oklog/ulid/v2` (or equivalent) | current | Generate `request_id` and entity IDs as ULIDs (Silo convention; spec §8.1). |
|
||||
|
||||
**Decision to make before Phase 3 (spec open Q#3):** adopt `sideshow/apns2` or hand-roll the APNs client. Recommendation: prototype both in Phase 3 behind the `internal/apns` interface; pick `apns2` if its recent commit/issue activity is acceptable, else hand-roll on `x/net/http2` + `golang-jwt/jwt/v5` mirroring the verified `apns2` internals (reference §3.1: `HostProduction`/`HostDevelopment`, `TokenTimeout=3000`s, `ReadIdleTimeout=15s`). Either way the rest of the service depends only on the `internal/apns` interface, not the concrete client.
|
||||
|
||||
**`pushwire` maintainability note (reference + spec §5).** The shared `pushwire` package builds the exact APNs/FCM payloads and headers. Because `silo-server`'s `custom_apns`/`custom_fcm` paths must produce identical payloads, `pushwire` is mirrored into `silo-server` with **one canonical source repo** (the relay), a **committed checksum verified in CI on both sides**, and a **closed-allowlist-key** test so an added `data`/payload key fails CI regardless of golden-file regeneration order (§2.3 sync runbook, §5.2). This is also a **supply-chain control for the content-free guarantee**: the payload-builder that enforces "data-only, no `notification` block, fixed key set" is enforced in two repos that must not drift. Payload shape is defined once.
|
||||
|
||||
---
|
||||
|
||||
## 4. Phased Tasks
|
||||
|
||||
Effort sizes: **S** ≈ 1 day, **M** ≈ 2–3 days, **L** ≈ 4–5 days (one engineer). Sizes are rough.
|
||||
|
||||
### Phase 0 — Scaffold, config, health, CI (size: M)
|
||||
|
||||
**Goal.** A buildable, lintable, testable repo that boots `cmd/relay`, serves `/healthz`, loads config, and is green in CI — with **no** provider dependency.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `go.mod`, `go.sum`, `.golangci.yml`, `Makefile`, `README.md`, `.github/workflows/ci.yml`, `deploy/Dockerfile`.
|
||||
- `cmd/relay/main.go` — load config, build slog JSON logger, start an `http.Server` with explicit timeouts and graceful shutdown, mount `/healthz`.
|
||||
- `internal/config/config.go` — `Load(ctx)`, `Validate()`, defaults; secret-manager interface stub (real wiring in Phase 6).
|
||||
- `internal/observability/logger.go` — `slog.NewJSONHandler` setup.
|
||||
- `internal/httpapi/server.go`, `internal/httpapi/router.go`, `internal/httpapi/health.go` — router, `/healthz`.
|
||||
- `internal/httpapi/errors.go` — the standard error body (spec §5.5) and a `writeError(w, status, code, msg, requestID)` helper.
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- Server timeouts are **mandatory** (zero = no timeout): `ReadHeaderTimeout`, `ReadTimeout`, `WriteTimeout`, `IdleTimeout`, `MaxHeaderBytes` (reference §3.3, spec §11). Body size cap ~16 KiB.
|
||||
- Graceful shutdown: on SIGTERM/SIGINT call `srv.Shutdown(ctx)` with a bounded context (spec §14.2).
|
||||
- `/healthz` returns `200 {"status":"ok"}`, unauthenticated, no dependency checks (spec §5.3).
|
||||
- Error body shape is fixed (spec §5.5):
|
||||
```json
|
||||
{ "error": { "code": "string_code", "message": "human readable", "request_id": "01JRELAY..." } }
|
||||
```
|
||||
Every request gets a ULID `request_id`, echoed as the `X-Request-Id` response header.
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Unit: `config.Validate` rejects missing required fields; `writeError` emits exact JSON + `X-Request-Id`.
|
||||
- Unit: `/healthz` returns 200 with the exact body.
|
||||
- CI: `make lint`, `make build`, `make test` green; `migrate-check` job present (no migrations yet → no-op pass).
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] `make build` produces `relay` and `relayctl` binaries.
|
||||
- [ ] `make lint` and `make test` pass in CI with zero provider credentials.
|
||||
- [ ] `cmd/relay` boots, serves `/healthz` 200, and shuts down gracefully on SIGTERM.
|
||||
- [ ] Every response carries `X-Request-Id`; errors use the §5.5 shape.
|
||||
|
||||
---
|
||||
|
||||
### Phase 1 — Storage, migrations, `relayctl` admin CLI (size: L)
|
||||
|
||||
**Goal.** The full PostgreSQL schema, a migration runner, the shared data-access package, and a working `relayctl` that can provision accounts/keys/allowlists and read op-logs — all without touching providers.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `migrations/sql/<ts>_init_relay_schema.sql` — exact schema from spec §8.1: `relay_accounts`, `relay_api_keys`, `relay_apns_allowlist`, `relay_fcm_allowlist`, `relay_op_logs`, plus indexes (`relay_api_keys_prefix_uidx`, `relay_api_keys_account_idx`, `relay_op_logs_account_time_idx`, `relay_op_logs_time_idx`).
|
||||
- `internal/store/postgres.go` — `pgxpool.New(ctx, dsn)`, `Ping`, migration runner hook.
|
||||
- `internal/store/redis.go` — `go-redis` client + `PING` (used Phase 2).
|
||||
- `internal/accounts/` — data-access: `CreateAccount`, `ListAccounts`, `DisableAccount`, `IssueKey`, `ListKeys`, `RevokeKey`, `LookupKeyByPrefix`, `SetApnsAllowlist`, `SetFcmAllowlist`, `GetAllowlists`. Used by both `httpapi` and `relayctl`.
|
||||
- `internal/accounts/keys.go` — key generation + hashing (see notes).
|
||||
- `internal/oplog/oplog.go` — `Write(ctx, entry)` to `relay_op_logs`; `TokenHash(token) string` (SHA-256 hex).
|
||||
- `cmd/relayctl/main.go` + subcommands per spec §5.6 table.
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **API-key format & hashing** (spec §8.2, reference §4.1):
|
||||
- Format `rk_<env>_<base62/base64url>`; env ∈ {`live`,`test`}. Random secret = 32 bytes from `crypto/rand`.
|
||||
- **Prefix** = `rk_<env>_<first 6–8 chars of random>`, non-secret, stored cleartext, uniquely indexed.
|
||||
- Store `key_hash = HMAC-SHA256(pepper, secret)` (32 bytes). Never store plaintext. Fast keyed hash is correct for 256-bit random keys (do **not** use argon2id here).
|
||||
- `relayctl key issue` prints the full `rk_…` **exactly once** to stdout; only hash+prefix persist (spec §8.3).
|
||||
- `relayctl` connects directly to Postgres with a privileged DSN held by operators (no public admin API; spec §5.6). Each mutating command writes an `admin.*` op-log with operator identity (OS user / `--actor`) and never the secret token (spec §5.6).
|
||||
- Allowlist commands replace the set atomically (UPSERT/DELETE in one tx) for `relay_apns_allowlist` / `relay_fcm_allowlist`.
|
||||
- Official allowlist config values to seed in examples/tests (allowlist config, **not** the contract — spec §8.4): APNs topics `com.continuum.app.ios`, `com.continuum.app.tvos`, `com.continuum.app.macos`; FCM project `continuum-prod-android`, package `com.continuum.app.android`.
|
||||
|
||||
**`relayctl` command surface (spec §5.6 / §13.3).**
|
||||
|
||||
| Command | Effect |
|
||||
|---|---|
|
||||
| `relayctl account create --name <label> [--note <text>]` | INSERT `relay_accounts`; print `account_id` |
|
||||
| `relayctl account list [--active]` | SELECT accounts + key count + last activity |
|
||||
| `relayctl account disable --account <id>` | `status='disabled'` |
|
||||
| `relayctl key issue --account <id> [--env live\|test] [--expires <dur>]` | INSERT key; print `rk_…` once |
|
||||
| `relayctl key list --account <id>` | SELECT (prefix/created/last_used/revoked; never secret) |
|
||||
| `relayctl key revoke --key-prefix <prefix>` | `revoked_at=now()` |
|
||||
| `relayctl allowlist apns set --account <id> --topics a,b,c` | replace APNs allowlist |
|
||||
| `relayctl allowlist fcm set --account <id> --project <p> --packages a,b` | replace FCM allowlist |
|
||||
| `relayctl allowlist show --account <id>` | print effective allowlists |
|
||||
| `relayctl logs --account <id> [--since …] [--outcome rejected]` | query `relay_op_logs` (redacted) |
|
||||
| `relayctl logs --gc` | retention GC: delete success rows >14d, failure/reject rows >90d (Phase 5; cron-schedulable) |
|
||||
| `relayctl ping-upstream --provider apns\|fcm --env sandbox` | credential-only health check; flips `relay_credential_healthy` (Phase 3/4/6) |
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Integration (PG via docker-compose): `make migrate-up` applies cleanly on a fresh DB; `make migrate-status` lists state.
|
||||
- Unit/integration: `IssueKey` → `LookupKeyByPrefix` round-trips; hash is HMAC-SHA256; plaintext never stored.
|
||||
- Integration: full `relayctl` flow — create account → issue key → set allowlists → list/show; `admin.*` op-logs written without secrets.
|
||||
- Unit: `oplog.TokenHash` is stable SHA-256 hex; op-log entry rejects/strips raw token.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] Migrations apply on a fresh DB and match spec §8.1 exactly (column names/types/constraints/indexes).
|
||||
- [ ] `relayctl` can provision an account, issue a key (printed once), set both allowlists, and read redacted logs.
|
||||
- [ ] No table stores tokens, content, or identity; `relay_op_logs` holds only `token_hash`, never raw tokens.
|
||||
- [ ] Key hash is HMAC-SHA256(pepper, secret); constant-time compare verified in a unit test.
|
||||
|
||||
---
|
||||
|
||||
### Phase 2 — Auth middleware, rate limit, idempotency (size: L)
|
||||
|
||||
**Goal.** The full request-path middleware chain (auth → rate limit → idempotency → decode/validate/allowlist) wired in front of stub send handlers that return a deterministic fake upstream result. No real provider yet.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `internal/httpapi/middleware_auth.go` — bearer parse, prefix lookup, constant-time compare, revoked/expired/disabled rejection, throttled `last_used_at`, per-IP anti-brute-force.
|
||||
- `internal/ratelimit/limiter.go` + `ratelimit/bucket.lua` — Redis Lua token bucket (per-account + coarse per-token).
|
||||
- `internal/httpapi/middleware_ratelimit.go` — applies the limiter, emits `429` + `Retry-After`.
|
||||
- `internal/idempotency/store.go` + `idempotency/idem.lua` (if needed) — `SET NX` lock, replay, 409, 422.
|
||||
- `internal/httpapi/middleware_idempotency.go` — wraps send handlers.
|
||||
- `internal/httpapi/decode.go` — `DisallowUnknownFields` decoder + field validation + allowlist enforcement.
|
||||
- `internal/httpapi/apple.go`, `internal/httpapi/fcm.go` — handler skeletons returning a fake `accepted` result (real upstream in Phase 3/4).
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **Auth** (spec §8.2): parse `Authorization: Bearer rk_<env>_<rest>` (malformed → `401 unauthorized`); derive `key_prefix`; `SELECT … WHERE key_prefix=$1` (unique index); reject if no row / revoked / expired / account disabled (do not distinguish reasons to caller — all `401 unauthorized`); `subtle.ConstantTimeCompare(HMAC(pepper, presented), key_hash)`; throttle `last_used_at` to ≤1/min/key; rate-limit auth failures per resolved client IP (see client-IP resolution below).
|
||||
- **Constant-time auth path (no enumeration oracle).** On a missing/revoked/expired row, **still** perform a dummy `HMAC-SHA256` against a fixed decoy hash with `subtle.ConstantTimeCompare` before returning the uniform `401`, so "unknown prefix" is not measurably faster than "known prefix, wrong secret." Keep the fine-grained failure reason (`unknown_prefix` vs `hash_mismatch`) in **internal** logs/metrics only — never in a response and never exposed to an untrusted `/metrics` scraper.
|
||||
- **Client-IP resolution / trusted proxy (security-critical).** The relay sits behind a TLS load balancer, so Go's `http.Request.RemoteAddr` is the LB address, not the caller. Derive the client IP from `RemoteAddr` **unless** the immediate peer is in an **allowlisted trusted-proxy CIDR list** (a config value), in which case take the **right-most untrusted hop** from the LB-set forwarding header. **Caller-supplied `X-Forwarded-For`/`X-Real-IP` headers are ignored** — trusting them would let an attacker spread brute-force across unlimited fake IPs (defeating the per-IP throttle) and poison `egress_ip` in op-logs. Both the per-IP auth-failure limiter and the logged `egress_ip` consume this single resolved IP. Account for shared egress (CGNAT/VPN — multiple self-hosted servers behind one IP, anticipated in spec §3.4): make the brute-force lockout **soft** (added latency / `Retry-After`), not a hard block, so a noisy/compromised neighbor cannot lock out legitimate servers sharing the IP.
|
||||
- **Bounded fail-open on Redis outage.** The per-account/daily rate limit is the primary cap on a stolen-key attacker's spend (and on shared downstream APNs/FCM quota). A naive fail-open removes that cap entirely during a Redis outage. Instead, when Redis is unreachable, fall back to a conservative **in-process** per-account limiter (`x/time/rate`) so each account is still capped to a small multiple of its normal rate; the allowlist (always enforced from Postgres) remains the hard boundary. A Redis outage pages ops; the cross-replica daily quota cannot be enforced during the outage (documented limitation).
|
||||
- **Rate limit** (spec §9): Redis HASH `rl:{account_id} = {tokens, last_refill_ts}` with TTL, atomic Lua (refill→check→consume in one `EVAL`). Defaults (spec §9.2): per-account ~10 req/s burst, refill 10/s with burst capacity 20; per-account daily 50,000 req/day; coarse `(account_id, token_hash)` ~1 req/3s. Over limit → `429 rate_limited` with **relative** `Retry-After` (delay-seconds, never an absolute epoch — reference §4.2). On Redis down: **bounded fail-open** (in-process per-account fallback limiter, above — not unlimited), log `rate_limit.degraded`, increment `relay_rate_limit_degraded_total` (spec §9.4).
|
||||
- **Daily quota mechanics.** Store the daily counter as a **separate Redis counter** `rl:day:{account_id}:{yyyymmdd}` with `INCR` + `EXPIRE` to the end of the window — it is **not** the per-second token bucket and **not** a Postgres column. Reset semantics: **fixed UTC calendar day** (`yyyymmdd` in UTC). During a Redis outage the daily counter, like the per-second bucket, falls back to the bounded in-process limiter and the precise cross-replica daily cap cannot be enforced (documented; pages ops). Emit `relay_rate_limited_total{kind="daily"}` and alert on accounts approaching/hitting the cap (a big-release burst can legitimately reach 50k/day; §6.4).
|
||||
- **Project-level FCM circuit breaker (multi-tenant fairness).** The ~600k msgs/min FCM project quota (reference §2.7) is **shared across all relay accounts**, but the per-account limit is local to each account — one noisy account can exhaust the shared quota for everyone. Add a relay-side **global per-project outbound limiter / circuit breaker** that tracks aggregate FCM send rate against the project quota and **sheds load with `503 upstream_throttled` before hammering FCM** when near quota (the relay is the only aggregation point that can honor reference §2.7's anti-spike / smooth-traffic guidance — avoid sending within a 2-minute window of :00/:15/:30/:45). Export `relay_fcm_project_quota_headroom` and alert (§6.4).
|
||||
- **Idempotency** (spec §10): require `Idempotency-Key` header on sends; missing → `400 missing_idempotency_key`. Length-limit ≤255 chars; the three-part `<delivery_id>:<device_id>:<attempt>` shape is **opaque** (not strictly parsed). Namespaced `idem:{account_id}:{key}`. `SET … NX EX <lock_ttl>` (`lock_ttl` ≈ 30s) in-flight lock:
|
||||
- NX success → process; on completion overwrite with serialized `{status, body, upstream_id, canonical_hash}` + ~24h TTL (success **and** error recorded).
|
||||
- NX fail + stored completed result → **replay** verbatim (same `apns_id`/`fcm_message_name`).
|
||||
- NX fail + still in-flight → `409 idempotency_conflict`.
|
||||
- Same key + different request payload (compare stored `canonical_hash`) → `422 idempotency_key_reuse`.
|
||||
- **Fails closed**: Redis unavailable → `503 upstream_unavailable` (never risk a double-send; spec §10.2).
|
||||
- **Lock-TTL invariant (no double-send / no clobber).** The upstream APNs/FCM call (plus HTTP/2 setup, JWT/OAuth refresh, GOAWAY reconnect) can otherwise outlast `lock_ttl`, letting a concurrent retry's NX succeed and fire a **second** upstream send. Pin the upstream call with a hard context deadline **strictly shorter than `lock_ttl`** (≈10s upstream vs 30s lock) and refuse to send upstream if the handler's own deadline has already passed the lock TTL. On completion, use a **compare-and-set** that only overwrites the stored value if it is still *this* request's in-flight marker, so a lock stolen by a concurrent request cannot have its result clobbered.
|
||||
- **Privacy invariant (no stored token).** The `canonical_hash` is computed over the validated fields with the device token **replaced by its already-computed `token_hash` (SHA-256)**; the stored idempotency value is exactly `{status, body, upstream_id, canonical_hash}` and holds **no raw token** — matching the op-log no-raw-token guarantee (NG1, spec §3.2/§12).
|
||||
- **Drain interaction.** On handler context-cancellation (e.g. a drain/shutdown that severs an in-flight send), **release or downgrade** the in-flight marker so an immediate caller retry does not get spurious `409 idempotency_conflict` for the full `lock_ttl` (see Phase 6 drain ordering and §5.7 drain test).
|
||||
- **Decode/validate/allowlist** (spec §5.1/§5.2/§8.4): `DisallowUnknownFields` (unknown key → `400 unexpected_field`). Validate every field per the spec field tables (token shape, environment, mode, ID length caps, collapse-id ≤64 bytes for APNs). Allowlist check **after** auth, **before** payload build: APNs `topic` ∈ account's `relay_apns_allowlist` else `403 topic_not_allowed`; FCM `(project_id, package_name)` ∈ `relay_fcm_allowlist` else `403 project_not_allowed` / `403 package_not_allowed`. Allowlist may be a short-TTL per-account-scoped in-memory cache invalidated on `relayctl allowlist … set`.
|
||||
|
||||
**Middleware order (spec §4.1).**
|
||||
|
||||
```
|
||||
TLS → auth → rate-limit → decode(DisallowUnknownFields) → validate+allowlist → idempotency → [Phase 3/4 upstream] → map → store idem result → op-log + metrics
|
||||
```
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Unit: bearer parse edge cases; constant-time compare; revoked/expired/disabled all map to `401`; `last_used_at` throttle.
|
||||
- Unit: the auth path is **constant-time for unknown-prefix vs wrong-secret** (dummy HMAC + `ConstantTimeCompare` on a missing/revoked row) — no enumeration timing oracle; the fine-grained reason stays internal-only.
|
||||
- Unit: **trusted-proxy client-IP resolution** — a forged caller `X-Forwarded-For`/`X-Real-IP` is ignored; only the LB-set header from an allowlisted trusted-proxy peer is honored; `RemoteAddr` is used otherwise.
|
||||
- Integration (Redis): token bucket refill/consume math; `429` + correct relative `Retry-After`; **daily counter** (`rl:day:{account}:{yyyymmdd}`, fixed-UTC reset, `EXPIRE`) trips at 50k; **bounded** fail-open on Redis down (in-process per-account cap engaged, not unlimited).
|
||||
- Integration (Redis): idempotency first/replay/409/422/missing-key matrix (spec §10.3); fail-closed `503` on Redis down; the stored value carries **no raw token** (canonical hash uses `token_hash`).
|
||||
- Integration (Redis): **Redis dropped mid-request** (not just down at start) — rate-limit layer degrades to bounded fail-open; idempotency layer fails closed (`503`); a lock whose TTL expires mid-send cannot be stolen into a double-send (upstream deadline < `lock_ttl`).
|
||||
- Contract: `DisallowUnknownFields` rejects every disallowed field (`title`, `body`, `image_url`, `media_id`, `username`, `server_url`, …) with `400 unexpected_field`.
|
||||
- Contract: allowlist rejections produce exact codes `topic_not_allowed` / `project_not_allowed` / `package_not_allowed`.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] A request with a valid key, allowlisted topic, and `Idempotency-Key` reaches the (stub) handler and returns `accepted`.
|
||||
- [ ] Duplicate idempotency key replays the identical body; in-flight → `409`; payload mismatch → `422`; missing → `400`.
|
||||
- [ ] Over-limit → `429` with relative `Retry-After`; Redis-down rate-limit fails open, idempotency fails closed.
|
||||
- [ ] Every unknown field and every non-allowlisted topic/project/package is rejected with the exact §5/§8 error code.
|
||||
|
||||
---
|
||||
|
||||
### Phase 3 — APNs client + `POST /v1/apple/send` (size: L) · gated on Apple provisioning
|
||||
|
||||
**Goal.** A production APNs client (cached ES256 JWT, reused HTTP/2 pool, correct reason→error mapping) and the live `/v1/apple/send` endpoint building the §5.1 payloads via `pushwire`, validated against the APNs **sandbox**.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `internal/apns/client.go` — the `APNsClient` interface + concrete impl (apns2 **or** hand-rolled per §3 decision).
|
||||
- `internal/apns/jwt.go` — ES256 JWT lifecycle (cache, ~50min refresh, never <20min, eager refresh on `ExpiredProviderToken`).
|
||||
- `internal/apns/transport.go` — per-team HTTP/2 pool with `ReadIdleTimeout`/`PingTimeout`; env→host selection.
|
||||
- `internal/apns/errors.go` — APNs status+`reason` → caller `error.code` + retryability (spec §6.4 table).
|
||||
- `internal/pushwire/apns.go` — build `private_alert` / `background_wake` `aps`+`silo` payloads and `apns-*` headers (spec §5.1).
|
||||
- `internal/httpapi/apple.go` — replace the Phase 2 stub with the real send.
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **JWT** (spec §6.1, reference §1.1): ES256 only; header `{"alg":"ES256","kid":<10-char Key ID>}`, claims `{"iss":<10-char Team ID>,"iat":<epoch s>}`. Cache one JWT behind a mutex; regenerate on a ~50-min timer, never <20-min apart; eager regenerate + **single in-process retry** on `403 ExpiredProviderToken`; back off / slow refresh on `429 TooManyProviderTokenUpdates`. **Per-team pool** keyed by team even though v1 has one team.
|
||||
- **Single-flighted JWT regeneration (stampede guard).** Under a burst of concurrent sends sharing one cached JWT, an expiry (or a `403 ExpiredProviderToken`) is observed by many in-flight goroutines at once; a naive per-goroutine regenerate-on-403 would trip `429 TooManyProviderTokenUpdates`. Regeneration must therefore be **single-flighted** (e.g. `golang.org/x/sync/singleflight` or a generation/epoch counter under the mutex): a goroutine seeing `ExpiredProviderToken` first checks whether the JWT was already refreshed since *its* request started and reuses the new token rather than triggering another refresh. Exactly **one** refresh happens per ~20-min window regardless of how many concurrent 403s arrive.
|
||||
- **Endpoint** (spec §6.2): `production` → `https://api.push.apple.com`; `sandbox` → `https://api.sandbox.push.apple.com`. Both `api.sandbox.push.apple.com` and the older alias `api.development.push.apple.com` are **valid, equivalent sandbox hosts that still resolve** — we standardize on `api.sandbox.push.apple.com` (the current canonical name and `apns2` v0.25.0's `HostDevelopment`); `02`'s use of `api.development.*` is not a discrepancy to resolve, just the older alias. The external contract `"environment":"sandbox"` is unchanged. The upstream hosts are **hardcoded constants never derived from request input**. Port 443, 2197 fallback.
|
||||
- **Connection** (spec §6.3, reference §3.3): one client/pool per process per env per team; never per push. Non-zero `ReadIdleTimeout`(~15s) + `PingTimeout`(~15s). Do not hardcode `SETTINGS_MAX_CONCURRENT_STREAMS`. Handle `GOAWAY` (drop+reconnect). Ensure system trust store includes USERTrust RSA (default in OS/Go pools).
|
||||
- **Headers** (spec §5.1 table): `apns-topic` = allowlisted topic; `apns-push-type` = `alert` (private_alert) / `background` (background_wake); `apns-priority` = `10` / `5` (background **must** be 5; 10 is an error — reference §1.4.1); `apns-collapse-id` = `collapse_id` if present (≤64 bytes); `apns-id` omitted (Apple generates, echoed back). **`apns-expiration` ships with a finite default** (configurable) rather than 0/omitted: a few hours for `private_alert`, a short TTL for `background_wake`. Omitting it makes Apple store-and-retry an undelivered push for **up to 30 days** (reference §1.4) — for a notification whose content is fetched live on wake, a wake fired days later is useless or confusing. Mirror this on FCM with a finite `android.ttl` (default is otherwise ~4 weeks) for cross-provider parity. (Resolved open Q#5; the exact default values want product input but v1 ships a sane non-30-day default, not an omission.)
|
||||
- **Payloads** (`pushwire`, spec §5.1): `private_alert` → `aps.alert` with `title-loc-key=SILO_NOTIFICATION_TITLE`, `loc-key=SILO_NOTIFICATION_GENERIC_BODY`, `sound=default`, plus `silo` dict (`v:1`, `wake:"notifications.changed"`, `server_device_id`, `delivery_id`); add `aps.badge` only if `badge` is present, non-null, and within a **bounded range (0–9999)** — out-of-range → `400 invalid_field` (it is the one numeric field that escapes the opaque-ID discipline, so it is bounded rather than copied verbatim). `background_wake` → `aps.content-available:1` + `silo` dict. Uncompressed JSON, far under 4 KB; defensive `400 payload_too_large` if it ever exceeds.
|
||||
- **Response → caller** (spec §5.1): `200` → `{ "request_id", "apns_id" (Apple's apns-id header), "status":"accepted" }`. Error mapping per spec §6.4 (e.g. `BadDeviceToken`/**`InvalidToken`**/`DeviceTokenNotForTopic`/`MissingDeviceToken` → `422 bad_device_token`; `Unregistered`/`ExpiredToken` → `410 unregistered` with `timestamp` passed through in `error.message`; `403 InvalidProviderToken`/etc → `502 upstream_auth_error` + ops alert; `429 TooManyRequests` → `429 rate_limited` + `Retry-After`; 5xx → `503 upstream_unavailable`). **`InvalidToken` is treated equivalently to `BadDeviceToken` (token-purge, non-retryable)** — Apple has migrated some responses from `BadDeviceToken` to `InvalidToken`, and the `02` contract requires treating them equivalently; omitting it would let a dead device fall through to the catch-all and never be disabled. Add a **default/fallback rule**: any unrecognized 400/403/410 `reason` maps to a safe non-retryable `502 upstream_error` rather than being dropped. **Connection-severed sends** — a request whose HTTP/2 stream is terminated by `GOAWAY` or any connection reset **before** a final APNs response arrives → `503 upstream_unavailable` (retryable), the same as a network timeout; the relay does **not** transparently retry it (the caller's outbox does), preserving at-most-once semantics under the idempotency key. Tag the op-log outcome / metric label `connection_severed` so these are distinguishable. **Environment/token mismatch caveat:** a `sandbox`/`production` mismatch (e.g. a prod token sent with `environment=sandbox`) presents upstream as `BadDeviceToken` → `bad_device_token`, which instructs the caller to purge a perfectly good token; emit a distinct op-log/metric so a systemic misconfig (many `bad_device_token` from one account) is distinguishable from genuinely dead tokens. Network/timeout to APNs → `503 upstream_unavailable`. The relay does **not** retry transient upstream errors across caller requests (the caller's outbox does); the only in-process retry is the JWT-expiry one (single-flighted — see JWT note below).
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Unit: JWT signs valid ES256 with correct header/claims; refresh-timer logic (≥20min, ≤60min, eager-on-expired); single retry on `ExpiredProviderToken`.
|
||||
- Concurrency: force JWT expiry under a burst of concurrent sends; assert **exactly one** regeneration (single-flight) and **zero** `429 TooManyProviderTokenUpdates`.
|
||||
- Unit (`pushwire`): golden-file payloads + headers for `private_alert` and `background_wake`, with and without `badge`/`collapse_id` (byte-exact vs spec §5.1). **Same golden files mirrored into `silo-server`.**
|
||||
- Unit: every row of the spec §6.4 reason→error table maps correctly (table-driven), **including `InvalidToken` → `bad_device_token`** (equivalent to `BadDeviceToken`) and the **default/fallback** rule (unrecognized reason → `502 upstream_error`, non-retryable).
|
||||
- Fake APNs upstream injecting a **`GOAWAY` / mid-stream reset** before a final response → caller gets `503 upstream_unavailable`, the op-log/metric is tagged `connection_severed`, and there is **no lost or double send** (the relay does not transparently retry).
|
||||
- Integration (**Apple sandbox**, behind `-tags=integration`, requires `.p8`): real send to `api.sandbox.push.apple.com` returns 200 + `apns_id`; an intentionally bad token returns the expected `bad_device_token`/`unregistered` mapping.
|
||||
- Integration: `relayctl ping-upstream --provider apns --env sandbox` signs+connects without sending content; optionally captures the sandbox-only `apns-unique-id` response header (reference §1.6) into the ping output for Push-Notifications-Console delivery-log correlation during client QA.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] `/v1/apple/send` builds the exact §5.1 payloads/headers and returns `{request_id, apns_id, status:"accepted"}` on success against sandbox.
|
||||
- [ ] Background pushes always send `apns-priority: 5` and `apns-push-type: background`.
|
||||
- [ ] JWT is cached, reused, and refreshed on the ~50-min cadence; `ExpiredProviderToken` triggers one regenerate+retry; concurrent 403s yield exactly one single-flighted regeneration.
|
||||
- [ ] Every spec §6.4 mapping is covered by a passing table-driven test, **including `InvalidToken` and the unrecognized-reason fallback**; a `GOAWAY`/connection-severed send maps to `503 upstream_unavailable` with no double-send; the `pushwire` golden files match across both repos.
|
||||
|
||||
---
|
||||
|
||||
### Phase 4 — FCM client + `POST /v1/fcm/send` (size: L) · gated on Firebase provisioning
|
||||
|
||||
**Goal.** A production FCM HTTP v1 client (cached OAuth2 token, tuned HTTP/2, correct status→error mapping) and the live `/v1/fcm/send` endpoint building the §5.2 **data-only** payloads via `pushwire`, validated with FCM `validateOnly:true`.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `internal/fcm/client.go` — the `FCMClient` interface + concrete impl (`x/oauth2/google` path per §3).
|
||||
- `internal/fcm/oauth.go` — `CredentialsFromJSONWithType(ctx, json, google.ServiceAccount, "https://www.googleapis.com/auth/firebase.messaging")` → cached/auto-refreshing `TokenSource`, one per project.
|
||||
- `internal/fcm/transport.go` — tuned HTTP/2 client; endpoint `POST https://fcm.googleapis.com/v1/projects/{PROJECT_ID}/messages:send`.
|
||||
- `internal/fcm/errors.go` — FCM `error.status` → caller `error.code` + retryability + Retry-After honoring (spec §7.4).
|
||||
- `internal/pushwire/fcm.go` — build data-only `message` + `android` config (spec §5.2).
|
||||
- `internal/httpapi/fcm.go` — replace the Phase 2 stub with the real send.
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **OAuth2** (spec §7.1, reference §2.2): least-privilege scope `firebase.messaging`; `TokenSource` caches + auto-refreshes (~1h tokens; never mint per request). On GCP prefer keyless Workload Identity / attached SA; off-GCP load SA JSON from the secret manager (open Q#4). One token source per Firebase project.
|
||||
- **Endpoint** (spec §7.2): global host only — no regional endpoints (reference §2.3). The host `fcm.googleapis.com` is a **hardcoded constant never derived from request input**. `{PROJECT_ID}` is the allowlist-checked `project_id`, and `project_id`/`package_name` are validated against a **strict charset** (e.g. `^[a-z0-9-]+$` / reverse-DNS) **before** allowlist lookup and URL interpolation, so an allowlist row can never introduce a path-traversal or host-override (no SSRF / path injection).
|
||||
- **Payload — data-only** (`pushwire`, spec §5.2): `message.token` = caller token; `message.data` is a **fixed, closed set of string-valued keys only** (`"v":"1"`, `"wake":"notifications.changed"`, `"server_device_id"`, `"delivery_id"`) — no caller-controlled key or value beyond the opaque IDs; **no `notification` block**; `message.android.priority` = `"HIGH"` (private_data) / `"NORMAL"` (background_wake); `message.android.collapseKey` set only if the caller sent `collapse_key`. **Field-casing reconciliation (reference §2.4):** the relay POSTs raw JSON to the real FCM HTTP v1 REST endpoint, whose AndroidConfig field is camelCase **`collapseKey`** (FCM v1 rejects unknown fields with `INVALID_ARGUMENT`, so a literal `collapse_key` inside `android` would fail every collapsible send). The **inbound caller request body field stays `collapse_key`** per `03`; only the relay-built upstream JSON uses `collapseKey`. Likewise `android.priority` is sent as canonical uppercase `"HIGH"`/`"NORMAL"` (reference §2.4 lists these as the enum values, with lowercase also accepted; the relay sends the canonical uppercase to be robust against future enum tightening). Honor the `03` contract's `priority: high`-equivalent (`"HIGH"`) for `private_data` (do not second-guess — spec §5.2 reconciliation note). Set a finite `android.ttl` (default ~4 weeks otherwise) for parity with the APNs `apns-expiration` default (above). Never set `topic`/`condition`. ≤4096 bytes. Use `validateOnly:true` (camelCase REST field — reference §2.5) only for the `relayctl` test path, never production.
|
||||
- **`badge` has no data-only FCM home.** The relay-built FCM payload is strictly data-only (no `notification` block), so a caller-provided `badge` has nowhere legitimate to go. **Drop `badge` from the FCM accepted schema** (it is disabled-by-default and would otherwise have to be smuggled into the closed `data` map, weakening the allowlist). On the APNs side, bound `badge` to a sane range (e.g. 0–9999) and reject otherwise with `400 invalid_field` — it is the one numeric field that escapes the opaque-ID discipline.
|
||||
- **Collapse-key coalescing is best-effort (reference §2.7).** FCM stores at most **4 distinct `collapseKey`s per device** while offline; a 5th evicts one nondeterministically. Since `collapse_key` is a per-series HMAC and a device may follow >4 series, collapsible coalescing is best-effort and excess distinct keys evict nondeterministically — acceptable because the durable inbox + sync (the caller's outbox) covers any dropped collapsible wake. APNs has no equivalent documented per-device collapse-id cap (note the asymmetry).
|
||||
- **Response → caller** (spec §5.2): `200` → `{ "request_id", "fcm_message_name" (FCM v1 `name`), "status":"accepted" }`. Error mapping per spec §7.4 (e.g. `INVALID_ARGUMENT` → `422 invalid_argument`; `SENDER_ID_MISMATCH` → `403 sender_id_mismatch`; `UNREGISTERED` → `410 unregistered`; `QUOTA_EXCEEDED` → `429 rate_limited` honoring upstream `Retry-After` else default **60s**; `INTERNAL`/`UNAVAILABLE` → `503 upstream_unavailable` honoring `Retry-After` on 503; `UNSPECIFIED_ERROR` → `502 upstream_error`, no auto-purge). Classify on `error.status`/`FcmError.errorCode`, not the bare HTTP code. **Retry-After** forwarded to the caller as relative delay-seconds (reference §4.2). Add a **default/fallback rule** so any unrecognized FCM `error.status`/`errorCode` maps to a safe non-retryable `502 upstream_error` (never an auto-purge), rather than being dropped.
|
||||
- **`THIRD_PARTY_AUTH_ERROR` (401) — re-classified (do NOT page as a relay-credential failure).** Per Firebase's error-codes page this means an invalid/missing **APNs cert/key or Web Push auth credential** and is returned only for iOS-via-APNs or Web Push registration tokens. `/v1/fcm/send` sends **only** Android registration tokens (`message.token`, package `com.continuum.app.android`, data-only), so a legitimate Android send can never produce `THIRD_PARTY_AUTH_ERROR`. If it ever appears, treat it as a **per-token/registration anomaly** → `502 upstream_error`, surface for investigation, and do **not** page on-call as a relay `.p8`/SA misconfig. The relay's **actual** OAuth2/service-account credential failures surface as a non-200 from `oauth2.googleapis.com/token` at mint time, or HTTP `401 UNAUTHENTICATED` / `403 PERMISSION_DENIED` (the `status`, not an `FcmError` code) on `messages:send` — **those** map to `502 upstream_auth_error` + ops alert.
|
||||
- **Per-device rate exceedance (`DeviceMessageRateExceeded`, reference §2.7).** The Android per-device caps (240/min, 5,000/hr) are **distinct** from project-level `QUOTA_EXCEEDED` and surface defensively as a quota/`INVALID_ARGUMENT`-adjacent signal whose exact code/string is not firmly documented; detect on `error.status`/`errorCode` and map to a retryable `429 rate_limited` with a conservative default `Retry-After`, **not** a token purge.
|
||||
- **`UNREGISTERED` caller-facing 410 is an intentional cross-provider normalization.** FCM natively returns `UNREGISTERED` as HTTP **404**; the relay remaps it to caller `410 unregistered` so the caller's device-disable ("purge dead token") logic is uniform with the APNs path (which natively uses 410). This is a deliberate divergence from FCM's native status, not a bug.
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Unit: token source caches; no per-request mint; refresh margin.
|
||||
- Unit (`pushwire`): golden-file data-only payloads for `private_data` and `background_wake`, with/without `collapse_key` (byte-exact vs spec §5.2; **all data values strings**; **no `notification` block**). Mirrored into `silo-server`.
|
||||
- Unit: every row of the spec §7.4 status→error table (table-driven), including `Retry-After` honoring + 60s default, the unrecognized-`error.status` fallback (`502 upstream_error`), the per-device-rate (`DeviceMessageRateExceeded`) → `429 rate_limited` (no purge), and `THIRD_PARTY_AUTH_ERROR` → `502 upstream_error` (per-token anomaly, **not** paged as relay-credential).
|
||||
- Unit/contract: the FCM upstream JSON uses camelCase `collapseKey`, uppercase `priority` (`"HIGH"`/`"NORMAL"`), and `validateOnly`; `badge` is rejected from the FCM schema; the `data` map is the closed key set (no extra keys).
|
||||
- Integration (**FCM `validateOnly:true`**, `-tags=integration`, requires SA JSON): a valid request validates OK; a malformed token surfaces `invalid_argument`/`unregistered` mapping.
|
||||
- Integration: `relayctl ping-upstream --provider fcm --env sandbox` mints a token + does a `validateOnly` send.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] `/v1/fcm/send` builds the exact §5.2 data-only payload (string values, no `notification`) and returns `{request_id, fcm_message_name, status:"accepted"}`.
|
||||
- [ ] OAuth2 token is cached/refreshed, never minted per request; one source per project.
|
||||
- [ ] Every spec §7.4 mapping is covered (incl. the unrecognized-status fallback, per-device-rate, and `THIRD_PARTY_AUTH_ERROR` as a non-paging per-token anomaly); `QUOTA_EXCEEDED`/`UNAVAILABLE` forward `Retry-After` (default 60s on 429).
|
||||
- [ ] The upstream FCM JSON uses `collapseKey` (camelCase), `"HIGH"`/`"NORMAL"` priority, `validateOnly` (camelCase), a finite `android.ttl`, and the closed `data` key set; `badge` is not accepted on the FCM path.
|
||||
- [ ] `validateOnly` test path works; production sends never set `validateOnly`.
|
||||
|
||||
---
|
||||
|
||||
### Phase 5 — Observability, redaction, metrics (size: M)
|
||||
|
||||
**Goal.** Production-grade redacted logging, op-log persistence, the full Prometheus metric catalog, and `/readyz`/`/metrics` wired and network-scoped.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `internal/observability/redact.go` — redaction middleware (strips `Authorization`/bearer, raw key, raw token, JWT, OAuth2 token, payload bodies).
|
||||
- `internal/observability/metrics.go` — Prometheus registry + the spec §13.2 catalog.
|
||||
- `internal/httpapi/health.go` — `/readyz` (PG `SELECT 1`, Redis `PING`; APNs and FCM reported **per-provider**, not as a single combined gate) and `/metrics` (`promhttp.HandlerFor`), both bound to the internal network.
|
||||
- `internal/oplog/gc.go` + `cmd/relayctl` `logs --gc` subcommand (or a background sweeper in `cmd/relay`) — the retention GC deliverable; plus the §8.1 partitioning DDL (or a documented partition/offload decision).
|
||||
- Wire `oplog.Write` into every send path with the redacted subset.
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **Redaction enforced in middleware** so no code path can leak (spec §11/§12/§13.1). Never log: bearer header, raw API key (log prefix or hash only), raw device tokens (log `SHA-256` truncated hash only), payload bodies, relay JWT, OAuth2 tokens. Do log: `request_id`, `account_id`, `status_code`, `error_code`, upstream reason, `token_hash`, `egress_ip`, `latency_ms`.
|
||||
- **Op-log retention is a concrete Phase 5 deliverable** (spec §8.1/§13.1): every send writes one `relay_op_logs` row (redacted by construction). Ship a real GC — **add `relayctl logs --gc`** (to the §5.6 command table and Phase 1/5 file lists) **or** a built-in background sweeper in `cmd/relay` — with a definite retention query (`DELETE` success rows >14d, failure/reject rows >90d) on a schedule with an owner. Either add **time-partitioning DDL** to the §8.1 schema or document an explicit partition/offload decision here (do **not** defer it to an open question). Add a disk-usage / `relay_op_logs` table-growth alert (at 50k/day/account the table grows unbounded without GC — a classic "works for months then the disk fills" surprise).
|
||||
- **Metrics catalog** (spec §13.2): `relay_requests_total`, `relay_request_duration_seconds`, `relay_upstream_requests_total`, `relay_upstream_duration_seconds`, `relay_auth_failures_total`, `relay_rate_limited_total` (incl. `kind="daily"`), `relay_rate_limit_degraded_total`, `relay_idempotency_total`, `relay_apns_jwt_refresh_total`, `relay_fcm_token_refresh_total`, `relay_redis_up`, `relay_pg_up`, plus `relay_provider_healthy{provider=...}`, `relay_credential_healthy{provider=...}`, and `relay_fcm_project_quota_headroom`. **Keep labels low-cardinality** (no token, no `request_id`).
|
||||
- **`/readyz` — per-provider, not all-or-nothing.** Return `200` only when the **core deps** (PG + Redis) are reachable, and report **per-provider** APNs/FCM health separately in `checks` (a `relay_provider_healthy{provider}` gauge). A single provider's credential/init failure must **not** down the whole replica (that would pull a healthy APNs replica from the LB just because FCM creds are temporarily unresolvable, killing Apple push too); instead the send handler for an un-initialized/degraded provider returns `503 upstream_unavailable` for **that provider only**. Surface per-provider circuit-breaker state so operators see "APNs degraded, FCM healthy" rather than a binary not-ready. Body shape: `503 {"status":"not_ready","checks":{...}}` only when a core dep is down (spec §5.4).
|
||||
- `/readyz` and `/metrics` bound to the internal/scrape network; only `/v1/*` and `/healthz` are public (spec §11/§14.1).
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Unit: redaction middleware drops every disallowed field even when handlers attempt to log them (assert no token/bearer/JWT/OAuth2 string appears in captured log output).
|
||||
- Unit: a success and a failure path each produce exactly one op-log row with the redacted subset and a `token_hash` (never raw token).
|
||||
- Unit: metric counters increment with correct labels for first/replay/conflict idempotency, auth-failure reasons, rate-limit kinds, JWT/token refresh results.
|
||||
- Integration: `/readyz` flips to `503` when PG or Redis is down; `/metrics` exposes the full catalog.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] No log line or op-log row can contain a raw token, bearer header, JWT, OAuth2 token, or payload body (asserted by tests).
|
||||
- [ ] All spec §13.2 metrics are exported with low-cardinality labels.
|
||||
- [ ] `/readyz` reflects real dependency + credential health; `/metrics` and `/readyz` are not publicly routable.
|
||||
|
||||
---
|
||||
|
||||
### Phase 6 — Hardening, load test, deploy (size: L) · gated on provisioning + secret manager
|
||||
|
||||
**Goal.** Production hardening (real secret-manager wiring, TLS, anti-abuse), a hundreds-of-users burst load/pacing test, idempotency-under-concurrency proof, container build, and rollout.
|
||||
|
||||
**Files to create/modify.**
|
||||
|
||||
- `internal/config/secrets_*.go` — real secret-manager integration (KMS/Vault/cloud secret manager); load `.p8`, SA JSON, pepper, DSNs at startup into memory; optional SIGHUP re-read.
|
||||
- `deploy/Dockerfile` (distroless/minimal), deploy manifests, TLS/HSTS config.
|
||||
- `test/load/` — k6/vegeta burst scenario + concurrency idempotency test harness.
|
||||
- Alerting rules (Prometheus → alertmanager / SLO doc).
|
||||
|
||||
**Implementation notes.**
|
||||
|
||||
- **Secrets** (spec §11, reference §4.5): credentials loaded once at startup from the secret manager into memory; never on disk in the image/repo, never logged, never returned by any endpoint. On GCP prefer keyless FCM (Workload Identity). Rolling restart picks up rotated secrets.
|
||||
- **TLS** (spec §11/§14.1): HTTPS/HTTP-2 only, TLS 1.2+ (prefer 1.3), HSTS on `relay.silo.app`. Terminate at LB or in-process.
|
||||
- **Anti-abuse** (spec §8.2/§11): per-IP auth-failure rate limiting keyed on the **trusted-proxy-resolved client IP** (caller-supplied `X-Forwarded-For`/`X-Real-IP` ignored — see Phase 2 client-IP resolution; the trusted-proxy CIDR list is config); **soft** lockout (latency / `Retry-After`) to avoid CGNAT/VPN collateral; body size cap; explicit timeouts; graceful shutdown. Document a response runbook for the key-guessing alert.
|
||||
- **Credential-health validation & rotation runbook** (spec §4.4/§14.3). Add a **startup + periodic synthetic credential check** that proves the loaded credential actually authenticates upstream — not just that a JWT signs / a token mints locally (which `/readyz` already checks). For FCM, a `validateOnly:true` send; for APNs, an auth-exercising send to a known-bad token that still distinguishes an auth-layer `403` (`InvalidProviderToken`) from a token-layer `400` (`BadDeviceToken`). Wire the result into a `relay_credential_healthy{provider=...}` gauge and into `/readyz`'s per-provider health (below). Schedule `relayctl ping-upstream` as a cron for continuous health, and **alert on the first `upstream_auth_error`** (§6.4) — Apple actively revokes leaked `.p8` keys and SA keys/bindings can be rotated out from under the relay. Document the **rotation runbook** (overlap window, dual-key support — APNs allows multiple keys and the relay pools by team).
|
||||
- **Graceful-shutdown drain ordering** (spec §14.2). On `SIGTERM`: (1) flip `/readyz` to `503` so the LB stops routing **new** requests; (2) keep serving in-flight handlers; (3) bound `srv.Shutdown(ctx)` to **less than `lock_ttl`** so any marker left by a killed-mid-flight handler expires quickly — **and** release/downgrade the in-flight idempotency marker on handler context-cancellation (Phase 2), so a drained-but-incomplete send frees its lock immediately rather than forcing the caller's retry to eat a 30s `409 idempotency_conflict`. A clean drain must leave **no orphaned in-flight idempotency markers**; the relationship between the shutdown grace period and `lock_ttl` is explicit.
|
||||
- **Load/pacing test** (spec §13 / testing strategy §5 below): simulate a hundreds-of-users burst (e.g. 300–500 concurrent device sends), assert the relay stays under the per-account cap, returns correct `429` + relative `Retry-After`, and one well-tuned upstream connection sustains the throughput (reference §1.3 — thousands/sec). Verify the FCM smooth-traffic guidance does not trip quota (reference §2.7).
|
||||
- **Idempotency under concurrency**: fire N concurrent requests with the **same** `Idempotency-Key`; assert **at most one** upstream send (count via a fake upstream), the rest replay or `409`.
|
||||
- **FCM quota-storm shedding**: when the shared project quota is exhausted, the relay-side project circuit breaker sheds with `503 upstream_throttled` before forwarding (Phase 2 / §6.4), rather than hammering FCM into a 429 stampede.
|
||||
- **HA** (spec §14.2): multiple stateless replicas behind the LB; rolling deploys; kill-safety (a drained/killed replica is covered by the caller's outbox retry). Relay-down delays, never loses.
|
||||
|
||||
**Tests.**
|
||||
|
||||
- Load: hundreds-of-users burst (latency p50/p95/p99, error rate, `429` correctness).
|
||||
- Concurrency: same-key idempotency → at-most-once upstream (assert against a counting fake).
|
||||
- Security: secret-manager load path; no secret in image (`docker history`/scan); TLS config (min version, HSTS).
|
||||
- Security: forged caller `X-Forwarded-For`/`X-Real-IP` does **not** change the resolved client IP / `egress_ip` (only the trusted-proxy header from an allowlisted peer is honored); per-IP throttle cannot be bypassed.
|
||||
- Drain: rolling-restart drains in-flight requests via `srv.Shutdown` without dropping a completed send, `/readyz` flips to `503` before draining, and **no orphaned in-flight idempotency markers** remain afterward (an immediate caller retry does not get a spurious `409`).
|
||||
- Resilience: FCM quota-exhaustion → relay sheds with `503 upstream_throttled` (project circuit breaker) instead of forwarding into a 429 storm.
|
||||
- Credential health: the synthetic startup/periodic check flips `relay_credential_healthy` and `/readyz` when the loaded credential is rejected upstream (auth-layer `403`), distinct from a token-layer `400`.
|
||||
|
||||
**Acceptance criteria.**
|
||||
|
||||
- [ ] Secrets load exclusively from the secret manager; image/repo scans show no embedded `.p8`/SA JSON/pepper.
|
||||
- [ ] A hundreds-of-users burst is handled within the per-account caps with correct `429`/`Retry-After`; one upstream connection sustains the load.
|
||||
- [ ] Concurrent identical-key sends produce **at most one** upstream call.
|
||||
- [ ] A rolling restart drains in-flight requests, flips `/readyz` to `503` first, and leaves **no orphaned in-flight idempotency markers**.
|
||||
- [ ] The synthetic credential check gates `/readyz`/`relay_credential_healthy` on real upstream acceptance; the first `upstream_auth_error` pages ops; the rotation runbook is documented.
|
||||
- [ ] Forged caller forwarding headers cannot poison `egress_ip` or bypass the per-IP throttle.
|
||||
- [ ] TLS 1.2+ enforced, HSTS set; `/metrics`+`/readyz` internal-only; staging deployment validated before production rollout.
|
||||
|
||||
---
|
||||
|
||||
## 5. Testing Strategy
|
||||
|
||||
### 5.1 Layers
|
||||
|
||||
| Layer | What it covers | Provider creds? |
|
||||
|---|---|---|
|
||||
| **Unit** | Field validation, JWT/token lifecycle math, key hashing/compare, error-table mappings, redaction, metric labels | No |
|
||||
| **Contract** | Exact `02`/`03` request/response shapes: `DisallowUnknownFields` rejects every disallowed field; success/error bodies are byte-exact; `pushwire` golden files | No |
|
||||
| **Integration** | Real upstream behavior: APNs **sandbox** sends; FCM **`validateOnly:true`** validation; PG migrations; Redis rate-limit/idempotency | APNs `.p8` / FCM SA (sandbox) |
|
||||
| **Redaction assertions** | No raw token/bearer/JWT/OAuth2/payload ever reaches a log or op-log | No |
|
||||
| **Load / pacing** | Hundreds-of-users burst; per-account cap + `429`/`Retry-After`; upstream connection reuse sustains throughput | Optional (fake upstream) |
|
||||
| **Idempotency-under-concurrency** | Same key, many concurrent requests → at-most-once upstream send | No (fake upstream) |
|
||||
| **Failure-mode** | APNs `GOAWAY`/mid-stream reset mapping (no lost/double send); JWT-expiry single-flight (one regen, zero `TooManyProviderTokenUpdates`); Redis dropped mid-request (idempotency fail-closed, rate-limit bounded fail-open); graceful-drain leaves no orphaned idempotency lock; FCM quota-exhaustion shedding | No (fakes) |
|
||||
|
||||
### 5.2 Contract tests against the 02/03 shapes
|
||||
|
||||
For each endpoint, a golden corpus of requests/responses derived from spec §5.1/§5.2:
|
||||
|
||||
- **Valid** `private_alert` / `background_wake` (APNs) and `private_data` / `background_wake` (FCM) requests → assert the built upstream payload + headers are **byte-exact** vs the spec (the `pushwire` golden files).
|
||||
- **Every disallowed field** (`title`, `body`, `image_url`, `media_id`, `username`, `server_url`, `notification`, …) → `400 unexpected_field`.
|
||||
- **Every validation failure** → its exact `error.code` (`invalid_token`, `invalid_environment`, `invalid_mode`, `invalid_collapse_id`, `topic_not_allowed`, `project_not_allowed`, `package_not_allowed`, `missing_idempotency_key`, …).
|
||||
- The same `pushwire` golden corpus is shared with `silo-server`'s `custom_apns`/`custom_fcm` tests. Because a copied package in one repo cannot fail the other repo's CI on its own, the mirror is a **checked-in generated artifact with a committed checksum** that **both** repos verify in CI against the canonical hash (see §3 sync runbook) — a divergence fails CI on both sides regardless of which repo changed first.
|
||||
- **Closed-allowlist assertion (privacy guarantee):** a dedicated test asserts the FCM `data` map keys and the APNs payload key set are **exactly** the closed allowlist, so adding any key (e.g. smuggling content into `data`) fails CI even if golden files are regenerated. The content-free guarantee is enforced structurally, not only by golden equality.
|
||||
|
||||
### 5.3 Integration against sandbox / validateOnly
|
||||
|
||||
- **APNs sandbox** (`api.sandbox.push.apple.com`): a real send returns 200 + `apns_id`; a deliberately invalid token returns the mapped `bad_device_token` / `unregistered`. Run behind `-tags=integration`, only when `.p8` secrets are present (CI `integration` job).
|
||||
- **FCM `validateOnly:true`** (reference §2.5): a valid request validates without delivering; a malformed token surfaces `invalid_argument`/`unregistered`. **Never** `validateOnly` on production sends.
|
||||
|
||||
### 5.4 Redaction assertions
|
||||
|
||||
Capture log + op-log output across success and every error path; assert via regex that none of: the bearer value, the raw `rk_` secret, a raw device token, the relay JWT, or an OAuth2 access token ever appears, and that `token_hash` is present where a token was involved (spec §12/§13.1).
|
||||
|
||||
### 5.5 Load / pacing test (hundreds-of-users burst)
|
||||
|
||||
Simulate a burst of several hundred concurrent device sends (one account, mixed APNs/FCM) against a **fake upstream** that records call counts and timing:
|
||||
|
||||
- Assert per-account rate limiting holds (10 req/s burst + 50k/day; spec §9.2) and over-limit responses are `429` with a **relative** `Retry-After`.
|
||||
- Assert one upstream HTTP/2 connection is reused (no connection-per-push) and sustains the burst (reference §1.3/§3.1).
|
||||
- Assert latency percentiles and zero double-sends.
|
||||
|
||||
### 5.6 Idempotency-under-concurrency test
|
||||
|
||||
Fire N (e.g. 50) concurrent requests sharing one `Idempotency-Key` at a fake upstream that increments a counter per call. Assert: **exactly one** upstream call; the other N−1 either replay the stored result or receive `409 idempotency_conflict`; a follow-up with a **different** payload but the same key returns `422 idempotency_key_reuse`. This proves at-most-once upstream send under the caller's concurrent outbox retries (spec §10.2).
|
||||
|
||||
### 5.7 Failure-mode tests (the exact modes on-call hits)
|
||||
|
||||
Beyond the happy path and the error-table mappings, the plan tests every failure mode that actually pages an engineer (all use fakes — no provider creds):
|
||||
|
||||
- **APNs `GOAWAY` / mid-stream reset.** A fake APNs upstream injects `GOAWAY` (or resets the stream) before a final response mid-burst → the caller-facing result is `503 upstream_unavailable`, the op-log/metric is tagged `connection_severed`, and a counting fake confirms **no lost or double send** (no transparent retry).
|
||||
- **JWT expiry under concurrency.** Force the cached APNs JWT to expire under a concurrent burst → assert **exactly one** single-flighted regeneration and **zero** `429 TooManyProviderTokenUpdates`.
|
||||
- **Redis dropped mid-request** (not just down at start) → idempotency fails **closed** (`503`); rate-limit degrades to **bounded** fail-open; a lock whose TTL expires mid-send cannot be stolen (upstream deadline `<` `lock_ttl`).
|
||||
- **Graceful-drain idempotency.** A rolling restart during in-flight sends leaves **no orphaned in-flight idempotency markers** (an immediate caller retry does not eat a spurious `409`); `/readyz` flips to `503` before draining.
|
||||
- **FCM quota-exhaustion shedding.** With the shared project quota exhausted, the relay-side project circuit breaker sheds with `503 upstream_throttled` instead of forwarding into a 429 storm.
|
||||
|
||||
---
|
||||
|
||||
## 6. Deployment
|
||||
|
||||
### 6.1 Container build
|
||||
|
||||
- `deploy/Dockerfile`: multi-stage; static `relay` (and `relayctl`) binaries on a distroless/minimal base. **No credentials baked in** (spec §11). Non-root user; read-only filesystem where possible.
|
||||
- Image carries only the binary + CA roots (ensure USERTrust RSA present — default in `ca-certificates`; spec §6.3).
|
||||
|
||||
### 6.2 Secret provisioning
|
||||
|
||||
| Secret | Source | Notes |
|
||||
|---|---|---|
|
||||
| APNs `.p8` + Key ID + Team ID | secret manager (or mounted file) | per official team; never env/image (spec §14.3) |
|
||||
| FCM SA JSON **or** Workload Identity | GCP keyless (on-GCP) / secret manager JSON (off-GCP) | open Q#4 (spec §15) |
|
||||
| API-key HMAC pepper | secret manager | rotation is heavier (open Q#6) |
|
||||
| Postgres DSN, Redis URL | secret manager | privileged `relayctl` DSN held only by operators |
|
||||
|
||||
Rotation: issue a new APNs key / FCM credential, deploy, retire the old; the relay loads secrets at startup so a rolling restart picks them up (spec §14.3). Optional SIGHUP re-read.
|
||||
|
||||
### 6.3 TLS, health, metrics wiring
|
||||
|
||||
- TLS 1.2+ (prefer 1.3), HTTP/2, HSTS on `relay.silo.app`; terminate at LB or in-process (spec §14.1).
|
||||
- **Public:** `/v1/apple/send`, `/v1/fcm/send`, `/healthz`. **Internal-only:** `/readyz`, `/metrics` (spec §11/§14.1).
|
||||
- Liveness probe → `/healthz`; readiness probe → `/readyz`; Prometheus scrapes `/metrics` on the internal network.
|
||||
|
||||
### 6.4 Monitoring / alerting
|
||||
|
||||
Alert on (from the §13.2 catalog):
|
||||
|
||||
- **First `upstream_auth_error` → page ops.** Alert on the caller-facing `upstream_auth_error` reason (the relay's genuine `.p8`/SA/OAuth2 credential failure — a non-200 from `oauth2.googleapis.com/token`, or `401 UNAUTHENTICATED`/`403 PERMISSION_DENIED`/`InvalidProviderToken` on send) > 0 → **credential misconfig, page ops**: never caller-fixable. **Do NOT** key this alert on `THIRD_PARTY_AUTH_ERROR`, which on this pure-Android path indicates a per-token/registration anomaly, not a relay-credential problem (see Phase 4 reclassification) — surface it for investigation at most, never page.
|
||||
- **Proactive credential-health alert.** Alert if the periodic synthetic credential check (Phase 6: a scheduled `relayctl ping-upstream` / `validateOnly` FCM send + APNs auth-exercising probe, plus the `relay_credential_healthy` gauge wired into `/readyz`) reports the loaded `.p8`/SA/Workload-Identity credential is no longer accepted upstream — Apple actively revokes leaked `.p8` keys and SA keys/bindings can be rotated out from under the relay, so this must fire **before** production sends start failing.
|
||||
- Sustained `relay_rate_limit_degraded_total` increase → Redis degraded (fail-open active; in-process per-account fallback limiter engaged).
|
||||
- `relay_redis_up == 0` / `relay_pg_up == 0`; any provider showing `relay_provider_healthy{provider=...} == 0` → that upstream is degraded (per-provider, not a whole-replica outage).
|
||||
- Elevated `relay_requests_total{status_code="5xx"}` rate; `relay_request_duration_seconds` p99 regression.
|
||||
- `relay_upstream_requests_total{upstream_reason="QUOTA_EXCEEDED"}` and **low `relay_fcm_project_quota_headroom`** → shared FCM project quota pressure: the relay-side project limiter is shedding with `503 upstream_throttled` before hammering FCM (watch Cloud Console; spec §14.5). Treat as a multi-tenant fairness event — one noisy account must not exhaust the ~600k/min project quota shared across all accounts.
|
||||
- Accounts approaching/hitting the **per-account daily cap** (`relay_rate_limited_total{kind="daily"}`) → a legitimate big-release burst can hit 50k/day; surface for an operator to consider a temporary cap bump.
|
||||
- Anomalous `relay_auth_failures_total` rate from an egress IP → possible key-guessing (see §6.4 anti-brute-force; the per-IP limiter consumes the resolved client IP, not a caller-supplied header). Note: the fine-grained `reason` (unknown-prefix vs hash-mismatch) is internal-only and must not be exposed to tenants.
|
||||
|
||||
### 6.5 Rollout
|
||||
|
||||
- **Staging first** (spec §14.4): separate hostname + separate accounts so client QA never touches production quotas. Validate APNs sandbox + FCM `validateOnly` end-to-end.
|
||||
- **HA / rolling** (spec §14.2): multiple stateless replicas behind the LB; rolling deploy with drain (`srv.Shutdown`); a killed replica is covered by the caller's outbox retry — **relay-down delays, never loses**.
|
||||
- Single region in v1, close to PG/Redis (spec §14.5).
|
||||
|
||||
---
|
||||
|
||||
## 7. Provisioning Checklist (the long pole — start day 1)
|
||||
|
||||
> These gate the phases that touch live providers (Phase 3 APNs, Phase 4 FCM, Phase 6 production). Run them **in parallel** with Phases 0–2, which need no credentials.
|
||||
|
||||
### 7.1 Apple (gates Phase 3 + Phase 6)
|
||||
|
||||
- [ ] Apple Developer **Program account / team** with push entitlement (capture the 10-char **Team ID**).
|
||||
- [ ] Generate an **APNs Auth Key (`.p8`)** in the developer portal; capture the **Key ID** (10 chars) and download the `.p8` **once** (Apple shows it once). Store in the secret manager (spec §14.3).
|
||||
- [ ] Consider Feb-2025 **scoped keys** (team-scoped or topic-specific) for least privilege (reference §1.2).
|
||||
- [ ] Register per-platform **bundle topics** for the official build config and seed the account allowlist (spec §8.4): `com.continuum.app.ios`, `com.continuum.app.tvos`, `com.continuum.app.macos`.
|
||||
- [ ] Standardize on the **sandbox host** `api.sandbox.push.apple.com` (reference §1.3; `api.development.push.apple.com` is an equivalent still-resolving alias — no build-time resolution needed) and confirm the APNs server TLS root (**USERTrust RSA**, present in standard pools; spec §6.3 / reference §1.7).
|
||||
- [ ] Decide `apns2` vs hand-rolled client (open Q#3) before Phase 3.
|
||||
|
||||
### 7.2 Firebase / Google (gates Phase 4 + Phase 6)
|
||||
|
||||
- [ ] Firebase **project(s)** for the official build config: `continuum-prod-android` (and a staging project if desired).
|
||||
- [ ] Register the Android **package** `com.continuum.app.android`; seed the FCM allowlist `(project_id, package_name)` (spec §8.4).
|
||||
- [ ] Provision a **service account** with the least-privilege `firebase.messaging` scope (reference §2.2). On GCP, prefer keyless **Workload Identity / attached SA**; off-GCP, download the **service-account JSON** and store it in the secret manager — never in repo/image (reference §2.2/§4.5; open Q#4).
|
||||
- [ ] Confirm the global send host `fcm.googleapis.com` (no regional endpoints; reference §2.3) and the default ~600k msgs/min project quota (reference §2.7 / spec §14.5).
|
||||
|
||||
### 7.3 Cross-cutting
|
||||
|
||||
- [ ] Secret manager chosen and wired (KMS/Vault/cloud secret manager) for `.p8`, SA JSON, pepper, DSNs (spec §11 / Phase 6).
|
||||
- [ ] DNS + TLS cert for `relay.silo.app` (and the staging hostname).
|
||||
|
||||
---
|
||||
|
||||
## 8. Risks, Mitigations, and Milestone Sequencing
|
||||
|
||||
### 8.1 Risks and mitigations
|
||||
|
||||
| # | Risk | Likelihood | Impact | Mitigation |
|
||||
|---|---|---|---|---|
|
||||
| R1 | **Provider provisioning slips** (Apple/Firebase approvals) — the real long pole | Med | High (blocks Phase 3/4/6) | Start §7 on day 1 in parallel; Phases 0–2 + all unit/contract tests need no creds; fakes for upstream |
|
||||
| R2 | `sideshow/apns2` is unmaintained (no release since Oct 2024) | Med | Med | Hide behind `internal/apns` interface; prototype hand-rolled `x/net/http2`+`golang-jwt/jwt/v5` in Phase 3; decide per open Q#3 |
|
||||
| R3 | **JWT/token mishandling** (over-frequent refresh → APNs `429 TooManyProviderTokenUpdates`; per-request mint → throttling; concurrent-403 stampede) | Med | High | Cache + mutex; ~50-min APNs refresh (never <20min), eager-on-expired; **single-flighted regeneration** so a concurrent burst of `ExpiredProviderToken` yields exactly one refresh per ~20-min window; OAuth2 `TokenSource` auto-refresh; metrics `relay_apns_jwt_refresh_total`/`relay_fcm_token_refresh_total` |
|
||||
| R4 | **Double-send** under concurrent caller retries (incl. lock-TTL expiry mid-send, and stale in-flight markers after a killed/drained replica) | Med | High | Redis `SET NX` idempotency, **fail-closed**; pin the upstream call deadline **strictly shorter than `lock_ttl`** (≈10 s upstream vs 30 s lock) and refuse to send if the deadline already passed the lock TTL; release/downgrade the in-flight marker on handler context-cancellation; at-most-once test (§5.6); drain test asserting no orphaned in-flight markers (§5.7); key includes `attempt_number` |
|
||||
| R5 | **Redis outage / eviction** | Low | Med | Rate-limit **fails open but bounded** (in-process per-account fallback limiter, logged + metric); idempotency **fails closed** (`503`, retryable via caller outbox); idempotency keyspace requires a **non-evicting `maxmemory-policy` (`noeviction`)** or a dedicated Redis/db so a rate-limit eviction can never silently drop an in-flight idempotency lock (which would violate at-most-once) |
|
||||
| R6 | **Content/identity leak** despite design | Low | Critical | Structural: `DisallowUnknownFields`, content-free schema, data-only FCM, badge-off-by-default, redaction in middleware, op-logs hold only `token_hash` |
|
||||
| R7 | **Cross-tenant push** (one account targeting another's topic/token) | Low | High | Per-account allowlist enforced after auth; namespaced Redis keys; per-account-scoped allowlist cache |
|
||||
| R8 | **Stolen relay API key** (incl. abuse-amplification during a Redis outage, and per-IP-throttle bypass via forged forwarding headers) | Low | Med | Revocable keys (`relayctl key revoke`), per-account rate caps + allowlist bound spend; **bounded** fail-open (in-process per-account cap when Redis is down — never unlimited); per-IP auth-failure throttling keyed on a **trusted-proxy-resolved client IP** (caller-supplied `X-Forwarded-For`/`X-Real-IP` ignored) |
|
||||
| R9 | **`pushwire` drift** between relay and `silo-server` | Med | Med | One **canonical source repo**; mirror is a checked-in generated artifact with a committed **checksum** verified in CI on **both** sides; golden-file + closed-allowlist-key tests fail CI on divergence regardless of regeneration order |
|
||||
| R10 | **APNs TLS root rotation** (USERTrust) | Low | Low | Use `api.sandbox.push.apple.com` (the `api.development.*` alias is equivalent and still resolves — not a risk); rely on the standard trust store (already includes USERTrust RSA) |
|
||||
| R11 | **FCM high-priority deprioritization** over 7-day window (reference §2.8) | Low | Low | Honor `03`'s `priority:high` for `private_data` (app surfaces a notification); caller-side `priority:normal` fallback is the lever (open Q#10) |
|
||||
| R12 | **Op-log volume** at 50k/day/account across many accounts (unbounded table → disk fills) | Med | Low | **Shipped** retention GC (`relayctl logs --gc` / background sweeper: 14d success / 90d failure) on a schedule with an owner; time-partitioning DDL (or a documented partition/offload decision); table-growth/disk alert |
|
||||
|
||||
### 8.2 Milestone sequencing & rough effort
|
||||
|
||||
| Milestone | Phases | Effort | Provider creds needed | Parallelizable |
|
||||
|---|---|---|---|---|
|
||||
| **M0 — Skeleton green** | Phase 0 | ~M (1 wk) | No | — |
|
||||
| **M1 — Storage + admin** | Phase 1 | ~L (1 wk) | No | with §7 provisioning |
|
||||
| **M2 — Request path (no upstream)** | Phase 2 | ~L (1 wk) | No | with §7 provisioning |
|
||||
| **M3 — APNs live (sandbox)** | Phase 3 | ~L (1 wk) | **Apple `.p8`** | Phase 3 ∥ Phase 4 |
|
||||
| **M4 — FCM live (validateOnly)** | Phase 4 | ~L (1 wk) | **Firebase SA** | Phase 4 ∥ Phase 3 |
|
||||
| **M5 — Observability** | Phase 5 | ~M (3 d) | No | — |
|
||||
| **M6 — Hardened + deployed** | Phase 6 | ~L (1 wk) | All + secret manager | — |
|
||||
|
||||
**Rough total:** ~6–7 engineer-weeks of code, with **§7 provisioning running in parallel from day 1** as the gating long pole for M3/M4/M6. Phases 3 and 4 can be split across two engineers once Phase 2 lands. The critical path is: M0 → M1 → M2 → (M3 ∥ M4) → M5 → M6, intersected with provisioning availability.
|
||||
|
||||
---
|
||||
|
||||
## 9. Open Questions (carried from spec §15)
|
||||
|
||||
These do not block starting; resolve at the noted phase.
|
||||
|
||||
1. **Per-account rate limits — real numbers** (resolve from logs post-M6; spec §9.2). v1 ships ~10 req/s burst + 50k/day.
|
||||
2. **APNs sandbox hostname** (resolved — note only). Use `api.sandbox.push.apple.com`; `api.development.push.apple.com` is an equivalent, still-resolving sandbox alias. The two are interchangeable, so no build-time resolution is required — this is a standardization note, not an open question.
|
||||
3. **`apns2` vs hand-rolled APNs client** — decide before Phase 3 (§3, reference §3.1).
|
||||
4. **FCM credentials: GCP-keyless vs off-GCP JSON** — resolve in deployment design (Phase 6).
|
||||
5. **`apns-expiration` / `android.ttl` defaults** (largely resolved). v1 ships a **finite** default (not omitted/30-day): a few hours for `private_alert`, short TTL for `background_wake`, with a matching finite FCM `android.ttl`; both configurable. Only the exact numeric values want product input.
|
||||
6. **API-key hashing: HMAC pepper vs plain salted SHA-256** — plan chooses HMAC pepper; confirm rotation tradeoff (Phase 1).
|
||||
7. **Idempotency-Key spec status** — header name + Stripe-style semantics are de-facto; re-check for an RFC.
|
||||
8. **Op-log volume & retention** (resolved into a Phase 5 deliverable). Ship `relayctl logs --gc` (or a background sweeper): 14d success / 90d failure, on a schedule with an owner, plus time-partitioning DDL (or a documented partition/offload decision) and a table-growth alert — not deferred.
|
||||
9. **Multi-region** — out of scope for v1; design later if latency to distant self-hosters matters.
|
||||
10. **FCM priority deprioritization** — honor `03`'s `priority:high`; revisit only if metrics show systematic deprioritization.
|
||||
@@ -0,0 +1,554 @@
|
||||
# APNs & FCM 2026 Reference — Privacy-Preserving Push Relay
|
||||
|
||||
> **Provenance.** This document reflects **2026-current** Apple (APNs) and Google (FCM HTTP v1) developer documentation, plus the current Go ecosystem and OWASP guidance. Every non-obvious factual claim carries an inline citation in the form `(source: URL, date)`. Where a primary source disagreed with secondary/blog material, the primary source wins and the secondary claim is flagged as inaccurate. Where a fact remains uncertain, it is called out explicitly rather than overstated.
|
||||
>
|
||||
> **Compile date:** _<!-- PLACEHOLDER: set at compile time -->_
|
||||
>
|
||||
> **Commands assume the repository root is the cwd.** No local absolute filesystem paths appear in this document; all repo references are repository-relative.
|
||||
|
||||
## Scope
|
||||
|
||||
This is the factual foundation for a self-hosted, privacy-preserving push **relay** — a server that accepts notification requests from authenticated tenants and forwards them to Apple Push Notification service (APNs) and Firebase Cloud Messaging (FCM) on their behalf, without the providers (or other tenants) learning more than necessary. It captures the wire-level contracts (auth, endpoints, headers, payloads, errors, throttling), the recommended Go libraries and tuning, and the security/ops practices the spec and implementation plan are written against. It does **not** cover client-side SDK integration except where client behavior (token rotation, notification-permission gating) constrains relay design.
|
||||
|
||||
---
|
||||
|
||||
## 1. APNs (2026)
|
||||
|
||||
### 1.1 Provider authentication — token-based (JWT, ES256)
|
||||
|
||||
APNs supports **both** token-based and certificate-based provider authentication over HTTP/2 + TLS in 2026; Apple does **not** deprecate certificate-based auth. Both have dedicated docs and no deprecation notice (source: https://developer.apple.com/documentation/usernotifications/setting-up-a-remote-notification-server, 2026-06-13). The relay should use **token-based** (`.p8`) auth: the signing key does not expire (it can be revoked), one key covers sandbox + production and all/subset of a team's apps, and there is no yearly certificate renewal.
|
||||
|
||||
> **Correction vs. common blog claims:** Third-party posts claiming Apple is "deprecating / shutting down certificate-based auth in 2025" are **inaccurate**. The only confirmed 2025 changes are (a) the APNs **server** TLS root-CA rotation (§1.7) and (b) the optional token-key hardening features (§1.2). Neither deprecates provider auth (source: https://developer.apple.com/documentation/usernotifications/setting-up-a-remote-notification-server, 2026-06-13).
|
||||
|
||||
**JWT structure.** APNs supports **only the ES256** algorithm (ECDSA over P-256); no other algorithm is accepted (source: https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns, 2026-06-13).
|
||||
|
||||
```jsonc
|
||||
// JWT header
|
||||
{
|
||||
"alg": "ES256", // ES256 ONLY
|
||||
"kid": "ABC123DEFG" // 10-char Key ID from the developer account
|
||||
}
|
||||
// JWT claims
|
||||
{
|
||||
"iss": "DEF123GHIJ", // 10-char Team ID
|
||||
"iat": 1437179036 // issued-at, seconds since Epoch (UTC)
|
||||
}
|
||||
```
|
||||
|
||||
The JWT is signed with the `.p8` ECDSA P-256 private key using ES256. It is sent on **every** notification request as an HTTP/2 header in Base64URL-encoded JWT form (source: https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns, 2026-06-13):
|
||||
|
||||
```http
|
||||
authorization: bearer eyAia2lkIjog...MEYCIQDz...
|
||||
```
|
||||
|
||||
**Lifecycle & regeneration cadence (critical):**
|
||||
|
||||
| Rule | Value | Source |
|
||||
|---|---|---|
|
||||
| Max token age (`iat`) | ≤ 1 hour, else `ExpiredProviderToken` (403) | (source: …/establishing-a-token-based-connection-to-apns, 2026-06-13) |
|
||||
| Refresh **no more** often than | once every **20 minutes** (per connection) | (same) |
|
||||
| Refresh **no less** often than | once every **60 minutes** | (same) |
|
||||
| Over-frequent refresh → | `TooManyProviderTokenUpdates` (429) | (source: …/handling-notification-responses-from-apns, 2026-06-13) |
|
||||
|
||||
**Relay pattern:** Generate **one** ES256 JWT, cache it in memory behind a mutex, and reuse it across **all** senders/connections/topics. Regenerate on a ~50-minute timer (well inside the 1-hour window, never < 20 min apart), or eagerly on a `403 ExpiredProviderToken`. Never sign a fresh JWT per request.
|
||||
|
||||
**One key, many servers.** A single key/token can be reused across multiple provider servers, multiple connections, and all (or a subset) of a team's apps/topics — confirmed verbatim by Apple: "You can use the same token from multiple provider servers" and "You can use one token to distribute notifications for all or a subset of your company's apps" (source: https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns, 2026-06-13). **Caveat:** a connection cannot map to multiple teams — "If you manage APNs for multiple developer accounts, open separate connection pools for each of them" (same source). For a multi-tenant relay, this means **per-team connection pools**.
|
||||
|
||||
### 1.2 Token-key hardening (Feb 2025, optional)
|
||||
|
||||
On **2025-02-17** Apple added optional scoped token keys; the classic unrestricted team key still works, and "you don't have to update your keys unless you want to take advantage of the new capabilities" (source: https://developer.apple.com/news/?id=wy4tb0uo, 2025-02-17). Two new key types:
|
||||
|
||||
| Key type | Scope | Limit |
|
||||
|---|---|---|
|
||||
| **Team-scoped key** | Sandbox **or** Production only; all topics in the team | max **2 keys per environment** |
|
||||
| **Topic-specific (topic-based) key** | Bound to specific bundle IDs (least privilege) | max **200 keys for Sandbox and 200 for Production, with up to 400 topics per topic-based key** |
|
||||
|
||||
(source: https://developer.apple.com/documentation/usernotifications/establishing-a-token-based-connection-to-apns, 2026-06-13 — verbatim scoped-key limit confirmed). For a multi-tenant relay these are a meaningful isolation lever: issue separately scoped `.p8` keys per environment/app rather than one all-powerful key.
|
||||
|
||||
### 1.3 Endpoints & HTTP/2 connection management
|
||||
|
||||
| Environment | Host | Ports |
|
||||
|---|---|---|
|
||||
| Production | `api.push.apple.com` | 443 or 2197 |
|
||||
| Development / sandbox | `api.sandbox.push.apple.com` | 443 or 2197 |
|
||||
|
||||
(source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13). Port 2197 is functionally equivalent to 443 and exists so firewalls can allow APNs while blocking other 443 traffic — keep it as a fallback. `api.push.apple.com` (production) and `api.sandbox.push.apple.com` (sandbox) are the current canonical hosts named in Apple's docs. The older `api.development.push.apple.com` is a **still-valid equivalent alias for sandbox**: as of 2026-06-13 both names resolve to the same Akamai CNAME (`api-sand-vs.push-apple.com.akadns.net`) and the same IP set (verified by DNS, e.g. `17.188.168.x`), so they hit the identical sandbox backend. We **standardize on `api.sandbox.push.apple.com`** (it matches the canonical docs and `sideshow/apns2` v0.25.0's `HostDevelopment` constant), but the alias is not dead — see §5.
|
||||
|
||||
- **Transport:** HTTP/2 with **TLS 1.2 or later** (TLS 1.3 acceptable). `:method POST`, `:path /3/device/<device_token>` where the token is the hex bytes of the device token (source: …/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- **Connection reuse:** "Reuse a connection as long as possible. In most cases, you can reuse a connection for many hours to days. If your connection is mostly idle, you may send a HTTP2 PING frame after an hour of inactivity." Reuse reduces bandwidth/CPU; rapid connect/disconnect is treated as DoS-like behavior (source: …/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- **Stream multiplexing:** "APNs allows multiple concurrent streams for each connection, but don't assume a specific number of streams." Read the server's `SETTINGS_MAX_CONCURRENT_STREAMS`; do not hardcode. The exact number varies by server load and auth type (same source). A single well-tuned connection handles thousands of pushes/sec.
|
||||
- **GOAWAY handling:** When APNs terminates a connection it sends an HTTP/2 `GOAWAY` frame whose payload includes JSON with a `reason` key. Apply RFC 9113 §6.8: stop opening new streams, finish in-flight streams with ID ≤ `Last-Stream-ID`, open a new connection for the rest; `NO_ERROR` = graceful shutdown, any other code = connection error (source: https://www.rfc-editor.org/rfc/rfc9113#section-6.8, 2022-06). The relay should drop a GOAWAY'd connection from the pool and transparently reconnect. Go's `net/http2` transport does most of this automatically when one `http.Client` is reused.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
R[Relay sender goroutines] -->|stream 1..N over 1 conn| C{HTTP/2 conn pool}
|
||||
C -->|POST /3/device/token| A[api.push.apple.com:443]
|
||||
A -. GOAWAY reason JSON .-> C
|
||||
C -->|drop + reconnect| C2[new conn]
|
||||
C2 -->|in-flight <= Last-Stream-ID finish on old conn| A
|
||||
```
|
||||
|
||||
### 1.4 Request headers
|
||||
|
||||
`:method = POST`, `:path = /3/device/<device_token>`. Payload is **uncompressed** JSON.
|
||||
|
||||
| Header | Required | Allowed values / format | Notes |
|
||||
|---|---|---|---|
|
||||
| `authorization` | yes (token auth) | `bearer <ES256 JWT>` | Ignored on cert connections |
|
||||
| `apns-topic` | yes | bundle ID, optionally with a type suffix (see §1.5) | `MissingTopic`/`BadTopic` (400) on error |
|
||||
| `apns-push-type` | required on **watchOS 6+**, strongly recommended elsewhere | `alert`, `background`, `controls`, `fileprovider`, `liveactivity`, `location`, `mdm`, `pushtotalk`, `voip`, `widgets`, `complication` | Must match payload; invalid → `InvalidPushType` (400) |
|
||||
| `apns-priority` | no (default 10) | `10`, `5`, `1` | See §1.4.1; invalid → `BadPriority` (400) |
|
||||
| `apns-expiration` | no | UNIX epoch seconds (UTC) | `0` = deliver once, no store; nonzero = store & retry up to **30 days**; invalid → `BadExpirationDate` (400) |
|
||||
| `apns-collapse-id` | no | ≤ **64 bytes** | Same value merges notifications on-device (latest wins); >64B → `BadCollapseId` (400) |
|
||||
| `apns-id` | no | canonical UUID (8-4-4-4-12, lowercase hex) | Echoed in response; auto-generated if omitted; invalid → `BadMessageId` (400) |
|
||||
|
||||
(source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13). `apns-push-type` required on watchOS 6+ confirmed (source: https://developer.apple.com/documentation/usernotifications, 2026-06-13).
|
||||
|
||||
**`apns-topic` suffixes by push type** (source: …/sending-notification-requests-to-apns, 2026-06-13):
|
||||
|
||||
| Push type | `apns-topic` suffix |
|
||||
|---|---|
|
||||
| `complication` | `.h.complication` |
|
||||
| `controls` | `.push-type.controls` |
|
||||
| `fileprovider` | `.pushkit.fileprovider` |
|
||||
| `liveactivity` | `.push-type.liveactivity` |
|
||||
| `location` | `.location-query` |
|
||||
| `pushtotalk` | `.voip-ptt` |
|
||||
| `voip` | `.voip` |
|
||||
| `widgets` | `.push-type.widgets` |
|
||||
| `alert` / `background` | (none — bare bundle ID) |
|
||||
|
||||
#### 1.4.1 `apns-priority` semantics (corrected)
|
||||
|
||||
`apns-priority` is optional; if omitted, APNs sets priority to 10. Three values, with these exact semantics (source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13):
|
||||
|
||||
| Value | Meaning |
|
||||
|---|---|
|
||||
| `10` | Send the notification **immediately** (use for alert/sound/badge). |
|
||||
| `5` | Send based on **power considerations** on the user's device. |
|
||||
| `1` | Prioritize power over all factors and **do not wake the device** (lowest-power). |
|
||||
|
||||
- For `apns-push-type=background`, Apple's guidance is explicit and **narrower** than some sources state: "**Always use priority 5. Using priority 10 is an error.**" Apple does **not** list priority `1` as an accepted alternative for background pushes (source: same, 2026-06-13).
|
||||
- The claim that priority `1` is a "new tier for Live-Activity-style delivery" is **not supported** by Apple's docs. Live Activity updates use `apns-push-type=liveactivity` (typically priority 10 for immediate or 5 for throttled), not a priority-1 mechanism. Priority `1` is simply the lowest-power option (source: same, 2026-06-13).
|
||||
|
||||
### 1.5 Payload shapes & size limits
|
||||
|
||||
**Size limits** (source: https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification, 2026-06-13):
|
||||
|
||||
| Type | Max payload |
|
||||
|---|---|
|
||||
| Regular (alert/background/etc.) | **4096 bytes (4 KB)** |
|
||||
| VoIP (PushKit) | **5120 bytes (5 KB)** |
|
||||
|
||||
Oversize → `413 PayloadTooLarge`. Payload must **not** be compressed. Live Activity payloads use the standard 4 KB limit.
|
||||
|
||||
**Payload root** = reserved `aps` dictionary + optional custom keys. `aps` keys (source: same, 2026-06-13): `alert`, `badge`, `sound`, `thread-id`, `category`, `content-available`, `mutable-content`, `target-content-id`, `interruption-level`, `relevance-score`, `filter-criteria`.
|
||||
|
||||
**Alert (user-facing) example:**
|
||||
|
||||
```json
|
||||
{
|
||||
"aps": {
|
||||
"alert": { "title": "Title", "subtitle": "Sub", "body": "Body text" },
|
||||
"badge": 1,
|
||||
"sound": "default",
|
||||
"thread-id": "thread-42",
|
||||
"interruption-level": "active",
|
||||
"relevance-score": 0.8
|
||||
},
|
||||
"custom-key": "opaque-relay-payload"
|
||||
}
|
||||
```
|
||||
|
||||
The `alert` sub-dictionary supports `title`/`subtitle`/`body` plus localization keys (`loc-key`/`loc-args`, `title-loc-key`/`title-loc-args`, `subtitle-loc-key`/`subtitle-loc-args`), `launch-image`, and the legacy `action-loc-key` (source: same, 2026-06-13).
|
||||
|
||||
`interruption-level` accepts exactly: `passive`, `active` (default), `time-sensitive` (needs Time Sensitive Notifications entitlement), `critical` (needs Critical Alerts entitlement, Apple-approved). Critical sound uses a **dictionary**: `{ "critical": 1, "name": "...", "volume": 0.0–1.0 }` (source: same, 2026-06-13).
|
||||
|
||||
**Background (silent) example** — must set `apns-push-type=background`, `apns-priority=5`, and an `aps` containing **only** `content-available: 1` (no `alert`/`badge`/`sound`):
|
||||
|
||||
```json
|
||||
{ "aps": { "content-available": 1 } }
|
||||
```
|
||||
|
||||
(source: https://developer.apple.com/documentation/usernotifications/pushing-background-updates-to-your-app, 2026-06).
|
||||
|
||||
**Live Activity** remote pushes use `apns-push-type=liveactivity`, `apns-topic=<bundleID>.push-type.liveactivity`, **token-based auth only**. The `aps` adds: `event` (`start`|`update`|`end`), `content-state` (decodes into the app's `ActivityAttributes.ContentState`), `timestamp`, `stale-date`, `dismissal-date` (UNIX seconds), optional `relevance-score` and `alert`. `attributes-type` + `attributes` are included **only** for `event: start` (push-to-start). For Live Activities, `relevance-score` is an **unconstrained Double** (Smart Stack ordering), not the 0–1 range used by `UNNotificationContent.relevanceScore` (source: https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications, 2026-06).
|
||||
|
||||
### 1.6 Status codes & reason strings
|
||||
|
||||
**HTTP status codes** (source: https://developer.apple.com/documentation/usernotifications/handling-notification-responses-from-apns, 2026-06-13): 200, 400, 403, 404, 405, 410, 413, 429, 500, 503.
|
||||
|
||||
For unsuccessful requests the body is a JSON object `{"reason":"<String>"}`; for **410** it additionally includes a `timestamp` (Number, **milliseconds** since Epoch) indicating when the token became invalid for the topic (source: same).
|
||||
|
||||
| Status | Reason strings | Meaning / handling |
|
||||
|---|---|---|
|
||||
| 200 | — | Success. Echoed `apns-id` header; in **dev**, `apns-unique-id` header (response-only) for Push Notifications Console Delivery Log. |
|
||||
| 400 | `BadCollapseId`, `BadDeviceToken`, `BadExpirationDate`, `BadMessageId`, `BadPriority`, `BadTopic`, `DeviceTokenNotForTopic`, `DuplicateHeaders`, `IdleTimeout`, `InvalidPushType`, `MissingDeviceToken`, `MissingTopic`, `PayloadEmpty`, `TopicDisallowed` | Bad request. **Do not retry.** `BadDeviceToken`/`DeviceTokenNotForTopic` ⇒ purge token. |
|
||||
| 403 | `BadCertificate`, `BadCertificateEnvironment`, `ExpiredProviderToken`, `Forbidden`, `InvalidProviderToken`, `MissingProviderToken`, `UnrelatedKeyIdInToken`, `BadEnvironmentKeyIdInToken` | Auth/cert error. `ExpiredProviderToken` ⇒ regenerate JWT & retry once. `Invalid/MissingProviderToken` ⇒ config error (don't blind-retry). `UnrelatedKeyIdInToken` ⇒ open a new connection. |
|
||||
| 404 | `BadPath` | Invalid `:path`. Don't retry. |
|
||||
| 405 | `MethodNotAllowed` | Only `POST` allowed. Don't retry. |
|
||||
| 410 | `Unregistered`, `ExpiredToken` | Token no longer active for topic. **Permanently stop sending** to that token (until app re-registers). Use the `timestamp` (ms): only purge if your registration is older than it. Not a connection error / no disconnection. |
|
||||
| 413 | `PayloadTooLarge` | Payload exceeds 4 KB (5 KB VoIP). Don't retry. |
|
||||
| 429 | `TooManyProviderTokenUpdates`, `TooManyRequests` | Back off. `TooManyProviderTokenUpdates` ⇒ slow JWT refresh (≤ once/20 min). `TooManyRequests` ⇒ per-device-token backoff. |
|
||||
| 500 | `InternalServerError` | Transient. Retry after ~15 min with backoff. |
|
||||
| 503 | `ServiceUnavailable`, `Shutdown` | Transient. Retry after ~15 min with backoff. |
|
||||
|
||||
(source: https://developer.apple.com/documentation/usernotifications/handling-notification-responses-from-apns, 2026-06-13).
|
||||
|
||||
**Retry policy summary.** Never retry permanent errors (`BadDeviceToken`, `Unregistered`, `ExpiredToken`, `PayloadTooLarge`, `DeviceTokenNotForTopic`, `Forbidden`); back off on 429; retry 5xx after ~15 min with backoff. 410 is **not** treated as a connection error (source: same, 2026-06-13). Always parse the JSON `reason`, not just the status code.
|
||||
|
||||
### 1.7 APNs server TLS root rotation (2025) — trust-store only
|
||||
|
||||
APNs migrated its **server** TLS certificate to a new SHA-2 root, **USERTrust RSA Certification Authority** (a Sectigo root): sandbox **2025-01-20**, production **2025-02-24** (source: https://developer.apple.com/news/?id=09za8wzy, 2024-10-17; effective per https://developer.apple.com/news/upcoming-requirements/?id=02242025a, 2025-02-24).
|
||||
|
||||
- This affects only how the relay **validates the APNs server TLS chain** — it does **not** require updating the APNs SSL provider certificates Apple issued you, nor app/account certs.
|
||||
- Standard OS / Go system root pools (macOS/iOS, Linux `ca-certificates`, Go `crypto/x509` system pool) **already trust** USERTrust RSA, so no action is needed unless the relay **pins** or ships a **custom/minimal** trust store — in which case it must add this root or TLS handshakes fail after the cut-over dates.
|
||||
- No newer APNs cert/infrastructure requirement is published on Apple's Upcoming Requirements page as of June 2026 (source: https://developer.apple.com/news/upcoming-requirements/?id=02242025a, 2025-02-24).
|
||||
|
||||
### 1.8 Background-push throttling
|
||||
|
||||
Background (`content-available`) pushes are best-effort and throttled. Apple's verbatim guidance: "The number of background notifications allowed by the system depends on current conditions, but don't try to send more than **two or three per hour**." The system may delay, coalesce, or drop them based on battery/network/usage (source: https://developer.apple.com/documentation/usernotifications/pushing-background-updates-to-your-app, 2026-06). The older "throttle disabled when a debugger is attached" behavior is Apple-Forums developer-experience guidance, **not** in the current spec — do not rely on it.
|
||||
|
||||
### 1.9 Legacy binary protocol — dead
|
||||
|
||||
The legacy binary (TCP, ports 2195/2196) APNs protocol is fully retired; Apple set the migration deadline to the HTTP/2 provider API at **March 31, 2021** (source: https://developer.apple.com/news/?id=c88acm2b, 2020-10-09; earlier announced https://developer.apple.com/news/?id=11042019a, 2019-11-04). **Do not implement it.** There is no binary fallback in 2026.
|
||||
|
||||
### 1.10 Broadcast push (Live Activity channels) — situational
|
||||
|
||||
Broadcast push for Live Activities (channels) is current (iOS/iPadOS 18+). You `POST /4/broadcasts/apps/<bundleID>` over HTTP/2 + TLS 1.2+. Required headers: `apns-channel-id` (base64), `apns-push-type=liveactivity`, `apns-priority` (10/5/1), `apns-expiration`; optional `apns-request-id` (UUID, echoed back). Channels are managed via a **separate** channel-management API (`/1/apps/<bundleId>/channels`, `/1/apps/<bundleId>/all-channels`); limit 10,000 channels per app per environment (source: https://developer.apple.com/documentation/usernotifications/sending-broadcast-push-notification-requests-to-apns, 2026-06-13).
|
||||
|
||||
> **Hostname nuance (correction):** Apple's broadcast **send** code samples use broadcast-specific hosts `api-broadcast.push.apple.com` (prod) / `api-broadcast.sandbox.push.apple.com` (sandbox) — **not** the plain `api.push.apple.com` used for `/3/device`. Channel **management** uses a third host pair `api-manage-broadcast(.sandbox).push.apple.com` on ports 2195/2196. Apple's own connection-section text confusingly lists the plain hosts (a documented inconsistency) (source: same, 2026-06-13). If broadcast is in scope, verify the exact host against Apple's code samples at build time.
|
||||
|
||||
---
|
||||
|
||||
## 2. FCM HTTP v1 (2026)
|
||||
|
||||
### 2.1 Legacy-API shutdown status
|
||||
|
||||
Sending with the legacy FCM HTTP and XMPP APIs was **deprecated 2023-06-20**, and **shutdown began 2024-07-22** — verbatim: "Sending messages (including upstream messages) with the FCM legacy APIs for HTTP and XMPP was deprecated on June 20, 2023, and shutdown begins on July 22, 2024" (source: https://firebase.google.com/docs/cloud-messaging/migrate-v1, 2025-05-29). The legacy HTTP endpoint (`fcm.googleapis.com/fcm/send`), XMPP (`fcm-xmpp.googleapis.com`), and legacy "server key" string auth are **gone**. Only **HTTP v1** remains.
|
||||
|
||||
The **batch send** endpoint (`fcm.googleapis.com/batch`) was also discontinued, shutdown date **2024-06-20** (source: same, 2025-05-29). For a raw-protocol relay there is **no batch endpoint** — send one `POST` per registration token and fan out via HTTP/2 multiplexing or a bounded concurrency pool.
|
||||
|
||||
### 2.2 OAuth2 service-account authentication
|
||||
|
||||
Auth is **OAuth2 service-account bearer tokens**. Recommended (least-privilege) scope: `https://www.googleapis.com/auth/firebase.messaging` (the broader `cloud-platform` works but is a superset) (source: https://firebase.google.com/docs/cloud-messaging/auth-server, 2025-05-29). Send the token as:
|
||||
|
||||
```http
|
||||
Authorization: Bearer <oauth2_access_token>
|
||||
```
|
||||
|
||||
**Token minting (RFC 7523 JWT-bearer grant):** build a JWT signed **RS256** with the service-account private key, then exchange it (source: https://developers.google.com/identity/protocols/oauth2/service-account, 2025):
|
||||
|
||||
```jsonc
|
||||
// JWT claims for the token exchange
|
||||
{
|
||||
"iss": "<service-account-email>",
|
||||
"scope": "https://www.googleapis.com/auth/firebase.messaging",
|
||||
"aud": "https://oauth2.googleapis.com/token",
|
||||
"iat": 1700000000,
|
||||
"exp": 1700003600 // iat + up to 3600s (max 1 hour)
|
||||
}
|
||||
```
|
||||
|
||||
```http
|
||||
POST https://oauth2.googleapis.com/token
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<signed_JWT>
|
||||
```
|
||||
|
||||
Response: `{ "access_token": "...", "token_type": "Bearer", "expires_in": 3600 }`. **Access tokens are valid 3600s (1 hour).** Cache and re-mint before expiry (≈5-minute safety margin); never mint per request (source: same, 2025). Prefer a maintained Google auth library over hand-rolling.
|
||||
|
||||
**Credential resolution (ADC):** `GOOGLE_APPLICATION_CREDENTIALS` env var (path to SA JSON) → platform attached/default SA → error. Firebase: the JSON "should be kept private. Do not add it to public repos!" (source: https://firebase.google.com/docs/cloud-messaging/send/v1-api, 2026-06). On GCP, prefer keyless auth (§4.5).
|
||||
|
||||
### 2.3 Endpoint & message JSON
|
||||
|
||||
```http
|
||||
POST https://fcm.googleapis.com/v1/projects/{PROJECT_ID}/messages:send
|
||||
Authorization: Bearer <oauth2_access_token>
|
||||
Content-Type: application/json
|
||||
```
|
||||
|
||||
The path accepts the project ID **or** project number. Host is global `fcm.googleapis.com`; **no** regional/data-residency send endpoints exist as of June 2026, and no new required top-level fields were introduced through 2025–2026 (source: https://firebase.google.com/docs/cloud-messaging/send/v1-api, 2026-06-12).
|
||||
|
||||
**Request body** = `{ "validateOnly"?: bool, "message": Message }`. `message` is **required** and must carry **exactly one** target: `token`, `topic`, or `condition` (source: https://pkg.go.dev/google.golang.org/api/fcm/v1, current). For a relay sending to one device, use `message.token`.
|
||||
|
||||
```jsonc
|
||||
{
|
||||
"message": {
|
||||
"token": "<registration_token>",
|
||||
"data": { "k1": "v1", "k2": "v2" }, // map<string,string> ONLY
|
||||
"android": { "priority": "HIGH", "ttl": "0s", "collapseKey": "sync" },
|
||||
"apns": { "headers": { "apns-priority": "10" }, "payload": { "aps": { /* ... */ } } }
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Data-only emphasis (relay-critical):** A message with a `notification` block is **auto-displayed** by the FCM SDK when the app is backgrounded/killed, and the app code does **not** run until the user taps it. A **data-only** message (no `notification` block) is **always** delivered to the app's handler (`onMessageReceived` / background handler) and never auto-displayed — full app control in foreground and background. **A relay that needs its own logic/custom display on every message should send DATA-ONLY messages** (source: https://firebase.google.com/docs/cloud-messaging/customize-messages/set-message-type, 2025-05-29).
|
||||
|
||||
**`message.data` is `map<string,string>` — all values MUST be strings** (no nested objects/numbers/booleans; JSON-stringify anything structured). Reserved/forbidden keys: `from`, `message_type`, `notification`, and any key prefixed `google.`/`gcm.` (or reserved words) → `INVALID_ARGUMENT`. `android.data` overrides top-level `data` for Android (source: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages, 2025-05-29; https://firebase.google.com/docs/cloud-messaging/error-codes, 2026-06-12).
|
||||
|
||||
**Payload size:** max **4096 bytes** for token/device sends, **2048 bytes** for topic sends; over → `INVALID_ARGUMENT` (source: https://firebase.google.com/docs/cloud-messaging/error-codes, 2026-06-12).
|
||||
|
||||
### 2.4 AndroidConfig
|
||||
|
||||
`message.android` (REST camelCase) (source: https://pkg.go.dev/google.golang.org/api/fcm/v1, current):
|
||||
|
||||
| Field | Type / values | Notes |
|
||||
|---|---|---|
|
||||
| `priority` | `"NORMAL"` \| `"HIGH"` (lowercase also accepted) | Default: NORMAL for data messages, HIGH for notification messages. HIGH attempts immediate delivery and can wake a dozing device. |
|
||||
| `collapseKey` | string | Group id; only the last queued message delivered on reconnect. **Not** a request-validation limit: FCM *stores* at most **4** distinct collapse keys per device while the device is offline; a 5th evicts one nondeterministically (a delivery/storage behavior — see §2.7). The API does **not** reject a request for using a 5th distinct key. |
|
||||
| `ttl` | Duration string in seconds with `s` suffix, e.g. `"3s"`, `"3.000000001s"` | `"0s"` = deliver-now-or-drop. Range **0 – 2,419,200s (28 days)**; out-of-range → `INVALID_ARGUMENT`. |
|
||||
| `restrictedPackageName` | string | Deliver only if the token's app package matches. |
|
||||
| `directBootOk` | bool | Allow delivery during Direct Boot. |
|
||||
| `data` | map<string,string> | Overrides `message.data` for Android. |
|
||||
|
||||
Newer **optional** fields exist (`bandwidthConstrainedOk`, `restrictedSatelliteOk`); admin-SDK support landed ~March 2026; both are documented Optional and do not change the required contract (source: https://firebase.google.com/docs/cloud-messaging/send/v1-api, 2026-06-12).
|
||||
|
||||
### 2.5 `validateOnly` (dry-run)
|
||||
|
||||
Set top-level `validateOnly: true` to fully validate the request (token format, payload size, field correctness) and return a message-id-shaped response **without delivering**. Default `false` (source: https://pkg.go.dev/google.golang.org/api/fcm/v1, current). Use in CI/tests.
|
||||
|
||||
### 2.6 Error model
|
||||
|
||||
Error body shape (source: https://firebase.google.com/docs/cloud-messaging/error-codes, 2026-06-12):
|
||||
|
||||
```json
|
||||
{ "error": { "code": 404, "message": "...", "status": "UNREGISTERED",
|
||||
"details": [ { "@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError", "errorCode": "UNREGISTERED" } ] } }
|
||||
```
|
||||
|
||||
Classify on `error.status` / the FcmError `errorCode`, not just the HTTP code.
|
||||
|
||||
| HTTP | `error.status` | Meaning | Handling |
|
||||
|---|---|---|---|
|
||||
| 400 | `INVALID_ARGUMENT` | Malformed payload **or** bad token (bad format, mismatched package, oversize, reserved data key, bad TTL). `details` = `google.rpc.BadRequest`. | **Do not retry.** Delete token **only** if payload is independently known-valid; otherwise fix payload. |
|
||||
| 401 | `THIRD_PARTY_AUTH_ERROR` | Invalid/missing APNs cert/key or Web Push auth credential. | Fix credentials (ops alert). **Do not retry.** |
|
||||
| 403 | `SENDER_ID_MISMATCH` | Authenticated sender ID ≠ sender that registered the token. | Send from the correct project; permanent. **Do not retry.** |
|
||||
| 404 | `UNREGISTERED` | App unregistered (uninstall, APNs token invalid, 270-day Android inactivity, restore). | **Permanently delete the token.** Never retry. |
|
||||
| 429 | `QUOTA_EXCEEDED` (RESOURCE_EXHAUSTED) | Project / device / topic rate quota exceeded. `details` = `google.rpc.QuotaFailure` naming which quota. | Retry with exponential backoff; min initial delay **1 minute** for project- and topic-rate cases. |
|
||||
| 500 | `INTERNAL` | Unknown internal error. | Retry per "Handling retries"; **no** documented `Retry-After`. |
|
||||
| 503 | `UNAVAILABLE` | Server overloaded / temporarily unavailable. | Retry with backoff + jitter; **honor `Retry-After`** if present. |
|
||||
| (none) | `UNSPECIFIED_ERROR` | Catch-all; "No more information is available about this error." | Conservative: **do not** auto-delete token; surface for investigation. |
|
||||
|
||||
(source: https://firebase.google.com/docs/cloud-messaging/error-codes, 2026-06-12 / 2026-06-13).
|
||||
|
||||
> **Corrections / precision notes:**
|
||||
> - `QUOTA_EXCEEDED` does **not** carry a `google.rpc.RetryInfo` per FCM's own page — only `google.rpc.QuotaFailure` is promised. General Google API guidance allows `RetryInfo`; treat its presence as optional and fall back to `Retry-After`/60 s (source: same, 2026-06-13).
|
||||
> - `INVALID_ARGUMENT` (400) is **ambiguous**: it can mean malformed payload **or** bad token. Only treat it as a dead token when the payload is independently validated (source: same, 2026-06-12).
|
||||
> - Strictly, `UNSPECIFIED_ERROR` is the `FcmError.errorCode` inside `details[]`, not the top-level google.rpc `status` — operationally equivalent (source: same, 2026-06-13).
|
||||
|
||||
### 2.7 Retry-After, backoff, quotas, throttling
|
||||
|
||||
**Retry classification at scale** (source: https://firebase.google.com/docs/cloud-messaging/scale-fcm, 2026-06-12):
|
||||
|
||||
- **Never retry** 400 / 401 / 403 / 404 — "abort, and do not retry."
|
||||
- **429:** "retry after waiting for the duration set in the retry-after header. If no retry-after header is set, default to **60 seconds**."
|
||||
- **500 / 503:** "retry with exponential back-off" (+ jitter; honor `Retry-After` on 503 if present).
|
||||
- "Wait at least **10 seconds** before retrying a failed request"; set ≥10 s send timeout; cap retries; a request still failing after **60 minutes** is an outage/miscategorization, not something to retry forever.
|
||||
|
||||
**Exponential backoff must use jitter** ("varying the retry delays through a random process"). **Smooth traffic:** ramp 0→max RPS over ≥60 s, and "avoid sending messages within a 2 minute window of each of the :00, :15, :30, and :45 minute marks" to dodge synchronized spikes (source: same, 2026-06-12).
|
||||
|
||||
**Project quota.** Default per-project downstream quota is **600,000 messages per minute** ("covers over 99% of FCM developers"), enforced via a **600K-token 1-minute token bucket that refills to full at the end of each (non-clock-aligned) minute**. Over quota → `429 RESOURCE_EXHAUSTED` (`QUOTA_EXCEEDED`) until refill. **The quota measures messages, not requests.** Client errors (HTTP 400–499) count against quota **except 429s** (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-13; corroborated https://firebase.google.com/docs/cloud-messaging/scale-fcm, 2026-06-12).
|
||||
|
||||
> **Correction:** The research claim that "different message types cost different token amounts" is **not** documented and is treated as inaccurate — quota is counted **per message** regardless of type; topics are merely more efficient because one request fans out to many subscribers (source: https://firebase.google.com/docs/cloud-messaging/scale-fcm, 2026-06-12).
|
||||
|
||||
**Per-device Android limits:** up to **240 messages/minute** and **5,000 messages/hour** to a single device (headroom for bursts, e.g. rapid chat). On iOS, FCM returns an error when APNs per-device limits are exceeded (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-13).
|
||||
|
||||
> **Correction:** Exceeding the **per-device** Android caps is **not** reported as `QUOTA_EXCEEDED` (429). `429 / QUOTA_EXCEEDED` is the **project-level** per-minute quota. Firebase's throttling/quotas page does **not** specify a particular error code or status string for the per-device caps — it only says "For iOS, we return an error when the rate exceeds APNs limits." Secondary sources name the per-device signal `DeviceMessageRateExceeded`, but this string is **not** firmly documented on a primary Google page, so **classify it defensively** rather than matching an exact literal (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-13).
|
||||
>
|
||||
> **Relay handling (per-device rate).** Because the exact code/string is uncertain, the relay should treat any per-device-rate signal — whether surfaced as an `INVALID_ARGUMENT`-adjacent error, a quota-style `RESOURCE_EXHAUSTED` that names a per-device (not project) quota in `details`, or a `DeviceMessageRateExceeded`-shaped string — as a **transient, retryable rate condition**: map it to a caller-facing **429 with a conservative default `Retry-After`** and **never** as a token purge (the token is healthy; only the rate is too high). Detect on `error.status` / the `FcmError.errorCode` and the `QuotaFailure` scope, not on a hardcoded string. The relay's whole purpose is fanning bursty per-device wakes, so this is a plausible real outcome that must have a defined mapping rather than falling through to `UNSPECIFIED_ERROR`/502.
|
||||
|
||||
**Collapsible-message throttling:** burst of **20 per app per device**, refill **1 every 3 minutes** (source: same, 2026-06-13).
|
||||
|
||||
> **Correction:** Exceeding the collapsible burst is a **silent delay** (battery protection), **not** a `QUOTA_EXCEEDED` error: "we delay messages to reduce the impact on a user's battery" (source: same, 2026-06-13).
|
||||
|
||||
**Distinct collapse keys:** FCM stores at most **4** different collapsible messages per device (one per distinct `collapse_key`); a 5th evicts a key nondeterministically. Default `collapse_key` = app package name (source: https://firebase.google.com/docs/cloud-messaging/customize-messages/collapsible-message-types, 2026-06-12).
|
||||
|
||||
> **Design implication (collapse-key cap vs. per-series keys).** If the relay derives `collapse_key` per notification series (e.g. a per-series HMAC), a single device that follows **more than 4 active series** will exceed FCM's 4-distinct-key store, and excess distinct keys evict each other nondeterministically — so **collapsible coalescing is best-effort**, not guaranteed, once a device crosses 4 series. This is acceptable only if a dropped collapsible wake is recoverable (e.g. the caller's durable inbox + sync covers it); otherwise the caller should cap distinct `collapse_key`s per device. **Asymmetry to note:** APNs has **no** documented per-device cap on the number of distinct `apns-collapse-id` values, so this 4-key eviction behavior is FCM-only — the two providers are not symmetric here, and the cross-provider design docs should reconcile this rather than assume parity.
|
||||
|
||||
**Non-collapsible offline storage (Android):** up to **100** messages stored while offline; beyond that, **all** stored messages are discarded and the device gets a single "contact your server" signal on reconnect. Do not treat FCM as a durable queue; the app must re-sync (source: same, 2026-06-12).
|
||||
|
||||
**Quota increases:** self-serve up to **+25%** in Google Cloud Console (gated on ≥80% usage for ≥5 consecutive minutes/day, <5% client-error ratio at peak, scale best practices). Larger/temporary bumps via Firebase Support: ≥15 days lead (≥30 days for >18M msgs/min), ≤2 events/year, ≤30-day total duration (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-13).
|
||||
|
||||
### 2.8 High-priority data-message behavior & power management
|
||||
|
||||
- **Normal priority:** delivered immediately when awake; in Doze, batched to maintenance windows. **High priority:** FCM attempts immediate delivery, can wake a sleeping device, grants temporary network + a partial wakelock (source: https://developer.android.com/training/monitoring-device-state/doze-standby, 2024-07-28).
|
||||
- **Deprioritization:** A high-priority message that does **not** produce a user-facing notification gets **deprioritized to normal** (and may be throttled): "If FCM detects a pattern in which messages don't result in user-facing notifications, your messages may be deprioritized to normal priority." FCM evaluates ~7 days of per-app-instance behavior (source: https://firebase.blog/posts/2025/04/fcm-on-android/, 2025-04-17). **Do not use high priority for silent data-only syncs; use normal priority and reserve high priority for messages that produce a notification.**
|
||||
- **App Standby Buckets:** high-priority delivery is still subject to the device's standby-bucket quota; once exhausted, further high-priority messages are effectively treated as normal. Exact per-bucket numeric caps are **not published** by Google and are dynamic/OEM-dependent (source: https://firebase.blog/posts/2019/02/life-of-a-message/, 2019-02-26).
|
||||
- **Background-restricted apps:** receive **no** FCM messages (any priority, any state) until launched to the foreground (source: https://developer.android.com/training/monitoring-device-state/doze-standby, 2024-07-28).
|
||||
- **Android 13+ (`POST_NOTIFICATIONS`):** notifications require the runtime `POST_NOTIFICATIONS` permission; without it, delivered notification messages are silently dropped. If the app's first notification channel is created while in the background (which the FCM SDK does on first receipt), Android won't display or prompt until the next app open — so early notifications are **lost**. **Data-message delivery to `onMessageReceived` is unaffected** (source: https://developer.android.com/develop/ui/views/notifications/notification-permission, 2024-08-08).
|
||||
|
||||
### 2.9 Registration-token lifecycle
|
||||
|
||||
- **Rotation hook:** `onNewToken()` fires on initial generation and every refresh (including FCM-initiated refresh when "the security of the previous token had been compromised"). Tokens also change on reinstall, data-clear, device restore. Persist the token to your server inside `onNewToken` (source: https://firebase.google.com/docs/cloud-messaging/manage-tokens, 2026-06-13).
|
||||
- **Android inactivity expiry:** "When stale tokens reach **270 days** of inactivity, FCM considers them expired." iOS/web rely on APNs/Web Push (no inactivity-based expiry), so cleanup there is driven by send-time error responses (source: same, 2026-06-13).
|
||||
- **Freshness strategy:** store each token **with a server timestamp** (FCM SDKs don't provide one), refresh ~**monthly** (no benefit more often than weekly), skip tokens outside your staleness window before sending, and delete on `UNREGISTERED` (404) or on `INVALID_ARGUMENT` (400) **when the payload is verified valid** (source: same, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## 3. Go ecosystem (2026)
|
||||
|
||||
> **Version targeting:** `jackc/pgx v5.10` and `firebase.google.com/go/v4 v4.20` both require **Go 1.25+**, so target **Go 1.25+** for the whole relay (source: https://pkg.go.dev/github.com/jackc/pgx/v5, 2026-06-03; https://pkg.go.dev/firebase.google.com/go/v4/messaging, 2026-05-14).
|
||||
|
||||
### 3.1 APNs client
|
||||
|
||||
| Choice | Version | Rationale |
|
||||
|---|---|---|
|
||||
| `github.com/sideshow/apns2` | **v0.25.0** (tagged 2024-10-25 UTC; pkg.go.dev shows "Oct 24, 2024" due to TZ rounding) | De-facto Go APNs HTTP/2 client. Auto-manages ES256 JWT generation/refresh and HTTP/2 connection reuse. Host constants match Apple. **Hold ONE `*apns2.Client` per process/environment; never recreate per push** (TLS setup is slow). One client "can potentially do 4,000+ pushes per second." (source: https://github.com/sideshow/apns2/blob/v0.25.0/client.go, 2024-10-25) |
|
||||
| Hand-rolled (alternative) | `net/http` + `golang.org/x/net/http2` (**v0.56.0**, 2026-06-09) + `golang-jwt/jwt/v5` (**v5.3.1**) | Full control of the HTTP/2 transport; mirror apns2: parse `.p8` EC key, sign one ES256 JWT, cache ~50 min, reuse one `*http2.Transport` with `ReadIdleTimeout`/`PingTimeout` set. (source: https://pkg.go.dev/github.com/golang-jwt/jwt/v5, 2026-01-01; https://pkg.go.dev/golang.org/x/net/http2, 2026-06-09) |
|
||||
|
||||
`apns2` internals (verified against v0.25.0 source): `HostProduction = "https://api.push.apple.com"`, `HostDevelopment = "https://api.sandbox.push.apple.com"`; `NewTokenClient(*token.Token{AuthKey, KeyID, TeamID})` / `NewClient(cert)`; chain `.Production()`/`.Development()`. `token.TokenTimeout = 3000` (50 min, deliberately under Apple's 1-hour window); `GenerateIfExpired()` regenerates under a mutex. Timeouts: `HTTPClientTimeout=60s`, `ReadIdleTimeout=15s`, `TCPKeepAlive=15s`, `TLSDialTimeout=20s`. `ClientManager` (MaxSize 64, MaxAge 10m) pools clients across multiple certs/keys (source: https://github.com/sideshow/apns2/blob/v0.25.0/client.go, 2024-10-25; token/token.go, same).
|
||||
|
||||
> **Maintenance caveat:** `apns2` has had no release since v0.25.0 (Oct 2024); bus-factor risk. Verify recent commit/issue activity before committing vs. the hand-rolled path (see §5, item 2).
|
||||
|
||||
### 3.2 FCM / OAuth2
|
||||
|
||||
Pick **exactly one** of these two FCM approaches, not both:
|
||||
|
||||
| Choice | Version | Rationale |
|
||||
|---|---|---|
|
||||
| `golang.org/x/oauth2/google` (lighter) | **v0.36.0** (2026-02-11) | Best for a **fixed/known payload**: `CredentialsFromJSONWithType(ctx, jsonKey, google.ServiceAccount, "https://www.googleapis.com/auth/firebase.messaging")` → `creds.TokenSource` already caches+auto-refreshes; POST your own JSON to the v1 endpoint with a tuned HTTP/2 client. Fewer deps; full control of connection reuse; **you implement retries/error-classification**. Note: `CredentialsFromJSON` is deprecated in favor of `CredentialsFromJSONWithType`. (source: https://pkg.go.dev/golang.org/x/oauth2/google, 2026-02-11) |
|
||||
| `firebase.google.com/go/v4/messaging` (batteries-included) | **v4.20.0** (2026-05-14, Go 1.25+) | Auto-mints/caches OAuth token, auto-retries transient errors, rich error helpers: `IsUnregistered` (drop token), `IsQuotaExceeded`, `IsUnavailable`, `IsInvalidArgument`, `IsThirdPartyAuthError`, `IsSenderIDMismatch`. Use `Send` / `SendEach` / `SendEachForMulticast` (each = one HTTP call per message, ≤500). **`SendAll`/`SendMulticast` are deprecated (and removed in newer SDK majors)** — do not use. (source: https://pkg.go.dev/firebase.google.com/go/v4/messaging, 2026-05-14) |
|
||||
|
||||
For a privacy-preserving relay forwarding opaque tenant payloads, the **`x/oauth2/google` + custom HTTP/2 client** path is usually preferable: it avoids the heavy Admin SDK and gives direct control of connection reuse and error handling.
|
||||
|
||||
### 3.3 HTTP/2 tuning (both transports)
|
||||
|
||||
`golang.org/x/net/http2.Transport` (**v0.56.0**, 2026-06-09) knobs (source: https://pkg.go.dev/golang.org/x/net/http2, 2026-06-09):
|
||||
|
||||
- `ReadIdleTimeout` (~10–15 s): if no frame received for this duration, send a PING health check (0 = disabled).
|
||||
- `PingTimeout` (~15 s, default): close the conn if no PING response arrives.
|
||||
- `WriteByteTimeout`: close if no bytes can be written within the timeout.
|
||||
- `StrictMaxConcurrentStreams` (bool): if `false` (default) new TCP conns open to stay under each conn's `SETTINGS_MAX_CONCURRENT_STREAMS`; if `true` the server limit is global and callers block.
|
||||
|
||||
**Setting a non-zero `ReadIdleTimeout` + `PingTimeout` is the single most important tuning for a long-lived relay** — it reaps half-open APNs/FCM connections. Do not hardcode max-concurrent-streams; let the transport read the server SETTINGS (the exact APNs/FCM advertised value is not a published primary-source constant — measure at runtime; see §5, item 3).
|
||||
|
||||
**Inbound server:** `net/http` gets HTTP/2 automatically over TLS and supports graceful shutdown. **Set explicit timeouts** — the zero value means no timeout: `ReadHeaderTimeout`, `ReadTimeout`, `WriteTimeout`, `IdleTimeout`, `MaxHeaderBytes` (default 1 MiB). On SIGTERM call `srv.Shutdown(ctx)` with a bounded context (source: https://pkg.go.dev/net/http, 2026-06-01).
|
||||
|
||||
### 3.4 Storage
|
||||
|
||||
| Choice | Version | Rationale |
|
||||
|---|---|---|
|
||||
| `github.com/jackc/pgx/v5` + `pgxpool` | **v5.10.0** (2026-06-03, Go 1.25+) | Recommended Postgres driver/pool. Use `pgxpool.New(ctx, dsn)` (not `pgx.Connect`) for a server — concurrency-safe, health-checked. Token store, API-key store, idempotency. (source: https://pkg.go.dev/github.com/jackc/pgx/v5, 2026-06-03) |
|
||||
| `github.com/redis/go-redis/v9` | **v9.20.1** (2026-06-11) | Context-first Redis client with built-in pooling. Distributed rate-limiting, idempotency in-flight locks, dedup/queue state. (source: https://pkg.go.dev/github.com/redis/go-redis/v9, 2026-06-11) |
|
||||
|
||||
### 3.5 Observability
|
||||
|
||||
| Choice | Version | Rationale |
|
||||
|---|---|---|
|
||||
| `log/slog` (stdlib) | Go 1.21+ | Structured logging, no third-party dep. `slog.NewJSONHandler` for production aggregation; redaction middleware (§4.6). (source: https://pkg.go.dev/log/slog, 2026-06-01) |
|
||||
| `github.com/prometheus/client_golang` | **v1.23.2** (2025-09-05) | Prometheus instrumentation; custom registry + `promhttp.HandlerFor` for `/metrics`. Instrument per-provider send latency (histogram) and outcome counters labeled by provider + status/reason. (source: https://pkg.go.dev/github.com/prometheus/client_golang/prometheus, 2025-09-05) |
|
||||
|
||||
### 3.6 Reference pattern (not a dependency)
|
||||
|
||||
`github.com/appleboy/gorush` is a maintained reference push **server** (built on `sideshow/apns2`, supports FCM) with a notification queue, worker pool, and pluggable stats storage — useful as a **pattern reference** for worker pools and retry/queue design, but it is a full server, not a library to embed (source: https://github.com/appleboy/gorush, 2025-11-20).
|
||||
|
||||
### 3.7 Pinned versions (summary)
|
||||
|
||||
```
|
||||
github.com/sideshow/apns2 v0.25.0
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
golang.org/x/net v0.56.0 // http2
|
||||
golang.org/x/oauth2 v0.36.0
|
||||
firebase.google.com/go/v4 v4.20.0 // only if using the Admin SDK path
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
github.com/redis/go-redis/v9 v9.20.1
|
||||
golang.org/x/time (rate — see §4.2)
|
||||
// Go 1.25+ required
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Security & ops (2026)
|
||||
|
||||
### 4.1 API-key storage & comparison
|
||||
|
||||
- **Generate** 32 bytes from `crypto/rand`; format as `<prefix>_<base62/base64url>` where the **prefix** (8–12 chars, optionally tenant/env-tagged) is **non-secret**, stored in cleartext, and **indexed**. The remaining 32 bytes are the secret. This GitHub/Stripe-style design lets you `SELECT` the row by indexed prefix in O(1), then constant-time-compare the hash — no full-table scan, no SQL index over the secret (engineering consensus aligned with OWASP API2:2023, source: https://owasp.org/API-Security/editions/2023/en/0xa2-broken-authentication/, 2023).
|
||||
- **Hashing:** Store **SHA-256** (or **HMAC-SHA-256** with a server-side pepper kept in the secret manager) of the secret — never plaintext. For **high-entropy random keys** (≥128, ideally 256 bits), a fast hash is acceptable; OWASP's slow-KDF warning is scoped to **low-entropy human passwords**, where "Fast hashing algorithms such as SHA-256 are not suitable" — that warning does **not** apply to 256-bit random API keys, and `argon2id` per-request would needlessly throttle the relay (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06). _Note: this passwords-vs-keys distinction is engineering consensus consistent with OWASP, not a single verbatim OWASP line — see §5, item 12._
|
||||
- **If** you ever store a low-entropy password (not API keys), OWASP's current argon2id minimum is **m=19456 KiB (19 MiB), t=2, p=1** with a unique per-secret salt (source: same, 2026-06). Do **not** apply argon2id to per-request API-key verification.
|
||||
- **Comparison:** use `crypto/subtle.ConstantTimeCompare(x, y []byte)` on the fixed-length (32-byte) hashes. Never use `==` or `bytes.Equal` on raw secret material (source: https://pkg.go.dev/crypto/subtle, current).
|
||||
- **OWASP API2:2023:** "API keys should not be used for user authentication. They should only be used for API clients authentication." Never put keys in URLs/query strings; implement anti-brute-force on auth endpoints (source: https://owasp.org/API-Security/editions/2023/en/0xa2-broken-authentication/, 2023).
|
||||
- **Metadata per key:** `account_id`, `key_hash`, `prefix` (indexed), `created_at`, `last_used_at` (written throttled, e.g. ≤ once/min, to avoid a DB write per request), `expires_at` (optional), `revoked_at`. Support **multiple live keys per account** for zero-downtime rotation; reject revoked/expired keys on every request (aligned with OWASP API2:2023, source: same, 2023).
|
||||
|
||||
### 4.2 Rate limiting
|
||||
|
||||
- **Single instance:** `golang.org/x/time/rate` token bucket — `NewLimiter(r Limit, b int)`. Use `Allow()` to drop/reject (return 429) on inbound; `Wait(ctx)`/`Reserve()` to pace outbound APNs/FCM calls. `Reservation.Delay()` gives the wait → use it to compute `Retry-After`. Per-account: `map[accountID]*Limiter` with eviction (per-instance only) (source: https://pkg.go.dev/golang.org/x/time/rate, current).
|
||||
- **Multi-instance (behind LB):** per-instance limiters no longer enforce a global limit — use a **Redis-backed distributed limiter** with an **atomic Lua** script (refill → check → consume in one `EVAL`), state as a Redis HASH `{tokens, last_refill_ts}` + TTL for idle-account eviction. Alternatives: `redis-cell` (GCRA, `CL.THROTTLE` returns remaining quota + retry-after) or `redis-gcra` Lua scripts. Decide **fail-open vs fail-closed** when Redis is down (fail-open for availability; fail-closed for abuse-sensitive endpoints) (source: https://github.com/Losant/redis-gcra, current).
|
||||
- **Response:** on limit exceeded, return **`429 Too Many Requests`** (RFC 6585 §4, 2012-04) with a **`Retry-After`** header. ABNF: `Retry-After = HTTP-date / delay-seconds`, `delay-seconds = 1*DIGIT` (RFC 9110 §10.2.3, 2022-06). **Common bug to avoid:** do **not** emit an absolute Unix epoch integer — `delay-seconds` is a **relative** count of seconds; an HTTP-date must use the full date format. Prefer an integer relative-seconds value from `Reservation.Delay()` (source: https://www.rfc-editor.org/rfc/rfc9110.html#section-10.2.3, 2022-06).
|
||||
- **OWASP API4:2023** (renamed "Unrestricted Resource Consumption"): enforce rate limits, "Define and enforce a maximum size of data on all incoming parameters and payloads," cap per-account push volume, and "Configure spending limits for all service providers/API integrations" — directly relevant to capping per-tenant APNs/FCM spend (source: https://owasp.org/API-Security/editions/2023/en/0xa4-unrestricted-resource-consumption/, 2023).
|
||||
|
||||
### 4.3 Idempotency
|
||||
|
||||
Accept an **`Idempotency-Key`** header (UUIDv4, **scoped per account**). _Status note:_ this is an IETF **draft** (`draft-ietf-httpapi-idempotency-key-header-07`, 2025-10-15), **not** a ratified RFC, and that version **expired 2026-04-18** — treat the header **name** and pattern as the de-facto standard (Stripe uses the same header), but do not cite it as an RFC (source: https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header-07, 2025-10-15).
|
||||
|
||||
Status-code lifecycle from the draft:
|
||||
|
||||
| Situation | Response |
|
||||
|---|---|
|
||||
| First request | Process; record the response. |
|
||||
| Duplicate, original **completed** | Replay the previously completed result (success or error). |
|
||||
| Duplicate, original **in-flight** | **409 Conflict** (RFC 7807 body, type `.../idempotency`). |
|
||||
| Same key, **different payload** | **422 Unprocessable Content**. |
|
||||
| Missing key where required | **400 Bad Request**. |
|
||||
|
||||
(source: same, 2025-10-15).
|
||||
|
||||
**Stripe-style production model** (de-facto reference): header `Idempotency-Key`, value up to 255 chars, recommend UUIDv4; saved result (status + body) replayed for **≥24 hours** for both success and failure; same key + different params → error; concurrent in-flight → not saved, client may retry; applies to POST (GET/DELETE ignore it) (source: https://docs.stripe.com/api/idempotent_requests, 2026-06).
|
||||
|
||||
**Relay implementation:** atomic `SET idem:{account}:{key}` with **NX + TTL** as an in-flight lock. NX success → process, then overwrite the marker with the serialized `{status, body}` and a ~24 h TTL. NX fail + stored response → replay it with the original status. NX fail + still in-flight → return **409**. Same key + different payload → **422**. This prevents double-sends to APNs/FCM under concurrent retries.
|
||||
|
||||
### 4.4 Provider auth recap (security view)
|
||||
|
||||
- **APNs:** cache one ES256 JWT, regenerate on a ~50-min timer (never < 20 min apart) under a mutex, reuse across senders; refresh on `403 ExpiredProviderToken`; back off on `429 TooManyProviderTokenUpdates`. Prefer team-scoped + topic-specific `.p8` keys (Feb 2025) for least privilege. Ensure the TLS trust store includes USERTrust RSA / SHA-2 root (§1.7). (sources as cited in §1.)
|
||||
- **FCM:** cache the OAuth2 access token (~1 h) rather than minting per message. On GCP, prefer keyless auth (§4.5). (sources as cited in §2.)
|
||||
|
||||
### 4.5 Secret management
|
||||
|
||||
OWASP Secrets Management ranking — **KMS/Vault/secret-manager (best) > tightly-permissioned mounted secret file (acceptable) > plain env var (discouraged) > in image/repo (forbidden)**. Verbatim: "environment variables are generally accessible to all processes and may be included in logs or system dumps. Using environment variables is therefore not recommended unless the other methods are not possible." Encrypt at rest (AES-256-GCM or ChaCha20-Poly1305); "You should not store keys next to the secrets they encrypt" (envelope encryption / KMS); least privilege; prefer dynamic/short-lived secrets (source: https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html, 2026-06). Store the APNs `.p8` and FCM SA JSON in a secret manager; never commit or bake into the image.
|
||||
|
||||
**Google keyless direction (2025):** prefer Workload Identity / service-account impersonation / attached SA over downloadable JSON keys; org policy `constraints/iam.disableServiceAccountKeyCreation` blocks new keys. If the relay runs **on GCP**, use an attached SA / Workload Identity. If **off-GCP**, a JSON key is sometimes unavoidable — then it **must** live in a secret manager, not a repo or plain env var (source: https://docs.cloud.google.com/iam/docs/keys-disable-enable, 2026-06).
|
||||
|
||||
### 4.6 Log redaction
|
||||
|
||||
OWASP Logging exclude-list (verbatim items): "Access tokens"; "Authentication passwords"; "Session identification values"; "Database connection strings"; "Encryption keys and other primary secrets"; payment/PII. Redact by remove/mask/sanitize/hash/encrypt (source: https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html, 2026-06).
|
||||
|
||||
For the relay specifically — **never log**: the `Authorization`/bearer header, the raw API key (log only the non-secret **prefix** or a hash), full APNs/FCM **device tokens** (truncate/hash), or **notification payload bodies**. **Do log**: request ID, `account_id`/tenant ID, status code, error `reason`/`status`, and Retry-After/idempotency decisions for triage. Implement redaction in logging middleware so it can't be bypassed.
|
||||
|
||||
### 4.7 Multi-tenant isolation
|
||||
|
||||
Combine **noisy-neighbor resource control** (per-tenant rate/volume quotas at gateway/service/data tiers) with **per-tenant scoping** of every stored API key, rate-limit bucket, idempotency key, cached provider token, and **log line**. "Attach a tenant ID to every request, log entry, metric, and trace." Namespace all shared-store keys by `account_id` to prevent cross-tenant collision/replay. Cap downstream APNs/FCM spend per tenant (OWASP API4:2023). For APNs, recall the per-team connection-pool constraint (§1.1) (source: https://owasp.org/API-Security/editions/2023/en/0xa4-unrestricted-resource-consumption/, 2023).
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
T[Tenant request + API key + Idempotency-Key] --> AUTH[Auth middleware: prefix lookup + ConstantTimeCompare]
|
||||
AUTH --> RL[Per-tenant rate limit: Redis Lua token bucket]
|
||||
RL -->|429 + Retry-After| T
|
||||
RL --> IDEM[Idempotency: Redis SET NX lock / replay / 409 / 422]
|
||||
IDEM --> ROUTE{Provider}
|
||||
ROUTE -->|APNs| APNS[ES256 JWT cached ~50m, per-team HTTP/2 pool]
|
||||
ROUTE -->|FCM| FCM[OAuth2 token cached ~1h, v1 messages:send]
|
||||
APNS --> STORE[(pgx: tokens / keys / idempotency)]
|
||||
FCM --> STORE
|
||||
AUTH -. redacted .-> LOG[slog JSON: tenant, req-id, status, reason]
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Open questions / re-verify before implementation
|
||||
|
||||
1. **APNs token page exact wording.** `establishing-a-token-based-connection-to-apns` is a JS-rendered SPA; content was confirmed via Apple's JSON data endpoint and corroborating sources. Re-read the live page to confirm exact 20/60-minute refresh wording and the scoped-key limits ("200 keys per env, up to 400 topics per topic-based key") near build time.
|
||||
2. **`apns2` maintenance / bus factor.** No release since v0.25.0 (Oct 2024). Check recent commit/issue activity before committing to it vs. a hand-rolled `net/http2` + `golang-jwt/jwt/v5` client.
|
||||
3. **APNs `SETTINGS_MAX_CONCURRENT_STREAMS`.** Intentionally variable and not documented by Apple. Measure at runtime; do not hardcode connection sizing.
|
||||
4. **APNs sandbox hostname alias (resolved — interchangeable).** `api.sandbox.push.apple.com` and `api.development.push.apple.com` are interchangeable sandbox aliases: as of 2026-06-13 both resolve to the same Akamai CNAME and IP set (DNS-verified, §1.3), so either reaches the same sandbox backend. We standardize on `api.sandbox.push.apple.com` (canonical in Apple's docs and `apns2`'s `HostDevelopment`). No build-time blocker — this is a naming preference, not a discrepancy.
|
||||
5. **APNs broadcast hosts.** If Live Activity broadcast/channels are in scope, confirm the broadcast-specific hosts (`api-broadcast.*`, `api-manage-broadcast.*` on 2195/2196) against Apple's code samples; the connection-section text in Apple's docs is internally inconsistent.
|
||||
6. **Future APNs requirements.** None published after Feb 2025 as of June 2026 — re-check Apple's Upcoming Requirements page (e.g. a future TLS-1.3 minimum or another CA rotation) close to deployment.
|
||||
7. **FCM `RetryInfo` on 429.** FCM's error-codes page promises only `google.rpc.QuotaFailure`, not `RetryInfo`. Treat `RetryInfo` as optional; fall back to `Retry-After` / 60 s default.
|
||||
8. **FCM data-residency endpoints.** None for FCM as of June 2026 (global `fcm.googleapis.com` only). Re-check release notes if EU/other data-residency requirements emerge.
|
||||
9. **FCM project quota tiers above 600K/min.** Confirm a project's real ceiling via Google Cloud Console quotas rather than assuming a secondary-source tier.
|
||||
10. **App Standby Bucket caps.** Per-bucket high-priority wake-up caps are unpublished and OEM/version-dependent; confirm via on-device testing (`adb shell am set-standby-bucket` / `get-standby-bucket`) if precise behavior matters.
|
||||
11. **Android 270-day token expiry.** Currently documented (manage-tokens, 2026-06-13) but a platform constant that can change — re-verify at build time.
|
||||
12. **API-key "fast hash OK" citation.** Engineering consensus consistent with OWASP's passwords-vs-keys threat model, but not a single verbatim OWASP line for API keys; if a hard citation is required, point to that distinction, and decide plain salted SHA-256 vs HMAC-SHA-256 pepper (pepper adds defense-in-depth but complicates rotation).
|
||||
13. **Idempotency-Key draft status.** Draft 07 expired 2026-04-18 without becoming an RFC; re-check for an 08+/RFC before relying on its exact status-code semantics as "standard."
|
||||
14. **GCP vs off-GCP for FCM credentials.** Whether the relay runs on GCP (keyless Workload Identity) or off-GCP (JSON key in a secret manager) changes the recommended FCM credential approach — resolve in the deployment design.
|
||||
@@ -0,0 +1,268 @@
|
||||
# Silo Push Relay — Decision Record
|
||||
|
||||
> **Provenance.** This is the decision record for the Silo Push Relay. It resolves the implementation decisions that the engineering spec ([`00-relay-spec.md`](./00-relay-spec.md)) left open in **§15 ("Open questions needing human / product input")**, building on the phased build plan ([`01-implementation-plan.md`](./01-implementation-plan.md)) and the cited 2026 factual foundation ([`02-apns-fcm-2026-reference.md`](./02-apns-fcm-2026-reference.md)). Each decision below is **firm** and **implementer-followable**: pick it, build it. The external wire contract in [`../02-apns-relay.md`](../02-apns-relay.md) and [`../03-fcm-relay.md`](../03-fcm-relay.md) is authoritative and unchanged — no decision here adds, renames, or repurposes a caller-visible field. Commands assume the repository root is the cwd.
|
||||
|
||||
These decisions reflect **2026-current** APNs and FCM behavior and a deliberately narrow v1 cut line: a single-region, self-hosted-ecosystem relay whose two priorities are **reliability** and **custody of the official Apple/Google push credentials**. Where the spec already leaned a direction, this record ratifies and operationalizes it; where it left a real fork, this record picks one decisively and says exactly what that commits the team to.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
| Decision | Recommendation (one line) | Confidence | Gates |
|
||||
|---|---|---|---|
|
||||
| APNs Go client (§15 #3) | Hand-roll on `net/http` + `x/net/http2` + `golang-jwt/jwt/v5` behind `internal/apns`; do **not** take a production dep on `sideshow/apns2`. | High | Phase 3 |
|
||||
| FCM credentials (§15 #4) | Default **keyless** via ADC (attached SA on GCP, WIF off-GCP); SA-JSON-in-secret-manager is the explicit last-resort fallback. | High | Phase 4 / Phase 6 |
|
||||
| `apns-expiration` policy (§15 #5) | Relay-set **finite** TTL: 4h `private_alert`, 1h `background_wake`, parallel FCM `android.ttl`; relay-owned config, **not** a caller field. | High | Phase 3 (APNs) / Phase 4 (FCM) |
|
||||
| Per-account rate limits (§15 #1) | Ship **hard-coded global defaults** (config constants + env overrides); add per-account columns + `relayctl quota` only when logs prove need. | High | Phase 2 |
|
||||
| API-key hashing (§15 #6) | `HMAC-SHA256(pepper, secret)` — fast keyed hash, pepper in secret manager; **not** argon2id, **not** salted SHA-256. | High | Phase 1 / Phase 2 |
|
||||
| Op-log retention (§15 #8) | Native PostgreSQL **declarative RANGE partitioning** by day, retention by **partition DROP** via `pg_partman`; not bulk `DELETE`, not external offload. | High | Phase 1 / Phase 5 |
|
||||
|
||||
---
|
||||
|
||||
## Decision 1 — APNs Go client: hand-roll, do not depend on `sideshow/apns2`
|
||||
|
||||
**Decision.** Should the relay's APNs client adopt `sideshow/apns2`, or hand-roll the client on `net/http` + `golang.org/x/net/http2` + `golang-jwt/jwt/v5`? (Spec §15 #3; plan §3 "Decision to make before Phase 3".)
|
||||
|
||||
**Resolution.** **Hand-roll the APNs client** on `net/http` with `x/net/http2` and `golang-jwt/jwt/v5`, behind the existing `internal/apns` interface. Do **not** take a production dependency on `sideshow/apns2`. Mirror its verified internals — production host `api.push.apple.com`, sandbox host `api.sandbox.push.apple.com`, token TTL 3000 s with a 20-minute refresh floor, an HTTP/2 transport with `ReadIdleTimeout`/`PingTimeout` near 15 s, one client per process per (env, team) — and add the behavior `apns2` lacks: single-flighted JWT regeneration keyed by team, an (env, topic) → `.p8` key-selection map for Apple's Feb 2025 scoped keys with startup validation, a finite `apns-expiration` policy (Decision 3), and a connection-severed → `503` mapping with no transparent retry. Optionally keep `apns2 v0.25.0` as a throwaway Phase 3 oracle, then drop it before Phase 6.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **Hand-roll (chosen)** | No dependency on a stalled library; relay owns its whole upstream transport. Surface is small and fully specified. `jwt/v5` and `x/net/http2` are maintained and already needed (FCM, HTTP/2 tuning). Enables single-flight JWT regen, scoped-key selection, finite expiration, and connection-severed mapping that `apns2` lacks. | More code/tests up front for the JWT lifecycle, HTTP/2 pool, GOAWAY handling, and reason mapping. Must re-earn `apns2`'s production hardening through tests. |
|
||||
| Adopt `sideshow/apns2 v0.25.0` | Fastest path to a working client; de-facto library with verified internals, battle-tested at throughput, MIT, small, vendorable. | Maintenance drift: no release since Oct 2024, one untagged commit in twelve months, none in 2026; 25 open issues / 8 PRs with no maintainer response in 2025–2026, including an unaddressed connection-leak issue (#238). Lacks single-flight regen, scoped-key selection, expiration policy, connection-severed semantics — so it would be wrapped anyway. Bus-factor risk. |
|
||||
| Adopt a fork (e.g. `ad-mos/apns2`) | In principle routes around the inactive upstream; adds Live Activity. | Less current than upstream (untagged pseudo-version from Jul 2024, no stable tags) — bus factor is worse. Live Activity is out of scope. |
|
||||
|
||||
**Rationale.** Three points decide it for a reliability-first, multi-year, self-hosted relay. First, **maintenance health is poor**: `v0.25.0` (Oct 2024) is still the newest tag (source: https://github.com/sideshow/apns2/releases, 2026-06-13), the only commit to master in twelve months is one untagged dep-bump PR #239 dated 2025-07-22 (source: https://github.com/sideshow/apns2/commits/master, 2026-06-13), and an open issue (#238, 2024-11-11) reports exactly the failure class that matters here — an HTTP/2 connection/goroutine leak in a process holding long-lived connections for days — with no maintainer response in 2025 or 2026 (source: https://github.com/sideshow/apns2/issues, 2026-06-13). Second, **relay requirements already exceed `apns2`**: single-flight JWT regen (to avoid `429 TooManyProviderTokenUpdates`), the (env, topic) → `.p8` scoped-key map with startup validation for Apple's Feb 2025 scoped keys, a finite `apns-expiration`, connection-severed → `503` with no transparent retry, and sandbox-only `apns-unique-id` capture — `apns2` has none of these, so it would be wrapped regardless, leaving only small JWT/HTTP-2 glue. Third, **that glue is low risk to own**: the 2026 reference verified every constant, `jwt/v5` and `x/net/http2` are maintained and already needed, and Go's `net/http2` handles GOAWAY and reconnect transparently on a reused client. Compatibility is a non-issue — `apns2` master pins `x/net v0.33.0` / go 1.18 but module MVS resolves up to the relay's `x/net v0.56.0` and `jwt v5.3.1` and builds under Go 1.25/1.26 (source: https://github.com/sideshow/apns2/blob/master/go.mod, 2026-06-13) — so the disqualifier is maintenance and missing behavior, not versions.
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Deps.** `go.mod` takes `golang-jwt/jwt/v5 v5.3.1` and `golang.org/x/net v0.56.0` as production deps (both already planned in plan §3). `apns2` is **not** a production dep — at most a Phase 3 prototype dropped before Phase 6.
|
||||
- **Code (`internal/apns`).**
|
||||
- `jwt.go` — ES256 `.p8` sign; in-memory cache; ~50-minute regen timer; 20-minute floor; eager regen on `403 ExpiredProviderToken` with a single retry; **single-flight regen keyed by team**.
|
||||
- `transport.go` — per-(env, team) HTTP/2 transport with non-zero `ReadIdleTimeout`/`PingTimeout`, hardcoded host constants by environment enum, GOAWAY reconnect, USERTrust RSA via the system trust store.
|
||||
- `client.go` — the `internal/apns` interface implementation.
|
||||
- `errors.go` — the spec §6.4 reason table, including `InvalidToken` → `BadDeviceToken`, unrecognized reason → `502`, connection-severed → `503`.
|
||||
- Key-selection map and startup validation live in `internal/config`.
|
||||
- Everything else depends only on the `APNsClient` interface, so the choice is reversible.
|
||||
- **Ops.** A CVE or HTTP/2 bug is patched by bumping `x/net` rather than waiting on an inactive repo. Test against a fake APNs upstream for GOAWAY and mid-stream reset, a JWT-expiry burst that must yield **exactly one** regen, plus the Apple sandbox integration test (Phase 3).
|
||||
- **Schema.** None — upstream-client-only; no Postgres/Redis or external-contract change.
|
||||
|
||||
**Revisit if.** Reverse toward `apns2` only if, **before Phase 3 ships**, `sideshow/apns2` cuts a fresh tagged release newer than `v0.25.0` and shows sustained maintainer responsiveness — clearing the backlog and addressing the #238 leak class — over two to three months, resolving the bus-factor concern. Conversely, **re-validate the hand-rolled JWT/HTTP-2 internals** if Apple changes APNs provider auth (a new required header, a non-ES256 algorithm, a host change, or deprecating token auth), since the hand-rolled client now owns those mechanics.
|
||||
|
||||
**Sources.**
|
||||
- `apns2` latest release/tag is `v0.25.0` (Oct 24–25 2024); no newer tag exists (source: https://github.com/sideshow/apns2/releases, 2026-06-13).
|
||||
- Only commit to master in twelve months is untagged dep-bump PR #239 (2025-07-22); no 2026 commits (source: https://github.com/sideshow/apns2/commits/master, 2026-06-13).
|
||||
- 25 open issues / 8 PRs; issue #238 (potential HTTP/2 connection/goroutine leak, 2024-11-11) has no maintainer response in 2025–2026 (source: https://github.com/sideshow/apns2/issues, 2026-06-13).
|
||||
- pkg.go.dev flags `v0.25.0` as not the latest module version; newest released version is ~20 months old (source: https://pkg.go.dev/github.com/sideshow/apns2, 2026-06-13).
|
||||
- `apns2` master `go.mod` declares go 1.18, pins `x/net v0.33.0` and `jwt v5.2.1`; MVS resolves to the relay's `x/net v0.56.0` / `jwt v5.3.1`, builds under Go 1.25/1.26 (source: https://github.com/sideshow/apns2/blob/master/go.mod, 2026-06-13).
|
||||
- Fork `ad-mos/apns2` (Live Activity) is less current: untagged pseudo-version 2024-07-30, no stable tags (source: https://pkg.go.dev/github.com/ad-mos/apns2, 2026-06-13).
|
||||
- `x/net v0.56.0` (HTTP/2 `ReadIdleTimeout`/`PingTimeout` tuning) published 2026-06-09, actively maintained (source: https://pkg.go.dev/golang.org/x/net, 2026-06-13).
|
||||
- Verified `apns2 v0.25.0` internals to mirror: `HostProduction api.push.apple.com`, `HostDevelopment api.sandbox.push.apple.com`, `TokenTimeout 3000` under a mutex, `ReadIdleTimeout 15s`, one client per process per env (source: https://github.com/sideshow/apns2/blob/v0.25.0/client.go, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## Decision 2 — FCM credentials: default keyless via ADC, SA-JSON-in-secret-manager as last resort
|
||||
|
||||
**Decision.** Should the relay authenticate to FCM HTTP v1 via GCP keyless identity (Workload Identity Federation / attached service account) or via a service-account JSON key stored in a secret manager? (Spec §4.4, §15 #4; reference §4.5.)
|
||||
|
||||
**Resolution.** **Default to keyless.** The relay MUST authenticate via Application Default Credentials (ADC) using `golang.org/x/oauth2/google` with scope `https://www.googleapis.com/auth/firebase.messaging`, with the concrete credential **source** chosen by deployment:
|
||||
|
||||
1. **On GCP compute (GCE/GKE/Cloud Run):** use an **attached service account** — zero key material; the metadata server mints short-lived tokens.
|
||||
2. **Off-GCP (self-hosted LXC/VM — the documented Silo home-hosting reality):** use **Workload Identity Federation** with a **non-secret** credential-configuration file plus an OIDC token from the host's own IdP — still no exported key.
|
||||
3. **Only if WIF cannot be stood up** (no usable OIDC issuer for the host): fall back to a **service-account JSON key**, which MUST live in a secret manager loaded once at startup into memory — **never** as a literal-key env var, **never** baked into the image, **never** on disk in the repo.
|
||||
|
||||
In all three cases the code path is identical: `google.FindDefaultCredentials` / `DefaultTokenSource(ctx, "…/firebase.messaging")` returns an auto-caching, auto-refreshing `TokenSource`; the relay does **not** branch on credential type. This makes keyless the default and JSON-in-secret-manager the explicit last resort, matching spec §4.4 / reference §4.5 with one refinement: even the off-GCP case is keyless via WIF first.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **Keyless via ADC — attached SA on GCP, WIF off-GCP; SA-JSON-in-secret-manager last resort (chosen)** | No long-lived exportable secret in the common path — eliminates the highest-severity leak vector for a service whose whole reason to exist is custody of the official Google push credential (spec G3). Aligns with Google's current guidance that SA keys are "an exception rather than the norm" and that orgs created on/after 2024-05-03 block key creation by default. WIF's credential-config file carries no private key and need not be confidential. Tokens are short-lived (~1h) and auto-rotated — no manual rotation, no rotation-induced outage mode. Single code path through `x/oauth2/google` regardless of source. Org-policy `iam.disableServiceAccountKeyCreation` can be enforced relay-wide with no impact. | WIF off-GCP requires the host to expose a usable OIDC token (or a supported IdP: AWS/Azure/GitHub/GitLab/K8s/AD/OIDC/SAML/X.509); a bare LXC with no IdP must stand one up or fall back to JSON. Slightly more one-time setup (pool + provider + attribute mapping). |
|
||||
| Service-account JSON key in a secret manager (loaded at startup, never env/in-image) | Works on any host with no IdP or GCP compute — the universal fallback for a self-hosted ecosystem. Simple mental model. Same `x/oauth2/google` minting code via `CredentialsFromJSONWithType`. Secret-manager custody (vs env/in-image) is itself OWASP-aligned hardening, acceptable when keyless is genuinely unavailable. | A long-lived, exportable private key is exactly what Google now treats as an exception and blocks by default for 2024-05-03+ orgs — if the official Silo Firebase project lives in such an org, the policy must be relaxed just to create it. Single high-value theft target (full FCM send authority), manual rotation burden, rotation-induced outage window. The untyped `CredentialsFromJSON` loader is deprecated; must use the typed variant. Fallback, not default. |
|
||||
| Use the Firebase Admin SDK and let it resolve ADC | Batteries-included: auto token mint/cache/refresh, rich error helpers, also resolves ADC (inherits keyless transparently). | Orthogonal to the credential decision and explicitly **not** the relay's chosen client — plan §3 / reference §3.2 deliberately pick the lighter `x/oauth2/google` path for a fixed payload and direct control of connection reuse and error classification. Adopting it to "solve credentials" re-opens a settled client-library decision for no credential benefit (ADC resolution is identical either way). |
|
||||
|
||||
**Rationale.** The relay's single most sensitive asset is the official Google push credential (spec G3, §4.4); a fully compromised relay must not hand an attacker a portable, long-lived FCM send key. Keyless removes that artifact in the common path: on GCP the key never exists; off-GCP, WIF replaces it with a non-secret credential-config file plus a short-lived host-issued OIDC token that Google STS exchanges for a ~1h access token. Google's own current guidance is unambiguous — SA keys are "an exception rather than the norm," and organizations created on or after 2024-05-03 enforce `iam.disableServiceAccountKeyCreation` / `disableServiceAccountKeyUpload` by default (source: https://docs.cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys, 2026-06-13) — so a brand-new project should treat keyless as the baseline. Reliability-first reinforces this: long-lived keys add a manual-rotation outage mode and a "key silently revoked/expired" failure class that keyless does not have. Crucially, the **implementation cost of defaulting to keyless is near-zero**, because `x/oauth2/google` ADC already resolves all three sources — GCE/GKE metadata, `external_account` WIF config files, and SA JSON — behind one `TokenSource` with the same scope, so the relay does not branch (source: https://pkg.go.dev/golang.org/x/oauth2/google, 2026-02-11). JSON-in-secret-manager remains an honest fallback for a host with no IdP, but is the exception: secret-manager-held and loaded once into memory, never an env-var key or image layer (spec §4.4 ranking secret-manager > mounted file > env var > in-image). This is a refinement, not a contradiction, of spec §4.4 ("On GCP, prefer keyless …; off-GCP, the JSON lives in the secret manager") — the verified addition is that **off-GCP can and should try WIF (keyless) first**, because WIF explicitly targets non-Google-Cloud workloads and its config file carries no secret.
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Code.** `internal/fcm/oauth.go` builds the `TokenSource` exclusively through ADC — `google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/firebase.messaging")` (or `FindDefaultCredentials` + `creds.TokenSource`) — and does **not** special-case credential type. The fallback SA-JSON path uses `CredentialsFromJSONWithType(ctx, json, google.ServiceAccount, scope)` (**not** the deprecated `CredentialsFromJSON`) with the JSON sourced from the secret manager.
|
||||
- **Config.** `internal/config` gains a single `fcm_credential_mode` resolution: attached-SA (no config) > WIF credential-config file path > secret-manager SA JSON, mapped onto ADC (set `GOOGLE_APPLICATION_CREDENTIALS` to a mounted, non-secret WIF config, or inject the SA JSON into the secret-manager-backed loader).
|
||||
- **Ops/deploy.** On GCP: bind an attached SA with rights to send to the official Firebase project, and apply org policy `iam.disableServiceAccountKeyCreation`. Off-GCP: stand up a workload-identity pool + provider, configure the host's OIDC issuer, deploy the WIF `external_account` credential-configuration file (it may be mounted/baked because this specific file contains **no private key** and need not be kept confidential, per Google's WIF docs — **it is the only artifact exempt, and only because it carries no secret**). **This refines, not overrides, spec §11: the no-in-image rule still applies in full to every actual secret — the APNs `.p8`, the FCM service-account JSON, the HMAC pepper, and DB/Redis credentials remain strictly forbidden in the image under spec §11.** The startup credential health-check (`relayctl ping-upstream --provider fcm`, spec §5.4, plan Phase 4/6) validates the resolved `TokenSource` mints a token regardless of source. Removes the manual key-rotation runbook in the common path; documents the WIF pool/provider setup and the JSON-fallback secret-manager procedure for the exception case.
|
||||
- **Schema/contract.** No DB schema change; no change to the external `02`/`03` contract or to `pushwire` payloads.
|
||||
|
||||
**Revisit if.** (1) The relay must deploy on a host with **neither** GCP attached-SA **nor** any WIF-compatible identity source — then the SA-JSON-in-secret-manager fallback becomes the de facto default for that deployment and its rotation runbook must be activated. (2) The official Silo Firebase project is created under / moved into a Google org that enforces `iam.disableServiceAccountKeyCreation` by default (org created on/after 2024-05-03), making the JSON fallback unavailable without a policy exception — then keyless is mandatory. (3) `x/oauth2/google` changes its ADC resolution order or deprecates the typed-credentials API, requiring a client-library re-verification.
|
||||
|
||||
**Sources.**
|
||||
- Google: "consider the use of service account keys as an exception rather than the norm"; recommended constraints Disable SA key creation/upload; "If your organization was created on or after May 3, 2024, these constraints are enforced by default." (source: https://docs.cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys, 2026-06-13).
|
||||
- WIF lets external workloads (AWS/Azure/GitHub/GitLab/K8s/on-prem AD/OIDC/SAML 2.0/X.509) access GCP without SA keys via OAuth2/STS exchange; "eliminates the maintenance and security burden associated with service account keys." (source: https://docs.cloud.google.com/iam/docs/workload-identity-federation, 2026-06-13).
|
||||
- For off-GCP workloads, WIF uses a credential-config file that "doesn't contain a private key and doesn't need to be kept confidential"; ADC auto-discovers it via `GOOGLE_APPLICATION_CREDENTIALS`; supported in Go (source: https://docs.cloud.google.com/iam/docs/workload-identity-federation-with-other-providers, 2026-06-11).
|
||||
- `x/oauth2/google v0.36.0` `FindDefaultCredentials` / `DefaultTokenSource` resolves, in order: `GOOGLE_APPLICATION_CREDENTIALS` JSON (incl. WIF/`external_account`), gcloud user creds, then GCE/GKE metadata (attached SA); `CredentialsType` includes `ServiceAccount` and `ExternalAccount`; `CredentialsFromJSON` is deprecated — use `CredentialsFromJSONWithType`; same `TokenSource` regardless of source (source: https://pkg.go.dev/golang.org/x/oauth2/google, 2026-02-11).
|
||||
- FCM HTTP v1 auth docs recommend ADC on Compute Engine/GKE/App Engine/Functions, warn that referencing the SA JSON file "should be done with extreme care," recommend `GOOGLE_APPLICATION_CREDENTIALS` as "more secure and … strongly recommended," cite Workload Identity as an alternative to downloaded keys, and specify scope `https://www.googleapis.com/auth/firebase.messaging` (source: https://firebase.google.com/docs/cloud-messaging/auth-server, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## Decision 3 — `apns-expiration` policy: relay-set finite TTL for WAKE pushes
|
||||
|
||||
**Decision.** Should the relay set a finite `apns-expiration` (and parallel FCM `android.ttl`) on `private_alert` WAKE pushes so a stale wake doesn't deliver days later, or omit it and accept Apple's ~30-day store-and-retry? What concrete default, and does the policy live in the relay or stay caller-controlled per the `02`/`03` contract? (Spec §5.1, §15 #5; resolves spec open question 5.)
|
||||
|
||||
**Resolution.** **Set a FINITE `apns-expiration` by default in the relay.** Ship `apns-expiration = now + 4 hours` for the APNs `private_alert` mode and `now + 1 hour` for `background_wake`, with a parallel FCM `android.ttl` keyed by the FCM wire mode — `"14400s"` for `private_data` (the FCM counterpart of the APNs `private_alert`) / `"3600s"` for `background_wake` — for cross-provider parity. Both values are **per-deployment config** (relay-owned defaults, not caller-supplied) and do **not** change the external `02`/`03` wire contract, which has no expiration field — the relay sets the upstream header itself. Do **not** use `apns-expiration=0` (single-attempt, no store) and do **not** omit it (Apple stores/retries up to ~30 days). This confirms and operationalizes spec open question 5; the firm refinement is to keep it relay-owned with config-tunable defaults rather than promoting it to a caller field.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **Finite relay-set default (~4h `private_alert` / ~1h `background_wake`), config-tunable, parallel FCM `ttl` (chosen)** | A WAKE is only useful while it can still produce a near-real-time fetch; the inbox ([`../01-release-events-and-inbox.md`](../01-release-events-and-inbox.md)) is the source of truth, so a wake missed for hours is re-driven by the caller outbox or the next foreground sync, not by a stale push. Avoids a device buzzing for an event already read/deleted server-side days ago. Bounds APNs/FCM store-and-retry use. Keeps the content-free posture intact (no new caller field, no contract change). A multi-day-offline device just gets a clean expiry. With `collapse-id`, a finite TTL compounds correctly: a newer wake supersedes the old one and the old one self-expires. Config-tunable so product can re-tune without a code/contract change. | Bakes a partly product/UX call into infra: a device back online at hour 5 gets no wake and relies on its next foreground sync (acceptable — inbox is authoritative — but a real behavioral choice). One hardcoded default per mode cannot suit future urgency tiers. APNs (absolute epoch) vs FCM (duration) asymmetry means the relay must compute the APNs epoch from `now()+TTL` per request. |
|
||||
| Omit `apns-expiration` / omit `android.ttl` (Apple ~30-day store-and-retry; FCM 4-week default) | Maximizes raw delivery probability; simplest code; matches provider default (no per-request epoch math). | Verified behavior: omitted `apns-expiration` → store/retry up to ~30 days; omitted FCM `ttl` → 4 weeks. For a content-free WAKE that exists only to trigger a live fetch, a wake fired days later is useless at best, confusing at worst. Spends store-and-retry budget on valueless pushes. Contradicts the "inbox is the source of truth" premise. Spec explicitly rejects the 30-day default. |
|
||||
| `apns-expiration = 0` / `android.ttl = 0s` (deliver-once-now-or-discard) | Strongest guarantee against stale wakes; trivial constant. | `0` means no storage at all: a device merely briefly offline (locked, poor signal, Doze) loses the wake entirely, defeating the point. Background pushes are already APNs-throttled (~2–3/hr) and may be delayed; with `TTL=0` too many legitimate wakes drop. FCM `ttl=0` also disables collapsible-message throttling — a behavior change with no upside. |
|
||||
| Promote expiration to a caller-controlled `02`/`03` field | Maximum flexibility: the self-hosted server, which alone knows true urgency, picks the TTL per push; future-proofs urgency tiers without a relay redeploy. | Violates the locked `02`/`03` contract (additive-only, not a listed v1 capability) and widens the content-free surface with a new caller-controlled numeric channel visible to Apple/Google — exactly what §5.1 bounds (cf. the badge field). Adds validation/clamping burden. Premature: v1 has exactly one wake semantic. Can be added later additively if a real urgency-tier requirement emerges. |
|
||||
|
||||
**Rationale.** The decision hinges on what a `private_alert` IS in Silo: a content-free **WAKE** whose only job is to make the device fetch fresh notifications from the user server, where the **inbox**, not the push, is the source of truth (spec §5.1, [`../01-release-events-and-inbox.md`](../01-release-events-and-inbox.md)). That framing makes a wake's value decay with time — once hours old, the device's own next foreground sync (or a fresher wake in the same collapse series) has already reconciled state, so a late wake yields no benefit and a small confusion cost. Both extremes are verified-wrong for this use case: omitting the header invokes Apple's ~30-day store-and-retry (verbatim: stored "for 30 days or less") and FCM's 4-week default — far too long; `expiration=0` / `ttl=0s` means deliver-once-or-discard with no storage, dropping wakes for a device merely briefly offline (especially bad given APNs already throttles background pushes). A finite few-hours TTL is the correct middle: long enough to ride out normal transient offline windows (Doze, locked phone, brief signal loss), short enough that a multi-day-offline device gets a clean expiry and relies on its authoritative next sync. `background_wake` gets the shorter ~1h TTL because it is even more time-sensitive and even more throttled (APNs requires `apns-priority 5` for background pushes; priority 10 is an error). The `collapse-id` interaction reinforces this: collapse already means latest-wins on-device, and a finite expiration is the complementary mechanism for the cross-series / no-newer-wake case, so the two together ensure stale wakes neither pile up nor fire late. Keeping it relay-owned and config-tunable (not a caller field) preserves the locked `02`/`03` contract and the structural content-free guarantee while letting product re-tune durations — which is how the spec already resolved open question 5.
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Code.** `internal/pushwire/apns.go` computes `apns-expiration = now()+TTL` as a UNIX epoch (UTC, seconds) per request from config, per APNs mode (`private_alert` vs `background_wake`). `internal/pushwire/fcm.go` sets `android.ttl` as the parallel duration string keyed by the **FCM** wire mode — `"14400s"` for `private_data` (the FCM counterpart of the APNs `private_alert`; spec §5.2 lists FCM modes as `private_data` | `background_wake`) / `"3600s"` for `background_wake`. Note the mode-name asymmetry: an implementer building `internal/pushwire/fcm.go` must key on `private_data`, not `private_alert`, on the FCM path. Because the value is `now()`-relative, the golden-file payload/header tests in Phase 3/4 must **inject a fixed clock** or assert the header is present and within tolerance rather than byte-exact; the mirrored `silo-server` `pushwire` copy and its checksum/golden tests must use the same clock-injection approach so the two repos do not drift.
|
||||
- **Config.** Two new non-secret tuning values (`apns_expiration_private_alert`, `apns_expiration_background_wake`) in `internal/config`, with the FCM `ttl` paired/derived, defaulted in code, env/flag-overridable, loaded and validated at startup (reject values exceeding the APNs storage max or FCM 28-day max; reject `<=0` if omit/0 is disallowed by policy).
|
||||
- **Contract.** No change to the `02`/`03` wire contract and no new caller field — preserving additive-only API rules and the content-free guarantee.
|
||||
- **Ops.** A stale-wake or missed-wake-rate concern becomes a **config tune, not a logic redeploy**. No schema change, no new dependency.
|
||||
- **Tests.** Assert `background_wake` never emits `apns-priority 10` alongside its shorter TTL, and that an out-of-range configured TTL fails startup validation.
|
||||
|
||||
**Revisit if.** (1) Silo introduces more than one wake semantic or an urgency/priority tier (today there is exactly one: `wake = notifications.changed`), at which point per-mode or per-request TTL — possibly an additive `02`/`03` caller field with a bounded max — becomes justified. (2) Production metrics show a material missed-wake rate attributable to the ~4h/~1h window being too short for the real offline distribution of Silo devices. (3) Apple or Google changes `apns-expiration` / `android.ttl` semantics or the storage-policy maximum (re-verify the cited pages near deployment). (4) The durable-inbox + foreground-sync assumption weakens (e.g., a future notification type whose value is NOT recoverable from the inbox), which would change whether a missed wake is truly harmless.
|
||||
|
||||
**Sources.**
|
||||
- APNs verbatim: "If the value is 0, APNs attempts to deliver the notification only once and doesn't store it." and "If the value is nonzero, APNs stores the notification and tries to deliver it at least once, repeating the attempt as needed until the specified date." (source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- Omitted `apns-expiration` → Apple stores per its storage policy: "it may store the notification for 30 days or less … APNs attempts to deliver … the next time the device activates and is available online." (source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- `apns-expiration` carries UNIX epoch seconds (UTC); an invalid value yields `400 BadExpirationDate`; `apns-priority` for background pushes MUST be 5 (priority 10 is an error) (source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- `apns-collapse-id` merges notifications on-device (latest of the same id wins), ≤ 64 bytes, independent of expiration — so finite expiration + collapse means a newer wake supersedes an older queued one AND an undelivered wake self-expires (source: https://developer.apple.com/documentation/usernotifications/sending-notification-requests-to-apns, 2026-06-13).
|
||||
- FCM `android.ttl`: default when omitted is four weeks; max 2,419,200 s (28 days); `ttl=0` discards messages that can't be delivered immediately and disables collapsible-message throttling (source: https://firebase.google.com/docs/cloud-messaging/customize-messages/setting-message-lifespan, 2026-06-13).
|
||||
- FCM collapse: with a collapse key, an offline-stored message is replaced by the newer one; without one, both are stored; at most 4 distinct collapse keys per device while offline (source: https://firebase.google.com/docs/cloud-messaging/customize-messages/setting-message-lifespan, 2026-06-13).
|
||||
- Spec already resolved this as open question 5: v1 ships finite, configurable defaults (~4h `private_alert`, ~1h `background_wake`; parallel FCM `android.ttl`), relay-owned and tunable; the external `02`/`03` contract is unchanged (source: ./00-relay-spec.md, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## Decision 4 — Per-account rate limits: hard-coded global defaults in v1
|
||||
|
||||
**Decision.** Ship hard-coded global rate-limit defaults (~10 req/s burst, 50k/day), or add per-account quota columns (`rate_per_sec`, `burst`, `daily_quota`) on `relay_accounts` settable via `relayctl`, seeded with the documented defaults? (Spec §9.2, §15 #1.)
|
||||
|
||||
**Resolution.** **Ship hard-coded global defaults for v1**: ~10 req/s burst (capacity-20 token bucket), 50,000 req/day (fixed-UTC), coarse per-(account, token) ~1 req/3s — enforced as **named config constants with env-var overrides**, NOT per-account DB columns. Keep all per-account **enforcement** plumbing exactly as specified (Redis bucket keyed `rl:{account_id}`, daily counter `rl:day:{account_id}:{yyyymmdd}`, the §9.5 shared-FCM-project circuit breaker). Add per-account override columns only when production logs prove at least one account legitimately needs a different cap. This matches the relay's own stated lean in spec §9.2 ("v1 ships a single global default and tunes from logs") and open Q#1, so it ratifies the plan rather than reopening it.
|
||||
|
||||
Make the later per-account path cheap by: (1) reading the three limit values from a single typed struct `accountLimits{RatePerSec, Burst, DailyQuota}` resolved per request, defaulting to the global constants, so introducing a per-account source later is a one-function change, not a call-site sweep; (2) keeping the Redis bucket/daily keys labeled by `account_id` already (done). When the trigger fires, add columns `rate_per_sec int`, `burst int`, `daily_quota int` (all **NULLABLE**, NULL = inherit global) to `relay_accounts` in one timestamped Goose migration, plus a `relayctl quota set --account <id> [--rate-per-sec N] [--burst N] [--daily-quota N]` / `quota show` / `quota reset` subcommand — a backward-compatible, additive change.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **Hard-coded global defaults (config constants + env overrides), no per-account columns in v1 (chosen)** | Matches the relay's own documented lean (spec §9.2, open Q#1). Zero new schema/CLI surface — less code, fewer tests, smaller attack surface for a reliability-first first release. The abuse-containment requirement (OWASP API4:2023) is **already** met: the global default is enforced per-account (keyed by `account_id` in Redis) plus the §9.5 shared-project circuit breaker, so a stolen/misbehaving key is capped whether the number is a constant or a column. Operators still adjust globally via env var. Adding columns later is genuinely cheap (PG metadata-only `ADD COLUMN`). | A single hot account legitimately exceeding 50k/day must be handled by raising the GLOBAL default (env var, affects all) or an emergency change — until override columns exist. Slightly less satisfying day-one operability. Mitigated by the §9.2 alert at ≥80% of the daily cap. |
|
||||
| Per-account columns on `relay_accounts`, settable via `relayctl`, seeded with defaults — in v1 | Per-account tuning from day one; cleanest long-term shape; NULL-default inheritance is clean; avoids any future migration. | Builds an override surface against zero production evidence (the client already paces at 5 req/s, half the 10 req/s default, so real traffic won't press the limit). Adds schema + `relayctl quota` subcommands + tests + an inheritance/precedence rule and its edge cases to a reliability-first v1. More surface for a misconfiguration (`daily_quota=0`, absurd burst) to cause an outage or remove the abuse cap. The migration it "saves" is nearly free anyway. |
|
||||
| Per-account columns now but NOT exposed via `relayctl` (DB-only `UPDATE`) | Future-proof schema without building/testing the CLI; emergency override via privileged DSN. | Worst of both: pays the schema + inheritance cost yet has no safe, audited workflow — raw `UPDATE`s bypass the `relay_op_logs` `admin.*` audit trail that `relayctl` provides (spec §5.6), contradicting the "all admin ops audited" posture. No advantage over adding columns + CLI together when the trigger fires. |
|
||||
|
||||
**Rationale.** Four things make global defaults the right v1 call. (1) The migration-cost argument for "add columns now to avoid pain later" does not hold: PostgreSQL `ALTER TABLE ADD COLUMN` with a **constant** default is a metadata-only operation (no row rewrite) since PG 11, and `relay_accounts` holds one row per opt-in installation — hundreds at most — so even a full rewrite is trivial; NULLABLE columns (NULL = inherit) avoid even needing a default, and adding columns + a `relayctl quota` subcommand later is backward-compatible (source: https://www.postgresql.org/docs/current/ddl-alter.html, 2026-06-13). (2) The security requirement (OWASP API4:2023, Unrestricted Resource Consumption) is satisfied by a per-account **cap**, which the global default already provides — every bucket is keyed by `account_id`, and the §9.5 shared-FCM-project circuit breaker bounds cross-tenant blast radius; OWASP recommends per-client throttling and business-tuned limits but does not require per-account **configurability** (source: https://owasp.org/API-Security/editions/2023/en/0xa4-unrestricted-resource-consumption/, 2026-06-13). (3) Real traffic won't press the default: the Silo server paces client-side at 5 req/s (`02`/`03`), half the 10 req/s relay backstop, so the relay limit exists to catch a misbehaving/compromised caller, not to be tuned per well-behaved tenant (source: ./00-relay-spec.md, 2026-06-13). (4) This is a reliability-first first release; the §15/§9 text already leans "single global default, tune from logs," so shipping that ratifies the plan and minimizes v1 surface. The correct sequencing is: ship one default, watch the ≥80%-of-daily-cap alert and per-account `429` metrics, and add targeted overrides only when a real account demands one — at which point the change is cheap and evidence-driven. The shared-project risk is unavoidable regardless of limit shape: FCM's default per-project quota is 600,000 messages/minute enforced **per project** (shared across all tenants using it), returning `429 RESOURCE_EXHAUSTED` over quota — which is exactly why the §9.5 circuit breaker is required no matter whether the per-account number is a constant or a column (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-12).
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Schema.** NO change to the spec §8.1 v1 schema — `relay_accounts` ships **without** `rate_per_sec` / `burst` / `daily_quota` columns.
|
||||
- **Code.** Limit values live as named config constants in `internal/config` (per plan §2.4, "non-secret defaults … live as constants with env overrides") with env-var overrides; the rate-limit middleware (`internal/ratelimit`) reads them through a single `accountLimits` struct resolved per request, defaulting to the globals, so a future per-account source is a one-function swap, not a call-site sweep.
|
||||
- **Redis.** Keys remain account-scoped (`rl:{account_id}`, `rl:day:{account_id}:{yyyymmdd}`) — already specified, so enforcement granularity is unchanged.
|
||||
- **CLI.** NO new `relayctl quota` subcommand in v1; the §5.6 command table is unchanged.
|
||||
- **Ops.** Operators tune the SINGLE global default via env var + rolling restart; the §9.2 ≥80%-of-daily-cap alert and the `relay_rate_limited_total{kind=account|daily}` / `relay_daily_quota_used_ratio` metrics are the feedback loop. The §9.5 shared-FCM-project circuit breaker is unaffected and still required.
|
||||
- **Commits the team** to a later additive migration + `relayctl quota set/show/reset` CLI **only if/when** the revisit trigger fires (columns NULL = inherit global).
|
||||
|
||||
**Revisit if.** Add the per-account columns + `relayctl quota set/show/reset` CLI as soon as ANY of: (a) production metrics show one or more **legitimate** accounts repeatedly hitting the 50k/day cap or 10 req/s burst (`relay_rate_limited_total{kind=daily|account}` or `relay_daily_quota_used_ratio >= 0.8` sustained for a real, non-abusive account) such that raising the GLOBAL default would over-provision everyone else and erode the abuse cap; (b) operators need to throttle or temporarily zero a SPECIFIC misbehaving account without affecting others (today only all-or-nothing revoke/disable exists); (c) the relay onboards tenant tiers with deliberately different quotas — at which point per-account quotas become a product requirement, not a tuning convenience.
|
||||
|
||||
**Sources.**
|
||||
- PostgreSQL `ALTER TABLE ADD COLUMN` with a CONSTANT default is a fast metadata-only operation (no table rewrite; default materialized lazily); only a VOLATILE default forces a per-row update (source: https://www.postgresql.org/docs/current/ddl-alter.html, 2026-06-13).
|
||||
- OWASP API4:2023 recommends rate limiting per client/operation and business-tuned limits; it does NOT prescribe per-client configurable quota columns or a specific multi-tenant quota architecture — a per-tenant cap satisfies the control (source: https://owasp.org/API-Security/editions/2023/en/0xa4-unrestricted-resource-consumption/, 2026-06-13).
|
||||
- FCM default per-project downstream quota is 600,000 messages/minute, enforced PER PROJECT (shared across tenants) via a per-minute token bucket; over quota returns `429 RESOURCE_EXHAUSTED` — hence the shared-project circuit breaker is required regardless of per-account limit shape (source: https://firebase.google.com/docs/cloud-messaging/throttling-and-quotas, 2026-06-12).
|
||||
- Spec §9.2 states the v1 intent: "These are tunable per account (future column on `relay_accounts`); v1 ships a single global default and tunes from logs," and lists per-account columns as open question #1 (source: ./00-relay-spec.md, 2026-06-13).
|
||||
- The Silo server already paces relay calls client-side at a default 5 req/s shared token bucket (`02`/`03` "Relay pacing"), half the 10 req/s relay backstop (source: ./00-relay-spec.md, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## Decision 5 — API-key hashing: `HMAC-SHA256(pepper, secret)`
|
||||
|
||||
**Decision.** For high-entropy random relay API keys (`rk_<env>_<random>`, ≥128 bits), what is the correct at-rest hashing in 2026: `HMAC-SHA256(pepper)` vs salted SHA-256 vs argon2id? (Spec §8.2, §15 #6; plan Phase 1; reference §4.1.)
|
||||
|
||||
**Resolution.** **Store API keys as `HMAC-SHA256(pepper, secret)`** — a fast keyed hash with a server-side pepper held in the secret manager, separate from PostgreSQL. The spec/plan already lean this way (spec §8.2, plan Phase 1, reference §4.1); keep it. Concretely: generate the secret as ≥32 bytes from `crypto/rand`, format `rk_<env>_<base62/base64url>`; store a non-secret, uniquely-indexed prefix (`rk_<env>_<first 6–8 chars>`) cleartext for O(1) lookup; store `key_hash = HMAC-SHA256(pepper, presented_secret)` (32 bytes, `bytea`); verify with `crypto/subtle.ConstantTimeCompare` against the stored hash (and against a fixed decoy hash on prefix-miss to keep the path constant-time). Do **not** use argon2id/bcrypt here — they are for low-entropy human passwords and only add per-request latency and (for bcrypt) a 72-byte input-truncation hazard with no security gain against a 256-bit keyspace. No per-key random salt is required because the full-entropy secret already makes the hash input unique and rainbow tables infeasible; the pepper is the deliberate, shared defense-in-depth lever instead.
|
||||
|
||||
**Rotation note (load-bearing):** a pepper change cannot be done in place — it requires re-hashing every live key (impossible without the plaintext, which is never stored) or, the workable path, a **dual-pepper verification window** (try the new pepper, fall back to the old) plus a `pepper_version` column, then re-issuing keys under the new pepper and retiring the old pepper once all live keys are re-issued. Because key secrets are unrecoverable, "re-hash all keys" is really "re-issue all keys," so v1 must (a) tag each `key_hash` row with a `pepper_version`, (b) support reading under N peppers during overlap, and (c) treat pepper rotation as a make-before-break, multi-key-reissue operation aligned with the existing credential-rotation runbook (spec §14.3). Pepper rotation should be rare (compromise-driven), not scheduled.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **`HMAC-SHA256(pepper, secret)` — fast keyed hash with secret-manager pepper (chosen)** | Fast: sub-microsecond verify, no per-request CPU/RAM tax on the hot auth path (the only synchronous DB touch is the prefix lookup). Correct for high-entropy keys: a 256-bit random secret is ~2^256 keyspace, infeasible to brute-force regardless of hash speed. Defense-in-depth: if PostgreSQL leaks but the pepper does not, the stolen `key_hash` values are inert to an offline attacker. Matches OWASP's pepper guidance (shared secret, stored separately, applied via HMAC-SHA256). Plays naturally with prefix-indexed O(1) lookup + constant-time compare. Already the spec/plan choice — zero rework. | Pepper rotation is heavier: cannot re-hash in place, so it needs a dual-pepper overlap window + `pepper_version` column + key re-issuance. The pepper is a single shared secret whose compromise affects all keys at once (mitigated: secret-manager-only, never in DB/image). Slightly more moving parts than plain SHA-256. |
|
||||
| Per-key salted SHA-256 (fast hash, random salt per key, no pepper) | Also fast and correct for high-entropy keys; trivially simple; no pepper to provision/rotate; salt defeats cross-key precompute (near-moot at 256-bit). | No defense-in-depth on a DB-only leak: with salt+hash an attacker can verify guesses offline — but at 256-bit entropy that offline attack is itself infeasible, so the practical delta is small. The real loss is the cheap, high-value "DB leak but pepper intact ⇒ stolen hashes inert" property, which is exactly this design's headline threat (hostile/compromised relay operator with full DB, spec §3.5). For a credential-custody-focused service holding official Apple/Google push credentials, paying the modest rotation cost to keep that property is the right trade. |
|
||||
| argon2id (or bcrypt) — slow memory-hard KDF | Industry-default "correct" answer for password storage; unimpeachable if these were human-chosen secrets; brute-force resistance for low-entropy inputs. | Wrong tool. OWASP's slow-KDF requirement is scoped to passwords ("Fast hashing algorithms such as SHA-256 are not suitable for password storage…") — the threat is low password entropy, which does not exist for a 256-bit `crypto/rand` key. Adds real per-request cost (argon2id at OWASP's m=19 MiB, t=2 is tens of ms and tens of MB per verification — a self-inflicted DoS/memory-amplification vector on an auth endpoint). bcrypt additionally truncates input at 72 bytes (needs an HMAC/SHA pre-hash anyway). No security benefit: cannot make an already-infeasible 2^256 search more infeasible. Spec and reference explicitly reject it. |
|
||||
|
||||
**Rationale.** The decision turns on one fact: the secret is **high-entropy and machine-generated, not a human password**. Slow KDFs (argon2id/bcrypt) exist to compensate for low password entropy by making each guess expensive — their entire value is throttling a feasible offline guessing attack. Against a ≥128-bit (here 256-bit) `crypto/rand` secret the guessing attack is not feasible to begin with (~2^256 ≈ 10^77 candidates), so the slow hash protects against nothing while imposing real cost; the GPU gap that justifies slow KDFs for an 8-char password (≈180 billion SHA-256 h/s vs ≈1000 argon2 h/s) is irrelevant when the keyspace itself is unreachable (source: https://www.thesslstore.com/blog/what-is-256-bit-encryption/, 2026-06-13). OWASP confines the fast-hash prohibition to "password storage" (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06-13), and OWASP API2:2023 treats API keys as a distinct category requiring anti-brute-force at the endpoint — which the relay supplies via per-IP throttle + per-account rate limits, not a slow hash (source: https://owasp.org/API-Security/editions/2023/en/0xa2-broken-authentication/, 2026-06-13). So a **fast** hash is the correct family. Within fast hashes, the pepper-vs-salt choice is a defense-in-depth-vs-rotation-cost trade, and the pepper buys a property that maps directly onto this service's threat model: the headline adversary is a compromised relay operator / DB leak (spec §3.5), and the relay holds the official Apple `.p8` and Google SA credentials for the whole ecosystem. With the pepper in the secret manager (never in PG, never in the image), a Postgres-only compromise yields `key_hash` values that are inert without the pepper key — an attacker cannot even mount the (already-infeasible) offline search, and cannot forge or recognize valid keys. OWASP defines a pepper as a secret "shared between stored passwords," stored separately, applied as an HMAC where "the pepper is acting as the HMAC key" — exactly this design (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06-13). The rotation cost is real but bounded and rare (compromise-driven), and reuses the existing make-before-break credential-rotation runbook (spec §14.3). Salted SHA-256 is a defensible simpler fallback if the team judges pepper rotation not worth it, but it forfeits the DB-leak-resistance property that is this design's reason to care; argon2id is the only genuinely wrong choice and both spec and reference already reject it.
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Schema/DB — amends spec §8.1 / plan Phase 1.** `relay_api_keys` stores `key_hash bytea = HMAC-SHA256(pepper, secret)` (32 bytes), a cleartext uniquely-indexed `key_prefix`, and `account_id` / `created_at` / `last_used_at` / `expires_at` / `revoked_at` (spec §8.1). No salt column. **This decision adds one column the reproduced §8.1 `relay_api_keys` table does not have: `pepper_version smallint NOT NULL DEFAULT 1` (a `key_id` column is an acceptable equivalent), included in the Phase 1 init migration from the start** so dual-pepper rotation is possible later without a migration under pressure. This is an **additive, backward-compatible deviation** from the §8.1 schema reproduced in the spec and the plan's "exact schema from spec §8.1" Phase 1 statement — an implementer must add the column up front rather than discovering the gap during pepper rotation.
|
||||
- **Code.** `internal/accounts/keys.go`: `crypto/rand` secret generation; HMAC-SHA256 hashing. `internal/httpapi/middleware_auth.go`: `crypto/subtle.ConstantTimeCompare` for verification; a fixed decoy-hash compare on prefix-miss to keep the auth path constant-time (no enumeration oracle); verification tries the active pepper and, during a rotation window, falls back to prior pepper(s) keyed by `pepper_version`.
|
||||
- **Ops / secret management.** The pepper is a first-class secret in the secret manager (KMS/Vault/cloud), never in PG, never in the image/env where avoidable, loaded once at startup (spec §11, reference §4.5); it must be **backed up / escrowed** because losing it invalidates every live key.
|
||||
- **Rotation runbook.** Pepper rotation = make-before-break (provision new pepper alongside old, verify under both, re-issue all keys under the new pepper, retire old) — reuse the §14.3 credential-rotation pattern; document that "re-hash all keys" is really "re-issue all keys."
|
||||
- **Performance.** Auth stays a sub-microsecond hash + one indexed lookup, preserving the stateless-fast hot path (spec §4.1). Commits to NOT pulling argon2/bcrypt onto the request path.
|
||||
- **Tests.** Unit tests asserting the hash is `HMAC-SHA256(pepper, ·)`, constant-time compare, plaintext never stored, and unknown-prefix vs wrong-secret are timing-indistinguishable (plan Phase 1/2 acceptance criteria).
|
||||
|
||||
**Revisit if.** (1) The relay ever accepts a LOW-entropy or human-chosen secret (an operator passphrase, basic-auth admin password, shortened/customer-supplied key) — then a slow KDF (argon2id at OWASP's m=19 MiB, t=2, p=1 minimum) becomes mandatory for that credential class. (2) The pepper would have to live in the same trust boundary as PostgreSQL (same host/backup, in the DB) — its defense-in-depth value evaporates and plain salted SHA-256 becomes the honest, simpler choice. (3) A hard, citable, API-key-specific standard later prescribes a different at-rest scheme. (4) Pepper rotation proves operationally painful and pepper-compromise risk is judged low — drop to per-key salted SHA-256. (5) Key entropy is ever reduced below ~128 bits.
|
||||
|
||||
**Sources.**
|
||||
- OWASP: "Fast hashing algorithms such as SHA-256 are not suitable for **password** storage because they allow attackers to perform large numbers of guesses quickly." (scoped to passwords) (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06-13).
|
||||
- OWASP argon2id recommended configs (e.g. m=19456 KiB, t=2, p=1) are a per-verification CPU/RAM tax that would hit every relay auth if wrongly applied to API keys (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06-13).
|
||||
- OWASP defines a pepper as a secret "shared between stored passwords," "stored separately from the password database," applied via HMAC where "the pepper is acting as the HMAC key" (source: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html, 2026-06-13).
|
||||
- OWASP API2:2023 flags weakly-hashed credentials, requires anti-brute-force mechanisms on auth endpoints, and states "API keys should not be used for user authentication … only for API clients authentication." (source: https://owasp.org/API-Security/editions/2023/en/0xa2-broken-authentication/, 2026-06-13).
|
||||
- A 256-bit random key has ~2^256 (~10^77) values — computationally infeasible to brute-force regardless of hash speed; security derives from key entropy, not hash slowness; the ~180-billion-vs-~1000 h/s gap is irrelevant when the keyspace is unreachable (source: https://www.thesslstore.com/blog/what-is-256-bit-encryption/, 2026-06-13).
|
||||
- bcrypt truncates input at 72 bytes (a long `rk_` key would be silently shortened, needing an HMAC/SHA pre-hash anyway), making a slow KDF not merely unnecessary but operationally hazardous for long high-entropy keys; fast HMAC-SHA256/SHA-256 is appropriate for quick auth checks on high-entropy tokens (source: https://mojoauth.com/compare-hashing-algorithms/hmac-sha256-vs-bcrypt, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## Decision 6 — `relay_op_logs` retention: PostgreSQL daily RANGE partitioning, retention by partition DROP
|
||||
|
||||
**Decision.** For `relay_op_logs` at ~50k/day/account scale with 14-day-success / 30–90-day-failure retention, use native PostgreSQL declarative time partitioning (with partition drop), a simple indexed table with a periodic `DELETE` job, or offload to a log pipeline (Loki/ELK) — and what is the exact retention mechanism for v1? (Spec §8.1, §15 #8; plan Phase 5.)
|
||||
|
||||
**Resolution.** **Ship v1 with native PostgreSQL declarative RANGE partitioning** of `relay_op_logs` by `occurred_at`, using **DAILY** partitions, and enforce retention by **DROPPING whole partitions** on a schedule — not by row-level `DELETE`, and not by offloading to a log pipeline. Pre-create partitions ahead of time and drop those older than the retention horizon. Run the maintenance (create-ahead + drop-old) with **`pg_partman 5.4.3`** invoked from a scheduled `relayctl logs --gc` / cron `SELECT run_maintenance()`, configured to **DROP (not just DETACH)** expired partitions. Keep retention simple in v1: a single uniform **90-day** drop horizon for the whole table (the failure-retention ceiling), so no partition straddles two policies. The differential 14-day-success vs 90-day-failure trim is then a cheap row-level `DELETE` **within the youngest partitions only** (success rows older than 14 days), where the working set is tiny and bloat is bounded — the bulk reclamation that matters comes free from dropping 90-day-old partitions. This is the partition-drop mechanism the spec already calls "the recommended store strategy" (§8.1) and resolves open question §15 #8 in favor of partitioning over a plain `DELETE` job or external offload.
|
||||
|
||||
**Alternatives considered.**
|
||||
|
||||
| Option | Pros | Cons |
|
||||
|---|---|---|
|
||||
| **Native declarative RANGE partitioning by `occurred_at` (daily), retention by partition DROP via `pg_partman`/cron (chosen)** | Retention is metadata-only: `DROP TABLE` on a day partition "can very quickly delete millions of records" and "entirely avoids the VACUUM overhead caused by a bulk DELETE." No dead-tuple/index churn from the append workload — the exact failure mode a daily bulk-DELETE on a high-write table hits. Stays inside the existing Postgres dependency (no new store), honoring "operational simplicity that still scales." `pg_partman` (5.4.3, PG14+) automates create-ahead + drop and runs from plain cron via `run_maintenance()` with no background worker, fitting the Goose + scheduled-job model. Inserts still target one parent table; the redacted-by-construction write path is unchanged. Scales linearly; old partitions drop in O(1). | Schema change: a partitioned table's PRIMARY KEY must include the partition key, so the current `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY` becomes composite `(occurred_at, id)`. Adds `pg_partman` (or hand-rolled DDL) plus a create-ahead job whose failure (no future partition) would block inserts — needs a monitor. Differential 14d/90d retention is no longer one query; a small DELETE runs inside recent partitions. `DROP TABLE` on a partition briefly needs an ACCESS EXCLUSIVE lock on the parent (mitigated by `DETACH … CONCURRENTLY` first, or accepting a sub-second lock on a low-contention audit table). |
|
||||
| Simple indexed table + periodic `DELETE` job (the spec's `relayctl logs --gc` baseline, as the only mechanism) | Zero schema change — keeps the §8.1 table and surrogate bigint PK. Trivially expresses differential policy in one statement. Already a Phase 5 deliverable, so lowest-effort and a correct *fallback*. Fine at low account counts. | At 50k/day/account across accounts this is the anti-pattern the PG docs warn against: a large recurring DELETE leaves dead tuples and index bloat that standard VACUUM marks reusable but does not return to disk, so the table and its two `occurred_at` indexes bloat and slow the very append path. Reclaiming space needs `VACUUM FULL` (ACCESS EXCLUSIVE) or `pg_repack` — ongoing toil. The delete competes with hot-path inserts for I/O and locks. Works for months then the disk-growth surprise hits — exactly risk R12. |
|
||||
| Offload to a log pipeline (Loki/ELK), Postgres holds only recent rows | Purpose-built for high-volume append-only logs with cheap time-based retention and rich ad-hoc querying; removes write pressure from Postgres; scales far past 50k/day/account. | Violates v1's operational-simplicity / self-hosted brief: a whole new stateful system to deploy, secure, scope per-account, and back up for a single-region relay. `relay_op_logs` is an audit/forensics table queried by `relayctl` and joined to `relay_accounts` (FK `account_id`); moving it out breaks the in-Postgres admin/audit model and the single-point redaction guarantees. Over-engineered for the stated scale; reintroduces a deliberately-avoided dependency. The right answer later, not for v1. |
|
||||
|
||||
**Rationale.** The workload is unambiguously a high-write, append-only, time-ordered table whose only deletes are time-based retention — the textbook case for declarative range partitioning. PostgreSQL's own current (v18) docs state the decision plainly: dropping a partition "is far faster than a bulk operation" and "entirely avoids the VACUUM overhead caused by a bulk DELETE," and recommend sizing partitions to match the retention cadence (source: https://www.postgresql.org/docs/current/ddl-partitioning.html, 2026-06-13). Reliability-first means the hot push path must not contend with a daily bulk DELETE or pay for its dead-tuple/index bloat; partition DROP makes retention a metadata operation that touches neither autovacuum nor the live indexes — whereas recurring large DELETEs accumulate bloat that VACUUM marks reusable but does not shrink on disk, requiring `VACUUM FULL` or `pg_repack` to reclaim (source: https://www.tigerdata.com/learn/how-to-reduce-bloat-in-large-postgresql-tables, 2026-06-13). **Daily** (not weekly/monthly) granularity is chosen because the success horizon is only 14 days and the failure horizon 90 days — daily children keep each partition small, make the 90-day drop precise, and keep the create-ahead window short. The plain-DELETE option is kept as the differential-trim tool inside young partitions and as a degraded fallback, not as bulk reclamation. A log pipeline is the correct answer at much larger multi-region scale, but for a single-region self-hosted v1 it adds a new stateful dependency that contradicts the "stays in Postgres" posture and severs the FK/audit/redaction model that lives in one place. `pg_partman` is the pragmatic automation: de-facto standard, current (5.4.3, 2026-03-05), PG14+, **defaults to DETACH** (so retention must be explicitly configured to DROP via `retention_keep_table = false`), and runs from ordinary cron via `run_maintenance()` so no background worker is required — matching the relay's Goose + scheduled-job model (source: https://github.com/pgpartman/pg_partman, 2026-06-13). The spec already names partitioning "the recommended store strategy"; this decision firms that into "partition + drop, daily, single 90-day horizon."
|
||||
|
||||
**Consequences (what this commits).**
|
||||
|
||||
- **Schema (§8.1) — amends the plan.** `relay_op_logs` becomes `PARTITION BY RANGE (occurred_at)` with daily child partitions; the surrogate `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY` must become a composite PRIMARY KEY `(occurred_at, id)` (id remains identity for ordering/correlation; `occurred_at` carries the partition key). The two indexes — `(account_id, occurred_at DESC)` and `(occurred_at DESC)` — are created on the partitioned parent and propagate to children; the `account_id` FK with `ON DELETE SET NULL` is unaffected. **This decision explicitly supersedes spec §8.1's non-partitioned `relay_op_logs` DDL (its single-column `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`) and the plan's "exact schema from spec §8.1" Phase 1 statement: the partitioned-from-birth design only works if the structural DDL lands in the Phase 1 init migration.** Converting an already-populated non-partitioned table to partitioned later is exactly the painful full-table rewrite that partitioning exists to avoid, so it must **not** be deferred to Phase 5.
|
||||
- **Migrations — Phase 1 (structural, amending the §8.1 reproduction).** The Phase 1 init migration creates the partitioned parent + an initial window of daily partitions **in place of** the non-partitioned spec §8.1 `relay_op_logs` DDL and its single-column PK; `pg_partman` is added via `CREATE EXTENSION` + a `part_config` row (parent table, daily interval, premake N future days, `retention = '90 days'`, `retention_keep_table = false` to DROP). The composite `(occurred_at, id)` PK and the `CREATE EXTENSION pg_partman` + `part_config` row are part of this Phase 1 init migration, not a later Phase 5 add-on.
|
||||
- **Code / ops.** The oplog writer is unchanged (still `INSERT` into the parent). A scheduled job — `relayctl logs --gc` or cron `SELECT run_maintenance()` — must run at least daily to (a) pre-create upcoming day partitions and (b) drop partitions older than 90 days; its **failure** (missing future partition) would reject inserts on the hot path, so it needs a dedicated alert in addition to the existing table-growth/disk alert (R12). The differential 14d-success trim runs as a bounded `DELETE` within recent partitions only.
|
||||
- **Deps.** Adds `pg_partman 5.4.3` (PG14+) as a Postgres extension dependency; if the deployment cannot install extensions, fall back to hand-rolled create-ahead/drop DDL in `relayctl` (same partition design, no extension).
|
||||
- **Locking.** Schedule drops at low-traffic times and/or `DETACH … CONCURRENTLY` then `DROP` to avoid the brief ACCESS EXCLUSIVE lock on the parent.
|
||||
- **Phase split (amends the plan).** The plan's Phase 5 "partitioning DDL (or documented partition/offload decision)" line item is **resolved** by this decision, but the structural DDL itself moves **into Phase 1** (per the Schema/Migrations items above). **Phase 5 owns only** the operational pieces: the scheduled `run_maintenance()` / `relayctl logs --gc` create-ahead+drop job, the create-ahead-failure alert (a missing future partition rejects inserts on the hot path), and the bounded 14d-success differential `DELETE` within recent partitions. The plan's Phase 1 "exact schema from spec §8.1" line and Phase 5 "partitioning DDL" deferral are both superseded.
|
||||
|
||||
**Revisit if.** (a) Sustained ingest materially exceeds the v1 envelope — e.g. tens of accounts pushing near 50k/day each so `relay_op_logs` exceeds ~100M live rows, or Postgres shows I/O/bloat pressure from op-log writes — at which point offloading to a dedicated log pipeline (Loki/ELK/ClickHouse) with Postgres keeping a short hot window becomes worth its added ops cost. (b) The deployment target cannot install `pg_partman` AND hand-rolled partition DDL proves error-prone. (c) The differential success/failure retention needs to grow into many distinct per-event horizons such that a single uniform partition-drop horizon no longer fits. (d) Operators need rich ad-hoc/full-text forensic search over op-logs beyond indexed Postgres queries.
|
||||
|
||||
**Sources.**
|
||||
- PostgreSQL: "Dropping an individual partition using DROP TABLE, or doing ALTER TABLE DETACH PARTITION, is far faster than a bulk operation. These commands also entirely avoid the VACUUM overhead caused by a bulk DELETE." (PG 18) (source: https://www.postgresql.org/docs/current/ddl-partitioning.html, 2026-06-13).
|
||||
- PostgreSQL: dropping the no-longer-needed partition "can very quickly delete millions of records because it doesn't have to individually delete every record"; `DROP TABLE` on a partition needs ACCESS EXCLUSIVE on the parent, while `DETACH PARTITION CONCURRENTLY` needs only SHARE UPDATE EXCLUSIVE (source: https://www.postgresql.org/docs/current/ddl-partitioning.html, 2026-06-13).
|
||||
- A unique/primary-key constraint on a declaratively partitioned table must include all partition-key columns — so partitioning by `occurred_at` requires the composite key (source: https://www.postgresql.org/docs/current/ddl-partitioning.html, 2026-06-13).
|
||||
- `pg_partman` latest is 5.4.3 (2026-03-05); requires PostgreSQL ≥ 14; third-party extension; can run maintenance from external cron via `run_maintenance()`; by default DETACHes rather than DROPs expired partitions, so drop-on-retention must be configured (`retention_keep_table = false`) (source: https://github.com/pgpartman/pg_partman, 2026-06-13).
|
||||
- For high-churn tables, recurring large DELETEs accumulate dead tuples and index bloat; VACUUM marks them reusable but does not shrink files, requiring `VACUUM FULL` or `pg_repack`; partition DETACH+DROP needs no vacuum at all (source: https://www.tigerdata.com/learn/how-to-reduce-bloat-in-large-postgresql-tables, 2026-06-13).
|
||||
- `pg_partman` retention: "By default, partitions are not dropped (DROP), but are detached (DETACH)"; the configured interval drops partitions older than that interval; pg_partman never drops the last remaining child (source: https://www.keithf4.com/partman-retention/, 2026-06-13).
|
||||
|
||||
---
|
||||
|
||||
## What this unblocks
|
||||
|
||||
Each decision retires a spec §15 open question and clears a specific implementation-plan phase to proceed without further product/architecture input:
|
||||
|
||||
| Decision | Retires | Unblocks |
|
||||
|---|---|---|
|
||||
| 1 — APNs client (hand-roll) | §15 #3, plan §3 "decision before Phase 3", risk R2 | **Phase 3** — `internal/apns` (`jwt.go`/`transport.go`/`client.go`/`errors.go`) and `POST /v1/apple/send` can be built directly against `net/http2` + `jwt/v5` with no library evaluation gating the work; the Apple provisioning checklist item "decide `apns2` vs hand-rolled" (plan §7.1) is closed. |
|
||||
| 2 — FCM credentials (keyless-first) | §15 #4 | **Phase 4** (`internal/fcm/oauth.go` builds the ADC `TokenSource`; `POST /v1/fcm/send`) and **Phase 6** (secret-manager wiring, the GCP attached-SA / off-GCP WIF deploy paths, and the `relayctl ping-upstream --provider fcm` credential-health gate). |
|
||||
| 3 — `apns-expiration` policy (finite, relay-owned) | §15 #5 (spec open question 5) | **Phase 3** (APNs `apns-expiration` epoch in `internal/pushwire/apns.go`) and **Phase 4** (parallel FCM `android.ttl` in `internal/pushwire/fcm.go`); the two new `internal/config` TTL values and their clock-injected golden tests are now specified. |
|
||||
| 4 — Rate limits (global defaults) | §15 #1 | **Phase 2** — `internal/ratelimit` ships the global constants + env overrides and the `accountLimits` resolution struct with no new schema, no `relayctl quota` CLI, and no inheritance-precedence edge cases; spec §8.1 schema and §5.6 command table are unchanged. |
|
||||
| 5 — API-key hashing (HMAC-pepper) | §15 #6 | **Phase 1** (`relay_api_keys` schema with `key_hash bytea` + a `pepper_version smallint NOT NULL DEFAULT 1` column added to the reproduced §8.1 table; the pepper as a first-class secret-manager secret) and **Phase 2** (`internal/httpapi/middleware_auth.go` constant-time verify with decoy-hash and dual-pepper fallback). |
|
||||
| 6 — Op-log retention (daily partition + DROP) | §15 #8 | **Phase 1** (the structural DDL — partitioned `relay_op_logs` parent, composite `(occurred_at, id)` PK, `CREATE EXTENSION pg_partman` + `part_config` row — lands in the init migration, **amending** the plan's "exactly spec §8.1" Phase 1 statement and superseding the non-partitioned §8.1 DDL) and **Phase 5** (only the `relayctl logs --gc` / `run_maintenance()` schedule, the create-ahead-failure alert, and the bounded differential 14d-success trim). |
|
||||
@@ -0,0 +1,71 @@
|
||||
# Silo Push Relay — Design Index
|
||||
|
||||
> **Provenance:** Authored 2026-06-13 from a research-and-review workflow (live primary-source research against Apple Developer / Firebase / Google / RFC / pkg.go.dev docs, adversarial fact-checking of time-sensitive claims, draft, multi-lens review, and revision). Reflects **2026-current** APNs and FCM behavior. These design docs live here in `silo-server` alongside the `02`/`03` notification contracts purely for **co-location** — the relay **code** lives in its own repository (provisional name `silo-push-relay`).
|
||||
|
||||
This folder specifies the **Silo Push Relay**: the small Silo-operated service that holds the official Apple (`.p8` APNs auth key) and Google (Firebase service-account JSON) push credentials and forwards **opaque, content-free** push requests to APNs and FCM on behalf of opted-in self-hosted Silo servers.
|
||||
|
||||
It is the **server counterpart** to the relay-client contracts in [`../02-apns-relay.md`](../02-apns-relay.md) and [`../03-fcm-relay.md`](../03-fcm-relay.md). Those documents define the wire contract from the self-hosted Silo server's perspective (`POST /v1/apple/send`, `POST /v1/fcm/send`); this folder specifies the service that answers it. **That external contract is authoritative and reproduced byte-for-byte; these docs add only the server-internal design (`02`/`03` deliberately left it "to a separate repo").**
|
||||
|
||||
## Reading order
|
||||
|
||||
1. **[`00-relay-spec.md`](./00-relay-spec.md)** — Engineering spec. Scope, trust/threat model, architecture, the full public API contract (both send endpoints + health/metrics + the `relayctl` admin CLI), the upstream APNs and FCM clients, the PostgreSQL account/key/allowlist schema, rate limiting, idempotency, security, privacy, observability, deployment.
|
||||
2. **[`01-implementation-plan.md`](./01-implementation-plan.md)** — Phased Go build plan. Repo bootstrap and layout, pinned dependency choices with rationale, seven implementation phases (each with files, tasks, tests, acceptance criteria), testing strategy, deployment, and the Apple/Firebase provisioning checklist.
|
||||
3. **[`02-apns-fcm-2026-reference.md`](./02-apns-fcm-2026-reference.md)** — The cited 2026 factual foundation. APNs (token/ES256 auth, endpoints, headers, payloads, status/reason tables, throttling) and FCM v1 (legacy-shutdown status, OAuth2 auth, endpoint, message JSON, error model, quotas) plus the recommended Go ecosystem and security/ops practices. Every non-obvious claim carries an inline source + date.
|
||||
4. **[`03-decisions.md`](./03-decisions.md)** — Decision record resolving the spec's open implementation questions (APNs client, FCM credentials, `apns-expiration`, rate limits, API-key hashing, op-log retention), each with a firm recommendation, alternatives, rationale, consequences, and cited current sources.
|
||||
|
||||
## Status
|
||||
|
||||
| Doc | Status |
|
||||
|---|---|
|
||||
| 00-relay-spec | Draft (implementation-grade) |
|
||||
| 01-implementation-plan | Draft (implementation-grade, phased) |
|
||||
| 02-apns-fcm-2026-reference | Reference (2026-current, cited) |
|
||||
| 03-decisions | Decided (6 resolutions, high confidence) |
|
||||
|
||||
**Implementation status:** Phases 0–2 are built in the separate `silo-push-relay` repository. Phase 0 = the service skeleton (`/healthz` + `/readyz`, config, JSON logging, §5.5 envelope, graceful shutdown). Phase 1 = storage + admin (PostgreSQL schema via embedded Goose migrations, the `accounts` data-access layer with `HMAC-SHA256(pepper)` key hashing, the `relayctl` admin CLI with audited actions). Phase 2 = the full request pipeline on `POST /v1/apple/send` + `/v1/fcm/send` — bearer auth (constant-time compare with decoy, throttled `last_used_at`, soft per-IP brute-force cap, trusted-proxy client-IP resolution), Redis token-bucket rate limiting (per-account + daily + coarse per-token) with bounded in-process fail-open, `DisallowUnknownFields` decode/validate/allowlist, and fail-closed idempotency (NX lock + nonce, replay/409/422, compare-and-set). Upstream APNs/FCM calls are **stubbed** pending Phases 3–4. Two Phase-2-listed items were deferred to Phase 4 because they need the live FCM send path: the shared FCM-project circuit breaker (§9.5) and anti-spike smoothing. Phases 3–6 not started; provider-touching phases gated on Apple/Firebase provisioning (see `01` §7). `relay_op_logs` partitioning (Decision 6) is a dedicated later migration so migrations run on stock PostgreSQL.
|
||||
|
||||
## Key design decisions
|
||||
|
||||
- **Faithful external contract.** `POST /v1/apple/send` and `POST /v1/fcm/send` — request fields, validation, response shapes, relay-built payloads, and APNs headers — are reproduced exactly from `02`/`03`. Only the relay's server-internal design is newly specified.
|
||||
- **Content-free by construction.** The send structs admit only the opaque `02`/`03` fields; JSON is decoded with `DisallowUnknownFields`, so any unexpected key is a hard `400`. FCM is always data-only (no `notification` block); badge omitted by default.
|
||||
- **Stateless request path.** The only synchronous DB touch is an indexed API-key prefix lookup plus a throttled (`≤1/min`) `last_used_at` write; idempotency and rate-limit state live entirely in Redis, so replicas scale horizontally and are kill-safe.
|
||||
- **Admin = CLI, not HTTP.** Account/key/allowlist administration is `relayctl` writing directly to PostgreSQL; there is **no public admin HTTP API** in v1 (minimal attack surface).
|
||||
- **API keys.** `rk_<env>_<random>` with a non-secret indexed prefix + `HMAC-SHA256(pepper, secret)` stored, constant-time compare, revocable, multi-live-key rotation; the full token is shown exactly once.
|
||||
- **Upstream mechanics (2026-correct).** One cached ES256 JWT per APNs team (regenerate ~50 min, never < 20 min apart, eager refresh on `ExpiredProviderToken`); one cached OAuth2 token per FCM project (~1 h); HTTP/2 connection reuse with PING health checks.
|
||||
- **Idempotency fails closed; rate limiting fails open.** If Redis is unavailable, sends are refused (`503`) rather than risk a double-send; rate limiting degrades to allow (availability over perfect enforcement), with a metric.
|
||||
- **"Relay-down delays, never loses."** The self-hosted server-side outbox (`push_delivery_attempts` pending rows in `../01-release-events-and-inbox.md`) guarantees re-delivery, so ordinary stateless-replica HA suffices.
|
||||
|
||||
### Reconciliations where the 2026 reference corrects the 2026-04 contracts
|
||||
|
||||
These are upstream-only details; the **external `02`/`03` contract is unchanged**:
|
||||
|
||||
- **APNs sandbox host** is `api.sandbox.push.apple.com` (the canonical name); `02`'s `api.development.push.apple.com` is a still-valid alias (same backend). Internal detail; the external `"environment":"sandbox"` field is untouched.
|
||||
- **FCM 429** honored with `Retry-After` (60 s fallback) while still surfacing `Retry-After` to the caller as `03` expects.
|
||||
- **FCM data values** forced to strings (`map<string,string>`).
|
||||
- **FCM `priority:high`** honored for `private_data`; provider deprioritization is the caller's concern.
|
||||
|
||||
## Decisions (resolved in `03-decisions.md`)
|
||||
|
||||
The spec's open implementation questions are now decided (all high confidence):
|
||||
|
||||
1. **APNs client** — **hand-roll** on `net/http` + `x/net/http2` + `golang-jwt/jwt/v5` behind an `internal/apns` interface; do **not** take a production dependency on `sideshow/apns2` (no release since Oct 2024). Optionally keep apns2 as a throwaway Phase-3 oracle.
|
||||
2. **FCM credentials** — **keyless by default** via Application Default Credentials: attached service account on GCP, Workload Identity Federation off-GCP; service-account JSON in a secret manager only as a last resort. Identical code path in all three cases.
|
||||
3. **`apns-expiration`** — relay sets a **finite TTL** (4 h for `private_alert`, 1 h for `background_wake`) with a parallel FCM `android.ttl`; relay-owned config, external `02`/`03` contract unchanged.
|
||||
4. **Per-account rate limits** — **hard-coded global defaults** for v1 (~10 req/s burst, 50k/day), resolved through a typed `accountLimits` struct so per-account override columns are a cheap additive migration later.
|
||||
5. **API-key hashing** — `HMAC-SHA256(pepper, secret)` with a `pepper_version` column and a make-before-break dual-pepper rotation window. No argon2id (keys are high-entropy).
|
||||
6. **Op-log retention** — native PostgreSQL **daily RANGE partitioning**, retention by **partition DROP** (pg_partman), uniform 90-day horizon with a cheap row-level success-trim inside young partitions.
|
||||
|
||||
## Confirmed (2026-06-13)
|
||||
|
||||
- **Module org** — `github.com/Silo-Server/silo-push-relay` (matches the real org in `silo-server`'s `go.mod`; the plan's `silo-app` was provisional).
|
||||
- **Deployment target** — the relay is **not** deployed to GCP. Per Decision 2 this selects the off-GCP credential path: **Workload Identity Federation** (keyless, non-secret credential-config + OIDC token) preferred, with **service-account JSON in a secret manager** as the last-resort fallback. No attached-GCP-SA path.
|
||||
|
||||
## Still needs human / operational input
|
||||
|
||||
1. **Provider provisioning** (the real long pole) — Apple Developer account + APNs `.p8` key + per-platform bundle topics; Firebase project(s) + the WIF binding (or SA JSON in a secret manager). Gates Phases 3/4/6. See `01` §7.
|
||||
2. **Off-GCP OIDC issuer for WIF** — confirm the relay host has a usable OIDC issuer for Workload Identity Federation; if none, fall back to SA-JSON-in-secret-manager (Decision 2).
|
||||
3. **Multi-region** — out of scope for v1 (single region); revisit a regional Redis/Postgres strategy if latency to distant self-hosters matters.
|
||||
|
||||
## Relationship to the parent notifications design
|
||||
|
||||
This relay corresponds to phases 7–8 of the parent plan ([`../README.md`](../README.md) "Phasing"): `notifications.apple_push_enabled` and `notifications.android_push_enabled`. Those phases require Silo to provision Apple Developer + Firebase accounts and operate this service. The self-hosted server side (registration, dispatcher, pacing, retry, the dispatch outbox) is specified in `../02-apns-relay.md`, `../03-fcm-relay.md`, and `../01-release-events-and-inbox.md`.
|
||||
@@ -1,188 +0,0 @@
|
||||
# Core Notifications System — Design
|
||||
|
||||
Date: 2026-06-10
|
||||
Status: approved (design); implementation plan to follow
|
||||
|
||||
## Summary
|
||||
|
||||
A first-party, whole-system notification layer for Silo: persisted per-user notifications with an inbox, read state, per-category preferences, real-time toasts over the existing event WebSocket, and admin announcements. Ships across the server, web UI, and both mobile apps (in-app only — no APNs/FCM push in this project). External delivery (Discord, email, webhooks) remains the job of the existing `silo.notifications` plugin, which consumes the same events.
|
||||
|
||||
## Goals
|
||||
|
||||
- Persisted per-user notifications: inbox, unread badge, read/dismiss state.
|
||||
- Real-time in-app delivery to connected clients via the existing `events.Hub` / `/api/v1/events/ws` pipeline.
|
||||
- Five categories: `request`, `content`, `announcement`, `system`, `admin` (operational alerts for admin users), plus opt-in `content_digest`.
|
||||
- Per-category user preferences (announcements not mutable).
|
||||
- Admin announcements with audience targeting and expiry.
|
||||
- A documented `notifications.send` event contract so any plugin can create in-app notifications (and, via the existing external plugin's subscription to the same event, external ones).
|
||||
- Core request lifecycle events (`request.*`) — net-new publishing from `internal/requests`, consumed by notifications and available to the requests UI.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- Mobile push (APNs/FCM) — future project; nothing here blocks it.
|
||||
- Request-system unification (`request_router.v1`, migrating ebook/audiobook request plugins onto `media_requests`) — separate future project. When it lands, the book plugins' `notifications.send` publishes are replaced by core `request.*` events with no notifications schema/API change.
|
||||
- Absorbing external delivery channels into core.
|
||||
- Fine-grained per-show/per-author subscription rules (watchlist/favorites are the v1 signal).
|
||||
|
||||
## Architecture
|
||||
|
||||
New package `internal/notifications` in silo-server:
|
||||
|
||||
1. **Materializer** — in-process consumer registered on `events.Hub` at startup (same pattern as `internal/plugins/event_dispatcher.go`). Runs events through per-category matchers; writes `notifications` rows for each recipient.
|
||||
2. **Store** — Postgres repository: insert (idempotent), list (cursor), unread count, mark read, dismiss, preferences CRUD.
|
||||
3. **Notifier** — after insert, publishes `notification.created` on a new `ChannelNotifications` event channel, user-scoped like `ChannelUserState`. The WS handler (`internal/api/handlers/events_ws.go`) adds this channel to the per-user subscribe set; frames carry the full notification object. Subscribe-time snapshot carries unread count only.
|
||||
4. **API handlers** — REST endpoints below.
|
||||
5. **Service API** — `notifications.Create(...)` for direct in-process callers (system/security events).
|
||||
|
||||
Creation is DB-first and synchronous: a notification exists when the row commits. WS fan-out is best-effort; clients that miss frames reconcile on next fetch. No delivery queue or retry in core — "delivery" is reading your own table.
|
||||
|
||||
## Data model
|
||||
|
||||
One migration, three tables:
|
||||
|
||||
```sql
|
||||
notifications (
|
||||
id bigserial PRIMARY KEY,
|
||||
user_id int NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
profile_id text NULL REFERENCES user_profiles(id) ON DELETE CASCADE, -- NULL = user-wide
|
||||
category text NOT NULL, -- 'request' | 'content' | 'announcement' | 'system' | 'admin'
|
||||
type text NOT NULL, -- e.g. 'request.approved', 'content.watchlist_episode'
|
||||
title text NOT NULL,
|
||||
body text NOT NULL,
|
||||
link text NULL, -- in-app deep link
|
||||
item_id bigint NULL, -- optional content reference (artwork, dedup)
|
||||
source_event text NULL, -- originating event name (audit)
|
||||
dedup_ref text NULL, -- idempotency: UNIQUE (user_id, type, dedup_ref) WHERE dedup_ref IS NOT NULL
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
read_at timestamptz NULL,
|
||||
dismissed_at timestamptz NULL,
|
||||
expires_at timestamptz NULL
|
||||
);
|
||||
-- index: (user_id, created_at DESC) partial WHERE dismissed_at IS NULL
|
||||
-- partial unique index for dedup as above; inserts use ON CONFLICT DO NOTHING
|
||||
|
||||
notification_preferences (
|
||||
user_id int NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
category text NOT NULL, -- 'request' | 'content' | 'system' | 'admin' | 'content_digest'
|
||||
enabled bool NOT NULL,
|
||||
PRIMARY KEY (user_id, category)
|
||||
);
|
||||
-- absent row = enabled, except content_digest which defaults OFF
|
||||
-- 'announcement' is not a valid category here (not mutable)
|
||||
|
||||
announcements (
|
||||
id bigserial PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
body text NOT NULL,
|
||||
audience jsonb NOT NULL, -- {"all":true} | {"user_ids":[...]} | {"library_ids":[...]}
|
||||
created_by int REFERENCES users(id),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
expires_at timestamptz NULL
|
||||
);
|
||||
```
|
||||
|
||||
Announcements fan out to `notifications` rows at publish time (audience resolved then), so the inbox is always a single-table query and read state is per-user.
|
||||
|
||||
Child profiles: rows with `category IN ('request','system','admin')` are filtered out of child-profile sessions server-side; content matching never targets a child profile unless the matched watchlist/favorite belongs to that profile.
|
||||
|
||||
Retention (nightly job, existing scheduler pattern): dismissed rows purged after 30 days; all rows after 90 days; unread rows from expired announcements at expiry.
|
||||
|
||||
## API
|
||||
|
||||
User endpoints (authenticated, profile from claims):
|
||||
|
||||
```
|
||||
GET /api/v1/notifications ?unread=1&category=&cursor=&limit=
|
||||
GET /api/v1/notifications/unread-count → {count}
|
||||
POST /api/v1/notifications/read {ids:[...]} | {all:true}
|
||||
POST /api/v1/notifications/{id}/dismiss
|
||||
GET /api/v1/notifications/preferences
|
||||
PUT /api/v1/notifications/preferences {category: enabled, ...}
|
||||
```
|
||||
|
||||
List returns user-wide rows plus active-profile rows, newest first, cursor-paginated in the standard envelope. Child-profile filtering server-side.
|
||||
|
||||
Admin endpoints (RequireAdmin group):
|
||||
|
||||
```
|
||||
GET /api/v1/admin/announcements
|
||||
POST /api/v1/admin/announcements {title, body, audience, expires_at?}
|
||||
DELETE /api/v1/admin/announcements/{id} -- also dismisses unread fanned-out rows
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
### New: core request lifecycle events
|
||||
|
||||
`internal/requests/service.go` and the reconciliation task publish on a new `ChannelRequests`:
|
||||
|
||||
- `request.submitted`, `request.approved`, `request.declined`, `request.completed`, `request.failed`
|
||||
- Payload: `request_id, user_id, profile_id, title, media_type, quality, status`
|
||||
|
||||
`ChannelRequests` is added to WS subscribe sets (requesting user + admins), giving the requests UI live updates as a side benefit. This is the only change to the requests subsystem in this project.
|
||||
|
||||
### New: `notifications.send` plugin contract
|
||||
|
||||
The materializer consumes `notifications.send` events:
|
||||
|
||||
```json
|
||||
{ "user_id": 1, "profile_id": null, "category": "request",
|
||||
"type": "request.fulfilled", "title": "...", "body": "...",
|
||||
"link": null, "dedup_ref": "ebook-req-123:fulfilled" }
|
||||
```
|
||||
|
||||
Payloads are validated; malformed events are dropped with a logged warning. The external `silo.notifications` plugin already subscribes to this event name, so one publish reaches both in-app and external channels. The ebook/audiobook request plugins each add one `PublishEvent("notifications.send", ...)` at their acknowledged/fulfilled/failed transitions (their existing status events do not carry `user_id`; the plugins own that context).
|
||||
|
||||
### Matchers
|
||||
|
||||
- **request** — consumes core `request.*` events only. Dedup ref `(request_id, status)`.
|
||||
- **content** — consumes `library.item_added` / `catalog.library.changed`. For each added item: resolve series/author; match per profile against watchlist, favorites (author/series link), and in-progress series (last play within 21 days); union, dedup, filter by library access. Burst guard: one notification per series per profile per hour (season imports collapse).
|
||||
- **content_digest** — nightly job (not event-driven), opt-in: one row per user summarizing the previous day's additions to accessible libraries.
|
||||
- **system** — not matched from the hub; emitted via `notifications.Create()` directly at the source (password change, new-device login, guest-pass redemption). Sites also publish their events as today; the explicit call means security notices never depend on matcher wiring.
|
||||
- **announcement** — fan-out at publish time.
|
||||
- **admin** — consumes existing `ChannelJobs` / `ChannelScans` / `ChannelTasks` failure events plus plugin-host crash/install failures; targets admin users; repeat-failure throttle (same source, max one per hour).
|
||||
|
||||
Preference checks run in the materializer before insert (cached per-user prefs lookup).
|
||||
|
||||
## Web UI
|
||||
|
||||
1. **Bell + dropdown** in the top bar: unread badge from `unread-count`, kept live by `ChannelNotifications` frames via the existing `RealtimeEventsProvider` socket. Dropdown: latest ~10, mark-all-read, deep links.
|
||||
2. **Toasts** — WS notification frames fire sonner toasts; suppressed while the dropdown is open or when the frame targets another profile.
|
||||
3. **Inbox page** (`/notifications`) — paginated list, category filter tabs, dismiss, mark-read on view.
|
||||
4. **Settings** — per-category toggles (`content_digest` default off; `admin` visible to admins only). Admin: Announcements page (list/compose/expire) under admin settings.
|
||||
|
||||
## Mobile (in-app, both platforms)
|
||||
|
||||
Same three features in silo-apple and silo-android against the frozen REST API: inbox screen (list, mark-read, dismiss), unread badge on existing navigation, preferences screen. Transport: REST refresh of unread-count + first page on app foreground and pull-to-refresh. No WS subscription in v1 (current app sockets are playback-scoped; no background delivery exists without push anyway). TV variants: badge + read-only inbox, no preferences editing.
|
||||
|
||||
## Error handling
|
||||
|
||||
- Matcher isolation: a panic/error in one matcher logs and skips that event for that matcher only; never blocks the hub or other matchers (same containment as the plugin event dispatcher).
|
||||
- Idempotent inserts via the dedup partial unique index + `ON CONFLICT DO NOTHING`; replayed events cannot double-notify.
|
||||
- WS fan-out is fire-and-forget; DB rows are the source of truth.
|
||||
- `notifications.send` validation failures: drop + warn (plugin bugs must not break core paths).
|
||||
|
||||
## Testing
|
||||
|
||||
- Unit tests per matcher with synthetic events; content-matcher edge cases: child profiles, library access, burst guard, dedup.
|
||||
- Store tests against Postgres (repo convention).
|
||||
- One integration test: event → materializer → row → WS frame through a real hub.
|
||||
- API handler tests: auth, profile scoping, child filtering, admin gates.
|
||||
- Web: component tests for bell/inbox; extend the RealtimeEventsProvider test pattern for the new channel.
|
||||
|
||||
## Implementation sequencing
|
||||
|
||||
1. **Server** — migration, store, materializer + matchers, request event publishing, API, WS channel, retention job.
|
||||
2. **Web** — bell, toasts, inbox, settings, announcements admin.
|
||||
3. **Mobile** — apple + android in-app surfaces against the frozen API.
|
||||
4. (Separate projects, later: request unification via `request_router.v1`; mobile push.)
|
||||
|
||||
## Future work
|
||||
|
||||
- Request unification: core `request.*` events replace the book plugins' `notifications.send` publishes; notifications unchanged.
|
||||
- Push: APNs/FCM device registry; the notifier gains a push fan-out alongside WS.
|
||||
- Digest scheduling preferences (weekly, time-of-day).
|
||||
- Profile claims enrichment: embed `is_child` in profile-scoped auth claims. Fixes two implementation-time findings: the WS pipeline cannot child-filter (snapshot and live frames use client-asserted profile identity; REST remains authoritative and fails closed — see the snapshot comment in `internal/api/handlers/events_ws.go`), and the REST `childSafe` check costs one profile lookup per request.
|
||||
- Admin-alert coverage: only `job.failed` and `scan.failed` events exist today. `task.failed` and plugin-host crash events are not published by their subsystems yet; when they are, the admin matcher needs only a map entry.
|
||||
- Dedicated `item_added` catalog event: the content matcher keys on `catalog.item.changed` (`metadata_updated`) plus a 48-hour `media_items.created_at` recency gate, because the scanner publishes no per-item add event. An explicit ingest-time event would remove the proxy heuristic.
|
||||
- New-device login notifications: deferred from v1 (no device-fingerprint store exists); password-change notifications shipped.
|
||||
@@ -1,173 +0,0 @@
|
||||
# Core Push Notifications — Design
|
||||
|
||||
Date: 2026-06-10
|
||||
Status: approved (design); implementation plan to follow
|
||||
|
||||
## Summary
|
||||
|
||||
A server-side push subsystem that mirrors the in-app notifications created in Phase 1 out to registered devices via APNs (Apple), FCM (Android), and Web Push (browsers). Delivery is durable (a DB-backed queue with a retry worker) and presence-gated (a notification only pushes to devices whose user has no live WebSocket connection after a short grace delay). This is a core-server project: the mobile clients have no push code yet, so the deliverable is the registry, queue, worker, transports, and APIs — built transport-agnostic so APNs/FCM are drop-in alongside the immediately-testable Web Push.
|
||||
|
||||
## Goals
|
||||
|
||||
- Deliver Phase-1 notifications to devices over APNs, FCM, and Web Push.
|
||||
- Durable delivery: survive restarts, retry with backoff, prune dead tokens, observable in the admin task view.
|
||||
- Presence-gated: don't buzz a device for a notification the user is actively seeing in-app.
|
||||
- Reuse the existing per-category mute preferences; add per-device push on/off. No new per-channel matrix.
|
||||
- Encrypted provider credentials via the existing settings mechanism.
|
||||
- Transport-agnostic core: one `Transport` interface, three implementations, selected by device platform.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- Mobile/web client integration (separate per-client projects; this ships the server contract).
|
||||
- A separate in-app-vs-push preference matrix (category mutes + per-device toggle only).
|
||||
- Rich push (images, action buttons) — title/body/link/category payload only in v1.
|
||||
- New-device-login / security pushes beyond what the notification categories already produce.
|
||||
|
||||
## Architecture
|
||||
|
||||
New `internal/push` package and a new `internal/presence` package, plus touches to three existing seams (`notifications.Service.Create`, the events WS handler, `cmd/silo/main.go` wiring). Five independently-testable units:
|
||||
|
||||
1. **Device/token registry** — extends `user_devices` with push columns; a registration endpoint clients call to enroll/refresh/revoke a token and toggle per-device push. Owns "which devices want push and how to reach them."
|
||||
2. **Presence registry** (`internal/presence`) — process-local `user_id → live-connection refcount`, incremented/decremented by the events WS handler on connect/disconnect. Answers "does this user have a live connection right now?" Cluster-aware via a short-TTL Redis key; degrades to per-node if Redis is absent.
|
||||
3. **Enqueuer** — called from `notifications.Service.Create()` after the row commits. Resolves push-eligible devices, writes one `push_deliveries` row per device with `not_before = now + grace`.
|
||||
4. **Delivery worker** — a `PushDeliveryTask` on the existing task framework; claims due rows, re-checks presence, dispatches via the platform's transport, records outcome, prunes dead tokens.
|
||||
5. **Transports** — a `Transport` interface (`Send(ctx, target, payload) → Result`) with APNs, FCM, and Web Push implementations selected by device platform; credentials from the encrypted settings repo.
|
||||
|
||||
Data flow: `Create()` → enqueuer writes deliveries → worker (interval) claims due & un-present → transport sends → outcome / dead-token prune. The in-app path (immediate WS publish) is untouched; push is the deferred, gated mirror.
|
||||
|
||||
### Seam reference (current code)
|
||||
|
||||
- Fan-out hook: `internal/notifications/service.go` `Create()` — after `created, err := s.store.Insert(...)` succeeds and before/alongside the `hub.PublishJSON(... "notification.created" ...)` call. Enqueue is best-effort and must not block or fail `Create()`.
|
||||
- Presence increment: `internal/api/handlers/events_ws.go` — after a successful subscribe, `release := presence.Add(claims.UserID)`, `defer release()`.
|
||||
- No presence tracking exists today (confirmed); the registry is net-new.
|
||||
- `user_devices` exists (`migrations/sql/180_user_devices.sql`, PK `user_id, profile_id, device_id`) with no token column.
|
||||
- Credential encryption: `internal/catalog/encrypted_settings_repo.go` `SensitiveSettingKeys`.
|
||||
- Background-worker precedent: `internal/taskmanager/tasks/*` (e.g. the request reconciler, notifications digest/retention).
|
||||
|
||||
## Data model
|
||||
|
||||
Extend `user_devices` (a push token belongs to a device):
|
||||
|
||||
```sql
|
||||
ALTER TABLE user_devices
|
||||
ADD COLUMN push_token text NULL,
|
||||
ADD COLUMN push_transport text NULL, -- 'apns' | 'fcm' | 'webpush'
|
||||
ADD COLUMN push_enabled boolean NOT NULL DEFAULT true,
|
||||
ADD COLUMN push_token_at timestamptz NULL,
|
||||
ADD COLUMN push_failures integer NOT NULL DEFAULT 0;
|
||||
```
|
||||
|
||||
A device is push-eligible when `push_token IS NOT NULL AND push_enabled`. For `webpush`, `push_token` holds the JSON subscription object (endpoint + p256dh + auth); APNs/FCM store the bare token string. Each transport interprets its own format.
|
||||
|
||||
Delivery queue:
|
||||
|
||||
```sql
|
||||
CREATE TABLE push_deliveries (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
notification_id bigint NOT NULL REFERENCES notifications(id) ON DELETE CASCADE,
|
||||
user_id integer NOT NULL,
|
||||
device_id text NOT NULL,
|
||||
transport text NOT NULL,
|
||||
status text NOT NULL DEFAULT 'pending', -- pending|sent|failed|skipped|dead
|
||||
attempts integer NOT NULL DEFAULT 0,
|
||||
not_before timestamptz NOT NULL,
|
||||
last_error text NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX push_deliveries_claim_idx
|
||||
ON push_deliveries (not_before)
|
||||
WHERE status IN ('pending','failed');
|
||||
```
|
||||
|
||||
Worker claim: `status IN ('pending','failed') AND not_before <= now()`, ordered by `not_before`, `FOR UPDATE SKIP LOCKED` (safe across nodes/concurrent workers).
|
||||
|
||||
Outcomes:
|
||||
- **sent** — terminal success.
|
||||
- **skipped** — user was present (live WS) at claim time; the in-app delivery sufficed. Terminal.
|
||||
- **failed** — soft error (timeout, 5xx, rate-limit); bump `attempts`, push `not_before` out by the backoff schedule (1m, 5m, 30m). Once the schedule is exhausted (4 total attempts) → `dead`.
|
||||
- **dead** — hard error / unregistered token; terminal. Also clears `push_token` and sets `push_enabled=false` on the `user_devices` row so the dead device stops being enqueued.
|
||||
|
||||
Deliveries do not duplicate notification content — the worker joins `notifications` at send time so an edited/expired notification sends current content (and an expired/dismissed one can be skipped). Retention: terminal deliveries older than 7 days are deleted by a pass on the existing notifications retention task.
|
||||
|
||||
Provider config (added to `SensitiveSettingKeys`, encrypted at rest):
|
||||
`push.apns.p8_key`, `push.apns.key_id`, `push.apns.team_id`, `push.apns.bundle_id`, `push.fcm.service_account_json`, `push.webpush.vapid_public`, `push.webpush.vapid_private`, `push.webpush.subject`. A transport with missing/blank config reports itself unconfigured; its deliveries short-circuit to `skipped` (reason: unconfigured) rather than erroring in a loop.
|
||||
|
||||
## API
|
||||
|
||||
Device push registration (authenticated user routes; device identified by the existing `X-Silo-Device-Id` header + `user_id` from claims — never from the body):
|
||||
|
||||
```
|
||||
PUT /api/v1/notifications/push/device {transport, token} → 204
|
||||
enroll/refresh this device's token (upsert onto the user_devices row);
|
||||
resets push_failures, sets push_enabled=true, push_token_at=now
|
||||
DELETE /api/v1/notifications/push/device → 204
|
||||
revoke: clear token, disable push for this device
|
||||
GET /api/v1/notifications/push/devices → {devices:[{device_id,name,platform,push_enabled,registered_at}]}
|
||||
PUT /api/v1/notifications/push/devices/{device_id} {enabled} → 204
|
||||
per-device on/off toggle
|
||||
GET /api/v1/notifications/push/webpush-key → {vapid_public_key} (empty when web push unconfigured)
|
||||
```
|
||||
|
||||
Admin provider config uses the existing admin settings mechanism (encrypted `push.*` keys) — no bespoke write endpoints. One read for the admin UI:
|
||||
|
||||
```
|
||||
GET /api/v1/admin/push/status → {apns:{configured:bool}, fcm:{configured:bool}, webpush:{configured:bool}}
|
||||
```
|
||||
|
||||
Status reports booleans only; secrets are never returned.
|
||||
|
||||
## Presence registry
|
||||
|
||||
`presence.Registry` interface: `Connected(userID int) bool` and `Add(userID int) (release func())`. The events WS handler calls `release := registry.Add(claims.UserID)` after a successful subscribe and `defer release()` on disconnect.
|
||||
|
||||
- In-process implementation: `map[int]int` refcount under a mutex.
|
||||
- Cluster-aware implementation: `INCR push:presence:{userID}` in Redis with a 60s TTL refreshed on the WS heartbeat; `DECR` on release; `Connected` is key `> 0`. Redis is already used by the events hub.
|
||||
- Presence failure (Redis down) fails **open** — we push — because a missed push is worse than a redundant one.
|
||||
|
||||
The worker re-checks `Connected` at claim time; a present user's due delivery becomes `skipped`. The `not_before` grace (default 30s) means an actively-in-app user almost never gets a redundant push, while a user who closed the app moments ago does.
|
||||
|
||||
## Error handling & delivery semantics
|
||||
|
||||
- Enqueuer is best-effort; failure to write deliveries logs and returns. `Create()` is never blocked or failed by push.
|
||||
- Worker isolates per-delivery: one device's failure never blocks the batch.
|
||||
- Soft errors → `failed` + exponential backoff on `not_before` (1m, 5m, 30m), then `dead` after 4 total attempts.
|
||||
- Hard errors (`Unregistered`/`InvalidToken`/FCM `NOT_FOUND`) → immediate `dead` + clear token + disable push on `user_devices`.
|
||||
- Unconfigured transport → its deliveries short-circuit to `skipped` (reason logged once, not per-row spam).
|
||||
- Rate-limit responses park that transport briefly (honor `Retry-After`).
|
||||
- Child-profile suppression applied at enqueue: categories in {request, system, admin} are never enqueued for a child profile (same rule as the web toast suppression).
|
||||
- APNs/FCM payloads set a collapse/thread key = notification id to avoid duplicate banners across retries.
|
||||
|
||||
## Security
|
||||
|
||||
- Provider secrets live only in the encrypted settings repo; never logged, never returned by any read endpoint. `/admin/push/status` reports booleans.
|
||||
- Token registration is authenticated and device-scoped to the caller's `user_id` (from claims).
|
||||
- Push payload carries title/body/link/category + notification id — no content beyond what the notification already holds.
|
||||
|
||||
## Testing
|
||||
|
||||
- Worker tested with a fake `Transport` asserting outcome transitions: sent, skipped (present), failed-with-backoff, dead-prunes-token. No network.
|
||||
- Each real transport unit-tested against a mocked HTTP endpoint (request shape, auth header, error-code mapping) — standard repo pattern.
|
||||
- Enqueuer tested with a fake device store + presence stub: eligible-device resolution, child suppression, per-device disable, `not_before` grace.
|
||||
- Presence registry: refcount add/release, Connected transitions, Redis-absent fallback.
|
||||
- Store tests against Postgres: claim query with `SKIP LOCKED`, dead-token prune, retention.
|
||||
- One integration test: `Create() → delivery row → worker → fake transport → sent`.
|
||||
- Handler tests: registration auth-scoping, per-device toggle, `/admin/push/status` unconfigured states, `webpush-key` empty when unconfigured.
|
||||
|
||||
## Implementation sequencing
|
||||
|
||||
1. Migration (user_devices columns + push_deliveries) and domain types.
|
||||
2. `internal/presence` registry (in-process + Redis) and WS-handler wiring.
|
||||
3. Device registry store + registration/toggle endpoints.
|
||||
4. Enqueuer + hook into `Create()`.
|
||||
5. `Transport` interface + fake; delivery worker + task registration; outcome/backoff/dead-prune.
|
||||
6. Real transports (Web Push first — end-to-end testable today; then APNs, then FCM) + encrypted config keys + `/admin/push/status`.
|
||||
7. Retention pass + full verification.
|
||||
|
||||
## Future work
|
||||
|
||||
- Client integration: Apple (APNs token registration + handling), Android (FCM), web (service worker + VAPID subscription). Each binds to the frozen registration API.
|
||||
- Presence-aware in-app→push escalation tuning (per-category grace, quiet hours).
|
||||
- Rich push (images from `item_id` artwork, action buttons).
|
||||
- Per-channel preference matrix if category+device proves too coarse.
|
||||
- Shared-device push semantics: a physical device registers one OS token but one user_devices row per profile. For user-wide notifications, EligibleDevices currently returns one delivery per profile-row → the same token is sent to more than once (the per-notification collapse key dedupes the on-device banner, so the user sees one, but the sends are redundant). And child-category suppression on a device shared by a child and an adult profile is ambiguous because push cannot know the active profile at send time. Both resolve with the active-profile/claims-enrichment work also needed for WS child-filtering: carry the active profile identity so EligibleDevices can dedupe by token and apply the correct profile's suppression.
|
||||
@@ -1,120 +0,0 @@
|
||||
# Core Push — Web Client & Admin Config — Design
|
||||
|
||||
Date: 2026-06-10
|
||||
Status: approved (design); implementation plan to follow
|
||||
|
||||
## Summary
|
||||
|
||||
The core-buildable slice of Phase 3 (client work): a Web Push browser client and an admin push-provider config UI, both living in the silo-server web app plus one ~10-line Go endpoint. This makes the push subsystem operable end to end from the browser — an operator enters provider credentials in an admin page, a user enables push on their browser, and notifications arrive as OS banners with the app closed. The mobile clients (silo-apple APNs, silo-android FCM) remain out of scope; they are separate per-repo projects that consume the same frozen registration API.
|
||||
|
||||
This delivers user-facing notification delivery to browsers. It is not a plugin-communication channel; plugins relate to notifications only via the existing `notifications.send` event contract (a plugin produces a user notification, which then flows to in-app + push).
|
||||
|
||||
## Goals
|
||||
|
||||
- Web Push delivery to browsers: service worker, subscription, receive, click-through.
|
||||
- A per-device "push on this device" control and a device-management list in user settings.
|
||||
- An admin page to configure APNs / FCM / Web Push provider credentials with readiness status.
|
||||
- VAPID key generation from the admin UI.
|
||||
- Reuse everything already built: the frozen push registration API, the encrypted settings write path, the Phase-2 notifications settings page and api client.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- Mobile client integration (silo-apple / silo-android) — separate projects against the frozen API.
|
||||
- A PWA framework / offline caching (vite-plugin-pwa) — a static service worker covers push; offline is out of scope.
|
||||
- Any change to the push delivery worker, transports, or registration API (all frozen).
|
||||
- Rich push (images, action buttons).
|
||||
|
||||
## Frozen dependencies (already on this branch)
|
||||
|
||||
Push registration API (`internal/api/handlers/push.go`, routes in `router.go`):
|
||||
|
||||
```
|
||||
PUT /api/v1/notifications/push/device {transport, token} → 204 (RequireProfile)
|
||||
DELETE /api/v1/notifications/push/device → 204 (RequireProfile)
|
||||
GET /api/v1/notifications/push/devices → {devices:[DeviceInfo]}
|
||||
PUT /api/v1/notifications/push/devices/{device_id} {enabled} → 204
|
||||
GET /api/v1/notifications/push/webpush-key → {vapid_public_key}
|
||||
GET /api/v1/admin/push/status → {apns,fcm,webpush: bool}
|
||||
```
|
||||
|
||||
`DeviceInfo`: `{device_id, name, platform, transport, push_enabled, registered_at}`. The api client (`web/src/api/client.ts`) auto-attaches `X-Silo-Device-Id` and `X-Profile-Id`. WebPush tokens are stored as the JSON subscription object.
|
||||
|
||||
Admin settings write path: `PUT /api/v1/admin/settings/{key}` `{value}` (`admin.go` HandleUpdateSetting) — all `push.*` keys are pre-registered sensitive in `encrypted_settings_repo.go` (encrypted at rest, value redacted from reads). No new write endpoint needed. Sensitive-status hook (`useAdminSensitiveStatus`) reports which keys are stored.
|
||||
|
||||
Web push server transport already sends payload `{id, title, body, link, category}` with `Topic: "n"+base36(notificationID)` for collapse.
|
||||
|
||||
## Architecture
|
||||
|
||||
Six units (five web, one tiny Go endpoint):
|
||||
|
||||
1. **Service worker** — `web/public/sw.js` (static). `push` → `showNotification` from payload; `notificationclick` → focus an open app tab (postMessage the link) or `openWindow(link)`; `pushsubscriptionchange` → re-subscribe with the cached VAPID key and re-register (best-effort token refresh).
|
||||
2. **SW registration** — in `web/src/main.tsx`, registers `/sw.js` when `serviceWorker` + `PushManager` are supported; no-op otherwise.
|
||||
3. **`usePushDevice` hook** — `web/src/hooks/usePushDevice.ts`. Owns the browser-push lifecycle and exposes a status state machine + `enable()`/`disable()`.
|
||||
4. **Settings UI** — extend `web/src/pages/settings/NotificationsSettings.tsx`: "Push on this device" control + a device-management list (per-device enable/disable).
|
||||
5. **Admin push-config page** — `web/src/pages/admin-settings/PushSettings.tsx`: three credential cards + VAPID keygen + readiness badges.
|
||||
6. **VAPID keygen endpoint** — `POST /api/v1/admin/push/generate-vapid-keys` (RequireAdmin), ~10 lines in `push.go`. The only net-new server code.
|
||||
|
||||
## Push client data flow
|
||||
|
||||
`usePushDevice` reports `status`:
|
||||
- **unsupported** — no serviceWorker/PushManager, or iOS Safari not installed as a PWA. UI explains; no action.
|
||||
- **blocked** — `Notification.permission === "denied"`. UI explains how to re-enable in site settings (cannot un-deny programmatically).
|
||||
- **off** — supported, not subscribed. Toggle → `enable()`.
|
||||
- **on** — subscribed and registered. Toggle → `disable()`.
|
||||
- **pending** — transition in flight; control disabled.
|
||||
|
||||
`enable()`: `Notification.requestPermission()` → if granted, `navigator.serviceWorker.ready` → `GET /notifications/push/webpush-key` → `pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array(vapidPublic) })` → `PUT /notifications/push/device { transport: "webpush", token: JSON.stringify(subscription.toJSON()) }`. The api client supplies device + profile headers (satisfies the route's RequireProfile). Cache the VAPID public key (Cache Storage / IndexedDB) so the SW can re-subscribe on `pushsubscriptionchange` without the app open. Success → `on`, toast.
|
||||
|
||||
`disable()`: `pushManager.getSubscription()` → `unsubscribe()` (best-effort) → `DELETE /notifications/push/device` → `off`, toast.
|
||||
|
||||
Receiving (SW): payload `{id,title,body,link,category}` → `showNotification(title, { body, data:{link}, tag:"n"+id })`. The `tag` is an on-device display-dedup keyed on the notification id (a redelivery of the same notification replaces the visible banner rather than stacking); it is independent of the server's Web Push `Topic` header (which dedups in the push-service queue). `notificationclick` → focus matching-origin client + postMessage `{type:"notification-click", link}`, else `openWindow(link ?? "/notifications")`.
|
||||
|
||||
Idempotency / healing: `enable()` is safe to re-run (server upserts); a stale local subscription with no server row heals on re-enable. Permission-granted-but-subscribe-fails → toast, stay `off`.
|
||||
|
||||
## Admin config page
|
||||
|
||||
`PushSettings.tsx`, registered in `AdminSettingsLayout` `SETTINGS_GROUPS` (Connections group). Pattern copied from `IntegrationsSettings.tsx` (`SettingField` + `CredentialStatus` + `useUpdateServerSetting`). Cards:
|
||||
|
||||
- **Web Push** (leads — usable today): `push.webpush.vapid_public`, `push.webpush.vapid_private`, `push.webpush.subject`. "Generate keys" button → `POST /admin/push/generate-vapid-keys` → fills public/private fields (not auto-saved; admin reviews then saves each). Subject defaults to a `mailto:` hint.
|
||||
- **APNs**: `push.apns.p8_key` (textarea), `push.apns.key_id`, `push.apns.team_id`, `push.apns.bundle_id`.
|
||||
- **FCM**: `push.fcm.service_account_json` (textarea).
|
||||
|
||||
Readiness: card header badge from `GET /admin/push/status`; per-field "configured, leave blank to keep" from the sensitive-status hook (secrets never returned). Each save = `PUT /admin/settings/{key}` `{value}` via `useUpdateServerSetting`; encryption automatic; badges refetch after save.
|
||||
|
||||
VAPID keygen endpoint: generates and returns `{vapid_public, vapid_private}`; does not persist. Handler test: non-empty pair, admin-gated.
|
||||
|
||||
### Open decision — "Send test push" button
|
||||
|
||||
OPTION (admin's call at spec review): a "Send test push" button that creates a one-off system notification addressed to the admin themselves to verify config→subscribe→receive end to end from the UI. Requires one more net-new server endpoint (`POST /api/v1/admin/push/test` → `notifications.Create` a system notification for the caller). Recommended for operability (turns "did I configure this right?" into one click) but it is the only other net-new server surface. **Decision: include it** unless cut at review. (Implementation plan will gate this behind the decision.)
|
||||
|
||||
## Error handling
|
||||
|
||||
- Capability gating (unsupported, denied) are UI states, not errors; iOS Safari shows "Add to Home Screen to enable push."
|
||||
- Transient subscribe/register failures → toast, leave `off`; nothing half-committed (server PUT is source of truth, idempotent).
|
||||
- SW resilience: tolerate partial payload (generic title fallback); `notificationclick` falls back to `/notifications`; failed `pushsubscriptionchange` re-register logs and retries on next app load (browser stops receiving until reopened — never a crash).
|
||||
- Admin page: save failures toast; readiness badges always reflect server truth (refetch after save); partial config shows "Not configured" until complete; generate-keys failure leaves fields untouched.
|
||||
|
||||
## Testing
|
||||
|
||||
- `usePushDevice` — unit tests with mocked serviceWorker/PushManager/Notification/api client: status derivation, enable() happy path (permission→key→subscribe→PUT body), permission-denied short-circuit, disable() (unsubscribe + DELETE), idempotent re-enable.
|
||||
- Settings UI — component tests: each status renders the right control; device-list rows + per-device toggle mutation.
|
||||
- Service worker — vitest over the pure handlers: payload→showNotification-args mapping, click→target-URL logic. DOM wiring exercised in the manual smoke.
|
||||
- Admin page — component tests: three cards render, badges reflect `/admin/push/status`, save calls `PUT /admin/settings/{key}` with the right key, generate-keys fills fields.
|
||||
- Server — VAPID keygen handler test (non-empty pair, admin-gated); test-push endpoint handler test (if included).
|
||||
- Manual smoke (documented in plan): real browser — configure VAPID, enable push, trigger a notification, confirm OS banner with app closed, click → deep link.
|
||||
|
||||
## Implementation sequencing
|
||||
|
||||
1. Server: VAPID keygen endpoint (+ test-push endpoint if kept).
|
||||
2. Web: service worker + registration + VAPID-key caching.
|
||||
3. Web: `usePushDevice` hook + tests.
|
||||
4. Web: NotificationsSettings push control + device list.
|
||||
5. Web: admin PushSettings page + nav.
|
||||
6. Verification + manual browser smoke.
|
||||
|
||||
## Future work
|
||||
|
||||
- Mobile clients (APNs/FCM) against the frozen API.
|
||||
- Shared-device token dedup + active-profile child-suppression (recorded in the push server spec) — informs whether multiple browsers/profiles dedupe.
|
||||
- Per-category push targeting if category mutes prove too coarse.
|
||||
- PWA/offline (vite-plugin-pwa) if the app later wants installable offline behavior.
|
||||
@@ -1,65 +0,0 @@
|
||||
# Home Notification Bars — Design
|
||||
|
||||
Date: 2026-06-11
|
||||
Status: approved (design); implementation to follow
|
||||
|
||||
## Summary
|
||||
|
||||
Surface notifications at the top of the Home page so they're seen without opening the inbox tab, with system-level **announcements** visually distinguished from personal notifications. Two slim, dismissible bars stack at the top of Home: an **announcement bar** (distinct accent + 📢 + "Announcement" label) above a **notification strip** (the most recent unread personal notification). Both reuse existing hooks; no server changes. Web-only, additive.
|
||||
|
||||
## Goals
|
||||
|
||||
- Show the most recent unread notification high on the Home page, not only behind the bell/inbox.
|
||||
- Make admin **announcements** clearly distinct from request/content/system/admin notifications, via a separate styled bar.
|
||||
- Dismiss (✕) marks read and advances to the next unread; bars hide when nothing's unread.
|
||||
- Reuse the Phase-2 notifications hooks; inherit per-profile + child filtering and live WebSocket updates for free.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- No server/API changes (the data already distinguishes `category: "announcement"`).
|
||||
- No change to the bell, inbox page, or toast (carrying the 📢 styling there is a possible follow-up, out of scope here).
|
||||
- Not app-wide — Home page only.
|
||||
|
||||
## Components
|
||||
|
||||
Two components in `web/src/components/home/` (or alongside Home), each small and independently testable, plus one mount edit.
|
||||
|
||||
### AnnouncementBar (`web/src/components/AnnouncementBar.tsx`)
|
||||
- Data: `useNotificationsList({ unread: true, category: "announcement" })` → the first (newest) item. Reliably finds the latest unread announcement regardless of how many other notifications are unread.
|
||||
- Render: a distinct bar using the app's accent tokens (reads as a system notice, not a personal row) — a 📢 icon, an "ANNOUNCEMENT" label, the title, truncated body, relative time, and a ✕.
|
||||
- Empty: renders nothing when there's no unread announcement.
|
||||
- ✕: `useMarkRead().mutate({ ids: [id] })` → the unread query updates → the next unread announcement shows, or the bar hides. Marking read also drops the bell badge (shared cache).
|
||||
- Click (anywhere but ✕): navigate to the notification's `link` (or `/notifications` when it has none) and mark it read.
|
||||
|
||||
### NotificationStrip (`web/src/components/NotificationStrip.tsx`)
|
||||
- Data: `useNotificationsList({ unread: true })` → the first item whose `category !== "announcement"` (so announcements live only in the bar above, never duplicated here).
|
||||
- Render: the slim strip — 🔔 icon, title, truncated body, relative time, ✕. Lighter styling than the announcement bar.
|
||||
- Empty: renders nothing when there's no unread non-announcement.
|
||||
- ✕ / click: same behavior as the bar (mark read + advance / navigate + mark read).
|
||||
|
||||
### Mount (`web/src/pages/Home.tsx`)
|
||||
At the top of the Home content region (near the existing `heroSlot` / `TasteSeedBanner`), render `<AnnouncementBar />` then `<NotificationStrip />` as the first elements, so they sit above the content rows. When both are empty, Home renders exactly as today.
|
||||
|
||||
## Data flow & shared behavior
|
||||
|
||||
- Both bars read through TanStack Query; the announcement bar's query (`{unread:true, category:"announcement"}`) and the strip's (`{unread:true}`) are distinct cache keys — two small fetches, both already used elsewhere.
|
||||
- Live updates: `useNotificationsLive` already invalidates the notifications list on `notification.created`, so both bars reactively show the newest unread without a reload.
|
||||
- Filtering: the list endpoint already scopes to the active profile and suppresses child-restricted categories server-side, so the bars inherit correct per-profile behavior.
|
||||
- The relative-time helper is the existing `timeAgo` (with the absolute-date fallback for old timestamps).
|
||||
|
||||
## Error handling
|
||||
|
||||
- While the query is loading, render nothing (no skeleton flash for a slim bar).
|
||||
- A mark-read failure surfaces the existing mutation `onError` toast; the bar stays until the next successful read.
|
||||
- A notification with no `link` navigates to `/notifications` rather than a dead link.
|
||||
|
||||
## Testing
|
||||
|
||||
- `AnnouncementBar.test.tsx`: renders the latest unread announcement with the 📢/label styling; ✕ calls `markRead({ids:[id]})`; clicking navigates (mocked `useNavigate`) to the link and marks read; renders nothing when no unread announcement.
|
||||
- `NotificationStrip.test.tsx`: shows the newest unread non-announcement (skips an announcement at the head of the list); ✕ marks read; click navigates + marks read; renders nothing when no unread non-announcement.
|
||||
- Existing suites stay green; tsc + lint clean.
|
||||
|
||||
## Future work
|
||||
|
||||
- Carry the 📢 announcement styling into the bell dropdown and inbox rows for a consistent identity.
|
||||
- Optional: a per-user "hide the home bars" preference if they prove noisy.
|
||||
@@ -31,25 +31,21 @@ require (
|
||||
github.com/oklog/ulid/v2 v2.1.0
|
||||
github.com/pgvector/pgvector-go v0.3.0
|
||||
github.com/pressly/goose/v3 v3.27.1
|
||||
github.com/sideshow/apns2 v0.25.0
|
||||
github.com/wneessen/go-mail v0.7.3
|
||||
github.com/zishang520/socket.io/v2 v2.5.0
|
||||
go.n16f.net/thumbhash v1.1.0
|
||||
golang.org/x/image v0.41.0
|
||||
golang.org/x/net v0.55.0
|
||||
golang.org/x/oauth2 v0.36.0
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go/compute/metadata v0.9.0 // indirect
|
||||
github.com/PuerkitoBio/goquery v1.8.0 // indirect
|
||||
github.com/andybalholm/brotli v1.2.1 // indirect
|
||||
github.com/andybalholm/cascadia v1.3.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/dunglas/httpsfv v1.1.0 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/gookit/color v1.5.4 // indirect
|
||||
github.com/hashicorp/yamux v0.1.2 // indirect
|
||||
@@ -62,7 +58,6 @@ require (
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/oklog/run v1.1.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.60.0 // indirect
|
||||
github.com/quic-go/webtransport-go v0.10.0 // indirect
|
||||
@@ -76,6 +71,7 @@ require (
|
||||
github.com/zishang520/engine.io/v2 v2.5.0 // indirect
|
||||
github.com/zishang520/socket.io-go-parser/v2 v2.5.0 // indirect
|
||||
github.com/zishang520/webtransport-go v0.9.1 // indirect
|
||||
go.opentelemetry.io/otel/sdk/metric v1.41.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
)
|
||||
|
||||
@@ -104,10 +100,10 @@ require (
|
||||
github.com/sony/sonyflake v1.3.0
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
golang.org/x/sync v0.21.0
|
||||
golang.org/x/sync v0.20.0
|
||||
golang.org/x/text v0.37.0
|
||||
golang.org/x/time v0.15.0
|
||||
google.golang.org/grpc v1.81.1
|
||||
golang.org/x/time v0.14.0
|
||||
google.golang.org/grpc v1.80.0
|
||||
google.golang.org/protobuf v1.36.11
|
||||
)
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdBtwLoEkH9Zs=
|
||||
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
|
||||
entgo.io/ent v0.14.3 h1:wokAV/kIlH9TeklJWGGS7AYJdVckr0DloWjIcO9iIIQ=
|
||||
entgo.io/ent v0.14.3/go.mod h1:aDPE/OziPEu8+OWbzy4UlvWmD2/kbRuWfK2A40hcxJM=
|
||||
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
|
||||
@@ -10,8 +8,6 @@ github.com/Silo-Server/silo-plugin-sdk v0.6.0 h1:Gi9TdH9kt7b8X4xRXH493/nSYb9n0GO
|
||||
github.com/Silo-Server/silo-plugin-sdk v0.6.0/go.mod h1:etqmxLTwjxpFH9goAjBDfNDoqHMv2/sqUXu8yx3hNfA=
|
||||
github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4=
|
||||
github.com/abadojack/whatlanggo v1.0.1/go.mod h1:66WiQbSbJBIlOZMsvbKe5m6pzQovxCH9B/K8tQB2uoc=
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
|
||||
github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro=
|
||||
github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
|
||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||
@@ -51,9 +47,8 @@ github.com/bufbuild/protocompile v0.14.1/go.mod h1:ppVdAIhbr2H8asPk6k4pY7t9zB1OU
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
||||
@@ -76,9 +71,6 @@ github.com/go-pg/pg/v10 v10.11.0 h1:CMKJqLgTrfpE/aOVeLdybezR2om071Vh38OLZjsyMI0=
|
||||
github.com/go-pg/pg/v10 v10.11.0/go.mod h1:4BpHRoxE61y4Onpof3x1a2SQvi9c+q1dJnrNdMjsroA=
|
||||
github.com/go-pg/zerochecker v0.2.0 h1:pp7f72c3DobMWOb2ErtZsnrPaSvHd2W4o9//8HtF4mU=
|
||||
github.com/go-pg/zerochecker v0.2.0/go.mod h1:NJZ4wKL0NmTtz0GKCoJ8kym6Xn/EQzXRl2OnAe7MmDo=
|
||||
github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
@@ -165,9 +157,8 @@ github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNs
|
||||
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
|
||||
github.com/pgvector/pgvector-go v0.3.0 h1:Ij+Yt78R//uYqs3Zk35evZFvr+G0blW0OUN+Q2D1RWc=
|
||||
github.com/pgvector/pgvector-go v0.3.0/go.mod h1:duFy+PXWfW7QQd5ibqutBO4GxLsUZ9RVXhFZGIBsWSA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pressly/goose/v3 v3.27.1 h1:6uEvcprBybDmW4hcz3gYujhARhye+GoWKhEWyzD5sh4=
|
||||
github.com/pressly/goose/v3 v3.27.1/go.mod h1:maruOxsPnIG2yHHyo8UqKWXYKFcH7Q76csUV7+7KYoM=
|
||||
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||
@@ -198,13 +189,10 @@ github.com/santhosh-tekuri/jsonschema/v6 v6.0.2 h1:KRzFb2m7YtdldCEkzs6KqmJw4nqEV
|
||||
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU=
|
||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
github.com/sideshow/apns2 v0.25.0 h1:XOzanncO9MQxkb03T/2uU2KcdVjYiIf0TMLzec0FTW4=
|
||||
github.com/sideshow/apns2 v0.25.0/go.mod h1:7Fceu+sL0XscxrfLSkAoH6UtvKefq3Kq1n4W3ayQZqE=
|
||||
github.com/sony/sonyflake v1.3.0 h1:tiB4Dlp0lnmKp/h6BLXA14P8Qi+LYS9+0QRpcrKHvg4=
|
||||
github.com/sony/sonyflake v1.3.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
@@ -225,6 +213,8 @@ github.com/vmihailenco/tagparser v0.1.2 h1:gnjoVuB/kljJ5wICEEOpx98oXMWPLj22G67Vb
|
||||
github.com/vmihailenco/tagparser v0.1.2/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
github.com/wneessen/go-mail v0.7.3 h1:g3DravXC5SMlVdboFrQA8Jx95A8sOzoBeS5F+vzNRK0=
|
||||
github.com/wneessen/go-mail v0.7.3/go.mod h1:QGhBX0yNbc1J+Mkjcu7z2rpj4B4l+BmDY8gYznPC9sk=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
|
||||
@@ -250,10 +240,10 @@ go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
|
||||
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
|
||||
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
|
||||
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
|
||||
go.opentelemetry.io/otel/sdk v1.41.0 h1:YPIEXKmiAwkGl3Gu1huk1aYWwtpRLeskpV+wPisxBp8=
|
||||
go.opentelemetry.io/otel/sdk v1.41.0/go.mod h1:ahFdU0G5y8IxglBf0QBJXgSe7agzjE4GiTJ6HT9ud90=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.41.0 h1:siZQIYBAUd1rlIWQT2uCxWJxcCO7q3TriaMlf08rXw8=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.41.0/go.mod h1:HNBuSvT7ROaGtGI50ArdRLUnvRTRGniSUZbxiWxSO8Y=
|
||||
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
@@ -266,7 +256,6 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
golang.org/x/crypto v0.0.0-20170512130425-ab89591268e0/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||
@@ -285,7 +274,6 @@ golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220403103023-749bd193bc2b/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
@@ -294,8 +282,6 @@ golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
|
||||
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
|
||||
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
||||
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -303,8 +289,8 @@ golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
|
||||
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -313,7 +299,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -346,8 +331,8 @@ golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
||||
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
@@ -357,19 +342,17 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxb
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa h1:mZHHdPZl0dbGHCflZgAq/Q468DWVFcU2whhB2KAo8fk=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.81.1 h1:VnnIIZ88UzOOKLukQi+ImGz8O1Wdp8nAGGnvOfEIWQQ=
|
||||
google.golang.org/grpc v1.81.1/go.mod h1:xGH9GfzOyMTGIOXBJmXt+BX/V0kcdQbdcuwQ/zNw42I=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529 h1:XF8+t6QQiS0o9ArVan/HW8Q7cycNPGsJf6GA2nXxYAg=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260420184626-e10c466a9529/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM=
|
||||
google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -86,7 +86,9 @@ type AdminHandler struct {
|
||||
accountProvisioner *auth.AccountProvisioner
|
||||
DetailSvc *catalog.DetailService
|
||||
StatsSource AdminStatsSource
|
||||
Config *config.Config
|
||||
EventBus cache.EventBus
|
||||
EventsHub *evt.Hub
|
||||
SettingsRepo ServerSettingsStore
|
||||
JobRepo AdminJobCreator
|
||||
ItemRefreshResolver ItemRefreshScopeResolver
|
||||
@@ -96,6 +98,7 @@ type AdminHandler struct {
|
||||
BootstrapSensitiveValues map[string]string
|
||||
OnUserSessionsRevoked func(ctx context.Context, userID int)
|
||||
OnServerSettingUpdated func(ctx context.Context, key, value string)
|
||||
RestartStatus *ServerRestartStatusTracker
|
||||
}
|
||||
|
||||
// NewAdminHandler creates a new AdminHandler backed by the given
|
||||
@@ -531,15 +534,6 @@ func (h *AdminHandler) HandleUpdateUser(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
|
||||
// Publish a domain event rather than creating the notification inline; the
|
||||
// notifications materializer's system matcher turns it into the user's
|
||||
// "Password changed" notice, keeping all notification creation event-driven.
|
||||
if req.Password != nil && h.RealtimeHub != nil {
|
||||
_ = h.RealtimeHub.EventsHub().PublishJSON(r.Context(),
|
||||
evt.ChannelSessions, notifications.EventUserPasswordChanged, nil,
|
||||
evt.PublishOptions{UserID: id})
|
||||
}
|
||||
|
||||
user, err := h.userRepo.GetByID(r.Context(), id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to fetch updated user")
|
||||
@@ -2158,8 +2152,10 @@ func (h *AdminHandler) HandleUpdateSetting(w http.ResponseWriter, r *http.Reques
|
||||
if h.OnServerSettingUpdated != nil {
|
||||
h.OnServerSettingUpdated(r.Context(), key, req.Value)
|
||||
}
|
||||
|
||||
restartRequired := config.RestartRequired(key)
|
||||
if restartRequired {
|
||||
h.markServerRestartRequired("server_settings")
|
||||
}
|
||||
if sensitiveSettingKeys[key] {
|
||||
writeJSON(w, http.StatusOK, adminSettingResponse{Key: key, RestartRequired: restartRequired})
|
||||
return
|
||||
|
||||
@@ -1,143 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
"github.com/Silo-Server/silo-server/internal/models"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal fake UserRepository for AdminHandler tests.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type fakeUserRepo struct {
|
||||
user *models.User
|
||||
getErr error
|
||||
}
|
||||
|
||||
func (f *fakeUserRepo) List(_ context.Context) ([]*models.User, error) { return nil, nil }
|
||||
|
||||
func (f *fakeUserRepo) Create(_ context.Context, _ models.CreateUserInput) (*models.User, error) {
|
||||
return f.user, nil
|
||||
}
|
||||
|
||||
func (f *fakeUserRepo) Update(_ context.Context, _ int, _ models.UpdateUserInput) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeUserRepo) Delete(_ context.Context, _ int) error { return nil }
|
||||
|
||||
func (f *fakeUserRepo) GetByID(_ context.Context, _ int) (*models.User, error) {
|
||||
return f.user, f.getErr
|
||||
}
|
||||
|
||||
// Compile-time check.
|
||||
var _ UserRepository = (*fakeUserRepo)(nil)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: build AdminHandler with a realtime hub whose events the test can
|
||||
// observe. Password changes publish a domain event; the notifications
|
||||
// materializer (tested separately) turns it into the user's notification.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func newAdminHandlerWithHub(userRepo UserRepository) (*AdminHandler, *evt.Hub) {
|
||||
rh := notifications.NewHub("test", nil)
|
||||
h := NewAdminHandler(userRepo, nil, nil)
|
||||
h.RealtimeHub = rh
|
||||
return h, rh.EventsHub()
|
||||
}
|
||||
|
||||
// adminUpdateRequest builds a PUT /admin/users/{id} HTTP request.
|
||||
func adminUpdateRequest(body []byte, userID string) *http.Request {
|
||||
req := httptest.NewRequest(http.MethodPut, "/admin/users/"+userID, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add("id", userID)
|
||||
return req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleUpdateUser password change notification
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// TestHandleUpdateUser_WithPassword_PublishesEvent asserts that including a
|
||||
// Password in the update payload publishes one user.password_changed event on
|
||||
// ChannelSessions targeted at the updated user.
|
||||
func TestHandleUpdateUser_WithPassword_PublishesEvent(t *testing.T) {
|
||||
targetUser := &models.User{
|
||||
ID: 99,
|
||||
Username: "alice",
|
||||
Email: "alice@example.com",
|
||||
Role: "user",
|
||||
}
|
||||
repo := &fakeUserRepo{user: targetUser}
|
||||
h, eventsHub := newAdminHandlerWithHub(repo)
|
||||
ch, unsub := eventsHub.Subscribe()
|
||||
defer unsub()
|
||||
|
||||
body := []byte(`{"password":"newSecret123"}`)
|
||||
req := adminUpdateRequest(body, "99")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
h.HandleUpdateUser(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
select {
|
||||
case env := <-ch:
|
||||
if env.Channel != evt.ChannelSessions {
|
||||
t.Errorf("channel = %q, want %q", env.Channel, evt.ChannelSessions)
|
||||
}
|
||||
if env.Event != notifications.EventUserPasswordChanged {
|
||||
t.Errorf("event = %q, want %q", env.Event, notifications.EventUserPasswordChanged)
|
||||
}
|
||||
if env.UserID != 99 {
|
||||
t.Errorf("event user_id = %d, want 99", env.UserID)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("expected a user.password_changed event, got none")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleUpdateUser_WithoutPassword_NoEvent asserts that a non-password
|
||||
// update publishes no user.password_changed event.
|
||||
func TestHandleUpdateUser_WithoutPassword_NoEvent(t *testing.T) {
|
||||
targetUser := &models.User{
|
||||
ID: 99,
|
||||
Username: "alice",
|
||||
Email: "alice@example.com",
|
||||
Role: "user",
|
||||
}
|
||||
repo := &fakeUserRepo{user: targetUser}
|
||||
h, eventsHub := newAdminHandlerWithHub(repo)
|
||||
ch, unsub := eventsHub.Subscribe()
|
||||
defer unsub()
|
||||
|
||||
body := []byte(`{"username":"alice-renamed"}`)
|
||||
req := adminUpdateRequest(body, "99")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
h.HandleUpdateUser(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
select {
|
||||
case env := <-ch:
|
||||
t.Errorf("expected no event, got %q on %q", env.Event, env.Channel)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
// no event, as expected
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// AdminServerChannelsHandler exposes admin CRUD for server notification
|
||||
// channels (community broadcast destinations). All routes are mounted inside
|
||||
// the admin-only group.
|
||||
type AdminServerChannelsHandler struct {
|
||||
system *notifications.System
|
||||
}
|
||||
|
||||
// NewAdminServerChannelsHandler creates the handler.
|
||||
func NewAdminServerChannelsHandler(system *notifications.System) *AdminServerChannelsHandler {
|
||||
return &AdminServerChannelsHandler{system: system}
|
||||
}
|
||||
|
||||
func (h *AdminServerChannelsHandler) service() *notifications.ServerChannelService {
|
||||
if h == nil || h.system == nil {
|
||||
return nil
|
||||
}
|
||||
return h.system.ServerChannels
|
||||
}
|
||||
|
||||
// serverChannelResponse is the API view of a server channel. Like webhooks it
|
||||
// never includes the destination URL (Discord webhook tokens are bearer
|
||||
// credentials in the URL path) or the stored signing secret — only url_host.
|
||||
type serverChannelResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
URLHost string `json:"url_host"`
|
||||
Enabled bool `json:"enabled"`
|
||||
NotifyNewMovies bool `json:"notify_new_movies"`
|
||||
NotifyNewEpisodes bool `json:"notify_new_episodes"`
|
||||
NotifyRequestSubmitted bool `json:"notify_request_submitted"`
|
||||
NotifyRequestApproved bool `json:"notify_request_approved"`
|
||||
NotifyRequestDeclined bool `json:"notify_request_declined"`
|
||||
NotifyRequestFulfilled bool `json:"notify_request_fulfilled"`
|
||||
ConsecutiveFailures int `json:"consecutive_failures"`
|
||||
DisabledReason *string `json:"disabled_reason"`
|
||||
LastSuccessAt *time.Time `json:"last_success_at"`
|
||||
LastFailureAt *time.Time `json:"last_failure_at"`
|
||||
LastFailureStatus *int `json:"last_failure_status"`
|
||||
LastFailureMessage *string `json:"last_failure_message"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// SigningSecret is present only in create / rotate-secret responses.
|
||||
SigningSecret string `json:"signing_secret,omitempty"`
|
||||
}
|
||||
|
||||
func serverChannelToResponse(ch notifications.ServerChannel) serverChannelResponse {
|
||||
return serverChannelResponse{
|
||||
ID: ch.ID,
|
||||
Name: ch.Name,
|
||||
Type: ch.Type,
|
||||
URLHost: ch.URLHost,
|
||||
Enabled: ch.Enabled,
|
||||
NotifyNewMovies: ch.NotifyNewMovies,
|
||||
NotifyNewEpisodes: ch.NotifyNewEpisodes,
|
||||
NotifyRequestSubmitted: ch.NotifyRequestSubmitted,
|
||||
NotifyRequestApproved: ch.NotifyRequestApproved,
|
||||
NotifyRequestDeclined: ch.NotifyRequestDeclined,
|
||||
NotifyRequestFulfilled: ch.NotifyRequestFulfilled,
|
||||
ConsecutiveFailures: ch.ConsecutiveFailures,
|
||||
DisabledReason: ch.DisabledReason,
|
||||
LastSuccessAt: ch.LastSuccessAt,
|
||||
LastFailureAt: ch.LastFailureAt,
|
||||
LastFailureStatus: ch.LastFailureStatus,
|
||||
LastFailureMessage: ch.LastFailureMessage,
|
||||
CreatedAt: ch.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type serverChannelRequest struct {
|
||||
Name *string `json:"name"`
|
||||
URL *string `json:"url"`
|
||||
Type *string `json:"type"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
NotifyNewMovies *bool `json:"notify_new_movies"`
|
||||
NotifyNewEpisodes *bool `json:"notify_new_episodes"`
|
||||
NotifyRequestSubmitted *bool `json:"notify_request_submitted"`
|
||||
NotifyRequestApproved *bool `json:"notify_request_approved"`
|
||||
NotifyRequestDeclined *bool `json:"notify_request_declined"`
|
||||
NotifyRequestFulfilled *bool `json:"notify_request_fulfilled"`
|
||||
}
|
||||
|
||||
func (r serverChannelRequest) toInput() notifications.ServerChannelInput {
|
||||
return notifications.ServerChannelInput{
|
||||
Name: r.Name,
|
||||
URL: r.URL,
|
||||
Type: r.Type,
|
||||
Enabled: r.Enabled,
|
||||
NotifyNewMovies: r.NotifyNewMovies,
|
||||
NotifyNewEpisodes: r.NotifyNewEpisodes,
|
||||
NotifyRequestSubmitted: r.NotifyRequestSubmitted,
|
||||
NotifyRequestApproved: r.NotifyRequestApproved,
|
||||
NotifyRequestDeclined: r.NotifyRequestDeclined,
|
||||
NotifyRequestFulfilled: r.NotifyRequestFulfilled,
|
||||
}
|
||||
}
|
||||
|
||||
func writeServerChannelError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, notifications.ErrServerChannelsDisabled):
|
||||
writeError(w, http.StatusForbidden, "server_channels_disabled", "Server channels are disabled")
|
||||
case errors.Is(err, notifications.ErrServerChannelNotFound):
|
||||
writeError(w, http.StatusNotFound, "not_found", "Server channel not found")
|
||||
case errors.Is(err, notifications.ErrServerChannelLimit):
|
||||
writeError(w, http.StatusUnprocessableEntity, "limit_reached", "Server channel limit reached")
|
||||
case errors.Is(err, notifications.ErrServerChannelInvalid):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Server channel operation failed")
|
||||
}
|
||||
}
|
||||
|
||||
// HandleList handles GET /admin/notifications/server-channels.
|
||||
func (h *AdminServerChannelsHandler) HandleList(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
channels, err := service.List(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list server channels")
|
||||
return
|
||||
}
|
||||
responses := make([]serverChannelResponse, 0, len(channels))
|
||||
for _, ch := range channels {
|
||||
responses = append(responses, serverChannelToResponse(ch))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"channels": responses})
|
||||
}
|
||||
|
||||
// HandleCreate handles POST /admin/notifications/server-channels. For generic
|
||||
// channels the response carries the signing secret exactly once.
|
||||
func (h *AdminServerChannelsHandler) HandleCreate(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
var req serverChannelRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
ch, signingSecret, err := service.Create(r.Context(), apimw.GetUserID(r.Context()), req.toInput())
|
||||
if err != nil {
|
||||
writeServerChannelError(w, err)
|
||||
return
|
||||
}
|
||||
response := serverChannelToResponse(*ch)
|
||||
response.SigningSecret = signingSecret
|
||||
writeJSON(w, http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// HandleUpdate handles PUT /admin/notifications/server-channels/{id}.
|
||||
func (h *AdminServerChannelsHandler) HandleUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
var req serverChannelRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
ch, err := service.Update(r.Context(), chi.URLParam(r, "id"), req.toInput())
|
||||
if err != nil {
|
||||
writeServerChannelError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, serverChannelToResponse(*ch))
|
||||
}
|
||||
|
||||
// HandleDelete handles DELETE /admin/notifications/server-channels/{id}.
|
||||
// Idempotent.
|
||||
func (h *AdminServerChannelsHandler) HandleDelete(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
if err := service.Delete(r.Context(), chi.URLParam(r, "id")); err != nil {
|
||||
writeServerChannelError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleRotateSecret handles POST /admin/notifications/server-channels/{id}/rotate-secret.
|
||||
func (h *AdminServerChannelsHandler) HandleRotateSecret(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
signingSecret, err := service.RotateSecret(r.Context(), chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
writeServerChannelError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"signing_secret": signingSecret})
|
||||
}
|
||||
|
||||
// HandleTest handles POST /admin/notifications/server-channels/{id}/test. The
|
||||
// test send is synchronous and never touches the watermark or failure
|
||||
// counters.
|
||||
func (h *AdminServerChannelsHandler) HandleTest(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.service()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Server channels are not available")
|
||||
return
|
||||
}
|
||||
result, err := service.Test(r.Context(), chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
writeServerChannelError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, result)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
)
|
||||
|
||||
func TestWriteServerChannelErrorMapping(t *testing.T) {
|
||||
cases := []struct {
|
||||
err error
|
||||
want int
|
||||
}{
|
||||
{notifications.ErrServerChannelsDisabled, http.StatusForbidden},
|
||||
{notifications.ErrServerChannelNotFound, http.StatusNotFound},
|
||||
{notifications.ErrServerChannelLimit, http.StatusUnprocessableEntity},
|
||||
{fmt.Errorf("%w: name is required", notifications.ErrServerChannelInvalid), http.StatusBadRequest},
|
||||
{fmt.Errorf("boom"), http.StatusInternalServerError},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
rec := httptest.NewRecorder()
|
||||
writeServerChannelError(rec, tc.err)
|
||||
if rec.Code != tc.want {
|
||||
t.Errorf("writeServerChannelError(%v) = %d, want %d", tc.err, rec.Code, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The response shape must never leak the destination URL (a bearer credential
|
||||
// for Discord webhooks) or the stored signing secret; signing_secret appears
|
||||
// only when explicitly set by the create/rotate paths.
|
||||
func TestServerChannelResponseNeverLeaksSecrets(t *testing.T) {
|
||||
secret := "ciphertext-secret"
|
||||
ch := notifications.ServerChannel{
|
||||
ID: "ch-1",
|
||||
Name: "Community",
|
||||
Type: "discord",
|
||||
URLCiphertext: "enc:v1:secret-url-material",
|
||||
URLHost: "discord.com",
|
||||
SigningSecretCiphertext: &secret,
|
||||
Enabled: true,
|
||||
}
|
||||
body, err := json.Marshal(serverChannelToResponse(ch))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded := string(body)
|
||||
if strings.Contains(encoded, "secret-url-material") || strings.Contains(encoded, "ciphertext-secret") {
|
||||
t.Fatalf("response leaked ciphertext material: %s", encoded)
|
||||
}
|
||||
if strings.Contains(encoded, "signing_secret") {
|
||||
t.Fatalf("signing_secret must be omitted unless explicitly set: %s", encoded)
|
||||
}
|
||||
if !strings.Contains(encoded, `"url_host":"discord.com"`) {
|
||||
t.Fatalf("url_host missing from response: %s", encoded)
|
||||
}
|
||||
|
||||
response := serverChannelToResponse(ch)
|
||||
response.SigningSecret = "shown-once"
|
||||
body, err = json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), `"signing_secret":"shown-once"`) {
|
||||
t.Fatalf("explicit signing_secret missing: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// A handler with no notifications system reports 503 instead of panicking.
|
||||
func TestServerChannelHandlersUnavailableWithoutSystem(t *testing.T) {
|
||||
h := NewAdminServerChannelsHandler(nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.HandleList(rec, httptest.NewRequest(http.MethodGet, "/admin/notifications/server-channels", nil))
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("HandleList without system = %d, want 503", rec.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/jellycompat"
|
||||
)
|
||||
|
||||
type adminServerStatusResponse struct {
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
RestartRequired bool `json:"restart_required"`
|
||||
RestartRequiredAt *time.Time `json:"restart_required_at,omitempty"`
|
||||
RestartRequiredReason string `json:"restart_required_reason,omitempty"`
|
||||
RestartRequested bool `json:"restart_requested"`
|
||||
RestartRequestedAt *time.Time `json:"restart_requested_at,omitempty"`
|
||||
}
|
||||
|
||||
// HandleGetServerStatus handles GET /admin/server/status.
|
||||
func (h *AdminHandler) HandleGetServerStatus(w http.ResponseWriter, r *http.Request) {
|
||||
snapshot := h.RestartStatus.Snapshot()
|
||||
resp := adminServerStatusResponse{
|
||||
StartedAt: snapshot.StartedAt,
|
||||
RestartRequired: snapshot.RestartRequired,
|
||||
RestartRequiredAt: snapshot.RestartRequiredAt,
|
||||
RestartRequiredReason: snapshot.RestartRequiredReason,
|
||||
RestartRequested: snapshot.RestartRequested,
|
||||
RestartRequestedAt: snapshot.RestartRequestedAt,
|
||||
}
|
||||
|
||||
if h.SettingsRepo != nil {
|
||||
settings, err := h.SettingsRepo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load settings")
|
||||
return
|
||||
}
|
||||
if jellycompat.WebComponentStatusForConfig(h.Config, settings).RestartRequired {
|
||||
resp.RestartRequired = true
|
||||
if resp.RestartRequiredReason == "" {
|
||||
resp.RestartRequiredReason = "jellyfin_compat"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) markServerRestartRequired(reason string) {
|
||||
if h == nil {
|
||||
return
|
||||
}
|
||||
h.RestartStatus.MarkRequired(reason)
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/config"
|
||||
)
|
||||
|
||||
func TestAdminServerStatusClearsAfterProcessRestart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
restartStatus.MarkRequired("settings")
|
||||
|
||||
handler := &AdminHandler{RestartStatus: restartStatus}
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/server/status", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.HandleGetServerStatus(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp adminServerStatusResponse
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if !resp.RestartRequired {
|
||||
t.Fatal("RestartRequired = false, want true")
|
||||
}
|
||||
|
||||
restartedHandler := &AdminHandler{RestartStatus: NewServerRestartStatusTracker()}
|
||||
rec = httptest.NewRecorder()
|
||||
restartedHandler.HandleGetServerStatus(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status after restart = %d, body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode response after restart: %v", err)
|
||||
}
|
||||
if resp.RestartRequired {
|
||||
t.Fatal("RestartRequired = true after new process tracker, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminServerStatusDoesNotPromoteLiveJellyfinIdentitySettings(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg, err := config.LoadFromDB(map[string]string{
|
||||
"jellyfin_compat.enabled": "true",
|
||||
"jellyfin_compat.listen": ":8096",
|
||||
"jellyfin_compat.public_url": "http://127.0.0.1:8096",
|
||||
"jellyfin_compat.server_name": "Silo",
|
||||
"jellyfin_compat.emulated_server_version": "10.11.0",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
RestartStatus: NewServerRestartStatusTracker(),
|
||||
SettingsRepo: &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.public_url": "https://compat.example.test",
|
||||
"jellyfin_compat.server_name": "Silo Compat",
|
||||
"jellyfin_compat.emulated_server_version": "10.11.6",
|
||||
}},
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/server/status", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleGetServerStatus(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp adminServerStatusResponse
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if resp.RestartRequired {
|
||||
t.Fatal("RestartRequired = true, want false for live Jellyfin identity settings")
|
||||
}
|
||||
}
|
||||
@@ -335,13 +335,15 @@ func TestAdminUpdateSettingReportsRestartRequired(t *testing.T) {
|
||||
// Infrastructure settings are captured at startup.
|
||||
{key: "database.max_connections", value: "40", restartRequired: true},
|
||||
{key: "s3.public_bucket", value: "assets", restartRequired: true},
|
||||
{key: "jellyfin_compat.enabled", value: "false", restartRequired: true},
|
||||
// Branding is read live from the settings repo per request.
|
||||
{key: "branding.server_name", value: "Casa", restartRequired: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.key, func(t *testing.T) {
|
||||
settings := &fakeServerSettingsStore{}
|
||||
handler := &AdminHandler{SettingsRepo: settings}
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
handler := &AdminHandler{SettingsRepo: settings, RestartStatus: restartStatus}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPut,
|
||||
@@ -363,6 +365,13 @@ func TestAdminUpdateSettingReportsRestartRequired(t *testing.T) {
|
||||
if resp.RestartRequired != tc.restartRequired {
|
||||
t.Fatalf("restart_required = %v, want %v", resp.RestartRequired, tc.restartRequired)
|
||||
}
|
||||
snapshot := restartStatus.Snapshot()
|
||||
if snapshot.RestartRequired != tc.restartRequired {
|
||||
t.Fatalf("tracker restart required = %v, want %v", snapshot.RestartRequired, tc.restartRequired)
|
||||
}
|
||||
if !tc.restartRequired && snapshot.RestartRequiredReason != "" {
|
||||
t.Fatalf("tracker reason = %q, want empty", snapshot.RestartRequiredReason)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/branding"
|
||||
)
|
||||
|
||||
// BrandingHandler exposes the public branding read, the public asset serving
|
||||
// endpoint, and the admin asset upload/delete endpoints. All branding logic is
|
||||
// delegated to the branding.Service.
|
||||
type BrandingHandler struct {
|
||||
svc *branding.Service
|
||||
}
|
||||
|
||||
// NewBrandingHandler constructs a BrandingHandler around a branding service.
|
||||
func NewBrandingHandler(svc *branding.Service) *BrandingHandler {
|
||||
return &BrandingHandler{svc: svc}
|
||||
}
|
||||
|
||||
// brandingResponse is returned by GET /theme/branding. It is a superset of the
|
||||
// historical {server_name, login_subtitle} shape — new fields are additive per
|
||||
// the v1 API rules. Asset URLs are stable, cache-bustable paths (empty when no
|
||||
// custom asset is set).
|
||||
type brandingResponse struct {
|
||||
ServerName string `json:"server_name"`
|
||||
LoginSubtitle string `json:"login_subtitle"`
|
||||
AccentColor string `json:"accent_color,omitempty"`
|
||||
DefaultTheme string `json:"default_theme,omitempty"`
|
||||
WordmarkURL string `json:"wordmark_url,omitempty"`
|
||||
MarkURL string `json:"mark_url,omitempty"`
|
||||
FaviconURL string `json:"favicon_url,omitempty"`
|
||||
LoginBgURL string `json:"login_bg_url,omitempty"`
|
||||
}
|
||||
|
||||
// HandleGetBranding returns the server branding configuration. Public endpoint —
|
||||
// no authentication required so branding applies before login (white-label).
|
||||
func (h *BrandingHandler) HandleGetBranding(w http.ResponseWriter, r *http.Request) {
|
||||
snap := h.svc.Load(r.Context())
|
||||
w.Header().Set("Cache-Control", "public, max-age=60")
|
||||
writeJSON(w, http.StatusOK, brandingResponse{
|
||||
ServerName: snap.ServerName,
|
||||
LoginSubtitle: snap.LoginSubtitle,
|
||||
AccentColor: snap.AccentColor,
|
||||
DefaultTheme: snap.DefaultTheme,
|
||||
WordmarkURL: snap.AssetURL(branding.KindWordmark),
|
||||
MarkURL: snap.AssetURL(branding.KindMark),
|
||||
FaviconURL: snap.AssetURL(branding.KindFavicon),
|
||||
LoginBgURL: snap.AssetURL(branding.KindLoginBg),
|
||||
})
|
||||
}
|
||||
|
||||
// HandleServeAsset streams a custom branding asset. Public endpoint — assets are
|
||||
// non-sensitive and must load before login. Assets are content-addressed, so
|
||||
// they are served with a long immutable cache lifetime.
|
||||
func (h *BrandingHandler) HandleServeAsset(w http.ResponseWriter, r *http.Request) {
|
||||
kind := chi.URLParam(r, "kind")
|
||||
if !branding.IsValidKind(kind) {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Unknown branding asset")
|
||||
return
|
||||
}
|
||||
|
||||
data, contentType, ref, err := h.svc.GetAsset(r.Context(), branding.AssetKind(kind))
|
||||
switch {
|
||||
case errors.Is(err, branding.ErrAssetNotConfigured):
|
||||
writeError(w, http.StatusNotFound, "not_found", "No custom asset configured")
|
||||
return
|
||||
case errors.Is(err, branding.ErrStorageUnavailable):
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Asset storage is not configured")
|
||||
return
|
||||
case err != nil:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load asset")
|
||||
return
|
||||
}
|
||||
|
||||
etag := `"` + ref + `"`
|
||||
// Uploaded assets (e.g. SVG favicons) are admin-controlled but served from
|
||||
// the app origin. Prevent content-type sniffing and neutralize scripts in a
|
||||
// directly-navigated SVG (stored-XSS defense).
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Security-Policy", branding.AssetContentSecurityPolicy)
|
||||
if r.Header.Get("If-None-Match") == etag {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("ETag", etag)
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
|
||||
// HandleUploadAsset accepts a multipart image upload for a branding asset kind
|
||||
// and records it. Admin-only (wired behind requireActingAdmin).
|
||||
func (h *BrandingHandler) HandleUploadAsset(w http.ResponseWriter, r *http.Request) {
|
||||
kind := chi.URLParam(r, "kind")
|
||||
if !branding.IsValidKind(kind) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unknown branding asset")
|
||||
return
|
||||
}
|
||||
if !h.svc.HasStorage() {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Asset upload storage (S3) is not configured")
|
||||
return
|
||||
}
|
||||
|
||||
maxBytes := branding.MaxUploadBytes(branding.AssetKind(kind))
|
||||
if err := r.ParseMultipartForm(maxBytes + (1 << 20)); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid multipart form")
|
||||
return
|
||||
}
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Missing file field")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := io.ReadAll(io.LimitReader(file, maxBytes+1))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to read upload")
|
||||
return
|
||||
}
|
||||
if int64(len(data)) > maxBytes {
|
||||
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "Image exceeds the maximum size for this asset")
|
||||
return
|
||||
}
|
||||
|
||||
ref, err := h.svc.UploadAsset(r.Context(), branding.AssetKind(kind), data, header.Header.Get("Content-Type"))
|
||||
switch {
|
||||
case errors.Is(err, branding.ErrUnsupportedImage):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unsupported image type; use PNG, JPEG, WebP (or PNG/ICO/SVG for favicon)")
|
||||
return
|
||||
case errors.Is(err, branding.ErrStorageUnavailable):
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Asset upload storage (S3) is not configured")
|
||||
return
|
||||
case errors.Is(err, branding.ErrInvalidKind):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unknown branding asset")
|
||||
return
|
||||
case err != nil:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to store asset")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]string{
|
||||
"kind": kind,
|
||||
"ref": ref,
|
||||
"url": branding.AssetURLFor(branding.AssetKind(kind), ref),
|
||||
})
|
||||
}
|
||||
|
||||
// HandleDeleteAsset clears a custom branding asset. Admin-only.
|
||||
func (h *BrandingHandler) HandleDeleteAsset(w http.ResponseWriter, r *http.Request) {
|
||||
kind := chi.URLParam(r, "kind")
|
||||
if !branding.IsValidKind(kind) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unknown branding asset")
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteAsset(r.Context(), branding.AssetKind(kind)); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to remove asset")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"time"
|
||||
|
||||
silomail "github.com/Silo-Server/silo-server/internal/mail"
|
||||
)
|
||||
|
||||
// EmailHandler exposes admin operations for the shared outbound email
|
||||
// facility (internal/mail). Feature-specific email content lives with the
|
||||
// features; this handler only owns configuration verification.
|
||||
type EmailHandler struct {
|
||||
sender silomail.Sender
|
||||
}
|
||||
|
||||
// NewEmailHandler creates an EmailHandler.
|
||||
func NewEmailHandler(sender silomail.Sender) *EmailHandler {
|
||||
return &EmailHandler{sender: sender}
|
||||
}
|
||||
|
||||
type emailTestRequest struct {
|
||||
To string `json:"to"`
|
||||
}
|
||||
|
||||
type emailTestResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// HandleTest handles POST /admin/email/test: synchronously sends a test
|
||||
// message so admins can verify SMTP settings before any feature depends on
|
||||
// them.
|
||||
func (h *EmailHandler) HandleTest(w http.ResponseWriter, r *http.Request) {
|
||||
if h == nil || h.sender == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Email is not available")
|
||||
return
|
||||
}
|
||||
var req emailTestRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
if _, err := mail.ParseAddress(req.To); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "A valid recipient address is required")
|
||||
return
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
err := h.sender.Send(r.Context(), silomail.Message{
|
||||
To: []string{req.To},
|
||||
Subject: "Silo test email",
|
||||
TextBody: "This is a test email from your Silo server.\n\n" +
|
||||
"If you received it, outbound email is configured correctly.",
|
||||
HTMLBody: silomail.RenderLayout(silomail.LayoutOptions{
|
||||
Preheader: "Outbound email from your Silo server is configured correctly.",
|
||||
Title: "Outbound email is working",
|
||||
BodyHTML: silomail.EmailParagraph("This is a test email from your Silo server.") +
|
||||
silomail.EmailParagraph("If you're reading it, the SMTP settings are correct and "+
|
||||
"notification emails will look like this one."),
|
||||
}),
|
||||
})
|
||||
response := emailTestResponse{
|
||||
OK: err == nil,
|
||||
DurationMS: time.Since(started).Milliseconds(),
|
||||
}
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, silomail.ErrNotConfigured):
|
||||
response.Message = "Email is not configured. Set the SMTP host, from address, and enable email first."
|
||||
default:
|
||||
response.Message = err.Error()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, response)
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
"github.com/Silo-Server/silo-server/internal/historyimport"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/Silo-Server/silo-server/internal/presence"
|
||||
"github.com/Silo-Server/silo-server/internal/scanqueue"
|
||||
"github.com/Silo-Server/silo-server/internal/taskmanager"
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -33,11 +32,6 @@ type activeScanLister interface {
|
||||
ListActive(ctx context.Context) ([]evt.ScanRun, error)
|
||||
}
|
||||
|
||||
// notificationUnreadCounter is the subset of notifications.Service used by EventsHandler.
|
||||
type notificationUnreadCounter interface {
|
||||
UnreadCount(ctx context.Context, userID int, profileID string, childSafe bool) (int, error)
|
||||
}
|
||||
|
||||
type EventsHandler struct {
|
||||
hub *evt.Hub
|
||||
jobs *AdminJobsHandler
|
||||
@@ -46,8 +40,15 @@ type EventsHandler struct {
|
||||
scans *evt.ScanRegistry
|
||||
persistedScans activeScanLister
|
||||
historyImports historyImportActiveLister
|
||||
notifications notificationUnreadCounter
|
||||
presence presence.Registry
|
||||
notifications *notifications.System
|
||||
}
|
||||
|
||||
// SetNotificationsSystem wires the user-notification system: websocket
|
||||
// handshake tickets and the notifications channel snapshot.
|
||||
func (h *EventsHandler) SetNotificationsSystem(system *notifications.System) {
|
||||
if h != nil {
|
||||
h.notifications = system
|
||||
}
|
||||
}
|
||||
|
||||
func NewEventsHandler(
|
||||
@@ -58,8 +59,6 @@ func NewEventsHandler(
|
||||
scans *evt.ScanRegistry,
|
||||
persistedScans *scanqueue.Service,
|
||||
historyImports historyImportActiveLister,
|
||||
notificationsSvc *notifications.Service,
|
||||
presenceReg presence.Registry,
|
||||
) *EventsHandler {
|
||||
return &EventsHandler{
|
||||
hub: hub,
|
||||
@@ -69,8 +68,6 @@ func NewEventsHandler(
|
||||
scans: scans,
|
||||
persistedScans: persistedScans,
|
||||
historyImports: historyImports,
|
||||
notifications: notificationsSvc,
|
||||
presence: presenceReg,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +83,27 @@ func (h *EventsHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
// Browsers cannot set custom headers on websocket handshakes, so profile
|
||||
// identity arrives as a short-lived single-use ticket minted via
|
||||
// POST /events/ws-ticket. A connection without a ticket stays unbound and
|
||||
// simply cannot subscribe to the profile-scoped notifications channel.
|
||||
boundProfileID := ""
|
||||
if ticket := r.URL.Query().Get("ticket"); ticket != "" && h.notifications != nil {
|
||||
ticketUserID, ticketProfileID, ok := h.notifications.Tickets.Consume(r.Context(), ticket)
|
||||
if ok && ticketUserID == claims.UserID {
|
||||
boundProfileID = ticketProfileID
|
||||
} else {
|
||||
// Expired, reused, consumed on a different node, or minted for
|
||||
// another user: degrade to an unbound connection instead of
|
||||
// failing the handshake. The binding grants nothing on its own —
|
||||
// the client retries it when its notifications subscription is
|
||||
// rejected — whereas a hard 403 would take down every realtime
|
||||
// channel over a notifications-only concern.
|
||||
slog.Warn("events: websocket ticket rejected; connection unbound",
|
||||
"user_id", claims.UserID)
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -98,16 +116,7 @@ func (h *EventsHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
eventsCh, unsubscribe := h.hub.Subscribe()
|
||||
defer unsubscribe()
|
||||
|
||||
if h.presence != nil {
|
||||
release := h.presence.Add(ctx, claims.UserID)
|
||||
defer release()
|
||||
}
|
||||
|
||||
startWebSocketPingLoop(ctx, func() error {
|
||||
if h.presence != nil {
|
||||
h.presence.Refresh(ctx, claims.UserID)
|
||||
}
|
||||
return writeWebSocketControl(conn, websocket.PingMessage, nil)
|
||||
})
|
||||
|
||||
@@ -164,7 +173,7 @@ func (h *EventsHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request)
|
||||
)
|
||||
return
|
||||
case data := <-readMessages:
|
||||
nextSubs, handled, ok := h.handleEventsClientMessage(conn, r, claims, data, allowedChannels)
|
||||
nextSubs, handled, ok := h.handleEventsClientMessage(conn, r, claims, boundProfileID, data, allowedChannels)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -186,10 +195,10 @@ func (h *EventsHandler) HandleWebSocket(w http.ResponseWriter, r *http.Request)
|
||||
if _, subscribed := subscriptions[env.Channel]; !subscribed {
|
||||
continue
|
||||
}
|
||||
if !allowsEventForClaims(claims, env) {
|
||||
if !allowsEventForClaims(claims, boundProfileID, env) {
|
||||
continue
|
||||
}
|
||||
if err := h.writeEventFrame(conn, r, claims, env); err != nil {
|
||||
if err := h.writeEventFrame(conn, r, claims, boundProfileID, env); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -200,6 +209,7 @@ func (h *EventsHandler) handleEventsClientMessage(
|
||||
conn *websocket.Conn,
|
||||
r *http.Request,
|
||||
claims *auth.Claims,
|
||||
boundProfileID string,
|
||||
data []byte,
|
||||
allowed []evt.EventChannel,
|
||||
) (map[evt.EventChannel]struct{}, bool, bool) {
|
||||
@@ -267,6 +277,16 @@ func (h *EventsHandler) handleEventsClientMessage(
|
||||
})
|
||||
continue
|
||||
}
|
||||
// The notifications channel is profile-scoped: it requires a
|
||||
// connection bound to a profile via a websocket ticket.
|
||||
if channel == evt.ChannelNotifications && boundProfileID == "" {
|
||||
rejected = append(rejected, evt.EventsRejectedChannel{
|
||||
Channel: channel,
|
||||
Code: "profile_required",
|
||||
Message: "A profile-bound websocket ticket is required",
|
||||
})
|
||||
continue
|
||||
}
|
||||
if _, seen := nextSubs[channel]; seen {
|
||||
continue
|
||||
}
|
||||
@@ -284,7 +304,7 @@ func (h *EventsHandler) handleEventsClientMessage(
|
||||
}
|
||||
|
||||
for _, channel := range accepted {
|
||||
if err := h.writeSnapshotFrame(conn, r, claims, channel); err != nil {
|
||||
if err := h.writeSnapshotFrame(conn, r, claims, boundProfileID, channel); err != nil {
|
||||
return nil, false, false
|
||||
}
|
||||
}
|
||||
@@ -296,9 +316,8 @@ func allowedChannelsForRole(role string) []evt.EventChannel {
|
||||
channels := []evt.EventChannel{
|
||||
evt.ChannelCatalog,
|
||||
evt.ChannelHistoryImport,
|
||||
evt.ChannelNotifications,
|
||||
evt.ChannelRequests,
|
||||
evt.ChannelUserState,
|
||||
evt.ChannelNotifications,
|
||||
}
|
||||
if role == "admin" {
|
||||
channels = append(channels,
|
||||
@@ -306,36 +325,32 @@ func allowedChannelsForRole(role string) []evt.EventChannel {
|
||||
evt.ChannelSessions,
|
||||
evt.ChannelTasks,
|
||||
evt.ChannelScans,
|
||||
evt.ChannelSettings,
|
||||
)
|
||||
}
|
||||
return channels
|
||||
}
|
||||
|
||||
func allowsEventForClaims(claims *auth.Claims, env evt.Envelope) bool {
|
||||
func allowsEventForClaims(claims *auth.Claims, boundProfileID string, env evt.Envelope) bool {
|
||||
if claims == nil {
|
||||
return false
|
||||
}
|
||||
if env.AdminOnly && claims.Role != "admin" {
|
||||
return false
|
||||
}
|
||||
if env.UserID > 0 && env.UserID != claims.UserID {
|
||||
// The notifications and requests channels carry per-user private content
|
||||
// (inbox titles/bodies, requested media names). Nobody — including
|
||||
// admins — may receive another user's events on them. Other channels
|
||||
// keep the admin cross-user monitoring bypass.
|
||||
if claims.Role != "admin" || isPersonalChannel(env.Channel) {
|
||||
return false
|
||||
}
|
||||
if env.Channel == evt.ChannelNotifications {
|
||||
// Notifications are personal: even admins only receive their own
|
||||
// profile's deliveries, and only on a profile-bound connection.
|
||||
return boundProfileID != "" &&
|
||||
env.UserID == claims.UserID &&
|
||||
env.ProfileID == boundProfileID
|
||||
}
|
||||
if env.UserID > 0 && claims.Role != "admin" && env.UserID != claims.UserID {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// isPersonalChannel reports whether a channel carries per-user private content
|
||||
// that must never be delivered across user boundaries, even to admins.
|
||||
func isPersonalChannel(c evt.EventChannel) bool {
|
||||
return c == evt.ChannelNotifications || c == evt.ChannelRequests
|
||||
}
|
||||
|
||||
func marshalJSON(value any) json.RawMessage {
|
||||
if value == nil {
|
||||
return json.RawMessage("null")
|
||||
@@ -350,33 +365,24 @@ func marshalJSON(value any) json.RawMessage {
|
||||
func (h *EventsHandler) snapshotForChannel(
|
||||
r *http.Request,
|
||||
claims *auth.Claims,
|
||||
boundProfileID string,
|
||||
channel evt.EventChannel,
|
||||
) (json.RawMessage, error) {
|
||||
switch channel {
|
||||
case evt.ChannelCatalog, evt.ChannelUserState:
|
||||
return json.RawMessage("null"), nil
|
||||
case evt.ChannelNotifications:
|
||||
if h == nil || h.notifications == nil {
|
||||
return json.RawMessage(`{"unread_count":0}`), nil
|
||||
// Recent unread deliveries for the bound profile so reconnecting
|
||||
// clients hydrate without a separate REST call. Same row shape as the
|
||||
// inbox list API.
|
||||
if h == nil || h.notifications == nil || boundProfileID == "" {
|
||||
return json.RawMessage("[]"), nil
|
||||
}
|
||||
// Profile ID is read directly from the WS upgrade request header because
|
||||
// the events/ws route does not go through the RequireProfile middleware.
|
||||
// childSafe is intentionally false here: the EventsHandler has no access
|
||||
// to a profile store, so the WS snapshot is a conservative approximation.
|
||||
// The REST GET /notifications/unread-count endpoint is authoritative for
|
||||
// child-safe profile filtering.
|
||||
profileID := r.Header.Get("X-Profile-Id")
|
||||
count, err := h.notifications.UnreadCount(r.Context(), claims.UserID, profileID, false)
|
||||
rows, err := h.notifications.Deliveries.RecentUnread(r.Context(), boundProfileID, 25)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return marshalJSON(struct {
|
||||
UnreadCount int `json:"unread_count"`
|
||||
}{UnreadCount: count}), nil
|
||||
case evt.ChannelRequests:
|
||||
// Request state is fetched via REST; the WS channel only carries
|
||||
// lifecycle events. Return an empty object as the initial snapshot.
|
||||
return json.RawMessage(`{}`), nil
|
||||
return marshalJSON(h.notifications.PayloadsForRows(r.Context(), rows)), nil
|
||||
case evt.ChannelJobs:
|
||||
if h == nil || h.jobs == nil || h.jobs.repo == nil {
|
||||
return json.RawMessage("[]"), nil
|
||||
@@ -445,9 +451,10 @@ func (h *EventsHandler) writeSnapshotFrame(
|
||||
conn *websocket.Conn,
|
||||
r *http.Request,
|
||||
claims *auth.Claims,
|
||||
boundProfileID string,
|
||||
channel evt.EventChannel,
|
||||
) error {
|
||||
data, err := h.snapshotForChannel(r, claims, channel)
|
||||
data, err := h.snapshotForChannel(r, claims, boundProfileID, channel)
|
||||
if err != nil {
|
||||
slog.Error(
|
||||
"events: failed to build initial snapshot",
|
||||
@@ -473,14 +480,19 @@ func (h *EventsHandler) writeEventFrame(
|
||||
conn *websocket.Conn,
|
||||
r *http.Request,
|
||||
claims *auth.Claims,
|
||||
boundProfileID string,
|
||||
env evt.Envelope,
|
||||
) error {
|
||||
data := env.Data
|
||||
if len(data) == 0 || (env.Channel == evt.ChannelSessions && env.Event == "sessions.replaced") {
|
||||
snapshot, err := h.snapshotForChannel(r, claims, env.Channel)
|
||||
snapshot, err := h.snapshotForChannel(r, claims, boundProfileID, env.Channel)
|
||||
if err != nil {
|
||||
// Drop the frame but keep the stream open (same contract as
|
||||
// writeSnapshotFrame): durable state covers the gap on the next
|
||||
// event or reconnect, while closing the socket tears down every
|
||||
// channel the client subscribed to.
|
||||
slog.Error("events: failed to build event payload", "channel", env.Channel, "event", env.Event, "error", err)
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
data = snapshot
|
||||
}
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fake notificationUnreadCounter for snapshot tests.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type fakeUnreadCounter struct {
|
||||
count int
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeUnreadCounter) UnreadCount(_ context.Context, _ int, _ string, _ bool) (int, error) {
|
||||
return f.count, f.err
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// allowedChannelsForRole tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestAllowedChannelsForRole_UserContainsNotificationsAndRequests(t *testing.T) {
|
||||
channels := allowedChannelsForRole("user")
|
||||
if !slices.Contains(channels, evt.ChannelNotifications) {
|
||||
t.Errorf("allowedChannelsForRole(user) missing ChannelNotifications; got %v", channels)
|
||||
}
|
||||
if !slices.Contains(channels, evt.ChannelRequests) {
|
||||
t.Errorf("allowedChannelsForRole(user) missing ChannelRequests; got %v", channels)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowedChannelsForRole_AdminContainsAllUserChannelsPlusExtras(t *testing.T) {
|
||||
userChannels := allowedChannelsForRole("user")
|
||||
adminChannels := allowedChannelsForRole("admin")
|
||||
|
||||
for _, ch := range userChannels {
|
||||
if !slices.Contains(adminChannels, ch) {
|
||||
t.Errorf("allowedChannelsForRole(admin) missing user channel %q", ch)
|
||||
}
|
||||
}
|
||||
|
||||
adminExtras := []evt.EventChannel{
|
||||
evt.ChannelJobs,
|
||||
evt.ChannelSessions,
|
||||
evt.ChannelTasks,
|
||||
evt.ChannelScans,
|
||||
}
|
||||
for _, ch := range adminExtras {
|
||||
if !slices.Contains(adminChannels, ch) {
|
||||
t.Errorf("allowedChannelsForRole(admin) missing admin-only channel %q", ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// snapshotForChannel tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestSnapshotForChannel_NotificationsNilService(t *testing.T) {
|
||||
h := &EventsHandler{notifications: nil}
|
||||
r := httptest.NewRequest("GET", "/events/ws", nil)
|
||||
claims := &auth.Claims{UserID: 42, Role: "user"}
|
||||
|
||||
data, err := h.snapshotForChannel(r, claims, evt.ChannelNotifications)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
var got struct {
|
||||
UnreadCount int `json:"unread_count"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v, raw=%s", err, data)
|
||||
}
|
||||
if got.UnreadCount != 0 {
|
||||
t.Errorf("nil service: want unread_count=0, got %d", got.UnreadCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotForChannel_NotificationsWithService(t *testing.T) {
|
||||
h := &EventsHandler{notifications: &fakeUnreadCounter{count: 7}}
|
||||
r := httptest.NewRequest("GET", "/events/ws", nil)
|
||||
r.Header.Set("X-Profile-Id", "profile-abc")
|
||||
claims := &auth.Claims{UserID: 5, Role: "user"}
|
||||
|
||||
data, err := h.snapshotForChannel(r, claims, evt.ChannelNotifications)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
var got struct {
|
||||
UnreadCount int `json:"unread_count"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v, raw=%s", err, data)
|
||||
}
|
||||
if got.UnreadCount != 7 {
|
||||
t.Errorf("want unread_count=7, got %d", got.UnreadCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotForChannel_RequestsReturnsEmptyObject(t *testing.T) {
|
||||
h := &EventsHandler{}
|
||||
r := httptest.NewRequest("GET", "/events/ws", nil)
|
||||
claims := &auth.Claims{UserID: 1, Role: "user"}
|
||||
|
||||
data, err := h.snapshotForChannel(r, claims, evt.ChannelRequests)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatalf("unmarshal: %v, raw=%s", err, data)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("ChannelRequests snapshot should be empty object, got %v", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/cache"
|
||||
"github.com/Silo-Server/silo-server/internal/config"
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
"github.com/Silo-Server/silo-server/internal/jellycompat"
|
||||
)
|
||||
|
||||
const jellyfinCompatWebOperationUpdatedEvent = "jellyfin_compat.web_operation.updated"
|
||||
|
||||
type jellyfinCompatSettingsRequest struct {
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
PublicURL *string `json:"public_url,omitempty"`
|
||||
ServerName *string `json:"server_name,omitempty"`
|
||||
EmulatedServerVersion *string `json:"emulated_server_version,omitempty"`
|
||||
WebEnabled *bool `json:"web_enabled,omitempty"`
|
||||
WebVersion *string `json:"web_version,omitempty"`
|
||||
WebDir *string `json:"web_dir,omitempty"`
|
||||
WebInstallDir *string `json:"web_install_dir,omitempty"`
|
||||
}
|
||||
|
||||
type jellyfinCompatWebInstallRequest struct {
|
||||
Version string `json:"version,omitempty"`
|
||||
SourceURL string `json:"source_url,omitempty"`
|
||||
}
|
||||
|
||||
// HandleGetJellyfinCompatStatus handles GET /admin/jellyfin-compat/status.
|
||||
func (h *AdminHandler) HandleGetJellyfinCompatStatus(w http.ResponseWriter, r *http.Request) {
|
||||
settings, ok := h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, jellycompat.WebComponentStatusForConfig(h.Config, settings))
|
||||
}
|
||||
|
||||
// HandleUpdateJellyfinCompatSettings handles PATCH /admin/jellyfin-compat/settings.
|
||||
func (h *AdminHandler) HandleUpdateJellyfinCompatSettings(w http.ResponseWriter, r *http.Request) {
|
||||
if h.SettingsRepo == nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Settings store not configured")
|
||||
return
|
||||
}
|
||||
var req jellyfinCompatSettingsRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
updates := map[string]string{}
|
||||
if req.Enabled != nil {
|
||||
if *req.Enabled {
|
||||
updates["jellyfin_compat.enabled"] = "true"
|
||||
} else {
|
||||
updates["jellyfin_compat.enabled"] = "false"
|
||||
}
|
||||
}
|
||||
setOptionalString(updates, "jellyfin_compat.public_url", req.PublicURL)
|
||||
setOptionalString(updates, "jellyfin_compat.server_name", req.ServerName)
|
||||
setOptionalString(updates, "jellyfin_compat.emulated_server_version", req.EmulatedServerVersion)
|
||||
if req.WebEnabled != nil {
|
||||
updates["jellyfin_compat.web_enabled"] = strconv.FormatBool(*req.WebEnabled)
|
||||
}
|
||||
if req.Enabled != nil && !*req.Enabled {
|
||||
updates["jellyfin_compat.web_enabled"] = "false"
|
||||
}
|
||||
setOptionalString(updates, "jellyfin_compat.web_version", req.WebVersion)
|
||||
setOptionalString(updates, "jellyfin_compat.web_install_dir", req.WebInstallDir)
|
||||
if req.WebDir != nil {
|
||||
root := strings.TrimSpace(updates["jellyfin_compat.web_install_dir"])
|
||||
if root == "" {
|
||||
settings, ok := h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
root = strings.TrimSpace(settings["jellyfin_compat.web_install_dir"])
|
||||
}
|
||||
if root == "" {
|
||||
root = config.DefaultJellyfinWebInstallDir
|
||||
}
|
||||
managedPath := jellycompat.ManagedWebInstallPath(root)
|
||||
if raw := strings.TrimSpace(*req.WebDir); raw != "" && filepath.Clean(raw) != filepath.Clean(managedPath) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Jellyfin Web active directory is managed by Silo and cannot point at an arbitrary path")
|
||||
return
|
||||
}
|
||||
updates["jellyfin_compat.web_dir"] = managedPath
|
||||
}
|
||||
if len(updates) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "At least one setting is required")
|
||||
return
|
||||
}
|
||||
for key, value := range updates {
|
||||
if err := h.SettingsRepo.Set(r.Context(), key, value); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to update setting")
|
||||
return
|
||||
}
|
||||
h.publishSettingChanged(r, key, value)
|
||||
}
|
||||
if jellyfinCompatSettingsRequireRestart(updates) {
|
||||
h.markServerRestartRequired("jellyfin_compat")
|
||||
}
|
||||
settings, ok := h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, jellycompat.WebComponentStatusForConfig(h.Config, settings))
|
||||
}
|
||||
|
||||
func jellyfinCompatSettingsRequireRestart(updates map[string]string) bool {
|
||||
for key := range updates {
|
||||
if config.RestartRequired(key) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HandleInstallJellyfinCompatWeb handles POST /admin/jellyfin-compat/web/install.
|
||||
func (h *AdminHandler) HandleInstallJellyfinCompatWeb(w http.ResponseWriter, r *http.Request) {
|
||||
h.installJellyfinCompatWeb(w, r)
|
||||
}
|
||||
|
||||
// HandleUpdateJellyfinCompatWeb handles POST /admin/jellyfin-compat/web/update.
|
||||
func (h *AdminHandler) HandleUpdateJellyfinCompatWeb(w http.ResponseWriter, r *http.Request) {
|
||||
h.installJellyfinCompatWeb(w, r)
|
||||
}
|
||||
|
||||
// HandleRemoveJellyfinCompatWeb handles POST /admin/jellyfin-compat/web/remove.
|
||||
func (h *AdminHandler) HandleRemoveJellyfinCompatWeb(w http.ResponseWriter, r *http.Request) {
|
||||
settings, ok := h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
root := strings.TrimSpace(settings["jellyfin_compat.web_install_dir"])
|
||||
if root == "" {
|
||||
root = config.DefaultJellyfinWebInstallDir
|
||||
}
|
||||
_, err := jellycompat.StartWebComponentRemove(jellycompat.WebComponentRemoveOptions{
|
||||
InstallRoot: root,
|
||||
OnProgress: h.publishJellyfinCompatWebOperationProgress,
|
||||
})
|
||||
if err != nil {
|
||||
writeJellyfinCompatOperationError(w, err)
|
||||
return
|
||||
}
|
||||
if err := h.persistJellyfinCompatWebEnabled(r.Context(), false); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to update setting")
|
||||
return
|
||||
}
|
||||
settings, ok = h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, jellycompat.WebComponentStatusForConfig(h.Config, settings))
|
||||
}
|
||||
|
||||
func (h *AdminHandler) installJellyfinCompatWeb(w http.ResponseWriter, r *http.Request) {
|
||||
settings, ok := h.jellyfinCompatSettings(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req jellyfinCompatWebInstallRequest
|
||||
if r.Body != nil {
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
}
|
||||
root := strings.TrimSpace(settings["jellyfin_compat.web_install_dir"])
|
||||
if root == "" {
|
||||
root = config.DefaultJellyfinWebInstallDir
|
||||
}
|
||||
sourceURL := strings.TrimSpace(req.SourceURL)
|
||||
if sourceURL == "" {
|
||||
sourceURL = strings.TrimSpace(settings["jellyfin_compat.web_source_url"])
|
||||
}
|
||||
version := strings.TrimSpace(req.Version)
|
||||
if version == "" {
|
||||
version = strings.TrimSpace(settings["jellyfin_compat.web_version"])
|
||||
}
|
||||
if version == "" {
|
||||
emulatedVersion := strings.TrimSpace(settings["jellyfin_compat.emulated_server_version"])
|
||||
if emulatedVersion == "" && h.Config != nil {
|
||||
emulatedVersion = h.Config.JellyfinCompat.EmulatedServerVersion
|
||||
}
|
||||
if emulatedVersion == "" {
|
||||
emulatedVersion = config.DefaultJellyfinCompatEmulatedServerVersion
|
||||
}
|
||||
resolvedVersion, err := jellycompat.ResolveCompatibleWebVersion(r.Context(), sourceURL, emulatedVersion)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "web_version_unavailable", err.Error())
|
||||
return
|
||||
}
|
||||
version = resolvedVersion
|
||||
}
|
||||
status, err := jellycompat.StartWebComponentInstall(jellycompat.WebComponentInstallOptions{
|
||||
InstallRoot: root,
|
||||
SourceURL: sourceURL,
|
||||
Version: version,
|
||||
OnProgress: h.publishJellyfinCompatWebOperationProgress,
|
||||
}, func(ctx context.Context, status jellycompat.WebComponentStatus) error {
|
||||
return h.persistJellyfinCompatWebInstallSettings(ctx, status)
|
||||
})
|
||||
if err != nil {
|
||||
writeJellyfinCompatOperationError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, status)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) persistJellyfinCompatWebInstallSettings(ctx context.Context, status jellycompat.WebComponentStatus) error {
|
||||
if h.SettingsRepo == nil {
|
||||
return nil
|
||||
}
|
||||
updates := map[string]string{
|
||||
"jellyfin_compat.web_enabled": "true",
|
||||
"jellyfin_compat.web_install_dir": status.InstallRoot,
|
||||
"jellyfin_compat.web_dir": jellycompat.ManagedWebInstallPath(status.InstallRoot),
|
||||
"jellyfin_compat.web_version": status.PinnedVersion,
|
||||
}
|
||||
if status.SourceURL != "" {
|
||||
updates["jellyfin_compat.web_source_url"] = status.SourceURL
|
||||
}
|
||||
for key, value := range updates {
|
||||
if err := h.SettingsRepo.Set(ctx, key, value); err != nil {
|
||||
return fmt.Errorf("persist Jellyfin Web setting %s: %w", key, err)
|
||||
}
|
||||
h.publishSettingChangedContext(ctx, key, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *AdminHandler) persistJellyfinCompatWebEnabled(ctx context.Context, enabled bool) error {
|
||||
if h.SettingsRepo == nil {
|
||||
return nil
|
||||
}
|
||||
value := strconv.FormatBool(enabled)
|
||||
if err := h.SettingsRepo.Set(ctx, "jellyfin_compat.web_enabled", value); err != nil {
|
||||
return fmt.Errorf("persist Jellyfin Web setting jellyfin_compat.web_enabled: %w", err)
|
||||
}
|
||||
h.publishSettingChangedContext(ctx, "jellyfin_compat.web_enabled", value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *AdminHandler) publishJellyfinCompatWebOperationProgress(op jellycompat.WebComponentOperationStatus) {
|
||||
if h == nil || h.EventsHub == nil {
|
||||
return
|
||||
}
|
||||
_ = h.EventsHub.PublishJSON(
|
||||
context.Background(),
|
||||
evt.ChannelSettings,
|
||||
jellyfinCompatWebOperationUpdatedEvent,
|
||||
op,
|
||||
evt.PublishOptions{AdminOnly: true},
|
||||
)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) jellyfinCompatSettings(w http.ResponseWriter, r *http.Request) (map[string]string, bool) {
|
||||
if h.SettingsRepo == nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Settings store not configured")
|
||||
return nil, false
|
||||
}
|
||||
settings, err := h.SettingsRepo.GetAll(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load settings")
|
||||
return nil, false
|
||||
}
|
||||
return settings, true
|
||||
}
|
||||
|
||||
func setOptionalString(updates map[string]string, key string, value *string) {
|
||||
if value == nil {
|
||||
return
|
||||
}
|
||||
updates[key] = strings.TrimSpace(*value)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) publishSettingChanged(r *http.Request, key, value string) {
|
||||
h.publishSettingChangedContext(r.Context(), key, value)
|
||||
}
|
||||
|
||||
func (h *AdminHandler) publishSettingChangedContext(ctx context.Context, key, value string) {
|
||||
if h.EventBus != nil {
|
||||
_ = h.EventBus.Publish(ctx, cache.ChannelAdmin,
|
||||
cache.Event{Type: cache.EventSettingsChanged, Payload: key})
|
||||
}
|
||||
if h.OnServerSettingUpdated != nil {
|
||||
h.OnServerSettingUpdated(ctx, key, value)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJellyfinCompatOperationError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, jellycompat.ErrWebComponentOperationActive):
|
||||
writeError(w, http.StatusConflict, "operation_active", "A Jellyfin Web operation is already running")
|
||||
case errors.Is(err, jellycompat.ErrWebInstallerUnavailable):
|
||||
writeError(w, http.StatusServiceUnavailable, "installer_unavailable", err.Error())
|
||||
default:
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,339 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/config"
|
||||
"github.com/Silo-Server/silo-server/internal/jellycompat"
|
||||
)
|
||||
|
||||
func TestUpdateJellyfinCompatSettingsRejectsArbitraryWebDir(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.web_install_dir": "/var/lib/silo/compat/jellyfin-web",
|
||||
}},
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPatch,
|
||||
"/admin/jellyfin-compat/settings",
|
||||
strings.NewReader(`{"web_dir":"/etc"}`),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleUpdateJellyfinCompatSettings(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "cannot point at an arbitrary path") {
|
||||
t.Fatalf("unexpected response body %q", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateJellyfinCompatSettingsUpdatesWebEnabled(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.web_install_dir": "/var/lib/silo/compat/jellyfin-web",
|
||||
}}
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: settings,
|
||||
RestartStatus: restartStatus,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPatch,
|
||||
"/admin/jellyfin-compat/settings",
|
||||
strings.NewReader(`{"web_enabled":false}`),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleUpdateJellyfinCompatSettings(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_enabled"]; got != "false" {
|
||||
t.Fatalf("jellyfin_compat.web_enabled = %q, want false", got)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"web_enabled":false`) {
|
||||
t.Fatalf("response body %q does not include disabled web_enabled", rec.Body.String())
|
||||
}
|
||||
if snapshot := restartStatus.Snapshot(); snapshot.RestartRequired {
|
||||
t.Fatalf("RestartRequired = true, want false for web_enabled-only update")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateJellyfinCompatSettingsDoesNotMarkRestartForLiveFields(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
root := t.TempDir()
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{}}
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: settings,
|
||||
RestartStatus: restartStatus,
|
||||
}
|
||||
|
||||
body := `{"public_url":"https://compat.example.test","server_name":"Silo Compat","emulated_server_version":"10.11.6","web_version":"10.11.6","web_install_dir":"` + root + `"}`
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPatch,
|
||||
"/admin/jellyfin-compat/settings",
|
||||
strings.NewReader(body),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleUpdateJellyfinCompatSettings(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.public_url"]; got != "https://compat.example.test" {
|
||||
t.Fatalf("jellyfin_compat.public_url = %q, want updated value", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.server_name"]; got != "Silo Compat" {
|
||||
t.Fatalf("jellyfin_compat.server_name = %q, want updated value", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.emulated_server_version"]; got != "10.11.6" {
|
||||
t.Fatalf("jellyfin_compat.emulated_server_version = %q, want 10.11.6", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_version"]; got != "10.11.6" {
|
||||
t.Fatalf("jellyfin_compat.web_version = %q, want 10.11.6", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_install_dir"]; got != root {
|
||||
t.Fatalf("jellyfin_compat.web_install_dir = %q, want %q", got, root)
|
||||
}
|
||||
if snapshot := restartStatus.Snapshot(); snapshot.RestartRequired {
|
||||
t.Fatalf("RestartRequired = true, want false for live Jellyfin settings")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJellyfinCompatSettingsRequireRestartFollowsRestartRegistry(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
updates map[string]string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "listener setting",
|
||||
updates: map[string]string{
|
||||
"jellyfin_compat.enabled": "true",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "live identity settings",
|
||||
updates: map[string]string{
|
||||
"jellyfin_compat.public_url": "https://compat.example.test",
|
||||
"jellyfin_compat.server_name": "Silo Compat",
|
||||
"jellyfin_compat.emulated_server_version": "10.11.6",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "live web settings",
|
||||
updates: map[string]string{
|
||||
"jellyfin_compat.web_enabled": "false",
|
||||
"jellyfin_compat.web_version": "10.11.6",
|
||||
"jellyfin_compat.web_dir": "/var/lib/silo/compat/jellyfin-web/current",
|
||||
"jellyfin_compat.web_install_dir": "/var/lib/silo/compat/jellyfin-web",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "mixed live and listener settings",
|
||||
updates: map[string]string{
|
||||
"jellyfin_compat.enabled": "false",
|
||||
"jellyfin_compat.web_enabled": "false",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := jellyfinCompatSettingsRequireRestart(tc.updates); got != tc.want {
|
||||
t.Fatalf("jellyfinCompatSettingsRequireRestart() = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateJellyfinCompatSettingsDisablesWebWhenAPIDisabled(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.enabled": "true",
|
||||
"jellyfin_compat.web_enabled": "true",
|
||||
}}
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
published := map[string]string{}
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: settings,
|
||||
RestartStatus: restartStatus,
|
||||
OnServerSettingUpdated: func(_ context.Context, key, value string) {
|
||||
published[key] = value
|
||||
},
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPatch,
|
||||
"/admin/jellyfin-compat/settings",
|
||||
strings.NewReader(`{"enabled":false,"web_enabled":true}`),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleUpdateJellyfinCompatSettings(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.enabled"]; got != "false" {
|
||||
t.Fatalf("jellyfin_compat.enabled = %q, want false", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_enabled"]; got != "false" {
|
||||
t.Fatalf("jellyfin_compat.web_enabled = %q, want false", got)
|
||||
}
|
||||
if got := published["jellyfin_compat.web_enabled"]; got != "false" {
|
||||
t.Fatalf("published jellyfin_compat.web_enabled = %q, want false", got)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"enabled":false`) {
|
||||
t.Fatalf("response body %q does not include disabled API", rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"web_enabled":false`) {
|
||||
t.Fatalf("response body %q does not include disabled web_enabled", rec.Body.String())
|
||||
}
|
||||
if snapshot := restartStatus.Snapshot(); !snapshot.RestartRequired {
|
||||
t.Fatal("RestartRequired = false, want true for API setting update")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveJellyfinCompatWebDisablesWebSetting(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.enabled": "true",
|
||||
"jellyfin_compat.web_enabled": "true",
|
||||
"jellyfin_compat.web_install_dir": t.TempDir(),
|
||||
}}
|
||||
published := map[string]string{}
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: settings,
|
||||
OnServerSettingUpdated: func(_ context.Context, key, value string) {
|
||||
published[key] = value
|
||||
},
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/admin/jellyfin-compat/web/remove",
|
||||
strings.NewReader(`{}`),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleRemoveJellyfinCompatWeb(rec, req)
|
||||
|
||||
if rec.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusAccepted, rec.Body.String())
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_enabled"]; got != "false" {
|
||||
t.Fatalf("jellyfin_compat.web_enabled = %q, want false", got)
|
||||
}
|
||||
if got := published["jellyfin_compat.web_enabled"]; got != "false" {
|
||||
t.Fatalf("published jellyfin_compat.web_enabled = %q, want false", got)
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"web_enabled":false`) {
|
||||
t.Fatalf("response body %q does not include disabled web_enabled", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateJellyfinCompatSettingsMarksRestartForProxyChanges(t *testing.T) {
|
||||
cfg, err := config.LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB: %v", err)
|
||||
}
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{}}
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
handler := &AdminHandler{
|
||||
Config: cfg,
|
||||
SettingsRepo: settings,
|
||||
RestartStatus: restartStatus,
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPatch,
|
||||
"/admin/jellyfin-compat/settings",
|
||||
strings.NewReader(`{"enabled":true}`),
|
||||
)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.HandleUpdateJellyfinCompatSettings(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d: %s", rec.Code, http.StatusOK, rec.Body.String())
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.enabled"]; got != "true" {
|
||||
t.Fatalf("jellyfin_compat.enabled = %q, want true", got)
|
||||
}
|
||||
snapshot := restartStatus.Snapshot()
|
||||
if !snapshot.RestartRequired {
|
||||
t.Fatal("RestartRequired = false, want true for proxy setting update")
|
||||
}
|
||||
if snapshot.RestartRequiredReason != "jellyfin_compat" {
|
||||
t.Fatalf("RestartRequiredReason = %q, want jellyfin_compat", snapshot.RestartRequiredReason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPersistJellyfinCompatWebInstallSettingsEnablesWebUI(t *testing.T) {
|
||||
settings := &fakeServerSettingsStore{values: map[string]string{
|
||||
"jellyfin_compat.web_enabled": "false",
|
||||
}}
|
||||
handler := &AdminHandler{SettingsRepo: settings}
|
||||
|
||||
err := handler.persistJellyfinCompatWebInstallSettings(context.Background(), jellycompat.WebComponentStatus{
|
||||
InstallRoot: "/var/lib/silo/compat/jellyfin-web",
|
||||
PinnedVersion: "10.11.6",
|
||||
InstalledVersion: "10.11.6",
|
||||
SourceURL: jellycompat.DefaultWebSourceURL,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("persistJellyfinCompatWebInstallSettings: %v", err)
|
||||
}
|
||||
|
||||
if got := settings.values["jellyfin_compat.web_enabled"]; got != "true" {
|
||||
t.Fatalf("jellyfin_compat.web_enabled = %q, want true", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_install_dir"]; got != "/var/lib/silo/compat/jellyfin-web" {
|
||||
t.Fatalf("jellyfin_compat.web_install_dir = %q", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_dir"]; got != jellycompat.ManagedWebInstallPath("/var/lib/silo/compat/jellyfin-web") {
|
||||
t.Fatalf("jellyfin_compat.web_dir = %q", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_version"]; got != "10.11.6" {
|
||||
t.Fatalf("jellyfin_compat.web_version = %q, want 10.11.6", got)
|
||||
}
|
||||
if got := settings.values["jellyfin_compat.web_source_url"]; got != jellycompat.DefaultWebSourceURL {
|
||||
t.Fatalf("jellyfin_compat.web_source_url = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -1,316 +1,395 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/Silo-Server/silo-server/internal/userstore"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// profileResolver is the minimal interface NotificationsHandler needs from the
|
||||
// userstore provider. Defined on the handler side so tests can stub it without
|
||||
// importing the full provider.
|
||||
type profileResolver interface {
|
||||
ForUser(ctx context.Context, userID int) (userstore.UserStore, error)
|
||||
}
|
||||
const (
|
||||
notificationsDefaultLimit = 25
|
||||
notificationsMaxLimit = 100
|
||||
notificationsSyncLimit = 50
|
||||
)
|
||||
|
||||
// NotificationsHandler handles inbox, preferences, and announcement endpoints.
|
||||
// NotificationsHandler serves the profile-scoped notification inbox,
|
||||
// preferences, capability, and websocket-ticket endpoints. All routes are
|
||||
// mounted behind RequireProfile.
|
||||
type NotificationsHandler struct {
|
||||
svc *notifications.Service
|
||||
profiles profileResolver // nil-tolerant: ChildSafe=false when absent
|
||||
system *notifications.System
|
||||
hub *evt.Hub
|
||||
}
|
||||
|
||||
// NewNotificationsHandler creates a new NotificationsHandler.
|
||||
// provider may be nil; when nil, ChildSafe defaults to false on all requests.
|
||||
func NewNotificationsHandler(svc *notifications.Service, provider ...profileResolver) *NotificationsHandler {
|
||||
h := &NotificationsHandler{svc: svc}
|
||||
if len(provider) > 0 {
|
||||
h.profiles = provider[0]
|
||||
}
|
||||
return h
|
||||
// NewNotificationsHandler creates a NotificationsHandler.
|
||||
func NewNotificationsHandler(system *notifications.System, hub *evt.Hub) *NotificationsHandler {
|
||||
return &NotificationsHandler{system: system, hub: hub}
|
||||
}
|
||||
|
||||
// childSafe resolves the active profile and returns Profile.IsChild.
|
||||
// Fails closed: an unresolvable profile is treated as a child so restricted
|
||||
// categories are never leaked on errors. Logs at debug level only — never fails
|
||||
// the request. Nil resolver / missing ids keep returning false (unauthenticated
|
||||
// paths never reach here; that's the wiring-absent case).
|
||||
func (h *NotificationsHandler) childSafe(r *http.Request, userID int, profileID string) bool {
|
||||
if h.profiles == nil || userID == 0 || profileID == "" {
|
||||
return false
|
||||
}
|
||||
store, err := h.profiles.ForUser(r.Context(), userID)
|
||||
if err != nil {
|
||||
slog.Debug("notifications: childSafe: ForUser failed", "user_id", userID, "error", err)
|
||||
return true // fail closed: treat unknown as child
|
||||
}
|
||||
p, err := store.GetProfile(r.Context(), profileID)
|
||||
if err != nil || p == nil {
|
||||
slog.Debug("notifications: childSafe: GetProfile failed", "profile_id", profileID, "error", err)
|
||||
return true // fail closed: treat unknown as child
|
||||
}
|
||||
return p.IsChild
|
||||
type notificationListResponse struct {
|
||||
Notifications []notifications.DeliveryRowPayload `json:"notifications"`
|
||||
// NextCursor pages further into the past via the `before` query param.
|
||||
// Empty when this page may be the last.
|
||||
NextCursor string `json:"next_cursor,omitempty"`
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// User-scoped inbox endpoints
|
||||
// ---------------------------------------------------------------------------
|
||||
type notificationSyncResponse struct {
|
||||
Notifications []notifications.DeliveryRowPayload `json:"notifications"`
|
||||
NextCursor string `json:"next_cursor,omitempty"`
|
||||
UnreadCount int `json:"unread_count"`
|
||||
}
|
||||
|
||||
// HandleList handles GET /notifications.
|
||||
// Query params: unread=1, category, cursor (int64), limit (int, default 50, max 100).
|
||||
// Response: {"items":[...],"next_cursor":<int64|null>}
|
||||
type unreadCountResponse struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type wsTicketResponse struct {
|
||||
Ticket string `json:"ticket"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func parseNotificationsLimit(r *http.Request, fallback int) int {
|
||||
raw := r.URL.Query().Get("limit")
|
||||
if raw == "" {
|
||||
return fallback
|
||||
}
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return min(limit, notificationsMaxLimit)
|
||||
}
|
||||
|
||||
// HandleList handles GET /notifications (newest-first inbox page).
|
||||
func (h *NotificationsHandler) HandleList(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
unreadOnly := r.URL.Query().Get("status") == "unread"
|
||||
limit := parseNotificationsLimit(r, notificationsDefaultLimit)
|
||||
|
||||
q := r.URL.Query()
|
||||
|
||||
limit := 50
|
||||
if v := q.Get("limit"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||
limit = n
|
||||
var before *notifications.Cursor
|
||||
if raw := r.URL.Query().Get("before"); raw != "" {
|
||||
cursor, err := notifications.DecodeCursor(raw)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid before cursor")
|
||||
return
|
||||
}
|
||||
}
|
||||
// keep in sync with the clamp in internal/notifications/store.go List
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
before = &cursor
|
||||
}
|
||||
|
||||
f := notifications.ListFilter{
|
||||
UserID: userID,
|
||||
ProfileID: profileID,
|
||||
Limit: limit,
|
||||
ChildSafe: h.childSafe(r, userID, profileID),
|
||||
}
|
||||
if q.Get("unread") == "1" {
|
||||
f.UnreadOnly = true
|
||||
}
|
||||
if cat := q.Get("category"); cat != "" {
|
||||
f.Category = notifications.Category(cat)
|
||||
}
|
||||
if v := q.Get("cursor"); v != "" {
|
||||
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
|
||||
f.Cursor = n
|
||||
}
|
||||
}
|
||||
|
||||
items, err := h.svc.List(r.Context(), f)
|
||||
rows, err := h.system.Deliveries.ListInbox(r.Context(), profileID, unreadOnly, limit, before)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list notifications")
|
||||
return
|
||||
}
|
||||
response := notificationListResponse{Notifications: h.system.PayloadsForRows(r.Context(), rows)}
|
||||
if len(rows) == limit {
|
||||
last := rows[len(rows)-1]
|
||||
response.NextCursor = notifications.Cursor{CreatedAt: last.CreatedAt, ID: last.ID}.Encode()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, response)
|
||||
}
|
||||
|
||||
var nextCursor *int64
|
||||
if len(items) == limit && limit > 0 {
|
||||
last := items[len(items)-1].ID
|
||||
nextCursor = &last
|
||||
// HandleSync handles GET /notifications/sync — the forward (ascending) cursor
|
||||
// sync used by clients waking from a push or reconnecting after a gap.
|
||||
func (h *NotificationsHandler) HandleSync(w http.ResponseWriter, r *http.Request) {
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
limit := parseNotificationsLimit(r, notificationsSyncLimit)
|
||||
|
||||
var since *notifications.Cursor
|
||||
if raw := r.URL.Query().Get("since"); raw != "" {
|
||||
cursor, err := notifications.DecodeCursor(raw)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid since cursor")
|
||||
return
|
||||
}
|
||||
since = &cursor
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Items []*notifications.Notification `json:"items"`
|
||||
NextCursor *int64 `json:"next_cursor"`
|
||||
}{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
rows, err := h.system.Deliveries.ListSync(r.Context(), profileID, since, limit)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to sync notifications")
|
||||
return
|
||||
}
|
||||
unread, err := h.system.Deliveries.UnreadCount(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to count unread notifications")
|
||||
return
|
||||
}
|
||||
|
||||
response := notificationSyncResponse{
|
||||
Notifications: h.system.PayloadsForRows(r.Context(), rows),
|
||||
UnreadCount: unread,
|
||||
}
|
||||
if len(rows) > 0 {
|
||||
last := rows[len(rows)-1]
|
||||
response.NextCursor = notifications.Cursor{CreatedAt: last.CreatedAt, ID: last.ID}.Encode()
|
||||
} else if since != nil {
|
||||
response.NextCursor = since.Encode()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, response)
|
||||
}
|
||||
|
||||
// HandleGet handles GET /notifications/{id}; 404 for other profiles' rows.
|
||||
func (h *NotificationsHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
id := chi.URLParam(r, "id")
|
||||
|
||||
row, err := h.system.Deliveries.GetByID(r.Context(), profileID, id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load notification")
|
||||
return
|
||||
}
|
||||
if row == nil {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Notification not found")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, h.system.PayloadForRow(r.Context(), *row))
|
||||
}
|
||||
|
||||
// HandleUnreadCount handles GET /notifications/unread-count.
|
||||
// Response: {"count": n}
|
||||
func (h *NotificationsHandler) HandleUnreadCount(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
count, err := h.svc.UnreadCount(r.Context(), userID, profileID, h.childSafe(r, userID, profileID))
|
||||
count, err := h.system.Deliveries.UnreadCount(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get unread count")
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to count unread notifications")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Count int `json:"count"`
|
||||
}{Count: count})
|
||||
writeJSON(w, http.StatusOK, unreadCountResponse{Count: count})
|
||||
}
|
||||
|
||||
// markReadRequest is the request body for POST /notifications/read.
|
||||
type markReadRequest struct {
|
||||
IDs []int64 `json:"ids"`
|
||||
All bool `json:"all"`
|
||||
}
|
||||
|
||||
// HandleMarkRead handles POST /notifications/read.
|
||||
// Body: {"ids":[...]} OR {"all":true}. 400 when neither present.
|
||||
// HandleMarkRead handles POST /notifications/{id}/read. Idempotent.
|
||||
func (h *NotificationsHandler) HandleMarkRead(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
id := chi.URLParam(r, "id")
|
||||
|
||||
var req markReadRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
if !req.All && len(req.IDs) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Provide ids or all:true")
|
||||
return
|
||||
}
|
||||
|
||||
if req.All {
|
||||
if err := h.svc.MarkAllRead(r.Context(), userID, profileID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mark all read")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := h.svc.MarkRead(r.Context(), userID, profileID, req.IDs); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mark notifications read")
|
||||
return
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleDismiss handles POST /notifications/{id}/dismiss.
|
||||
// 400 for invalid id; 404 on ErrNotFound; 204 on success.
|
||||
func (h *NotificationsHandler) HandleDismiss(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
rawID := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseInt(rawID, 10, 64)
|
||||
transitioned, err := h.system.Deliveries.MarkRead(r.Context(), profileID, id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid notification id")
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mark notification read")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.Dismiss(r.Context(), userID, profileID, id); err != nil {
|
||||
if errors.Is(err, notifications.ErrNotFound) {
|
||||
if !transitioned {
|
||||
// Already read is fine (idempotent); unknown IDs are a 404.
|
||||
exists, err := h.system.Deliveries.Exists(r.Context(), profileID, id)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mark notification read")
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Notification not found")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to dismiss notification")
|
||||
return
|
||||
}
|
||||
if transitioned {
|
||||
h.publishReadEvent(r, userID, profileID, id)
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleGetPreferences handles GET /notifications/preferences.
|
||||
// Response: {"preferences":[{category,enabled},...]}
|
||||
func (h *NotificationsHandler) HandleGetPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
// HandleReadAll handles POST /notifications/read-all.
|
||||
func (h *NotificationsHandler) HandleReadAll(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
prefs, err := h.svc.GetPreferences(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get preferences")
|
||||
if _, err := h.system.Deliveries.MarkAllRead(r.Context(), profileID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mark notifications read")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Preferences []notifications.Preference `json:"preferences"`
|
||||
}{Preferences: prefs})
|
||||
h.publishReadEvent(r, userID, profileID, "")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// putPreferencesRequest is the request body for PUT /notifications/preferences.
|
||||
type putPreferencesRequest struct {
|
||||
Preferences []notifications.Preference `json:"preferences"`
|
||||
// publishReadEvent lets other connected tabs of the same profile reconcile
|
||||
// read state. An empty id means "all read".
|
||||
func (h *NotificationsHandler) publishReadEvent(r *http.Request, userID int, profileID, id string) {
|
||||
if h.hub == nil {
|
||||
return
|
||||
}
|
||||
payload := map[string]any{"profile_id": profileID}
|
||||
if id != "" {
|
||||
payload["id"] = id
|
||||
} else {
|
||||
payload["all"] = true
|
||||
}
|
||||
_ = h.hub.PublishJSON(r.Context(), evt.ChannelNotifications, notifications.EventNotificationRead,
|
||||
payload, evt.PublishOptions{UserID: userID, ProfileID: profileID})
|
||||
}
|
||||
|
||||
// HandlePutPreferences handles PUT /notifications/preferences.
|
||||
// 400 on empty or invalid category; 204 on success.
|
||||
func (h *NotificationsHandler) HandlePutPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
// HandleGetPreferences handles GET /notifications/preferences.
|
||||
func (h *NotificationsHandler) HandleGetPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
prefs, err := h.system.Preferences.Get(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load notification preferences")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, prefs)
|
||||
}
|
||||
|
||||
var req putPreferencesRequest
|
||||
type updatePreferencesRequest struct {
|
||||
Enabled *bool `json:"enabled"`
|
||||
NotifyFavorites *bool `json:"notify_favorites"`
|
||||
NotifyWatchlist *bool `json:"notify_watchlist"`
|
||||
NotifyContinueWatching *bool `json:"notify_continue_watching"`
|
||||
NotifyNextUp *bool `json:"notify_next_up"`
|
||||
}
|
||||
|
||||
// HandleUpdatePreferences handles PUT /notifications/preferences. Fields are
|
||||
// optional; omitted fields keep their current value. Idempotent.
|
||||
func (h *NotificationsHandler) HandleUpdatePreferences(w http.ResponseWriter, r *http.Request) {
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req updatePreferencesRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
if len(req.Preferences) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "preferences must not be empty")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.SetPreferences(r.Context(), userID, req.Preferences); err != nil {
|
||||
if errors.Is(err, notifications.ErrInvalidCategory) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save preferences")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Admin announcement endpoints
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// HandleListAnnouncements handles GET /admin/announcements.
|
||||
// Response: {"items":[...]}
|
||||
func (h *NotificationsHandler) HandleListAnnouncements(w http.ResponseWriter, r *http.Request) {
|
||||
items, err := h.svc.ListAnnouncements(r.Context())
|
||||
prefs, err := h.system.Preferences.Get(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list announcements")
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load notification preferences")
|
||||
return
|
||||
}
|
||||
if items == nil {
|
||||
items = []*notifications.Announcement{}
|
||||
if req.Enabled != nil {
|
||||
prefs.Enabled = *req.Enabled
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Items []*notifications.Announcement `json:"items"`
|
||||
}{Items: items})
|
||||
if req.NotifyFavorites != nil {
|
||||
prefs.NotifyFavorites = *req.NotifyFavorites
|
||||
}
|
||||
if req.NotifyWatchlist != nil {
|
||||
prefs.NotifyWatchlist = *req.NotifyWatchlist
|
||||
}
|
||||
if req.NotifyContinueWatching != nil {
|
||||
prefs.NotifyContinueWatching = *req.NotifyContinueWatching
|
||||
}
|
||||
if req.NotifyNextUp != nil {
|
||||
prefs.NotifyNextUp = *req.NotifyNextUp
|
||||
}
|
||||
if err := h.system.Preferences.Upsert(r.Context(), prefs); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save notification preferences")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, prefs)
|
||||
}
|
||||
|
||||
// HandleCreateAnnouncement handles POST /admin/announcements.
|
||||
// Decodes an Announcement, sets CreatedBy from claims, publishes; 400 on
|
||||
// validation error; 201 with the announcement on success.
|
||||
func (h *NotificationsHandler) HandleCreateAnnouncement(w http.ResponseWriter, r *http.Request) {
|
||||
claims := apimw.GetClaims(r.Context())
|
||||
|
||||
// Announcements fan out one row per (user, profile); cap the request body so
|
||||
// a large title/body cannot be amplified into the whole server's inboxes.
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 64*1024)
|
||||
|
||||
var a notifications.Announcement
|
||||
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
if claims != nil {
|
||||
userID := claims.UserID
|
||||
a.CreatedBy = &userID
|
||||
}
|
||||
|
||||
if err := h.svc.PublishAnnouncement(r.Context(), &a); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, &a)
|
||||
type capabilityResponse struct {
|
||||
InApp capabilityInApp `json:"in_app"`
|
||||
ApplePush capabilityPush `json:"apple_push"`
|
||||
AndroidPush capabilityPush `json:"android_push"`
|
||||
WebPush capabilityWebPush `json:"web_push"`
|
||||
Webhooks capabilityWebhooks `json:"webhooks"`
|
||||
Email capabilityAccountChannel `json:"email"`
|
||||
Discord capabilityAccountChannel `json:"discord"`
|
||||
}
|
||||
|
||||
// HandleDeleteAnnouncement handles DELETE /admin/announcements/{id}.
|
||||
// 404 on ErrNotFound; 204 on success.
|
||||
func (h *NotificationsHandler) HandleDeleteAnnouncement(w http.ResponseWriter, r *http.Request) {
|
||||
rawID := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseInt(rawID, 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid announcement id")
|
||||
return
|
||||
}
|
||||
// capabilityAccountChannel describes an account-level digest channel (email,
|
||||
// Discord DMs).
|
||||
type capabilityAccountChannel struct {
|
||||
Available bool `json:"available"`
|
||||
// Modes lists the cadences users may pick (per-episode is an admin
|
||||
// allowance); DigestHour tells the UI when daily digests go out.
|
||||
Modes []string `json:"modes"`
|
||||
DigestHour int `json:"digest_hour"`
|
||||
}
|
||||
|
||||
if err := h.svc.DeleteAnnouncement(r.Context(), id); err != nil {
|
||||
if errors.Is(err, notifications.ErrNotFound) {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Announcement not found")
|
||||
return
|
||||
type capabilityWebPush struct {
|
||||
Available bool `json:"available"`
|
||||
PublicKey string `json:"public_key,omitempty"`
|
||||
}
|
||||
|
||||
type capabilityInApp struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type capabilityPush struct {
|
||||
Available bool `json:"available"`
|
||||
Provider string `json:"provider"`
|
||||
SupportedModes []string `json:"supported_modes"`
|
||||
}
|
||||
|
||||
type capabilityWebhooks struct {
|
||||
Available bool `json:"available"`
|
||||
MaxPerProfile int `json:"max_per_profile"`
|
||||
SupportedTypes []string `json:"supported_types"`
|
||||
}
|
||||
|
||||
// HandleCapability handles GET /notifications/capability. Clients render
|
||||
// setup UI from this response instead of introspecting admin settings. Push
|
||||
// channels report unavailable until they ship
|
||||
// (docs/superpowers/plans/notifications/02-03).
|
||||
func (h *NotificationsHandler) HandleCapability(w http.ResponseWriter, r *http.Request) {
|
||||
webhooks := capabilityWebhooks{Available: false, MaxPerProfile: 0, SupportedTypes: []string{}}
|
||||
if h.system.Webhooks != nil && h.system.Settings.WebhooksEnabled(r.Context()) {
|
||||
webhooks = capabilityWebhooks{
|
||||
Available: true,
|
||||
MaxPerProfile: h.system.Settings.WebhooksMaxPerProfile(r.Context()),
|
||||
SupportedTypes: []string{"discord", "generic"},
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to delete announcement")
|
||||
}
|
||||
webPush := capabilityWebPush{}
|
||||
if h.system.WebPush != nil && h.system.Settings.WebPushEnabled(r.Context()) {
|
||||
if publicKey, err := h.system.WebPush.PublicKey(r.Context()); err == nil && publicKey != "" {
|
||||
webPush = capabilityWebPush{Available: true, PublicKey: publicKey}
|
||||
}
|
||||
}
|
||||
email := capabilityAccountChannel{Modes: []string{}}
|
||||
if h.system.EmailAvailable(r.Context()) {
|
||||
modes := []string{notifications.ChannelModeDailyDigest}
|
||||
if h.system.Settings.EmailAllowPerEpisode(r.Context()) {
|
||||
modes = append(modes,
|
||||
notifications.ChannelModePerEpisode,
|
||||
notifications.ChannelModePerEpisodeAndDigest)
|
||||
}
|
||||
email = capabilityAccountChannel{
|
||||
Available: true,
|
||||
Modes: modes,
|
||||
DigestHour: h.system.Settings.EmailDigestHour(r.Context()),
|
||||
}
|
||||
}
|
||||
discordCap := capabilityAccountChannel{Modes: []string{}}
|
||||
if h.system.DiscordAvailable(r.Context()) {
|
||||
modes := []string{notifications.ChannelModeDailyDigest}
|
||||
if h.system.Settings.DiscordAllowPerEpisode(r.Context()) {
|
||||
modes = append(modes,
|
||||
notifications.ChannelModePerEpisode,
|
||||
notifications.ChannelModePerEpisodeAndDigest)
|
||||
}
|
||||
discordCap = capabilityAccountChannel{
|
||||
Available: true,
|
||||
Modes: modes,
|
||||
DigestHour: h.system.Settings.DiscordDigestHour(r.Context()),
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, capabilityResponse{
|
||||
InApp: capabilityInApp{Enabled: h.system.Settings.UIEnabled(r.Context())},
|
||||
ApplePush: capabilityPush{Available: false, Provider: "off", SupportedModes: []string{"in_app_only"}},
|
||||
AndroidPush: capabilityPush{Available: false, Provider: "off", SupportedModes: []string{"in_app_only"}},
|
||||
WebPush: webPush,
|
||||
Webhooks: webhooks,
|
||||
Email: email,
|
||||
Discord: discordCap,
|
||||
})
|
||||
}
|
||||
|
||||
// HandleMintWSTicket handles POST /events/ws-ticket: mints a short-lived
|
||||
// single-use websocket handshake ticket bound to (user, profile). Long-lived
|
||||
// tokens must never ride the websocket query string — reverse-proxy access
|
||||
// logs capture it.
|
||||
func (h *NotificationsHandler) HandleMintWSTicket(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
ticket, ttl, err := h.system.Tickets.Mint(r.Context(), userID, profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to mint websocket ticket")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
writeJSON(w, http.StatusOK, wsTicketResponse{
|
||||
Ticket: ticket,
|
||||
ExpiresIn: int(ttl.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/discord"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
)
|
||||
|
||||
// discordSettingsPath is the SPA page the OAuth callback redirects back to,
|
||||
// with ?discord_linked=1 or ?discord_error=<reason> for the page to toast.
|
||||
const discordSettingsPath = "/settings/notifications"
|
||||
|
||||
// DiscordNotificationsHandler serves the account-level Discord DM channel:
|
||||
// preferences, the OAuth account-link flow, and the admin bot test. The
|
||||
// preferences and link-init endpoints require auth; the link callback is
|
||||
// public because Discord redirects the browser there without credentials —
|
||||
// the one-time server-side state row authenticates it instead.
|
||||
type DiscordNotificationsHandler struct {
|
||||
system *notifications.System
|
||||
publicURL string
|
||||
}
|
||||
|
||||
// NewDiscordNotificationsHandler creates a DiscordNotificationsHandler.
|
||||
// publicURL may be empty, in which case linking is reported unavailable
|
||||
// (Discord needs a stable redirect_uri origin).
|
||||
func NewDiscordNotificationsHandler(system *notifications.System, publicURL string) *DiscordNotificationsHandler {
|
||||
return &DiscordNotificationsHandler{system: system, publicURL: strings.TrimRight(publicURL, "/")}
|
||||
}
|
||||
|
||||
func (h *DiscordNotificationsHandler) redirectURI() string {
|
||||
return h.publicURL + "/api/v1/notifications/discord/link/callback"
|
||||
}
|
||||
|
||||
// discordPreferencesResponse is the account-level Discord DM setting plus
|
||||
// link state and health.
|
||||
type discordPreferencesResponse struct {
|
||||
Linked bool `json:"linked"`
|
||||
DiscordUsername string `json:"discord_username,omitempty"`
|
||||
Mode string `json:"mode"`
|
||||
LinkFailure string `json:"link_failure,omitempty"`
|
||||
}
|
||||
|
||||
type updateDiscordPreferencesRequest struct {
|
||||
Mode string `json:"mode"`
|
||||
}
|
||||
|
||||
func discordPrefsResponse(prefs notifications.DiscordPrefs) discordPreferencesResponse {
|
||||
return discordPreferencesResponse{
|
||||
Linked: prefs.Linked(),
|
||||
DiscordUsername: prefs.DiscordUsername,
|
||||
Mode: prefs.Mode,
|
||||
LinkFailure: prefs.LinkFailure,
|
||||
}
|
||||
}
|
||||
|
||||
// HandleGetPreferences handles GET /notifications/discord-preferences.
|
||||
func (h *DiscordNotificationsHandler) HandleGetPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
prefs, err := h.system.DiscordPrefsFor(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load Discord preferences")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, discordPrefsResponse(prefs))
|
||||
}
|
||||
|
||||
// HandleUpdatePreferences handles PUT /notifications/discord-preferences.
|
||||
func (h *DiscordNotificationsHandler) HandleUpdatePreferences(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
|
||||
var req updateDiscordPreferencesRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
err := h.system.SetDiscordMode(r.Context(), userID, req.Mode)
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, notifications.ErrDiscordModeInvalid):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unknown Discord notification mode")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrDiscordModeNotAllowed):
|
||||
writeError(w, http.StatusBadRequest, "not_allowed", "Per-episode Discord DMs are disabled by the administrator")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrDiscordNotLinked):
|
||||
writeError(w, http.StatusBadRequest, "not_linked", "Link a Discord account first")
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save Discord preferences")
|
||||
return
|
||||
}
|
||||
prefs, err := h.system.DiscordPrefsFor(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load Discord preferences")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, discordPrefsResponse(prefs))
|
||||
}
|
||||
|
||||
// HandleUnlink handles DELETE /notifications/discord-link: removes the
|
||||
// linked identity and switches the channel off.
|
||||
func (h *DiscordNotificationsHandler) HandleUnlink(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
if err := h.system.UnlinkDiscord(r.Context(), userID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to unlink Discord account")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, discordPreferencesResponse{Mode: notifications.ChannelModeOff})
|
||||
}
|
||||
|
||||
type discordLinkInitResponse struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// HandleLinkInit handles POST /notifications/discord/link/init: records a
|
||||
// one-time state for the signed-in account and returns the Discord consent
|
||||
// URL for the SPA to navigate to.
|
||||
func (h *DiscordNotificationsHandler) HandleLinkInit(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
if !h.system.DiscordAvailable(r.Context()) {
|
||||
writeError(w, http.StatusConflict, "not_configured", "Discord integration is not enabled by the administrator")
|
||||
return
|
||||
}
|
||||
if h.publicURL == "" {
|
||||
writeError(w, http.StatusConflict, "no_public_url", "Linking requires SILO_PUBLIC_URL to be configured")
|
||||
return
|
||||
}
|
||||
|
||||
stateBytes := make([]byte, 32)
|
||||
if _, err := rand.Read(stateBytes); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to start Discord link")
|
||||
return
|
||||
}
|
||||
state := hex.EncodeToString(stateBytes)
|
||||
if err := h.system.BeginDiscordLink(r.Context(), state, userID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to start Discord link")
|
||||
return
|
||||
}
|
||||
|
||||
query := url.Values{
|
||||
"client_id": {h.system.Settings.DiscordClientID(r.Context())},
|
||||
"response_type": {"code"},
|
||||
"scope": {"identify"},
|
||||
"redirect_uri": {h.redirectURI()},
|
||||
"state": {state},
|
||||
}
|
||||
writeJSON(w, http.StatusOK, discordLinkInitResponse{
|
||||
URL: discord.AuthorizeURL + "?" + query.Encode(),
|
||||
})
|
||||
}
|
||||
|
||||
// HandleLinkCallback handles GET /notifications/discord/link/callback.
|
||||
// Public route: Discord redirects the user's browser here. The one-time
|
||||
// state row authenticates the request and recovers which account started
|
||||
// the flow; the browser is then sent back to the settings page.
|
||||
func (h *DiscordNotificationsHandler) HandleLinkCallback(w http.ResponseWriter, r *http.Request) {
|
||||
redirectBack := func(params url.Values) {
|
||||
http.Redirect(w, r, discordSettingsPath+"?"+params.Encode(), http.StatusFound)
|
||||
}
|
||||
|
||||
// The admin may have switched the integration off between init and
|
||||
// callback; don't complete a link for a disabled channel.
|
||||
if !h.system.DiscordAvailable(r.Context()) {
|
||||
redirectBack(url.Values{"discord_error": {"disabled"}})
|
||||
return
|
||||
}
|
||||
|
||||
query := r.URL.Query()
|
||||
if query.Get("error") != "" {
|
||||
// The user declined on Discord's consent screen.
|
||||
redirectBack(url.Values{"discord_error": {"denied"}})
|
||||
return
|
||||
}
|
||||
state := query.Get("state")
|
||||
code := query.Get("code")
|
||||
if state == "" || code == "" {
|
||||
redirectBack(url.Values{"discord_error": {"invalid_callback"}})
|
||||
return
|
||||
}
|
||||
|
||||
userID, ok, err := h.system.ConsumeDiscordLinkState(r.Context(), state)
|
||||
if err != nil || !ok {
|
||||
redirectBack(url.Values{"discord_error": {"state_invalid"}})
|
||||
return
|
||||
}
|
||||
if _, err := h.system.CompleteDiscordLink(r.Context(), userID, code, h.redirectURI()); err != nil {
|
||||
redirectBack(url.Values{"discord_error": {"exchange_failed"}})
|
||||
return
|
||||
}
|
||||
redirectBack(url.Values{"discord_linked": {"1"}})
|
||||
}
|
||||
|
||||
type discordTestResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// HandleAdminTest handles POST /admin/notifications/discord/test: verifies
|
||||
// the configured bot token by fetching the bot's own identity.
|
||||
func (h *DiscordNotificationsHandler) HandleAdminTest(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
botUser, err := h.system.TestDiscordBot(r.Context())
|
||||
duration := time.Since(start).Milliseconds()
|
||||
if errors.Is(err, notifications.ErrDiscordNotConfigured) {
|
||||
writeJSON(w, http.StatusOK, discordTestResponse{
|
||||
OK: false, DurationMS: duration, Message: "Bot token is not configured",
|
||||
})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, discordTestResponse{
|
||||
OK: false, DurationMS: duration, Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, discordTestResponse{
|
||||
OK: true, DurationMS: duration, Message: "Connected as " + botUser.Username,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
)
|
||||
|
||||
// emailPreferencesResponse is one profile's email notification state. The
|
||||
// channel is profile-scoped: each profile verifies its own destination
|
||||
// address and receives nothing until it has one — there is no account-email
|
||||
// fallback.
|
||||
type emailPreferencesResponse struct {
|
||||
Mode string `json:"mode"`
|
||||
// CustomEmail is the verified destination ('' = none; channel inert).
|
||||
CustomEmail string `json:"custom_email"`
|
||||
// PendingEmail is an address awaiting link-click verification.
|
||||
PendingEmail string `json:"pending_email"`
|
||||
// CanEditAddress is false for child profiles, which cannot set
|
||||
// addresses (and so cannot receive email notifications).
|
||||
CanEditAddress bool `json:"can_edit_address"`
|
||||
}
|
||||
|
||||
type updateEmailPreferencesRequest struct {
|
||||
Mode string `json:"mode"`
|
||||
}
|
||||
|
||||
type updateEmailAddressRequest struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func emailPreferencesPayload(state notifications.EmailPreferencesState) emailPreferencesResponse {
|
||||
return emailPreferencesResponse{
|
||||
Mode: state.Mode,
|
||||
CustomEmail: state.CustomEmail,
|
||||
PendingEmail: state.PendingEmail,
|
||||
CanEditAddress: !state.IsChild,
|
||||
}
|
||||
}
|
||||
|
||||
// respondEmailPreferences re-reads and writes the profile's full email state,
|
||||
// so every mutation returns the same shape as GET.
|
||||
func (h *NotificationsHandler) respondEmailPreferences(w http.ResponseWriter, r *http.Request, userID int, profileID string) {
|
||||
state, err := h.system.EmailPreferences(r.Context(), userID, profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load email preferences")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, emailPreferencesPayload(state))
|
||||
}
|
||||
|
||||
// HandleGetEmailPreferences handles GET /notifications/email-preferences.
|
||||
func (h *NotificationsHandler) HandleGetEmailPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
h.respondEmailPreferences(w, r, apimw.GetUserID(r.Context()), apimw.GetProfileID(r.Context()))
|
||||
}
|
||||
|
||||
// HandleUpdateEmailPreferences handles PUT /notifications/email-preferences.
|
||||
func (h *NotificationsHandler) HandleUpdateEmailPreferences(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req updateEmailPreferencesRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
err := h.system.SetEmailMode(r.Context(), userID, profileID, req.Mode)
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, notifications.ErrEmailModeInvalid):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Unknown email notification mode")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailModeNotAllowed):
|
||||
writeError(w, http.StatusBadRequest, "not_allowed", "Per-episode email is disabled by the administrator")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailNoAddress):
|
||||
writeError(w, http.StatusBadRequest, "no_email", "Verify an email address for this profile first")
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save email preferences")
|
||||
return
|
||||
}
|
||||
h.respondEmailPreferences(w, r, userID, profileID)
|
||||
}
|
||||
|
||||
// HandleRequestEmailAddress handles PUT /notifications/email-preferences/address.
|
||||
// It stores the candidate address and emails it a verification link; the
|
||||
// address only becomes the destination once that link is clicked.
|
||||
func (h *NotificationsHandler) HandleRequestEmailAddress(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req updateEmailAddressRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
err := h.system.RequestEmailAddress(r.Context(), userID, profileID, req.Email)
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, notifications.ErrEmailInvalidAddress):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid email address")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailChildProfile):
|
||||
writeError(w, http.StatusForbidden, "child_profile", "Child profiles cannot set a custom notification address")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailAddressInUse):
|
||||
writeError(w, http.StatusConflict, "address_in_use", "That email address is already used by another profile or account")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailVerifyRateLimited):
|
||||
writeError(w, http.StatusTooManyRequests, "rate_limited", "Too many verification emails; try again later")
|
||||
return
|
||||
case errors.Is(err, notifications.ErrEmailNoLinkBase):
|
||||
writeError(w, http.StatusConflict, "no_external_url", "The server has no external URL configured for verification links")
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to send the verification email")
|
||||
return
|
||||
}
|
||||
h.respondEmailPreferences(w, r, userID, profileID)
|
||||
}
|
||||
|
||||
// HandleClearEmailAddress handles DELETE /notifications/email-preferences/address.
|
||||
func (h *NotificationsHandler) HandleClearEmailAddress(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
err := h.system.ClearEmailAddress(r.Context(), userID, profileID)
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.Is(err, notifications.ErrEmailChildProfile):
|
||||
writeError(w, http.StatusForbidden, "child_profile", "Child profiles cannot change the notification address")
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to remove the custom address")
|
||||
return
|
||||
}
|
||||
h.respondEmailPreferences(w, r, userID, profileID)
|
||||
}
|
||||
|
||||
// EmailLinkHandler serves the public tokenized email endpoints: address
|
||||
// verification and unsubscribe. Both are clicked from email clients on
|
||||
// devices that may have no Silo session, so they render minimal standalone
|
||||
// HTML instead of redirecting into the authenticated app.
|
||||
type EmailLinkHandler struct {
|
||||
system *notifications.System
|
||||
}
|
||||
|
||||
// NewEmailLinkHandler creates an EmailLinkHandler.
|
||||
func NewEmailLinkHandler(system *notifications.System) *EmailLinkHandler {
|
||||
return &EmailLinkHandler{system: system}
|
||||
}
|
||||
|
||||
// HandleVerify handles GET /notifications/email/verify?token=...
|
||||
func (h *EmailLinkHandler) HandleVerify(w http.ResponseWriter, r *http.Request) {
|
||||
outcome, err := h.system.VerifyEmailToken(r.Context(), r.URL.Query().Get("token"))
|
||||
switch {
|
||||
case err != nil:
|
||||
writeEmailLinkPage(w, http.StatusInternalServerError, "Something went wrong",
|
||||
"The address could not be verified. Try the link again in a moment.")
|
||||
case outcome == notifications.EmailVerifyConflict:
|
||||
writeEmailLinkPage(w, http.StatusConflict, "Address already in use",
|
||||
"This address now belongs to another profile or account. Choose a different address in Silo's notification settings.")
|
||||
case outcome == notifications.EmailVerifyInvalid:
|
||||
writeEmailLinkPage(w, http.StatusBadRequest, "Link expired or already used",
|
||||
"Request a new verification email from Silo's notification settings.")
|
||||
default:
|
||||
writeEmailLinkPage(w, http.StatusOK, "Address verified",
|
||||
"Silo notifications for this profile will now be delivered here. You can close this page.")
|
||||
}
|
||||
}
|
||||
|
||||
// HandleUnsubscribe handles GET and POST /notifications/email/unsubscribe?token=...
|
||||
// POST is the RFC 8058 one-click target mail clients call directly.
|
||||
func (h *EmailLinkHandler) HandleUnsubscribe(w http.ResponseWriter, r *http.Request) {
|
||||
ok, err := h.system.UnsubscribeEmail(r.Context(), r.URL.Query().Get("token"))
|
||||
switch {
|
||||
case err != nil:
|
||||
writeEmailLinkPage(w, http.StatusInternalServerError, "Something went wrong",
|
||||
"Could not unsubscribe. Try the link again in a moment.")
|
||||
case !ok:
|
||||
writeEmailLinkPage(w, http.StatusBadRequest, "Link invalid",
|
||||
"This unsubscribe link is no longer valid. Manage notifications in Silo's settings.")
|
||||
default:
|
||||
writeEmailLinkPage(w, http.StatusOK, "Unsubscribed",
|
||||
"This profile will no longer receive notification emails. Re-enable them any time in Silo's settings.")
|
||||
}
|
||||
}
|
||||
|
||||
// writeEmailLinkPage renders the minimal standalone page behind tokenized
|
||||
// email links.
|
||||
func writeEmailLinkPage(w http.ResponseWriter, status int, title, detail string) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
fmt.Fprintf(w, `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"><title>%s — Silo</title></head>
|
||||
<body style="font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;background:#101014;color:#e8e8ec;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0;">
|
||||
<div style="max-width:420px;padding:32px;text-align:center;">
|
||||
<h1 style="font-size:20px;margin:0 0 12px;">%s</h1>
|
||||
<p style="font-size:14px;color:#9a9aa4;margin:0;">%s</p>
|
||||
</div></body></html>`,
|
||||
html.EscapeString(title), html.EscapeString(title), html.EscapeString(detail))
|
||||
}
|
||||
@@ -1,673 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
evt "github.com/Silo-Server/silo-server/internal/events"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/Silo-Server/silo-server/internal/userstore"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fake profile resolver for child-safe tests.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// fakeProfileStore satisfies userstore.UserStore minimally.
|
||||
// Only GetProfile is exercised by childSafe.
|
||||
type fakeProfileStore struct {
|
||||
userstore.UserStore // embed for unimplemented methods
|
||||
isChild bool
|
||||
}
|
||||
|
||||
func (s *fakeProfileStore) GetProfile(_ context.Context, id string) (*userstore.Profile, error) {
|
||||
return &userstore.Profile{ID: id, IsChild: s.isChild}, nil
|
||||
}
|
||||
|
||||
// fakeResolverAsProfileResolver implements the profileResolver interface used
|
||||
// by NotificationsHandler.
|
||||
type fakeResolverAsProfileResolver struct {
|
||||
isChild bool
|
||||
}
|
||||
|
||||
func (f *fakeResolverAsProfileResolver) ForUser(_ context.Context, _ int) (userstore.UserStore, error) {
|
||||
return &fakeProfileStore{isChild: f.isChild}, nil
|
||||
}
|
||||
|
||||
// fakeResolverWithError returns an error from ForUser.
|
||||
type fakeResolverWithError struct{}
|
||||
|
||||
func (f *fakeResolverWithError) ForUser(_ context.Context, _ int) (userstore.UserStore, error) {
|
||||
return nil, errors.New("resolver failed")
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fake store implementing notifications.Store for handler tests.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type fakeNotificationsStore struct {
|
||||
notifications.Store // embed for unimplemented panics
|
||||
|
||||
list []*notifications.Notification
|
||||
announcements []*notifications.Announcement
|
||||
unreadCount int
|
||||
prefs map[notifications.Category]bool
|
||||
markReadCalled bool
|
||||
markAllReadCalled bool
|
||||
dismissErr error
|
||||
setPrefErr error
|
||||
deleteAnnouncementErr error
|
||||
insertCalled bool
|
||||
|
||||
// capturedFilter records the most recent ListFilter passed to List.
|
||||
capturedFilter *notifications.ListFilter
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) Insert(_ context.Context, n *notifications.Notification) (bool, error) {
|
||||
f.insertCalled = true
|
||||
f.list = append(f.list, n)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) List(_ context.Context, filter notifications.ListFilter) ([]*notifications.Notification, error) {
|
||||
f.capturedFilter = &filter
|
||||
return f.list, nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) UnreadCount(_ context.Context, _ int, _ string, _ bool) (int, error) {
|
||||
return f.unreadCount, nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) MarkRead(_ context.Context, _ int, _ string, _ []int64) error {
|
||||
f.markReadCalled = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) MarkAllRead(_ context.Context, _ int, _ string) error {
|
||||
f.markAllReadCalled = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) Dismiss(_ context.Context, _ int, _ string, _ int64) error {
|
||||
return f.dismissErr
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) Preferences(_ context.Context, _ int) (map[notifications.Category]bool, error) {
|
||||
if f.prefs != nil {
|
||||
return f.prefs, nil
|
||||
}
|
||||
return map[notifications.Category]bool{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) SetPreferences(_ context.Context, _ int, _ []notifications.Preference) error {
|
||||
return f.setPrefErr
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) InsertAnnouncement(_ context.Context, a *notifications.Announcement) error {
|
||||
a.ID = int64(len(f.announcements) + 1)
|
||||
a.CreatedAt = time.Now().UTC()
|
||||
f.announcements = append(f.announcements, a)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) ListAnnouncements(_ context.Context) ([]*notifications.Announcement, error) {
|
||||
return f.announcements, nil
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) DeleteAnnouncement(_ context.Context, id int64) error {
|
||||
if f.deleteAnnouncementErr != nil {
|
||||
return f.deleteAnnouncementErr
|
||||
}
|
||||
for i, a := range f.announcements {
|
||||
if a.ID == id {
|
||||
f.announcements = append(f.announcements[:i], f.announcements[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return notifications.ErrNotFound
|
||||
}
|
||||
|
||||
func (f *fakeNotificationsStore) DismissAnnouncementNotifications(_ context.Context, _ int64) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeNotificationsStore) ProfileIDsForUsers(_ context.Context, _ []int) (map[int][]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeNotificationsStore) PurgeOld(_ context.Context, _, _ time.Time) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
func (f *fakeNotificationsStore) AdminUserIDs(_ context.Context) ([]int, error) { return nil, nil }
|
||||
func (f *fakeNotificationsStore) UserIDsWithLibraryAccess(_ context.Context, _ int) ([]int, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeNotificationsStore) AllEnabledUserIDs(_ context.Context) ([]int, error) { return nil, nil }
|
||||
|
||||
// Compile-time check.
|
||||
var _ notifications.Store = (*fakeNotificationsStore)(nil)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helper: build a handler over the fake store.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func newTestNotificationsHandler(store *fakeNotificationsStore) *NotificationsHandler {
|
||||
hub := evt.NewHub("test", nil)
|
||||
svc := notifications.NewService(store, hub)
|
||||
return NewNotificationsHandler(svc)
|
||||
}
|
||||
|
||||
func newTestNotificationsHandlerWithResolver(store *fakeNotificationsStore, resolver profileResolver) *NotificationsHandler {
|
||||
hub := evt.NewHub("test", nil)
|
||||
svc := notifications.NewService(store, hub)
|
||||
return NewNotificationsHandler(svc, resolver)
|
||||
}
|
||||
|
||||
// notifUserRequest creates an authenticated request with a profile ID.
|
||||
func notifUserRequest(method, target string, body []byte, userID int, profileID string) *http.Request {
|
||||
var req *http.Request
|
||||
if len(body) > 0 {
|
||||
req = httptest.NewRequest(method, target, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
req = httptest.NewRequest(method, target, nil)
|
||||
}
|
||||
ctx := apimw.SetClaims(req.Context(), &auth.Claims{UserID: userID, Role: "user"})
|
||||
ctx = apimw.SetProfileID(ctx, profileID)
|
||||
return req.WithContext(ctx)
|
||||
}
|
||||
|
||||
// notifAdminRequest creates an admin-authenticated request.
|
||||
func notifAdminRequest(method, target string, body []byte, userID int) *http.Request {
|
||||
var req *http.Request
|
||||
if len(body) > 0 {
|
||||
req = httptest.NewRequest(method, target, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
req = httptest.NewRequest(method, target, nil)
|
||||
}
|
||||
ctx := apimw.SetClaims(req.Context(), &auth.Claims{UserID: userID, Role: "admin"})
|
||||
return req.WithContext(ctx)
|
||||
}
|
||||
|
||||
// notifChiRequest adds a chi URL param to a request context.
|
||||
func notifChiRequest(r *http.Request, key, value string) *http.Request {
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add(key, value)
|
||||
return r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleList
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleList_HappyPath_TwoRowsNoCursor(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
store := &fakeNotificationsStore{
|
||||
list: []*notifications.Notification{
|
||||
{ID: 2, UserID: 1, Category: notifications.CategoryContent, Type: "content.added", Title: "New Movie", CreatedAt: now},
|
||||
{ID: 1, UserID: 1, Category: notifications.CategoryRequest, Type: "request.approved", Title: "Approved", CreatedAt: now.Add(-time.Minute)},
|
||||
},
|
||||
}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications", nil, 1, "prof-1")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Items []*notifications.Notification `json:"items"`
|
||||
NextCursor *int64 `json:"next_cursor"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if len(resp.Items) != 2 {
|
||||
t.Fatalf("items count = %d, want 2", len(resp.Items))
|
||||
}
|
||||
// len < limit (default 50), so next_cursor must be null.
|
||||
if resp.NextCursor != nil {
|
||||
t.Fatalf("next_cursor = %v, want nil (items < limit)", resp.NextCursor)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleUnreadCount
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleUnreadCount_ReturnsCount(t *testing.T) {
|
||||
store := &fakeNotificationsStore{unreadCount: 7}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications/unread-count", nil, 1, "prof-1")
|
||||
h.HandleUnreadCount(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp.Count != 7 {
|
||||
t.Fatalf("count = %d, want 7", resp.Count)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleMarkRead
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleMarkRead_EmptyBody_Returns400(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPost, "/notifications/read", []byte(`{}`), 1, "prof-1")
|
||||
h.HandleMarkRead(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleMarkRead_AllTrue_Returns204(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPost, "/notifications/read", []byte(`{"all":true}`), 1, "prof-1")
|
||||
h.HandleMarkRead(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !store.markAllReadCalled {
|
||||
t.Fatal("MarkAllRead was not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleMarkRead_IDs_Returns204(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPost, "/notifications/read", []byte(`{"ids":[1,2,3]}`), 1, "prof-1")
|
||||
h.HandleMarkRead(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !store.markReadCalled {
|
||||
t.Fatal("MarkRead was not called")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleDismiss
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleDismiss_NotFound_Returns404(t *testing.T) {
|
||||
store := &fakeNotificationsStore{dismissErr: notifications.ErrNotFound}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPost, "/notifications/999/dismiss", nil, 1, "prof-1")
|
||||
req = notifChiRequest(req, "id", "999")
|
||||
h.HandleDismiss(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleDismiss_BadID_Returns400(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPost, "/notifications/abc/dismiss", nil, 1, "prof-1")
|
||||
req = notifChiRequest(req, "id", "abc")
|
||||
h.HandleDismiss(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandleGetPreferences
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleGetPreferences_DefaultsReturned(t *testing.T) {
|
||||
// No stored prefs → service returns all MutableCategories with defaults.
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications/preferences", nil, 1, "prof-1")
|
||||
h.HandleGetPreferences(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Preferences []notifications.Preference `json:"preferences"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Preferences) != len(notifications.MutableCategories) {
|
||||
t.Fatalf("prefs count = %d, want %d", len(resp.Preferences), len(notifications.MutableCategories))
|
||||
}
|
||||
|
||||
prefMap := make(map[notifications.Category]bool)
|
||||
for _, p := range resp.Preferences {
|
||||
prefMap[p.Category] = p.Enabled
|
||||
}
|
||||
|
||||
// content_digest defaults to false (opt-in); all others default to true.
|
||||
for _, cat := range notifications.MutableCategories {
|
||||
enabled, ok := prefMap[cat]
|
||||
if !ok {
|
||||
t.Errorf("category %q missing from preferences", cat)
|
||||
continue
|
||||
}
|
||||
if cat == notifications.CategoryContentDigest {
|
||||
if enabled {
|
||||
t.Errorf("content_digest default should be false (opt-in), got true")
|
||||
}
|
||||
} else {
|
||||
if !enabled {
|
||||
t.Errorf("category %q default should be true, got false", cat)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: HandlePutPreferences
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandlePutPreferences_RejectsAnnouncementCategory_400(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
// "announcement" is not in MutableCategories.
|
||||
body := `{"preferences":[{"category":"announcement","enabled":false}]}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPut, "/notifications/preferences", []byte(body), 1, "prof-1")
|
||||
h.HandlePutPreferences(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandlePutPreferences_ValidPrefs_204(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
body := `{"preferences":[{"category":"request","enabled":false},{"category":"content","enabled":true}]}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodPut, "/notifications/preferences", []byte(body), 1, "prof-1")
|
||||
h.HandlePutPreferences(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: Admin announcements
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestNotificationsHandleCreateAnnouncement_201(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
body := `{"title":"Test Announcement","body":"Hello","audience":{"all":true}}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifAdminRequest(http.MethodPost, "/admin/announcements", []byte(body), 1)
|
||||
h.HandleCreateAnnouncement(rec, req)
|
||||
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("status = %d, want 201; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp notifications.Announcement
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp.Title != "Test Announcement" {
|
||||
t.Fatalf("title = %q, want %q", resp.Title, "Test Announcement")
|
||||
}
|
||||
if resp.CreatedBy == nil || *resp.CreatedBy != 1 {
|
||||
t.Fatalf("created_by = %v, want 1", resp.CreatedBy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleCreateAnnouncement_ValidationError_400(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
// Empty title triggers service validation error.
|
||||
body := `{"title":"","body":"Hello","audience":{"all":true}}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifAdminRequest(http.MethodPost, "/admin/announcements", []byte(body), 1)
|
||||
h.HandleCreateAnnouncement(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleDeleteAnnouncement_204(t *testing.T) {
|
||||
store := &fakeNotificationsStore{
|
||||
announcements: []*notifications.Announcement{
|
||||
{ID: 5, Title: "To Delete", Audience: notifications.Audience{All: true}},
|
||||
},
|
||||
}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifAdminRequest(http.MethodDelete, "/admin/announcements/5", nil, 1)
|
||||
req = notifChiRequest(req, "id", "5")
|
||||
h.HandleDeleteAnnouncement(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleDeleteAnnouncement_NotFound_404(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifAdminRequest(http.MethodDelete, "/admin/announcements/999", nil, 1)
|
||||
req = notifChiRequest(req, "id", "999")
|
||||
h.HandleDeleteAnnouncement(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotificationsHandleListAnnouncements_ReturnsItems(t *testing.T) {
|
||||
store := &fakeNotificationsStore{
|
||||
announcements: []*notifications.Announcement{
|
||||
{ID: 1, Title: "Hello", Audience: notifications.Audience{All: true}},
|
||||
},
|
||||
}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifAdminRequest(http.MethodGet, "/admin/announcements", nil, 1)
|
||||
h.HandleListAnnouncements(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Items []*notifications.Announcement `json:"items"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Items) != 1 {
|
||||
t.Fatalf("items count = %d, want 1", len(resp.Items))
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests: ChildSafe profile filtering
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// TestNotificationsList_ChildProfileFiltered asserts that when the profile
|
||||
// resolver reports IsChild=true, the ListFilter passed to the store has
|
||||
// ChildSafe=true.
|
||||
func TestNotificationsList_ChildProfileFiltered(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
resolver := &fakeResolverAsProfileResolver{isChild: true}
|
||||
h := newTestNotificationsHandlerWithResolver(store, resolver)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications", nil, 1, "prof-child")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if store.capturedFilter == nil {
|
||||
t.Fatal("List was not called; capturedFilter is nil")
|
||||
}
|
||||
if !store.capturedFilter.ChildSafe {
|
||||
t.Fatalf("capturedFilter.ChildSafe = false, want true for child profile")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotificationsList_AdultProfileNotFiltered asserts that a non-child
|
||||
// profile leaves ChildSafe=false in the ListFilter.
|
||||
func TestNotificationsList_AdultProfileNotFiltered(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
resolver := &fakeResolverAsProfileResolver{isChild: false}
|
||||
h := newTestNotificationsHandlerWithResolver(store, resolver)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications", nil, 1, "prof-adult")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if store.capturedFilter == nil {
|
||||
t.Fatal("List was not called; capturedFilter is nil")
|
||||
}
|
||||
if store.capturedFilter.ChildSafe {
|
||||
t.Fatalf("capturedFilter.ChildSafe = true, want false for non-child profile")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotificationsList_NilResolverDefaultsToFalse asserts that when no
|
||||
// resolver is wired (nil), ChildSafe remains false.
|
||||
func TestNotificationsList_NilResolverDefaultsToFalse(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
h := newTestNotificationsHandler(store) // no resolver
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications", nil, 1, "prof-1")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if store.capturedFilter == nil {
|
||||
t.Fatal("List was not called; capturedFilter is nil")
|
||||
}
|
||||
if store.capturedFilter.ChildSafe {
|
||||
t.Fatalf("capturedFilter.ChildSafe = true, want false when no resolver is wired")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotificationsList_ResolverErrorFailsClosed asserts that when the resolver
|
||||
// returns an error, ChildSafe=true (fail closed).
|
||||
func TestNotificationsList_ResolverErrorFailsClosed(t *testing.T) {
|
||||
store := &fakeNotificationsStore{}
|
||||
resolver := &fakeResolverWithError{}
|
||||
h := newTestNotificationsHandlerWithResolver(store, resolver)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications", nil, 1, "prof-1")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if store.capturedFilter == nil {
|
||||
t.Fatal("List was not called; capturedFilter is nil")
|
||||
}
|
||||
if !store.capturedFilter.ChildSafe {
|
||||
t.Fatalf("capturedFilter.ChildSafe = false, want true (fail closed on resolver error)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNotificationsList_NextCursorSet asserts that when limit=2 and the store
|
||||
// returns exactly 2 notifications, next_cursor is set to the last item's ID.
|
||||
func TestNotificationsList_NextCursorSet(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
store := &fakeNotificationsStore{
|
||||
list: []*notifications.Notification{
|
||||
{ID: 12, UserID: 1, Category: notifications.CategoryContent, Type: "content.added", Title: "New Movie", CreatedAt: now},
|
||||
{ID: 11, UserID: 1, Category: notifications.CategoryRequest, Type: "request.approved", Title: "Approved", CreatedAt: now.Add(-time.Minute)},
|
||||
},
|
||||
}
|
||||
h := newTestNotificationsHandler(store)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
req := notifUserRequest(http.MethodGet, "/notifications?limit=2", nil, 1, "prof-1")
|
||||
h.HandleList(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Items []*notifications.Notification `json:"items"`
|
||||
NextCursor *int64 `json:"next_cursor"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if len(resp.Items) != 2 {
|
||||
t.Fatalf("items count = %d, want 2", len(resp.Items))
|
||||
}
|
||||
// With limit=2 and 2 items returned, next_cursor should be set to the last item's ID (11).
|
||||
if resp.NextCursor == nil {
|
||||
t.Fatal("next_cursor is nil, want 11")
|
||||
}
|
||||
if *resp.NextCursor != 11 {
|
||||
t.Fatalf("next_cursor = %d, want 11", *resp.NextCursor)
|
||||
}
|
||||
}
|
||||
|
||||
// Suppress unused import.
|
||||
var _ = strings.Contains
|
||||
@@ -0,0 +1,221 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// webhookResponse is the API view of a webhook. It never includes the
|
||||
// destination URL (Discord webhook tokens are bearer credentials in the URL
|
||||
// path) or the signing secret — only url_host for identification.
|
||||
type webhookResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
URLHost string `json:"url_host"`
|
||||
Enabled bool `json:"enabled"`
|
||||
NotifyFavorites bool `json:"notify_favorites"`
|
||||
NotifyWatchlist bool `json:"notify_watchlist"`
|
||||
NotifyContinueWatching bool `json:"notify_continue_watching"`
|
||||
NotifyNextUp bool `json:"notify_next_up"`
|
||||
NotifyRequests bool `json:"notify_requests"`
|
||||
ConsecutiveFailures int `json:"consecutive_failures"`
|
||||
DisabledReason *string `json:"disabled_reason"`
|
||||
LastSuccessAt *time.Time `json:"last_success_at"`
|
||||
LastFailureAt *time.Time `json:"last_failure_at"`
|
||||
LastFailureStatus *int `json:"last_failure_status"`
|
||||
LastFailureMessage *string `json:"last_failure_message"`
|
||||
// SigningSecret is present only in create / rotate-secret responses.
|
||||
SigningSecret string `json:"signing_secret,omitempty"`
|
||||
}
|
||||
|
||||
func webhookToResponse(hook notifications.Webhook) webhookResponse {
|
||||
return webhookResponse{
|
||||
ID: hook.ID,
|
||||
Name: hook.Name,
|
||||
Type: hook.Type,
|
||||
URLHost: hook.URLHost,
|
||||
Enabled: hook.Enabled,
|
||||
NotifyFavorites: hook.NotifyFavorites,
|
||||
NotifyWatchlist: hook.NotifyWatchlist,
|
||||
NotifyContinueWatching: hook.NotifyContinueWatching,
|
||||
NotifyNextUp: hook.NotifyNextUp,
|
||||
NotifyRequests: hook.NotifyRequests,
|
||||
ConsecutiveFailures: hook.ConsecutiveFailures,
|
||||
DisabledReason: hook.DisabledReason,
|
||||
LastSuccessAt: hook.LastSuccessAt,
|
||||
LastFailureAt: hook.LastFailureAt,
|
||||
LastFailureStatus: hook.LastFailureStatus,
|
||||
LastFailureMessage: hook.LastFailureMessage,
|
||||
}
|
||||
}
|
||||
|
||||
type webhookRequest struct {
|
||||
Name *string `json:"name"`
|
||||
URL *string `json:"url"`
|
||||
Type *string `json:"type"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
NotifyFavorites *bool `json:"notify_favorites"`
|
||||
NotifyWatchlist *bool `json:"notify_watchlist"`
|
||||
NotifyContinueWatching *bool `json:"notify_continue_watching"`
|
||||
NotifyNextUp *bool `json:"notify_next_up"`
|
||||
NotifyRequests *bool `json:"notify_requests"`
|
||||
}
|
||||
|
||||
func (r webhookRequest) toInput() notifications.WebhookInput {
|
||||
return notifications.WebhookInput{
|
||||
Name: r.Name,
|
||||
URL: r.URL,
|
||||
Type: r.Type,
|
||||
Enabled: r.Enabled,
|
||||
NotifyFavorites: r.NotifyFavorites,
|
||||
NotifyWatchlist: r.NotifyWatchlist,
|
||||
NotifyContinueWatching: r.NotifyContinueWatching,
|
||||
NotifyNextUp: r.NotifyNextUp,
|
||||
NotifyRequests: r.NotifyRequests,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *NotificationsHandler) webhooks() *notifications.WebhookService {
|
||||
if h == nil || h.system == nil {
|
||||
return nil
|
||||
}
|
||||
return h.system.Webhooks
|
||||
}
|
||||
|
||||
func writeWebhookError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, notifications.ErrWebhooksDisabled):
|
||||
writeError(w, http.StatusForbidden, "webhooks_disabled", "Webhooks are disabled by the server administrator")
|
||||
case errors.Is(err, notifications.ErrWebhookNotFound):
|
||||
writeError(w, http.StatusNotFound, "not_found", "Webhook not found")
|
||||
case errors.Is(err, notifications.ErrWebhookLimit):
|
||||
writeError(w, http.StatusUnprocessableEntity, "limit_reached", "Webhook limit reached for this profile")
|
||||
case errors.Is(err, notifications.ErrWebhookInvalid):
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Webhook operation failed")
|
||||
}
|
||||
}
|
||||
|
||||
// HandleListWebhooks handles GET /notifications/webhooks.
|
||||
func (h *NotificationsHandler) HandleListWebhooks(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
hooks, err := service.List(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list webhooks")
|
||||
return
|
||||
}
|
||||
responses := make([]webhookResponse, 0, len(hooks))
|
||||
for _, hook := range hooks {
|
||||
responses = append(responses, webhookToResponse(hook))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"webhooks": responses})
|
||||
}
|
||||
|
||||
// HandleCreateWebhook handles POST /notifications/webhooks. For generic
|
||||
// webhooks the response carries the signing secret exactly once.
|
||||
func (h *NotificationsHandler) HandleCreateWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req webhookRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
hook, signingSecret, err := service.Create(r.Context(), userID, profileID, req.toInput())
|
||||
if err != nil {
|
||||
writeWebhookError(w, err)
|
||||
return
|
||||
}
|
||||
response := webhookToResponse(*hook)
|
||||
response.SigningSecret = signingSecret
|
||||
writeJSON(w, http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// HandleUpdateWebhook handles PUT /notifications/webhooks/{id}.
|
||||
func (h *NotificationsHandler) HandleUpdateWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req webhookRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
hook, err := service.Update(r.Context(), profileID, chi.URLParam(r, "id"), req.toInput())
|
||||
if err != nil {
|
||||
writeWebhookError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, webhookToResponse(*hook))
|
||||
}
|
||||
|
||||
// HandleDeleteWebhook handles DELETE /notifications/webhooks/{id}. Idempotent.
|
||||
func (h *NotificationsHandler) HandleDeleteWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
if err := service.Delete(r.Context(), profileID, chi.URLParam(r, "id")); err != nil {
|
||||
writeWebhookError(w, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleRotateWebhookSecret handles POST /notifications/webhooks/{id}/rotate-secret.
|
||||
func (h *NotificationsHandler) HandleRotateWebhookSecret(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
signingSecret, err := service.RotateSecret(r.Context(), profileID, chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
writeWebhookError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"signing_secret": signingSecret})
|
||||
}
|
||||
|
||||
// HandleTestWebhook handles POST /notifications/webhooks/{id}/test. The test
|
||||
// send is synchronous and never touches the retry/auto-disable counters.
|
||||
func (h *NotificationsHandler) HandleTestWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webhooks()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Webhooks are not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
result, err := service.Test(r.Context(), profileID, chi.URLParam(r, "id"))
|
||||
if err != nil {
|
||||
writeWebhookError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, result)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/notifications"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// webPushSubscriptionResponse is the API view of a browser push
|
||||
// registration. The keys are write-only: clients re-subscribe rather than
|
||||
// read them back.
|
||||
type webPushSubscriptionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
DeviceName string `json:"device_name,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
LastSuccessAt *time.Time `json:"last_success_at"`
|
||||
LastFailureAt *time.Time `json:"last_failure_at"`
|
||||
}
|
||||
|
||||
func webPushToResponse(sub notifications.WebPushSubscription) webPushSubscriptionResponse {
|
||||
return webPushSubscriptionResponse{
|
||||
ID: sub.ID,
|
||||
Endpoint: sub.Endpoint,
|
||||
DeviceName: sub.DeviceName,
|
||||
Enabled: sub.Enabled,
|
||||
CreatedAt: sub.CreatedAt,
|
||||
LastSuccessAt: sub.LastSuccessAt,
|
||||
LastFailureAt: sub.LastFailureAt,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *NotificationsHandler) webPush() *notifications.WebPushService {
|
||||
if h == nil || h.system == nil {
|
||||
return nil
|
||||
}
|
||||
return h.system.WebPush
|
||||
}
|
||||
|
||||
type webPushSubscribeRequest struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Keys struct {
|
||||
P256dh string `json:"p256dh"`
|
||||
Auth string `json:"auth"`
|
||||
} `json:"keys"`
|
||||
DeviceName string `json:"device_name"`
|
||||
}
|
||||
|
||||
// HandleWebPushSubscribe handles POST /notifications/web-push/subscriptions.
|
||||
// The body matches PushSubscription.toJSON() plus an optional device name.
|
||||
func (h *NotificationsHandler) HandleWebPushSubscribe(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webPush()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Web push is not available")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
|
||||
var req webPushSubscribeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
sub, err := service.Subscribe(r.Context(), userID, profileID,
|
||||
req.Endpoint, req.Keys.P256dh, req.Keys.Auth, req.DeviceName)
|
||||
if err != nil {
|
||||
if errors.Is(err, notifications.ErrWebPushInvalid) {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to register push subscription")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, webPushToResponse(*sub))
|
||||
}
|
||||
|
||||
// HandleWebPushList handles GET /notifications/web-push/subscriptions.
|
||||
func (h *NotificationsHandler) HandleWebPushList(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webPush()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Web push is not available")
|
||||
return
|
||||
}
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
subs, err := service.List(r.Context(), profileID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list push subscriptions")
|
||||
return
|
||||
}
|
||||
responses := make([]webPushSubscriptionResponse, 0, len(subs))
|
||||
for _, sub := range subs {
|
||||
responses = append(responses, webPushToResponse(sub))
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"subscriptions": responses})
|
||||
}
|
||||
|
||||
// HandleWebPushDelete handles DELETE /notifications/web-push/subscriptions/{id}.
|
||||
func (h *NotificationsHandler) HandleWebPushDelete(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webPush()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Web push is not available")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
if err := service.Unsubscribe(r.Context(), userID, profileID, chi.URLParam(r, "id"), ""); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to remove push subscription")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
type webPushUnsubscribeRequest struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
}
|
||||
|
||||
// HandleWebPushUnsubscribe handles POST /notifications/web-push/unsubscribe.
|
||||
// Browsers only know their endpoint, not the server-side row ID. Idempotent.
|
||||
func (h *NotificationsHandler) HandleWebPushUnsubscribe(w http.ResponseWriter, r *http.Request) {
|
||||
service := h.webPush()
|
||||
if service == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "Web push is not available")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
profileID := apimw.GetProfileID(r.Context())
|
||||
var req webPushUnsubscribeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Endpoint == "" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "An endpoint is required")
|
||||
return
|
||||
}
|
||||
if err := service.Unsubscribe(r.Context(), userID, profileID, "", req.Endpoint); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to remove push subscription")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -40,6 +40,7 @@ type PluginHandler struct {
|
||||
chainRepo *metadata.ChainRepository
|
||||
imageResolver *metadata.PluginImageResolver
|
||||
uploads *uploads.Manager
|
||||
restartStatus *ServerRestartStatusTracker
|
||||
}
|
||||
|
||||
func NewPluginHandler(
|
||||
@@ -51,6 +52,7 @@ func NewPluginHandler(
|
||||
proxy *plugins.HTTPProxy,
|
||||
chainRepo *metadata.ChainRepository,
|
||||
imageResolver *metadata.PluginImageResolver,
|
||||
restartStatus *ServerRestartStatusTracker,
|
||||
) *PluginHandler {
|
||||
return &PluginHandler{
|
||||
repositories: repositories,
|
||||
@@ -61,6 +63,7 @@ func NewPluginHandler(
|
||||
proxy: proxy,
|
||||
chainRepo: chainRepo,
|
||||
imageResolver: imageResolver,
|
||||
restartStatus: restartStatus,
|
||||
uploads: uploads.NewManager(uploads.ManagerOptions{
|
||||
MaxSize: maxPluginUploadSize,
|
||||
MaxChunkSize: maxPluginUploadChunkSize,
|
||||
@@ -1037,6 +1040,7 @@ func (h *PluginHandler) HandlePutTaskBinding(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
h.restartStatus.MarkRequired("plugin_task_binding")
|
||||
writeJSON(w, http.StatusOK, pluginTaskBindingUpdateResponse{RestartRequired: true})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,234 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
webpush "github.com/SherClockHolmes/webpush-go"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/push"
|
||||
)
|
||||
|
||||
// maxPushTokenLen bounds a registered push token. APNs/FCM device tokens are
|
||||
// short; a Web Push subscription JSON (endpoint + keys) is ~1 KB.
|
||||
const maxPushTokenLen = 8192
|
||||
|
||||
// pushRegistry is the minimal interface PushHandler needs from *push.Store.
|
||||
type pushRegistry interface {
|
||||
RegisterToken(ctx context.Context, userID int, profileID, deviceID, transport, token string) error
|
||||
RevokeToken(ctx context.Context, userID int, profileID, deviceID string) error
|
||||
SetDeviceEnabled(ctx context.Context, userID int, deviceID string, enabled bool) error
|
||||
ListDevices(ctx context.Context, userID int) ([]push.DeviceInfo, error)
|
||||
}
|
||||
|
||||
// pushConfigReader is the minimal interface PushHandler needs from *push.Config.
|
||||
type pushConfigReader interface {
|
||||
WebPush(ctx context.Context) push.WebPushConfig
|
||||
Status(ctx context.Context) push.Status
|
||||
}
|
||||
|
||||
// pushNotifier is the minimal interface PushHandler needs to send system notifications.
|
||||
type pushNotifier interface {
|
||||
CreateSystem(ctx context.Context, userID int, typ, title, body string)
|
||||
}
|
||||
|
||||
// PushHandler handles push notification registration, management, and status endpoints.
|
||||
type PushHandler struct {
|
||||
reg pushRegistry
|
||||
config pushConfigReader
|
||||
notifier pushNotifier
|
||||
}
|
||||
|
||||
// NewPushHandler creates a new PushHandler. notifier may be nil (test-push endpoint
|
||||
// returns 503 when nil).
|
||||
func NewPushHandler(reg pushRegistry, config pushConfigReader, notifier pushNotifier) *PushHandler {
|
||||
return &PushHandler{reg: reg, config: config, notifier: notifier}
|
||||
}
|
||||
|
||||
// registerRequest is the request body for HandleRegister.
|
||||
type registerRequest struct {
|
||||
Transport string `json:"transport"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// deviceContext extracts the authenticated user, device id, and active profile
|
||||
// required by the profile-scoped push device endpoints. It writes a 400 and
|
||||
// returns ok=false when any of the three is missing.
|
||||
func (h *PushHandler) deviceContext(w http.ResponseWriter, r *http.Request) (userID int, deviceID, profileID string, ok bool) {
|
||||
userID = apimw.GetUserID(r.Context())
|
||||
if userID == 0 {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Authentication required")
|
||||
return 0, "", "", false
|
||||
}
|
||||
deviceID = strings.TrimSpace(r.Header.Get(deviceIDHeader))
|
||||
if deviceID == "" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "X-Silo-Device-Id header is required")
|
||||
return 0, "", "", false
|
||||
}
|
||||
profileID = strings.TrimSpace(apimw.GetProfileID(r.Context()))
|
||||
if profileID == "" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "X-Profile-Id header is required")
|
||||
return 0, "", "", false
|
||||
}
|
||||
return userID, deviceID, profileID, true
|
||||
}
|
||||
|
||||
// HandleRegister handles PUT /notifications/push/device.
|
||||
// Requires userID (from JWT claims), X-Silo-Device-Id, and X-Profile-Id headers.
|
||||
// Body: {"transport": "apns"|"fcm"|"webpush", "token": "..."}
|
||||
// Returns 204 on success.
|
||||
func (h *PushHandler) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
userID, deviceID, profileID, ok := h.deviceContext(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 16*1024)
|
||||
var req registerRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
switch req.Transport {
|
||||
case push.TransportAPNs, push.TransportFCM, push.TransportWebPush:
|
||||
// valid
|
||||
default:
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "transport must be one of apns, fcm, webpush")
|
||||
return
|
||||
}
|
||||
if req.Token == "" {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "token must not be empty")
|
||||
return
|
||||
}
|
||||
// Cap token length: APNs/FCM device tokens are short; a Web Push subscription
|
||||
// JSON (endpoint + keys) is ~1 KB. 8 KB is generous and bounds the stored row.
|
||||
if len(req.Token) > maxPushTokenLen {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "token too large")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.reg.RegisterToken(r.Context(), userID, profileID, deviceID, req.Transport, req.Token); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to register push token")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleRevoke handles DELETE /notifications/push/device.
|
||||
// Requires userID, X-Silo-Device-Id, and X-Profile-Id headers.
|
||||
// Returns 204 on success.
|
||||
func (h *PushHandler) HandleRevoke(w http.ResponseWriter, r *http.Request) {
|
||||
userID, deviceID, profileID, ok := h.deviceContext(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.reg.RevokeToken(r.Context(), userID, profileID, deviceID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to revoke push token")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleListDevices handles GET /notifications/push/devices.
|
||||
// Response: {"devices": [...]}
|
||||
func (h *PushHandler) HandleListDevices(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
|
||||
devices, err := h.reg.ListDevices(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list devices")
|
||||
return
|
||||
}
|
||||
if devices == nil {
|
||||
devices = []push.DeviceInfo{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Devices []push.DeviceInfo `json:"devices"`
|
||||
}{Devices: devices})
|
||||
}
|
||||
|
||||
// toggleDeviceRequest is the request body for HandleToggleDevice.
|
||||
type toggleDeviceRequest struct {
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// HandleToggleDevice handles PUT /notifications/push/devices/{device_id}.
|
||||
// Body: {"enabled": true|false}
|
||||
// Returns 204 on success, 404 if device not found.
|
||||
func (h *PushHandler) HandleToggleDevice(w http.ResponseWriter, r *http.Request) {
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
deviceID := chi.URLParam(r, "device_id")
|
||||
|
||||
var req toggleDeviceRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
||||
return
|
||||
}
|
||||
if req.Enabled == nil {
|
||||
writeError(w, http.StatusBadRequest, "bad_request", "enabled is required")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.reg.SetDeviceEnabled(r.Context(), userID, deviceID, *req.Enabled); err != nil {
|
||||
if errors.Is(err, push.ErrNotFound) {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Device not found")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to update device")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// HandleWebPushKey handles GET /notifications/push/webpush-key.
|
||||
// Response: {"vapid_public_key": "..."} — empty string when not configured.
|
||||
func (h *PushHandler) HandleWebPushKey(w http.ResponseWriter, r *http.Request) {
|
||||
cfg := h.config.WebPush(r.Context())
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
VAPIDPublicKey string `json:"vapid_public_key"`
|
||||
}{VAPIDPublicKey: cfg.VAPIDPublic})
|
||||
}
|
||||
|
||||
// HandleAdminStatus handles GET /admin/push/status.
|
||||
// Response: {"apns": bool, "fcm": bool, "webpush": bool}
|
||||
func (h *PushHandler) HandleAdminStatus(w http.ResponseWriter, r *http.Request) {
|
||||
status := h.config.Status(r.Context())
|
||||
writeJSON(w, http.StatusOK, status)
|
||||
}
|
||||
|
||||
// HandleGenerateVAPIDKeys handles POST /admin/push/generate-vapid-keys.
|
||||
// Generates a fresh VAPID key pair and returns it as JSON.
|
||||
// Response: {"vapid_public": "...", "vapid_private": "..."}
|
||||
func (h *PushHandler) HandleGenerateVAPIDKeys(w http.ResponseWriter, r *http.Request) {
|
||||
// GenerateVAPIDKeys returns (privateKey, publicKey string, err error).
|
||||
priv, pub, err := webpush.GenerateVAPIDKeys()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "failed to generate keys")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"vapid_public": pub, "vapid_private": priv})
|
||||
}
|
||||
|
||||
// HandleSendTestPush handles POST /admin/push/test.
|
||||
// Sends a system test push notification to the authenticated admin user.
|
||||
func (h *PushHandler) HandleSendTestPush(w http.ResponseWriter, r *http.Request) {
|
||||
if h.notifier == nil {
|
||||
writeError(w, http.StatusServiceUnavailable, "unavailable", "notifications unavailable")
|
||||
return
|
||||
}
|
||||
userID := apimw.GetUserID(r.Context())
|
||||
if userID == 0 {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized", "auth required")
|
||||
return
|
||||
}
|
||||
h.notifier.CreateSystem(r.Context(), userID, "system.test_push", "Test push",
|
||||
"If you can see this on a device, push delivery is working.")
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
@@ -1,496 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
"github.com/Silo-Server/silo-server/internal/push"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fakes
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type fakePushRegistry struct {
|
||||
registerErr error
|
||||
revokeErr error
|
||||
enableErr error
|
||||
devices []push.DeviceInfo
|
||||
|
||||
// capture last RegisterToken call
|
||||
lastUserID int
|
||||
lastProfileID string
|
||||
lastDeviceID string
|
||||
lastTransport string
|
||||
lastToken string
|
||||
}
|
||||
|
||||
func (f *fakePushRegistry) RegisterToken(_ context.Context, userID int, profileID, deviceID, transport, token string) error {
|
||||
f.lastUserID = userID
|
||||
f.lastProfileID = profileID
|
||||
f.lastDeviceID = deviceID
|
||||
f.lastTransport = transport
|
||||
f.lastToken = token
|
||||
return f.registerErr
|
||||
}
|
||||
|
||||
func (f *fakePushRegistry) RevokeToken(_ context.Context, _ int, _, _ string) error {
|
||||
return f.revokeErr
|
||||
}
|
||||
|
||||
func (f *fakePushRegistry) SetDeviceEnabled(_ context.Context, _ int, _ string, _ bool) error {
|
||||
return f.enableErr
|
||||
}
|
||||
|
||||
func (f *fakePushRegistry) ListDevices(_ context.Context, _ int) ([]push.DeviceInfo, error) {
|
||||
return f.devices, nil
|
||||
}
|
||||
|
||||
type fakePushConfig struct {
|
||||
webPush push.WebPushConfig
|
||||
status push.Status
|
||||
}
|
||||
|
||||
func (f *fakePushConfig) WebPush(_ context.Context) push.WebPushConfig { return f.webPush }
|
||||
func (f *fakePushConfig) Status(_ context.Context) push.Status { return f.status }
|
||||
|
||||
type fakeNotifier struct {
|
||||
calls []fakeNotifierCall
|
||||
}
|
||||
|
||||
type fakeNotifierCall struct {
|
||||
userID int
|
||||
typ string
|
||||
title string
|
||||
body string
|
||||
}
|
||||
|
||||
func (f *fakeNotifier) CreateSystem(_ context.Context, userID int, typ, title, body string) {
|
||||
f.calls = append(f.calls, fakeNotifierCall{userID: userID, typ: typ, title: title, body: body})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// pushUserRequest builds a request with JWT claims (userID) and optionally
|
||||
// sets the device-id and profile-id headers.
|
||||
func pushUserRequest(method, target string, body []byte, userID int, profileID, deviceID string) *http.Request {
|
||||
var req *http.Request
|
||||
if len(body) > 0 {
|
||||
req = httptest.NewRequest(method, target, bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
req = httptest.NewRequest(method, target, nil)
|
||||
}
|
||||
ctx := apimw.SetClaims(req.Context(), &auth.Claims{UserID: userID, Role: "user"})
|
||||
if profileID != "" {
|
||||
ctx = apimw.SetProfileID(ctx, profileID)
|
||||
}
|
||||
req = req.WithContext(ctx)
|
||||
if deviceID != "" {
|
||||
req.Header.Set(deviceIDHeader, deviceID)
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
// pushChiRequest injects a chi URL parameter into the request context.
|
||||
func pushChiRequest(r *http.Request, key, value string) *http.Request {
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.URLParams.Add(key, value)
|
||||
return r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
||||
}
|
||||
|
||||
// newPushHandler builds a PushHandler backed by the given fakes.
|
||||
func newPushHandler(reg *fakePushRegistry, cfg *fakePushConfig) *PushHandler {
|
||||
return NewPushHandler(reg, cfg, nil)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleRegister
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleRegister_MissingDeviceID_Returns400(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "apns", "token": "tok"})
|
||||
// no device header
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 1, "prof-1", "")
|
||||
h.HandleRegister(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRegister_MissingProfileID_Returns400(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "apns", "token": "tok"})
|
||||
// device header set but no profile
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 1, "", "dev-1")
|
||||
h.HandleRegister(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRegister_BadTransport_Returns400(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "smoke-signals", "token": "tok"})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 1, "prof-1", "dev-1")
|
||||
h.HandleRegister(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRegister_EmptyToken_Returns400(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "apns", "token": ""})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 1, "prof-1", "dev-1")
|
||||
h.HandleRegister(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRegister_HappyPath_Returns204(t *testing.T) {
|
||||
reg := &fakePushRegistry{}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "fcm", "token": "my-fcm-token"})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 42, "prof-99", "dev-abc")
|
||||
h.HandleRegister(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if reg.lastUserID != 42 {
|
||||
t.Errorf("userID = %d, want 42", reg.lastUserID)
|
||||
}
|
||||
if reg.lastProfileID != "prof-99" {
|
||||
t.Errorf("profileID = %q, want prof-99", reg.lastProfileID)
|
||||
}
|
||||
if reg.lastDeviceID != "dev-abc" {
|
||||
t.Errorf("deviceID = %q, want dev-abc", reg.lastDeviceID)
|
||||
}
|
||||
if reg.lastTransport != "fcm" {
|
||||
t.Errorf("transport = %q, want fcm", reg.lastTransport)
|
||||
}
|
||||
if reg.lastToken != "my-fcm-token" {
|
||||
t.Errorf("token = %q, want my-fcm-token", reg.lastToken)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRegister_WebPushTransport_Returns204(t *testing.T) {
|
||||
reg := &fakePushRegistry{}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]string{"transport": "webpush", "token": "{}"})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/device", body, 1, "prof-1", "dev-1")
|
||||
h.HandleRegister(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleRevoke
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleRevoke_HappyPath_Returns204(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodDelete, "/notifications/push/device", nil, 1, "prof-1", "dev-1")
|
||||
h.HandleRevoke(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleRevoke_MissingDevice_Returns400(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodDelete, "/notifications/push/device", nil, 1, "prof-1", "")
|
||||
h.HandleRevoke(rec, req)
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleListDevices
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleListDevices_ReturnsDevicesJSON(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
reg := &fakePushRegistry{
|
||||
devices: []push.DeviceInfo{
|
||||
{DeviceID: "dev-1", Name: "iPhone", Platform: "ios", Transport: push.TransportAPNs, PushEnabled: true, RegisteredAt: &now},
|
||||
{DeviceID: "dev-2", Name: "Chrome", Platform: "web", Transport: push.TransportWebPush, PushEnabled: false},
|
||||
},
|
||||
}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodGet, "/notifications/push/devices", nil, 5, "prof-1", "")
|
||||
h.HandleListDevices(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
Devices []push.DeviceInfo `json:"devices"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Devices) != 2 {
|
||||
t.Fatalf("devices count = %d, want 2", len(resp.Devices))
|
||||
}
|
||||
if resp.Devices[0].DeviceID != "dev-1" {
|
||||
t.Errorf("devices[0].DeviceID = %q, want dev-1", resp.Devices[0].DeviceID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleListDevices_EmptySlice_ReturnsEmptyArray(t *testing.T) {
|
||||
reg := &fakePushRegistry{devices: nil}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodGet, "/notifications/push/devices", nil, 1, "", "")
|
||||
h.HandleListDevices(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
body := rec.Body.String()
|
||||
// Must produce {"devices":[]} not {"devices":null}
|
||||
if !containsJSON(body, `"devices":[]`) {
|
||||
t.Errorf("expected empty array in response, got: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleToggleDevice
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleToggleDevice_HappyPath_Returns204(t *testing.T) {
|
||||
h := newPushHandler(&fakePushRegistry{}, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]bool{"enabled": true})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/devices/dev-1", body, 1, "", "")
|
||||
req = pushChiRequest(req, "device_id", "dev-1")
|
||||
h.HandleToggleDevice(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want 204; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleToggleDevice_NotFound_Returns404(t *testing.T) {
|
||||
reg := &fakePushRegistry{enableErr: push.ErrNotFound}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]bool{"enabled": false})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/devices/dev-x", body, 1, "", "")
|
||||
req = pushChiRequest(req, "device_id", "dev-x")
|
||||
h.HandleToggleDevice(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleToggleDevice_WrappedNotFound_Returns404(t *testing.T) {
|
||||
reg := &fakePushRegistry{enableErr: errors.Join(errors.New("outer"), push.ErrNotFound)}
|
||||
h := newPushHandler(reg, &fakePushConfig{})
|
||||
rec := httptest.NewRecorder()
|
||||
body := mustMarshal(t, map[string]bool{"enabled": false})
|
||||
req := pushUserRequest(http.MethodPut, "/notifications/push/devices/dev-x", body, 1, "", "")
|
||||
req = pushChiRequest(req, "device_id", "dev-x")
|
||||
h.HandleToggleDevice(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleWebPushKey
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleWebPushKey_ReturnsConfiguredKey(t *testing.T) {
|
||||
cfg := &fakePushConfig{webPush: push.WebPushConfig{VAPIDPublic: "BPublicKey123", VAPIDPrivate: "priv", Subject: "mailto:a@b.com"}}
|
||||
h := newPushHandler(&fakePushRegistry{}, cfg)
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodGet, "/notifications/push/webpush-key", nil, 1, "", "")
|
||||
h.HandleWebPushKey(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
VAPIDPublicKey string `json:"vapid_public_key"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp.VAPIDPublicKey != "BPublicKey123" {
|
||||
t.Errorf("vapid_public_key = %q, want BPublicKey123", resp.VAPIDPublicKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleWebPushKey_Unconfigured_ReturnsEmptyString(t *testing.T) {
|
||||
cfg := &fakePushConfig{webPush: push.WebPushConfig{}}
|
||||
h := newPushHandler(&fakePushRegistry{}, cfg)
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodGet, "/notifications/push/webpush-key", nil, 0, "", "")
|
||||
h.HandleWebPushKey(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var resp struct {
|
||||
VAPIDPublicKey string `json:"vapid_public_key"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp.VAPIDPublicKey != "" {
|
||||
t.Errorf("vapid_public_key = %q, want empty string", resp.VAPIDPublicKey)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleAdminStatus
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleAdminStatus_ReturnsBooleans(t *testing.T) {
|
||||
cfg := &fakePushConfig{status: push.Status{APNs: true, FCM: false, WebPush: true}}
|
||||
h := newPushHandler(&fakePushRegistry{}, cfg)
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodGet, "/admin/push/status", nil, 1, "", "")
|
||||
h.HandleAdminStatus(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
var resp push.Status
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if !resp.APNs {
|
||||
t.Error("apns = false, want true")
|
||||
}
|
||||
if resp.FCM {
|
||||
t.Error("fcm = true, want false")
|
||||
}
|
||||
if !resp.WebPush {
|
||||
t.Error("webpush = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleGenerateVAPIDKeys
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleGenerateVAPIDKeys_ReturnsPair(t *testing.T) {
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/push/generate-vapid-keys", nil)
|
||||
h.HandleGenerateVAPIDKeys(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp struct {
|
||||
VAPIDPublic string `json:"vapid_public"`
|
||||
VAPIDPrivate string `json:"vapid_private"`
|
||||
}
|
||||
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if resp.VAPIDPublic == "" {
|
||||
t.Error("vapid_public is empty")
|
||||
}
|
||||
if resp.VAPIDPrivate == "" {
|
||||
t.Error("vapid_private is empty")
|
||||
}
|
||||
if resp.VAPIDPublic == resp.VAPIDPrivate {
|
||||
t.Errorf("vapid_public and vapid_private are identical: %q", resp.VAPIDPublic)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HandleSendTestPush
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestPushHandleSendTestPush_CreatesForCaller(t *testing.T) {
|
||||
n := &fakeNotifier{}
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, n)
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodPost, "/admin/push/test", nil, 7, "", "")
|
||||
h.HandleSendTestPush(rec, req)
|
||||
|
||||
if rec.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d, want 202; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if len(n.calls) != 1 {
|
||||
t.Fatalf("CreateSystem called %d times, want 1", len(n.calls))
|
||||
}
|
||||
call := n.calls[0]
|
||||
if call.userID != 7 {
|
||||
t.Errorf("userID = %d, want 7", call.userID)
|
||||
}
|
||||
if call.typ != "system.test_push" {
|
||||
t.Errorf("typ = %q, want system.test_push", call.typ)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPushHandleSendTestPush_NilNotifier503(t *testing.T) {
|
||||
h := NewPushHandler(&fakePushRegistry{}, &fakePushConfig{}, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
req := pushUserRequest(http.MethodPost, "/admin/push/test", nil, 1, "", "")
|
||||
h.HandleSendTestPush(rec, req)
|
||||
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want 503; body: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func mustMarshal(t *testing.T, v any) []byte {
|
||||
t.Helper()
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal: %v", err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// containsJSON checks that s contains the substring (after stripping whitespace
|
||||
// equivalents that json.Marshal doesn't emit — good enough for our payloads).
|
||||
func containsJSON(s, sub string) bool {
|
||||
return len(s) > 0 && (len(sub) == 0 || len(s) >= len(sub) && func() bool {
|
||||
for i := 0; i <= len(s)-len(sub); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}())
|
||||
}
|
||||
@@ -10,14 +10,15 @@ import (
|
||||
|
||||
// RateLimitHandler handles rate limit config admin endpoints.
|
||||
type RateLimitHandler struct {
|
||||
store ratelimit.SettingsStore
|
||||
mw *ratelimit.Middleware
|
||||
eventBus cache.EventBus
|
||||
store ratelimit.SettingsStore
|
||||
mw *ratelimit.Middleware
|
||||
eventBus cache.EventBus
|
||||
restartStatus *ServerRestartStatusTracker
|
||||
}
|
||||
|
||||
// NewRateLimitHandler creates a new RateLimitHandler.
|
||||
func NewRateLimitHandler(store ratelimit.SettingsStore, mw *ratelimit.Middleware, eventBus cache.EventBus) *RateLimitHandler {
|
||||
return &RateLimitHandler{store: store, mw: mw, eventBus: eventBus}
|
||||
func NewRateLimitHandler(store ratelimit.SettingsStore, mw *ratelimit.Middleware, eventBus cache.EventBus, restartStatus *ServerRestartStatusTracker) *RateLimitHandler {
|
||||
return &RateLimitHandler{store: store, mw: mw, eventBus: eventBus, restartStatus: restartStatus}
|
||||
}
|
||||
|
||||
type rateLimitConfigResponse struct {
|
||||
@@ -156,10 +157,17 @@ func (h *RateLimitHandler) HandleUpdateConfig(w http.ResponseWriter, r *http.Req
|
||||
|
||||
// Save backend setting (infrastructure-level, requires restart)
|
||||
if req.Backend == "memory" || req.Backend == "redis" {
|
||||
currentBackend, _ := h.store.Get(r.Context(), "ratelimit.backend")
|
||||
if currentBackend == "" {
|
||||
currentBackend = "memory"
|
||||
}
|
||||
if err := h.store.Set(r.Context(), "ratelimit.backend", req.Backend); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to save backend setting")
|
||||
return
|
||||
}
|
||||
if currentBackend != req.Backend {
|
||||
h.restartStatus.MarkRequired("ratelimit_backend")
|
||||
}
|
||||
}
|
||||
|
||||
// The limiter is constructed at startup, so enabling while it is absent
|
||||
|
||||
@@ -86,7 +86,7 @@ func TestRateLimitHandlerWithoutRunningLimiter(t *testing.T) {
|
||||
// endpoints must still work so an admin can re-enable it from the UI.
|
||||
store := newFakeRateLimitStore()
|
||||
store.values["ratelimit.enabled"] = "false"
|
||||
h := NewRateLimitHandler(store, nil, nil)
|
||||
h := NewRateLimitHandler(store, nil, nil, NewServerRestartStatusTracker())
|
||||
|
||||
got := getRateLimitConfig(t, h)
|
||||
if got.Enabled {
|
||||
@@ -116,7 +116,7 @@ func TestRateLimitHandlerWithoutRunningLimiter(t *testing.T) {
|
||||
func TestRateLimitHandlerWithRunningLimiter(t *testing.T) {
|
||||
store := newFakeRateLimitStore()
|
||||
mw := newRunningRateLimitMiddleware(t, store)
|
||||
h := NewRateLimitHandler(store, mw, nil)
|
||||
h := NewRateLimitHandler(store, mw, nil, NewServerRestartStatusTracker())
|
||||
|
||||
got := getRateLimitConfig(t, h)
|
||||
if !got.Active {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/catalog"
|
||||
"github.com/Silo-Server/silo-server/internal/models"
|
||||
)
|
||||
|
||||
// serverCollectionsPerLibraryCap bounds how many collections each library
|
||||
// contributes to the aggregated server-collections response. The frontend
|
||||
// renders each library as a horizontal teaser row with a "See all" link into
|
||||
// the per-library Collections tab, so it never needs the full set up front.
|
||||
// Capping keeps the payload and the per-library presign cost bounded.
|
||||
const serverCollectionsPerLibraryCap = 20
|
||||
|
||||
// serverCollectionsLibrary is one library's bucket of visible server (admin)
|
||||
// collections. Collections is a capped teaser slice; TotalCount is the full
|
||||
// visible count, used to drive the "See all (N)" affordance.
|
||||
type serverCollectionsLibrary struct {
|
||||
LibraryID int `json:"library_id"`
|
||||
LibraryName string `json:"library_name"`
|
||||
TotalCount int `json:"total_count"`
|
||||
Collections []libraryTabCollection `json:"collections"`
|
||||
}
|
||||
|
||||
type serverCollectionsResponse struct {
|
||||
Libraries []serverCollectionsLibrary `json:"libraries"`
|
||||
}
|
||||
|
||||
// capServerCollections trims a library's collections to the per-library teaser
|
||||
// cap and returns the capped slice alongside the original total count (used for
|
||||
// the frontend's "See all (N)" affordance).
|
||||
func capServerCollections(collections []*models.LibraryCollection) ([]*models.LibraryCollection, int) {
|
||||
total := len(collections)
|
||||
if total > serverCollectionsPerLibraryCap {
|
||||
return collections[:serverCollectionsPerLibraryCap], total
|
||||
}
|
||||
return collections, total
|
||||
}
|
||||
|
||||
// HandleListServerCollections aggregates visible server (admin-curated library)
|
||||
// collections across every library the requester can access, bucketed by
|
||||
// library. It powers the "Server collections" section of the user-facing
|
||||
// Collections tab, surfacing collections that otherwise live only inside each
|
||||
// individual library's tab.
|
||||
//
|
||||
// This is intentionally a separate endpoint from GET /collections (which
|
||||
// returns the user's editable personal collections): the two have different
|
||||
// access semantics and cache/refetch lifecycles, and overloading the personal
|
||||
// response with read-only server collections would be a client-facing trap.
|
||||
func (h *LibraryCollectionHandler) HandleListServerCollections(w http.ResponseWriter, r *http.Request) {
|
||||
resp := serverCollectionsResponse{Libraries: []serverCollectionsLibrary{}}
|
||||
if h.FolderRepo == nil {
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
folders, err := h.FolderRepo.List(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load libraries")
|
||||
return
|
||||
}
|
||||
|
||||
// Folders are already ordered by sort_order; iterate in that order so the
|
||||
// rendered rows match the user's library ordering.
|
||||
for _, f := range folders {
|
||||
if !f.Enabled {
|
||||
continue
|
||||
}
|
||||
// Honor the requester's library access scope; never reveal collections
|
||||
// from libraries outside AllowedLibraryIDs.
|
||||
if !requestCanAccessLibrary(r, f.ID) {
|
||||
continue
|
||||
}
|
||||
|
||||
// ListByLibrary returns only visible collections, ordered
|
||||
// featured-first, so the capped slice surfaces the best ones.
|
||||
collections, err := h.repo.ListByLibrary(r.Context(), f.ID, catalog.ListLibraryCollectionsOptions{})
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to load collections")
|
||||
return
|
||||
}
|
||||
if len(collections) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
collections, total := capServerCollections(collections)
|
||||
|
||||
colls := make([]libraryTabCollection, 0, len(collections))
|
||||
for _, c := range collections {
|
||||
colls = append(colls, libraryTabCollection{
|
||||
ID: c.ID,
|
||||
Title: c.Title,
|
||||
PosterURL: h.presignGPURL(r, c.PosterURL),
|
||||
PosterThumbhash: c.PosterThumbhash,
|
||||
ItemCount: c.ItemCount,
|
||||
Featured: c.Featured,
|
||||
})
|
||||
}
|
||||
|
||||
resp.Libraries = append(resp.Libraries, serverCollectionsLibrary{
|
||||
LibraryID: f.ID,
|
||||
LibraryName: f.Name,
|
||||
TotalCount: total,
|
||||
Collections: colls,
|
||||
})
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/models"
|
||||
)
|
||||
|
||||
func TestCapServerCollections(t *testing.T) {
|
||||
makeN := func(n int) []*models.LibraryCollection {
|
||||
out := make([]*models.LibraryCollection, n)
|
||||
for i := range out {
|
||||
out[i] = &models.LibraryCollection{ID: string(rune('a' + i%26))}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
t.Run("under cap returns all, total equals length", func(t *testing.T) {
|
||||
in := makeN(serverCollectionsPerLibraryCap - 3)
|
||||
got, total := capServerCollections(in)
|
||||
if total != serverCollectionsPerLibraryCap-3 {
|
||||
t.Fatalf("total = %d, want %d", total, serverCollectionsPerLibraryCap-3)
|
||||
}
|
||||
if len(got) != len(in) {
|
||||
t.Fatalf("len(got) = %d, want %d", len(got), len(in))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("at cap returns all", func(t *testing.T) {
|
||||
in := makeN(serverCollectionsPerLibraryCap)
|
||||
got, total := capServerCollections(in)
|
||||
if total != serverCollectionsPerLibraryCap || len(got) != serverCollectionsPerLibraryCap {
|
||||
t.Fatalf("got len=%d total=%d, want both %d", len(got), total, serverCollectionsPerLibraryCap)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("over cap trims slice but reports full total", func(t *testing.T) {
|
||||
in := makeN(serverCollectionsPerLibraryCap + 17)
|
||||
got, total := capServerCollections(in)
|
||||
if len(got) != serverCollectionsPerLibraryCap {
|
||||
t.Fatalf("len(got) = %d, want %d", len(got), serverCollectionsPerLibraryCap)
|
||||
}
|
||||
if total != serverCollectionsPerLibraryCap+17 {
|
||||
t.Fatalf("total = %d, want %d", total, serverCollectionsPerLibraryCap+17)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -18,6 +18,7 @@ var ErrServerRestartAlreadyRequested = errors.New("server restart already reques
|
||||
type ServerControlHandler struct {
|
||||
requestRestart func(ctx context.Context) error
|
||||
commands *playback.CommandDispatcher
|
||||
restartStatus *ServerRestartStatusTracker
|
||||
}
|
||||
|
||||
type serverRestartRequest struct {
|
||||
@@ -35,10 +36,12 @@ type serverRestartResponse struct {
|
||||
func NewServerControlHandler(
|
||||
requestRestart func(ctx context.Context) error,
|
||||
commands *playback.CommandDispatcher,
|
||||
restartStatus *ServerRestartStatusTracker,
|
||||
) *ServerControlHandler {
|
||||
return &ServerControlHandler{
|
||||
requestRestart: requestRestart,
|
||||
commands: commands,
|
||||
restartStatus: restartStatus,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +62,7 @@ func (h *ServerControlHandler) HandleRestart(w http.ResponseWriter, r *http.Requ
|
||||
|
||||
if err := h.requestRestart(context.Background()); err != nil {
|
||||
if errors.Is(err, ErrServerRestartAlreadyRequested) {
|
||||
h.restartStatus.MarkRestartRequested()
|
||||
writeJSON(w, http.StatusAccepted, serverRestartResponse{
|
||||
Status: "already_requested",
|
||||
Message: "Server restart is already in progress.",
|
||||
@@ -71,6 +75,7 @@ func (h *ServerControlHandler) HandleRestart(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
h.restartStatus.MarkRestartRequested()
|
||||
writeJSON(w, http.StatusAccepted, serverRestartResponse{
|
||||
Status: "restart_requested",
|
||||
Message: "Server restart requested. The process will shut down gracefully.",
|
||||
|
||||
@@ -24,10 +24,11 @@ func TestServerControlRestartRequestsShutdown(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
called := 0
|
||||
restartStatus := NewServerRestartStatusTracker()
|
||||
handler := NewServerControlHandler(func(context.Context) error {
|
||||
called++
|
||||
return nil
|
||||
}, nil)
|
||||
}, nil, restartStatus)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/server/restart", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -50,12 +51,15 @@ func TestServerControlRestartRequestsShutdown(t *testing.T) {
|
||||
if resp.NotifiedSessions != 0 {
|
||||
t.Fatalf("notified_sessions = %d, want 0", resp.NotifiedSessions)
|
||||
}
|
||||
if snapshot := restartStatus.Snapshot(); !snapshot.RestartRequested {
|
||||
t.Fatal("RestartRequested = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerControlRestartUnavailable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := NewServerControlHandler(nil, nil)
|
||||
handler := NewServerControlHandler(nil, nil, NewServerRestartStatusTracker())
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/server/restart", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -71,7 +75,7 @@ func TestServerControlRestartAlreadyRequested(t *testing.T) {
|
||||
|
||||
handler := NewServerControlHandler(func(context.Context) error {
|
||||
return ErrServerRestartAlreadyRequested
|
||||
}, nil)
|
||||
}, nil, NewServerRestartStatusTracker())
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/server/restart", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -110,7 +114,7 @@ func TestServerControlRestartNotifiesPlaybackSessions(t *testing.T) {
|
||||
dispatcher := playback.NewCommandDispatcher(sessionMgr, realtimeHub, nil)
|
||||
handler := NewServerControlHandler(func(context.Context) error {
|
||||
return nil
|
||||
}, dispatcher)
|
||||
}, dispatcher, NewServerRestartStatusTracker())
|
||||
|
||||
req := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ServerRestartStatusTracker keeps process-local restart state. A real server
|
||||
// restart creates a new tracker, which clears any pending restart banner state.
|
||||
type ServerRestartStatusTracker struct {
|
||||
mu sync.RWMutex
|
||||
|
||||
startedAt time.Time
|
||||
restartRequired bool
|
||||
restartRequiredAt time.Time
|
||||
restartRequiredReason string
|
||||
restartRequested bool
|
||||
restartRequestedAt time.Time
|
||||
}
|
||||
|
||||
type ServerRestartStatusSnapshot struct {
|
||||
StartedAt time.Time
|
||||
RestartRequired bool
|
||||
RestartRequiredAt *time.Time
|
||||
RestartRequiredReason string
|
||||
RestartRequested bool
|
||||
RestartRequestedAt *time.Time
|
||||
}
|
||||
|
||||
func NewServerRestartStatusTracker() *ServerRestartStatusTracker {
|
||||
return &ServerRestartStatusTracker{
|
||||
startedAt: time.Now().UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerRestartStatusTracker) MarkRequired(reason string) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
reason = strings.TrimSpace(reason)
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if !s.restartRequired {
|
||||
s.restartRequired = true
|
||||
s.restartRequiredAt = now
|
||||
}
|
||||
if reason != "" {
|
||||
s.restartRequiredReason = reason
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerRestartStatusTracker) MarkRestartRequested() {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if !s.restartRequested {
|
||||
s.restartRequested = true
|
||||
s.restartRequestedAt = now
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerRestartStatusTracker) Snapshot() ServerRestartStatusSnapshot {
|
||||
if s == nil {
|
||||
now := time.Now().UTC()
|
||||
return ServerRestartStatusSnapshot{StartedAt: now}
|
||||
}
|
||||
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
var restartRequiredAt *time.Time
|
||||
if !s.restartRequiredAt.IsZero() {
|
||||
t := s.restartRequiredAt
|
||||
restartRequiredAt = &t
|
||||
}
|
||||
|
||||
var restartRequestedAt *time.Time
|
||||
if !s.restartRequestedAt.IsZero() {
|
||||
t := s.restartRequestedAt
|
||||
restartRequestedAt = &t
|
||||
}
|
||||
|
||||
return ServerRestartStatusSnapshot{
|
||||
StartedAt: s.startedAt,
|
||||
RestartRequired: s.restartRequired,
|
||||
RestartRequiredAt: restartRequiredAt,
|
||||
RestartRequiredReason: s.restartRequiredReason,
|
||||
RestartRequested: s.restartRequested,
|
||||
RestartRequestedAt: restartRequestedAt,
|
||||
}
|
||||
}
|
||||
@@ -54,25 +54,6 @@ func (h *ThemeHandler) HandleAdminCSS(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// brandingResponse is returned by GET /theme/branding.
|
||||
type brandingResponse struct {
|
||||
ServerName string `json:"server_name"`
|
||||
LoginSubtitle string `json:"login_subtitle"`
|
||||
}
|
||||
|
||||
// HandleBranding returns the server branding settings.
|
||||
// Public endpoint — no authentication required so branding appears on the login page.
|
||||
func (h *ThemeHandler) HandleBranding(w http.ResponseWriter, r *http.Request) {
|
||||
serverName, _ := h.settings.Get(r.Context(), "branding.server_name")
|
||||
loginSubtitle, _ := h.settings.Get(r.Context(), "branding.login_subtitle")
|
||||
|
||||
w.Header().Set("Cache-Control", "public, max-age=60")
|
||||
writeJSON(w, http.StatusOK, brandingResponse{
|
||||
ServerName: serverName,
|
||||
LoginSubtitle: loginSubtitle,
|
||||
})
|
||||
}
|
||||
|
||||
// allowedDownloadHosts restricts which hosts the theme download proxy will fetch from.
|
||||
var allowedDownloadHosts = map[string]bool{
|
||||
"raw.githubusercontent.com": true,
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
)
|
||||
|
||||
func runActingAdminMiddleware(t *testing.T, role, profileID string, check PrimaryProfileChecker) int {
|
||||
t.Helper()
|
||||
next := RequireActingAdmin(check)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/sessions", nil)
|
||||
if profileID != "" {
|
||||
req.Header.Set("X-Profile-Id", profileID)
|
||||
}
|
||||
ctx := SetClaims(req.Context(), &auth.Claims{UserID: 7, Role: role, TokenType: auth.TokenTypeAccess})
|
||||
rec := httptest.NewRecorder()
|
||||
next.ServeHTTP(rec, req.WithContext(ctx))
|
||||
return rec.Code
|
||||
}
|
||||
|
||||
func primaryChecker(isPrimary, found bool, err error) PrimaryProfileChecker {
|
||||
return func(_ context.Context, _ int, _ string) (bool, bool, error) {
|
||||
return isPrimary, found, err
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_RejectsNonAdmin(t *testing.T) {
|
||||
if code := runActingAdminMiddleware(t, "user", "", nil); code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_AllowsAdminWithoutProfile(t *testing.T) {
|
||||
check := primaryChecker(false, true, nil) // must not be consulted with no declared profile
|
||||
if code := runActingAdminMiddleware(t, "admin", "", check); code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_AllowsPrimaryProfile(t *testing.T) {
|
||||
check := primaryChecker(true, true, nil)
|
||||
if code := runActingAdminMiddleware(t, "admin", "prof-1", check); code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_RejectsNonPrimaryProfile(t *testing.T) {
|
||||
check := primaryChecker(false, true, nil)
|
||||
if code := runActingAdminMiddleware(t, "admin", "prof-2", check); code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_RejectsUnknownProfile(t *testing.T) {
|
||||
// A declared profile that doesn't resolve to one of the caller's profiles
|
||||
// fails closed; otherwise a bogus X-Profile-Id would restore admin powers.
|
||||
check := primaryChecker(false, false, nil)
|
||||
if code := runActingAdminMiddleware(t, "admin", "prof-x", check); code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_CheckerErrorIsInternalError(t *testing.T) {
|
||||
check := primaryChecker(false, false, errors.New("db down"))
|
||||
if code := runActingAdminMiddleware(t, "admin", "prof-1", check); code != http.StatusInternalServerError {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireActingAdmin_NilCheckerBehavesLikeRequireAdmin(t *testing.T) {
|
||||
if code := runActingAdminMiddleware(t, "admin", "prof-2", nil); code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
@@ -164,6 +164,83 @@ func RequireAdmin(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// PrimaryProfileChecker reports whether profileID belongs to userID and, if
|
||||
// so, whether it is the household primary profile. found must be false when
|
||||
// the profile does not exist or belongs to a different account.
|
||||
type PrimaryProfileChecker func(ctx context.Context, userID int, profileID string) (isPrimary bool, found bool, err error)
|
||||
|
||||
// RequireActingAdmin enforces the admin role plus the household policy that
|
||||
// admin powers are only exercised through the account's primary profile.
|
||||
// When the request declares an active profile (X-Profile-Id) that belongs to
|
||||
// the admin account but is not the primary profile, the request is refused;
|
||||
// requests with no declared profile keep working (clients that haven't
|
||||
// selected a profile yet). With a nil checker it behaves exactly like
|
||||
// RequireAdmin.
|
||||
//
|
||||
// Note this enforces the declared profile, not an authenticated one: all
|
||||
// profiles on an account share the login session, so this is a policy
|
||||
// boundary for well-behaved clients, not a defense against the account
|
||||
// holder themselves.
|
||||
func RequireActingAdmin(checkPrimary PrimaryProfileChecker) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims := GetClaims(r.Context())
|
||||
if claims == nil {
|
||||
writeUnauthorized(w, "Authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
if claims.Role != "admin" {
|
||||
writeForbidden(w, "Admin access required")
|
||||
return
|
||||
}
|
||||
|
||||
allowed, err := actingAdminAllowed(r, claims.UserID, checkPrimary)
|
||||
if err != nil {
|
||||
writeInternalError(w, "Failed to verify active profile")
|
||||
return
|
||||
}
|
||||
if !allowed {
|
||||
writeForbidden(w, "Admin access requires the account's primary profile")
|
||||
return
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// actingAdminAllowed reports whether an admin request may exercise admin
|
||||
// powers given the profile it declares. Allowed when no checker is
|
||||
// configured, no profile is declared, or the declared profile is the
|
||||
// account's primary profile. A declared profile that cannot be resolved to
|
||||
// one of the caller's profiles fails closed: otherwise a non-primary session
|
||||
// could regain admin powers by sending a bogus X-Profile-Id.
|
||||
func actingAdminAllowed(r *http.Request, userID int, checkPrimary PrimaryProfileChecker) (bool, error) {
|
||||
if checkPrimary == nil {
|
||||
return true, nil
|
||||
}
|
||||
profileID := declaredProfileID(r)
|
||||
if profileID == "" {
|
||||
return true, nil
|
||||
}
|
||||
isPrimary, found, err := checkPrimary(r.Context(), userID, profileID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return found && isPrimary, nil
|
||||
}
|
||||
|
||||
// declaredProfileID returns the active profile the request declares: the
|
||||
// profile context when RequireProfile ran earlier in the chain, otherwise
|
||||
// the raw X-Profile-Id header.
|
||||
func declaredProfileID(r *http.Request) string {
|
||||
if id := GetProfileID(r.Context()); id != "" {
|
||||
return id
|
||||
}
|
||||
return r.Header.Get("X-Profile-Id")
|
||||
}
|
||||
|
||||
// SetClaims stores JWT claims in the context. This is useful for testing
|
||||
// handlers that depend on authentication without going through the full
|
||||
// middleware chain.
|
||||
@@ -243,6 +320,16 @@ func writeUnauthorized(w http.ResponseWriter, message string) {
|
||||
})
|
||||
}
|
||||
|
||||
// writeInternalError writes a 500 JSON error response.
|
||||
func writeInternalError(w http.ResponseWriter, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_ = json.NewEncoder(w).Encode(errorResponse{
|
||||
Error: "internal_error",
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
// writeForbidden writes a 403 JSON error response.
|
||||
func writeForbidden(w http.ResponseWriter, message string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
@@ -22,17 +22,25 @@ type MetadataTargetLibraryResolver interface {
|
||||
}
|
||||
|
||||
type PermissionMiddleware struct {
|
||||
users PermissionUserLoader
|
||||
libraries MetadataTargetLibraryResolver
|
||||
users PermissionUserLoader
|
||||
libraries MetadataTargetLibraryResolver
|
||||
checkPrimary PrimaryProfileChecker // nil disables the acting-admin profile policy
|
||||
}
|
||||
|
||||
func NewPermissionMiddleware(users PermissionUserLoader, libraries MetadataTargetLibraryResolver) *PermissionMiddleware {
|
||||
return &PermissionMiddleware{users: users, libraries: libraries}
|
||||
func NewPermissionMiddleware(
|
||||
users PermissionUserLoader,
|
||||
libraries MetadataTargetLibraryResolver,
|
||||
checkPrimary PrimaryProfileChecker,
|
||||
) *PermissionMiddleware {
|
||||
return &PermissionMiddleware{users: users, libraries: libraries, checkPrimary: checkPrimary}
|
||||
}
|
||||
|
||||
// RequireMetadataCurationForItem allows admins or users with metadata_curation
|
||||
// permission when every library containing the target item is within the user's
|
||||
// assigned libraries. A nil user library list means unrestricted library access.
|
||||
// RequireMetadataCurationForItem allows acting admins or users with
|
||||
// metadata_curation permission when every library containing the target item
|
||||
// is within the user's assigned libraries. A nil user library list means
|
||||
// unrestricted library access. An admin declaring a non-primary profile does
|
||||
// not get the admin bypass (see actingAdminAllowed) and is held to the same
|
||||
// explicitly-assigned-permission bar as everyone else.
|
||||
func (m *PermissionMiddleware) RequireMetadataCurationForItem(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
claims := GetClaims(r.Context())
|
||||
@@ -41,8 +49,19 @@ func (m *PermissionMiddleware) RequireMetadataCurationForItem(next http.Handler)
|
||||
return
|
||||
}
|
||||
if claims.Role == "admin" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
var checkPrimary PrimaryProfileChecker
|
||||
if m != nil {
|
||||
checkPrimary = m.checkPrimary
|
||||
}
|
||||
actingAdmin, err := actingAdminAllowed(r, claims.UserID, checkPrimary)
|
||||
if err != nil {
|
||||
writePermissionError(w, http.StatusInternalServerError, "internal_error", "Failed to verify active profile")
|
||||
return
|
||||
}
|
||||
if actingAdmin {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
if m == nil || m.users == nil || m.libraries == nil {
|
||||
writeForbidden(w, "Metadata curation permission required")
|
||||
@@ -60,7 +79,14 @@ func (m *PermissionMiddleware) RequireMetadataCurationForItem(next http.Handler)
|
||||
writeForbidden(w, "Metadata curation permission required")
|
||||
return
|
||||
}
|
||||
if !auth.HasEffectivePermission(user, auth.PermissionMetadataCuration) {
|
||||
hasPermission := auth.HasEffectivePermission(user, auth.PermissionMetadataCuration)
|
||||
if claims.Role == "admin" {
|
||||
// Reached only when the admin bypass was refused (non-primary
|
||||
// profile declared): the role-derived grant does not apply, only
|
||||
// an explicitly assigned permission does.
|
||||
hasPermission = auth.HasAssignedPermission(user, auth.PermissionMetadataCuration)
|
||||
}
|
||||
if !hasPermission {
|
||||
writeForbidden(w, "Metadata curation permission required")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ func runMetadataCurationMiddleware(user *models.User, libraryIDs []int, role str
|
||||
mw := NewPermissionMiddleware(
|
||||
fakePermissionUserLoader{user: user},
|
||||
fakeTargetLibraryResolver{ids: libraryIDs},
|
||||
nil,
|
||||
)
|
||||
next := mw.RequireMetadataCurationForItem(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
@@ -52,6 +53,52 @@ func runMetadataCurationMiddleware(user *models.User, libraryIDs []int, role str
|
||||
return rec.Code
|
||||
}
|
||||
|
||||
func runMetadataCurationMiddlewareWithProfile(
|
||||
user *models.User,
|
||||
libraryIDs []int,
|
||||
role, profileID string,
|
||||
check PrimaryProfileChecker,
|
||||
) int {
|
||||
mw := NewPermissionMiddleware(
|
||||
fakePermissionUserLoader{user: user},
|
||||
fakeTargetLibraryResolver{ids: libraryIDs},
|
||||
check,
|
||||
)
|
||||
next := mw.RequireMetadataCurationForItem(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
req := requestWithItemID(role)
|
||||
if profileID != "" {
|
||||
req.Header.Set("X-Profile-Id", profileID)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
next.ServeHTTP(rec, req)
|
||||
return rec.Code
|
||||
}
|
||||
|
||||
func TestRequireMetadataCurationForItem_AdminOnPrimaryProfileBypasses(t *testing.T) {
|
||||
code := runMetadataCurationMiddlewareWithProfile(nil, nil, "admin", "prof-1", primaryChecker(true, true, nil))
|
||||
if code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireMetadataCurationForItem_AdminOnNonPrimaryProfileWithoutAssignedPermission(t *testing.T) {
|
||||
admin := &models.User{ID: 7, Role: "admin", Enabled: true, LibraryIDs: nil, Permissions: nil}
|
||||
code := runMetadataCurationMiddlewareWithProfile(admin, []int{1}, "admin", "prof-2", primaryChecker(false, true, nil))
|
||||
if code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireMetadataCurationForItem_AdminOnNonPrimaryProfileWithAssignedPermission(t *testing.T) {
|
||||
admin := &models.User{ID: 7, Role: "admin", Enabled: true, LibraryIDs: nil, Permissions: []string{"metadata_curation"}}
|
||||
code := runMetadataCurationMiddlewareWithProfile(admin, []int{1}, "admin", "prof-2", primaryChecker(false, true, nil))
|
||||
if code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireMetadataCurationForItem_AllowsAdmin(t *testing.T) {
|
||||
code := runMetadataCurationMiddleware(nil, nil, "admin")
|
||||
if code != http.StatusNoContent {
|
||||
|
||||
+180
-77
@@ -26,6 +26,7 @@ import (
|
||||
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
||||
"github.com/Silo-Server/silo-server/internal/auth"
|
||||
"github.com/Silo-Server/silo-server/internal/autoscan"
|
||||
"github.com/Silo-Server/silo-server/internal/branding"
|
||||
"github.com/Silo-Server/silo-server/internal/cache"
|
||||
"github.com/Silo-Server/silo-server/internal/catalog"
|
||||
"github.com/Silo-Server/silo-server/internal/catalogseed"
|
||||
@@ -37,6 +38,7 @@ import (
|
||||
"github.com/Silo-Server/silo-server/internal/intromarkers"
|
||||
"github.com/Silo-Server/silo-server/internal/libraryingest"
|
||||
"github.com/Silo-Server/silo-server/internal/logstream"
|
||||
"github.com/Silo-Server/silo-server/internal/mail"
|
||||
"github.com/Silo-Server/silo-server/internal/markers"
|
||||
"github.com/Silo-Server/silo-server/internal/mdblist"
|
||||
"github.com/Silo-Server/silo-server/internal/metadata"
|
||||
@@ -48,8 +50,6 @@ import (
|
||||
"github.com/Silo-Server/silo-server/internal/opslog"
|
||||
"github.com/Silo-Server/silo-server/internal/playback"
|
||||
"github.com/Silo-Server/silo-server/internal/plugins"
|
||||
"github.com/Silo-Server/silo-server/internal/presence"
|
||||
"github.com/Silo-Server/silo-server/internal/push"
|
||||
"github.com/Silo-Server/silo-server/internal/ratelimit"
|
||||
"github.com/Silo-Server/silo-server/internal/recommendations"
|
||||
mediarequests "github.com/Silo-Server/silo-server/internal/requests"
|
||||
@@ -91,6 +91,7 @@ type Dependencies struct {
|
||||
S3Public *s3client.Client // public assets bucket client (may be nil)
|
||||
S3Private *s3client.Client // private internal bucket client (may be nil)
|
||||
S3UserDB *s3client.Client // user-db bucket client (may be nil)
|
||||
BrandingService *branding.Service // white-label branding (nil when DB unavailable)
|
||||
FolderRepo *catalog.FolderRepository // media folder repository (may be nil)
|
||||
FileRepo *scanner.FileRepository // media file repository (may be nil)
|
||||
Scanner *scanner.Scanner // scanner instance (may be nil)
|
||||
@@ -121,6 +122,7 @@ type Dependencies struct {
|
||||
NodeID string
|
||||
LogStreamHub *logstream.Hub
|
||||
RealtimeHub *notifications.Hub
|
||||
Notifications *notifications.System // user-facing release notifications (may be nil)
|
||||
EventsHub *evt.Hub
|
||||
ScanRegistry *evt.ScanRegistry
|
||||
LibraryScanQueue *scanqueue.Service
|
||||
@@ -156,6 +158,7 @@ type Dependencies struct {
|
||||
OnUserSessionsRevoked func(ctx context.Context, userID int)
|
||||
OnServerSettingUpdated func(ctx context.Context, key, value string)
|
||||
RequestServerRestart func(ctx context.Context) error
|
||||
ServerRestartStatus *handlers.ServerRestartStatusTracker
|
||||
|
||||
// UserCollectionSync handles per-profile imported collections (TMDB /
|
||||
// Trakt / MDBList) — the user-facing analogue of CollectionService.
|
||||
@@ -177,23 +180,6 @@ type Dependencies struct {
|
||||
// clients hitting /login, /api/*, /abs/api/*, and /abs/socket.io/* all
|
||||
// resolve correctly. May be nil; no ABS routes are registered in that case.
|
||||
ABSHandler absHandler
|
||||
|
||||
// NotificationsService is the notifications domain service used to power
|
||||
// the inbox, preferences, and announcement endpoints. May be nil; routes
|
||||
// are not registered in that case.
|
||||
NotificationsService *notifications.Service
|
||||
|
||||
// PushStore is the push token/device registry. May be nil; push routes
|
||||
// are not registered in that case.
|
||||
PushStore *push.Store
|
||||
|
||||
// PushConfig reads push provider configuration from the settings repo.
|
||||
// May be nil; push routes are not registered when PushStore is nil.
|
||||
PushConfig *push.Config
|
||||
|
||||
// PresenceRegistry tracks live WebSocket connections for presence-aware
|
||||
// push suppression. May be nil; a no-op fallback is used in that case.
|
||||
PresenceRegistry presence.Registry
|
||||
}
|
||||
|
||||
// absHandler is the narrow interface the router needs from the ABS handler.
|
||||
@@ -256,6 +242,33 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
|
||||
readyHandler := handlers.NewReadyHandler(pgPinger, s3Checker)
|
||||
|
||||
// Resolves whether a declared profile belongs to the user and is the
|
||||
// household primary profile. Nil (no user store) disables the
|
||||
// acting-admin profile policy, degrading admin routes to the plain
|
||||
// role check.
|
||||
var checkPrimaryProfile apimw.PrimaryProfileChecker
|
||||
if deps.UserStoreProvider != nil {
|
||||
userStores := deps.UserStoreProvider
|
||||
checkPrimaryProfile = func(ctx context.Context, userID int, profileID string) (bool, bool, error) {
|
||||
store, err := userStores.ForUser(ctx, userID)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
profile, err := store.GetProfile(ctx, profileID)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
if profile == nil {
|
||||
return false, false, nil
|
||||
}
|
||||
return profile.IsPrimary, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Admin authorization for routes: admin role, exercised through the
|
||||
// account's primary household profile (see apimw.RequireActingAdmin).
|
||||
requireActingAdmin := apimw.RequireActingAdmin(checkPrimaryProfile)
|
||||
|
||||
// Health handler advertises the server's identity so multi-server
|
||||
// clients can display a friendly name. Falls back to empty strings
|
||||
// if config is absent (tests, minimal fixtures); JSON omits empties.
|
||||
@@ -329,6 +342,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
permissionMiddleware = apimw.NewPermissionMiddleware(
|
||||
userRepo,
|
||||
apimw.NewPGMetadataTargetLibraryResolver(deps.DB),
|
||||
checkPrimaryProfile,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -516,11 +530,17 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
mediarequests.NewCatalogPresence(itemRepo, providerIDRepo),
|
||||
)
|
||||
AttachRequestRouter(requestSvc, deps.PluginService)
|
||||
requestSvc.SetEventsHub(deps.EventsHub)
|
||||
requestSvc.SetRequesterIdentityResolver(plugins.RequesterIdentityFromLookup(plugins.NewPgUserIdentityLookup(deps.DB)))
|
||||
if viewerResolver != nil {
|
||||
requestSvc.SetEntitlementResolver(mediarequests.NewAccessEntitlements(viewerResolver))
|
||||
}
|
||||
// Request lifecycle notifications (submitted / approved / declined):
|
||||
// server-channel broadcasts plus personal deliveries to the requester
|
||||
// on approve/decline. Fulfilled rides the reconcile service's
|
||||
// fulfillment notifier instead.
|
||||
if lifecycle := notifications.NewRequestLifecycleNotifier(deps.Notifications); lifecycle != nil {
|
||||
requestSvc.SetLifecycleNotifier(lifecycle)
|
||||
}
|
||||
requestHandler = handlers.NewRequestsHandler(requestSvc)
|
||||
|
||||
autoscanRepo := autoscan.NewRepository(deps.DB, deps.SecretCipher)
|
||||
@@ -780,7 +800,11 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
}
|
||||
}
|
||||
|
||||
serverControlHandler := handlers.NewServerControlHandler(deps.RequestServerRestart, playbackCommandDispatcher)
|
||||
restartStatus := deps.ServerRestartStatus
|
||||
if restartStatus == nil {
|
||||
restartStatus = handlers.NewServerRestartStatusTracker()
|
||||
}
|
||||
serverControlHandler := handlers.NewServerControlHandler(deps.RequestServerRestart, playbackCommandDispatcher, restartStatus)
|
||||
|
||||
// Build admin handler if we have a user repo.
|
||||
var adminHandler *handlers.AdminHandler
|
||||
@@ -791,14 +815,17 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
adminHandler.SessionsLoader = playbackSessionsLoader
|
||||
adminHandler.DetailSvc = detailSvc
|
||||
adminHandler.EventBus = deps.EventBus
|
||||
adminHandler.EventsHub = deps.EventsHub
|
||||
adminHandler.ImpersonationService = authService
|
||||
adminHandler.StatsSource = deps.AdminStatsProvider
|
||||
adminHandler.RealtimeHub = deps.RealtimeHub
|
||||
adminHandler.BootstrapSensitiveConfigured = deps.BootstrapSensitiveConfigured
|
||||
adminHandler.BootstrapSensitiveValues = deps.BootstrapSensitiveValues
|
||||
adminHandler.RestartStatus = restartStatus
|
||||
if settingsRepo != nil {
|
||||
adminHandler.SettingsRepo = settingsRepo
|
||||
}
|
||||
adminHandler.Config = deps.Config
|
||||
if deps.OnUserSessionsRevoked != nil {
|
||||
adminHandler.OnUserSessionsRevoked = deps.OnUserSessionsRevoked
|
||||
}
|
||||
@@ -1338,6 +1365,15 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Route("/api/v1", func(r chi.Router) {
|
||||
r.Get("/health", healthHandler.ServeHTTP)
|
||||
r.Get("/ready", readyHandler.ServeHTTP)
|
||||
|
||||
// Branding handler is shared between the public read/serve endpoints
|
||||
// (registered with the theme endpoints below) and the admin
|
||||
// upload/delete endpoints (registered in the admin group).
|
||||
var brandingHandler *handlers.BrandingHandler
|
||||
if deps.BrandingService != nil {
|
||||
brandingHandler = handlers.NewBrandingHandler(deps.BrandingService)
|
||||
}
|
||||
|
||||
if webhookSyncHandler != nil {
|
||||
r.Post("/plex-sync/webhooks/{secret}", webhookSyncHandler.HandleWebhook)
|
||||
r.Post("/webhook-sync/webhooks/{secret}", webhookSyncHandler.HandleWebhook)
|
||||
@@ -1347,7 +1383,11 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
if settingsRepo != nil {
|
||||
themeHandler := handlers.NewThemeHandler(settingsRepo)
|
||||
r.Get("/theme/admin-css", themeHandler.HandleAdminCSS)
|
||||
r.Get("/theme/branding", themeHandler.HandleBranding)
|
||||
if brandingHandler != nil {
|
||||
// Public branding read + asset serving (pre-login white-label).
|
||||
r.Get("/theme/branding", brandingHandler.HandleGetBranding)
|
||||
r.Get("/branding/assets/{kind}", brandingHandler.HandleServeAsset)
|
||||
}
|
||||
|
||||
// Catalog and download proxies require auth (to avoid open proxy).
|
||||
if authMiddleware != nil {
|
||||
@@ -1355,7 +1395,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Use(authMiddleware.RequireAuth)
|
||||
r.Get("/theme/catalog", themeHandler.HandleCatalog)
|
||||
r.Get("/theme/download", themeHandler.HandleDownload)
|
||||
r.With(apimw.RequireAdmin).Post("/theme/catalog/refresh", themeHandler.HandleCatalogRefresh)
|
||||
r.With(requireActingAdmin).Post("/theme/catalog/refresh", themeHandler.HandleCatalogRefresh)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1469,6 +1509,28 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
})
|
||||
}
|
||||
|
||||
// Discord account-link OAuth callback: public — Discord redirects the
|
||||
// browser here without credentials; the one-time link-state row
|
||||
// authenticates the request and maps it back to the initiating
|
||||
// account. The static path coexists with the authenticated
|
||||
// /notifications subrouter below (static routes win in chi).
|
||||
var discordNotificationsHandler *handlers.DiscordNotificationsHandler
|
||||
if deps.Notifications != nil {
|
||||
discordNotificationsHandler = handlers.NewDiscordNotificationsHandler(deps.Notifications, deps.PublicURL)
|
||||
r.Get("/notifications/discord/link/callback", discordNotificationsHandler.HandleLinkCallback)
|
||||
|
||||
// Tokenized email links: public — clicked from mail clients on
|
||||
// devices without a Silo session; the single-use token (verify)
|
||||
// or per-profile capability token (unsubscribe) authenticates the
|
||||
// request. Static paths coexist with the authenticated
|
||||
// /notifications subrouter below, same as the Discord callback.
|
||||
deps.Notifications.SetPublicURL(deps.PublicURL)
|
||||
emailLinkHandler := handlers.NewEmailLinkHandler(deps.Notifications)
|
||||
r.Get("/notifications/email/verify", emailLinkHandler.HandleVerify)
|
||||
r.Get("/notifications/email/unsubscribe", emailLinkHandler.HandleUnsubscribe)
|
||||
r.Post("/notifications/email/unsubscribe", emailLinkHandler.HandleUnsubscribe)
|
||||
}
|
||||
|
||||
// API key management routes (auth only, no viewer access needed).
|
||||
if apiKeyRepo != nil && authMiddleware != nil {
|
||||
r.Group(func(r chi.Router) {
|
||||
@@ -1513,12 +1575,61 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
deps.ScanRegistry,
|
||||
deps.LibraryScanQueue,
|
||||
historyImportSvc,
|
||||
deps.NotificationsService,
|
||||
deps.PresenceRegistry,
|
||||
)
|
||||
eventsHandler.SetNotificationsSystem(deps.Notifications)
|
||||
r.Get("/events/ws", eventsHandler.HandleWebSocket)
|
||||
}
|
||||
|
||||
// User notifications: profile-scoped inbox, preferences, and
|
||||
// the websocket handshake ticket.
|
||||
if deps.Notifications != nil {
|
||||
if detailSvc != nil {
|
||||
deps.Notifications.SetImageResolver(detailSvc)
|
||||
}
|
||||
notificationsHandler := handlers.NewNotificationsHandler(deps.Notifications, deps.EventsHub)
|
||||
r.With(apimw.RequireProfile).Post("/events/ws-ticket", notificationsHandler.HandleMintWSTicket)
|
||||
// Discord DM channel: the linked identity and mode hang off
|
||||
// the login account, not a profile, so these stay outside
|
||||
// the RequireProfile subrouter below (static paths coexist
|
||||
// with it, same as the public email-link routes above).
|
||||
if discordNotificationsHandler != nil {
|
||||
r.Get("/notifications/discord-preferences", discordNotificationsHandler.HandleGetPreferences)
|
||||
r.Put("/notifications/discord-preferences", discordNotificationsHandler.HandleUpdatePreferences)
|
||||
r.Delete("/notifications/discord-link", discordNotificationsHandler.HandleUnlink)
|
||||
r.Post("/notifications/discord/link/init", discordNotificationsHandler.HandleLinkInit)
|
||||
}
|
||||
r.Route("/notifications", func(r chi.Router) {
|
||||
r.Use(apimw.RequireProfile)
|
||||
r.Get("/", notificationsHandler.HandleList)
|
||||
r.Get("/sync", notificationsHandler.HandleSync)
|
||||
r.Get("/unread-count", notificationsHandler.HandleUnreadCount)
|
||||
r.Get("/capability", notificationsHandler.HandleCapability)
|
||||
r.Get("/preferences", notificationsHandler.HandleGetPreferences)
|
||||
r.Put("/preferences", notificationsHandler.HandleUpdatePreferences)
|
||||
r.Get("/email-preferences", notificationsHandler.HandleGetEmailPreferences)
|
||||
r.Put("/email-preferences", notificationsHandler.HandleUpdateEmailPreferences)
|
||||
r.Put("/email-preferences/address", notificationsHandler.HandleRequestEmailAddress)
|
||||
r.Delete("/email-preferences/address", notificationsHandler.HandleClearEmailAddress)
|
||||
r.Post("/read-all", notificationsHandler.HandleReadAll)
|
||||
r.Route("/webhooks", func(r chi.Router) {
|
||||
r.Get("/", notificationsHandler.HandleListWebhooks)
|
||||
r.Post("/", notificationsHandler.HandleCreateWebhook)
|
||||
r.Put("/{id}", notificationsHandler.HandleUpdateWebhook)
|
||||
r.Delete("/{id}", notificationsHandler.HandleDeleteWebhook)
|
||||
r.Post("/{id}/rotate-secret", notificationsHandler.HandleRotateWebhookSecret)
|
||||
r.Post("/{id}/test", notificationsHandler.HandleTestWebhook)
|
||||
})
|
||||
r.Route("/web-push", func(r chi.Router) {
|
||||
r.Get("/subscriptions", notificationsHandler.HandleWebPushList)
|
||||
r.Post("/subscriptions", notificationsHandler.HandleWebPushSubscribe)
|
||||
r.Delete("/subscriptions/{id}", notificationsHandler.HandleWebPushDelete)
|
||||
r.Post("/unsubscribe", notificationsHandler.HandleWebPushUnsubscribe)
|
||||
})
|
||||
r.Get("/{id}", notificationsHandler.HandleGet)
|
||||
r.Post("/{id}/read", notificationsHandler.HandleMarkRead)
|
||||
})
|
||||
}
|
||||
|
||||
// Marker read/write/clear for any authenticated viewer: users
|
||||
// fix and create intro/recap/credits/preview markers from the
|
||||
// player. Writes are stamped source="manual" and contributed to
|
||||
@@ -1537,7 +1648,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
// Library management routes (admin-only).
|
||||
if libraryHandler != nil {
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(apimw.RequireAdmin)
|
||||
r.Use(requireActingAdmin)
|
||||
|
||||
r.Route("/libraries", func(r chi.Router) {
|
||||
r.Get("/", libraryHandler.HandleListLibraries)
|
||||
@@ -1663,36 +1774,6 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
}
|
||||
}
|
||||
|
||||
// Notifications inbox and preferences (profile-scoped).
|
||||
if deps.NotificationsService != nil {
|
||||
notificationsHandler := handlers.NewNotificationsHandler(deps.NotificationsService, deps.UserStoreProvider)
|
||||
r.Route("/notifications", func(r chi.Router) {
|
||||
r.Use(apimw.RequireProfile)
|
||||
r.Get("/", notificationsHandler.HandleList)
|
||||
r.Get("/unread-count", notificationsHandler.HandleUnreadCount)
|
||||
r.Post("/read", notificationsHandler.HandleMarkRead)
|
||||
r.Post("/{id}/dismiss", notificationsHandler.HandleDismiss)
|
||||
r.Get("/preferences", notificationsHandler.HandleGetPreferences)
|
||||
r.Put("/preferences", notificationsHandler.HandlePutPreferences)
|
||||
})
|
||||
}
|
||||
|
||||
// Push registration, device management, and VAPID key.
|
||||
// Register and revoke are profile-scoped: they store a profileID alongside the
|
||||
// push token so that notifications are delivered to the correct profile.
|
||||
// RequireProfile is applied per-route (not via r.Use) to leave the three
|
||||
// user-scoped endpoints (devices list, toggle, webpush-key) without it.
|
||||
if deps.PushStore != nil && deps.PushConfig != nil {
|
||||
pushHandler := handlers.NewPushHandler(deps.PushStore, deps.PushConfig, deps.NotificationsService)
|
||||
r.Route("/notifications/push", func(r chi.Router) {
|
||||
r.With(apimw.RequireProfile).Put("/device", pushHandler.HandleRegister)
|
||||
r.With(apimw.RequireProfile).Delete("/device", pushHandler.HandleRevoke)
|
||||
r.Get("/devices", pushHandler.HandleListDevices)
|
||||
r.Put("/devices/{device_id}", pushHandler.HandleToggleDevice)
|
||||
r.Get("/webpush-key", pushHandler.HandleWebPushKey)
|
||||
})
|
||||
}
|
||||
|
||||
// Progress and sync routes (profile-scoped).
|
||||
if progressHandler != nil {
|
||||
r.Route("/progress", func(r chi.Router) {
|
||||
@@ -1724,6 +1805,12 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Route("/collections", func(r chi.Router) {
|
||||
r.Use(apimw.RequireProfile)
|
||||
r.Get("/", collectionHandler.HandleListCollections)
|
||||
if libraryCollectionHandler != nil {
|
||||
// Aggregated server (admin-curated) collections across
|
||||
// every accessible library. Separate from "/" (personal,
|
||||
// editable) by design — different access + cache lifecycle.
|
||||
r.Get("/server", libraryCollectionHandler.HandleListServerCollections)
|
||||
}
|
||||
r.Post("/", collectionHandler.HandleCreateCollection)
|
||||
r.Post("/preview", collectionHandler.HandlePreviewCollection)
|
||||
r.Put("/order", collectionHandler.HandleReorderCollections)
|
||||
@@ -1807,6 +1894,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
deps.PluginHTTPProxy,
|
||||
metadata.NewChainRepository(deps.DB),
|
||||
deps.PluginImageResolver,
|
||||
restartStatus,
|
||||
)
|
||||
r.Get("/plugins", pluginHandler.HandleListUserPluginSettings)
|
||||
r.Get("/plugins/{installation_id}", pluginHandler.HandleGetUserPluginSettings)
|
||||
@@ -2079,7 +2167,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
// Admin routes.
|
||||
if adminHandler != nil {
|
||||
r.Route("/admin", func(r chi.Router) {
|
||||
metadataItemAccess := apimw.RequireAdmin
|
||||
metadataItemAccess := requireActingAdmin
|
||||
if permissionMiddleware != nil {
|
||||
metadataItemAccess = permissionMiddleware.RequireMetadataCurationForItem
|
||||
}
|
||||
@@ -2106,7 +2194,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
}
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(apimw.RequireAdmin)
|
||||
r.Use(requireActingAdmin)
|
||||
|
||||
r.Get("/users", adminHandler.HandleListUsers)
|
||||
r.Post("/users", adminHandler.HandleCreateUser)
|
||||
@@ -2132,7 +2220,13 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Get("/playback-history", adminHandler.HandleListPlaybackHistory)
|
||||
r.Get("/unmatched", adminHandler.HandleListUnmatched)
|
||||
r.Get("/stats", adminHandler.HandleGetStats)
|
||||
r.Get("/server/status", adminHandler.HandleGetServerStatus)
|
||||
r.Post("/server/restart", serverControlHandler.HandleRestart)
|
||||
r.Get("/jellyfin-compat/status", adminHandler.HandleGetJellyfinCompatStatus)
|
||||
r.Patch("/jellyfin-compat/settings", adminHandler.HandleUpdateJellyfinCompatSettings)
|
||||
r.Post("/jellyfin-compat/web/install", adminHandler.HandleInstallJellyfinCompatWeb)
|
||||
r.Post("/jellyfin-compat/web/update", adminHandler.HandleUpdateJellyfinCompatWeb)
|
||||
r.Post("/jellyfin-compat/web/remove", adminHandler.HandleRemoveJellyfinCompatWeb)
|
||||
r.Get("/settings/sensitive-status", adminHandler.HandleGetSensitiveStatus)
|
||||
r.Post("/settings/check/{kind}", adminHandler.HandleCheckSettingsConnection)
|
||||
if sectionSettingsHandler != nil {
|
||||
@@ -2142,6 +2236,30 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Get("/settings/{key}", adminHandler.HandleGetSetting)
|
||||
r.Get("/settings", adminHandler.HandleGetSettings)
|
||||
r.Put("/settings/{key}", adminHandler.HandleUpdateSetting)
|
||||
if brandingHandler != nil {
|
||||
// Branding image upload/delete (scalar branding
|
||||
// fields use the generic settings PUT above).
|
||||
r.Post("/branding/assets/{kind}", brandingHandler.HandleUploadAsset)
|
||||
r.Delete("/branding/assets/{kind}", brandingHandler.HandleDeleteAsset)
|
||||
}
|
||||
if settingsRepo != nil {
|
||||
emailHandler := handlers.NewEmailHandler(mail.NewSMTPSender(settingsRepo))
|
||||
r.Post("/email/test", emailHandler.HandleTest)
|
||||
}
|
||||
if discordNotificationsHandler != nil {
|
||||
r.Post("/notifications/discord/test", discordNotificationsHandler.HandleAdminTest)
|
||||
}
|
||||
if deps.Notifications != nil && deps.Notifications.ServerChannels != nil {
|
||||
serverChannelsHandler := handlers.NewAdminServerChannelsHandler(deps.Notifications)
|
||||
r.Route("/notifications/server-channels", func(r chi.Router) {
|
||||
r.Get("/", serverChannelsHandler.HandleList)
|
||||
r.Post("/", serverChannelsHandler.HandleCreate)
|
||||
r.Put("/{id}", serverChannelsHandler.HandleUpdate)
|
||||
r.Delete("/{id}", serverChannelsHandler.HandleDelete)
|
||||
r.Post("/{id}/rotate-secret", serverChannelsHandler.HandleRotateSecret)
|
||||
r.Post("/{id}/test", serverChannelsHandler.HandleTest)
|
||||
})
|
||||
}
|
||||
if adminIntroHandler != nil {
|
||||
r.Post("/items/{id}/refresh-markers", adminIntroHandler.HandleRefreshEpisodeMarkers)
|
||||
r.Post("/items/{id}/redetect-intro", adminIntroHandler.HandleRedetectEpisodeIntro)
|
||||
@@ -2203,6 +2321,7 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
deps.PluginHTTPProxy,
|
||||
metadata.NewChainRepository(deps.DB),
|
||||
deps.PluginImageResolver,
|
||||
restartStatus,
|
||||
)
|
||||
r.Route("/plugins", func(r chi.Router) {
|
||||
r.Get("/repositories", pluginHandler.HandleListRepositories)
|
||||
@@ -2380,10 +2499,10 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
|
||||
// Rate limit admin routes. Mounted even when the limiter is not
|
||||
// running (deps.RateLimitMW == nil) so admins can always reach the
|
||||
// config — otherwise disabling rate limiting and restarting would
|
||||
// permanently lock the settings page out of re-enabling it.
|
||||
// config; otherwise disabling rate limiting and restarting would
|
||||
// lock the settings page out of re-enabling it.
|
||||
if settingsRepo != nil {
|
||||
rateLimitHandler := handlers.NewRateLimitHandler(settingsRepo, deps.RateLimitMW, deps.EventBus)
|
||||
rateLimitHandler := handlers.NewRateLimitHandler(settingsRepo, deps.RateLimitMW, deps.EventBus, restartStatus)
|
||||
r.Route("/rate-limits", func(r chi.Router) {
|
||||
r.Get("/config", rateLimitHandler.HandleGetConfig)
|
||||
r.Put("/config", rateLimitHandler.HandleUpdateConfig)
|
||||
@@ -2447,22 +2566,6 @@ func NewRouter(deps Dependencies) chi.Router {
|
||||
r.Get("/logs/audit", adminLogsHandler.HandleListAuditLogs)
|
||||
r.Get("/logs/ws", adminLogsHandler.HandleLogStreamWebSocket)
|
||||
}
|
||||
if deps.NotificationsService != nil {
|
||||
notificationsHandler := handlers.NewNotificationsHandler(deps.NotificationsService, deps.UserStoreProvider)
|
||||
r.Route("/announcements", func(r chi.Router) {
|
||||
r.Get("/", notificationsHandler.HandleListAnnouncements)
|
||||
r.Post("/", notificationsHandler.HandleCreateAnnouncement)
|
||||
r.Delete("/{id}", notificationsHandler.HandleDeleteAnnouncement)
|
||||
})
|
||||
}
|
||||
|
||||
if deps.PushStore != nil && deps.PushConfig != nil {
|
||||
pushHandler := handlers.NewPushHandler(deps.PushStore, deps.PushConfig, deps.NotificationsService)
|
||||
r.Get("/push/status", pushHandler.HandleAdminStatus)
|
||||
r.Post("/push/generate-vapid-keys", pushHandler.HandleGenerateVAPIDKeys)
|
||||
r.Post("/push/test", pushHandler.HandleSendTestPush)
|
||||
}
|
||||
|
||||
if adminPlaybackControlHandler != nil {
|
||||
r.Post("/sessions/{session_id}/pause", adminPlaybackControlHandler.HandlePauseSession)
|
||||
r.Post("/sessions/{session_id}/resume", adminPlaybackControlHandler.HandleResumeSession)
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
package branding
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/imageutil"
|
||||
)
|
||||
|
||||
// assetSpec describes how one branding image kind is validated, processed, and
|
||||
// stored.
|
||||
type assetSpec struct {
|
||||
kind AssetKind
|
||||
settingKey string // server_settings key holding the current content ref
|
||||
s3Prefix string // S3 key prefix; object key is "<prefix>/<ref>"
|
||||
maxBytes int64
|
||||
// process validates the declared content type and returns the bytes to
|
||||
// store, the content type to serve them with, and the file extension used
|
||||
// in the content ref.
|
||||
process func(data []byte, declaredType string) (out []byte, serveType, ext string, err error)
|
||||
}
|
||||
|
||||
// assetSpecs is the registry of all branding asset kinds. Adding a kind here is
|
||||
// the only change needed to support a new uploadable asset.
|
||||
var assetSpecs = map[AssetKind]assetSpec{
|
||||
KindWordmark: {
|
||||
kind: KindWordmark,
|
||||
settingKey: "branding.wordmark_ref",
|
||||
s3Prefix: "branding/wordmark",
|
||||
maxBytes: 8 << 20,
|
||||
process: processWebP(imageutil.GenerateVariants, 640),
|
||||
},
|
||||
KindMark: {
|
||||
kind: KindMark,
|
||||
settingKey: "branding.mark_ref",
|
||||
s3Prefix: "branding/mark",
|
||||
maxBytes: 8 << 20,
|
||||
process: processWebP(imageutil.GenerateSquareVariants, 512),
|
||||
},
|
||||
KindFavicon: {
|
||||
kind: KindFavicon,
|
||||
settingKey: "branding.favicon_ref",
|
||||
s3Prefix: "branding/favicon",
|
||||
maxBytes: 1 << 20,
|
||||
process: processFaviconPassthrough,
|
||||
},
|
||||
KindLoginBg: {
|
||||
kind: KindLoginBg,
|
||||
settingKey: "branding.login_bg_ref",
|
||||
s3Prefix: "branding/login-bg",
|
||||
maxBytes: 12 << 20,
|
||||
process: processWebP(imageutil.GenerateVariants, 2560),
|
||||
},
|
||||
}
|
||||
|
||||
// imageUploadTypes is the accepted set for WebP-converted kinds.
|
||||
var imageUploadTypes = map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/png": true,
|
||||
"image/webp": true,
|
||||
}
|
||||
|
||||
// imageVariantFunc matches imageutil.GenerateVariants / GenerateSquareVariants.
|
||||
type imageVariantFunc func(data []byte, sizes []int) (*imageutil.VariantResult, error)
|
||||
|
||||
// processWebP re-encodes any accepted image to a size-capped WebP using the
|
||||
// given variant generator (width-preserving or square). The aspect ratio is
|
||||
// preserved by the generator; narrower images are not upscaled.
|
||||
func processWebP(generate imageVariantFunc, size int) func([]byte, string) ([]byte, string, string, error) {
|
||||
return func(data []byte, declaredType string) ([]byte, string, string, error) {
|
||||
if !imageUploadTypes[declaredType] {
|
||||
return nil, "", "", ErrUnsupportedImage
|
||||
}
|
||||
res, err := generate(data, []int{size})
|
||||
if err != nil {
|
||||
return nil, "", "", ErrUnsupportedImage
|
||||
}
|
||||
return pickVariant(res, fmt.Sprintf("w%d", size)), "image/webp", ".webp", nil
|
||||
}
|
||||
}
|
||||
|
||||
// processFaviconPassthrough stores the favicon unchanged (no WebP re-encode) so
|
||||
// that .ico/.png keep working in browsers that don't render WebP favicons.
|
||||
func processFaviconPassthrough(data []byte, declaredType string) ([]byte, string, string, error) {
|
||||
switch declaredType {
|
||||
case "image/png":
|
||||
return data, "image/png", ".png", nil
|
||||
case "image/webp":
|
||||
return data, "image/webp", ".webp", nil
|
||||
case "image/x-icon", "image/vnd.microsoft.icon":
|
||||
return data, "image/x-icon", ".ico", nil
|
||||
case "image/svg+xml":
|
||||
return data, "image/svg+xml", ".svg", nil
|
||||
default:
|
||||
return nil, "", "", ErrUnsupportedImage
|
||||
}
|
||||
}
|
||||
|
||||
// pickVariant returns the named variant's bytes, falling back to the re-encoded
|
||||
// original when the exact width variant is absent.
|
||||
func pickVariant(res *imageutil.VariantResult, key string) []byte {
|
||||
var original []byte
|
||||
for _, v := range res.Variants {
|
||||
if v.Key == key {
|
||||
return v.Data
|
||||
}
|
||||
if v.Key == "original" {
|
||||
original = v.Data
|
||||
}
|
||||
}
|
||||
return original
|
||||
}
|
||||
|
||||
// MaxUploadBytes returns the maximum accepted upload size for a kind, or 0 when
|
||||
// the kind is unknown.
|
||||
func MaxUploadBytes(kind AssetKind) int64 {
|
||||
if spec, ok := assetSpecs[kind]; ok {
|
||||
return spec.maxBytes
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// contentTypeForExt maps a stored ref's extension to the Content-Type used when
|
||||
// serving it.
|
||||
func contentTypeForExt(ext string) string {
|
||||
switch ext {
|
||||
case ".webp":
|
||||
return "image/webp"
|
||||
case ".png":
|
||||
return "image/png"
|
||||
case ".ico":
|
||||
return "image/x-icon"
|
||||
case ".svg":
|
||||
return "image/svg+xml"
|
||||
default:
|
||||
return "application/octet-stream"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Package branding is the single source of truth for server white-labeling:
|
||||
// server name, custom logos (wordmark + mark), favicon, login background, and
|
||||
// the derived browser tab title, web app manifest, and theme color.
|
||||
//
|
||||
// It is intentionally a leaf package (it depends only on imageutil and the
|
||||
// s3client value type via small interfaces) so that both internal/server (which
|
||||
// templates index.html and serves the manifest/favicon) and
|
||||
// internal/api/handlers (which exposes the public read + admin upload endpoints)
|
||||
// can depend on it without an import cycle.
|
||||
package branding
|
||||
|
||||
import "errors"
|
||||
|
||||
// AssetKind identifies an uploadable branding image.
|
||||
type AssetKind string
|
||||
|
||||
const (
|
||||
// KindWordmark is the wide logo shown in the expanded sidebar.
|
||||
KindWordmark AssetKind = "wordmark"
|
||||
// KindMark is the square icon shown in the collapsed sidebar and PWA install.
|
||||
KindMark AssetKind = "mark"
|
||||
// KindFavicon is the browser tab icon. Served as-is (no WebP re-encode) so
|
||||
// Safari and mobile browsers keep working.
|
||||
KindFavicon AssetKind = "favicon"
|
||||
// KindLoginBg is the background image for the auth pages.
|
||||
KindLoginBg AssetKind = "login_bg"
|
||||
)
|
||||
|
||||
// Scalar branding settings keys (stored in the server_settings table). Asset
|
||||
// keys live on each assetSpec.
|
||||
const (
|
||||
KeyServerName = "branding.server_name"
|
||||
KeyLoginSubtitle = "branding.login_subtitle"
|
||||
KeyAccentColor = "branding.accent_color"
|
||||
KeyDefaultTheme = "branding.default_theme"
|
||||
)
|
||||
|
||||
// Defaults applied when a branding setting is unset. ServerName/LoginSubtitle
|
||||
// mirror the frontend's hardcoded fallbacks so behavior is unchanged out of the
|
||||
// box.
|
||||
const (
|
||||
DefaultServerName = "Silo"
|
||||
DefaultLoginSubtitle = "Sign in with an existing account."
|
||||
// DefaultThemeColor is used for the PWA manifest theme/background color when
|
||||
// no accent color is configured.
|
||||
DefaultThemeColor = "#0b0b0f"
|
||||
)
|
||||
|
||||
// assetURLBase is the public, stable path prefix for serving branding assets.
|
||||
// Assets are addressed by content ref (?v=<hash><ext>) for immutable caching.
|
||||
const assetURLBase = "/api/v1/branding/assets/"
|
||||
|
||||
// AssetContentSecurityPolicy hardens every served branding asset response.
|
||||
//
|
||||
// SECURITY: the favicon accepts SVG, a valid favicon format that can embed
|
||||
// <script> and on* handlers. Served on the app origin and navigated to
|
||||
// directly, such an SVG would otherwise execute in the viewer's session
|
||||
// (stored XSS) — X-Content-Type-Options alone does not stop a correctly-typed
|
||||
// SVG document from running scripts. `sandbox` (no allow-tokens) forces an
|
||||
// opaque origin with scripting disabled, and `default-src 'none'` blocks all
|
||||
// fetches; the asset still renders fine as an <img>/<link rel="icon">
|
||||
// subresource (where this policy does not apply). Harmless for raster images.
|
||||
const AssetContentSecurityPolicy = "default-src 'none'; style-src 'unsafe-inline'; sandbox"
|
||||
|
||||
// Errors returned by Service. Handlers map these to HTTP status codes.
|
||||
var (
|
||||
// ErrStorageUnavailable indicates S3 is not configured; branding image
|
||||
// upload/serving is unavailable but text branding still works.
|
||||
ErrStorageUnavailable = errors.New("branding: asset storage is not configured")
|
||||
// ErrAssetNotConfigured indicates no custom asset of the requested kind is set.
|
||||
ErrAssetNotConfigured = errors.New("branding: asset not configured")
|
||||
// ErrInvalidKind indicates an unknown asset kind.
|
||||
ErrInvalidKind = errors.New("branding: invalid asset kind")
|
||||
// ErrUnsupportedImage indicates the uploaded file is not an accepted image type.
|
||||
ErrUnsupportedImage = errors.New("branding: unsupported image type")
|
||||
)
|
||||
|
||||
// IsValidKind reports whether s names a known asset kind.
|
||||
func IsValidKind(s string) bool {
|
||||
_, ok := assetSpecs[AssetKind(s)]
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package branding
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Snapshot is an immutable view of the current branding configuration. It is
|
||||
// the input to HTML templating, manifest generation, and the public API
|
||||
// response.
|
||||
type Snapshot struct {
|
||||
ServerName string
|
||||
LoginSubtitle string
|
||||
AccentColor string // "" when unset
|
||||
DefaultTheme string // "" when unset
|
||||
|
||||
assets map[AssetKind]string // kind -> content ref ("<hash><ext>")
|
||||
}
|
||||
|
||||
// AssetRef returns the stored content ref for a kind, or "" when no custom
|
||||
// asset is set.
|
||||
func (s Snapshot) AssetRef(kind AssetKind) string { return s.assets[kind] }
|
||||
|
||||
// HasAsset reports whether a custom asset of the given kind is configured.
|
||||
func (s Snapshot) HasAsset(kind AssetKind) bool { return s.assets[kind] != "" }
|
||||
|
||||
// AssetURL returns the stable public URL for a custom asset, or "" when unset.
|
||||
// The content ref is carried as ?v= so the asset can be cached immutably.
|
||||
func (s Snapshot) AssetURL(kind AssetKind) string {
|
||||
return AssetURLFor(kind, s.assets[kind])
|
||||
}
|
||||
|
||||
// AssetURLFor builds the stable public URL for a kind given its content ref.
|
||||
// Returns "" when ref is empty.
|
||||
func AssetURLFor(kind AssetKind, ref string) string {
|
||||
if ref == "" {
|
||||
return ""
|
||||
}
|
||||
return assetURLBase + string(kind) + "?v=" + ref
|
||||
}
|
||||
|
||||
// ThemeColor returns the color used for the PWA manifest and the theme-color
|
||||
// meta tag: the configured accent color, or the default when unset.
|
||||
func (s Snapshot) ThemeColor() string {
|
||||
if s.AccentColor != "" {
|
||||
return s.AccentColor
|
||||
}
|
||||
return DefaultThemeColor
|
||||
}
|
||||
|
||||
// Favicon link literal as it appears in web/index.html. Kept in sync with that
|
||||
// file; RenderIndexHTML rewrites it to a custom favicon when one is set.
|
||||
const indexFaviconLink = `<link rel="icon" href="/favicon.ico" sizes="any" />`
|
||||
|
||||
// RenderIndexHTML injects branding into the SPA shell: the browser tab title
|
||||
// and, when configured, the custom favicon link and a theme-color meta tag.
|
||||
// Favicon/manifest paths themselves are served dynamically by the frontend
|
||||
// handler, so only the title and the cache-bustable favicon href are rewritten
|
||||
// here. Replacements that don't match are no-ops, so a build that changes the
|
||||
// shell degrades gracefully to the bundled defaults.
|
||||
func RenderIndexHTML(index []byte, snap Snapshot) []byte {
|
||||
out := string(index)
|
||||
|
||||
out = strings.Replace(out,
|
||||
"<title>Silo</title>",
|
||||
"<title>"+html.EscapeString(snap.ServerName)+"</title>",
|
||||
1,
|
||||
)
|
||||
|
||||
if u := snap.AssetURL(KindFavicon); u != "" {
|
||||
out = strings.Replace(out,
|
||||
indexFaviconLink,
|
||||
`<link rel="icon" href="`+html.EscapeString(u)+`" sizes="any" />`,
|
||||
1,
|
||||
)
|
||||
}
|
||||
|
||||
if snap.AccentColor != "" {
|
||||
meta := `<meta name="theme-color" content="` + html.EscapeString(snap.AccentColor) + `" />`
|
||||
out = strings.Replace(out, "</head>", " "+meta+"\n </head>", 1)
|
||||
}
|
||||
|
||||
return []byte(out)
|
||||
}
|
||||
|
||||
type manifestIcon struct {
|
||||
Src string `json:"src"`
|
||||
Sizes string `json:"sizes"`
|
||||
Type string `json:"type"`
|
||||
Purpose string `json:"purpose,omitempty"`
|
||||
}
|
||||
|
||||
type webManifest struct {
|
||||
Name string `json:"name"`
|
||||
ShortName string `json:"short_name"`
|
||||
Icons []manifestIcon `json:"icons"`
|
||||
ThemeColor string `json:"theme_color"`
|
||||
BackgroundColor string `json:"background_color"`
|
||||
Display string `json:"display"`
|
||||
StartURL string `json:"start_url"`
|
||||
}
|
||||
|
||||
// RenderManifest builds the dynamic web app manifest from the snapshot. When a
|
||||
// custom mark is set its WebP is advertised at both common sizes; otherwise the
|
||||
// bundled PNG icon set is used.
|
||||
func RenderManifest(snap Snapshot) []byte {
|
||||
var icons []manifestIcon
|
||||
if u := snap.AssetURL(KindMark); u != "" {
|
||||
icons = []manifestIcon{
|
||||
{Src: u, Sizes: "192x192", Type: "image/webp"},
|
||||
{Src: u, Sizes: "512x512", Type: "image/webp"},
|
||||
{Src: u, Sizes: "512x512", Type: "image/webp", Purpose: "maskable"},
|
||||
}
|
||||
} else {
|
||||
icons = []manifestIcon{
|
||||
{Src: "/web-app-icon-192.png", Sizes: "192x192", Type: "image/png"},
|
||||
{Src: "/web-app-icon-512.png", Sizes: "512x512", Type: "image/png"},
|
||||
{Src: "/maskable-icon-512.png", Sizes: "512x512", Type: "image/png", Purpose: "maskable"},
|
||||
}
|
||||
}
|
||||
|
||||
m := webManifest{
|
||||
Name: snap.ServerName,
|
||||
ShortName: snap.ServerName,
|
||||
Icons: icons,
|
||||
ThemeColor: snap.ThemeColor(),
|
||||
BackgroundColor: snap.ThemeColor(),
|
||||
Display: "standalone",
|
||||
StartURL: "/",
|
||||
}
|
||||
// Marshaling a fixed struct cannot fail.
|
||||
b, _ := json.Marshal(m)
|
||||
return b
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package branding
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newSnapshot(name string) Snapshot {
|
||||
return Snapshot{ServerName: name, assets: map[AssetKind]string{}}
|
||||
}
|
||||
|
||||
func TestRenderIndexHTMLReplacesTitle(t *testing.T) {
|
||||
in := []byte(`<html><head><title>Silo</title></head><body></body></html>`)
|
||||
out := string(RenderIndexHTML(in, newSnapshot("Acme Media")))
|
||||
if !strings.Contains(out, "<title>Acme Media</title>") {
|
||||
t.Fatalf("title not replaced: %q", out)
|
||||
}
|
||||
if strings.Contains(out, "<title>Silo</title>") {
|
||||
t.Fatalf("default title still present: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIndexHTMLEscapesTitle(t *testing.T) {
|
||||
in := []byte(`<title>Silo</title></head>`)
|
||||
out := string(RenderIndexHTML(in, newSnapshot(`A&B<script>`)))
|
||||
if strings.Contains(out, "<script>") {
|
||||
t.Fatalf("title not escaped: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "A&B<script>") {
|
||||
t.Fatalf("expected escaped title, got: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIndexHTMLRewritesFaviconWhenSet(t *testing.T) {
|
||||
in := []byte(indexFaviconLink + "</head>")
|
||||
snap := Snapshot{ServerName: "X", assets: map[AssetKind]string{KindFavicon: "abc123.png"}}
|
||||
out := string(RenderIndexHTML(in, snap))
|
||||
if !strings.Contains(out, `href="/api/v1/branding/assets/favicon?v=abc123.png"`) {
|
||||
t.Fatalf("favicon not rewritten: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIndexHTMLKeepsDefaultFaviconWhenUnset(t *testing.T) {
|
||||
in := []byte(indexFaviconLink + "</head>")
|
||||
out := string(RenderIndexHTML(in, newSnapshot("X")))
|
||||
if !strings.Contains(out, indexFaviconLink) {
|
||||
t.Fatalf("default favicon link should be preserved: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIndexHTMLInjectsThemeColorOnlyWhenAccentSet(t *testing.T) {
|
||||
in := []byte("<head></head>")
|
||||
if out := string(RenderIndexHTML(in, newSnapshot("X"))); strings.Contains(out, "theme-color") {
|
||||
t.Fatalf("theme-color should not be injected without accent: %q", out)
|
||||
}
|
||||
snap := newSnapshot("X")
|
||||
snap.AccentColor = "#5bc39d"
|
||||
out := string(RenderIndexHTML(in, snap))
|
||||
if !strings.Contains(out, `<meta name="theme-color" content="#5bc39d" />`) {
|
||||
t.Fatalf("theme-color meta not injected: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenderIndexHTMLAgainstRealShell guards against web/index.html drifting
|
||||
// away from the literals RenderIndexHTML depends on.
|
||||
func TestRenderIndexHTMLAgainstRealShell(t *testing.T) {
|
||||
path := filepath.Join("..", "..", "web", "index.html")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Skipf("index.html not available: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(data), "<title>Silo</title>") {
|
||||
t.Fatalf("web/index.html no longer contains the expected <title>Silo</title>; update RenderIndexHTML")
|
||||
}
|
||||
if !strings.Contains(string(data), indexFaviconLink) {
|
||||
t.Fatalf("web/index.html no longer contains the expected favicon link %q; update indexFaviconLink", indexFaviconLink)
|
||||
}
|
||||
snap := Snapshot{ServerName: "Acme", assets: map[AssetKind]string{KindFavicon: "f00.png"}}
|
||||
out := string(RenderIndexHTML(data, snap))
|
||||
if !strings.Contains(out, "<title>Acme</title>") {
|
||||
t.Fatalf("title not replaced in real shell")
|
||||
}
|
||||
if !strings.Contains(out, "/api/v1/branding/assets/favicon?v=f00.png") {
|
||||
t.Fatalf("favicon not rewritten in real shell")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderManifestDefaults(t *testing.T) {
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(RenderManifest(newSnapshot("Acme TV")), &m); err != nil {
|
||||
t.Fatalf("invalid manifest JSON: %v", err)
|
||||
}
|
||||
if m["name"] != "Acme TV" || m["short_name"] != "Acme TV" {
|
||||
t.Fatalf("unexpected name: %v / %v", m["name"], m["short_name"])
|
||||
}
|
||||
if m["theme_color"] != DefaultThemeColor {
|
||||
t.Fatalf("expected default theme color, got %v", m["theme_color"])
|
||||
}
|
||||
icons, _ := m["icons"].([]any)
|
||||
if len(icons) != 3 {
|
||||
t.Fatalf("expected 3 default icons, got %d", len(icons))
|
||||
}
|
||||
first, _ := icons[0].(map[string]any)
|
||||
if !strings.HasPrefix(first["src"].(string), "/web-app-icon") {
|
||||
t.Fatalf("expected bundled icon default, got %v", first["src"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderManifestUsesCustomMark(t *testing.T) {
|
||||
snap := Snapshot{ServerName: "Acme", AccentColor: "#112233", assets: map[AssetKind]string{KindMark: "m1.webp"}}
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(RenderManifest(snap), &m); err != nil {
|
||||
t.Fatalf("invalid manifest JSON: %v", err)
|
||||
}
|
||||
if m["theme_color"] != "#112233" {
|
||||
t.Fatalf("expected accent theme color, got %v", m["theme_color"])
|
||||
}
|
||||
icons, _ := m["icons"].([]any)
|
||||
first, _ := icons[0].(map[string]any)
|
||||
if !strings.Contains(first["src"].(string), "/api/v1/branding/assets/mark?v=m1.webp") {
|
||||
t.Fatalf("expected custom mark icon URL, got %v", first["src"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package branding
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"path"
|
||||
|
||||
"github.com/Silo-Server/silo-server/internal/s3client"
|
||||
)
|
||||
|
||||
// SettingsStore is the subset of the server settings repository the branding
|
||||
// service needs.
|
||||
type SettingsStore interface {
|
||||
Get(ctx context.Context, key string) (string, error)
|
||||
Set(ctx context.Context, key, value string) error
|
||||
}
|
||||
|
||||
// AssetStore is the subset of the S3 client used for branding asset bytes.
|
||||
type AssetStore interface {
|
||||
PutObject(ctx context.Context, bucket, key string, data []byte) error
|
||||
GetObject(ctx context.Context, bucket, key string) ([]byte, error)
|
||||
Bucket() string
|
||||
}
|
||||
|
||||
// Service is the single source of truth for branding. It assembles a Snapshot
|
||||
// from settings, processes/stores uploaded assets, and streams them back.
|
||||
//
|
||||
// The store is optional: when S3 is not configured it is nil and asset
|
||||
// upload/serving returns ErrStorageUnavailable, while text branding (name,
|
||||
// subtitle, accent, default theme) keeps working.
|
||||
type Service struct {
|
||||
settings SettingsStore
|
||||
store AssetStore
|
||||
}
|
||||
|
||||
// NewService constructs a branding Service. Pass a nil store when S3 is not
|
||||
// configured: text branding keeps working while asset upload/serve returns
|
||||
// ErrStorageUnavailable. Callers holding a concrete *s3client.Client must pass
|
||||
// a nil AssetStore when that client is nil (rather than the typed-nil pointer)
|
||||
// to avoid the typed-nil interface trap — see the construction in cmd/silo.
|
||||
func NewService(settings SettingsStore, store AssetStore) *Service {
|
||||
return &Service{settings: settings, store: store}
|
||||
}
|
||||
|
||||
// HasStorage reports whether asset upload/serving is available.
|
||||
func (s *Service) HasStorage() bool { return s != nil && s.store != nil }
|
||||
|
||||
// Load reads the current branding configuration. Per-key read errors are
|
||||
// tolerated and fall back to defaults so the SPA always renders.
|
||||
func (s *Service) Load(ctx context.Context) Snapshot {
|
||||
get := func(key string) string {
|
||||
v, _ := s.settings.Get(ctx, key)
|
||||
return v
|
||||
}
|
||||
snap := Snapshot{
|
||||
ServerName: firstNonEmpty(get(KeyServerName), DefaultServerName),
|
||||
LoginSubtitle: firstNonEmpty(get(KeyLoginSubtitle), DefaultLoginSubtitle),
|
||||
AccentColor: get(KeyAccentColor),
|
||||
DefaultTheme: get(KeyDefaultTheme),
|
||||
assets: make(map[AssetKind]string, len(assetSpecs)),
|
||||
}
|
||||
for kind, spec := range assetSpecs {
|
||||
if ref := get(spec.settingKey); ref != "" {
|
||||
snap.assets[kind] = ref
|
||||
}
|
||||
}
|
||||
return snap
|
||||
}
|
||||
|
||||
// UploadAsset validates, processes, and stores an uploaded branding image,
|
||||
// recording its content ref in settings. It returns the new ref ("<hash><ext>").
|
||||
func (s *Service) UploadAsset(ctx context.Context, kind AssetKind, data []byte, declaredType string) (string, error) {
|
||||
spec, ok := assetSpecs[kind]
|
||||
if !ok {
|
||||
return "", ErrInvalidKind
|
||||
}
|
||||
if !s.HasStorage() {
|
||||
return "", ErrStorageUnavailable
|
||||
}
|
||||
|
||||
out, _, ext, err := spec.process(data, declaredType)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Content-address by the hash of the *stored* bytes: the ref then truly
|
||||
// identifies what is served, so the ?v=<ref> cache-buster changes exactly
|
||||
// when the served content changes (and identical outputs dedupe).
|
||||
sum := sha256.Sum256(out)
|
||||
ref := hex.EncodeToString(sum[:])[:16] + ext
|
||||
key := spec.s3Prefix + "/" + ref
|
||||
|
||||
if err := s.store.PutObject(ctx, s.store.Bucket(), key, out); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := s.settings.Set(ctx, spec.settingKey, ref); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
// DeleteAsset clears the custom asset of the given kind. The S3 object is left
|
||||
// in place (orphaned objects are cheap and avoid concurrent-reader races); the
|
||||
// empty settings value is what deactivates it.
|
||||
func (s *Service) DeleteAsset(ctx context.Context, kind AssetKind) error {
|
||||
spec, ok := assetSpecs[kind]
|
||||
if !ok {
|
||||
return ErrInvalidKind
|
||||
}
|
||||
return s.settings.Set(ctx, spec.settingKey, "")
|
||||
}
|
||||
|
||||
// GetAsset fetches the bytes of the current custom asset of the given kind.
|
||||
// Returns ErrAssetNotConfigured when none is set, ErrStorageUnavailable when S3
|
||||
// is absent, and ErrAssetNotConfigured when the object is missing in S3.
|
||||
func (s *Service) GetAsset(ctx context.Context, kind AssetKind) (data []byte, contentType, ref string, err error) {
|
||||
spec, ok := assetSpecs[kind]
|
||||
if !ok {
|
||||
return nil, "", "", ErrInvalidKind
|
||||
}
|
||||
ref, _ = s.settings.Get(ctx, spec.settingKey)
|
||||
if ref == "" {
|
||||
return nil, "", "", ErrAssetNotConfigured
|
||||
}
|
||||
if !s.HasStorage() {
|
||||
return nil, "", "", ErrStorageUnavailable
|
||||
}
|
||||
key := spec.s3Prefix + "/" + ref
|
||||
data, err = s.store.GetObject(ctx, s.store.Bucket(), key)
|
||||
if err != nil {
|
||||
if errors.Is(err, s3client.ErrNotFound) {
|
||||
return nil, "", "", ErrAssetNotConfigured
|
||||
}
|
||||
return nil, "", "", err
|
||||
}
|
||||
return data, contentTypeForExt(path.Ext(ref)), ref, nil
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, v := range values {
|
||||
if v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -92,15 +92,18 @@ var SensitiveSettingKeys = map[string]bool{
|
||||
"requests.radarr.api_key": true,
|
||||
"requests.sonarr.api_key": true,
|
||||
|
||||
// Push notification provider credentials.
|
||||
"push.apns.p8_key": true,
|
||||
"push.apns.key_id": true,
|
||||
"push.apns.team_id": true,
|
||||
"push.apns.bundle_id": true,
|
||||
"push.fcm.service_account_json": true,
|
||||
"push.webpush.vapid_private": true,
|
||||
"push.webpush.vapid_public": true,
|
||||
"push.webpush.subject": true,
|
||||
// Shared outbound email (internal/mail) SMTP credential.
|
||||
"email.smtp_password": true,
|
||||
|
||||
// Discord notification integration. The client_id is public in Discord's
|
||||
// own UI, so only the secret and bot token are encrypted.
|
||||
"discord.client_secret": true,
|
||||
"discord.bot_token": true,
|
||||
|
||||
// Web Push VAPID keypair JSON (generated + persisted atomically as one
|
||||
// value by the notifications system; clients receive the public half via
|
||||
// the capability endpoint, never from the settings store).
|
||||
"notifications.web_push.vapid_keypair": true,
|
||||
}
|
||||
|
||||
// EncryptedSettingsRepo decorates a raw settings store, transparently
|
||||
@@ -137,6 +140,30 @@ func (r *EncryptedSettingsRepo) Set(ctx context.Context, key, value string) erro
|
||||
return r.inner.Set(ctx, key, value)
|
||||
}
|
||||
|
||||
// settingsConditionalWriter is the optional conditional-write capability of a
|
||||
// raw settings store (satisfied by *ServerSettingsRepo).
|
||||
type settingsConditionalWriter interface {
|
||||
SetIfAbsent(ctx context.Context, key, value string) (bool, error)
|
||||
}
|
||||
|
||||
// SetIfAbsent applies Set's encryption contract to a conditional write: the
|
||||
// value lands only when the key currently has no value, so concurrent
|
||||
// provisioners of generated secrets cannot overwrite each other.
|
||||
func (r *EncryptedSettingsRepo) SetIfAbsent(ctx context.Context, key, value string) (bool, error) {
|
||||
inner, ok := r.inner.(settingsConditionalWriter)
|
||||
if !ok {
|
||||
return false, fmt.Errorf("settings store does not support conditional writes")
|
||||
}
|
||||
if SensitiveSettingKeys[key] && value != "" {
|
||||
ct, err := r.cipher.Encrypt(value, secret.SettingsAAD(key))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("encrypt setting %q: %w", key, err)
|
||||
}
|
||||
value = ct
|
||||
}
|
||||
return inner.SetIfAbsent(ctx, key, value)
|
||||
}
|
||||
|
||||
// Get reads a value and applies the read-path contract: legacy plaintext passes
|
||||
// through, an enc:v1: value is decrypted, and a corrupt ciphertext errors.
|
||||
func (r *EncryptedSettingsRepo) Get(ctx context.Context, key string) (string, error) {
|
||||
|
||||
@@ -154,6 +154,10 @@ func TestSensitiveSettingKeys_Audited(t *testing.T) {
|
||||
"requests.radarr.api_key",
|
||||
"requests.sonarr.api_key",
|
||||
"watchsync.trakt.client_secret",
|
||||
"email.smtp_password",
|
||||
"discord.client_secret",
|
||||
"discord.bot_token",
|
||||
"notifications.web_push.vapid_keypair",
|
||||
}
|
||||
for _, k := range mustHave {
|
||||
if !SensitiveSettingKeys[k] {
|
||||
|
||||
@@ -29,6 +29,7 @@ const episodeCatalogSelectBody = `(
|
||||
COALESCE(e.tmdb_id, '') AS tmdb_id,
|
||||
COALESCE(e.tvdb_id, '') AS tvdb_id,
|
||||
COALESCE(NULLIF(s.poster_path, ''), NULLIF(si.poster_path, ''), NULLIF(e.still_path, ''), '') AS poster_path,
|
||||
''::text AS poster_source_path,
|
||||
COALESCE(NULLIF(s.poster_thumbhash, ''), NULLIF(si.poster_thumbhash, ''), NULLIF(e.still_thumbhash, ''), '') AS poster_thumbhash,
|
||||
COALESCE(si.backdrop_path, '') AS backdrop_path,
|
||||
COALESCE(si.backdrop_thumbhash, '') AS backdrop_thumbhash,
|
||||
|
||||
@@ -206,16 +206,77 @@ func buildHistoryDisplayBaseQuery(access AccessFilter, snapshot *time.Time) (str
|
||||
|
||||
ApplySectionAccessFilter("mi", access, &conditions, &args, &argIdx)
|
||||
|
||||
// display_id resolves a history row (which may be an episode) to its shown
|
||||
// item (the series, for episodes). For provider-anchored episode ids the
|
||||
// series is a pure string transform of the id (the format invariant), so we
|
||||
// skip the episodes_pkey probe entirely. Legacy Sonyflake and local episode
|
||||
// ids carry no embedded anchor, so they still fall back to the episodes
|
||||
// lookup. The join key is null-poisoned for anchored ids (= NULL is an
|
||||
// unsatisfiable b-tree scan key, so the planner never descends the index for
|
||||
// them) — an outer-only predicate like NOT LIKE would not actually skip the
|
||||
// probe.
|
||||
displayIDExpr := fmt.Sprintf(
|
||||
"COALESCE(%s, NULLIF(e.series_id, ''), h.media_item_id)",
|
||||
seriesFromAnchoredEpisodeExpr("h.media_item_id"),
|
||||
)
|
||||
|
||||
// Null-poison the episodes join key for fully-formed anchored episode ids so
|
||||
// the planner skips the episodes_pkey probe for them; everything else (legacy
|
||||
// Sonyflake, local, malformed) still falls back to the lookup.
|
||||
episodeJoinKey := fmt.Sprintf(
|
||||
"CASE WHEN %s THEN NULL ELSE h.media_item_id END",
|
||||
anchoredEpisodePredicate("h.media_item_id"),
|
||||
)
|
||||
|
||||
return fmt.Sprintf(
|
||||
`SELECT DISTINCT ON (history_events.display_id) history_events.display_id, history_events.watched_at
|
||||
FROM (
|
||||
SELECT COALESCE(NULLIF(e.series_id, ''), h.media_item_id) AS display_id, h.watched_at
|
||||
SELECT %[1]s AS display_id, h.watched_at
|
||||
FROM user_watch_history h
|
||||
LEFT JOIN episodes e ON e.content_id = h.media_item_id
|
||||
JOIN media_items mi ON mi.content_id = COALESCE(NULLIF(e.series_id, ''), h.media_item_id)
|
||||
WHERE %s
|
||||
LEFT JOIN episodes e
|
||||
ON e.content_id = %[3]s
|
||||
JOIN media_items mi ON mi.content_id = %[1]s
|
||||
WHERE %[2]s
|
||||
) history_events
|
||||
ORDER BY history_events.display_id ASC, history_events.watched_at DESC`,
|
||||
displayIDExpr,
|
||||
strings.Join(conditions, " AND "),
|
||||
episodeJoinKey,
|
||||
), args
|
||||
}
|
||||
|
||||
// anchoredEpisodePredicate is the SQL boolean that is TRUE only for a
|
||||
// fully-formed provider-anchored episode content_id —
|
||||
// episode-<provider>-<seriesId>-<season>-<episode>, i.e. five non-empty
|
||||
// "-"-separated components. It deliberately rejects a broader shape like
|
||||
// 'episode-broken': matching that on the prefix alone would transform it into
|
||||
// 'series-broken-' and skip the episodes fallback, so the row would vanish at
|
||||
// the media_items join. split_part is IMMUTABLE. The "-" delimiter matches the
|
||||
// content_id format (internal/contentid); keep the two in lockstep.
|
||||
func anchoredEpisodePredicate(col string) string {
|
||||
return fmt.Sprintf(
|
||||
`%[1]s LIKE 'episode-%%' `+
|
||||
`AND split_part(%[1]s, '-', 2) <> '' `+
|
||||
`AND split_part(%[1]s, '-', 3) <> '' `+
|
||||
`AND split_part(%[1]s, '-', 4) <> '' `+
|
||||
`AND split_part(%[1]s, '-', 5) <> ''`,
|
||||
col,
|
||||
)
|
||||
}
|
||||
|
||||
// seriesFromAnchoredEpisodeExpr returns a SQL expression that recovers a show's
|
||||
// content_id from a provider-anchored episode content_id by pure string
|
||||
// transform — episode-<p>-<sid>-<s>-<e> -> series-<p>-<sid> — per the format
|
||||
// invariant in docs/architecture/deterministic-content-id.md. It yields NULL
|
||||
// for any id that is not a fully-formed provider-anchored episode (movies,
|
||||
// series, local, legacy Sonyflake, or malformed episode ids), so callers
|
||||
// COALESCE to the episodes-table lookup for those. split_part/||/CASE are all
|
||||
// IMMUTABLE.
|
||||
func seriesFromAnchoredEpisodeExpr(col string) string {
|
||||
return fmt.Sprintf(
|
||||
`CASE WHEN %[2]s `+
|
||||
`THEN 'series-' || split_part(%[1]s, '-', 2) || '-' || split_part(%[1]s, '-', 3) END`,
|
||||
col,
|
||||
anchoredEpisodePredicate(col),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -42,8 +42,13 @@ func TestBuildHistoryDisplayBaseQueryIncludesSnapshotAndLibraryAccess(t *testing
|
||||
"media_item_libraries mil_disabled",
|
||||
"media_folder_id = ANY($5)",
|
||||
"mi.content_rating = ANY($",
|
||||
"LEFT JOIN episodes e ON e.content_id = h.media_item_id",
|
||||
"JOIN media_items mi ON mi.content_id = COALESCE(NULLIF(e.series_id, ''), h.media_item_id)",
|
||||
// Anchored episode ids resolve their show by string transform; the
|
||||
// episodes probe is null-poisoned (skipped) for them and kept only for
|
||||
// non-anchored (legacy/local/malformed) ids. The predicate requires the
|
||||
// full five-part episode form, not just the 'episode-' prefix.
|
||||
"LEFT JOIN episodes e\n\t\t\t\tON e.content_id = CASE WHEN h.media_item_id LIKE 'episode-%' AND split_part(h.media_item_id, '-', 2) <> '' AND split_part(h.media_item_id, '-', 3) <> '' AND split_part(h.media_item_id, '-', 4) <> '' AND split_part(h.media_item_id, '-', 5) <> '' THEN NULL ELSE h.media_item_id END",
|
||||
"split_part(h.media_item_id, '-', 2)",
|
||||
"JOIN media_items mi ON mi.content_id = COALESCE(",
|
||||
}
|
||||
for _, fragment := range expectedFragments {
|
||||
if !strings.Contains(query, fragment) {
|
||||
|
||||
@@ -117,7 +117,7 @@ var itemColumnNames = []string{
|
||||
"content_rating", "runtime", "overview", "tagline",
|
||||
"rating_imdb", "rating_tmdb", "rating_rt_critic", "rating_rt_audience",
|
||||
"imdb_id", "tmdb_id", "tvdb_id",
|
||||
"poster_path", "poster_thumbhash", "backdrop_path", "backdrop_thumbhash", "logo_path",
|
||||
"poster_path", "poster_source_path", "poster_thumbhash", "backdrop_path", "backdrop_thumbhash", "logo_path",
|
||||
"metadata_s3_path", "metadata_etag", "season_count",
|
||||
"studios", "networks", "countries", "keywords", "original_language", "release_date::text", "first_air_date", "last_air_date", "air_time", "air_timezone",
|
||||
"show_status",
|
||||
@@ -130,6 +130,7 @@ var itemColumnNames = []string{
|
||||
// lists coalesce them to ”.
|
||||
var nullableStringItemColumns = map[string]bool{
|
||||
"poster_path": true,
|
||||
"poster_source_path": true,
|
||||
"poster_thumbhash": true,
|
||||
"backdrop_path": true,
|
||||
"backdrop_thumbhash": true,
|
||||
@@ -215,6 +216,7 @@ func scanItem(row pgx.Row) (*models.MediaItem, error) {
|
||||
&item.TmdbID,
|
||||
&item.TvdbID,
|
||||
&item.PosterPath,
|
||||
&item.PosterSourcePath,
|
||||
&item.PosterThumbhash,
|
||||
&item.BackdropPath,
|
||||
&item.BackdropThumbhash,
|
||||
@@ -278,6 +280,7 @@ func scanItems(rows pgx.Rows) ([]*models.MediaItem, error) {
|
||||
&item.TmdbID,
|
||||
&item.TvdbID,
|
||||
&item.PosterPath,
|
||||
&item.PosterSourcePath,
|
||||
&item.PosterThumbhash,
|
||||
&item.BackdropPath,
|
||||
&item.BackdropThumbhash,
|
||||
@@ -351,6 +354,7 @@ func scanItemsWithTotal(rows pgx.Rows) ([]*models.MediaItem, int, error) {
|
||||
&item.TmdbID,
|
||||
&item.TvdbID,
|
||||
&item.PosterPath,
|
||||
&item.PosterSourcePath,
|
||||
&item.PosterThumbhash,
|
||||
&item.BackdropPath,
|
||||
&item.BackdropThumbhash,
|
||||
@@ -417,7 +421,7 @@ func (r *ItemRepository) upsert(ctx context.Context, execer itemExecer, item *mo
|
||||
content_rating, runtime, overview, tagline,
|
||||
rating_imdb, rating_tmdb, rating_rt_critic, rating_rt_audience,
|
||||
imdb_id, tmdb_id, tvdb_id,
|
||||
poster_path, poster_thumbhash, backdrop_path, backdrop_thumbhash, logo_path,
|
||||
poster_path, poster_source_path, poster_thumbhash, backdrop_path, backdrop_thumbhash, logo_path,
|
||||
metadata_s3_path, metadata_etag, season_count,
|
||||
studios, networks, countries, keywords, original_language, release_date, first_air_date, last_air_date, air_time, air_timezone,
|
||||
show_status,
|
||||
@@ -428,12 +432,12 @@ func (r *ItemRepository) upsert(ctx context.Context, execer itemExecer, item *mo
|
||||
$9, $10, $11, $12,
|
||||
$13, $14, $15, $16,
|
||||
$17, $18, $19,
|
||||
$20, $21, $22, $23, $24,
|
||||
$25, $26, $27,
|
||||
$28, $29, $30, $31, $32, $33, $34, $35, $36, $37,
|
||||
$38,
|
||||
$39, $40, $41,
|
||||
$42, $43, $44
|
||||
$20, $21, $22, $23, $24, $25,
|
||||
$26, $27, $28,
|
||||
$29, $30, $31, $32, $33, $34, $35, $36, $37, $38,
|
||||
$39,
|
||||
$40, $41, $42,
|
||||
$43, $44, $45
|
||||
)
|
||||
ON CONFLICT (content_id) DO UPDATE SET
|
||||
type = EXCLUDED.type,
|
||||
@@ -455,6 +459,7 @@ func (r *ItemRepository) upsert(ctx context.Context, execer itemExecer, item *mo
|
||||
tmdb_id = EXCLUDED.tmdb_id,
|
||||
tvdb_id = EXCLUDED.tvdb_id,
|
||||
poster_path = EXCLUDED.poster_path,
|
||||
poster_source_path = EXCLUDED.poster_source_path,
|
||||
poster_thumbhash = EXCLUDED.poster_thumbhash,
|
||||
backdrop_path = EXCLUDED.backdrop_path,
|
||||
backdrop_thumbhash = EXCLUDED.backdrop_thumbhash,
|
||||
@@ -502,6 +507,7 @@ func (r *ItemRepository) upsert(ctx context.Context, execer itemExecer, item *mo
|
||||
item.TmdbID,
|
||||
item.TvdbID,
|
||||
item.PosterPath,
|
||||
item.PosterSourcePath,
|
||||
item.PosterThumbhash,
|
||||
item.BackdropPath,
|
||||
item.BackdropThumbhash,
|
||||
@@ -915,6 +921,7 @@ func (r *ItemRepository) Search(ctx context.Context, query string, itemTypes []s
|
||||
// Argument order is intentionally fixed:
|
||||
//
|
||||
// $1 searchText (always)
|
||||
// $2 titlePrefixTsQuery (always)
|
||||
// itemType placeholders, allowed/disabled libraries, MaxContentRating
|
||||
// parsed.ExactTitleHint
|
||||
// parsed.Year (or NULL)
|
||||
@@ -930,8 +937,9 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
return "", "", nil
|
||||
}
|
||||
var conditions []string
|
||||
args = []any{searchText}
|
||||
argIdx := 2
|
||||
titlePrefixIdx := 2
|
||||
args = []any{searchText, buildTitlePrefixTsQuery(searchText)}
|
||||
argIdx := 3
|
||||
|
||||
// All title-side text on both sides of @@ flows through
|
||||
// public.normalize_search_text() (migrations 127 / 138), which strips
|
||||
@@ -949,12 +957,14 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
overviewVector := `to_tsvector('english', COALESCE(mi.overview, ''))`
|
||||
normalizedTitleExpr := `public.normalize_search_text(%s)`
|
||||
titleQuery := `websearch_to_tsquery('simple', public.normalize_search_text($1))`
|
||||
titlePrefixQuery := fmt.Sprintf("to_tsquery('simple', $%d)", titlePrefixIdx)
|
||||
titleMatch := fmt.Sprintf("%s @@ %s", titleVector, titleQuery)
|
||||
titlePrefixMatch := fmt.Sprintf("($%d <> '' AND %s @@ %s)", titlePrefixIdx, titleVector, titlePrefixQuery)
|
||||
overviewMatch := fmt.Sprintf("%s @@ websearch_to_tsquery('english', $1)", overviewVector)
|
||||
|
||||
// Keep the base match condition index-friendly; exact-title logic is used as
|
||||
// a ranking boost later, not as an additional scan predicate.
|
||||
conditions = append(conditions, fmt.Sprintf("(%s OR %s)", titleMatch, overviewMatch))
|
||||
conditions = append(conditions, fmt.Sprintf("(%s OR %s OR %s)", titleMatch, titlePrefixMatch, overviewMatch))
|
||||
|
||||
if len(itemTypes) > 0 {
|
||||
placeholders := make([]string, 0, len(itemTypes))
|
||||
@@ -1069,6 +1079,10 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
ELSE 0
|
||||
END) AS year_match,
|
||||
MAX(ts_rank_cd(%s, %s)) AS title_rank,
|
||||
MAX(CASE
|
||||
WHEN $%d <> '' THEN ts_rank_cd(%s, %s)
|
||||
ELSE 0
|
||||
END) AS title_prefix_rank,
|
||||
MAX(ts_rank_cd(%s, websearch_to_tsquery('english', $1))) AS overview_rank,
|
||||
MAX(CASE
|
||||
WHEN $%d <> '' THEN ts_rank_cd(%s, phraseto_tsquery('simple', public.normalize_search_text($%d)))
|
||||
@@ -1078,7 +1092,7 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
%s
|
||||
GROUP BY %s
|
||||
)
|
||||
`, qualifiedCols, exactTitleMatch, contiguousTitleMatch, yearIdx, yearIdx, titleVector, titleQuery, overviewVector, phraseIdx, titleVector, phraseIdx, fromClause, whereClause, groupByCols)
|
||||
`, qualifiedCols, exactTitleMatch, contiguousTitleMatch, yearIdx, yearIdx, titleVector, titleQuery, titlePrefixIdx, titleVector, titlePrefixQuery, overviewVector, phraseIdx, titleVector, phraseIdx, fromClause, whereClause, groupByCols)
|
||||
|
||||
// COUNT(*) OVER () runs after the GROUP BY in the scored CTE collapses
|
||||
// duplicates from the library JOIN, so the window count preserves the
|
||||
@@ -1103,7 +1117,7 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
// would otherwise default to 0.
|
||||
statsCTE := `
|
||||
, stats AS (
|
||||
SELECT MAX(CASE WHEN title_rank > 0 THEN 1 ELSE 0 END) AS has_title_match
|
||||
SELECT MAX(CASE WHEN title_rank > 0 OR title_prefix_rank > 0 THEN 1 ELSE 0 END) AS has_title_match
|
||||
FROM scored
|
||||
)`
|
||||
// postFilter is the FROM + CROSS JOIN + WHERE shared by both dataSQL and
|
||||
@@ -1112,11 +1126,12 @@ func (r *ItemRepository) buildSearchSQL(query string, itemTypes []string, limit,
|
||||
postFilter := fmt.Sprintf(`FROM scored
|
||||
CROSS JOIN stats
|
||||
WHERE scored.title_rank > 0
|
||||
OR scored.title_prefix_rank > 0
|
||||
OR (COALESCE(stats.has_title_match, 0) = 0 AND scored.overview_rank >= %g)`, overviewMatchFloor)
|
||||
dataSQL = scoredCTE + statsCTE + fmt.Sprintf(`
|
||||
SELECT %s, COUNT(*) OVER () AS total_count
|
||||
%s
|
||||
ORDER BY exact_title_match DESC, contiguous_title_match DESC, year_match DESC, phrase_rank DESC, title_rank DESC, overview_rank DESC, LOWER(title) ASC, content_id ASC
|
||||
ORDER BY exact_title_match DESC, contiguous_title_match DESC, year_match DESC, phrase_rank DESC, title_rank DESC, title_prefix_rank DESC, overview_rank DESC, LOWER(title) ASC, content_id ASC
|
||||
LIMIT $%d OFFSET $%d`, itemColumns, postFilter, argIdx, argIdx+1)
|
||||
countSQL = scoredCTE + statsCTE + fmt.Sprintf(`
|
||||
SELECT COUNT(*)
|
||||
@@ -1423,6 +1438,12 @@ func (r *ItemRepository) UpdateMetadata(ctx context.Context, contentID string, u
|
||||
addString("tvdb_id", upd.TvdbID)
|
||||
addIntArray("locked_fields", upd.LockedFields)
|
||||
addString("poster_path", upd.PosterPath)
|
||||
if upd.PosterPath != nil {
|
||||
// An explicit poster override invalidates the provider-origin source
|
||||
// path captured by image caching; outbound embeds must not keep
|
||||
// rendering the replaced provider artwork.
|
||||
setClauses = append(setClauses, "poster_source_path = NULL")
|
||||
}
|
||||
addString("poster_thumbhash", upd.PosterThumbhash)
|
||||
addString("backdrop_path", upd.BackdropPath)
|
||||
addString("backdrop_thumbhash", upd.BackdropThumbhash)
|
||||
|
||||
@@ -369,6 +369,25 @@ func TestItemRepo_Search_NormalizesTsqueryInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestItemRepo_Search_UsesTitlePrefixQueryForPartialLastToken pins typeahead
|
||||
// title search for inputs like "Pride and P": the full-text query for "p" is
|
||||
// otherwise an exact lexeme match and never reaches the title-normalized
|
||||
// contiguous ranking signal.
|
||||
func TestItemRepo_Search_UsesTitlePrefixQueryForPartialLastToken(t *testing.T) {
|
||||
repo := &ItemRepository{}
|
||||
sql, _, args := repo.buildSearchSQL("Pride and P", []string{"movie"}, 20, 0, AccessFilter{})
|
||||
|
||||
if !strings.Contains(sql, "to_tsquery('simple', $2)") {
|
||||
t.Fatalf("expected title prefix query to use a bound to_tsquery arg; got:\n%s", sql)
|
||||
}
|
||||
if !strings.Contains(sql, "title_prefix_rank") {
|
||||
t.Fatalf("expected title prefix matches to participate in ranking/filtering; got:\n%s", sql)
|
||||
}
|
||||
if len(args) < 2 || args[1] != "pride & p:*" {
|
||||
t.Fatalf("expected args[1] to be the normalized title prefix tsquery, got %#v", args)
|
||||
}
|
||||
}
|
||||
|
||||
// TestItemRepo_Search_ScoredCTEExposesItemColumnNames guards the contract
|
||||
// between the scored CTE and the outer SELECT in buildSearchSQL. The CTE
|
||||
// projects qualifiedItemColumns("mi") and the outer query re-selects those
|
||||
|
||||
@@ -98,6 +98,31 @@ func parseYearToken(token string) (int, bool) {
|
||||
return year, true
|
||||
}
|
||||
|
||||
func buildTitlePrefixTsQuery(input string) string {
|
||||
normalized := normalizeTitleForComparison(input)
|
||||
if normalized == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
fields := strings.Fields(normalized)
|
||||
if len(fields) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
parts := make([]string, 0, len(fields))
|
||||
for i, field := range fields {
|
||||
if field == "" {
|
||||
continue
|
||||
}
|
||||
if i == len(fields)-1 {
|
||||
parts = append(parts, field+":*")
|
||||
continue
|
||||
}
|
||||
parts = append(parts, field)
|
||||
}
|
||||
return strings.Join(parts, " & ")
|
||||
}
|
||||
|
||||
// normalizeTitleForComparison must stay in lockstep with the SQL function
|
||||
// public.normalize_search_text (migrations 127 / 138) and the title_normalized
|
||||
// generated column. Mismatches between Go and SQL normalization produce
|
||||
|
||||
@@ -68,3 +68,24 @@ func TestParseSearchQuery_ExactTitleHint_NormalizesSearchText(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildTitlePrefixTsQuery(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{"Pride and P", "pride & p:*"},
|
||||
{"Law & Ord", "law & ord:*"},
|
||||
{"Dune: Part Two", "dune & part & 2:*"},
|
||||
{"and", ""},
|
||||
{" ", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.in, func(t *testing.T) {
|
||||
got := buildTitlePrefixTsQuery(tc.in)
|
||||
if got != tc.want {
|
||||
t.Fatalf("buildTitlePrefixTsQuery(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,23 @@ func (r *ServerSettingsRepo) Set(ctx context.Context, key, value string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetIfAbsent inserts a setting only when the key has no value yet (absent or
|
||||
// empty), reporting whether this call won the write. Generated credentials
|
||||
// (e.g. the web push VAPID keypair) must be provisioned single-writer across
|
||||
// concurrent nodes: exactly one generated value may ever land.
|
||||
func (r *ServerSettingsRepo) SetIfAbsent(ctx context.Context, key, value string) (bool, error) {
|
||||
tag, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO server_settings (key, value) VALUES ($1, $2)
|
||||
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
|
||||
WHERE server_settings.value = ''`,
|
||||
key, value,
|
||||
)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("server_settings set-if-absent %q: %w", key, err)
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// GetAll retrieves all settings as a map.
|
||||
func (r *ServerSettingsRepo) GetAll(ctx context.Context) (map[string]string, error) {
|
||||
rows, err := r.pool.Query(ctx, `SELECT key, value FROM server_settings`)
|
||||
|
||||
@@ -204,26 +204,32 @@ type AudiobookshelfCompatConfig struct {
|
||||
|
||||
// JellyfinCompatConfig holds compatibility proxy settings.
|
||||
type JellyfinCompatConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Listen string `yaml:"listen"`
|
||||
PublicURL string `yaml:"public_url"`
|
||||
EmulatedServerVersion string `yaml:"emulated_server_version"`
|
||||
ServerID string `yaml:"server_id"`
|
||||
ServerName string `yaml:"server_name"`
|
||||
WebEnabled bool `yaml:"web_enabled"`
|
||||
WebVersion string `yaml:"web_version"`
|
||||
WebDir string `yaml:"web_dir"`
|
||||
WebInstallDir string `yaml:"web_install_dir"`
|
||||
SessionTTL time.Duration `yaml:"-"`
|
||||
PlaybackSessionTTL time.Duration `yaml:"-"`
|
||||
}
|
||||
|
||||
// jellyfinCompatConfigRaw is the raw YAML representation with duration strings.
|
||||
type jellyfinCompatConfigRaw struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Listen string `yaml:"listen"`
|
||||
PublicURL string `yaml:"public_url"`
|
||||
EmulatedServerVersion string `yaml:"emulated_server_version"`
|
||||
ServerID string `yaml:"server_id"`
|
||||
ServerName string `yaml:"server_name"`
|
||||
WebEnabled bool `yaml:"web_enabled"`
|
||||
WebVersion string `yaml:"web_version"`
|
||||
WebDir string `yaml:"web_dir"`
|
||||
WebInstallDir string `yaml:"web_install_dir"`
|
||||
SessionTTL string `yaml:"session_ttl"`
|
||||
PlaybackSessionTTL string `yaml:"playback_session_ttl"`
|
||||
}
|
||||
@@ -356,8 +362,10 @@ var defaultJellyfinCompatServerID = uuid.NewSHA1(
|
||||
[]byte("https://silo.local/jellycompat"),
|
||||
).String()
|
||||
|
||||
const DefaultJellyfinCompatEmulatedServerVersion = "10.12.0"
|
||||
const DefaultJellyfinWebVersion = "10.11.6"
|
||||
const DefaultBundledJellyfinWebDir = "/srv/jellyfin-web/current"
|
||||
const DefaultJellyfinWebInstallDir = "/var/lib/silo/compat/jellyfin-web"
|
||||
const DefaultJellyfinWebDir = DefaultJellyfinWebInstallDir + "/current"
|
||||
|
||||
// parseDuration parses a duration string that supports Go's time.ParseDuration
|
||||
// formats plus a custom "Nd" format for days.
|
||||
@@ -437,13 +445,16 @@ func setDefaults() *configRaw {
|
||||
RefreshTokenExpiry: "30d",
|
||||
},
|
||||
JellyfinCompat: jellyfinCompatConfigRaw{
|
||||
Enabled: false,
|
||||
Listen: ":8097",
|
||||
PublicURL: "http://127.0.0.1:8097",
|
||||
EmulatedServerVersion: "10.12.0",
|
||||
EmulatedServerVersion: DefaultJellyfinCompatEmulatedServerVersion,
|
||||
ServerID: defaultJellyfinCompatServerID,
|
||||
ServerName: "Silo",
|
||||
WebEnabled: true,
|
||||
WebVersion: DefaultJellyfinWebVersion,
|
||||
WebDir: DefaultBundledJellyfinWebDir,
|
||||
WebDir: DefaultJellyfinWebDir,
|
||||
WebInstallDir: DefaultJellyfinWebInstallDir,
|
||||
SessionTTL: "87600h",
|
||||
PlaybackSessionTTL: "6h",
|
||||
},
|
||||
|
||||
@@ -56,6 +56,17 @@ func boolOr(m map[string]string, key string, fallback bool) (bool, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func jellyfinCompatEnabledOrLegacyDefault(m map[string]string) (bool, error) {
|
||||
if v, ok := m["jellyfin_compat.enabled"]; ok && v != "" {
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid bool for %q: %w", "jellyfin_compat.enabled", err)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
return stringOr(m, "jellyfin_compat.listen", ":8096") != "", nil
|
||||
}
|
||||
|
||||
func firstConfiguredBool(m map[string]string, fallback bool, keys ...string) (bool, error) {
|
||||
for _, key := range keys {
|
||||
v, ok := m[key]
|
||||
@@ -349,13 +360,24 @@ func LoadFromDB(m map[string]string) (*Config, error) {
|
||||
}
|
||||
|
||||
// JellyfinCompat
|
||||
compatEnabled, err := jellyfinCompatEnabledOrLegacyDefault(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.JellyfinCompat.Enabled = compatEnabled
|
||||
cfg.JellyfinCompat.Listen = stringOr(m, "jellyfin_compat.listen", ":8096")
|
||||
cfg.JellyfinCompat.PublicURL = stringOr(m, "jellyfin_compat.public_url", "http://127.0.0.1:8096")
|
||||
cfg.JellyfinCompat.EmulatedServerVersion = stringOr(m, "jellyfin_compat.emulated_server_version", "10.12.0")
|
||||
cfg.JellyfinCompat.EmulatedServerVersion = stringOr(m, "jellyfin_compat.emulated_server_version", DefaultJellyfinCompatEmulatedServerVersion)
|
||||
cfg.JellyfinCompat.ServerID = stringOr(m, "jellyfin_compat.server_id", defaultJellyfinCompatServerIDFromDB)
|
||||
cfg.JellyfinCompat.ServerName = stringOr(m, "jellyfin_compat.server_name", "Silo")
|
||||
webEnabled, err := boolOr(m, "jellyfin_compat.web_enabled", true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.JellyfinCompat.WebEnabled = webEnabled
|
||||
cfg.JellyfinCompat.WebVersion = stringOr(m, "jellyfin_compat.web_version", DefaultJellyfinWebVersion)
|
||||
cfg.JellyfinCompat.WebDir = stringOr(m, "jellyfin_compat.web_dir", DefaultBundledJellyfinWebDir)
|
||||
cfg.JellyfinCompat.WebDir = stringOr(m, "jellyfin_compat.web_dir", DefaultJellyfinWebDir)
|
||||
cfg.JellyfinCompat.WebInstallDir = stringOr(m, "jellyfin_compat.web_install_dir", DefaultJellyfinWebInstallDir)
|
||||
sessionTTL, err := durationOr(m, "jellyfin_compat.session_ttl", 87600*time.Hour)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -34,6 +35,24 @@ func TestLoadFromDBMetadataPresignExpiryRejectsInvalidDuration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromDBJellyfinWebEnabledDefaultsToTrue(t *testing.T) {
|
||||
cfg, err := LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB() returned error: %v", err)
|
||||
}
|
||||
if !cfg.JellyfinCompat.WebEnabled {
|
||||
t.Fatal("JellyfinCompat.WebEnabled = false, want default true")
|
||||
}
|
||||
|
||||
cfg, err = LoadFromDB(map[string]string{"jellyfin_compat.web_enabled": "false"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB() returned error: %v", err)
|
||||
}
|
||||
if cfg.JellyfinCompat.WebEnabled {
|
||||
t.Fatal("JellyfinCompat.WebEnabled = true, want configured false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromDBAudiobookshelfCompatFlagGatesCompatListener(t *testing.T) {
|
||||
cfg, err := LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
@@ -59,3 +78,97 @@ func TestLoadFromDBAudiobookshelfCompatFlagGatesCompatListener(t *testing.T) {
|
||||
t.Fatalf("enabled audiobooks listener = %q, want default :13378", cfg.AudiobookshelfCompat.Listen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromDBJellyfinCompatEnabledPreservesLegacyListenerDefault(t *testing.T) {
|
||||
cfg, err := LoadFromDB(map[string]string{})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB() returned error: %v", err)
|
||||
}
|
||||
if !cfg.JellyfinCompat.Enabled {
|
||||
t.Fatal("JellyfinCompat.Enabled = false, want legacy default true when listener would bind")
|
||||
}
|
||||
if cfg.JellyfinCompat.Listen == "" {
|
||||
t.Fatal("JellyfinCompat.Listen is empty, want default listener")
|
||||
}
|
||||
|
||||
cfg, err = LoadFromDB(map[string]string{"jellyfin_compat.listen": ":19096"})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB() returned error: %v", err)
|
||||
}
|
||||
if !cfg.JellyfinCompat.Enabled {
|
||||
t.Fatal("JellyfinCompat.Enabled = false, want true when legacy listener is configured")
|
||||
}
|
||||
if cfg.JellyfinCompat.Listen != ":19096" {
|
||||
t.Fatalf("JellyfinCompat.Listen = %q, want configured listener", cfg.JellyfinCompat.Listen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromDBJellyfinCompatEnabledRespectsExplicitDisable(t *testing.T) {
|
||||
cfg, err := LoadFromDB(map[string]string{
|
||||
"jellyfin_compat.enabled": "false",
|
||||
"jellyfin_compat.listen": ":19096",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("LoadFromDB() returned error: %v", err)
|
||||
}
|
||||
if cfg.JellyfinCompat.Enabled {
|
||||
t.Fatal("JellyfinCompat.Enabled = true, want configured false")
|
||||
}
|
||||
|
||||
_, err = LoadFromDB(map[string]string{"jellyfin_compat.enabled": "maybe"})
|
||||
if err == nil {
|
||||
t.Fatal("LoadFromDB() error = nil, want invalid bool error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "jellyfin_compat.enabled") {
|
||||
t.Fatalf("LoadFromDB() error = %v, want key name", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestYAMLToSettingsMapJellyfinCompatEnabledPreservesLegacyListener(t *testing.T) {
|
||||
m := yamlSettingsMapFromString(t, `
|
||||
jellyfin_compat:
|
||||
listen: ":19096"
|
||||
`)
|
||||
if got := m["jellyfin_compat.enabled"]; got != "true" {
|
||||
t.Fatalf("jellyfin_compat.enabled = %q, want true", got)
|
||||
}
|
||||
if got := m["jellyfin_compat.listen"]; got != ":19096" {
|
||||
t.Fatalf("jellyfin_compat.listen = %q, want configured listener", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestYAMLToSettingsMapJellyfinCompatEnabledRespectsExplicitDisable(t *testing.T) {
|
||||
m := yamlSettingsMapFromString(t, `
|
||||
jellyfin_compat:
|
||||
enabled: false
|
||||
listen: ":19096"
|
||||
`)
|
||||
if got := m["jellyfin_compat.enabled"]; got != "false" {
|
||||
t.Fatalf("jellyfin_compat.enabled = %q, want false", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestYAMLToSettingsMapJellyfinCompatEnabledDefaultsToLegacyListener(t *testing.T) {
|
||||
m := yamlSettingsMapFromString(t, `server:
|
||||
mode: integrated
|
||||
`)
|
||||
if got := m["jellyfin_compat.enabled"]; got != "true" {
|
||||
t.Fatalf("jellyfin_compat.enabled = %q, want true for default listener", got)
|
||||
}
|
||||
if got := m["jellyfin_compat.listen"]; got == "" {
|
||||
t.Fatal("jellyfin_compat.listen is empty, want default listener")
|
||||
}
|
||||
}
|
||||
|
||||
func yamlSettingsMapFromString(t *testing.T, body string) map[string]string {
|
||||
t.Helper()
|
||||
path := t.TempDir() + "/silo.yaml"
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile() returned error: %v", err)
|
||||
}
|
||||
m, err := YAMLToSettingsMap(path)
|
||||
if err != nil {
|
||||
t.Fatalf("YAMLToSettingsMap() returned error: %v", err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -56,12 +56,12 @@ var restartRequiredKeys = map[string]bool{
|
||||
// Compat listeners and session stores.
|
||||
"audiobookshelf_compat.enabled": true,
|
||||
// public_url / server_name / emulated_server_version are read live per
|
||||
// request; server_id is generate-once and baked into the resource
|
||||
// mapper; the session TTLs are baked into store constructors.
|
||||
// request; Jellyfin Web settings are read from the settings repo by
|
||||
// the dynamic web handler; server_id is generate-once and baked into
|
||||
// the resource mapper; the session TTLs are baked into store constructors.
|
||||
"jellyfin_compat.enabled": true,
|
||||
"jellyfin_compat.listen": true,
|
||||
"jellyfin_compat.server_id": true,
|
||||
"jellyfin_compat.web_version": true,
|
||||
"jellyfin_compat.web_dir": true,
|
||||
"jellyfin_compat.session_ttl": true,
|
||||
"jellyfin_compat.playback_session_ttl": true,
|
||||
|
||||
|
||||
@@ -19,6 +19,20 @@ func TestRestartRequired(t *testing.T) {
|
||||
{"recommendations.enabled", true},
|
||||
// Legacy AI aliases classify like their canonical keys.
|
||||
{"subtitle_ai.max_concurrent_jobs", true},
|
||||
// Compat listeners and session stores are only bound at process startup.
|
||||
{"jellyfin_compat.enabled", true},
|
||||
{"jellyfin_compat.listen", true},
|
||||
{"jellyfin_compat.server_id", true},
|
||||
{"jellyfin_compat.session_ttl", true},
|
||||
{"jellyfin_compat.playback_session_ttl", true},
|
||||
// Jellyfin identity and Web UI settings are live-read.
|
||||
{"jellyfin_compat.public_url", false},
|
||||
{"jellyfin_compat.server_name", false},
|
||||
{"jellyfin_compat.emulated_server_version", false},
|
||||
{"jellyfin_compat.web_enabled", false},
|
||||
{"jellyfin_compat.web_version", false},
|
||||
{"jellyfin_compat.web_dir", false},
|
||||
{"jellyfin_compat.web_install_dir", false},
|
||||
{"subtitle_ai.base_url", false}, // like ai.base_url — hot-reloaded
|
||||
{"ai.api_key", false},
|
||||
{"subtitle_ai.enabled", false},
|
||||
|
||||
@@ -24,6 +24,39 @@ func firstNonEmpty(values ...string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func yamlHasPath(data []byte, path ...string) (bool, error) {
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal(data, &doc); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
node := &doc
|
||||
if node.Kind == yaml.DocumentNode {
|
||||
if len(node.Content) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
node = node.Content[0]
|
||||
}
|
||||
|
||||
for _, key := range path {
|
||||
if node.Kind != yaml.MappingNode {
|
||||
return false, nil
|
||||
}
|
||||
found := false
|
||||
for i := 0; i+1 < len(node.Content); i += 2 {
|
||||
if node.Content[i].Value == key {
|
||||
node = node.Content[i+1]
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// YAMLToSettingsMap reads a YAML config file and converts it to a flat
|
||||
// map[string]string suitable for insertion into the server_settings table.
|
||||
//
|
||||
@@ -40,6 +73,10 @@ func YAMLToSettingsMap(path string) (map[string]string, error) {
|
||||
if err := yaml.Unmarshal(data, raw); err != nil {
|
||||
return nil, fmt.Errorf("parsing YAML config: %w", err)
|
||||
}
|
||||
jellyfinCompatEnabledConfigured, err := yamlHasPath(data, "jellyfin_compat", "enabled")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing YAML config: %w", err)
|
||||
}
|
||||
|
||||
m := make(map[string]string)
|
||||
|
||||
@@ -199,13 +236,20 @@ func YAMLToSettingsMap(path string) (map[string]string, error) {
|
||||
setIfNonEmpty(m, "auth.refresh_token_expiry", raw.Auth.RefreshTokenExpiry)
|
||||
|
||||
// JellyfinCompat
|
||||
if jellyfinCompatEnabledConfigured {
|
||||
m["jellyfin_compat.enabled"] = strconv.FormatBool(raw.JellyfinCompat.Enabled)
|
||||
} else if raw.JellyfinCompat.Listen != "" {
|
||||
m["jellyfin_compat.enabled"] = "true"
|
||||
}
|
||||
setIfNonEmpty(m, "jellyfin_compat.listen", raw.JellyfinCompat.Listen)
|
||||
setIfNonEmpty(m, "jellyfin_compat.public_url", raw.JellyfinCompat.PublicURL)
|
||||
setIfNonEmpty(m, "jellyfin_compat.emulated_server_version", raw.JellyfinCompat.EmulatedServerVersion)
|
||||
setIfNonEmpty(m, "jellyfin_compat.server_id", raw.JellyfinCompat.ServerID)
|
||||
setIfNonEmpty(m, "jellyfin_compat.server_name", raw.JellyfinCompat.ServerName)
|
||||
setIfNonEmpty(m, "jellyfin_compat.web_enabled", strconv.FormatBool(raw.JellyfinCompat.WebEnabled))
|
||||
setIfNonEmpty(m, "jellyfin_compat.web_version", raw.JellyfinCompat.WebVersion)
|
||||
setIfNonEmpty(m, "jellyfin_compat.web_dir", raw.JellyfinCompat.WebDir)
|
||||
setIfNonEmpty(m, "jellyfin_compat.web_install_dir", raw.JellyfinCompat.WebInstallDir)
|
||||
setIfNonEmpty(m, "jellyfin_compat.session_ttl", raw.JellyfinCompat.SessionTTL)
|
||||
setIfNonEmpty(m, "jellyfin_compat.playback_session_ttl", raw.JellyfinCompat.PlaybackSessionTTL)
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user