Files
silo-server/internal/onboarding/onboarding.go
271a2e1741 feat: emailed invitations, claim + household setup, and server-driven onboarding tour (#501)
* feat(invitations): add emailed pre-provisioned invitations

Admins can invite a specific person by email: the invitation pre-binds
role, access group, and library access, and the invitee only chooses a
password. Their email address becomes their username, so login gains an
email fallback (username lookup first, email column only on miss for
inputs that parse as a bare address).

- invitations table: single-use token (SHA-256 at rest) bound to one
  address; a partial unique index makes resend-supersedes atomic; no
  users row exists until accept, so a typo'd address can't squat a
  username. Status is derived from timestamps, not stored.
- internal/invitations: repository, service, and branded email through
  the shared internal/mail sender. When SMTP is off the claim URL is
  returned for manual delivery instead of failing.
- Admin endpoints /admin/invitations (list/create/resend/revoke) beside
  the existing invite-codes routes; public claim endpoints
  /invitations/{token} (+/accept) rate-limited with the other auth
  endpoints. Unknown/expired/revoked/used tokens are indistinguishable.
- Accept returns the same login response shape as signup, so clients
  reuse their session plumbing.

Spec: docs/superpowers/specs/2026-07-27-invitations-and-onboarding-design.md
Plan: docs/superpowers/plans/2026-07-27-invitations-and-onboarding.md
Part of #215

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(web): add invitation admin tab, claim page, and household setup

- Admin → Users gains an Invitations tab: compose (email, role, access
  group, libraries, note, first-profile and tour toggles), list with
  derived status, resend, revoke. When the server has no SMTP the create
  response's claim URL is surfaced for copy-paste instead of a fake
  success.
- /invite/:token claim page: everything but the password was decided at
  send time, so it asks for exactly one thing and lands the user signed
  in. Expired/used links get an explanatory card, not a 404.
- /household-setup ("Who's watching?"): profile tiles plus the existing
  ProfileEditorDialog, all through the existing /profiles endpoint —
  no new backend. "Just me for now" is a first-class exit.

Part of #215

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(onboarding): add server-driven onboarding manifest and state

GET /onboarding/flow returns the ordered first-run tour for this server
and profile: steps for disabled features (requests, watch together,
recommendations, notifications) are filtered out server-side, surface=tv
drops steps needing text entry, and child profiles never see stops they
can't act on. Copy lives in Go, so a wording fix is a deploy — clients
render step kinds they know and skip unknown ones by contract.

setting_choice steps name an explicit write target (profile_field /
setting / device_setting) because playback quality is a profile column,
not a settings key — the tour writes through the same APIs the settings
screens use.

Per-profile completion state lives in the user store (SQLite schema v14
+ a Postgres twin table), keyed by (profile_id, tour_id) with monotonic
completed/skipped timestamps: finishing on one device silences every
other; a later progress write can never un-complete.

Part of #215

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(web): add the first-run feature tour

TourHost renders the server manifest as a modal overlay on Home: unknown
step kinds are skipped silently (the forward-compat contract), progress
posts per step, and setting_choice steps write real values through the
existing profile/settings mutations — by the last step the account is
genuinely configured. Skip is always one click and recorded server-side,
so no other device re-prompts. The tour ends by handing off to the
existing taste-seed picker, which now waits for the tour to finish
before its own redirect. Settings → Personalize gains a replay entry.

An invitation sent with show_tour=false plants a local hint that the
gate converts into a server-side skip for the first profile.

Part of #215

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(web): satisfy noUncheckedIndexedAccess in the tour's advance step

The Docker web build runs `tsc -b`, which applies the project's
noUncheckedIndexedAccess; the bounds check didn't narrow steps[next].
Look the step up once and branch on its presence instead.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(web): blur the whole app behind the tour, sidebar included

The tour overlay rendered inside the app layout, where an ancestor
creates a fixed-position containing block — inset-0 pinned to the
content pane, leaving the sidebar completely un-scrimmed. Portal the
dialog to <body> so the scrim truly covers the viewport, and raise the
backdrop blur from sm (4px) to xl (24px) so card titles and nav labels
aren't legible through it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(onboarding): name features by their UI labels in the tour copy

"Same movie, different couches" never said what the feature is called.
Every feature card now leads with the name the sidebar actually uses —
Watch Party, Requests, Watchlist, Calendar, Notifications — and says
where to find it, so the tour teaches vocabulary, not just concepts.
Server-side copy, so all three clients pick this up with no release.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(onboarding): add apps and Jellyfin-compat steps to the tour

Two new web-only feature cards near the end of the tour:

- "Take Silo with you" — native apps for iPhone/iPad/Apple TV and
  Android/Android TV, with outbound TestFlight and Play Store links.
  Steps gain an additive links field (label + url) that older clients
  ignore; the web TourHost renders them as external-link buttons.
- "Already use a Jellyfin app? It works here" — Infuse/VidHub/Findroid/
  Swiftfin connect via the Jellyfin API. Gated on
  jellyfin_compat.enabled (default-on, so unset counts as enabled;
  only an explicit "false" hides it).

Both steps are web-only: the apps card is pointless inside the apps it
advertises, and TV can't open store links. surface=phone/tv manifests
skip them, covered by tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(web): keep the tour card responsive on phone widths

Verified every step at 1600px, 390px, and 320px with an automated
overflow check. Fixes it found:

- Link buttons (apps step) now wrap and truncate instead of extending
  past the card edge.
- The footer wraps at very narrow widths, so the handoff step's wide
  primary button drops to its own line rather than overflowing.
- Progress pips hide on phones — decorative, and they crowded the
  Back/Next buttons.
- The card scrolls within 85dvh so a tall step never pins its buttons
  off-screen on landscape phones.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(web): render store links as branded badges in the tour

The apps step's plain outline buttons now render as store badges: the
Apple or Google Play mark with a store eyebrow (TestFlight beta /
Google Play) over the platform label — the familiar app-store badge
idiom. The brand is inferred from the link's host on the client, so
the server contract stays icon-free and non-store links keep the plain
external-link button. Labels drop the parenthesized store name the
eyebrow now carries.

Verified at 1600px and 390px with the overflow sweep: none.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-27 22:58:18 -04:00

163 lines
5.4 KiB
Go

// Package onboarding owns the server-driven first-run tour: an ordered step
// manifest filtered per server (features that are off never produce a step)
// and per surface (a TV can't type), plus per-profile completion state in
// the user store. Clients render step kinds they know and skip the rest,
// which is what lets the server add a stop without three app-store releases.
//
// Spec: docs/superpowers/specs/2026-07-27-invitations-and-onboarding-design.md
package onboarding
import "context"
// Version is the manifest contract version. Bump only for breaking changes
// to the step envelope itself; new step kinds are NOT breaking (clients skip
// unknown kinds by contract).
const Version = 1
// TourID identifies the current tour's content generation. A profile that
// completed this tour is never re-prompted for it; shipping a materially
// different tour later means minting a new ID.
const TourID = "core-2026-07"
// Surfaces a client can request the flow for.
const (
SurfaceWeb = "web"
SurfacePhone = "phone"
SurfaceTV = "tv"
)
// Step kinds. Clients switch on these and silently skip unknown values.
const (
KindWelcome = "welcome"
KindFeatureCard = "feature_card"
KindSettingChoice = "setting_choice"
KindHandoff = "handoff"
)
// Gate names steps may reference (see Gates).
const (
gateRequests = "requests"
gateWatchTogether = "watch_together"
gateRecommendations = "recommendations"
gateNotifications = "notifications"
gateCalendar = "calendar"
gateJellyfinCompat = "jellyfin_compat"
)
// Setting targets: which existing API a setting_choice writes through.
const (
TargetProfileField = "profile_field"
TargetSetting = "setting"
TargetDeviceSetting = "device_setting"
)
// Step is one stop in the tour.
type Step struct {
ID string `json:"id"`
Kind string `json:"kind"`
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
// Illustration is a client-side asset key; the server never sends URLs.
Illustration string `json:"illustration,omitempty"`
// Setting is present on setting_choice steps.
Setting *SettingSpec `json:"setting,omitempty"`
// Route is a client route for feature_card actions and handoff steps.
Route string `json:"route,omitempty"`
// ActionLabel labels the optional feature_card route action.
ActionLabel string `json:"action_label,omitempty"`
// Links are external URLs (store listings, docs) rendered as outbound
// buttons. Additive: clients that predate the field ignore it.
Links []StepLink `json:"links,omitempty"`
// needsInput marks steps unsuitable for 10-foot surfaces.
needsInput bool
// webOnly marks steps that only make sense in a browser (e.g. "install
// the apps" — pointless inside the app it advertises).
webOnly bool
// gate names the feature flag that must be on; empty = always shown.
gate string
}
// StepLink is one outbound link on a step.
type StepLink struct {
Label string `json:"label"`
URL string `json:"url"`
}
// SettingSpec describes the control a setting_choice renders and where the
// chosen value is written. Target selects the API: profile_field goes
// through PUT /profiles/{id}, setting through PUT /settings/{key},
// device_setting through PUT /settings/device/{key}.
type SettingSpec struct {
Target string `json:"target"`
Key string `json:"key"`
Control string `json:"control"` // "segmented" | "toggle" | "select"
Options []SettingOption `json:"options,omitempty"`
Default string `json:"default,omitempty"`
// Label annotates toggle controls.
Label string `json:"label,omitempty"`
}
// SettingOption is one choice of a segmented/select control.
type SettingOption struct {
Value string `json:"value"`
Label string `json:"label"`
}
// Flow is the response of GET /onboarding/flow.
type Flow struct {
Version int `json:"version"`
TourID string `json:"tour_id"`
Steps []Step `json:"steps"`
}
// Gates reports which optional server features are on. Each check is
// consulted at request time so admin toggles apply without a restart; a nil
// check means "off".
type Gates struct {
Requests func(ctx context.Context) bool
WatchTogether func(ctx context.Context) bool
Recommendations func(ctx context.Context) bool
Notifications func(ctx context.Context) bool
Calendar func(ctx context.Context) bool
JellyfinCompat func(ctx context.Context) bool
}
func (g Gates) enabled(ctx context.Context, gate string) bool {
check := map[string]func(context.Context) bool{
gateRequests: g.Requests,
gateWatchTogether: g.WatchTogether,
gateRecommendations: g.Recommendations,
gateNotifications: g.Notifications,
gateCalendar: g.Calendar,
gateJellyfinCompat: g.JellyfinCompat,
}[gate]
if check == nil {
return false
}
return check(ctx)
}
// FlowFor returns the ordered steps for one surface with gated and
// unsuitable steps removed. isChild drops steps a child profile may not act
// on (requests) or that write fields a child session can't.
func FlowFor(ctx context.Context, gates Gates, surface string, isChild bool) Flow {
steps := make([]Step, 0, len(tourSteps))
for _, step := range tourSteps {
if step.gate != "" && !gates.enabled(ctx, step.gate) {
continue
}
if surface == SurfaceTV && step.needsInput {
continue
}
if surface != SurfaceWeb && step.webOnly {
continue
}
if isChild && step.gate == gateRequests {
continue
}
steps = append(steps, step)
}
return Flow{Version: Version, TourID: TourID, Steps: steps}
}