* perf(catalog): fix audiobook detail N+1 + slow people facets Audiobook detail pages were slow in proportion to track count (up to 433 files/book). Root causes, found by EXPLAIN ANALYZE on the live DB: - effectiveAudioSelection ran 3-4 user-store queries (profile, audio pref, library pref) per file inside buildPlaybackInfo's loop, though the results are invariant across a request. Introduce a request-scoped audioPrefResolver that memoizes the store lookups (library prefs keyed by folder); a 400-file audiobook now issues each query once instead of per file. Selection logic is unchanged (audioPreference returns a copy so the original-language sentinel is still resolved per file). - buildAudiobookExtension ran its four independent related-content queries serially; run them concurrently so latency is the slowest, not the sum. - author/narrator browse facets did a full people-table scan; add a (kind, content_id, person_id) index so the facet resolves from an index-only scan of just that kind's credits (~112ms -> ~49ms on the live library). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * perf(catalog): cache audiobook author/narrator group browse The Authors/Narrators audiobook pages were slow on cold load and slow again after a hard refresh (fast only while the React Query client cache was warm). Root cause (EXPLAIN ANALYZE on live, 31K-audiobook library): the grouped browse query is ~234ms/page, there are ~13K distinct authors, and the client pages through the entire list on every load (sequential 500-row requests). With no server-side cache each of the ~20 pages re-ran the full aggregation (COUNT(*) OVER() forces it), so a cold load was ~20x234ms. The client's 60s staleTime was the only thing making a warm revisit fast; a refresh wiped it. Fix: AudiobookGroupsCache caches the full sorted group list per (library, group_by, sort, viewer) for 60s (matching the client staleTime, so no extra staleness) and serves every page as an in-memory slice — one aggregation per window instead of one per page, and a refresh is a cache hit. Also raise the client page size 500->2000 so fewer sequential round-trips are needed now that a larger page is a cheap slice. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * perf(settings): throttle per-request device last_seen upserts Device-setting reads (HandleGetDeviceSetting, HandleGetEffectiveSettings, HandleGetEffectiveSubtitleAppearance) each registered the request's device — an INSERT ... ON CONFLICT upsert of last_seen_at on a single per-device row. A page that fetches many settings fired hundreds of these concurrently; they serialized on that row's lock (observed 100-237ms each, ~250 per page load in the slow query log), taxing every settings fetch. Throttle device registration to one upsert per (profile, device) per 5 minutes via an in-process TTL cache, marking the device seen before the upsert so a concurrent burst collapses to a single write. last_seen_at stays fresh to within the window. Reads no longer issue a contended write on the hot path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * perf+fix(audiobooks): probe-repair, resume position, cache storm, groups reveal, hot-row + stats index From the full audiobook code review (EXPLAIN + slow-query trace on live): - #1 (P0, detail-page killer): NeedsCriticalProbeRepair required video codec/ resolution/tracks, which audio-only files never have, so PlaybackProbeEnsurer re-ran ffprobe per file on every detail/watch load (up to N serial spawns for an N-track book) and never converged. Gate video-field checks on the file actually having a video stream. TDD. - #3 (P0): abs session-sync rewound the resume cursor — UpdateProgressPosition did an unconditional SET with no monotonic guard, ignored its error, and no-op'd when no row existed (first-listen resume lost). Now a finish-preserving GREATEST upsert; caller logs failures. - #4 (P0 perf): progress reports fired every ~10s invalidated all of catalogKeys.all → refetched every active browse/detail query incl the 13k audiobook group lists. Scope invalidation to the reported item's detail. - #6 (P1 perf): Authors/Narrators page rendered all ~13k groups + cover images at once (main-thread freeze). Incremental reveal: render a capped window, grow on scroll via IntersectionObserver. - #10: throttle abs TouchToken last_seen upsert (one per token per 5min) — same hot-row contention class as the device fix. - #8: index abs_playback_sessions (user_id, profile_id, started_at) for the listening-stats aggregations. Deferred (need contract/validation): listening-time idempotency (client delta-vs- cumulative), scanner deleted-file reconcile, abs session retention job, abs list- handler batch fetch, scanner-output P2s (need re-backfill). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * perf(audiobooks): batch-fetch abs list/shelf handlers (kill N+1) handleSimilarItems, handleItemsInProgress, and handleGetMyProgress called MediaStore.GetAudiobookByID once per row — up to ~500 single fetches (each a few queries) on app open. Add GetAudiobooksByIDs (one access-scoped fetch + people/series hydrated once for the whole set) and look results up from the returned map, preserving order. Underlying primitives (GetByIDsWithAccess, hydratePeople/Series) were already batch-capable. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(audiobooks): reconcile deleted files on scan + prune session history #5: ScanAudiobookFolder only ever upserted — deleted/renamed books leaked media_items/media_files/memberships forever. Mirror the ebook reconcile: collect seenPaths during the walk, MarkMissing files no longer on disk, then reconcileLibraryMemberships. Safety mirrors ebooks/video: an inaccessible root (unmounted source) is skipped entirely, and a walk that saw zero files while the DB has rows only reconciles after operator cleanup confirmation (ebookEmptyCleanupAllowed) — so a flapping mount can't wipe the catalog. Soft mark only; the existing grace-period purge hard-deletes later. Reconcile runs only on a fully-completed (non-cancelled) scan. (#9 coarse case already handled: audiobookFolderShouldSkip skips unchanged folders; per-file reuse deferred.) #8-retention: abs_playback_sessions grew unbounded (one row per play-start, never deleted) and fed every listening-stats scan. Add an hourly sweep in SessionCleaner: close abandoned open sessions (no /close, stopped syncing >24h) and delete closed sessions older than 90 days. Mirrors the recommendation_cache / missing-files prune pattern. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(audiobooks): address max-effort code-review findings From /code-review max on the pre-PR diff: - DATA RACE (P0): SessionCleaner.lastABSSessionPrune is read+written by both the 15s ticker goroutine and the shutdown-path CleanStale call (main.go defers Stop() to after that call). Guard the prune-due gate with a mutex. (CleanStale was stateless before this branch, so concurrent calls were previously safe.) - ScanAudiobookFolder hardcoded fullScan=true into the empty-walk cleanup guard, but it's also called from ScanSubtree (incremental scans). An empty subtree scan would wrongly consume the operator's one-shot empty-cleanup allowance and warn. Thread a real fullScan flag (true from ScanFolder, false from the two subtree call sites), mirroring the ebook path. - Revert UpdateProgressPosition to UPDATE-only (drop the INSERT-on-missing): keep the monotonic GREATEST + finish guard that fixes the resume rewind, but restore the no-op-on-missing contract so a stray sync tick can't resurrect just-cleared progress or create a zero-duration continue-listening row. - Clamp the audiobook-groups handler limit (paging moved into the cache, leaving the old 500/page bound stranded); also gofmt the Scanner struct. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(audiobooks): address review feedback for scanner and stats * fix(audiobooks): address review feedback * fix(audiobooks): retry failed session prune --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
922 lines
27 KiB
Go
922 lines
27 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
|
|
"github.com/Silo-Server/silo-server/internal/cache"
|
|
"github.com/Silo-Server/silo-server/internal/userstore"
|
|
)
|
|
|
|
// deviceSeenThrottle bounds how often a device's last_seen_at is refreshed from
|
|
// request traffic. Device-setting reads each registered the device (an upsert
|
|
// on a single per-device row), so a page that fetches many settings serialized
|
|
// hundreds of upserts on that row's lock. Skipping the upsert when the device
|
|
// was seen within this window removes the contention while keeping last_seen
|
|
// fresh to within the window.
|
|
const deviceSeenThrottle = 5 * time.Minute
|
|
|
|
const subtitleAppearanceSettingKey = "subtitle_appearance"
|
|
const (
|
|
libraryPageStateSettingKey = "ui.library_page_state"
|
|
rememberLibraryPageStateSettingKey = "ui.remember_library_page_state"
|
|
searchMediaScopeSettingKey = "search.media_scope"
|
|
)
|
|
|
|
const (
|
|
deviceIDHeader = "X-Silo-Device-Id"
|
|
deviceNameHeader = "X-Silo-Device-Name"
|
|
devicePlatformHeader = "X-Silo-Device-Platform"
|
|
)
|
|
|
|
// ServerSettingReader reads individual keys from the server_settings table.
|
|
type ServerSettingReader interface {
|
|
Get(ctx context.Context, key string) (string, error)
|
|
}
|
|
|
|
// SettingsHandler handles user-scoped settings endpoints.
|
|
type SettingsHandler struct {
|
|
storeProvider userstore.UserStoreProvider
|
|
serverSettings ServerSettingReader
|
|
deviceSeen *cache.TTLCache[struct{}]
|
|
}
|
|
|
|
// NewSettingsHandler creates a new SettingsHandler.
|
|
func NewSettingsHandler(provider userstore.UserStoreProvider) *SettingsHandler {
|
|
return &SettingsHandler{
|
|
storeProvider: provider,
|
|
deviceSeen: cache.NewTTLCache[struct{}](),
|
|
}
|
|
}
|
|
|
|
// shouldRegisterDevice reports whether the device's last_seen_at should be
|
|
// refreshed now, throttling to one upsert per deviceSeenThrottle window per
|
|
// (profile, device). It marks the device seen before returning true so a burst
|
|
// of concurrent reads collapses to a single upsert instead of contending on the
|
|
// device row.
|
|
func (h *SettingsHandler) shouldRegisterDevice(profileID, deviceID string) bool {
|
|
if h == nil || h.deviceSeen == nil {
|
|
return true
|
|
}
|
|
key := profileID + "\x00" + deviceID
|
|
if _, seen := h.deviceSeen.Get(key); seen {
|
|
return false
|
|
}
|
|
h.deviceSeen.Set(key, struct{}{}, deviceSeenThrottle)
|
|
return true
|
|
}
|
|
|
|
// SetServerSettings configures the optional server settings reader for overlay config etc.
|
|
func (h *SettingsHandler) SetServerSettings(reader ServerSettingReader) {
|
|
h.serverSettings = reader
|
|
}
|
|
|
|
// --- Request/Response types ---
|
|
|
|
type setSettingRequest struct {
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type settingResponse struct {
|
|
Key string `json:"key"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type settingsListResponse struct {
|
|
Settings []settingResponse `json:"settings"`
|
|
}
|
|
|
|
type effectiveSettingResponse struct {
|
|
Key string `json:"key"`
|
|
ProfileID string `json:"profile_id,omitempty"`
|
|
UserValue string `json:"user_value,omitempty"`
|
|
DeviceValue string `json:"device_value,omitempty"`
|
|
EffectiveValue string `json:"effective_value"`
|
|
Source string `json:"source"`
|
|
HasDeviceOverride bool `json:"has_device_override"`
|
|
DeviceID string `json:"device_id,omitempty"`
|
|
DeviceName string `json:"device_name,omitempty"`
|
|
DevicePlatform string `json:"device_platform,omitempty"`
|
|
UpdatedAt string `json:"updated_at,omitempty"`
|
|
}
|
|
|
|
type effectiveSettingsResponse struct {
|
|
Settings []effectiveSettingResponse `json:"settings"`
|
|
}
|
|
|
|
type effectiveSubtitleAppearanceResponse struct {
|
|
Key string `json:"key"`
|
|
ProfileID string `json:"profile_id,omitempty"`
|
|
GlobalValue string `json:"global_value"`
|
|
DeviceValue string `json:"device_value,omitempty"`
|
|
EffectiveValue string `json:"effective_value"`
|
|
HasDeviceOverride bool `json:"has_device_override"`
|
|
DeviceID string `json:"device_id,omitempty"`
|
|
DeviceName string `json:"device_name,omitempty"`
|
|
DevicePlatform string `json:"device_platform,omitempty"`
|
|
UpdatedAt string `json:"updated_at,omitempty"`
|
|
}
|
|
|
|
type settingsScope string
|
|
|
|
const (
|
|
scopeUser settingsScope = "user"
|
|
scopeDevice settingsScope = "device"
|
|
)
|
|
|
|
type settingSpec struct {
|
|
Scope settingsScope
|
|
DefaultValue string
|
|
Validate func(string) error
|
|
}
|
|
|
|
var settingsRegistry = map[string]settingSpec{
|
|
"playback.preferred_quality": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "auto",
|
|
Validate: validateEnumSetting("playback.preferred_quality",
|
|
"auto", "original", "2160p", "1080p-high", "1080p-medium", "1080p", "1080p-8",
|
|
"720p-high", "720p-medium", "720p", "480p", "420p", "328p"),
|
|
},
|
|
"playback.audio_language": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "",
|
|
Validate: func(value string) error {
|
|
if len(strings.TrimSpace(value)) > 32 {
|
|
return fmt.Errorf("playback.audio_language must be 32 characters or fewer")
|
|
}
|
|
return nil
|
|
},
|
|
},
|
|
"playback.auto_skip_intro": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "false",
|
|
Validate: validateBoolSetting("playback.auto_skip_intro"),
|
|
},
|
|
"playback.auto_skip_credits": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "false",
|
|
Validate: validateBoolSetting("playback.auto_skip_credits"),
|
|
},
|
|
"playback.auto_skip_recap": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "false",
|
|
Validate: validateBoolSetting("playback.auto_skip_recap"),
|
|
},
|
|
"playback.auto_play_next_preview": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "false",
|
|
Validate: validateBoolSetting("playback.auto_play_next_preview"),
|
|
},
|
|
"playback.auto_play_next": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "true",
|
|
Validate: validateBoolSetting("playback.auto_play_next"),
|
|
},
|
|
"playback.next_up_prompt_seconds": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "30",
|
|
Validate: validateIntRange("playback.next_up_prompt_seconds", 0, 120),
|
|
},
|
|
subtitleAppearanceSettingKey: {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "",
|
|
Validate: validateJSONSetting(subtitleAppearanceSettingKey),
|
|
},
|
|
libraryPageStateSettingKey: {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "",
|
|
Validate: validateJSONSetting(libraryPageStateSettingKey),
|
|
},
|
|
rememberLibraryPageStateSettingKey: {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "true",
|
|
Validate: validateBoolSetting(rememberLibraryPageStateSettingKey),
|
|
},
|
|
// Preferred default scope for global/catalog search. "video" keeps
|
|
// results to movies and series; "all" mixes audiobooks in.
|
|
searchMediaScopeSettingKey: {
|
|
Scope: scopeUser,
|
|
DefaultValue: "video",
|
|
Validate: validateEnumSetting(searchMediaScopeSettingKey,
|
|
"all", "video", "audiobook"),
|
|
},
|
|
"player.hdr_enabled": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "true",
|
|
Validate: validateBoolSetting("player.hdr_enabled"),
|
|
},
|
|
"player.dv_profile7_hdr10_fallback": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "false",
|
|
Validate: validateBoolSetting("player.dv_profile7_hdr10_fallback"),
|
|
},
|
|
"player.playback_speed": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "1",
|
|
Validate: validateFloatRange("player.playback_speed", 0.25, 3.0),
|
|
},
|
|
"player.audio_sync_ms": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "0",
|
|
Validate: validateIntRange("player.audio_sync_ms", -5000, 5000),
|
|
},
|
|
"player.subtitle_sync_ms": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "0",
|
|
Validate: validateIntRange("player.subtitle_sync_ms", -10000, 10000),
|
|
},
|
|
"player.video_gravity": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "fit",
|
|
Validate: validateEnumSetting("player.video_gravity", "fit", "fill", "stretch"),
|
|
},
|
|
"player.orientation_mode": {
|
|
Scope: scopeDevice,
|
|
DefaultValue: "landscapeLocked",
|
|
Validate: validateEnumSetting("player.orientation_mode", "landscapeLocked", "rotateFreely"),
|
|
},
|
|
}
|
|
|
|
// --- Handler methods ---
|
|
|
|
// HandleListSettings handles GET /settings.
|
|
func (h *SettingsHandler) HandleListSettings(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
entries, err := store.ListSettings(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to list settings")
|
|
return
|
|
}
|
|
|
|
resp := settingsListResponse{
|
|
Settings: make([]settingResponse, 0, len(entries)),
|
|
}
|
|
for _, e := range entries {
|
|
if !keyUsesUserScope(e.Key) {
|
|
continue
|
|
}
|
|
resp.Settings = append(resp.Settings, settingResponse{
|
|
Key: e.Key,
|
|
Value: e.Value,
|
|
})
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// HandleGetSetting handles GET /settings/{key}.
|
|
func (h *SettingsHandler) HandleGetSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
key := chi.URLParam(r, "key")
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesUserScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeUser))
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
value, err := store.GetSetting(r.Context(), key)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get setting")
|
|
return
|
|
}
|
|
|
|
if value == "" {
|
|
writeError(w, http.StatusNotFound, "not_found", "Setting not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, settingResponse{
|
|
Key: key,
|
|
Value: value,
|
|
})
|
|
}
|
|
|
|
// HandleSetSetting handles PUT /settings/{key}.
|
|
func (h *SettingsHandler) HandleSetSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
key := chi.URLParam(r, "key")
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesUserScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeUser))
|
|
return
|
|
}
|
|
|
|
var req setSettingRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
|
return
|
|
}
|
|
if err := validateRegisteredSetting(key, req.Value, scopeUser); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
if err := store.SetSetting(r.Context(), key, req.Value); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to set setting")
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// HandleDeleteSetting handles DELETE /settings/{key}.
|
|
func (h *SettingsHandler) HandleDeleteSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
key := chi.URLParam(r, "key")
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesUserScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeUser))
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
if err := store.DeleteSetting(r.Context(), key); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to delete setting")
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// HandleGetDeviceSetting handles GET /settings/device/{key}.
|
|
func (h *SettingsHandler) HandleGetDeviceSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
profileID, ok := activeProfileIDFromRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
key := chi.URLParam(r, "key")
|
|
device := deviceMetadataFromRequest(r)
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesDeviceScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeDevice))
|
|
return
|
|
}
|
|
if device.DeviceID == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Device id is required")
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
h.registerRequestDevice(r.Context(), store, profileID, device)
|
|
|
|
value, err := store.GetDeviceSetting(r.Context(), profileID, device.DeviceID, key)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get device setting")
|
|
return
|
|
}
|
|
if value == nil {
|
|
writeError(w, http.StatusNotFound, "not_found", "Setting not found")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, settingResponse{
|
|
Key: key,
|
|
Value: value.Value,
|
|
})
|
|
}
|
|
|
|
// HandleSetDeviceSetting handles PUT /settings/device/{key}.
|
|
func (h *SettingsHandler) HandleSetDeviceSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
profileID, ok := activeProfileIDFromRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
key := chi.URLParam(r, "key")
|
|
device := deviceMetadataFromRequest(r)
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesDeviceScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeDevice))
|
|
return
|
|
}
|
|
if device.DeviceID == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Device id is required")
|
|
return
|
|
}
|
|
|
|
var req setSettingRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Invalid request body")
|
|
return
|
|
}
|
|
if err := validateRegisteredSetting(key, req.Value, scopeDevice); err != nil {
|
|
writeError(w, http.StatusBadRequest, "bad_request", err.Error())
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
if err := store.SetDeviceSetting(r.Context(), userstore.DeviceSettingEntry{
|
|
ProfileID: profileID,
|
|
DeviceID: device.DeviceID,
|
|
DeviceName: device.DeviceName,
|
|
DevicePlatform: device.DevicePlatform,
|
|
Key: key,
|
|
Value: req.Value,
|
|
}); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to set device setting")
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// HandleDeleteDeviceSetting handles DELETE /settings/device/{key}.
|
|
func (h *SettingsHandler) HandleDeleteDeviceSetting(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
profileID, ok := activeProfileIDFromRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
key := chi.URLParam(r, "key")
|
|
device := deviceMetadataFromRequest(r)
|
|
|
|
if key == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Setting key is required")
|
|
return
|
|
}
|
|
if !keyUsesDeviceScope(key) {
|
|
writeError(w, http.StatusBadRequest, "bad_request", fmt.Sprintf("%s is not a %s setting", key, scopeDevice))
|
|
return
|
|
}
|
|
if device.DeviceID == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Device id is required")
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
h.registerRequestDevice(r.Context(), store, profileID, device)
|
|
|
|
if err := store.DeleteDeviceSetting(r.Context(), profileID, device.DeviceID, key); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to delete device setting")
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
// HandleGetEffectiveSettings handles GET /settings/effective?keys=key1,key2
|
|
func (h *SettingsHandler) HandleGetEffectiveSettings(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
profileID, ok := activeProfileIDFromRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
keysParam := strings.TrimSpace(r.URL.Query().Get("keys"))
|
|
if keysParam == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "Query parameter keys is required")
|
|
return
|
|
}
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
|
|
device := deviceMetadataFromRequest(r)
|
|
h.registerRequestDevice(r.Context(), store, profileID, device)
|
|
keys := parseSettingKeys(keysParam)
|
|
resp := effectiveSettingsResponse{
|
|
Settings: make([]effectiveSettingResponse, 0, len(keys)),
|
|
}
|
|
for _, key := range keys {
|
|
resolved, err := h.resolveEffectiveSetting(r.Context(), store, profileID, device, key)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to resolve effective settings")
|
|
return
|
|
}
|
|
resp.Settings = append(resp.Settings, resolved)
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// HandleGetEffectiveSubtitleAppearance handles GET /settings/subtitle_appearance/effective.
|
|
func (h *SettingsHandler) HandleGetEffectiveSubtitleAppearance(w http.ResponseWriter, r *http.Request) {
|
|
userID := apimw.GetUserID(r.Context())
|
|
profileID, ok := activeProfileIDFromRequest(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
device := deviceMetadataFromRequest(r)
|
|
|
|
store, err := h.storeProvider.ForUser(r.Context(), userID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to access user store")
|
|
return
|
|
}
|
|
h.registerRequestDevice(r.Context(), store, profileID, device)
|
|
|
|
resolved, err := h.resolveEffectiveSetting(r.Context(), store, profileID, device, subtitleAppearanceSettingKey)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "internal_error", "Failed to get setting")
|
|
return
|
|
}
|
|
|
|
resp := effectiveSubtitleAppearanceResponse{
|
|
Key: subtitleAppearanceSettingKey,
|
|
ProfileID: resolved.ProfileID,
|
|
GlobalValue: resolved.UserValue,
|
|
DeviceValue: resolved.DeviceValue,
|
|
EffectiveValue: resolved.EffectiveValue,
|
|
HasDeviceOverride: resolved.HasDeviceOverride,
|
|
DeviceID: resolved.DeviceID,
|
|
DeviceName: resolved.DeviceName,
|
|
DevicePlatform: resolved.DevicePlatform,
|
|
UpdatedAt: resolved.UpdatedAt,
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// HandleSetSubtitleAppearanceDeviceOverride handles PUT /settings/device/subtitle_appearance.
|
|
func (h *SettingsHandler) HandleSetSubtitleAppearanceDeviceOverride(w http.ResponseWriter, r *http.Request) {
|
|
routeCtx := chi.NewRouteContext()
|
|
routeCtx.URLParams.Add("key", subtitleAppearanceSettingKey)
|
|
h.HandleSetDeviceSetting(w, r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeCtx)))
|
|
}
|
|
|
|
// HandleDeleteSubtitleAppearanceDeviceOverride handles DELETE /settings/device/subtitle_appearance.
|
|
func (h *SettingsHandler) HandleDeleteSubtitleAppearanceDeviceOverride(w http.ResponseWriter, r *http.Request) {
|
|
routeCtx := chi.NewRouteContext()
|
|
routeCtx.URLParams.Add("key", subtitleAppearanceSettingKey)
|
|
h.HandleDeleteDeviceSetting(w, r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeCtx)))
|
|
}
|
|
|
|
type requestDeviceMetadata struct {
|
|
DeviceID string
|
|
DeviceName string
|
|
DevicePlatform string
|
|
}
|
|
|
|
func activeProfileIDFromRequest(w http.ResponseWriter, r *http.Request) (string, bool) {
|
|
profileID := strings.TrimSpace(apimw.GetProfileID(r.Context()))
|
|
if profileID == "" {
|
|
writeError(w, http.StatusBadRequest, "bad_request", "X-Profile-Id header is required")
|
|
return "", false
|
|
}
|
|
return profileID, true
|
|
}
|
|
|
|
func deviceMetadataFromRequest(r *http.Request) requestDeviceMetadata {
|
|
return requestDeviceMetadata{
|
|
DeviceID: clampHeaderValue(r.Header.Get(deviceIDHeader), 128),
|
|
DeviceName: clampHeaderValue(r.Header.Get(deviceNameHeader), 120),
|
|
DevicePlatform: clampHeaderValue(r.Header.Get(devicePlatformHeader), 40),
|
|
}
|
|
}
|
|
|
|
func (h *SettingsHandler) registerRequestDevice(
|
|
ctx context.Context,
|
|
store userstore.UserStore,
|
|
profileID string,
|
|
device requestDeviceMetadata,
|
|
) {
|
|
if strings.TrimSpace(profileID) == "" || strings.TrimSpace(device.DeviceID) == "" {
|
|
return
|
|
}
|
|
if store == nil {
|
|
return
|
|
}
|
|
if !h.shouldRegisterDevice(profileID, device.DeviceID) {
|
|
return
|
|
}
|
|
registry, ok := store.(userstore.DeviceRegistry)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := registry.RegisterDevice(ctx, userstore.DeviceEntry{
|
|
ProfileID: profileID,
|
|
DeviceID: device.DeviceID,
|
|
DeviceName: device.DeviceName,
|
|
DevicePlatform: device.DevicePlatform,
|
|
}); err != nil {
|
|
slog.Warn("failed to register request device",
|
|
"profile_id", profileID,
|
|
"device_id", device.DeviceID,
|
|
"error", err,
|
|
)
|
|
}
|
|
}
|
|
|
|
func clampHeaderValue(value string, maxLen int) string {
|
|
value = strings.TrimSpace(value)
|
|
runes := []rune(value)
|
|
if len(runes) <= maxLen {
|
|
return value
|
|
}
|
|
return string(runes[:maxLen])
|
|
}
|
|
|
|
func parseSettingKeys(raw string) []string {
|
|
parts := strings.Split(raw, ",")
|
|
keys := make([]string, 0, len(parts))
|
|
seen := make(map[string]struct{}, len(parts))
|
|
for _, part := range parts {
|
|
key := strings.TrimSpace(part)
|
|
if key == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[key]; ok {
|
|
continue
|
|
}
|
|
seen[key] = struct{}{}
|
|
keys = append(keys, key)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
func validateRegisteredSetting(key, value string, expectedScope settingsScope) error {
|
|
spec, ok := settingsRegistry[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if spec.Scope != expectedScope {
|
|
return fmt.Errorf("%s is not a %s setting", key, expectedScope)
|
|
}
|
|
if spec.Validate == nil {
|
|
return nil
|
|
}
|
|
return spec.Validate(value)
|
|
}
|
|
|
|
func keyUsesUserScope(key string) bool {
|
|
spec, ok := settingsRegistry[key]
|
|
return !ok || spec.Scope == scopeUser
|
|
}
|
|
|
|
func keyUsesDeviceScope(key string) bool {
|
|
spec, ok := settingsRegistry[key]
|
|
return ok && spec.Scope == scopeDevice
|
|
}
|
|
|
|
func isMigratedPlaybackSetting(key string) bool {
|
|
switch key {
|
|
case "playback.preferred_quality",
|
|
"playback.audio_language",
|
|
"playback.auto_skip_intro",
|
|
"playback.auto_skip_credits",
|
|
"playback.auto_play_next",
|
|
"playback.next_up_prompt_seconds":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func usesLegacyUserFallback(key string) bool {
|
|
return key == subtitleAppearanceSettingKey
|
|
}
|
|
|
|
func validateEnumSetting(key string, allowed ...string) func(string) error {
|
|
allowedSet := make(map[string]struct{}, len(allowed))
|
|
for _, value := range allowed {
|
|
allowedSet[value] = struct{}{}
|
|
}
|
|
return func(value string) error {
|
|
if _, ok := allowedSet[value]; ok {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s must be one of %s", key, strings.Join(allowed, ", "))
|
|
}
|
|
}
|
|
|
|
func validateBoolSetting(key string) func(string) error {
|
|
return func(value string) error {
|
|
if value == "true" || value == "false" {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s must be true or false", key)
|
|
}
|
|
}
|
|
|
|
func validateIntRange(key string, min, max int) func(string) error {
|
|
return func(value string) error {
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return fmt.Errorf("%s must be an integer", key)
|
|
}
|
|
if parsed < min || parsed > max {
|
|
return fmt.Errorf("%s must be between %d and %d", key, min, max)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func validateFloatRange(key string, min, max float64) func(string) error {
|
|
return func(value string) error {
|
|
parsed, err := strconv.ParseFloat(value, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("%s must be a number", key)
|
|
}
|
|
if math.IsNaN(parsed) || parsed < min || parsed > max {
|
|
return fmt.Errorf("%s must be between %g and %g", key, min, max)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func validateJSONSetting(key string) func(string) error {
|
|
return func(value string) error {
|
|
var decoded any
|
|
if err := json.Unmarshal([]byte(value), &decoded); err != nil {
|
|
return fmt.Errorf("%s must be valid JSON", key)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (h *SettingsHandler) resolveEffectiveSetting(
|
|
ctx context.Context,
|
|
store userstore.UserStore,
|
|
profileID string,
|
|
device requestDeviceMetadata,
|
|
key string,
|
|
) (effectiveSettingResponse, error) {
|
|
spec, hasSpec := settingsRegistry[key]
|
|
resolved := effectiveSettingResponse{
|
|
Key: key,
|
|
ProfileID: profileID,
|
|
DeviceID: device.DeviceID,
|
|
DeviceName: device.DeviceName,
|
|
DevicePlatform: device.DevicePlatform,
|
|
}
|
|
|
|
if hasSpec && spec.Scope == scopeDevice {
|
|
resolved.EffectiveValue = ""
|
|
resolved.Source = "default"
|
|
if spec.DefaultValue != "" {
|
|
resolved.EffectiveValue = spec.DefaultValue
|
|
} else {
|
|
resolved.Source = "unset"
|
|
}
|
|
if device.DeviceID != "" {
|
|
override, err := store.GetDeviceSetting(ctx, profileID, device.DeviceID, key)
|
|
if err != nil {
|
|
return effectiveSettingResponse{}, err
|
|
}
|
|
if override != nil {
|
|
resolved.DeviceValue = override.Value
|
|
resolved.EffectiveValue = override.Value
|
|
resolved.Source = "device"
|
|
resolved.HasDeviceOverride = true
|
|
resolved.DeviceName = override.DeviceName
|
|
resolved.DevicePlatform = override.DevicePlatform
|
|
resolved.UpdatedAt = override.UpdatedAt
|
|
return resolved, nil
|
|
}
|
|
if isMigratedPlaybackSetting(key) {
|
|
legacyValue, err := store.GetSetting(ctx, key)
|
|
if err != nil {
|
|
return effectiveSettingResponse{}, err
|
|
}
|
|
if legacyValue != "" {
|
|
entry := userstore.DeviceSettingEntry{
|
|
ProfileID: profileID,
|
|
DeviceID: device.DeviceID,
|
|
DeviceName: device.DeviceName,
|
|
DevicePlatform: device.DevicePlatform,
|
|
Key: key,
|
|
Value: legacyValue,
|
|
}
|
|
if err := store.SetDeviceSetting(ctx, entry); err != nil {
|
|
return effectiveSettingResponse{}, err
|
|
}
|
|
resolved.DeviceValue = legacyValue
|
|
resolved.EffectiveValue = legacyValue
|
|
resolved.Source = "device"
|
|
resolved.HasDeviceOverride = true
|
|
return resolved, nil
|
|
}
|
|
}
|
|
}
|
|
if usesLegacyUserFallback(key) {
|
|
legacyValue, err := store.GetSetting(ctx, key)
|
|
if err != nil {
|
|
return effectiveSettingResponse{}, err
|
|
}
|
|
if legacyValue != "" {
|
|
resolved.UserValue = legacyValue
|
|
resolved.EffectiveValue = legacyValue
|
|
resolved.Source = "user"
|
|
return resolved, nil
|
|
}
|
|
}
|
|
return resolved, nil
|
|
}
|
|
|
|
userValue, err := store.GetSetting(ctx, key)
|
|
if err != nil {
|
|
return effectiveSettingResponse{}, err
|
|
}
|
|
resolved.UserValue = userValue
|
|
resolved.EffectiveValue = userValue
|
|
resolved.Source = "user"
|
|
|
|
if resolved.EffectiveValue != "" {
|
|
return resolved, nil
|
|
}
|
|
|
|
if hasSpec && spec.DefaultValue != "" {
|
|
resolved.EffectiveValue = spec.DefaultValue
|
|
resolved.Source = "default"
|
|
return resolved, nil
|
|
}
|
|
|
|
resolved.Source = "unset"
|
|
return resolved, nil
|
|
}
|
|
|
|
// overlayConfigResponse is returned by GET /settings/overlay-config.
|
|
type overlayConfigResponse struct {
|
|
Enabled bool `json:"enabled"`
|
|
Defaults string `json:"defaults,omitempty"`
|
|
}
|
|
|
|
// HandleGetOverlayConfig returns the server-wide overlay configuration.
|
|
// Available to all authenticated users (not admin-only).
|
|
func (h *SettingsHandler) HandleGetOverlayConfig(w http.ResponseWriter, r *http.Request) {
|
|
resp := overlayConfigResponse{Enabled: true}
|
|
|
|
if h.serverSettings != nil {
|
|
if v, _ := h.serverSettings.Get(r.Context(), "overlays.enabled"); v == "false" {
|
|
resp.Enabled = false
|
|
}
|
|
if v, _ := h.serverSettings.Get(r.Context(), "defaults.card_overlays"); v != "" {
|
|
resp.Defaults = v
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Cache-Control", "private, max-age=60")
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|