* docs: design spec for autoscan arr polling Periodic poller over autoscan-enabled Radarr/Sonarr instances (reusing request_integrations) that maps import paths to Silo media folders and enqueues targeted scans via the existing scantrigger + scanqueue. Lean single-service model: no cross-node fan-out guard or retry queue. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: implementation plan for autoscan arr polling Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): settings and sources schema Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): core types Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): path rewrite helper Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): dedupe imported paths to parent folders Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): arr import-history client Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): settings + sources repository Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): redis scan-suppression seam Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): PollOnce poll cycle * feat(autoscan): poll task and wiring * feat(autoscan): admin API endpoints * feat(autoscan): admin API endpoints Adds ErrIntegrationNotFound sentinel (errors.Is) instead of string matching. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan types and hooks Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(web): autoscan admin tab * fix(autoscan): release suppression claim on enqueue failure; reconfigure trigger on interval change; skip source on key-resolution error Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): update handler test for 3-arg NewAutoscanHandler * fix(autoscan): per-path suppression key, bounded poll window + overlap, boundary-safe rewrites, GREATEST cursor guard, async trigger, quiet unresolved-path skip, FK->404 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): normalize Windows path separators, surface status errors, re-seed source editor on save Addresses minor code-review findings: Windows backslash paths now normalized before rewrite/dedupe; HandleStatus returns repository errors instead of 200; the per-source editor re-seeds from server data after a save. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: design spec for autoscan rewrite-sync from arr root folders Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: implementation plan for autoscan rewrite-sync Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): suffix-match rewrite suggester Add suggestRewrites / commonSuffixLen for Task 1 of the autoscan arr-polling feature. Pure function: matches arr root-folder paths to Silo media folder paths by longest common trailing segment count, adjusted for depth-delta so coincidental same-named segments at different structural levels don't inflate confidence. Categorises each arr root as Proposed, Ambiguous, Unmatched, or Covered by an existing PathRewrite rule. TDD: test file written first, verified failing, then implementation added. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): GetSource single-source lookup * feat(autoscan): arr root-folder client + Silo folder lister * feat(autoscan): Service.SuggestRewrites Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): rewrite-suggestions endpoint Add GET /autoscan/sources/{id}/rewrite-suggestions admin endpoint: extend the autoscanTriggerer interface with SuggestRewrites, wire SetRewriteResolvers in the router, and add handler + test. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(web): autoscan rewrite-suggestions types and hook * feat(web): autoscan sync-rewrites preview * fix(autoscan): normalize covered-rule paths, dedup roots/folders, skip no-op suggestions Addresses final-review edge cases: coveredBy normalizes the existing rewrite's From (so a stored Windows/dup-slash rule still covers a root); duplicate arr roots and duplicate Silo folder paths are de-duplicated; an arr path that already equals its Silo path is not proposed as a no-op rewrite. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(web): vitest 4 compatible fetch spy in recipes.test (unblocks build after vitest 4.1.0 bump) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): non-null suggestion slices + move Sync into rewrites card - suggestRewrites initializes Proposed/Unmatched/Ambiguous/Covered to empty slices so the JSON response is [] not null — fixes the 'Something went wrong' crash when every root is already covered (frontend mapped over null). - Move the sync button into the Path rewrites card beside 'Add rewrite' and rename it 'Sync rewrites'; guard the proposed map with ?? []. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): long root-folder timeout + sync spinner + collapse rewrites on load - Root-folder fetch for sync uses a 2-min timeout: Radarr/Sonarr compute unmappedFolders by scanning all roots, so a large library's /rootfolder takes 20-30s+ and tripped arrclient's 30s default (Sonarr 502'd at exactly 30s). - Spin the sync icon + show 'Syncing…' while the request is in flight. - Path rewrites card starts collapsed on page load. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): rescan on Sonarr/Radarr file renames History polling previously only tracked downloadFolderImported events. A rename in Sonarr/Radarr (episodeFileRenamed / movieFileRenamed) moves a file without an import event, leaving the library folder stale until the next full scan. Extend the history client to also surface renamed paths: both the new path and the old sourcePath, since a rename can move a file between folders and both parents may need rescanning. Delete events are still skipped — upgrade-deletes are covered by the paired import, and standalone deletes carry no file path in arr history. Renames the interface method ImportedPaths -> ChangedPaths to reflect the broader scope. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test(autoscan): synchronize trigger test with detached PollOnce goroutine HandleTrigger dispatches PollOnce on a detached goroutine and responds 202 immediately. The test read trig.called straight after the handler returned, racing the goroutine (usually 'PollOnce was not invoked') and reading the field without synchronization (a data race under -race). Signal completion through a channel the fake sends on when PollOnce runs; the test waits on it (bounded) before asserting. The channel send happens-before the receive, so the subsequent read of called is race-free. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: design spec for autoscan as a pluggable scan-source category Reframes autoscan from a Requests-coupled, arr-only feature into a standalone Autoscan category. Change-detection providers become out-of-process plugins via a new additive scan_source.v1 capability (client-pull, opaque marker); Sonarr/Radarr is the first provider. Host keeps a provider-agnostic resolve/suppress/enqueue engine; all arr-specific logic (and path rewrites) move into the plugin. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: implementation plan for scan_source.v1 SDK capability First of the per-repo plans from the autoscan-plugin-architecture spec. Adds the additive scan_source.v1 capability to silo-plugin-sdk (proto + codegen + capability allowlist + runtime wiring), TDD per task, tagged as v0.5.0 so the host and arr-plugin plans can build against it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: implementation plan for autoscan host backend (part 1 of 2) Backend for the standalone Autoscan category: scan_source.v1 plugin plumbing (pluginhost client + plugins.Service resolver), generalized engine driven by a provider seam, autoscan_connections + autoscan_sources schema (decoupled from Requests), connection resolution (own or Requests-linked), admin API. Depends on silo-plugin-sdk v0.5.0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: implementation plans for autoscan arr plugin + host UI arr plugin: new installable scan_source.v1 plugin (history imports+renames, rewrites, Silo-native paths), structured like silo-plugin-tmdb; ports the arr-specific logic from the closed PR #43. host UI (part 2 of 2): standalone Autoscan admin category (connections, sources, settings) extracted out of Requests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * build(autoscan): replace silo-plugin-sdk with local scan_source.v1 checkout Temporary dev replace so the host backend can build against the unreleased scan_source.v1 capability (silo-plugin-sdk PR #2). Finalize to v0.5.0 once the SDK is tagged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(pluginhost): scan_source.v1 capability client wrapper Adds ScanSourceClient struct, the Client.ScanSource() accessor (mirrors ScheduledTask pattern), and a PollChanges method. Also introduces client_test.go with capability-gate tests for both scheduled_task.v1 and scan_source.v1 using a lazy gRPC ClientConn. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test+fix(pluginhost): cover capability-id gate, dedicated scan_source timeout Adds a "wrong id returns error" subtest to both capability-gate tests so the capability-ID component is exercised independently of the type. Introduces DefaultScanSourceTimeout (2m) for PollChanges, which polls an external arr API that can be slow, instead of the generic 10s control timeout. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(plugins): expose scan_source.v1 client resolver Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(migrations): autoscan v2 schema (connections + sources, no requests FK) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): v2 types and repository Replace the request_integrations-coupled model with the decoupled v2 schema (autoscan_settings + autoscan_connections + autoscan_sources). Connection CRUD, source upsert/list/get, and AdvanceMarker/RecordError for opaque marker bookkeeping. ErrIntegrationNotFound becomes ErrNotFound. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): resolve connections (own credentials or Requests-linked) ConnectionResolver turns a stored Connection into concrete credentials, reading a soft-linked Requests integration's live base URL/key when RequestIntegrationID is set, then resolving the api-key ref to plaintext. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): scan-source provider seam over the plugin resolver ScanSourceProvider lets the engine poll changed paths without a live plugin; pluginProvider adapts plugins.Service.ScanSourceClient in production. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): generic engine drives sources via scan_source provider Rewrite PollOnce to iterate enabled sources, resolve each connection, poll the provider for changed paths, and run the salvaged resolve→suppress→enqueue loop (uniqueParentDirs, (folder,path) suppression key, RequestError quiet-skip, release-claims-on-enqueue-fail) verbatim. Store the opaque next marker via AdvanceMarker only after a successful enqueue; RecordError + keep marker on provider failure. Tests reworked onto a fakeProvider/fakeStore with an added opaque-marker-verbatim assertion. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): drop conflicting connection CHECK, add connection resolver tests - migration 172: remove the autoscan_connections_source_present CHECK. It conflicted with request_integration_id ON DELETE SET NULL: deleting a Requests integration that a linked-only connection (base_url NULL) points at would null the FK and trip the CHECK, blocking the delete. The intended behavior is for the connection to survive as an orphaned 'needs attention' row. Creation-time validity is now enforced at the application layer. Verified on a throwaway DB: full chain applies and the delete-cascade leaves an orphaned (both-null) connection. - connection.go: TrimSpace the api key ref + resolved secret before the empty-string checks, matching requests.resolveAPIKey parity. - connection_test.go: fake-based tests for ConnectionResolver.Resolve (own creds, linked, linked-missing error, lookup error, trim/fallback). - repository.go: bound RecordError's stored last_error to 2048 chars. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(api): autoscan v2 admin endpoints Rewrite the autoscan admin HTTP handler against the v2 model: settings, connection CRUD, source update, manual trigger (detached PollOnce), and status. Connection/source responses omit api_key_ref and resolved keys (has_api_key flag only); unknown connection/source ids map to 404 via autoscan.ErrNotFound. Retire the host-side rewrite-suggestions endpoint (now lives in the arr plugin). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): wire v2 service, routes, retire rewrite-suggestions Export PollChangesClient/ScanSourceResolver from the autoscan provider so the api package can declare a structurally-conformant plugin adapter (Go has no return-type covariance, so the adapter must name the interface as its return type). Add api.BuildAutoscanService with the requests-integration lookup and plugin scan-source adapters, shared by the router (manual trigger) and the background poll task. Re-wire router routes to the v2 connections/sources/settings/trigger/status surface and drop the rewrite-suggestions route. Update cmd/silo to build the v2 poll task, seeding its interval from Settings.DefaultPollIntervalSeconds. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(api): enforce connection requires own URL or a Requests link Migration 172 dropped the DB CHECK that required an autoscan connection to carry either its own base_url or a request_integration_id, delegating that invariant to the application layer — but the enforcement was never added, so HandleCreateConnection/HandleUpdateConnection accepted both-NULL orphans that ConnectionResolver.Resolve would hand a plugin as an empty base URL. Add a shared validateConnectionInput helper (whitespace-only request_integration_id counts as absent) and reject both-empty payloads with HTTP 400 on both the create and update paths. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): deliver resolved connection to plugin PollChanges now populates PollChangesRequest.Connection with the resolved {base_url, api_key} instead of dropping the conn param on the floor. Drops the stale doc comment claiming the connection was delivered out-of-band at upsert time -- that mechanism never existed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): auto-discover sources from installed scan_source plugins Auto-discovery seeds a disabled, connection-less source row per installed scan_source.v1 capability before an operator binds a connection, so connection_id is now nullable end to end: - migration 172: connection_id drops NOT NULL (still ON DELETE RESTRICT) - Source.ConnectionID becomes *string; repository scans/writes it as nullable and adds idempotent EnsureSource (INSERT ... ON CONFLICT DO NOTHING) - new ScanSourceLister seam + Service.DiscoverSources, called at the start of PollOnce (errors logged, non-fatal); production adapter enumerates ListEnabled -> ListCapabilities filtered to scan_source.v1 - PollOnce skips an enabled source with no connection bound, recording 'no connection bound' so the UI can surface it - HandleUpdateSource rejects enabling a source with no effective connection (400); source DTOs expose connection_id as nullable - BuildAutoscanService / NewService thread the installation store at both wiring sites (router + poll task) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): honor per-source poll interval PollOnce now skips an enabled source that ran too recently: the floor is source.PollIntervalSeconds when set, else settings.DefaultPollIntervalSeconds. The global poll task fires at the default cadence, so this makes the per-source interval a 'poll at most every N seconds' floor. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(autoscan): reconcile spec + arr-plugin plan with credential-in-request + auto-discovery The credential-delivery mechanism changed during execution: the host now passes resolved {base_url, api_key} in PollChangesRequest.connection each poll (not plugin runtime config). Also records source auto-discovery, nullable connection_id, and the per-source interval floor decided at the final integration review. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan v2 types and query hooks Replace v1 autoscan types and hooks with v2 DTOs matching the backend handler (autoscan.go): settings, connection (with has_api_key, no raw key), source (installation_id/capability_id/connection_id), status. Add connections CRUD hooks, useAutoscanStatus, update sources hook to v2 input shape. Retain deprecated shims for AutoscanPathRewrite, AutoscanRewriteSuggestions, and useAutoscanRewriteSuggestions so AdminRequests.tsx continues to compile until Task 6 removes that tab. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan connections panel (reuse or own) Card+Table listing connections with "Reused from Requests" / "Own" badges. Add/edit dialog with two modes: reuse a Sonarr/Radarr Requests integration or enter own name/URL/API-key credentials. Delete with alert-dialog confirm. Never renders key material — only has_api_key is sent by the backend. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan sources panel Table of auto-discovered scan sources (one row per installed scan_source plugin capability). Operator can bind a connection via inline Select (auto-saved on change), set a per-source poll interval (saved on blur), and toggle enabled. Shows a "Needs connection" badge for unbound sources; attempting to enable without a connection lets the backend 400 surface via the existing toast in useUpdateAutoscanSource.onError. Status column shows last_run_at relative time or last_error with icon. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): standalone Autoscan admin page Tabs page (Sources | Connections | Settings) mirroring AdminRequests header/layout. Settings tab exposes global enable switch, default poll interval, and debounce — all auto-saved on blur or toggle. "Run now" button calls useTriggerAutoscan and toasts "Autoscan triggered" on 202. Route and sidebar nav are intentionally deferred to Task 5. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): route and sidebar nav for Autoscan category Add /admin/autoscan route pointing to AdminAutoscan and a matching "Autoscan" item in the Content group of the admin sidebar (with RefreshCw icon), so the new standalone page is reachable from the nav. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(web): move Autoscan out of Requests into its own category Remove the Autoscan tab, AutoscanTab/AutoscanSourceEditor component definitions, and AutoscanSettingsFormState from AdminRequests.tsx. Delete the Task-1 compatibility stubs: AutoscanPathRewrite and AutoscanRewriteSuggestions types from api/types.ts, and the useAutoscanRewriteSuggestions no-op shim from useAutoscan.ts. The build confirms zero dangling references. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): allow unbinding a source connection (full-state source update) Change the source-update input struct's connection_id from string to *string so the UI can send null to unbind, a UUID to bind, or omit (null) to clear. Remove the fall-back-to-existing logic; the handler now sets the source's ConnectionID directly from the input. The enable-guard fires when the resulting connection is nil regardless of cause. Frontend sends the complete triple (connection_id, enabled, poll_interval_seconds) on every mutation site; selecting "— No connection —" sends null for a real unbind. Adds aria-label to connection Select and interval Input for accessibility. Backend tests cover bind, unbind, unbind while enabled → 400, and enable without connection → 400. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(migrations): backfill autoscan v1 settings+connections instead of dropping Migration 172 unconditionally DROPped the shipped v1 autoscan_settings/ autoscan_sources (migration 171), losing an upgraded operator's enable flag, poll cadence, debounce, and arr server list — autoscan came back OFF. Rewrite 172 up to be non-destructive of what can be carried: rename the v1 tables aside, create the v2 schema, backfill settings (poll minutes -> seconds) and seed a reusable LINKED connection per distinct v1 source integration, then drop the renamed v1 tables. v2 sources are keyed on a plugin (installation_id, capability_id) that did not exist in v1, so they are left to runtime discovery; path rewrites move to plugin config and are intentionally not carried. Verified against a throwaway DB: after 171 + v1 seed data, applying 172 yields enabled=true, default_poll_interval_seconds=300, debounce_seconds=30, and one autoscan_connections row linked to the v1 integration. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): preserve api key on metadata-only connection edit UpdateConnection unconditionally wrote api_key_ref = nullable(c.APIKeyRef), so a metadata-only edit (the UI omits the key when left blank — "leave blank to keep existing") NULLed the stored key and broke the next poll. Mirror requests' UpdateIntegration: api_key_ref = CASE WHEN $5 = '' THEN api_key_ref ELSE $5 END, passing the raw trimmed string so a blank incoming ref keeps the existing value. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): skip orphaned sources + add source delete endpoint An enabled source whose scan_source plugin was uninstalled/disabled kept its autoscan_sources row, which errored every poll cycle, and there was no way to remove it. DiscoverSources now returns the set of currently-discovered (installation_id, capability_id) pairs; PollOnce skips any enabled source not in that set quietly (no RecordError), stopping the per-cycle error spam for orphans. A nil set (no lister / discovery failed) disables pruning so a transient discovery failure does not silence live sources. Adds DELETE /admin/autoscan/sources/{id} -> HandleDeleteSource -> repo.DeleteSource so an operator can clear orphans (unknown id -> 404). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): reject reused connection when Requests integration is disabled RequestIntegrationLookup.Get returned a linked integration's base_url/api_key even when the integration was disabled or had a blank base_url (the v1 poll gate `WHERE ri.enabled = true` was dropped in v2). Now Get surfaces a disabled or unconfigured linked integration as an error, which the engine turns into a logged skip / RecordError instead of polling an unusable target. The gating is extracted into a pure checkRequestIntegrationUsable helper so it is unit-testable without a DB-backed repo. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): reschedule poll task on settings change HandleUpdateSettings no longer rescheduled the poll task (the v1 triggerUpdater / UpdateTriggers wiring was dropped in v2), so a default_poll_interval_seconds change only applied after a restart. Re-add an optional triggerUpdater (taskmanager.UpdateTriggers) on AutoscanHandler, wired via SetTriggerUpdater from the router when a task manager is available. On a successful settings update the handler recomputes the interval trigger from default_poll_interval_seconds and calls UpdateTriggers("autoscan_poll", ...). The dependency is optional: a nil updater skips rescheduling so tests need no task manager, and a reschedule failure is non-fatal (the interval is persisted). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(web): disable enable toggle for unbound sources, add source delete + interval hint - Disable the Enable switch when a source has no effective bound connection (connection_id null and no pending edit selection), re-enabling once bound. - Add useDeleteAutoscanSource hook mirroring useDeleteAutoscanConnection pattern. - Add per-row delete button (Trash2 icon → AlertDialog confirm) to let operators remove orphaned/unwanted source rows. - Add interval floor helper text showing the global default poll interval so operators know values below it have no effect. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): consume source_paths from merged scan_source contract The merged plugin SDK renamed PollChangesResponse.changed_paths to source_paths and the plugin now returns RAW source-namespace paths. pluginProvider.PollChanges reads GetSourcePaths(); the host applies per-source path rewrites before resolving/enqueueing (separate commit). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(migrations): add path_rewrites to autoscan_sources Add path_rewrites jsonb NOT NULL DEFAULT '[]' to the autoscan_sources CREATE in migration 172 (unreleased/branch-only, so amended in place). The host now owns per-source prefix rewrites. v1 path_rewrites cannot be backfilled (v2 sources key on a plugin installation/capability with no v1 mapping); documented that operators must re-enter rewrites post-upgrade. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): host-owned per-source path rewrites Rewrite ownership moved from the scan_source plugin to the host. The plugin returns raw source-namespace paths; the host now normalizes separators and applies the source's per-source prefix rewrites before dedupe/resolve/enqueue. - types: add PathRewrite{From,To} and Source.PathRewrites - rewrite: re-add applyRewrites/normalizeSeparators; apply the MOST-SPECIFIC (longest From) match, not first-match, so a broad rule can't shadow a nested one regardless of ordering - service.PollOnce: rewrite raw provider paths before resolveAndClaim - repository: marshal/unmarshal path_rewrites jsonb in UpsertSource and all source scans (EnsureSource discovery rows take the DB default []) - handlers: autoscanSourceInput/response + status DTO carry path_rewrites (full-state like connection_id); reject blank from/to with 400 - tests: rewrite unit tests, engine applies rewrites before enqueue, handler round-trips path_rewrites and 400s on a blank rewrite Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): discover installed scan_source plugins on sources-list view A scan_source plugin installed via the normal /admin/plugins flow must show up in the Autoscan component immediately, not only after a poll cycle (which runs only when autoscan is enabled). HandleListSources now runs discovery (seeding a disabled, connection-less source row per installed scan_source capability) before listing. Best-effort: discovery failure does not block listing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): per-source path rewrites editor + plugins-page install hint Add AutoscanPathRewrite type and path_rewrites fields to AutoscanSource/ AutoscanSourceInput. SourcesPanel gains an expandable rewrite editor per source row (from→to pairs, Add/Remove/Save) threaded into the full-state body so connection, interval, and rewrite changes always carry all fields. Adds a Plugins-page install hint in both the empty state and above the table for discoverability. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(autoscan): host-owned path rewrites + install/discovery flow Reconcile the spec with the merged SDK decision (rewrites moved host-side; PollChangesResponse.source_paths carries raw provider paths). Document that scan-source plugins install via the normal /admin/plugins page and surface in Autoscan via discovery (run on poll cycles and on sources-list view). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * build: depend on merged silo-plugin-sdk via pseudo-version (drop local replace) PR #2 (scan_source.v1 + source_paths) is merged to silo-plugin-sdk main, so the host can resolve the canonical module at the merged commit (v0.4.1-0.20260603030807-807b07e785b2) instead of a local-path replace. The branch now builds off-machine (CI/Docker). Bump to a clean v0.5.0 once tagged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * refactor(migrations): single clean autoscan v2 migration (v1 never shipped) The v1 in-process autoscan (migration 171) was never released to origin/main, so no live system has v1 autoscan data to preserve. Collapse the v1-create + v2-rename/backfill/drop dance into one clean 171 that creates the v2 connections-based schema directly. Removes 172 entirely. The runner applies by version set-difference with no checksum validation, so the already-migrated test instance (171+172 recorded) skips both and is unaffected; fresh installs get the clean v2 schema in one step. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): allow many sources per plugin + add-source enumeration Drop the one-source-per-(installation, capability) model. A single installed scan_source plugin capability can now back many sources, each bound to a different connection (e.g. one Sonarr plugin fronting four arr servers). - migration 171: remove the autoscan_sources UNIQUE(installation_id, capability_id) constraint; sources are operator-created, not auto-seeded. - repository: replace UpsertSource (relied on the unique conflict) with a plain CreateSource (fresh uuid) + a by-id UpdateSource; remove EnsureSource. - discovery: replace auto-seeding (DiscoverSources/RefreshDiscovered) with ListAvailableScanSources (the Add-source picker list, enriched with plugin id + display name) and an installedScanSources set used only for orphan-skip. - service: PollOnce stops seeding and instead fetches the installed-capability set for orphan detection; Store gains GetSource and drops EnsureSource. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): connection test endpoint (engine) Add Service.TestConnection / TestConnectionByID: resolve a connection (ad-hoc input or an existing stored connection) to concrete credentials and probe the arr GET /api/v3/system/status with a short timeout. A reachable/authorized target yields OK=true plus the reported version; an unreachable / 401 / non-200 target yields OK=false with a human-readable error (the probe failure is part of the result payload, never an error from the method itself). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): host-side rewrite suggester + admin API for new endpoints Port the path-rewrite suggester back host-side (it had moved into the plugin): suggestRewrites suffix-matches arr root folders against Silo media folders to propose path rewrites, reporting proposed / unmatched / ambiguous / covered. Service.SuggestRewrites resolves the source's bound connection, lists arr roots (GET /api/v3/rootfolder) and Silo folder paths, and runs the matcher; a source with no bound connection returns ErrNoConnection (400). Admin API (all admin-gated): - POST /admin/autoscan/sources create a source - GET /admin/autoscan/scan-source-plugins Add-source picker list - POST /admin/autoscan/connections/test probe a connection - GET /admin/autoscan/sources/{id}/rewrite-suggestions sync rewrites HandleListSources no longer auto-seeds; create validates the capability is currently installed and that enabling requires a connection. Wiring threads the arr root-folder/status client and the catalog folder lister through BuildAutoscanService; the lister now surfaces plugin id + display name. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan hooks + types for sources, connection test, rewrites Add types and React Query hooks backing the autoscan admin UI batch: - AutoscanAvailableSource / useAvailableScanSources (scan-source plugins) - AutoscanSourceCreateInput / useCreateAutoscanSource (POST sources) - AutoscanConnectionTestResult / useTestAutoscanConnection (advisory test) - AutoscanRewriteSuggestions / useAutoscanRewriteSuggestions (on-demand) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): add-source dialog + sync-from-arr rewrites in SourcesPanel Add a "+ Add source" header action opening a dialog that creates a scan source from any installed scan-source plugin bound to an arr connection, so operators can add one source per connection (e.g. four arr instances). Empty state links to /admin/plugins when no plugins are installed. Add a "Sync from arr" button to each source's rewrite editor that fetches root-folder rewrite suggestions and renders a preview: checkbox-selectable Proposed rewrites plus collapsed Unmatched / Ambiguous / Already-mapped sections. "Apply selected" merges the checked rewrites (dedupe by `from`) and persists via the normal full-state source PUT. Sync is disabled until the source has a bound connection. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): test-connection button in autoscan ConnectionsPanel dialog Add an advisory "Test connection" button to the add/edit connection dialog. It probes the current dialog input — connection_id when editing, request_integration_id in reuse mode, or base_url/api_key_ref for own credentials — and renders the result inline: green "Connected (vX.Y)" on success, red error on failure. Never blocks save; stale results clear when credential fields change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): autoscan page polish + global enable toggle in header Surface a global Autoscan enable toggle and an enabled/disabled status badge next to the page title, alongside the existing "Run now" header action so primary controls are reachable without opening a tab. Remove the now-redundant enable switch from the Settings tab (it points at the header toggle instead). Tighten header layout for wrap on narrow widths. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(web): right-align autoscan enable toggle + Run now in the page header Drop the redundant nested justify-between wrapper so the header actions sit directly under .page-header (space-between + bottom-align), matching the /admin/libraries header layout. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): hold poll marker when paths return but none resolve A freshly-enabled source whose path_rewrites aren't configured yet returns provider paths that resolve to zero library folders. PollOnce previously advanced the marker unconditionally on any successful poll, permanently skipping those imports. Now the marker advances only when there is nothing to do (zero paths) or at least one path resolved+enqueued; when paths come back but none resolve, the marker is held and an explaining error recorded so the operator can fix the rewrites and a later poll re-reads the same window. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): don't prune sources of disabled-but-installed plugins PluginScanSourceLister used the installation store's ListEnabled, so a temporarily-disabled plugin dropped out of the discovered set and PollOnce treated its sources as orphaned, skipping them with no last_error (silent vanish). Switch to List so only a fully-uninstalled plugin counts as orphaned; a disabled-but-installed plugin's sources are still attempted and surface a visible RecordError when the client fails to load. The Add-source picker shares the same all-installed set. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): treat empty request_integration_id as no link ConnectionResolver.Resolve gated the linked-integration path on a non-nil RequestIntegrationID pointer, so a pointer-to-empty-string (from a both-NULL orphan or a stripped link) called requests.Get(""). Guard on a non-empty trimmed value so it falls back to the connection's own fields instead. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): align startup poll interval with reschedule computation Startup seeded the poll task by integer-dividing default_poll_interval_seconds by 60 (minutes), while HandleUpdateSettings reschedules with seconds*1000 ms; the two diverged for sub-minute and non-60-multiple intervals. NewAutoscanPollTask now takes the interval in milliseconds and main.go seeds it as seconds*1000, matching the reschedule path so both agree. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): normalize stored rewrite From at poll time applyRewrites matched the stored From after only TrimSpace/TrimSuffix, while suggest.go coveredBy normalizes via normalizePath (backslash->slash, collapse '//'). A Windows-style or dup-slash stored rewrite was thus reported 'covered' at suggest time yet never matched at poll time. applyRewrites now normalizes From through normalizePath so poll-time and suggest-time agree. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(web): don't corrupt source poll interval on enable/connection change Add a `parseInterval` helper that maps empty input to null (use global default), valid positive integers to the integer, and any other mid-edit-invalid value to the source's currently-persisted `poll_interval_seconds` — so toggling the enable switch or changing the connection cannot silently overwrite the interval with 0 or NaN. Wire the helper through `fullBody()` (the single source of truth for PUT payloads) and remove the two inline duplications in `handleConnectionChange` and `handleRewriteSave` that both previously used raw `Number()`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): make the source connection optional (provider-agnostic) A host connection is the credential/endpoint for server-based providers (Sonarr/Radarr); other scan_source providers (e.g. a CephFS/filesystem watcher that reads ceph.dir.r* xattrs) need none. PollOnce now polls connection-less sources, passing an empty ResolvedConnection the plugin may ignore; a plugin that requires credentials surfaces the error at poll time. Drops the enable-requires-connection 400s. Provider-specific config lives in the plugin's own global_config_schema, not a host connection. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(web): provider-agnostic autoscan copy + optional source connection Replace arr-hardcoded framing in AdminAutoscan, SourcesPanel, and ConnectionsPanel with neutral scan-source language. Remove the connection-required gate on the source enable toggle so connectionless providers (e.g. filesystem watchers) can be enabled; soften the badge from "Needs connection" to "No connection". Sync-from-server button remains gated on a bound connection (it needs a server to query). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: repo-relative paths in autoscan plans Replace local absolute filesystem paths (/opt/silo, sibling checkouts, /tmp/go/bin) in docs/superpowers/plans with repository-relative wording per CLAUDE.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): rune-safe last_error truncation Truncate RecordError messages on a UTF-8 rune boundary so a byte-bounded cut can't split a multi-byte rune and store invalid UTF-8. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): advance marker when resolved-but-suppressed (not unresolved) resolveAndClaim now reports resolvedAny (whether any path mapped to a Silo library folder, independent of suppression). PollOnce gates the "none matched a Silo library folder" hold+RecordError on !resolvedAny instead of len(targets)==0, so a poll whose paths resolved but were all debounced/suppressed advances the marker instead of being treated as a misconfiguration. Adds a regression test for the suppressed case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(api): normalize request_integration_id Trim whitespace and collapse empty-after-trim request_integration_id to nil on connection create and update, so a pointer-to-"" or " " is never persisted as a bogus Requests link. Also corrects a stale migration-172 comment to 171 (the collapsed migration number). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(autoscan): provider-agnostic poll-task copy Rename the poll task to "Autoscan poll" with a provider-agnostic description and progress message; drop Sonarr/Radarr/arr wording. Key() (autoscan_poll) is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test(autoscan): fix typo in connectionless-source test name Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): add scan source management * chore(deps): bump silo-plugin-sdk for structured scan source changes Pins silo-plugin-sdk to 0d78651, which adds source_config on PollChangesRequest plus the structured changes / ScanSourceChangeScope fields on PollChangesResponse that internal/autoscan/provider.go already consumes. Without this the branch fails to compile against the prior pin (807b07e). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): label scan sources by connection name in admin UI arr-plugin sources fan out one-per-connection under a single generic "arr" capability, so every row in the Sources and Activity panels rendered an identical "arr (plugin #N)" label. Lead with the bound connection name (Radarr/Sonarr/...) instead, demoting capability + plugin to a subtitle. Sources without a connection (e.g. cephfs) keep the capability fallback. Activity threads a source_id -> connection name lookup (built from the existing sources + connections queries) through the scan/poll tables the same way librariesByID is threaded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(autoscan): spec for generic + operator-editable source labels Design for a shared label-resolution helper (operator label -> connection name -> manifest display_name -> capability_id) consumed by the Sources and Activity panels, plus an operator-editable per-source label backed by a new autoscan_sources.label column. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(autoscan): implementation plan for source labels Task-by-task TDD plan: migration 174 (label column), Go domain/repo/handler wiring with server-side normalization, shared frontend label helper, and Sources/Activity panel integration. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): migration for source label column * feat(autoscan): source label domain field + normalizer * feat(autoscan): persist source label in repository Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): accept, normalize, and return source label Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): add label to source API types * feat(autoscan): shared source-label resolution helper Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(autoscan): polish source-label helper per review Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * feat(autoscan): label sources via shared helper + operator label input Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor(autoscan): clarify source label naming per review * feat(autoscan): resolve activity source labels via shared helper Replace the sourceNames Map plumbing in ActivityPanel with SourceLabelLookups and delegate both name functions to resolveEventSourceName from @/lib/autoscanLabels, enabling the full label chain (operator label → connection name → manifest display_name → capability_id) for all Scan History and Poll log rows. * fix(autoscan): carry label on status source + guard poll label Final-review follow-ups: add the label field to the autoscanStatusSource response (and AutoscanStatusSource type) so the status view matches the source response per spec, and give pollSourceName a non-empty fallback for symmetry with scanSourceName. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autoscan): resolve source aria-labels through the label chain Replace the legacy capability-only sourceLabel() helper with resolveSourceName() (operator label -> connection -> display_name -> capability). Row controls now announce the row's resolvedLabel (reflecting in-progress edits) and the delete dialog announces the resolved name, so screen readers hear "4K Movies" instead of "arr (plugin #4)". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autoscan): paginate queue + history with a shared table pager Replace the card/table hybrid and 200-row "Load more" cap on the autoscan Activity panel with proper tables and real pagination. Backend: add offset + total-count to the scans/events list endpoints so history pages through the full set instead of a capped window. Extract shared event/scan WHERE-clause builders so list and count filter identically, and add CountEvents / CountAutoscanScans. Frontend: add a reusable TablePagination component (rows-per-page, "showing X-Y of Z", numbered window with ellipses, responsive) and reuse it for the server-paginated history (scans + polls) and the client-paginated live queue. Unify all three tables behind one DataTable shell so they read as one family. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
2394 lines
101 KiB
Go
2394 lines
101 KiB
Go
// Package api provides the HTTP router and middleware setup for Silo.
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/access"
|
|
"github.com/Silo-Server/silo-server/internal/activitylog"
|
|
"github.com/Silo-Server/silo-server/internal/adminjob"
|
|
"github.com/Silo-Server/silo-server/internal/api/handlers"
|
|
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/cache"
|
|
"github.com/Silo-Server/silo-server/internal/catalog"
|
|
"github.com/Silo-Server/silo-server/internal/catalogseed"
|
|
"github.com/Silo-Server/silo-server/internal/clientip"
|
|
"github.com/Silo-Server/silo-server/internal/config"
|
|
"github.com/Silo-Server/silo-server/internal/download"
|
|
evt "github.com/Silo-Server/silo-server/internal/events"
|
|
"github.com/Silo-Server/silo-server/internal/historyimport"
|
|
"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/markers"
|
|
"github.com/Silo-Server/silo-server/internal/mdblist"
|
|
"github.com/Silo-Server/silo-server/internal/metadata"
|
|
"github.com/Silo-Server/silo-server/internal/metadata/tmdb"
|
|
metatrakt "github.com/Silo-Server/silo-server/internal/metadata/trakt"
|
|
"github.com/Silo-Server/silo-server/internal/nodepool"
|
|
"github.com/Silo-Server/silo-server/internal/notifications"
|
|
"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/ratelimit"
|
|
"github.com/Silo-Server/silo-server/internal/recommendations"
|
|
mediarequests "github.com/Silo-Server/silo-server/internal/requests"
|
|
"github.com/Silo-Server/silo-server/internal/requests/radarr"
|
|
"github.com/Silo-Server/silo-server/internal/requests/sonarr"
|
|
"github.com/Silo-Server/silo-server/internal/s3client"
|
|
"github.com/Silo-Server/silo-server/internal/scanner"
|
|
"github.com/Silo-Server/silo-server/internal/scanqueue"
|
|
"github.com/Silo-Server/silo-server/internal/sections"
|
|
"github.com/Silo-Server/silo-server/internal/subtitles"
|
|
subtitleai "github.com/Silo-Server/silo-server/internal/subtitles/ai"
|
|
"github.com/Silo-Server/silo-server/internal/subtitles/opensubtitles"
|
|
"github.com/Silo-Server/silo-server/internal/subtitles/subdl"
|
|
"github.com/Silo-Server/silo-server/internal/subtitles/subsource"
|
|
"github.com/Silo-Server/silo-server/internal/taskmanager"
|
|
"github.com/Silo-Server/silo-server/internal/taskmanager/repository"
|
|
"github.com/Silo-Server/silo-server/internal/usercollections"
|
|
"github.com/Silo-Server/silo-server/internal/userstore"
|
|
"github.com/Silo-Server/silo-server/internal/watchstate"
|
|
watchtrakt "github.com/Silo-Server/silo-server/internal/watchsync/providers/trakt"
|
|
"github.com/Silo-Server/silo-server/internal/watchtogether"
|
|
"github.com/Silo-Server/silo-server/internal/webhooksync"
|
|
)
|
|
|
|
// Dependencies holds all shared dependencies that handlers need.
|
|
type Dependencies struct {
|
|
Config *config.Config
|
|
BootstrapSensitiveConfigured map[string]bool
|
|
BootstrapSensitiveValues map[string]string
|
|
AppContext context.Context
|
|
DB *pgxpool.Pool
|
|
FrontendFS fs.FS
|
|
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)
|
|
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)
|
|
LibraryIngester *libraryingest.Executor // shared library ingest executor (may be nil)
|
|
ProbeEnsurer handlers.PlaybackProbeEnsurer // on-demand probe repair for playback/detail (may be nil)
|
|
UserStoreProvider userstore.UserStoreProvider // user store provider (may be nil)
|
|
SessionMgr *playback.SessionManager // playback session manager (may be nil)
|
|
SkippedRootRepo *metadata.SkippedRootRepository // skipped root repository (may be nil)
|
|
StaleIDRepo *metadata.StaleMediaIDRepository // stale media ID repository (may be nil)
|
|
Refresher handlers.AdminMetadataRefresher // metadata refresher (may be nil)
|
|
NodeRepo *nodepool.Repository // stream node repository (may be nil)
|
|
ProxyPool *nodepool.ProxyPool // proxy node pool (may be nil)
|
|
TranscodePool *nodepool.TranscodePool // transcode node pool (may be nil)
|
|
SessionSyncer handlers.PlaybackSessionSyncer // optional; immediate playback session sync trigger
|
|
EventBus cache.EventBus
|
|
AdminStatsProvider handlers.AdminStatsSource
|
|
Recommender recommendations.Recommender // nil when disabled
|
|
RecWorker *recommendations.Worker // nil when disabled
|
|
RatingsRepo *catalog.RatingsRepo
|
|
PersonRepo *catalog.PersonRepository
|
|
PersonRefreshQueue handlers.PersonRefreshQueue
|
|
PersonRefresher handlers.PersonRefresher
|
|
RateLimitMW *ratelimit.Middleware
|
|
ClientIPResolver *clientip.Resolver
|
|
NodeID string
|
|
LogStreamHub *logstream.Hub
|
|
RealtimeHub *notifications.Hub
|
|
EventsHub *evt.Hub
|
|
ScanRegistry *evt.ScanRegistry
|
|
LibraryScanQueue *scanqueue.Service
|
|
ActivityLogWriter activitylog.Writer
|
|
ActivityLogRepo *activitylog.Repo
|
|
OpsLogRepo *opslog.Repo
|
|
FFmpegLogSink playback.FFmpegLogSink
|
|
RedisClient *redis.Client // for session listing (may be nil)
|
|
TaskManager *taskmanager.TaskManager // task manager (may be nil)
|
|
IntroRepository *intromarkers.Repository
|
|
IntroAnalyzer *intromarkers.Analyzer
|
|
MarkerRegistry *markers.Registry
|
|
MarkerResolver markers.ExternalIDResolver
|
|
WatchProviderService handlers.WatchProviderService
|
|
PluginService *plugins.Service
|
|
PluginHTTPProxy *plugins.HTTPProxy
|
|
PluginUserConfig *plugins.UserConfigStore
|
|
AuthProviders []auth.RegisteredProvider
|
|
// PublicURL is the externally-reachable origin (scheme + host) for this
|
|
// silo instance. Used to build redirect_uri values handed to OAuth
|
|
// IdPs. Empty disables the /oauth/{install_id}/{init,callback} routes.
|
|
PublicURL string
|
|
ImageResolver catalog.ImageResolver // plugin-based image URL resolver (may be nil)
|
|
PluginImageResolver *metadata.PluginImageResolver // concrete resolver for runtime source registration (may be nil)
|
|
MetadataService handlers.MatchMetadataService // metadata search+process (may be nil)
|
|
CollectionService *catalog.LibraryCollectionService // collection service (may be nil)
|
|
ChapterThumbnailQueuer catalog.ChapterThumbnailQueuer
|
|
PlaybackRealtimeHub *playback.RealtimeHub
|
|
OnUserSessionsRevoked func(ctx context.Context, userID int)
|
|
OnServerSettingUpdated func(ctx context.Context, key, value string)
|
|
|
|
// UserCollectionSync handles per-profile imported collections (TMDB /
|
|
// Trakt / MDBList) — the user-facing analogue of CollectionService.
|
|
UserCollectionSync *usercollections.Service
|
|
UserCollectionScheduler *usercollections.Scheduler
|
|
|
|
// TrendingRefresher refreshes the persisted trending_discover snapshots.
|
|
// Built in main.go with TMDB wired; its Trakt fetcher is propagated here in
|
|
// router.go once the Trakt adapter exists (mirrors UserCollectionSync).
|
|
TrendingRefresher *sections.TrendingRefresher
|
|
|
|
// MDBListClient is used by user-facing list discovery endpoints
|
|
// (search/top). May be nil; the handlers report "not configured" in
|
|
// that case rather than failing.
|
|
MDBListClient *mdblist.Client
|
|
}
|
|
|
|
// NewRouter creates a chi.Router with all middleware and routes mounted
|
|
// under /api/v1/.
|
|
func NewRouter(deps Dependencies) chi.Router {
|
|
r := chi.NewRouter()
|
|
|
|
// Standard middleware.
|
|
r.Use(middleware.RequestID)
|
|
|
|
// Client IP resolution must run before request logging.
|
|
if deps.ClientIPResolver != nil {
|
|
r.Use(clientip.Middleware(deps.ClientIPResolver))
|
|
}
|
|
|
|
r.Use(apimw.RequestLogger(deps.NodeID))
|
|
r.Use(middleware.Recoverer)
|
|
r.Use(apimw.Metrics)
|
|
|
|
// Activity logging (before auth — captures all requests including failed auth).
|
|
if deps.ActivityLogWriter != nil {
|
|
r.Use(activitylog.NewMiddleware(deps.ActivityLogWriter, deps.NodeID))
|
|
}
|
|
|
|
// Build the readiness handler with optional S3 check.
|
|
var s3Checker handlers.S3HealthChecker
|
|
if deps.S3Public != nil {
|
|
s3Checker = deps.S3Public
|
|
} else if deps.S3Private != nil {
|
|
s3Checker = deps.S3Private
|
|
}
|
|
|
|
// PG pinger: use the pool if available.
|
|
var pgPinger handlers.PGPinger
|
|
if deps.DB != nil {
|
|
pgPinger = deps.DB
|
|
}
|
|
|
|
readyHandler := handlers.NewReadyHandler(pgPinger, s3Checker)
|
|
|
|
// 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.
|
|
var healthServerName, healthServerID string
|
|
if deps.Config != nil {
|
|
healthServerName = deps.Config.JellyfinCompat.ServerName
|
|
healthServerID = deps.Config.JellyfinCompat.ServerID
|
|
}
|
|
healthHandler := handlers.NewHealthHandler(healthServerName, healthServerID)
|
|
|
|
// Build server settings repo if DB is available (needed by auth and admin).
|
|
var settingsRepo *catalog.ServerSettingsRepo
|
|
if deps.DB != nil {
|
|
settingsRepo = catalog.NewServerSettingsRepo(deps.DB)
|
|
}
|
|
|
|
// Build auth handler and auth middleware if DB and config are available.
|
|
var userRepo *auth.UserRepository
|
|
var inviteCodeRepo *auth.InviteCodeRepository
|
|
var apiKeyRepo *auth.APIKeyRepository
|
|
var authService *auth.Service
|
|
var authHandler *handlers.AuthHandler
|
|
var authMiddleware *apimw.AuthMiddleware
|
|
var viewerAccessMiddleware *apimw.ViewerAccessMiddleware
|
|
var permissionMiddleware *apimw.PermissionMiddleware
|
|
var viewerResolver *access.Resolver
|
|
var profileTokenService *access.ProfileTokenService
|
|
var jwtService *auth.JWTService
|
|
var sessionRepo *auth.SessionRepository
|
|
var deviceLoginService *auth.DeviceLoginService
|
|
if deps.DB != nil && deps.Config != nil {
|
|
userRepo = auth.NewUserRepository(deps.DB)
|
|
sessionRepo = auth.NewSessionRepository(deps.DB)
|
|
inviteCodeRepo = auth.NewInviteCodeRepository(deps.DB)
|
|
apiKeyRepo = auth.NewAPIKeyRepository(deps.DB)
|
|
jwtService = auth.NewJWTService(
|
|
deps.Config.Auth.JWTSecret,
|
|
deps.Config.Auth.AccessTokenExpiry,
|
|
deps.Config.Auth.RefreshTokenExpiry,
|
|
)
|
|
provider := auth.NewLocalProvider(userRepo, sessionRepo)
|
|
authService = auth.NewService(
|
|
provider,
|
|
jwtService,
|
|
sessionRepo,
|
|
userRepo,
|
|
inviteCodeRepo,
|
|
settingsRepo,
|
|
deps.UserStoreProvider,
|
|
)
|
|
for _, registration := range deps.AuthProviders {
|
|
authService.RegisterProvider(registration.Info, registration.Provider)
|
|
}
|
|
deviceLoginService = auth.NewDeviceLoginService(deps.DB, userRepo, jwtService, sessionRepo)
|
|
authHandler = handlers.NewAuthHandler(authService, jwtService, deviceLoginService)
|
|
authMiddleware = apimw.NewAuthMiddleware(jwtService, sessionRepo, apiKeyRepo, userRepo)
|
|
profileTokenService = access.NewProfileTokenService(deps.Config.Auth.JWTSecret, 0)
|
|
if deps.UserStoreProvider != nil {
|
|
viewerResolver = access.NewResolver(userRepo, deps.UserStoreProvider, profileTokenService)
|
|
viewerAccessMiddleware = apimw.NewViewerAccessMiddleware(viewerResolver)
|
|
}
|
|
if deps.DB != nil {
|
|
permissionMiddleware = apimw.NewPermissionMiddleware(
|
|
userRepo,
|
|
apimw.NewPGMetadataTargetLibraryResolver(deps.DB),
|
|
)
|
|
}
|
|
}
|
|
if deps.SessionMgr != nil && userRepo != nil {
|
|
deps.SessionMgr.SetLimitProvider(func(ctx context.Context, userID int) (playback.SessionLimits, error) {
|
|
user, err := userRepo.GetByID(ctx, userID)
|
|
if err != nil {
|
|
return playback.SessionLimits{}, err
|
|
}
|
|
return playback.SessionLimits{
|
|
MaxStreams: user.MaxStreams,
|
|
MaxTranscodes: user.MaxTranscodes,
|
|
}, nil
|
|
})
|
|
}
|
|
|
|
// Build demo guard middleware if server settings are available.
|
|
var demoGuard *apimw.DemoGuard
|
|
if settingsRepo != nil {
|
|
demoGuard = apimw.NewDemoGuard(settingsRepo)
|
|
}
|
|
|
|
// Build library handler if folder repo is available.
|
|
var libraryHandler *handlers.LibraryHandler
|
|
if deps.FolderRepo != nil {
|
|
libraryHandler = handlers.NewLibraryHandler(deps.FolderRepo, deps.LibraryIngester, userRepo, deps.DB, deps.Refresher, deps.AppContext)
|
|
libraryHandler.EventBus = deps.EventBus
|
|
libraryHandler.EventsHub = deps.EventsHub
|
|
libraryHandler.ScanRegistry = deps.ScanRegistry
|
|
libraryHandler.ScanQueue = deps.LibraryScanQueue
|
|
if deps.DB != nil {
|
|
libraryHandler.JobRepo = adminjob.NewRepository(deps.DB)
|
|
}
|
|
|
|
// Library poster uploads are writable client-facing assets, so they
|
|
// belong in the public assets bucket.
|
|
if deps.S3Public != nil {
|
|
libraryHandler.S3Meta = deps.S3Public
|
|
}
|
|
|
|
// Wire provider chain repos for per-library provider priority management.
|
|
if deps.DB != nil && deps.PluginService != nil {
|
|
libraryHandler.ChainRepo = metadata.NewChainRepository(deps.DB)
|
|
libraryHandler.PluginInstallations = plugins.NewInstallationStore(deps.DB)
|
|
}
|
|
if invalidator, ok := deps.MetadataService.(interface{ InvalidateChainCache() }); ok {
|
|
libraryHandler.SetChainCacheInvalidator(invalidator)
|
|
}
|
|
if deps.SkippedRootRepo != nil {
|
|
libraryHandler.SkippedRootRepo = deps.SkippedRootRepo
|
|
}
|
|
if deps.StaleIDRepo != nil {
|
|
libraryHandler.StaleIDRepo = deps.StaleIDRepo
|
|
}
|
|
if deps.DB != nil {
|
|
libraryHandler.SectionRepo = sections.NewRepository(deps.DB)
|
|
}
|
|
if deps.UserStoreProvider != nil {
|
|
libraryHandler.StoreProvider = deps.UserStoreProvider
|
|
}
|
|
}
|
|
|
|
// Build ratings repo if DB is available. Use dep-injected repo when provided
|
|
// (e.g. already constructed in main.go for the recommendations engine).
|
|
var ratingsRepo *catalog.RatingsRepo
|
|
if deps.RatingsRepo != nil {
|
|
ratingsRepo = deps.RatingsRepo
|
|
} else if deps.DB != nil {
|
|
ratingsRepo = catalog.NewRatingsRepo(deps.DB)
|
|
}
|
|
|
|
// Build browse/search/items handlers if DB is available.
|
|
var itemsHandler *handlers.ItemsHandler
|
|
var catalogResourceHandler *handlers.CatalogResourceHandler
|
|
var catalogHandler *handlers.CatalogHandler
|
|
var peopleHandler *handlers.PeopleHandler
|
|
var itemRepo *catalog.ItemRepository
|
|
var episodeRepo *catalog.EpisodeRepository
|
|
var providerIDRepo *catalog.ProviderIDRepository
|
|
var seasonRepo *catalog.SeasonRepository
|
|
var detailSvc *catalog.DetailService
|
|
var calendarRepo *catalog.CalendarRepository
|
|
var webhookSyncHandler *handlers.WebhookSyncHandler
|
|
var requestHandler *handlers.RequestsHandler
|
|
var autoscanHandler *handlers.AutoscanHandler
|
|
if deps.DB != nil {
|
|
browseRepo := catalog.NewBrowseRepository(deps.DB)
|
|
itemRepo = catalog.NewItemRepository(deps.DB)
|
|
episodeRepo = catalog.NewEpisodeRepository(deps.DB)
|
|
providerIDRepo = catalog.NewProviderIDRepository(deps.DB)
|
|
calendarRepo = catalog.NewCalendarRepository(deps.DB)
|
|
|
|
var fileFetcher catalog.FileVersionFetcher
|
|
if deps.FileRepo != nil {
|
|
fileFetcher = deps.FileRepo
|
|
}
|
|
|
|
seasonRepo = catalog.NewSeasonRepository(deps.DB)
|
|
folderRepo := catalog.NewFolderRepository(deps.DB)
|
|
|
|
var episodeFileProvider handlers.EpisodeFileProvider
|
|
if deps.FileRepo != nil {
|
|
episodeFileProvider = deps.FileRepo
|
|
}
|
|
|
|
rootClaimRepo := catalog.NewRootClaimRepository(deps.DB)
|
|
groupClaimRepo := catalog.NewGroupClaimRepository(deps.DB)
|
|
detailSvc = catalog.NewDetailService(itemRepo, episodeRepo, seasonRepo, deps.PersonRepo, fileFetcher)
|
|
detailSvc.SetFolderRepository(folderRepo)
|
|
detailSvc.SetRootClaimRepository(rootClaimRepo)
|
|
detailSvc.SetGroupClaimRepository(groupClaimRepo)
|
|
detailSvc.SetProbeEnsurer(deps.ProbeEnsurer)
|
|
detailSvc.SetChapterThumbnailQueuer(deps.ChapterThumbnailQueuer)
|
|
if deps.ImageResolver != nil {
|
|
detailSvc.SetImageResolver(deps.ImageResolver)
|
|
}
|
|
detailSvc.SetUserStoreProvider(deps.UserStoreProvider)
|
|
itemsHandler = handlers.NewItemsHandler(
|
|
browseRepo,
|
|
itemRepo,
|
|
episodeRepo,
|
|
seasonRepo,
|
|
ratingsRepo,
|
|
episodeFileProvider,
|
|
deps.UserStoreProvider,
|
|
detailSvc,
|
|
providerIDRepo,
|
|
)
|
|
itemsHandler.EventsHub = deps.EventsHub
|
|
itemsHandler.UserRepo = userRepo
|
|
if requester, ok := deps.MetadataService.(handlers.MetadataRefreshRequester); ok {
|
|
itemsHandler.SetMetadataRefreshRequester(requester)
|
|
}
|
|
if dispatcher, ok := deps.WatchProviderService.(handlers.LocalWatchEventDispatcher); ok {
|
|
itemsHandler.SetLocalWatchEventDispatcher(dispatcher)
|
|
}
|
|
catalogResourceHandler = handlers.NewCatalogResourceHandler(itemsHandler)
|
|
catalogHandler = handlers.NewCatalogHandler(
|
|
catalog.NewCatalogResolver(browseRepo, itemRepo).
|
|
WithEpisodeRepository(episodeRepo).
|
|
WithUserStoreProvider(deps.UserStoreProvider),
|
|
itemsHandler,
|
|
)
|
|
|
|
tmdbAPIKey := ""
|
|
if deps.Config != nil {
|
|
tmdbAPIKey = deps.Config.TMDBAPIKey
|
|
}
|
|
requestsRepo := mediarequests.NewRepository(deps.DB)
|
|
requestSvc := mediarequests.NewService(
|
|
requestsRepo,
|
|
tmdb.NewClient(tmdbAPIKey, 40),
|
|
mediarequests.NewCatalogPresence(itemRepo, providerIDRepo),
|
|
)
|
|
requestSvc.SetSecretResolver(settingsRepo)
|
|
requestSvc.SetFulfillmentAdapters(radarr.NewClient(nil), sonarr.NewClient(nil))
|
|
if viewerResolver != nil {
|
|
requestSvc.SetEntitlementResolver(mediarequests.NewAccessEntitlements(viewerResolver))
|
|
}
|
|
requestHandler = handlers.NewRequestsHandler(requestSvc)
|
|
|
|
autoscanRepo := autoscan.NewRepository(deps.DB)
|
|
if deps.FolderRepo != nil && deps.LibraryScanQueue != nil && deps.PluginService != nil {
|
|
autoscanSvc := BuildAutoscanService(
|
|
autoscanRepo,
|
|
deps.PluginService,
|
|
plugins.NewInstallationStore(deps.DB),
|
|
requestsRepo,
|
|
settingsRepo,
|
|
deps.FolderRepo,
|
|
deps.LibraryScanQueue,
|
|
deps.RedisClient,
|
|
)
|
|
autoscanHandler = handlers.NewAutoscanHandler(autoscanRepo, autoscanSvc)
|
|
// Wire the optional poll-task rescheduler so a settings change
|
|
// re-applies the poll interval without a restart.
|
|
if deps.TaskManager != nil {
|
|
autoscanHandler.SetTriggerUpdater(deps.TaskManager)
|
|
}
|
|
}
|
|
|
|
if deps.PersonRepo != nil {
|
|
peopleHandler = handlers.NewPeopleHandler(deps.PersonRepo, browseRepo, itemRepo, detailSvc)
|
|
peopleHandler.SetItemsHandler(itemsHandler)
|
|
peopleHandler.SetRefreshQueue(deps.PersonRefreshQueue)
|
|
peopleHandler.SetRefreshService(deps.PersonRefresher)
|
|
}
|
|
}
|
|
|
|
// Build profile/personal data handlers if UserStoreProvider is available.
|
|
var profileHandler *handlers.ProfileHandler
|
|
var personalDataHandler *handlers.PersonalDataHandler
|
|
var progressHandler *handlers.ProgressHandler
|
|
var collectionHandler *handlers.CollectionHandler
|
|
var settingsHandler *handlers.SettingsHandler
|
|
var homeDismissalHandler *handlers.HomeDismissalHandler
|
|
var subtitlePrefHandler *handlers.SubtitlePrefHandler
|
|
var audioPrefHandler *handlers.AudioPrefHandler
|
|
var libraryPlaybackPrefHandler *handlers.LibraryPlaybackPrefHandler
|
|
var watchProviderHandler *handlers.WatchProviderHandler
|
|
var playbackSessionsLoader *handlers.PlaybackSessionsLoader
|
|
if deps.DB != nil {
|
|
playbackSessionsLoader = handlers.NewPlaybackSessionsLoader(deps.DB, deps.UserStoreProvider, detailSvc)
|
|
}
|
|
|
|
if deps.UserStoreProvider != nil {
|
|
profileHandler = handlers.NewProfileHandler(deps.UserStoreProvider)
|
|
profileHandler.UserRepo = userRepo
|
|
profileHandler.ProfileTokens = profileTokenService
|
|
profileHandler.AvatarStore = deps.S3Private
|
|
profileHandler.SessionsReader = playbackSessionsLoader
|
|
personalDataHandler = handlers.NewPersonalDataHandler(deps.UserStoreProvider, itemRepo)
|
|
if detailSvc != nil {
|
|
personalDataHandler.SetDetailService(detailSvc)
|
|
}
|
|
personalDataHandler.SetEpisodeRepo(episodeRepo)
|
|
personalDataHandler.SetSeasonRepo(seasonRepo)
|
|
personalDataHandler.EventsHub = deps.EventsHub
|
|
if dispatcher, ok := deps.WatchProviderService.(handlers.LocalFavoriteEventDispatcher); ok {
|
|
personalDataHandler.SetLocalFavoriteEventDispatcher(dispatcher)
|
|
}
|
|
progressHandler = handlers.NewProgressHandler(deps.UserStoreProvider)
|
|
progressHandler.EventsHub = deps.EventsHub
|
|
if settingsRepo != nil {
|
|
progressHandler.SettingsRepo = settingsRepo
|
|
}
|
|
if deps.DB != nil {
|
|
progressHandler.LibraryLookup = catalog.NewLibraryItemRepository(deps.DB)
|
|
}
|
|
collectionHandler = handlers.NewCollectionHandler(deps.UserStoreProvider)
|
|
if deps.DB != nil {
|
|
collectionHandler.Executor = &catalog.QueryExecutor{Pool: deps.DB}
|
|
}
|
|
if deps.S3Public != nil {
|
|
collectionHandler.S3GP = deps.S3Public
|
|
collectionHandler.PresignTTL = 4 * time.Hour
|
|
}
|
|
settingsHandler = handlers.NewSettingsHandler(deps.UserStoreProvider)
|
|
if settingsRepo != nil {
|
|
settingsHandler.SetServerSettings(settingsRepo)
|
|
}
|
|
homeDismissalHandler = handlers.NewHomeDismissalHandler(deps.UserStoreProvider)
|
|
subtitlePrefHandler = handlers.NewSubtitlePrefHandler(deps.UserStoreProvider)
|
|
audioPrefHandler = handlers.NewAudioPrefHandler(deps.UserStoreProvider)
|
|
libraryPlaybackPrefHandler = handlers.NewLibraryPlaybackPrefHandler(deps.UserStoreProvider)
|
|
if deps.FolderRepo != nil {
|
|
libraryPlaybackPrefHandler.SetLibraryLookup(deps.FolderRepo)
|
|
} else if deps.DB != nil {
|
|
libraryPlaybackPrefHandler.SetLibraryLookup(catalog.NewFolderRepository(deps.DB))
|
|
}
|
|
}
|
|
if deps.WatchProviderService != nil {
|
|
watchProviderHandler = handlers.NewWatchProviderHandler(deps.WatchProviderService)
|
|
}
|
|
|
|
// Build ratings handler if both repo and itemRepo are available.
|
|
var ratingsHandler *handlers.RatingsHandler
|
|
var recsRepoForStale *recommendations.Repo
|
|
if ratingsRepo != nil && itemRepo != nil {
|
|
ratingsHandler = handlers.NewRatingsHandler(ratingsRepo, itemRepo)
|
|
if deps.DB != nil {
|
|
recsRepoForStale = recommendations.NewRepo(deps.DB)
|
|
ratingsHandler.SetProfileStaler(recsRepoForStale)
|
|
ratingsHandler.SetProfileRefreshRequester(deps.RecWorker)
|
|
if personalDataHandler != nil {
|
|
personalDataHandler.SetProfileStaler(recsRepoForStale)
|
|
personalDataHandler.SetProfileRefreshRequester(deps.RecWorker)
|
|
}
|
|
if progressHandler != nil {
|
|
progressHandler.SetProfileStaler(recsRepoForStale)
|
|
progressHandler.SetProfileRefreshRequester(deps.RecWorker)
|
|
}
|
|
if itemsHandler != nil {
|
|
itemsHandler.SetProfileStaler(recsRepoForStale)
|
|
itemsHandler.SetProfileRefreshRequester(deps.RecWorker)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Create subtitleRepo early — only needs DB, shared with playback handler and subtitle search handler.
|
|
var subtitleRepo *subtitles.PgRepository
|
|
if deps.DB != nil {
|
|
subtitleRepo = subtitles.NewPgRepository(deps.DB)
|
|
}
|
|
|
|
// Notifier that pushes "subtitle ready" events to active sessions when an AI
|
|
// translation completes. Assigned inside the playback handler block where the
|
|
// realtime hub and session manager are in scope; nil when playback is off.
|
|
var subtitleAINotifier *playback.SubtitleReadyNotifier
|
|
|
|
// Build playback handler if session manager is available.
|
|
var playbackHandler *handlers.PlaybackHandler
|
|
var adminPlaybackControlHandler *handlers.AdminPlaybackControlHandler
|
|
var streamHandler *handlers.StreamHandler
|
|
var watchTogetherHandler *handlers.WatchTogetherHandler
|
|
if deps.SessionMgr != nil {
|
|
var playbackAdminStore handlers.PlaybackAdminStore
|
|
if deps.DB != nil {
|
|
playbackAdminStore = handlers.NewPGPlaybackAdminStore(deps.DB, deps.EventsHub)
|
|
}
|
|
if deps.FileRepo != nil {
|
|
playbackHandler = handlers.NewPlaybackHandler(deps.SessionMgr, deps.FileRepo)
|
|
streamHandler = handlers.NewStreamHandler(deps.SessionMgr, deps.FileRepo)
|
|
} else {
|
|
playbackHandler = handlers.NewPlaybackHandler(deps.SessionMgr)
|
|
}
|
|
|
|
// Wire UserStoreProvider for progress/history persistence.
|
|
if deps.UserStoreProvider != nil {
|
|
playbackHandler.StoreProvider = deps.UserStoreProvider
|
|
}
|
|
playbackHandler.StableIdentityResolver = watchstate.NewStableIdentityResolver(itemRepo, episodeRepo, providerIDRepo)
|
|
if scrobbler, ok := deps.WatchProviderService.(handlers.PlaybackWatchScrobbler); ok {
|
|
playbackHandler.WatchScrobbler = scrobbler
|
|
}
|
|
playbackHandler.AdminStore = playbackAdminStore
|
|
playbackHandler.EventsHub = deps.EventsHub
|
|
if deps.FileRepo != nil {
|
|
playbackHandler.MissingMarker = deps.FileRepo
|
|
}
|
|
if deps.SessionSyncer != nil {
|
|
playbackHandler.SessionSyncer = deps.SessionSyncer
|
|
}
|
|
if streamHandler != nil {
|
|
streamHandler.AdminStore = playbackAdminStore
|
|
streamHandler.EventsHub = deps.EventsHub
|
|
streamHandler.SessionSyncer = deps.SessionSyncer
|
|
if deps.FileRepo != nil {
|
|
streamHandler.MissingMarker = deps.FileRepo
|
|
}
|
|
}
|
|
|
|
// Wire optional proxy/transcode pools and JWT secret for node-aware stream URLs.
|
|
if deps.ProxyPool != nil {
|
|
playbackHandler.ProxyPool = deps.ProxyPool
|
|
}
|
|
if deps.TranscodePool != nil {
|
|
playbackHandler.TranscodePool = deps.TranscodePool
|
|
}
|
|
if deps.Config != nil && deps.Config.Auth.JWTSecret != "" {
|
|
playbackHandler.JWTSecret = deps.Config.Auth.JWTSecret
|
|
}
|
|
if deps.Config != nil {
|
|
playbackHandler.FFmpegPath = deps.Config.Playback.FFmpegPath
|
|
playbackHandler.HWAccel = deps.Config.Playback.HWAccel
|
|
playbackHandler.TranscodeDir = deps.Config.Playback.TranscodeDir
|
|
if cleaned, err := playbackHandler.CleanupOrphanedTranscodes(); err != nil {
|
|
slog.Warn("playback transcode cleanup failed", "dir", playbackHandler.TranscodeDir, "error", err)
|
|
} else if cleaned > 0 {
|
|
slog.Info("playback transcode cleanup removed orphaned dirs", "dir", playbackHandler.TranscodeDir, "count", cleaned)
|
|
}
|
|
}
|
|
playbackHandler.ProbeEnsurer = deps.ProbeEnsurer
|
|
playbackHandler.ChapterThumbnailQueuer = deps.ChapterThumbnailQueuer
|
|
if settingsRepo != nil {
|
|
playbackHandler.SettingsRepo = settingsRepo
|
|
}
|
|
if deps.FileRepo != nil {
|
|
playbackHandler.FileVersionFetcher = deps.FileRepo
|
|
}
|
|
if subtitleRepo != nil {
|
|
playbackHandler.SubtitleRepo = subtitleRepo
|
|
}
|
|
if recsRepoForStale != nil {
|
|
playbackHandler.SetProfileStaler(recsRepoForStale)
|
|
playbackHandler.SetProfileRefreshRequester(deps.RecWorker)
|
|
}
|
|
|
|
realtimeHub := deps.PlaybackRealtimeHub
|
|
if realtimeHub == nil {
|
|
realtimeHub = playback.NewRealtimeHub()
|
|
}
|
|
commandTracker := playback.NewCommandTracker()
|
|
playbackHandler.RealtimeHub = realtimeHub
|
|
playbackHandler.CommandTracker = commandTracker
|
|
playbackHandler.CommandDispatcher = playback.NewCommandDispatcher(deps.SessionMgr, realtimeHub, commandTracker)
|
|
playbackHandler.IntroAnalyzer = deps.IntroAnalyzer
|
|
playbackHandler.IntroRepository = deps.IntroRepository
|
|
playbackHandler.MarkerRegistry = deps.MarkerRegistry
|
|
playbackHandler.MarkerResolver = deps.MarkerResolver
|
|
if deps.FileRepo != nil {
|
|
playbackHandler.MarkerUpserter = deps.FileRepo
|
|
}
|
|
playbackHandler.MarkerUpdateNotifier = playback.NewMarkerUpdateNotifier(deps.SessionMgr, realtimeHub)
|
|
subtitleAINotifier = playback.NewSubtitleReadyNotifier(deps.SessionMgr, realtimeHub)
|
|
adminPlaybackControlHandler = handlers.NewAdminPlaybackControlHandler(playbackHandler)
|
|
|
|
if deps.DB != nil && deps.FileRepo != nil && viewerResolver != nil && deps.Config != nil && detailSvc != nil {
|
|
roomTokenService := watchtogether.NewRoomTokenService(deps.Config.Auth.JWTSecret, 24*time.Hour)
|
|
watchTogetherHandler = handlers.NewWatchTogetherHandler(
|
|
watchtogether.NewService(
|
|
watchtogether.NewRepository(deps.DB),
|
|
deps.SessionMgr,
|
|
deps.FileRepo,
|
|
playbackHandler.CommandDispatcher,
|
|
watchtogether.NewCatalogSelectionResolver(detailSvc),
|
|
watchtogether.NewSuggestionRepository(deps.DB),
|
|
),
|
|
viewerResolver,
|
|
roomTokenService,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Wire subtitle repo and S3 client onto streamHandler for S3-stored subtitle serving.
|
|
if streamHandler != nil && subtitleRepo != nil && deps.S3Public != nil {
|
|
streamHandler.SubtitleRepo = subtitleRepo
|
|
streamHandler.S3Client = deps.S3Public
|
|
streamHandler.S3Bucket = deps.S3Public.Bucket()
|
|
}
|
|
if streamHandler != nil && deps.Config != nil {
|
|
streamHandler.FFmpegPath = deps.Config.Playback.FFmpegPath
|
|
}
|
|
|
|
// Build admin handler if we have a user repo.
|
|
var adminHandler *handlers.AdminHandler
|
|
var catalogSeedHandler *handlers.CatalogSeedHandler
|
|
var adminJobsHandler *handlers.AdminJobsHandler
|
|
if userRepo != nil {
|
|
adminHandler = handlers.NewAdminHandler(userRepo, deps.DB, deps.UserStoreProvider)
|
|
adminHandler.SessionsLoader = playbackSessionsLoader
|
|
adminHandler.DetailSvc = detailSvc
|
|
adminHandler.EventBus = deps.EventBus
|
|
adminHandler.ImpersonationService = authService
|
|
adminHandler.StatsSource = deps.AdminStatsProvider
|
|
adminHandler.RealtimeHub = deps.RealtimeHub
|
|
adminHandler.BootstrapSensitiveConfigured = deps.BootstrapSensitiveConfigured
|
|
adminHandler.BootstrapSensitiveValues = deps.BootstrapSensitiveValues
|
|
if settingsRepo != nil {
|
|
adminHandler.SettingsRepo = settingsRepo
|
|
}
|
|
if deps.OnUserSessionsRevoked != nil {
|
|
adminHandler.OnUserSessionsRevoked = deps.OnUserSessionsRevoked
|
|
}
|
|
if deps.OnServerSettingUpdated != nil {
|
|
adminHandler.OnServerSettingUpdated = deps.OnServerSettingUpdated
|
|
}
|
|
}
|
|
if deps.DB != nil {
|
|
jobRepo := adminjob.NewRepository(deps.DB)
|
|
// Avoid wrapping a nil *s3client.Client in a non-nil interface;
|
|
// handlers rely on interface-nil checks to gate S3 features.
|
|
var privateStore handlers.CatalogSeedArtifactStore
|
|
if deps.S3Private != nil {
|
|
privateStore = deps.S3Private
|
|
}
|
|
catalogSeedHandler = handlers.NewCatalogSeedHandler(catalogseed.NewService(deps.DB, deps.PersonRepo, recommendations.NewRepo(deps.DB)), jobRepo, privateStore)
|
|
catalogSeedHandler.RealtimeHub = deps.RealtimeHub
|
|
adminJobsHandler = handlers.NewAdminJobsHandler(jobRepo, privateStore)
|
|
if adminHandler != nil && deps.FolderRepo != nil && deps.FileRepo != nil && itemRepo != nil && episodeRepo != nil {
|
|
adminHandler.JobRepo = jobRepo
|
|
adminHandler.ItemRefreshResolver = adminjob.NewItemRefreshResolver(
|
|
itemRepo,
|
|
catalog.NewSeasonRepository(deps.DB),
|
|
episodeRepo,
|
|
deps.FolderRepo,
|
|
deps.FileRepo,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Build admin match handler if metadata service and item repo are available.
|
|
var adminMatchHandler *handlers.AdminMatchHandler
|
|
if deps.MetadataService != nil && itemRepo != nil && deps.DB != nil {
|
|
adminMatchHandler = handlers.NewAdminMatchHandler(
|
|
itemRepo,
|
|
&handlers.PoolFolderLookup{Pool: deps.DB},
|
|
deps.MetadataService,
|
|
)
|
|
}
|
|
|
|
// Build admin image handler for poster/backdrop/logo selection.
|
|
var adminImageHandler *handlers.AdminImageHandler
|
|
if imageSvc, ok := deps.MetadataService.(handlers.ImageService); ok && itemRepo != nil && seasonRepo != nil && episodeRepo != nil && deps.DB != nil && detailSvc != nil {
|
|
adminImageHandler = handlers.NewAdminImageHandler(
|
|
itemRepo,
|
|
seasonRepo,
|
|
episodeRepo,
|
|
&handlers.PoolFolderLookup{Pool: deps.DB},
|
|
imageSvc,
|
|
deps.PluginImageResolver,
|
|
detailSvc,
|
|
)
|
|
}
|
|
|
|
var adminIntroHandler *handlers.AdminIntroHandler
|
|
if deps.IntroAnalyzer != nil && deps.IntroRepository != nil {
|
|
adminIntroHandler = handlers.NewAdminIntroHandler(
|
|
deps.IntroAnalyzer,
|
|
deps.IntroRepository,
|
|
deps.AppContext,
|
|
slog.Default(),
|
|
)
|
|
adminIntroHandler.Settings = settingsRepo
|
|
adminIntroHandler.FileResolver = deps.FileRepo
|
|
if playbackHandler != nil {
|
|
adminIntroHandler.MarkerUpdateNotifier = playbackHandler.MarkerUpdateNotifier
|
|
}
|
|
}
|
|
|
|
// Admin subtitle config handler only needs the DB repo — no S3 required.
|
|
var adminSubtitleHandler *handlers.AdminSubtitleHandler
|
|
var subtitleManager *subtitles.Manager
|
|
if subtitleRepo != nil {
|
|
adminSubtitleHandler = handlers.NewAdminSubtitleHandler(subtitleRepo)
|
|
}
|
|
|
|
// Build subtitle search handler if we have DB and S3.
|
|
var subtitleSearchHandler *handlers.SubtitleSearchHandler
|
|
if deps.DB != nil && deps.S3Public != nil && subtitleRepo != nil {
|
|
subtitleManager = subtitles.NewManager(subtitleRepo, deps.S3Public, deps.S3Public.Bucket())
|
|
|
|
// Load provider configs from DB and register enabled providers.
|
|
providerConfigs, _ := subtitleRepo.ListProviderConfigs(deps.AppContext)
|
|
for _, cfg := range providerConfigs {
|
|
if !cfg.Enabled {
|
|
continue
|
|
}
|
|
switch cfg.ProviderName {
|
|
case "opensubtitles":
|
|
if cfg.Username == "" || cfg.Password == "" {
|
|
continue
|
|
}
|
|
subtitleManager.RegisterProvider(opensubtitles.New(opensubtitles.Config{
|
|
Username: cfg.Username,
|
|
Password: cfg.Password,
|
|
}))
|
|
case "subdl":
|
|
if cfg.APIKey == "" {
|
|
continue
|
|
}
|
|
subtitleManager.RegisterProvider(subdl.New(subdl.Config{APIKey: cfg.APIKey}))
|
|
case "subsource":
|
|
if cfg.APIKey == "" {
|
|
continue
|
|
}
|
|
subtitleManager.RegisterProvider(subsource.New(subsource.Config{APIKey: cfg.APIKey}))
|
|
}
|
|
}
|
|
|
|
mediaResolver := &pgSubtitleMediaResolver{pool: deps.DB}
|
|
subtitleSearchHandler = handlers.NewSubtitleSearchHandler(subtitleManager, subtitleRepo, mediaResolver)
|
|
}
|
|
|
|
if adminSubtitleHandler != nil && deps.DB != nil && subtitleManager != nil {
|
|
adminSubtitleHandler.SetDownloadedSubtitleDeps(deps.DB, subtitleManager)
|
|
}
|
|
|
|
// Build the AI subtitle handler (on-demand translation). Generated tracks are
|
|
// stored as ordinary downloaded subtitles, so they reach every client through
|
|
// the existing subtitle pipeline with no client changes.
|
|
var subtitleAIHandler *handlers.SubtitleAIHandler
|
|
if subtitleManager != nil && subtitleRepo != nil && deps.FileRepo != nil && deps.DB != nil && deps.Config != nil {
|
|
aiCfg := subtitleai.Config{
|
|
Enabled: deps.Config.SubtitleAI.Enabled,
|
|
BaseURL: deps.Config.SubtitleAI.BaseURL,
|
|
APIKey: deps.Config.SubtitleAI.APIKey,
|
|
ChatModel: deps.Config.SubtitleAI.ChatModel,
|
|
MaxConcurrentJobs: deps.Config.SubtitleAI.MaxConcurrentJobs,
|
|
BatchSize: deps.Config.SubtitleAI.BatchSize,
|
|
ContextNeighbors: deps.Config.SubtitleAI.ContextNeighbors,
|
|
}
|
|
var aiNotifier subtitleai.Notifier
|
|
if subtitleAINotifier != nil {
|
|
aiNotifier = subtitleAINotifier
|
|
}
|
|
aiService := subtitleai.NewService(
|
|
deps.AppContext,
|
|
aiCfg,
|
|
subtitleai.NewPgJobRepository(deps.DB),
|
|
subtitleai.NewLLMTranslator(subtitleai.NewClient(aiCfg), aiCfg.BatchSize, aiCfg.ContextNeighbors),
|
|
subtitleManager,
|
|
subtitleRepo,
|
|
deps.FileRepo,
|
|
aiNotifier,
|
|
deps.Config.Playback.FFmpegPath,
|
|
slog.Default(),
|
|
)
|
|
aiService.Recover()
|
|
subtitleAIHandler = handlers.NewSubtitleAIHandler(aiService)
|
|
}
|
|
|
|
// Build section handler if DB is available.
|
|
var sectionHandler *handlers.SectionHandler
|
|
var sectionSettingsHandler *handlers.SectionSettingsHandler
|
|
var sectionBulkHandler *handlers.SectionBulkHandler
|
|
var libraryCollectionHandler *handlers.LibraryCollectionHandler
|
|
var libraryCollectionGroupHandler *handlers.LibraryCollectionGroupHandler
|
|
libraryCollectionService := deps.CollectionService
|
|
if deps.DB != nil {
|
|
sectionRepo := sections.NewRepository(deps.DB)
|
|
sectionBulkHandler = &handlers.SectionBulkHandler{Repo: sectionRepo}
|
|
sectionFetcher := sections.NewFetcher(deps.DB)
|
|
sectionFetcher.StoreProvider = deps.UserStoreProvider
|
|
sectionFetcher.CollectionRepo = catalog.NewLibraryCollectionRepository(deps.DB)
|
|
sectionFetcher.NextUpRepo = catalog.NewNextUpRepository(deps.DB, deps.UserStoreProvider)
|
|
if deps.DB != nil {
|
|
sectionFetcher.RecommendationRepo = recommendations.NewRepo(deps.DB)
|
|
if ratingsRepo != nil {
|
|
sectionFetcher.RecommendationReader = recommendations.NewReader(sectionFetcher.RecommendationRepo, ratingsRepo, deps.RecWorker, deps.UserStoreProvider)
|
|
}
|
|
}
|
|
sections.InstallRecipeDelegate(sectionFetcher)
|
|
sectionHandler = handlers.NewSectionHandler(sectionRepo, sectionFetcher)
|
|
sectionHandler.CollectionRepo = sectionFetcher.CollectionRepo
|
|
sectionHandler.FolderRepo = deps.FolderRepo
|
|
if deps.UserStoreProvider != nil {
|
|
sectionHandler.StoreProvider = deps.UserStoreProvider
|
|
}
|
|
sectionHandler.EpisodeRepo = episodeRepo
|
|
sectionHandler.DetailSvc = detailSvc
|
|
if userRepo != nil {
|
|
sectionHandler.UserRepo = userRepo
|
|
}
|
|
if settingsRepo != nil {
|
|
sectionHandler.Settings = settingsRepo
|
|
sectionSettingsHandler = &handlers.SectionSettingsHandler{Settings: settingsRepo}
|
|
}
|
|
|
|
libraryCollectionRepo := catalog.NewLibraryCollectionRepository(deps.DB)
|
|
if libraryCollectionService == nil {
|
|
libraryCollectionService = catalog.NewLibraryCollectionService(
|
|
libraryCollectionRepo,
|
|
itemRepo,
|
|
catalog.NewLibraryItemRepository(deps.DB),
|
|
nil,
|
|
)
|
|
}
|
|
if libraryCollectionService.TMDBCollections == nil {
|
|
apiKey := ""
|
|
if deps.Config != nil {
|
|
apiKey = deps.Config.TMDBAPIKey
|
|
}
|
|
libraryCollectionService.TMDBCollections = &tmdbCollectionAdapter{
|
|
client: tmdb.NewClient(apiKey, 40),
|
|
}
|
|
}
|
|
if libraryCollectionService.TMDBFranchises == nil {
|
|
apiKey := ""
|
|
if deps.Config != nil {
|
|
apiKey = deps.Config.TMDBAPIKey
|
|
}
|
|
libraryCollectionService.TMDBFranchises = &tmdbFranchiseAdapter{
|
|
client: tmdb.NewClient(apiKey, 40),
|
|
}
|
|
}
|
|
if libraryCollectionService.TMDBDiscovers == nil {
|
|
apiKey := ""
|
|
if deps.Config != nil {
|
|
apiKey = deps.Config.TMDBAPIKey
|
|
}
|
|
libraryCollectionService.TMDBDiscovers = &tmdbDiscoverAdapter{
|
|
client: tmdb.NewClient(apiKey, 40),
|
|
}
|
|
}
|
|
traktClientID := ""
|
|
if settingsRepo != nil {
|
|
ctx := deps.AppContext
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
if value, err := settingsRepo.Get(ctx, "watchsync.trakt.client_id"); err == nil {
|
|
traktClientID = value
|
|
}
|
|
}
|
|
if libraryCollectionService.TraktCollections == nil {
|
|
libraryCollectionService.TraktCollections = &traktCollectionAdapter{
|
|
client: metatrakt.NewClient(traktClientID, 5),
|
|
}
|
|
}
|
|
if libraryCollectionService.TraktTokenResolver == nil && deps.DB != nil && settingsRepo != nil {
|
|
libraryCollectionService.TraktTokenResolver = &traktCollectionTokenResolver{
|
|
pool: deps.DB,
|
|
settings: settingsRepo,
|
|
provider: watchtrakt.NewProvider(nil, ""),
|
|
}
|
|
}
|
|
|
|
// Propagate the now-wired Trakt + TMDB fetchers to the user-side sync
|
|
// service (constructed earlier in main.go before settingsRepo and the
|
|
// Trakt adapters existed, so its fetcher fields started nil).
|
|
if deps.UserCollectionSync != nil {
|
|
if deps.UserCollectionSync.TraktCollections == nil {
|
|
deps.UserCollectionSync.TraktCollections = libraryCollectionService.TraktCollections
|
|
}
|
|
if deps.UserCollectionSync.TraktTokenResolver == nil {
|
|
deps.UserCollectionSync.TraktTokenResolver = libraryCollectionService.TraktTokenResolver
|
|
}
|
|
if deps.UserCollectionSync.TMDBCollections == nil {
|
|
deps.UserCollectionSync.TMDBCollections = libraryCollectionService.TMDBCollections
|
|
}
|
|
}
|
|
|
|
// Propagate the now-wired Trakt fetcher to the trending refresher (built
|
|
// in main.go with TMDB only, before the Trakt adapter existed).
|
|
if deps.TrendingRefresher != nil && deps.TrendingRefresher.TraktTrending == nil {
|
|
deps.TrendingRefresher.TraktTrending = libraryCollectionService.TraktCollections
|
|
}
|
|
|
|
// Wire the trending snapshot reader into the section fetcher. The
|
|
// trending_discover home section reads its list from the persisted
|
|
// snapshot table; the upstream fetch happens out-of-band in the refresh
|
|
// task, so the read path never calls the provider.
|
|
sectionFetcher.TrendingSnapshots = sections.NewTrendingSnapshotRepository(deps.DB)
|
|
|
|
libraryCollectionHandler = handlers.NewLibraryCollectionHandler(
|
|
libraryCollectionRepo,
|
|
libraryCollectionService,
|
|
itemRepo,
|
|
4*time.Hour,
|
|
nil,
|
|
deps.S3Public,
|
|
)
|
|
libraryCollectionHandler.FrontendFS = deps.FrontendFS
|
|
libraryCollectionHandler.Executor = &catalog.QueryExecutor{Pool: deps.DB}
|
|
libraryCollectionHandler.SectionRepo = sectionRepo
|
|
libraryCollectionHandler.UserCollectionPool = deps.DB
|
|
libraryCollectionHandler.EventsHub = deps.EventsHub
|
|
if deps.FolderRepo != nil {
|
|
libraryCollectionHandler.FolderRepo = deps.FolderRepo
|
|
} else {
|
|
libraryCollectionHandler.FolderRepo = catalog.NewFolderRepository(deps.DB)
|
|
}
|
|
libraryCollectionGroupRepo := catalog.NewLibraryCollectionGroupRepository(deps.DB)
|
|
libraryCollectionHandler.GroupRepo = libraryCollectionGroupRepo
|
|
if deps.DB != nil {
|
|
libraryCollectionHandler.JobRepo = adminjob.NewRepository(deps.DB)
|
|
}
|
|
libraryCollectionGroupHandler = handlers.NewLibraryCollectionGroupHandler(
|
|
libraryCollectionGroupRepo,
|
|
libraryCollectionRepo,
|
|
deps.DB,
|
|
)
|
|
refresher := &catalog.SmartCountRefresher{
|
|
Pool: deps.DB,
|
|
Executor: &catalog.QueryExecutor{Pool: deps.DB},
|
|
}
|
|
libraryCollectionHandler.SmartCountRefresher = refresher
|
|
appCtx := deps.AppContext
|
|
if appCtx == nil {
|
|
appCtx = context.Background()
|
|
}
|
|
go func() {
|
|
select {
|
|
case <-time.After(15 * time.Second):
|
|
case <-appCtx.Done():
|
|
return
|
|
}
|
|
refreshed, errs := refresher.RefreshAll(appCtx)
|
|
slog.Info("smart-count refresh complete", "refreshed", refreshed, "errors", errs)
|
|
|
|
ticker := time.NewTicker(time.Hour)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-appCtx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
refreshed, errs := refresher.RefreshAll(appCtx)
|
|
slog.Debug("smart-count refresh complete", "refreshed", refreshed, "errors", errs)
|
|
}
|
|
}
|
|
}()
|
|
if detailSvc != nil {
|
|
libraryCollectionHandler.SetDetailService(detailSvc)
|
|
libraryCollectionHandler.SetupCollage()
|
|
}
|
|
}
|
|
|
|
// Build recommendations handler if ratings repo is available.
|
|
var recsHandler *handlers.RecommendationsHandler
|
|
if ratingsRepo != nil {
|
|
var recsRepo *recommendations.Repo
|
|
var recsReader *recommendations.Reader
|
|
if deps.DB != nil {
|
|
recsRepo = recommendations.NewRepo(deps.DB)
|
|
recsReader = recommendations.NewReader(recsRepo, ratingsRepo, deps.RecWorker, deps.UserStoreProvider)
|
|
}
|
|
recsHandler = handlers.NewRecommendationsHandler(deps.Recommender, recsReader, deps.UserStoreProvider, ratingsRepo, recsRepo, deps.Recommender != nil)
|
|
if deps.DB != nil {
|
|
recsFetcher := sections.NewFetcher(deps.DB)
|
|
recsFetcher.StoreProvider = deps.UserStoreProvider
|
|
recsFetcher.NextUpRepo = catalog.NewNextUpRepository(deps.DB, deps.UserStoreProvider)
|
|
recsHandler.Fetcher = recsFetcher
|
|
recsHandler.WatchTonightFetcher = recsFetcher
|
|
}
|
|
if detailSvc != nil {
|
|
recsHandler.DetailSvc = detailSvc
|
|
}
|
|
recsHandler.CalendarRepo = calendarRepo
|
|
recsHandler.EpisodeRepo = episodeRepo
|
|
if deps.PersonRepo != nil {
|
|
recsHandler.CastFetcher = deps.PersonRepo
|
|
}
|
|
if deps.RecWorker != nil {
|
|
recsHandler.RecWorker = deps.RecWorker
|
|
}
|
|
}
|
|
|
|
// Build download handler.
|
|
var downloadHandler *handlers.DownloadHandler
|
|
if deps.DB != nil && deps.FileRepo != nil && deps.Config != nil {
|
|
downloadRepo := download.NewRepository(deps.DB)
|
|
downloadBandwidth := download.NewBandwidthManager(
|
|
deps.Config.Download.ServerBandwidthBPS,
|
|
deps.Config.Download.UserBandwidthBPS,
|
|
)
|
|
downloadLimiter := download.NewQuantityLimiter(
|
|
downloadRepo,
|
|
deps.Config.Download.MaxConcurrentPerUser,
|
|
deps.Config.Download.MaxPerPeriod,
|
|
deps.Config.Download.PeriodDuration,
|
|
)
|
|
downloadSvc := download.NewService(
|
|
downloadRepo,
|
|
downloadBandwidth,
|
|
downloadLimiter,
|
|
deps.FileRepo,
|
|
itemRepo,
|
|
episodeRepo,
|
|
userRepo,
|
|
itemRepo,
|
|
settingsRepo,
|
|
&deps.Config.Download,
|
|
)
|
|
downloadHandler = handlers.NewDownloadHandler(downloadSvc)
|
|
} else {
|
|
downloadHandler = handlers.NewDownloadHandler(nil)
|
|
}
|
|
|
|
var historyImportHandler *handlers.HistoryImportHandler
|
|
var historyImportSvc *historyimport.Service
|
|
if deps.DB != nil {
|
|
historyRepo := historyimport.NewRepository(deps.DB)
|
|
historyImportSvc = historyimport.NewService(deps.AppContext, historyRepo, deps.UserStoreProvider)
|
|
historyIdentity := watchstate.NewStableIdentityResolver(itemRepo, episodeRepo, providerIDRepo)
|
|
historyImportSvc.SetStableIdentityResolver(historyIdentity)
|
|
if deps.EventsHub != nil {
|
|
historyImportSvc.AddObserver(evt.NewHistoryImportObserver(deps.EventsHub))
|
|
}
|
|
historyImportHandler = handlers.NewHistoryImportHandler(historyImportSvc)
|
|
if deps.UserStoreProvider != nil {
|
|
webhookSyncSvc := webhooksync.NewService(webhooksync.NewRepository(deps.DB), historyRepo, deps.UserStoreProvider)
|
|
webhookSyncSvc.SetStableIdentityResolver(historyIdentity)
|
|
webhookSyncHandler = handlers.NewWebhookSyncHandler(webhookSyncSvc)
|
|
}
|
|
}
|
|
|
|
r.Route("/api/v1", func(r chi.Router) {
|
|
r.Get("/health", healthHandler.ServeHTTP)
|
|
r.Get("/ready", readyHandler.ServeHTTP)
|
|
if webhookSyncHandler != nil {
|
|
r.Post("/plex-sync/webhooks/{secret}", webhookSyncHandler.HandleWebhook)
|
|
r.Post("/webhook-sync/webhooks/{secret}", webhookSyncHandler.HandleWebhook)
|
|
}
|
|
|
|
// Theme endpoints (admin-css is public for pre-login branding).
|
|
if settingsRepo != nil {
|
|
themeHandler := handlers.NewThemeHandler(settingsRepo)
|
|
r.Get("/theme/admin-css", themeHandler.HandleAdminCSS)
|
|
r.Get("/theme/branding", themeHandler.HandleBranding)
|
|
|
|
// Catalog and download proxies require auth (to avoid open proxy).
|
|
if authMiddleware != nil {
|
|
r.Group(func(r 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)
|
|
})
|
|
}
|
|
}
|
|
|
|
if deps.PluginHTTPProxy != nil {
|
|
r.HandleFunc("/plugins/{installation_id}/*", func(w http.ResponseWriter, r *http.Request) {
|
|
installationID, err := strconv.Atoi(chi.URLParam(r, "installation_id"))
|
|
if err != nil {
|
|
http.Error(w, "invalid installation id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
authenticated, admin, userID := resolveOptionalPluginAccessUser(r, jwtService, sessionRepo, apiKeyRepo, userRepo)
|
|
ctx := plugins.WithPluginAccessUser(r.Context(), authenticated, admin, userID)
|
|
deps.PluginHTTPProxy.ServeRoute(w, r.WithContext(ctx), installationID, authenticated, admin)
|
|
})
|
|
r.Get("/plugin-assets/{installation_id}/*", func(w http.ResponseWriter, r *http.Request) {
|
|
installationID, err := strconv.Atoi(chi.URLParam(r, "installation_id"))
|
|
if err != nil {
|
|
http.Error(w, "invalid installation id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
assetPath := strings.TrimPrefix(chi.URLParam(r, "*"), "/")
|
|
if assetPath == "" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
authenticated, admin := resolveOptionalPluginAccess(r, jwtService, sessionRepo)
|
|
deps.PluginHTTPProxy.ServeAsset(w, r.WithContext(plugins.WithPluginAccess(r.Context(), authenticated, admin)), installationID, assetPath)
|
|
})
|
|
}
|
|
|
|
// Auth routes: public (no auth required).
|
|
if authHandler != nil {
|
|
// OAuth handler is optional: it only stands up when PublicURL is
|
|
// configured (we need a stable redirect_uri origin for IdPs) and
|
|
// the DB is available (oauth_session storage).
|
|
var oauthHandler *auth.OAuthHandler
|
|
if deps.PublicURL != "" && deps.DB != nil && authService != nil && jwtService != nil {
|
|
stateSecret := auth.DeriveOAuthStateSecret([]byte(deps.Config.Auth.JWTSecret))
|
|
oauthStore := auth.NewPGOAuthStore(deps.DB, stateSecret)
|
|
resolveClient := func(ctx context.Context, installationID int) (auth.OAuthClient, string, error) {
|
|
pp := authService.FindOAuthInstallation(installationID)
|
|
if pp == nil {
|
|
return nil, "", errors.New("plugin not found")
|
|
}
|
|
c, err := pp.OAuthClient(ctx)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return c, pp.CapabilityID(), nil
|
|
}
|
|
oauthHandler = auth.NewOAuthHandler(auth.OAuthHandlerDeps{
|
|
Store: oauthStore,
|
|
CompletionStore: oauthStore,
|
|
StateSecret: stateSecret,
|
|
ResolveClient: resolveClient,
|
|
LoginCompleter: authService,
|
|
HostBaseURL: deps.PublicURL,
|
|
StateTTL: 10 * time.Minute,
|
|
})
|
|
}
|
|
authHandler.SetOAuthRoutesAvailable(oauthHandler != nil)
|
|
|
|
r.Route("/auth", func(r chi.Router) {
|
|
if deps.RateLimitMW != nil {
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("login")).Post("/login", authHandler.HandleLogin)
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("setup")).Post("/setup", authHandler.HandleSetup)
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("signup")).Post("/signup", authHandler.HandleSignup)
|
|
} else {
|
|
r.Post("/login", authHandler.HandleLogin)
|
|
r.Post("/setup", authHandler.HandleSetup)
|
|
r.Post("/signup", authHandler.HandleSignup)
|
|
}
|
|
r.Get("/setup", authHandler.HandleSetupStatus)
|
|
r.Get("/providers", authHandler.HandleProviders)
|
|
r.Post("/refresh", authHandler.HandleRefresh)
|
|
r.Get("/signup", authHandler.HandleSignupStatus)
|
|
if authMiddleware != nil {
|
|
r.With(authMiddleware.RequireAuth).Post("/plugin-launch", authHandler.HandlePluginLaunch)
|
|
}
|
|
if oauthHandler != nil {
|
|
r.Post("/oauth/complete", oauthHandler.HandleComplete)
|
|
r.Route("/oauth/{install_id}", func(r chi.Router) {
|
|
r.Post("/init", oauthHandler.HandleInit)
|
|
r.Get("/callback", oauthHandler.HandleCallback)
|
|
})
|
|
}
|
|
if deps.RateLimitMW != nil {
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("device_start")).Post("/device/start", authHandler.HandleDeviceStart)
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("device_lookup")).Get("/device", authHandler.HandleDeviceLookup)
|
|
r.With(deps.RateLimitMW.AuthEndpointHandler("device_poll")).Post("/device/poll", authHandler.HandleDevicePoll)
|
|
} else {
|
|
r.Post("/device/start", authHandler.HandleDeviceStart)
|
|
r.Get("/device", authHandler.HandleDeviceLookup)
|
|
r.Post("/device/poll", authHandler.HandleDevicePoll)
|
|
}
|
|
|
|
// Protected auth routes (require valid session).
|
|
if authMiddleware != nil {
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMiddleware.RequireAuth)
|
|
r.Post("/logout", authHandler.HandleLogout)
|
|
r.Post("/impersonation/end", authHandler.HandleEndImpersonation)
|
|
r.Get("/me", authHandler.HandleMe)
|
|
r.Get("/sessions", authHandler.HandleListSessions)
|
|
r.Delete("/sessions/{id}", authHandler.HandleDeleteSession)
|
|
r.Post("/device/approve", authHandler.HandleDeviceApprove)
|
|
r.Post("/device/deny", authHandler.HandleDeviceDeny)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
// API key management routes (auth only, no viewer access needed).
|
|
if apiKeyRepo != nil && authMiddleware != nil {
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMiddleware.RequireAuth)
|
|
if demoGuard != nil {
|
|
r.Use(demoGuard.Guard)
|
|
}
|
|
|
|
apiKeyHandler := handlers.NewAPIKeyHandler(apiKeyRepo)
|
|
r.Route("/api-keys", func(r chi.Router) {
|
|
r.Post("/", apiKeyHandler.HandleCreateAPIKey)
|
|
r.Get("/", apiKeyHandler.HandleListAPIKeys)
|
|
r.Delete("/{id}", apiKeyHandler.HandleDeleteAPIKey)
|
|
})
|
|
})
|
|
}
|
|
|
|
// All remaining routes require auth.
|
|
if authMiddleware != nil {
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(authMiddleware.RequireAuth)
|
|
if demoGuard != nil {
|
|
r.Use(demoGuard.Guard)
|
|
}
|
|
if deps.RateLimitMW != nil {
|
|
r.Use(deps.RateLimitMW.Handler)
|
|
}
|
|
if viewerAccessMiddleware != nil {
|
|
r.Use(viewerAccessMiddleware.RequireViewerAccess)
|
|
}
|
|
|
|
// User-facing library route (all authenticated users).
|
|
if libraryHandler != nil {
|
|
r.Get("/user/libraries", libraryHandler.HandleListUserLibraries)
|
|
}
|
|
if deps.EventsHub != nil {
|
|
eventsHandler := handlers.NewEventsHandler(
|
|
deps.EventsHub,
|
|
adminJobsHandler,
|
|
adminHandler,
|
|
deps.TaskManager,
|
|
deps.ScanRegistry,
|
|
deps.LibraryScanQueue,
|
|
historyImportSvc,
|
|
)
|
|
r.Get("/events/ws", eventsHandler.HandleWebSocket)
|
|
}
|
|
|
|
// Library management routes (admin-only).
|
|
if libraryHandler != nil {
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireAdmin)
|
|
|
|
r.Route("/libraries", func(r chi.Router) {
|
|
r.Get("/", libraryHandler.HandleListLibraries)
|
|
r.Get("/roots", libraryHandler.HandleListRoots)
|
|
r.Put("/roots/override", libraryHandler.HandleUpsertRootOverride)
|
|
r.Delete("/roots/override", libraryHandler.HandleDeleteRootOverride)
|
|
r.Get("/skipped-roots", libraryHandler.HandleListSkippedRoots)
|
|
r.Get("/stale-ids", libraryHandler.HandleListStaleIDs)
|
|
r.Post("/stale-ids/{contentID}/rematch", libraryHandler.HandleRematchStaleID)
|
|
r.Get("/unmatched-items", libraryHandler.HandleListUnmatchedItems)
|
|
r.Post("/", libraryHandler.HandleCreateLibrary)
|
|
r.Put("/reorder", libraryHandler.HandleReorderLibraries)
|
|
r.Put("/{id}", libraryHandler.HandleUpdateLibrary)
|
|
r.Delete("/{id}", libraryHandler.HandleDeleteLibrary)
|
|
r.Post("/{id}/check-mount", libraryHandler.HandleCheckLibraryMount)
|
|
r.Post("/{id}/confirm-empty-root-cleanup", libraryHandler.HandleConfirmEmptyRootCleanup)
|
|
r.Post("/{id}/refresh-metadata", libraryHandler.HandleRefreshLibraryMetadata)
|
|
r.Get("/{id}/providers", libraryHandler.HandleGetLibraryProviders)
|
|
r.Put("/{id}/providers", libraryHandler.HandleSetLibraryProviders)
|
|
r.Put("/{id}/poster", libraryHandler.HandleUploadPoster)
|
|
r.Delete("/{id}/poster", libraryHandler.HandleDeletePoster)
|
|
})
|
|
|
|
r.Post("/scan", libraryHandler.HandleScan)
|
|
r.Post("/scan/cancel", libraryHandler.HandleScanCancel)
|
|
})
|
|
}
|
|
|
|
// Browse, search, and item detail routes.
|
|
if itemsHandler != nil {
|
|
r.Get("/catalog", catalogHandler.HandleGetCatalog)
|
|
r.Get("/catalog/filters", catalogHandler.HandleGetCatalogFilters)
|
|
r.Post("/catalog/query", catalogHandler.HandlePostCatalogQuery)
|
|
if catalogResourceHandler != nil {
|
|
r.Get("/catalog/items/{id}", catalogResourceHandler.HandleGetItemDetail)
|
|
r.Get("/catalog/items/{id}/episodes", catalogResourceHandler.HandleGetItemEpisodes)
|
|
r.Get("/catalog/items/{id}/versions", catalogResourceHandler.HandleGetItemVersions)
|
|
r.Get("/catalog/series/{id}/seasons", catalogResourceHandler.HandleGetSeasons)
|
|
r.Get("/catalog/series/{id}/seasons/{num}", catalogResourceHandler.HandleGetSeason)
|
|
r.Get("/catalog/series/{id}/seasons/{num}/episodes", catalogResourceHandler.HandleGetEpisodes)
|
|
}
|
|
r.Get("/watch/{id}", itemsHandler.HandleGetWatchDetail)
|
|
}
|
|
|
|
if calendarRepo != nil {
|
|
calendarPopular := recommendations.NewRepo(deps.DB)
|
|
calendarTrending := sections.NewTrendingSnapshotRepository(deps.DB)
|
|
calendarHandler := handlers.NewCalendarHandler(calendarRepo, detailSvc, calendarPopular, calendarTrending)
|
|
r.With(apimw.RequireProfile).Get("/calendar", calendarHandler.HandleGetCalendar)
|
|
}
|
|
|
|
if peopleHandler != nil {
|
|
r.Get("/people", peopleHandler.HandleSearch)
|
|
r.Get("/people/{id}", peopleHandler.HandleGetPerson)
|
|
r.Post("/people/{id}/refresh", peopleHandler.HandleRefreshPerson)
|
|
}
|
|
|
|
if libraryCollectionHandler != nil {
|
|
r.Get("/library/{id}/collections", libraryCollectionHandler.HandleListLibraryCollections)
|
|
r.Get("/library/{id}/collections/{collection_id}/items", libraryCollectionHandler.HandleGetLibraryCollectionItems)
|
|
r.Get("/library/{id}/user-collections", libraryCollectionHandler.HandleListLibraryUserCollections)
|
|
}
|
|
|
|
// Profile routes.
|
|
if profileHandler != nil {
|
|
r.Route("/profiles", func(r chi.Router) {
|
|
r.Get("/household/sessions", profileHandler.HandleListHouseholdSessions)
|
|
r.Get("/", profileHandler.HandleListProfiles)
|
|
r.Post("/", profileHandler.HandleCreateProfile)
|
|
r.Put("/{id}", profileHandler.HandleUpdateProfile)
|
|
r.Delete("/{id}", profileHandler.HandleDeleteProfile)
|
|
r.Put("/{id}/avatar", profileHandler.HandleUploadAvatar)
|
|
r.Delete("/{id}/avatar", profileHandler.HandleDeleteAvatar)
|
|
r.Post("/{id}/verify-pin", profileHandler.HandleVerifyPIN)
|
|
})
|
|
}
|
|
|
|
// Favorites, watchlist, and history routes (profile-scoped).
|
|
if personalDataHandler != nil && itemsHandler != nil {
|
|
r.Route("/watched", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Post("/{id}", itemsHandler.HandleMarkWatched)
|
|
r.Delete("/{id}", itemsHandler.HandleMarkUnwatched)
|
|
})
|
|
|
|
r.Route("/favorites", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", personalDataHandler.HandleListFavorites)
|
|
r.Get("/{item_id}", personalDataHandler.HandleCheckFavorite)
|
|
r.Put("/{item_id}", personalDataHandler.HandleAddFavorite)
|
|
r.Delete("/{item_id}", personalDataHandler.HandleRemoveFavorite)
|
|
})
|
|
|
|
r.Route("/watchlist", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", personalDataHandler.HandleListWatchlist)
|
|
r.Get("/{item_id}", personalDataHandler.HandleCheckWatchlist)
|
|
r.Put("/{item_id}", personalDataHandler.HandleAddToWatchlist)
|
|
r.Delete("/{item_id}", personalDataHandler.HandleRemoveFromWatchlist)
|
|
})
|
|
|
|
r.Route("/history", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", personalDataHandler.HandleListHistory)
|
|
r.Post("/remove", personalDataHandler.HandleRemoveHistory)
|
|
})
|
|
|
|
// Ratings routes (profile-scoped).
|
|
if ratingsHandler != nil {
|
|
r.Route("/ratings", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", ratingsHandler.HandleListRatings)
|
|
r.Get("/{item_id}", ratingsHandler.HandleGetRating)
|
|
r.Put("/{item_id}", ratingsHandler.HandleSetRating)
|
|
r.Delete("/{item_id}", ratingsHandler.HandleDeleteRating)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Progress and sync routes (profile-scoped).
|
|
if progressHandler != nil {
|
|
r.Route("/progress", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", progressHandler.HandleListProgress)
|
|
})
|
|
|
|
r.Route("/sync", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Post("/progress", progressHandler.HandleSyncProgress)
|
|
})
|
|
}
|
|
|
|
// Collection routes (profile-scoped).
|
|
if collectionHandler != nil {
|
|
var userImportHandler *handlers.UserCollectionImportHandler
|
|
if deps.UserCollectionSync != nil {
|
|
userImportHandler = handlers.NewUserCollectionImportHandler(
|
|
deps.UserStoreProvider,
|
|
deps.UserCollectionSync,
|
|
deps.UserCollectionScheduler,
|
|
nil,
|
|
deps.MDBListClient,
|
|
deps.S3Public,
|
|
deps.FrontendFS,
|
|
4*time.Hour,
|
|
)
|
|
}
|
|
r.Route("/collections", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", collectionHandler.HandleListCollections)
|
|
r.Post("/", collectionHandler.HandleCreateCollection)
|
|
r.Post("/preview", collectionHandler.HandlePreviewCollection)
|
|
r.Put("/order", collectionHandler.HandleReorderCollections)
|
|
r.Post("/groups", collectionHandler.HandleCreateCollectionGroup)
|
|
r.Put("/groups/order", collectionHandler.HandleReorderCollectionGroups)
|
|
r.Put("/groups/{id}", collectionHandler.HandleUpdateCollectionGroup)
|
|
r.Delete("/groups/{id}", collectionHandler.HandleDeleteCollectionGroup)
|
|
if userImportHandler != nil {
|
|
r.Get("/templates", userImportHandler.HandleListTemplates)
|
|
r.Get("/import/mdblist/search", userImportHandler.HandleSearchMDBList)
|
|
r.Get("/import/mdblist/top", userImportHandler.HandleTopMDBList)
|
|
r.Post("/import/mdblist", userImportHandler.HandleImportMDBList)
|
|
r.Post("/import/tmdb", userImportHandler.HandleImportTMDB)
|
|
r.Post("/import/trakt", userImportHandler.HandleImportTrakt)
|
|
r.Post("/{id}/sync", userImportHandler.HandleSync)
|
|
}
|
|
r.Put("/{id}", collectionHandler.HandleUpdateCollection)
|
|
r.Delete("/{id}", collectionHandler.HandleDeleteCollection)
|
|
r.Delete("/{id}/image", collectionHandler.HandleDeleteCollectionImage)
|
|
r.Get("/{id}/items", collectionHandler.HandleListCollectionItems)
|
|
r.Put("/{id}/items/order", collectionHandler.HandleReorderCollectionItems)
|
|
r.Put("/{id}/items/{item_id}", collectionHandler.HandleAddCollectionItem)
|
|
r.Delete("/{id}/items/{item_id}", collectionHandler.HandleRemoveCollectionItem)
|
|
})
|
|
}
|
|
|
|
if homeDismissalHandler != nil {
|
|
r.Route("/home/dismissals", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Put("/{surface}/{item_id}", homeDismissalHandler.HandleUpsertDismissal)
|
|
r.Delete("/{surface}/{item_id}", homeDismissalHandler.HandleDeleteDismissal)
|
|
})
|
|
}
|
|
|
|
if watchProviderHandler != nil {
|
|
r.Route("/watch-providers", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", watchProviderHandler.HandleListProviders)
|
|
r.Get("/{provider}/connection", watchProviderHandler.HandleGetConnection)
|
|
r.Patch("/{provider}/connection", watchProviderHandler.HandleUpdateConnection)
|
|
r.Delete("/{provider}/connection", watchProviderHandler.HandleDeleteConnection)
|
|
r.Post("/{provider}/auth/device-code", watchProviderHandler.HandleStartDeviceAuth)
|
|
r.Post("/{provider}/auth/poll", watchProviderHandler.HandlePollDeviceAuth)
|
|
r.Post("/{provider}/auth/api-key", watchProviderHandler.HandleConnectAPIKey)
|
|
r.Post("/{provider}/sync", watchProviderHandler.HandleManualSync)
|
|
r.Get("/{provider}/sync-runs", watchProviderHandler.HandleListSyncRuns)
|
|
})
|
|
}
|
|
|
|
if requestHandler != nil {
|
|
r.Route("/requests", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/search", requestHandler.HandleSearch)
|
|
r.Get("/discover", requestHandler.HandleDiscover)
|
|
r.Get("/discover/studios", requestHandler.HandleListStudios)
|
|
r.Get("/discover/networks", requestHandler.HandleListNetworks)
|
|
r.Get("/discover/genres", requestHandler.HandleListGenres)
|
|
r.Get("/discover/browse/studio/{slug}", requestHandler.HandleBrowseStudio)
|
|
r.Get("/discover/browse/network/{slug}", requestHandler.HandleBrowseNetwork)
|
|
r.Get("/discover/browse/genre/{slug}", requestHandler.HandleBrowseGenre)
|
|
r.Get("/discover/{section}", requestHandler.HandleDiscoverSection)
|
|
r.Get("/detail/{media_type}/{tmdb_id}", requestHandler.HandleGetDetail)
|
|
r.Get("/status", requestHandler.HandleGetStatus)
|
|
r.Post("/", requestHandler.HandleCreate)
|
|
r.Get("/mine", requestHandler.HandleListMine)
|
|
r.Get("/{id}", requestHandler.HandleGet)
|
|
r.Post("/{id}/cancel", requestHandler.HandleCancel)
|
|
})
|
|
}
|
|
|
|
// Settings routes (user-scoped, no profile required).
|
|
if settingsHandler != nil {
|
|
r.Route("/settings", func(r chi.Router) {
|
|
if deps.PluginUserConfig != nil && deps.PluginService != nil {
|
|
pluginHandler := handlers.NewPluginHandler(
|
|
plugins.NewRepositoryStore(deps.DB),
|
|
plugins.NewInstallationStore(deps.DB),
|
|
plugins.NewRuntimeConfigStore(deps.DB),
|
|
deps.PluginService,
|
|
deps.PluginUserConfig,
|
|
deps.PluginHTTPProxy,
|
|
metadata.NewChainRepository(deps.DB),
|
|
deps.PluginImageResolver,
|
|
)
|
|
r.Get("/plugins", pluginHandler.HandleListUserPluginSettings)
|
|
r.Get("/plugins/{installation_id}", pluginHandler.HandleGetUserPluginSettings)
|
|
r.Put("/plugins/{installation_id}", pluginHandler.HandlePutUserPluginSettings)
|
|
}
|
|
r.Get("/", settingsHandler.HandleListSettings)
|
|
r.Get("/overlay-config", settingsHandler.HandleGetOverlayConfig)
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/effective", settingsHandler.HandleGetEffectiveSettings)
|
|
r.Get("/subtitle_appearance/effective", settingsHandler.HandleGetEffectiveSubtitleAppearance)
|
|
r.Put("/device/subtitle_appearance", settingsHandler.HandleSetSubtitleAppearanceDeviceOverride)
|
|
r.Delete("/device/subtitle_appearance", settingsHandler.HandleDeleteSubtitleAppearanceDeviceOverride)
|
|
r.Get("/device/{key}", settingsHandler.HandleGetDeviceSetting)
|
|
r.Put("/device/{key}", settingsHandler.HandleSetDeviceSetting)
|
|
r.Delete("/device/{key}", settingsHandler.HandleDeleteDeviceSetting)
|
|
})
|
|
r.Get("/{key}", settingsHandler.HandleGetSetting)
|
|
r.Put("/{key}", settingsHandler.HandleSetSetting)
|
|
r.Delete("/{key}", settingsHandler.HandleDeleteSetting)
|
|
})
|
|
}
|
|
|
|
if historyImportHandler != nil {
|
|
r.Route("/history-imports", func(r chi.Router) {
|
|
r.Get("/sources", historyImportHandler.HandleListSources)
|
|
r.Post("/emby-connect/login", historyImportHandler.HandleLoginConnect)
|
|
r.Post("/plex/auth/pin", historyImportHandler.HandleCreatePlexPin)
|
|
r.Post("/plex/auth/check", historyImportHandler.HandleCheckPlexPin)
|
|
r.Get("/runs", historyImportHandler.HandleListRuns)
|
|
r.Post("/runs", historyImportHandler.HandleCreateRun)
|
|
r.Get("/runs/{id}", historyImportHandler.HandleGetRun)
|
|
})
|
|
}
|
|
if webhookSyncHandler != nil {
|
|
r.Route("/plex-sync", func(r chi.Router) {
|
|
r.Get("/connections", webhookSyncHandler.HandleLegacyListConnections)
|
|
r.Post("/connections", webhookSyncHandler.HandleLegacyCreateConnection)
|
|
r.Delete("/connections/{id}", webhookSyncHandler.HandleLegacyDeleteConnection)
|
|
r.Post("/connections/{id}/webhook/rotate", webhookSyncHandler.HandleLegacyRotateWebhook)
|
|
r.Get("/connections/{id}/actors", webhookSyncHandler.HandleLegacyGetActors)
|
|
r.Put("/connections/{id}/actors", webhookSyncHandler.HandleLegacyUpdateActors)
|
|
})
|
|
r.Route("/webhook-sync", func(r chi.Router) {
|
|
r.Get("/connections", webhookSyncHandler.HandleListConnections)
|
|
r.Post("/connections", webhookSyncHandler.HandleCreateConnection)
|
|
r.Put("/connections/{id}", webhookSyncHandler.HandleUpdateConnection)
|
|
r.Delete("/connections/{id}", webhookSyncHandler.HandleDeleteConnection)
|
|
r.Post("/connections/{id}/webhook/rotate", webhookSyncHandler.HandleRotateWebhook)
|
|
r.Get("/connections/{id}/events", webhookSyncHandler.HandleListEvents)
|
|
r.Get("/connections/{id}/profile-mappings", webhookSyncHandler.HandleGetProfileMappings)
|
|
r.Put("/connections/{id}/profile-mappings", webhookSyncHandler.HandleUpdateProfileMappings)
|
|
})
|
|
}
|
|
|
|
// Subtitle preference routes (profile-scoped).
|
|
if subtitlePrefHandler != nil {
|
|
r.Route("/subtitle-prefs", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/{series_id}", subtitlePrefHandler.HandleGetSubtitlePref)
|
|
r.Put("/{series_id}", subtitlePrefHandler.HandleSetSubtitlePref)
|
|
r.Delete("/{series_id}", subtitlePrefHandler.HandleDeleteSubtitlePref)
|
|
})
|
|
}
|
|
|
|
// Audio preference routes (profile-scoped).
|
|
if audioPrefHandler != nil {
|
|
r.Route("/audio-prefs", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/{series_id}", audioPrefHandler.HandleGetAudioPref)
|
|
r.Put("/{series_id}", audioPrefHandler.HandleSetAudioPref)
|
|
r.Delete("/{series_id}", audioPrefHandler.HandleDeleteAudioPref)
|
|
})
|
|
}
|
|
|
|
// Library playback preference routes (profile-scoped).
|
|
if libraryPlaybackPrefHandler != nil {
|
|
r.Route("/library-playback-prefs", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", libraryPlaybackPrefHandler.HandleListLibraryPlaybackPrefs)
|
|
r.Put("/{library_id}", libraryPlaybackPrefHandler.HandleSetLibraryPlaybackPref)
|
|
r.Delete("/{library_id}", libraryPlaybackPrefHandler.HandleDeleteLibraryPlaybackPref)
|
|
})
|
|
}
|
|
|
|
// Subtitle search + AI translation routes.
|
|
if subtitleSearchHandler != nil {
|
|
if deps.FileRepo != nil && itemRepo != nil {
|
|
fileAuthorizer := &handlers.MediaFileAuthorizer{
|
|
FileResolver: deps.FileRepo,
|
|
ItemAccess: itemRepo,
|
|
EpisodeLookup: episodeRepo,
|
|
}
|
|
subtitleSearchHandler.FileAuthorizer = fileAuthorizer
|
|
if subtitleAIHandler != nil {
|
|
subtitleAIHandler.FileAuthorizer = fileAuthorizer
|
|
}
|
|
}
|
|
r.Route("/subtitles", func(r chi.Router) {
|
|
r.Post("/search", subtitleSearchHandler.HandleSearch)
|
|
r.Post("/download", subtitleSearchHandler.HandleDownload)
|
|
r.Post("/upload", subtitleSearchHandler.HandleUpload)
|
|
r.Post("/detect-language", subtitleSearchHandler.HandleDetectLanguage)
|
|
if subtitleAIHandler != nil {
|
|
r.Get("/ai/status", subtitleAIHandler.HandleStatus)
|
|
r.Post("/ai/translate", subtitleAIHandler.HandleTranslate)
|
|
r.Get("/ai/jobs", subtitleAIHandler.HandleListJobs)
|
|
r.Get("/ai/jobs/{job_id}", subtitleAIHandler.HandleGetJob)
|
|
r.Post("/ai/jobs/{job_id}/cancel", subtitleAIHandler.HandleCancelJob)
|
|
} else {
|
|
// Answer the capability probe with 200 {"enabled": false}
|
|
// when AI translation isn't wired, so the client gets a
|
|
// clean negative instead of a 404.
|
|
r.Get("/ai/status", handlers.WriteSubtitleAIDisabledStatus)
|
|
}
|
|
r.Get("/{media_file_id}", subtitleSearchHandler.HandleList)
|
|
r.Delete("/{id}", subtitleSearchHandler.HandleDelete)
|
|
})
|
|
}
|
|
|
|
// Playback routes.
|
|
if playbackHandler != nil {
|
|
playbackHandler.ItemAccess = itemRepo
|
|
playbackHandler.EpisodeLookup = episodeRepo
|
|
playbackHandler.OriginalLangLookup = itemRepo
|
|
playbackHandler.FFmpegLogSink = deps.FFmpegLogSink
|
|
|
|
r.Route("/playback", func(r chi.Router) {
|
|
// HLS transcode delivery — no profile auth needed;
|
|
// session ID (UUID) serves as the access token, same
|
|
// pattern as /stream/{session_id}.
|
|
r.Get("/transcode/{session_id}/master.m3u8", playbackHandler.HandleGetTranscodeManifest)
|
|
r.Get("/transcode/{session_id}/segment/{name}", playbackHandler.HandleGetTranscodeSegment)
|
|
|
|
// Playback realtime control socket — needs auth but not profile.
|
|
r.Get("/sessions/{session_id}/control/ws", playbackHandler.HandleSessionWebSocket)
|
|
|
|
// All mutation routes require profile auth.
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Post("/start", playbackHandler.HandleStartPlayback)
|
|
r.Post("/{session_id}/progress", playbackHandler.HandleUpdateProgress)
|
|
r.Patch("/{session_id}/audio", playbackHandler.HandleChangeAudioTrack)
|
|
r.Delete("/{session_id}", playbackHandler.HandleStopPlayback)
|
|
r.Post("/transcode/start", playbackHandler.HandleStartTranscode)
|
|
})
|
|
})
|
|
}
|
|
|
|
if watchTogetherHandler != nil {
|
|
r.Route("/watch-together", func(r chi.Router) {
|
|
r.Get("/rooms/{room_id}/ws", watchTogetherHandler.HandleRoomWebSocket)
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Post("/rooms", watchTogetherHandler.HandleCreateRoom)
|
|
r.Post("/join", watchTogetherHandler.HandleJoinRoom)
|
|
r.Get("/rooms/{room_id}", watchTogetherHandler.HandleGetRoom)
|
|
r.Put("/rooms/{room_id}/selection", watchTogetherHandler.HandleSelectRoomItem)
|
|
r.Patch("/rooms/{room_id}/policy", watchTogetherHandler.HandleUpdateRoomPolicy)
|
|
r.Delete("/rooms/{room_id}", watchTogetherHandler.HandleCloseRoom)
|
|
r.Get("/rooms/{room_id}/suggestions", watchTogetherHandler.HandleListSuggestions)
|
|
r.Post("/rooms/{room_id}/suggestions", watchTogetherHandler.HandleCreateSuggestion)
|
|
r.Delete("/rooms/{room_id}/suggestions/{suggestion_id}", watchTogetherHandler.HandleDeleteSuggestion)
|
|
r.Post("/rooms/{room_id}/suggestions/{suggestion_id}/vote", watchTogetherHandler.HandleVote)
|
|
r.Delete("/rooms/{room_id}/suggestions/{suggestion_id}/vote", watchTogetherHandler.HandleUnvote)
|
|
r.Post("/rooms/{room_id}/suggestions/promote", watchTogetherHandler.HandlePromoteSuggestion)
|
|
})
|
|
})
|
|
}
|
|
|
|
// Stream routes.
|
|
if streamHandler != nil {
|
|
r.Get("/stream/{session_id}", streamHandler.HandleStream)
|
|
r.Head("/stream/{session_id}", streamHandler.HandleStream)
|
|
r.Get("/stream/{session_id}/subtitles/{track}", streamHandler.HandleSubtitle)
|
|
r.Get("/stream/{session_id}/subtitles/{track}/fonts", streamHandler.HandleSubtitleFonts)
|
|
}
|
|
|
|
// Download routes.
|
|
r.Route("/downloads", func(r chi.Router) {
|
|
r.Post("/", downloadHandler.HandleCreateDownload)
|
|
r.Get("/", downloadHandler.HandleListDownloads)
|
|
r.Delete("/{id}", downloadHandler.HandleDeleteDownload)
|
|
r.Get("/{id}/file", downloadHandler.HandleDownloadFile)
|
|
})
|
|
r.Get("/direct-download", downloadHandler.HandleDirectDownload)
|
|
r.Head("/direct-download", downloadHandler.HandleDirectDownload)
|
|
|
|
// Recipe gallery catalog (no profile required — purely static metadata).
|
|
recipeHandler := &handlers.RecipeHandler{}
|
|
r.Get("/sections/recipes", recipeHandler.HandleList)
|
|
r.Get("/sections/recipes/{type}/candidates", recipeHandler.HandleCandidates)
|
|
|
|
// Section endpoints (profile-scoped).
|
|
if sectionHandler != nil {
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/home/layout", sectionHandler.HandleHomeLayout)
|
|
r.Get("/home/sections", sectionHandler.HandleHomeSections)
|
|
r.Get("/home/sections/{id}/items", sectionHandler.HandleHomeSectionItems)
|
|
r.Get("/library/{id}/layout", sectionHandler.HandleLibraryLayout)
|
|
r.Get("/library/{id}/sections", sectionHandler.HandleLibrarySections)
|
|
r.Get("/library/{id}/sections/{sectionId}/items", sectionHandler.HandleLibrarySectionItems)
|
|
})
|
|
|
|
r.Route("/profile/sections", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/", sectionHandler.HandleGetProfileOverrides)
|
|
r.Put("/", sectionHandler.HandleSaveProfileOverrides)
|
|
r.Delete("/reset", sectionHandler.HandleResetProfileOverrides)
|
|
r.Get("/settings", sectionHandler.HandleSectionSettings)
|
|
if sectionSettingsHandler != nil {
|
|
r.Get("/flags", sectionSettingsHandler.HandleGetProfileFlag)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Recommendation routes (profile-scoped).
|
|
if recsHandler != nil {
|
|
r.Route("/recommendations", func(r chi.Router) {
|
|
r.Use(apimw.RequireProfile)
|
|
r.Get("/for-you/main", recsHandler.HandleForYouMain)
|
|
r.Get("/for-you/rows", recsHandler.HandleForYouRows)
|
|
r.Get("/because-watched/{item_id}", recsHandler.HandleBecauseWatched)
|
|
r.Get("/similar/{item_id}", recsHandler.HandleSimilar)
|
|
r.Get("/similar-users", recsHandler.HandleSimilarUsers)
|
|
r.Get("/taste-profile", recsHandler.HandleTasteProfile)
|
|
r.Get("/popular", recsHandler.HandlePopular)
|
|
r.Get("/recently-added", recsHandler.HandleRecentlyAdded)
|
|
r.Get("/discover", recsHandler.HandleDiscover)
|
|
r.Get("/section/{kind}", recsHandler.HandleSection)
|
|
r.Get("/section/{kind}/{key}", recsHandler.HandleSection)
|
|
r.Get("/watch-tonight", recsHandler.HandleWatchTonight)
|
|
r.Get("/watch-tonight/cards", recsHandler.HandleWatchTonightCards)
|
|
r.Get("/taste-seed/items", recsHandler.HandleTasteSeedItems)
|
|
r.Post("/taste-seed", recsHandler.HandleTasteSeed)
|
|
})
|
|
}
|
|
|
|
// Admin routes.
|
|
if adminHandler != nil {
|
|
r.Route("/admin", func(r chi.Router) {
|
|
metadataItemAccess := apimw.RequireAdmin
|
|
if permissionMiddleware != nil {
|
|
metadataItemAccess = permissionMiddleware.RequireMetadataCurationForItem
|
|
}
|
|
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(metadataItemAccess)
|
|
r.Post("/items/{id}/refresh-metadata", adminHandler.HandleRefreshItemMetadata)
|
|
r.Patch("/items/{id}/metadata", adminHandler.HandleUpdateItemMetadata)
|
|
if adminMatchHandler != nil {
|
|
r.Post("/items/{id}/match/search", adminMatchHandler.HandleSearchItemMatchCandidates)
|
|
r.Post("/items/{id}/match/apply", adminMatchHandler.HandleApplyItemMatch)
|
|
}
|
|
})
|
|
|
|
if adminJobsHandler != nil {
|
|
// Curators must poll their own item-refresh jobs, so this stays outside
|
|
// the admin-only group. HandleGet enforces per-job authorization.
|
|
r.Get("/jobs/{id}", adminJobsHandler.HandleGet)
|
|
}
|
|
|
|
r.Group(func(r chi.Router) {
|
|
r.Use(apimw.RequireAdmin)
|
|
|
|
r.Get("/users", adminHandler.HandleListUsers)
|
|
r.Post("/users", adminHandler.HandleCreateUser)
|
|
r.Get("/users/{id}", adminHandler.HandleGetUser)
|
|
r.Put("/users/{id}", adminHandler.HandleUpdateUser)
|
|
r.Delete("/users/{id}", adminHandler.HandleDeleteUser)
|
|
r.Post("/users/{id}/impersonate", adminHandler.HandleImpersonateUser)
|
|
r.Get("/users/{id}/profiles", adminHandler.HandleListUserProfiles)
|
|
r.Get("/users/{id}/settings", adminHandler.HandleListUserSettings)
|
|
r.Get("/users/{id}/settings/{key}", adminHandler.HandleGetUserSetting)
|
|
r.Put("/users/{id}/settings/{key}", adminHandler.HandleUpdateUserSetting)
|
|
r.Delete("/users/{id}/settings/{key}", adminHandler.HandleDeleteUserSetting)
|
|
r.Get("/users/{id}/device-settings", adminHandler.HandleListUserDeviceSettings)
|
|
r.Get("/users/{id}/device-settings/{key}", adminHandler.HandleListUserDeviceSettingsByKey)
|
|
r.Put("/users/{id}/profiles/{profile_id}/device-settings/{key}/{device_id}", adminHandler.HandleUpdateUserDeviceSetting)
|
|
r.Delete("/users/{id}/device-settings/{key}", adminHandler.HandleDeleteUserDeviceSettingsByKey)
|
|
r.Delete("/users/{id}/profiles/{profile_id}/device-settings/{key}/{device_id}", adminHandler.HandleDeleteUserDeviceSetting)
|
|
r.Delete("/users/{id}/profiles/{profile_id}/devices/{device_id}/settings", adminHandler.HandleDeleteAllUserDeviceSettings)
|
|
r.Get("/devices", adminHandler.HandleListDevices)
|
|
r.Get("/devices/{user_id}/{device_id}", adminHandler.HandleGetDevice)
|
|
|
|
r.Get("/sessions", adminHandler.HandleListSessions)
|
|
r.Get("/playback-history", adminHandler.HandleListPlaybackHistory)
|
|
r.Get("/unmatched", adminHandler.HandleListUnmatched)
|
|
r.Get("/stats", adminHandler.HandleGetStats)
|
|
r.Get("/settings/sensitive-status", adminHandler.HandleGetSensitiveStatus)
|
|
r.Post("/settings/check/{kind}", adminHandler.HandleCheckSettingsConnection)
|
|
if sectionSettingsHandler != nil {
|
|
r.Get("/settings/sections", sectionSettingsHandler.HandleGet)
|
|
r.Put("/settings/sections", sectionSettingsHandler.HandlePut)
|
|
}
|
|
r.Get("/settings/{key}", adminHandler.HandleGetSetting)
|
|
r.Get("/settings", adminHandler.HandleGetSettings)
|
|
r.Put("/settings/{key}", adminHandler.HandleUpdateSetting)
|
|
if adminIntroHandler != nil {
|
|
r.Post("/items/{id}/refresh-markers", adminIntroHandler.HandleRefreshEpisodeMarkers)
|
|
r.Post("/items/{id}/redetect-intro", adminIntroHandler.HandleRedetectEpisodeIntro)
|
|
}
|
|
if peopleHandler != nil {
|
|
r.Post("/people/{id}/refresh", peopleHandler.HandleAdminRefreshPerson)
|
|
r.Patch("/people/{id}", peopleHandler.HandleAdminUpdatePerson)
|
|
}
|
|
|
|
if adminImageHandler != nil {
|
|
r.Get("/items/{id}/images", adminImageHandler.HandleGetItemImages)
|
|
r.Post("/items/{id}/images/apply", adminImageHandler.HandleApplyItemImage)
|
|
}
|
|
|
|
filesystemHandler := handlers.NewFilesystemHandler()
|
|
r.Get("/filesystem/browse", filesystemHandler.HandleBrowse)
|
|
|
|
if catalogSeedHandler != nil {
|
|
r.Route("/catalog", func(r chi.Router) {
|
|
r.Post("/export", catalogSeedHandler.HandleExport)
|
|
r.Post("/export-jobs", catalogSeedHandler.HandleCreateExportJob)
|
|
r.Post("/export-jobs/{id}/publish", catalogSeedHandler.HandlePublishExportJob)
|
|
r.Post("/import-jobs", catalogSeedHandler.HandleCreateImportJob)
|
|
r.Get("/import-sources", catalogSeedHandler.HandleListImportSources)
|
|
r.Get("/local-import-sources", catalogSeedHandler.HandleListLocalImportSources)
|
|
r.Post("/import", catalogSeedHandler.HandleImport)
|
|
})
|
|
}
|
|
|
|
if adminJobsHandler != nil {
|
|
r.Route("/jobs", func(r chi.Router) {
|
|
r.Get("/", adminJobsHandler.HandleList)
|
|
})
|
|
}
|
|
|
|
if deps.PluginService != nil && deps.PluginUserConfig != nil {
|
|
pluginHandler := handlers.NewPluginHandler(
|
|
plugins.NewRepositoryStore(deps.DB),
|
|
plugins.NewInstallationStore(deps.DB),
|
|
plugins.NewRuntimeConfigStore(deps.DB),
|
|
deps.PluginService,
|
|
deps.PluginUserConfig,
|
|
deps.PluginHTTPProxy,
|
|
metadata.NewChainRepository(deps.DB),
|
|
deps.PluginImageResolver,
|
|
)
|
|
r.Route("/plugins", func(r chi.Router) {
|
|
r.Get("/repositories", pluginHandler.HandleListRepositories)
|
|
r.Post("/repositories", pluginHandler.HandleCreateRepository)
|
|
r.Put("/repositories/{id}", pluginHandler.HandleUpdateRepository)
|
|
r.Delete("/repositories/{id}", pluginHandler.HandleDeleteRepository)
|
|
r.Get("/catalog", pluginHandler.HandleCatalog)
|
|
r.Get("/installations", pluginHandler.HandleListInstallations)
|
|
r.Post("/installations", pluginHandler.HandleCreateInstallation)
|
|
r.Post("/uploads", pluginHandler.HandleUploadInstallation)
|
|
r.Put("/installations/{id}", pluginHandler.HandleUpdateInstallation)
|
|
r.Post("/installations/{id}/update", pluginHandler.HandleApplyUpdate)
|
|
r.Post("/installations/{id}/config/test", pluginHandler.HandleTestInstallationConfig)
|
|
r.Put("/installations/{id}/config", pluginHandler.HandlePutInstallationConfig)
|
|
r.Put("/installations/{id}/auth-binding", pluginHandler.HandlePutAuthBinding)
|
|
r.Put("/installations/{id}/task-bindings/{capability_id}", pluginHandler.HandlePutTaskBinding)
|
|
r.Delete("/installations/{id}", pluginHandler.HandleDeleteInstallation)
|
|
})
|
|
}
|
|
|
|
if historyImportHandler != nil {
|
|
r.Route("/history-import-sources", func(r chi.Router) {
|
|
r.Get("/", historyImportHandler.HandleAdminListSources)
|
|
r.Post("/", historyImportHandler.HandleAdminCreateSource)
|
|
r.Put("/{id}", historyImportHandler.HandleAdminUpdateSource)
|
|
r.Delete("/{id}", historyImportHandler.HandleAdminDeleteSource)
|
|
})
|
|
|
|
r.Route("/history-imports", func(r chi.Router) {
|
|
r.Post("/plex/login", historyImportHandler.HandleAdminPlexLogin)
|
|
r.Put("/sources/{id}/token", historyImportHandler.HandleAdminSetSourceToken)
|
|
r.Delete("/sources/{id}/token", historyImportHandler.HandleAdminClearSourceToken)
|
|
r.Get("/sources/{id}/users", historyImportHandler.HandleAdminDiscoverUsers)
|
|
r.Post("/sources/{id}/bulk-run", historyImportHandler.HandleAdminBulkRun)
|
|
r.Get("/mappings", historyImportHandler.HandleAdminListMappings)
|
|
r.Post("/mappings", historyImportHandler.HandleAdminCreateMapping)
|
|
r.Put("/mappings/{id}", historyImportHandler.HandleAdminUpdateMapping)
|
|
r.Delete("/mappings/{id}", historyImportHandler.HandleAdminDeleteMapping)
|
|
r.Post("/mappings/{id}/run", historyImportHandler.HandleAdminCreateRun)
|
|
r.Get("/runs", historyImportHandler.HandleAdminListRuns)
|
|
r.Get("/runs/{id}", historyImportHandler.HandleAdminGetRun)
|
|
r.Post("/runs/{id}/cancel", historyImportHandler.HandleAdminCancelRun)
|
|
})
|
|
}
|
|
|
|
if sectionHandler != nil {
|
|
r.Route("/sections", func(r chi.Router) {
|
|
r.Get("/", sectionHandler.HandleListSections)
|
|
r.Post("/", sectionHandler.HandleCreateSection)
|
|
r.Post("/preview", sectionHandler.HandlePreview)
|
|
r.Put("/reorder", sectionHandler.HandleReorderSections)
|
|
r.Post("/restore-defaults", sectionHandler.HandleRestoreDefaults)
|
|
r.Put("/{id}", sectionHandler.HandleUpdateSection)
|
|
r.Delete("/{id}", sectionHandler.HandleDeleteSection)
|
|
if sectionBulkHandler != nil {
|
|
r.Post("/bulk-create", sectionBulkHandler.HandleBulkCreate)
|
|
}
|
|
})
|
|
}
|
|
|
|
if libraryCollectionHandler != nil {
|
|
collectionTemplateHandler := handlers.NewCollectionTemplateHandler(nil)
|
|
r.Route("/collections", func(r chi.Router) {
|
|
r.Get("/", libraryCollectionHandler.HandleListAdminCollections)
|
|
r.Get("/templates", collectionTemplateHandler.HandleListTemplates)
|
|
r.Get("/template-bundles", libraryCollectionHandler.HandleListTemplateBundles)
|
|
r.Post("/template-bundles/{bundleID}/apply", libraryCollectionHandler.HandleApplyTemplateBundle)
|
|
r.Post("/template-bundles/{bundleID}/apply-job", libraryCollectionHandler.HandleApplyTemplateBundleJob)
|
|
r.Post("/", libraryCollectionHandler.HandleCreateAdminCollection)
|
|
r.Post("/preview", libraryCollectionHandler.HandlePreviewAdminCollection)
|
|
r.Put("/order", libraryCollectionHandler.HandleReorderAdminCollections)
|
|
r.Put("/{id}", libraryCollectionHandler.HandleUpdateAdminCollection)
|
|
r.Delete("/{id}", libraryCollectionHandler.HandleDeleteAdminCollection)
|
|
r.Post("/{id}/sync", libraryCollectionHandler.HandleSyncAdminCollection)
|
|
r.Delete("/{id}/image", libraryCollectionHandler.HandleDeleteCollectionImage)
|
|
r.Put("/{id}/items/order", libraryCollectionHandler.HandleReorderAdminCollectionItems)
|
|
r.Put("/{id}/items/{item_id}", libraryCollectionHandler.HandleAddAdminCollectionItem)
|
|
r.Delete("/{id}/items/{item_id}", libraryCollectionHandler.HandleRemoveAdminCollectionItem)
|
|
r.Post("/import/mdblist", libraryCollectionHandler.HandleImportMDBList)
|
|
r.Post("/import/tmdb", libraryCollectionHandler.HandleImportTMDBCollection)
|
|
r.Post("/import/trakt", libraryCollectionHandler.HandleImportTraktCollection)
|
|
})
|
|
}
|
|
if libraryCollectionGroupHandler != nil {
|
|
r.Route("/libraries/{libraryID}/collection-groups", func(r chi.Router) {
|
|
r.Get("/", libraryCollectionGroupHandler.HandleListGroups)
|
|
r.Post("/", libraryCollectionGroupHandler.HandleCreateGroup)
|
|
r.Put("/reorder", libraryCollectionGroupHandler.HandleReorderGroups)
|
|
})
|
|
r.Route("/collection-groups", func(r chi.Router) {
|
|
r.Put("/{id}", libraryCollectionGroupHandler.HandleUpdateGroup)
|
|
r.Delete("/{id}", libraryCollectionGroupHandler.HandleDeleteGroup)
|
|
r.Put("/{groupID}/collections/reorder", libraryCollectionGroupHandler.HandleReorderCollectionsInGroup)
|
|
})
|
|
}
|
|
|
|
if deps.NodeRepo != nil {
|
|
jwtSecret := ""
|
|
if deps.Config != nil {
|
|
jwtSecret = deps.Config.Auth.JWTSecret
|
|
}
|
|
nodeHandler := handlers.NewNodeHandler(deps.NodeRepo, deps.ProxyPool, deps.TranscodePool, deps.NodeRepo, deps.EventBus, deps.RedisClient, jwtSecret)
|
|
r.Route("/nodes", func(r chi.Router) {
|
|
r.Get("/", nodeHandler.HandleListNodes)
|
|
r.Post("/", nodeHandler.HandleCreateNode)
|
|
r.Put("/{id}", nodeHandler.HandleUpdateNode)
|
|
r.Delete("/{id}", nodeHandler.HandleDeleteNode)
|
|
r.Post("/{id}/check", nodeHandler.HandleCheckNode)
|
|
r.Post("/force-reload", nodeHandler.HandleForceReloadNodes)
|
|
r.Post("/{id}/force-reload", nodeHandler.HandleForceReloadNode)
|
|
})
|
|
// Live node sessions (reads from Redis)
|
|
// Note: /admin/sessions is already used for playback sessions from PostgreSQL.
|
|
r.Get("/node-sessions", nodeHandler.HandleListSessions)
|
|
}
|
|
|
|
// System inspection.
|
|
{
|
|
sysJWTSecret := ""
|
|
if deps.Config != nil {
|
|
sysJWTSecret = deps.Config.Auth.JWTSecret
|
|
}
|
|
systemHandler := handlers.NewSystemHandler(deps.TranscodePool, sysJWTSecret)
|
|
r.Route("/system", func(r chi.Router) {
|
|
r.Get("/build", systemHandler.HandleBuildInfo)
|
|
r.Get("/hw-accel", systemHandler.HandleHWAccel)
|
|
})
|
|
}
|
|
|
|
if deps.RecWorker != nil {
|
|
adminRecsHandler := handlers.NewAdminRecommendationsHandler(deps.RecWorker)
|
|
r.Route("/recommendations", func(r chi.Router) {
|
|
r.Get("/status", adminRecsHandler.HandleStatus)
|
|
r.Post("/trigger/embeddings", adminRecsHandler.HandleTriggerEmbeddings)
|
|
r.Post("/trigger/taste-profiles", adminRecsHandler.HandleTriggerTasteProfiles)
|
|
r.Post("/trigger/cowatch", adminRecsHandler.HandleTriggerCowatch)
|
|
r.Post("/trigger/recommendations", adminRecsHandler.HandleTriggerRecommendations)
|
|
})
|
|
}
|
|
|
|
if inviteCodeRepo != nil {
|
|
inviteCodeHandler := handlers.NewInviteCodeHandler(inviteCodeRepo)
|
|
r.Route("/invite-codes", func(r chi.Router) {
|
|
r.Get("/", inviteCodeHandler.HandleListInviteCodes)
|
|
r.Post("/", inviteCodeHandler.HandleCreateInviteCode)
|
|
r.Put("/{id}", inviteCodeHandler.HandleUpdateInviteCode)
|
|
r.Post("/{id}/top-up", inviteCodeHandler.HandleTopUpInviteCode)
|
|
r.Delete("/{id}", inviteCodeHandler.HandleDeleteInviteCode)
|
|
})
|
|
}
|
|
|
|
if adminSubtitleHandler != nil {
|
|
r.Route("/subtitle-providers", func(r chi.Router) {
|
|
r.Get("/", adminSubtitleHandler.HandleListProviders)
|
|
r.Route("/{provider}", func(r chi.Router) {
|
|
r.Put("/", adminSubtitleHandler.HandleUpdateProvider)
|
|
r.Post("/test", adminSubtitleHandler.HandleTestProvider)
|
|
})
|
|
})
|
|
r.Route("/subtitles", func(r chi.Router) {
|
|
r.Get("/", adminSubtitleHandler.HandleListDownloadedSubtitles)
|
|
r.Route("/{id}", func(r chi.Router) {
|
|
r.Patch("/", adminSubtitleHandler.HandlePatchDownloadedSubtitle)
|
|
r.Get("/download", adminSubtitleHandler.HandleDownloadDownloadedSubtitle)
|
|
r.Delete("/", adminSubtitleHandler.HandleDeleteDownloadedSubtitle)
|
|
})
|
|
})
|
|
}
|
|
|
|
// Rate limit admin routes
|
|
if deps.RateLimitMW != nil && settingsRepo != nil {
|
|
rateLimitHandler := handlers.NewRateLimitHandler(settingsRepo, deps.RateLimitMW, deps.EventBus)
|
|
r.Route("/rate-limits", func(r chi.Router) {
|
|
r.Get("/config", rateLimitHandler.HandleGetConfig)
|
|
r.Put("/config", rateLimitHandler.HandleUpdateConfig)
|
|
})
|
|
}
|
|
|
|
if apiKeyRepo != nil {
|
|
apiKeyHandler := handlers.NewAPIKeyHandler(apiKeyRepo)
|
|
r.Get("/users/{userId}/api-keys", apiKeyHandler.HandleAdminListUserAPIKeys)
|
|
r.Get("/api-keys", apiKeyHandler.HandleAdminListAllAPIKeys)
|
|
r.Post("/api-keys", apiKeyHandler.HandleAdminCreateAPIKey)
|
|
r.Delete("/api-keys/{id}", apiKeyHandler.HandleAdminDeleteAPIKey)
|
|
r.Put("/api-keys/{id}/tier", apiKeyHandler.HandleAdminUpdateTier)
|
|
}
|
|
|
|
if requestHandler != nil {
|
|
r.Get("/requests", requestHandler.HandleAdminList)
|
|
r.Post("/requests/{id}/approve", requestHandler.HandleApprove)
|
|
r.Post("/requests/{id}/decline", requestHandler.HandleDecline)
|
|
r.Post("/requests/{id}/cancel", requestHandler.HandleCancel)
|
|
r.Post("/requests/{id}/retry", requestHandler.HandleRetry)
|
|
r.Get("/request-settings", requestHandler.HandleGetSettings)
|
|
r.Put("/request-settings", requestHandler.HandleUpdateSettings)
|
|
r.Get("/request-users/{user_id}/limit", requestHandler.HandleGetUserLimit)
|
|
r.Put("/request-users/{user_id}/limit", requestHandler.HandleUpdateUserLimit)
|
|
r.Get("/request-integrations", requestHandler.HandleListIntegrations)
|
|
r.Post("/request-integrations", requestHandler.HandleCreateIntegration)
|
|
r.Put("/request-integrations/{id}", requestHandler.HandleUpdateIntegration)
|
|
r.Delete("/request-integrations/{id}", requestHandler.HandleDeleteIntegration)
|
|
r.Post("/request-integrations/{id}/options", requestHandler.HandleLoadIntegrationOptions)
|
|
}
|
|
|
|
if autoscanHandler != nil {
|
|
r.Get("/autoscan/settings", autoscanHandler.HandleGetSettings)
|
|
r.Put("/autoscan/settings", autoscanHandler.HandleUpdateSettings)
|
|
r.Get("/autoscan/connections", autoscanHandler.HandleListConnections)
|
|
r.Post("/autoscan/connections", autoscanHandler.HandleCreateConnection)
|
|
r.Put("/autoscan/connections/{id}", autoscanHandler.HandleUpdateConnection)
|
|
r.Delete("/autoscan/connections/{id}", autoscanHandler.HandleDeleteConnection)
|
|
r.Post("/autoscan/connections/test", autoscanHandler.HandleTestConnection)
|
|
r.Get("/autoscan/scan-source-plugins", autoscanHandler.HandleListAvailableScanSources)
|
|
r.Get("/autoscan/sources", autoscanHandler.HandleListSources)
|
|
r.Post("/autoscan/sources", autoscanHandler.HandleCreateSource)
|
|
r.Put("/autoscan/sources/{id}", autoscanHandler.HandleUpdateSource)
|
|
r.Delete("/autoscan/sources/{id}", autoscanHandler.HandleDeleteSource)
|
|
r.Get("/autoscan/sources/{id}/rewrite-suggestions", autoscanHandler.HandleRewriteSuggestions)
|
|
r.Get("/autoscan/scans", autoscanHandler.HandleListScans)
|
|
r.Get("/autoscan/events", autoscanHandler.HandleListEvents)
|
|
r.Post("/autoscan/trigger", autoscanHandler.HandleTrigger)
|
|
r.Get("/autoscan/status", autoscanHandler.HandleStatus)
|
|
}
|
|
|
|
if deps.ActivityLogRepo != nil {
|
|
adminIPHandler := handlers.NewAdminIPHandler(deps.ActivityLogRepo)
|
|
r.Get("/users/{id}/ips", adminIPHandler.HandleGetUserIPs)
|
|
r.Get("/ips", adminIPHandler.HandleGetIPUsers)
|
|
}
|
|
if deps.OpsLogRepo != nil && deps.ActivityLogRepo != nil {
|
|
adminLogsHandler := handlers.NewAdminLogsHandler(deps.OpsLogRepo, deps.ActivityLogRepo, deps.LogStreamHub)
|
|
r.Get("/logs/app", adminLogsHandler.HandleListOperationalLogs)
|
|
r.Get("/logs/audit", adminLogsHandler.HandleListAuditLogs)
|
|
r.Get("/logs/ws", adminLogsHandler.HandleLogStreamWebSocket)
|
|
}
|
|
if adminPlaybackControlHandler != nil {
|
|
r.Post("/sessions/{session_id}/pause", adminPlaybackControlHandler.HandlePauseSession)
|
|
r.Post("/sessions/{session_id}/resume", adminPlaybackControlHandler.HandleResumeSession)
|
|
r.Post("/sessions/{session_id}/stop", adminPlaybackControlHandler.HandleStopSession)
|
|
r.Post("/sessions/{session_id}/terminate", adminPlaybackControlHandler.HandleTerminateSession)
|
|
r.Post("/sessions/{session_id}/message", adminPlaybackControlHandler.HandleMessageSession)
|
|
}
|
|
|
|
if deps.TaskManager != nil {
|
|
taskHistoryRepo := repository.NewPgExecutionRepository(deps.DB)
|
|
taskMetrics := handlers.NewTaskMetricsService(metadata.NewRefreshDebtRepository(deps.DB))
|
|
taskHandler := handlers.NewTaskHandler(deps.TaskManager, taskHistoryRepo, taskMetrics)
|
|
r.Route("/tasks", func(r chi.Router) {
|
|
r.Get("/", taskHandler.HandleListTasks)
|
|
r.Get("/{key}", taskHandler.HandleGetTask)
|
|
r.Get("/{key}/metrics", taskHandler.HandleGetMetrics)
|
|
r.Post("/{key}/run", taskHandler.HandleRunTask)
|
|
r.Post("/{key}/cancel", taskHandler.HandleCancelTask)
|
|
r.Put("/{key}/triggers", taskHandler.HandleUpdateTriggers)
|
|
r.Get("/{key}/history", taskHandler.HandleGetHistory)
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
// pgSubtitleMediaResolver implements handlers.SubtitleMediaResolver using a direct PG query.
|
|
type pgSubtitleMediaResolver struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func (r *pgSubtitleMediaResolver) GetMediaFileWithMetadata(ctx context.Context, fileID int) (*handlers.MediaFileMetadata, error) {
|
|
var meta handlers.MediaFileMetadata
|
|
err := r.pool.QueryRow(ctx, `
|
|
SELECT
|
|
mf.id,
|
|
mf.file_path,
|
|
COALESCE(mf.file_size, 0),
|
|
COALESCE(mf.file_hash, ''),
|
|
COALESCE(mf.resolution, ''),
|
|
COALESCE(mf.codec_video, ''),
|
|
COALESCE(mf.codec_audio, ''),
|
|
mi.title,
|
|
COALESCE(mi.year, 0),
|
|
COALESCE(mi.imdb_id, ''),
|
|
COALESCE(e.season_number, 0),
|
|
COALESCE(e.episode_number, 0)
|
|
FROM media_files mf
|
|
JOIN media_items mi ON mi.content_id = mf.content_id
|
|
LEFT JOIN episodes e ON e.content_id = mf.episode_id
|
|
WHERE mf.id = $1
|
|
`, fileID).Scan(
|
|
&meta.FileID,
|
|
&meta.FilePath,
|
|
&meta.FileSize,
|
|
&meta.FileHash,
|
|
&meta.Resolution,
|
|
&meta.VideoCodec,
|
|
&meta.AudioCodec,
|
|
&meta.Title,
|
|
&meta.Year,
|
|
&meta.IMDbID,
|
|
&meta.Season,
|
|
&meta.Episode,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &meta, nil
|
|
}
|
|
|
|
func resolveOptionalPluginAccess(
|
|
r *http.Request,
|
|
jwtService *auth.JWTService,
|
|
sessionRepo *auth.SessionRepository,
|
|
) (bool, bool) {
|
|
authenticated, admin, _ := resolveOptionalPluginAccessUser(r, jwtService, sessionRepo, nil, nil)
|
|
return authenticated, admin
|
|
}
|
|
|
|
// resolveOptionalPluginAccessUser is like resolveOptionalPluginAccess but also
|
|
// returns the authenticated user's ID, and accepts API-key bearer tokens
|
|
// (sa_*) when apiKeyRepo + userRepo are provided.
|
|
func resolveOptionalPluginAccessUser(
|
|
r *http.Request,
|
|
jwtService *auth.JWTService,
|
|
sessionRepo *auth.SessionRepository,
|
|
apiKeyRepo *auth.APIKeyRepository,
|
|
userRepo *auth.UserRepository,
|
|
) (bool, bool, int) {
|
|
if jwtService == nil || sessionRepo == nil {
|
|
return false, false, 0
|
|
}
|
|
|
|
token := ""
|
|
if header := r.Header.Get("Authorization"); header != "" {
|
|
parts := strings.SplitN(header, " ", 2)
|
|
if len(parts) == 2 && strings.EqualFold(parts[0], "bearer") {
|
|
token = strings.TrimSpace(parts[1])
|
|
}
|
|
}
|
|
if token == "" {
|
|
token = strings.TrimSpace(r.URL.Query().Get("token"))
|
|
}
|
|
if token == "" {
|
|
if cookie, err := r.Cookie(auth.PluginAccessCookieName); err == nil {
|
|
token = strings.TrimSpace(cookie.Value)
|
|
}
|
|
}
|
|
if token == "" {
|
|
return false, false, 0
|
|
}
|
|
|
|
if strings.HasPrefix(token, "sa_") {
|
|
if apiKeyRepo == nil || userRepo == nil {
|
|
return false, false, 0
|
|
}
|
|
apiKey, err := apiKeyRepo.GetByKey(r.Context(), token)
|
|
if err != nil {
|
|
return false, false, 0
|
|
}
|
|
user, err := userRepo.GetByID(r.Context(), apiKey.UserID)
|
|
if err != nil || !user.Enabled {
|
|
return false, false, 0
|
|
}
|
|
return true, user.Role == "admin", user.ID
|
|
}
|
|
|
|
claims, err := jwtService.ValidateToken(token)
|
|
if err != nil || (claims.TokenType != auth.TokenTypeAccess && claims.TokenType != auth.TokenTypePluginAccess) {
|
|
return false, false, 0
|
|
}
|
|
valid, err := sessionRepo.IsValid(r.Context(), claims.SessionID)
|
|
if err != nil || !valid {
|
|
return false, false, 0
|
|
}
|
|
return true, claims.Role == "admin", claims.UserID
|
|
}
|
|
|
|
// NewTMDBCollectionFetcher creates a TMDBCollectionFetcher from an API key.
|
|
// Exported so main.go can construct it for the collection sync scheduler.
|
|
func NewTMDBCollectionFetcher(apiKey string) catalog.TMDBCollectionFetcher {
|
|
return &tmdbCollectionAdapter{
|
|
client: tmdb.NewClient(apiKey, 40),
|
|
}
|
|
}
|
|
|
|
// tmdbCollectionAdapter adapts the tmdb.Client to the catalog.TMDBCollectionFetcher interface.
|
|
type tmdbCollectionAdapter struct {
|
|
client *tmdb.Client
|
|
}
|
|
|
|
func (a *tmdbCollectionAdapter) GetCollectionPreset(ctx context.Context, preset, mediaType, timeWindow string, limit int) ([]catalog.TMDBCollectionEntry, error) {
|
|
results, err := a.client.GetCollectionPreset(ctx, preset, mediaType, timeWindow, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries := make([]catalog.TMDBCollectionEntry, len(results))
|
|
for i, r := range results {
|
|
entry := catalog.TMDBCollectionEntry{
|
|
ID: r.ID,
|
|
MediaType: r.MediaType,
|
|
Title: r.Title,
|
|
}
|
|
|
|
// Fetch external IDs (IMDb, TVDB) for better matching against local library.
|
|
if externalIDs, err := a.client.GetExternalIDs(ctx, r.MediaType, r.ID); err == nil && externalIDs != nil {
|
|
entry.IMDbID = externalIDs.IMDbID
|
|
entry.TVDBID = externalIDs.TVDBID
|
|
}
|
|
|
|
entries[i] = entry
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
// tmdbFranchiseAdapter adapts tmdb.Client to catalog.TMDBCollectionByIDFetcher
|
|
// for the `tmdb_collection` sync mode. Like the preset adapter, it enriches
|
|
// each TMDB collection part with external IDs so the catalog matcher can fall
|
|
// back to IMDb/TVDB when a local item lacks a TMDB ID.
|
|
type tmdbFranchiseAdapter struct {
|
|
client *tmdb.Client
|
|
}
|
|
|
|
func (a *tmdbFranchiseAdapter) GetCollection(ctx context.Context, id int) ([]catalog.TMDBCollectionEntry, error) {
|
|
collection, err := a.client.GetCollection(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if collection == nil {
|
|
return nil, nil
|
|
}
|
|
entries := make([]catalog.TMDBCollectionEntry, len(collection.Parts))
|
|
for i, p := range collection.Parts {
|
|
mediaType := p.MediaType
|
|
if mediaType == "" {
|
|
mediaType = "movie"
|
|
}
|
|
entry := catalog.TMDBCollectionEntry{
|
|
ID: p.ID,
|
|
MediaType: mediaType,
|
|
Title: p.Title,
|
|
}
|
|
if externalIDs, err := a.client.GetExternalIDs(ctx, mediaType, p.ID); err == nil && externalIDs != nil {
|
|
entry.IMDbID = externalIDs.IMDbID
|
|
entry.TVDBID = externalIDs.TVDBID
|
|
}
|
|
entries[i] = entry
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
// tmdbDiscoverAdapter adapts tmdb.Client to catalog.TMDBDiscoverFetcher for
|
|
// the `tmdb_discover` sync mode. Like the preset adapter, it enriches each
|
|
// result with external IDs so the catalog matcher can fall back to IMDb/TVDB
|
|
// when a local item lacks a TMDB ID.
|
|
type tmdbDiscoverAdapter struct {
|
|
client *tmdb.Client
|
|
}
|
|
|
|
func (a *tmdbDiscoverAdapter) Discover(ctx context.Context, mediaType string, params catalog.TMDBDiscoverParams, limit int) ([]catalog.TMDBCollectionEntry, error) {
|
|
results, err := a.client.Discover(ctx, mediaType, tmdb.DiscoverParams{
|
|
WithGenres: params.WithGenres,
|
|
WithoutGenres: params.WithoutGenres,
|
|
SortBy: params.SortBy,
|
|
VoteCountGte: params.VoteCountGte,
|
|
VoteAverageGte: params.VoteAverageGte,
|
|
ReleaseDateGte: params.ReleaseDateGte,
|
|
ReleaseDateLte: params.ReleaseDateLte,
|
|
Certifications: params.Certifications,
|
|
CertificationLte: params.CertificationLte,
|
|
WithRuntimeGte: params.WithRuntimeGte,
|
|
WithRuntimeLte: params.WithRuntimeLte,
|
|
OriginalLanguage: params.OriginalLanguage,
|
|
Limit: limit,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries := make([]catalog.TMDBCollectionEntry, len(results))
|
|
for i, r := range results {
|
|
entry := catalog.TMDBCollectionEntry{
|
|
ID: r.ID,
|
|
MediaType: r.MediaType,
|
|
Title: r.Title,
|
|
}
|
|
if externalIDs, err := a.client.GetExternalIDs(ctx, r.MediaType, r.ID); err == nil && externalIDs != nil {
|
|
entry.IMDbID = externalIDs.IMDbID
|
|
entry.TVDBID = externalIDs.TVDBID
|
|
}
|
|
entries[i] = entry
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
type traktCollectionAdapter struct {
|
|
client *metatrakt.Client
|
|
}
|
|
|
|
func (a *traktCollectionAdapter) GetCollectionPreset(ctx context.Context, preset, mediaType string, limit int, accessToken string) ([]catalog.TraktCollectionEntry, error) {
|
|
results, err := a.client.GetCollectionPreset(ctx, preset, mediaType, limit, accessToken)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
entries := make([]catalog.TraktCollectionEntry, len(results))
|
|
for i, r := range results {
|
|
entries[i] = catalog.TraktCollectionEntry{
|
|
TraktID: r.TraktID,
|
|
TMDBID: r.TMDBID,
|
|
TVDBID: r.TVDBID,
|
|
IMDbID: r.IMDbID,
|
|
MediaType: r.MediaType,
|
|
Title: r.Title,
|
|
Year: r.Year,
|
|
Rank: r.Rank,
|
|
}
|
|
}
|
|
return entries, nil
|
|
}
|