* docs: define ebook architecture matching audiobooks * docs: plan ebook audiobook-parity implementation * feat: add ebook scanner parser foundation * fix: harden ebook scanner foundation * fix: handle ebook isbn labels * fix: guard ebook subtree scans * feat: scan ebook libraries in core * fix: preserve ebook scan people credits * fix: refresh ebook scan metadata safely * feat: persist ebook series membership * test: cover ebook series persistence decisions * fix: address ebook scanner PR review * docs: clarify ebook foundation PR scope * feat: add ebook metadata enricher * fix: harden ebook poster cache * feat: wire ebook metadata sync task * feat: expose ebook library metadata setup * feat: add ebook catalog scope support * feat: add ebook detail view * feat: label ebook file versions by format * feat: use file-size copy for downloads * feat: use file language in download dialog * test: cover ebook detail authors and downloads * fix: drop narrator credits from ebook scanner merges * fix: align ebook collection filters with book media * fix: drop asin provider ids from ebook enrichment * fix: force ebook people refresh for stale narrators * chore: omit ebook planning docs from branch * feat: add ebook detail related content * feat: add ebook reader file entrypoint * feat: render ebooks with foliate reader * feat: persist ebook reader progress * feat: add ebook reader controls * feat: extract ebook pdf metadata * feat: favor scanner isbn during ebook enrichment * feat: extract fbz ebook metadata * feat: count cbz ebook pages * feat: show ebook file page counts * feat: show ebook download summaries * feat: switch ebook reader files * feat: prefer epub for ebook read action * feat: surface ebook reader progress * feat: sync ebook reader progress cache * feat: hide ebook read action for unsupported files * feat: filter ebook reader file selector * fix: serve fbz ebook archives with reader mime type * fix: detect fbz ebooks from compound filename * fix: authorize fbz ebooks from compound filename * fix: scope ebook catalog facets * fix: reject narrator queries for ebooks * fix: build ebook recommendation text from authors * fix: include ebooks in embedding eligibility * fix: include ebooks in recommendation media mix * fix: include ebooks in recently added recommendations * feat: include ebook progress in recommendation signals * feat: include ebooks in continue watching sections * feat: include ebooks in catalog progress metrics * fix: read ebook isbn from epub metadata * fix: filter ebook asin provider aliases * fix: fall back from unsupported ebook reader files * fix: sort ebook catalogs by reader progress * fix: filter ebook catalogs by reader progress * fix: include ebooks in last watched catalog filters * feat: reflect ebook reader progress in item user state * feat: share ebook progress state across item surfaces * feat: report ebook scan progress * fix: include ebook activity in recommendations * fix: expose ebook reader progress on item detail * fix: support ebook subtree scans * fix: honor profile header for ebook item progress * fix: add ebook library default sections * fix: route ebook continue cards to reader * fix: hide watched toggle for ebooks * fix: route ebook watch tonight cards to reader * fix: route ebook hero actions to reader * fix: detect archive ebook reader formats by filename * feat: cache embedded ebook covers during scan * fix: encode ebook hero reader links * fix: persist non-epub ebook reader progress * fix: scope narrator catalog badges to audiobooks * fix: merge ebook reader progress during item repair * fix: label ebook progress filters as read * fix: show ebook related rails as book covers * fix: remove txt ebook reader support * fix: reject txt ebook reader files * fix: label ebook advanced filters as read * fix: label ebook personalized sorts as read * fix: remove plain text reader loader path * test: cover ebook unread catalog rules * fix: preserve ebook reader library context * fix: link ebook genres with library scope * fix: encode related rail item links * fix: encode catalog card item links * fix: encode hero and continue item links * fix: encode watch tonight item links * fix: encode recommendation and search item links * test: cover ebook scan format set * fix: label ebook search results clearly * fix: make global search prompt media neutral * fix: encode catalog read API ids * fix: encode item API ids * fix: include ebook reader vendor in docker build * fix: make ebook reader build clean * fix: clean ebook embedded descriptions * docs: plan ebook reader shell parity * feat: add ebook reader shell controls * fix: widen ebook scrolled reader flow * fix: remove scrolled reader content width cap * docs: plan ebook reader full parity * feat: persist ebook reader config * feat: add ebook annotations and bookmarks * feat: add ebook reader tools and aids * feat: add ebook advanced reader settings * fix: keep ebook reader panel in viewport * fix: use foliate sizing units for ebook scroll flow * fix: keep ebook settings controls readable * fix: simplify ebook reader settings controls * feat(ebooks): extract local covers during scan (#98) * feat(ebooks): extract local covers during scan * fix(ebooks): read nullable poster paths during cover scan * fix(catalog): coalesce nullable media artwork fields * fix(ebooks): group sibling formats by book identity * fix(ebooks): tolerate legacy ebook metadata encodings * fix(ebooks): decode PDF hex metadata strings * fix(ebooks): harden local cover extraction and format grouping Address review findings on the local cover scan: - Restrict generic sidecar covers (cover.jpg, folder.png, ...) to single-book directories, always accept images named after the book file, and apply exactly one cover per reconcile with sidecar taking precedence over the embedded cover. - Replace the read-then-write poster update with an atomic conditional UPDATE (ItemRepository.SetLocalPoster) so provider/admin artwork is never clobbered by concurrent writers, and refresh locally owned posters when the extracted cover bytes change (thumbhash compare). - Preserve UTF-8 PDF Info strings (including a UTF-8 BOM) instead of forcing everything through Windows-1252; the cp1252 fallback now only applies to non-UTF-8 bytes. - Select EPUB covers by manifest media-type with properties="cover-image" outranking the EPUB2 meta name="cover" id, so XHTML cover pages no longer shadow the real image. - Order CBZ pages naturally (2.jpg before 10.jpg, ch2/ before ch10/) when picking the cover page, via a single O(n) min-scan. - Bump the ebook content group key scheme to version 2 and reprocess rows written under older versions so pre-existing libraries gain sibling-format grouping instead of accumulating duplicates. - Group different formats only (a same-format sibling with colliding sparse metadata stays a separate item) and stop a joining sibling's embedded metadata from overwriting a provider-matched item. - Decode any IANA-labelled OPF/FB2 XML charset (windows-1251, koi8-r, shift_jis, ...) via x/net/html/charset, and wire the charset reader into FB2 parsing which previously had none. - Strip the full .fb2.zip double extension from filename-derived titles and group keys. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: rxwatcher <rxwatcher@users.noreply.github.com> Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> * feat(ebooks): add reader profiles and ruler (#99) * feat(ebooks): extract local covers during scan * fix(ebooks): read nullable poster paths during cover scan * fix(catalog): coalesce nullable media artwork fields * fix(ebooks): group sibling formats by book identity * fix(ebooks): tolerate legacy ebook metadata encodings * fix(ebooks): decode PDF hex metadata strings * feat(ebooks): add reader profiles and ruler * fix(ebooks): address reader ruler and profile review findings - skip renderer setStyles/render when computed styles and attributes are unchanged, so ruler position updates no longer re-style the book view - drag the ruler via a local draft that commits on release, with the surface rect cached at pointer-down - migrate font values persisted before the generic stacks (Inter, Georgia, Merriweather, legacy serif) so the font select never renders blank, with a Custom fallback option for unknown values - make the ruler band click-through and move dragging to a dedicated keyboard-accessible slider handle so links and text selection keep working under the band - share font stacks between options and profiles via READER_FONT_STACKS - surface the active reading profile, move presets to the top of the settings panel, and drop the redundant profile button aria-labels Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ebooks): resolve prefer-const lint error in readest document lib `pnpm run lint` failed on the branch because `direction` is never reassigned in getDirection; split the destructure so only the reassigned `writingMode` stays mutable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: rxwatcher <rxwatcher@users.noreply.github.com> Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com> * Merge branch 'main' into work/ebooks-reader-base Brings the ebook integration branch up to date with main (audiobook library redesign, continue-watching rework and card affordances, quic-go bump, jellycompat fixes). Conflict resolutions favor main's generalized mechanisms and register ebooks with them: - media scope validation goes through IsValidMediaScope (now including "ebook" alongside main's "video" group scope), in Go and in the web filter/search types - continue-watching uses main's typed rails; reading-type sections pull resume points from ebook_reader_progress and the ebook library default section is wired to ContinueTypeConfig(ContinueTypeReading) - item_repo keeps main's derived select-list machinery (itemColumnExpr) and both poster accessors (GetPoster/SetLocalPoster for ebook covers, GetPosterPath for audiobook covers) - web cards/hero/watch-tonight adopt main's buildMediaPlayHref helpers, which now route ebooks to /reader/ebook and encode content ids; ebook affordances (BookOpen icon, Read verb, percent-read subtitle) carry over onto main's reworked components - LibraryForm ebook support ported into main's refactored useLibraryForm/libraryTypes modules Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(docker): copy foliate-js vendor into Dockerfile.dev frontend stage foliate-js is a file:vendor/foliate-js dependency, so pnpm install needs the vendor directory before the lockfile install layer. The production Dockerfile already copies it; the dev image was missed, breaking make dev-deploy with ENOENT on /app/web/vendor/foliate-js. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(ebooks): render Continue Reading sections as upright poster cards All-ebook continue sections previously fell through to the horizontal 16:9 wide card; include ebooks in the poster-variant check so book covers render in their natural 2:3 framing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ui): stop related-rail highlight ring clipping on detail pages Move the current-item ring onto the cover artwork with a themed ring-offset color (matching the sidebar profile highlight) and give the scroll container top headroom so the ring is not cut off by overflow-x-auto. Applies to both ebook and audiobook detail rails. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(scanner): harden ebook scanning against data loss and bad metadata - Reconcile missing ebook files like video/audio, with real per-root walk failure tracking (failed/unmounted roots are excluded from deletion), symlinked-root support via the shared logical walker, and the empty-root cleanup allowance before any destructive reconciliation. - Create ebook items as 'pending' so enrichment can promote them to 'matched' (backfill migration included), and protect matched items from re-scan clobbering: title/year skipped, people/series fill-empty only. - PDF metadata: scan head + tail windows (non-linearized PDFs keep the Info dict at the end), require proper key delimiters, head values win. - Cap plain .fb2 reads like .fbz entries; drop .md as an ebook format. - gofmt internal/scanner/audiobook.go (pre-existing drift). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(ebooks): make enrichment failures non-terminal with dedicated backoff state - Provider errors now record a failure (capped retries) instead of stamping last_refreshed, which permanently excluded items after transient outages. - Unconfigured metadata chains and the scan-window membership race skip the item without stamping or burning a retry. - Failure tracking moves to a new ebook_enrichment_state table, decoupling it from media_items.refresh_failures (shared with metadata refresh debt). - Preserve non-author people credits when persisting enrichment results. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(catalog): gate ebook progress on hidden history and centralize threshold - Apply user_history_hidden_items gating (video semantics) to the ebook watched/in-progress filters, progress sort plan, and Continue Reading. - Continue Reading pages past dismissed items via the shared collector and dedupes items across pages (also fixes the video path's latent exposure). - Centralize the 0.9 finished threshold as models.EbookFinishedProgressThreshold with a single SQL-interpolated mirror in catalog. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(recommendations): correct watcher counting and wire ebook taste signals - itemWatchersQuery dedupes to distinct (watcher, item) rows so one binge-watcher can no longer satisfy minWatchers; the eligibility floor now counts distinct accounts rather than profiles. - Hidden-history gating on GetEbookReaderProgressForUser (signal reader). - Ebook reading produces canonical implicit taste signals (weighted like the equivalent movie progress ratio); ebooks join taste-seed candidates. - Stale GetRecentlyAddedItems doc comment corrected. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): harden ebook reader endpoints and serve a Content-Security-Policy - Serve a CSP on all SPA HTML responses: blob/srcdoc book iframes inherit it, so script-src 'self' 'wasm-unsafe-eval' blocks script execution from malicious book content (sandbox alone is defeated by the WebKit allow-scripts requirement). Threat model documented on the constant. - X-Content-Type-Options: nosniff on frontend, jellycompat, and ebook file responses; MIME resolution can no longer fall through to octet-stream for an admitted ebook file. - Annotation PATCH: presence-aware field semantics (absent keeps, present sets/clears), invariant re-validation on the merged row, and an atomic SELECT ... FOR UPDATE read-merge-write. - Request size caps (413) on progress/config/annotation writes; Content-Disposition via mime.FormatMediaType; hidden-history gating in the shared ebook progress lister; FK-cascade indexes for reader tables. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(api): native read-state endpoints for ebooks - POST/DELETE /watched/{id} accepts ebook content IDs: mark read upserts progress 1.0 preserving the reader's file/location (or picks the preferred reader file for never-opened books); mark unread mirrors video unwatch semantics and deletes the progress row. - /history/remove accepts ebooks: hides via user_history_hidden_items without touching the reading position (hidden != unread; next reading activity resurfaces the book, mirroring video re-watch). - Access-filter checks match the video branch; shared logic lives in ebook_read_state.go. Sort metrics/user-state thresholds use the shared constant; profile-header fallback deduplicated. Clients: response is {type: "ebook", affected_count: 1, played: bool}; the existing watched SSE event fires. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web): harden the ebook reader UI - Open-flow race: cancellation checked after every await with full stale-run teardown (no wrong-file progress saves, no leaked views/blob URLs); book.destroy() on cleanup. - Progress: monotonic stale-response guard; visibilitychange flush uses the refresh-capable client, pagehide uses keepalive; per-book cross-format progress documented as deliberate. - Settings: side effects out of the setState updater; local edits no longer clobbered by late server config; pending saves flushed on unmount/pagehide. - TTS: generation token so Stop actually stops (Chromium/Firefox synthetic events); Media Session uninstalled on unmount. - External book links: http(s) only, opened with noopener,noreferrer. - apiBlob 512 MiB guard with a user-facing error; fraction bookmarks navigable; search-result key collisions fixed; dead e-ink code removed; getLibrarySortRelevanceScope deduplicated; md format dropped. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): mark read/unread affordances for ebooks - Item detail gets a Mark Read/Unread button; card menus drop the ebook gate and share type-aware labels/toasts (also dedupes audiobook wording). - Watched-state invalidation includes the reader progress query key so the Continue button and percent refresh after toggling. - Continue Reading dismiss copy for ebooks; dismissal path now URL-encodes item IDs (ebook content IDs can contain reserved characters). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: record the PR #124 review and hardening pass Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: rxwatcher <rxwatcher@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1340 lines
44 KiB
Go
1340 lines
44 KiB
Go
package catalog
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/models"
|
|
)
|
|
|
|
// BrowseFilters represents all supported filter, sort, and pagination parameters
|
|
// for the /items browse endpoint.
|
|
type BrowseFilters struct {
|
|
Type string // single type ("movie") or comma-separated ("movie,series")
|
|
Genre string // single genre filter (GIN index)
|
|
NamePrefix string // case-insensitive prefix filter on sort_title/title
|
|
ContentIDs []string // optional allowlist of exact content IDs
|
|
LibraryID int // filter by specific library
|
|
LibraryIDs []int // accessible library IDs (nil = all)
|
|
DisabledLibraryIDs []int // user-disabled libraries to exclude (only used when LibraryIDs is nil)
|
|
MaxContentRating string // maximum allowed content rating ceiling
|
|
YearMin int // minimum year (inclusive)
|
|
YearMax int // maximum year (inclusive)
|
|
ContentRating []string // comma-separated content ratings (e.g., PG-13, TV-MA)
|
|
Status string // pending, matched, unmatched (optional filter)
|
|
PersonID int64 // filter items by person (joins item_people)
|
|
Sort string // created_at, release_date, rating_imdb, rating_tmdb, year, sort_title, recently_added
|
|
Order string // asc, desc
|
|
Limit int
|
|
MaxLimit int // optional caller-specific cap; zero keeps the default browse cap
|
|
Offset int
|
|
SnapshotAt *time.Time // pagination fence: exclude items created after this timestamp
|
|
RequireBackdrop bool // only return items with a non-empty backdrop_path (Jellyfin ImageTypes=Backdrop filter)
|
|
}
|
|
|
|
// BrowseResult contains the paginated result of a browse query.
|
|
type BrowseResult struct {
|
|
Items []*models.MediaItem `json:"items"`
|
|
Total int `json:"total"`
|
|
HasMore bool `json:"has_more"`
|
|
}
|
|
|
|
// BrowseRepository provides browse/filter queries on the media_items table.
|
|
type BrowseRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// NewBrowseRepository creates a new BrowseRepository.
|
|
func NewBrowseRepository(pool *pgxpool.Pool) *BrowseRepository {
|
|
return &BrowseRepository{pool: pool}
|
|
}
|
|
|
|
// Pool returns the underlying pgx pool for ad-hoc queries.
|
|
func (r *BrowseRepository) Pool() *pgxpool.Pool {
|
|
return r.pool
|
|
}
|
|
|
|
// Browse executes a filtered, sorted, paginated query against media_items.
|
|
func (r *BrowseRepository) Browse(ctx context.Context, filters BrowseFilters) (*BrowseResult, error) {
|
|
return r.browse(ctx, filters, true)
|
|
}
|
|
|
|
func (r *BrowseRepository) BrowsePage(ctx context.Context, filters BrowseFilters, includeTotal bool) (*BrowseResult, error) {
|
|
return r.browse(ctx, filters, includeTotal)
|
|
}
|
|
|
|
func (r *BrowseRepository) browse(ctx context.Context, filters BrowseFilters, includeTotal bool) (*BrowseResult, error) {
|
|
plan, earlyEmpty, err := r.buildBrowsePlan(filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if earlyEmpty {
|
|
return &BrowseResult{Items: []*models.MediaItem{}, Total: 0, HasMore: false}, nil
|
|
}
|
|
|
|
dataQuery, queryArgs := plan.pagedSQL(false)
|
|
rows, err := r.pool.Query(ctx, dataQuery, queryArgs...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("browsing media items: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var (
|
|
items []*models.MediaItem
|
|
total int
|
|
)
|
|
items, err = scanBrowseItems(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hasMore := false
|
|
hasExtraRow := len(items) > plan.limit
|
|
if hasExtraRow {
|
|
items = items[:plan.limit]
|
|
}
|
|
if includeTotal {
|
|
switch {
|
|
case plan.offset == 0 && len(items) == 0:
|
|
total = 0
|
|
case !hasExtraRow && len(items) > 0:
|
|
total = plan.offset + len(items)
|
|
default:
|
|
countSQL, countArgs := plan.countSQL()
|
|
if err := r.pool.QueryRow(ctx, countSQL, countArgs...).Scan(&total); err != nil {
|
|
return nil, fmt.Errorf("count browse page: %w", err)
|
|
}
|
|
}
|
|
hasMore = total > plan.offset+plan.limit
|
|
} else if hasExtraRow {
|
|
hasMore = true
|
|
}
|
|
|
|
return &BrowseResult{
|
|
Items: items,
|
|
Total: total,
|
|
HasMore: hasMore,
|
|
}, nil
|
|
}
|
|
|
|
// browseQueryPlan captures the rendered SQL fragments and bound args for a
|
|
// browse data query. It is produced by buildBrowsePlan and consumed both by
|
|
// browse() (to execute) and by tests (to inspect the emitted SQL without a
|
|
// database).
|
|
type browseQueryPlan struct {
|
|
selectClause string
|
|
fromClause string
|
|
whereClause string
|
|
groupByClause string
|
|
orderBy string
|
|
// args holds bound values for the FROM/WHERE/GROUP BY portion of the
|
|
// plan. orderArgs is kept separate so countSQL — which omits ORDER BY —
|
|
// can bind only the args its SQL actually references. Combining the two
|
|
// would make countSQL pass extra bound values past the count query's
|
|
// placeholder set, and pgx/Postgres reject that with "bind message
|
|
// supplies N parameters, but prepared statement requires M". The
|
|
// random+SnapshotAt sort path (buildOrderByPlan) is the canonical case:
|
|
// it bakes a bound timestamp arg into the ORDER BY expression that the
|
|
// count query has no use for.
|
|
args []any
|
|
orderArgs []any
|
|
limit int
|
|
offset int
|
|
limitArgIdx int // 1-based bound-arg index for the LIMIT param
|
|
}
|
|
|
|
// countSQL renders a count-only query that returns the total number of rows
|
|
// matching the plan's FROM/WHERE/GROUP BY, ignoring LIMIT/OFFSET/ORDER BY.
|
|
// Used by exact-total callers so the data SELECT can remain a pure page query.
|
|
// Wraps the inner query in `SELECT COUNT(*) FROM (...) sub` so any GROUP BY in
|
|
// the inner query is preserved.
|
|
//
|
|
// Binds only p.args (FROM/WHERE/GROUP BY values). orderArgs are deliberately
|
|
// excluded — the count SQL has no ORDER BY clause to reference them, and
|
|
// passing extra bound values would fail with "bind message supplies N
|
|
// parameters, but prepared statement requires M".
|
|
func (p browseQueryPlan) countSQL() (string, []any) {
|
|
args := append([]any{}, p.args...)
|
|
sql := fmt.Sprintf(
|
|
"SELECT COUNT(*) FROM (SELECT 1 FROM %s %s %s) sub",
|
|
p.fromClause, p.whereClause, p.groupByClause,
|
|
)
|
|
return sql, args
|
|
}
|
|
|
|
// pagedSQL renders the final paged SELECT and returns it together with the
|
|
// fully-bound arg list. The query always asks for one extra row so callers can
|
|
// detect whether more pages exist without coupling the sorted page fetch to an
|
|
// exact count.
|
|
func (p browseQueryPlan) pagedSQL(_ bool) (string, []any) {
|
|
queryLimit := p.limit
|
|
queryLimit++
|
|
// Bind order: where/from args, then order-by args, then LIMIT, then OFFSET.
|
|
// limitArgIdx is computed by buildBrowsePlan after consuming order args, so
|
|
// it correctly points at LIMIT after the orderArgs are bound below.
|
|
args := append([]any{}, p.args...)
|
|
args = append(args, p.orderArgs...)
|
|
args = append(args, queryLimit, p.offset)
|
|
offsetArgIdx := p.limitArgIdx + 1
|
|
|
|
selectList := p.selectClause
|
|
sql := fmt.Sprintf(
|
|
"SELECT %s FROM %s %s %s %s LIMIT $%d OFFSET $%d",
|
|
selectList, p.fromClause, p.whereClause, p.groupByClause, p.orderBy,
|
|
p.limitArgIdx, offsetArgIdx,
|
|
)
|
|
return sql, args
|
|
}
|
|
|
|
// buildBrowsePlan assembles the WHERE clause, FROM clause, args, ORDER BY, and
|
|
// GROUP BY for a browse query. It performs no I/O. browse() uses it to run the
|
|
// actual query; tests use it to assert the emitted SQL. earlyEmpty == true
|
|
// means the filters are unsatisfiable (e.g. empty library allowlist) and the
|
|
// caller should return an empty result without executing.
|
|
func (r *BrowseRepository) buildBrowsePlan(filters BrowseFilters) (browseQueryPlan, bool, error) {
|
|
// Normalize defaults.
|
|
if filters.Limit <= 0 {
|
|
filters.Limit = 20
|
|
}
|
|
maxLimit := filters.MaxLimit
|
|
if maxLimit <= 0 {
|
|
maxLimit = 100
|
|
}
|
|
if filters.Limit > maxLimit {
|
|
filters.Limit = maxLimit
|
|
}
|
|
if filters.Offset < 0 {
|
|
filters.Offset = 0
|
|
}
|
|
if filters.Order == "" {
|
|
filters.Order = "desc"
|
|
}
|
|
if filters.Sort == "" {
|
|
filters.Sort = "created_at"
|
|
}
|
|
|
|
// Build WHERE clause and args.
|
|
var conditions []string
|
|
var args []any
|
|
argIdx := 1
|
|
|
|
// Type filter (supports comma-separated multi-type, e.g. "movie,series").
|
|
if filters.Type != "" {
|
|
types := splitTypes(filters.Type)
|
|
if len(types) == 1 {
|
|
conditions = append(conditions, fmt.Sprintf("mi.type = $%d", argIdx))
|
|
args = append(args, types[0])
|
|
argIdx++
|
|
} else {
|
|
conditions = append(conditions, fmt.Sprintf("mi.type = ANY($%d)", argIdx))
|
|
args = append(args, types)
|
|
argIdx++
|
|
}
|
|
}
|
|
|
|
if filters.ContentIDs != nil {
|
|
if len(filters.ContentIDs) == 0 {
|
|
return browseQueryPlan{}, true, nil
|
|
}
|
|
conditions = append(conditions, fmt.Sprintf("mi.content_id = ANY($%d)", argIdx))
|
|
args = append(args, filters.ContentIDs)
|
|
argIdx++
|
|
}
|
|
|
|
// Genre filter (GIN array containment).
|
|
if filters.Genre != "" {
|
|
conditions = append(conditions, fmt.Sprintf("mi.genres @> ARRAY[$%d]::text[]", argIdx))
|
|
args = append(args, filters.Genre)
|
|
argIdx++
|
|
}
|
|
|
|
if prefix := strings.TrimSpace(filters.NamePrefix); prefix != "" {
|
|
// Dual-column OR so titles without a curated sort_title still match.
|
|
// First arm matches the idx_media_items_sort_key expression
|
|
// (LOWER(COALESCE(NULLIF(BTRIM(sort_title),''), title))) so the
|
|
// anchored LIKE is sargable; second arm uses idx_media_items_search_exact_title
|
|
// (LOWER(title)). Both arms are equivalent when sort_title is empty,
|
|
// which is harmless — the planner can BitmapOr the two index scans.
|
|
conditions = append(conditions, fmt.Sprintf(
|
|
"(LOWER(COALESCE(NULLIF(BTRIM(mi.sort_title), ''), mi.title)) LIKE $%d ESCAPE '\\' OR LOWER(mi.title) LIKE $%d ESCAPE '\\')",
|
|
argIdx, argIdx,
|
|
))
|
|
args = append(args, likePrefixPattern(prefix))
|
|
argIdx++
|
|
}
|
|
|
|
// Year range filters.
|
|
if filters.YearMin > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("mi.year >= $%d", argIdx))
|
|
args = append(args, filters.YearMin)
|
|
argIdx++
|
|
}
|
|
if filters.YearMax > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("mi.year <= $%d", argIdx))
|
|
args = append(args, filters.YearMax)
|
|
argIdx++
|
|
}
|
|
|
|
// Content rating filter (multi-value).
|
|
if len(filters.ContentRating) > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("mi.content_rating = ANY($%d)", argIdx))
|
|
args = append(args, filters.ContentRating)
|
|
argIdx++
|
|
}
|
|
|
|
// Status filter.
|
|
if filters.Status != "" {
|
|
conditions = append(conditions, fmt.Sprintf("mi.status = $%d", argIdx))
|
|
args = append(args, filters.Status)
|
|
argIdx++
|
|
}
|
|
|
|
// Backdrop presence filter (Jellyfin ImageTypes=Backdrop). Pushed down so
|
|
// random/limited selections only ever pick items that actually have a
|
|
// backdrop — filtering after the LIMIT would wrongly return empty pages.
|
|
if filters.RequireBackdrop {
|
|
conditions = append(conditions, "NULLIF(BTRIM(mi.backdrop_path), '') IS NOT NULL")
|
|
}
|
|
|
|
// Library access control: restrict to user's accessible libraries.
|
|
needsLibJoin := filters.LibraryID > 0 || filters.LibraryIDs != nil || len(filters.DisabledLibraryIDs) > 0 || filters.Sort == "recently_added"
|
|
needsPersonJoin := filters.PersonID > 0
|
|
// When filtering by exactly one library and no person join, each
|
|
// content_id matches at most one mil row, so the GROUP BY usually added
|
|
// to dedup the junction join is unnecessary. Skipping it lets ORDER BY
|
|
// mil.first_seen_at exploit idx_item_libraries_folder_seen_content
|
|
// directly — turning a full-library scan + heapsort into a top-N index
|
|
// walk.
|
|
singleLibraryNoDedup := filters.LibraryID > 0 && filters.LibraryIDs == nil && len(filters.DisabledLibraryIDs) == 0 && !needsPersonJoin
|
|
|
|
if filters.PersonID > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("ip.person_id = $%d", argIdx))
|
|
args = append(args, filters.PersonID)
|
|
argIdx++
|
|
}
|
|
|
|
if filters.LibraryID > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("mil.media_folder_id = $%d", argIdx))
|
|
args = append(args, filters.LibraryID)
|
|
argIdx++
|
|
}
|
|
|
|
if filters.LibraryIDs != nil {
|
|
if len(filters.LibraryIDs) == 0 {
|
|
// User has empty library access — return no results.
|
|
return browseQueryPlan{}, true, nil
|
|
}
|
|
conditions = append(conditions, fmt.Sprintf("mil.media_folder_id = ANY($%d)", argIdx))
|
|
args = append(args, filters.LibraryIDs)
|
|
argIdx++
|
|
}
|
|
|
|
if len(filters.DisabledLibraryIDs) > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("NOT (mil.media_folder_id = ANY($%d))", argIdx))
|
|
args = append(args, filters.DisabledLibraryIDs)
|
|
argIdx++
|
|
}
|
|
|
|
applyAccessFilter("mi", AccessFilter{MaxContentRating: filters.MaxContentRating}, &conditions, &args, &argIdx)
|
|
|
|
if filters.SnapshotAt != nil {
|
|
conditions = append(conditions, fmt.Sprintf("mi.created_at <= $%d", argIdx))
|
|
args = append(args, *filters.SnapshotAt)
|
|
argIdx++
|
|
}
|
|
|
|
// Build FROM clause with optional JOIN.
|
|
fromClause := "media_items mi"
|
|
if needsLibJoin {
|
|
fromClause = "media_items mi JOIN media_item_libraries mil ON mi.content_id = mil.content_id"
|
|
}
|
|
if needsPersonJoin {
|
|
fromClause += " JOIN item_people ip ON ip.content_id = mi.content_id"
|
|
}
|
|
|
|
// Build WHERE string.
|
|
whereClause := ""
|
|
if len(conditions) > 0 {
|
|
whereClause = "WHERE " + strings.Join(conditions, " AND ")
|
|
}
|
|
|
|
// Build ORDER BY clause. orderArgs are kept out of `args` so countSQL can
|
|
// bind only its FROM/WHERE/GROUP BY values; pagedSQL binds them together.
|
|
// argIdx still advances past them so limitArgIdx points at the right
|
|
// LIMIT placeholder once orderArgs are bound positionally before LIMIT.
|
|
orderBy, orderArgs := buildOrderByPlan(filters.Sort, filters.Order, filters.SnapshotAt, argIdx, singleLibraryNoDedup, browseFiltersAreMovieOnly(filters))
|
|
argIdx += len(orderArgs)
|
|
|
|
selectClause := browseItemColumns("mi")
|
|
groupByClause := ""
|
|
switch {
|
|
case singleLibraryNoDedup:
|
|
// Single library, no person join: mil row is unique per content_id.
|
|
// Project mil.first_seen_at directly so ORDER BY can use the index.
|
|
selectClause += ", mil.first_seen_at"
|
|
case needsLibJoin:
|
|
// Multi-library or disabled-library filter: dedup with GROUP BY and
|
|
// expose the earliest first_seen_at as the "added at" timestamp.
|
|
selectClause += ", MIN(mil.first_seen_at)"
|
|
groupByClause = "GROUP BY " + browseGroupByColumns("mi")
|
|
case needsPersonJoin:
|
|
// Person join needs GROUP BY to deduplicate, but mil is not available.
|
|
selectClause += ", mi.created_at"
|
|
groupByClause = "GROUP BY " + browseGroupByColumns("mi")
|
|
default:
|
|
// No junction join — use created_at as the added_at fallback.
|
|
selectClause += ", mi.created_at"
|
|
}
|
|
|
|
return browseQueryPlan{
|
|
selectClause: selectClause,
|
|
fromClause: fromClause,
|
|
whereClause: whereClause,
|
|
groupByClause: groupByClause,
|
|
orderBy: orderBy,
|
|
args: args,
|
|
orderArgs: orderArgs,
|
|
limit: filters.Limit,
|
|
offset: filters.Offset,
|
|
limitArgIdx: argIdx,
|
|
}, false, nil
|
|
}
|
|
|
|
// filterWhereClause builds the common WHERE clause and FROM clause used by the
|
|
// ListXxx distinct-value methods. If the returned earlyEmpty flag is true the
|
|
// caller should return an empty result immediately (e.g. when LibraryIDs is an
|
|
// empty slice).
|
|
func filterWhereClause(filters BrowseFilters) (fromClause, whereClause string, args []any, earlyEmpty bool) {
|
|
return filterWhereClauseForSource(filters, "media_items mi", "")
|
|
}
|
|
|
|
func filterWhereClauseForSource(filters BrowseFilters, baseRelation string, mediaScope string) (fromClause, whereClause string, args []any, earlyEmpty bool) {
|
|
var conditions []string
|
|
argIdx := 1
|
|
|
|
if filters.Type != "" {
|
|
types := splitTypes(filters.Type)
|
|
if len(types) == 1 {
|
|
conditions = append(conditions, fmt.Sprintf("mi.type = $%d", argIdx))
|
|
args = append(args, types[0])
|
|
argIdx++
|
|
} else {
|
|
conditions = append(conditions, fmt.Sprintf("mi.type = ANY($%d)", argIdx))
|
|
args = append(args, types)
|
|
argIdx++
|
|
}
|
|
}
|
|
if prefix := strings.TrimSpace(filters.NamePrefix); prefix != "" {
|
|
// Same dual-column shape as filterWhereClauseForSource's primary
|
|
// browse path — see comment there for index-alignment rationale.
|
|
conditions = append(conditions, fmt.Sprintf(
|
|
"(LOWER(COALESCE(NULLIF(BTRIM(mi.sort_title), ''), mi.title)) LIKE $%d ESCAPE '\\' OR LOWER(mi.title) LIKE $%d ESCAPE '\\')",
|
|
argIdx, argIdx,
|
|
))
|
|
args = append(args, likePrefixPattern(prefix))
|
|
argIdx++
|
|
}
|
|
if filters.Status != "" {
|
|
conditions = append(conditions, fmt.Sprintf("mi.status = $%d", argIdx))
|
|
args = append(args, filters.Status)
|
|
argIdx++
|
|
}
|
|
if filters.PersonID > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("ip.person_id = $%d", argIdx))
|
|
args = append(args, filters.PersonID)
|
|
argIdx++
|
|
}
|
|
if filters.LibraryID > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("mil.media_folder_id = $%d", argIdx))
|
|
args = append(args, filters.LibraryID)
|
|
argIdx++
|
|
}
|
|
if filters.ContentIDs != nil {
|
|
if len(filters.ContentIDs) == 0 {
|
|
return "", "", nil, true
|
|
}
|
|
conditions = append(conditions, fmt.Sprintf("mi.content_id = ANY($%d)", argIdx))
|
|
args = append(args, filters.ContentIDs)
|
|
argIdx++
|
|
}
|
|
if filters.LibraryIDs != nil {
|
|
if len(filters.LibraryIDs) == 0 {
|
|
return "", "", nil, true
|
|
}
|
|
conditions = append(conditions, fmt.Sprintf("mil.media_folder_id = ANY($%d)", argIdx))
|
|
args = append(args, filters.LibraryIDs)
|
|
argIdx++
|
|
}
|
|
|
|
if len(filters.DisabledLibraryIDs) > 0 {
|
|
conditions = append(conditions, fmt.Sprintf("NOT (mil.media_folder_id = ANY($%d))", argIdx))
|
|
args = append(args, filters.DisabledLibraryIDs)
|
|
argIdx++
|
|
}
|
|
|
|
applyAccessFilter("mi", AccessFilter{MaxContentRating: filters.MaxContentRating}, &conditions, &args, &argIdx)
|
|
|
|
fromClause = baseRelation
|
|
libraryContentExpr := catalogLibraryContentExprForScope(mediaScope, "mi")
|
|
if filters.LibraryID > 0 || filters.LibraryIDs != nil || len(filters.DisabledLibraryIDs) > 0 {
|
|
membershipTable, membershipKey := catalogLibraryMembershipTableAndKeyForScope(mediaScope)
|
|
fromClause += " JOIN " + membershipTable + " mil ON " + libraryContentExpr + " = mil." + membershipKey
|
|
}
|
|
if filters.PersonID > 0 {
|
|
fromClause += " JOIN item_people ip ON ip.content_id = mi.content_id"
|
|
}
|
|
|
|
if len(conditions) > 0 {
|
|
whereClause = "WHERE " + strings.Join(conditions, " AND ")
|
|
}
|
|
return fromClause, whereClause, args, false
|
|
}
|
|
|
|
// listDistinctArrayColumn returns sorted distinct non-empty values from an
|
|
// array column (genres, studios, networks, countries).
|
|
func (r *BrowseRepository) listDistinctArrayColumn(ctx context.Context, column string, filters BrowseFilters) ([]string, error) {
|
|
return listDistinctArrayColumnWithSource(ctx, r.pool, column, filters, "media_items mi", "")
|
|
}
|
|
|
|
func listDistinctArrayColumnWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
column string,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, nil
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT val
|
|
FROM (
|
|
SELECT UNNEST(mi.%s) AS val
|
|
FROM %s
|
|
%s
|
|
) vals
|
|
WHERE val <> ''
|
|
ORDER BY val ASC
|
|
LIMIT %d
|
|
`, column, fromClause, whereClause, catalogFacetMaxValues)
|
|
|
|
rows, err := pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing %s: %w", column, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
values := make([]string, 0)
|
|
for rows.Next() {
|
|
var v string
|
|
if err := rows.Scan(&v); err != nil {
|
|
return nil, fmt.Errorf("scan %s: %w", column, err)
|
|
}
|
|
v = strings.TrimSpace(v)
|
|
if v == "" {
|
|
continue
|
|
}
|
|
values = append(values, v)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate %s: %w", column, err)
|
|
}
|
|
|
|
slices.Sort(values)
|
|
return values, nil
|
|
}
|
|
|
|
// listDistinctScalarColumn returns sorted distinct non-empty values from a
|
|
// scalar text column (e.g. content_rating).
|
|
func (r *BrowseRepository) listDistinctScalarColumn(ctx context.Context, column string, filters BrowseFilters) ([]string, error) {
|
|
return listDistinctScalarColumnWithSource(ctx, r.pool, column, filters, "media_items mi", "")
|
|
}
|
|
|
|
func listDistinctScalarColumnWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
column string,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, nil
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT mi.%s
|
|
FROM %s
|
|
%s
|
|
AND mi.%s <> ''
|
|
ORDER BY mi.%s ASC
|
|
LIMIT %d
|
|
`, column, fromClause, whereClause, column, column, catalogFacetMaxValues)
|
|
|
|
// When there are no WHERE conditions, the extra AND is invalid — prepend WHERE instead.
|
|
if whereClause == "" {
|
|
query = fmt.Sprintf(`
|
|
SELECT DISTINCT mi.%s
|
|
FROM %s
|
|
WHERE mi.%s <> ''
|
|
ORDER BY mi.%s ASC
|
|
LIMIT %d
|
|
`, column, fromClause, column, column, catalogFacetMaxValues)
|
|
}
|
|
|
|
rows, err := pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("listing %s: %w", column, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
values := make([]string, 0)
|
|
for rows.Next() {
|
|
var v string
|
|
if err := rows.Scan(&v); err != nil {
|
|
return nil, fmt.Errorf("scan %s: %w", column, err)
|
|
}
|
|
v = strings.TrimSpace(v)
|
|
if v == "" {
|
|
continue
|
|
}
|
|
values = append(values, v)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate %s: %w", column, err)
|
|
}
|
|
|
|
slices.Sort(values)
|
|
return values, nil
|
|
}
|
|
|
|
// ListGenres returns the distinct genres matching the supplied browse filters.
|
|
func (r *BrowseRepository) ListGenres(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctArrayColumn(ctx, "genres", filters)
|
|
}
|
|
|
|
// ListStudios returns the distinct studios matching the supplied browse filters.
|
|
func (r *BrowseRepository) ListStudios(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctArrayColumn(ctx, "studios", filters)
|
|
}
|
|
|
|
// ListNetworks returns the distinct networks matching the supplied browse filters.
|
|
func (r *BrowseRepository) ListNetworks(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctArrayColumn(ctx, "networks", filters)
|
|
}
|
|
|
|
// ListCountries returns the distinct countries matching the supplied browse filters.
|
|
func (r *BrowseRepository) ListCountries(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctArrayColumn(ctx, "countries", filters)
|
|
}
|
|
|
|
// ListContentRatings returns the distinct content ratings matching the supplied browse filters.
|
|
func (r *BrowseRepository) ListContentRatings(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctScalarColumn(ctx, "content_rating", filters)
|
|
}
|
|
|
|
func (r *BrowseRepository) ListOriginalLanguages(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctScalarColumn(ctx, "original_language", filters)
|
|
}
|
|
|
|
func (r *BrowseRepository) ListResolutions(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return listResolutionsWithSource(ctx, r.pool, filters, "media_items mi", "")
|
|
}
|
|
|
|
func listResolutionsWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, earlyEmpty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if earlyEmpty {
|
|
return nil, nil
|
|
}
|
|
mediaFileJoin := catalogMediaFileJoinConditionForScope(mediaScope, "mf", "mi")
|
|
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT mf.resolution
|
|
FROM %s
|
|
JOIN media_files mf ON %s
|
|
%s
|
|
AND mf.missing_since IS NULL
|
|
AND mf.resolution IS NOT NULL
|
|
AND BTRIM(mf.resolution) <> ''
|
|
ORDER BY mf.resolution ASC
|
|
`, fromClause, mediaFileJoin, browseFilterPrefix(whereClause))
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
func (r *BrowseRepository) ListAudioLanguages(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return r.listDistinctJSONBLanguageWithFilters(ctx, "audio_tracks", filters)
|
|
}
|
|
|
|
func (r *BrowseRepository) ListSubtitleLanguages(ctx context.Context, filters BrowseFilters) ([]string, error) {
|
|
return listSubtitleLanguagesWithSource(ctx, r.pool, filters, "media_items mi", "")
|
|
}
|
|
|
|
func listSubtitleLanguagesWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, earlyEmpty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if earlyEmpty {
|
|
return nil, nil
|
|
}
|
|
mediaFileJoin := catalogMediaFileJoinConditionForScope(mediaScope, "mf", "mi")
|
|
|
|
// Embedded subtitles use the migration 104 generated text[]
|
|
// `subtitle_language_codes`; external subs still need a JSONB unnest
|
|
// because the generated column only covers subtitle_tracks
|
|
// (audit 2026-05-01 §2.5b).
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT value
|
|
FROM (
|
|
SELECT lang AS value
|
|
FROM %s
|
|
JOIN media_files mf ON %s
|
|
CROSS JOIN LATERAL UNNEST(mf.subtitle_language_codes) AS lang
|
|
%s
|
|
AND mf.missing_since IS NULL
|
|
|
|
UNION
|
|
|
|
SELECT LOWER(COALESCE(track->>'language', '')) AS value
|
|
FROM %s
|
|
JOIN media_files mf ON %s
|
|
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(mf.external_subtitles, '[]'::jsonb)) AS track
|
|
%s
|
|
AND mf.missing_since IS NULL
|
|
) languages
|
|
WHERE value IS NOT NULL AND value <> ''
|
|
ORDER BY value ASC
|
|
`, fromClause, mediaFileJoin, browseFilterPrefix(whereClause), fromClause, mediaFileJoin, browseFilterPrefix(whereClause))
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
func (r *BrowseRepository) listDistinctJSONBLanguageWithFilters(ctx context.Context, column string, filters BrowseFilters) ([]string, error) {
|
|
return listDistinctJSONBLanguageWithSource(ctx, r.pool, column, filters, "media_items mi", "")
|
|
}
|
|
|
|
// listDistinctPeopleByKindWithSource returns distinct people.name values for a
|
|
// PersonKind across the scoped result set. Powers the Authors and Narrators
|
|
// facets — both share item_people, just keyed by `kind`. The from/where
|
|
// clauses from filterWhereClauseForSource gate by library / access / scope,
|
|
// so an audiobook-only library or audiobook media_scope drops video-only
|
|
// content automatically.
|
|
func listDistinctPeopleByKindWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
kind models.PersonKind,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, earlyEmpty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if earlyEmpty {
|
|
return []string{}, nil
|
|
}
|
|
args = append(args, int(kind))
|
|
kindIdx := len(args)
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT p.name
|
|
FROM %s
|
|
JOIN item_people ip ON ip.content_id = mi.content_id AND ip.kind = $%d
|
|
JOIN people p ON p.id = ip.person_id
|
|
%s
|
|
AND p.name IS NOT NULL
|
|
AND BTRIM(p.name) <> ''
|
|
ORDER BY p.name ASC
|
|
LIMIT %d
|
|
`, fromClause, kindIdx, browseFilterPrefix(whereClause), catalogFacetMaxValues)
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
// listDistinctAudiobookSeriesWithSource returns distinct series_name values
|
|
// from the active book series table joined onto the scoped result set. Names are trimmed
|
|
// and case-folded for sort so the picker doesn't show duplicates that differ
|
|
// only by whitespace or casing; the literal trimmed series_name is still
|
|
// returned so existing rules continue to match.
|
|
//
|
|
// The DISTINCT happens in an inline subquery (rather than directly on the
|
|
// outer SELECT) so the outer ORDER BY can apply LOWER(...) without violating
|
|
// Postgres' "ORDER BY expressions must appear in select list" rule for
|
|
// SELECT DISTINCT.
|
|
func listDistinctAudiobookSeriesWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, earlyEmpty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if earlyEmpty {
|
|
return []string{}, nil
|
|
}
|
|
query := fmt.Sprintf(`
|
|
SELECT name FROM (
|
|
SELECT DISTINCT BTRIM(s.series_name) AS name
|
|
FROM %s
|
|
JOIN %s s ON s.content_id = mi.content_id
|
|
%s
|
|
AND s.series_name IS NOT NULL
|
|
AND BTRIM(s.series_name) <> ''
|
|
) names
|
|
ORDER BY LOWER(name) ASC
|
|
LIMIT %d
|
|
`, fromClause, bookSeriesTableForMediaScope(mediaScope), browseFilterPrefix(whereClause), catalogFacetMaxValues)
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
// searchDistinctArrayColumnWithSource prefix-searches the distinct values
|
|
// of an array column (genres, studios, networks, countries) within the
|
|
// scoped result set. Returns up to limit matches in alphabetical order
|
|
// plus a hasMore flag (true when the underlying result set held more
|
|
// rows than limit).
|
|
func searchDistinctArrayColumnWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
column string,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
prefix string,
|
|
limit int,
|
|
) ([]string, bool, error) {
|
|
prefix = strings.TrimSpace(prefix)
|
|
if limit <= 0 {
|
|
return []string{}, false, nil
|
|
}
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, false, nil
|
|
}
|
|
args = append(args, prefix+"%")
|
|
prefixIdx := len(args)
|
|
// LOWER() in ORDER BY: this query is already wrapped in a subquery
|
|
// (the DISTINCT UNNEST), so the outer ORDER BY can reference any
|
|
// expression freely.
|
|
query := fmt.Sprintf(`
|
|
SELECT name FROM (
|
|
SELECT DISTINCT UNNEST(mi.%s) AS name
|
|
FROM %s
|
|
%s
|
|
) vals
|
|
WHERE name IS NOT NULL
|
|
AND BTRIM(name) <> ''
|
|
AND LOWER(name) LIKE LOWER($%d)
|
|
ORDER BY LOWER(name) ASC
|
|
LIMIT %d
|
|
`, column, fromClause, whereClause, prefixIdx, limit+1)
|
|
return queryFacetSearchResults(ctx, pool, query, args, limit)
|
|
}
|
|
|
|
// searchDistinctScalarColumnWithSource is the scalar (e.g. content_rating)
|
|
// variant of searchDistinctArrayColumnWithSource.
|
|
func searchDistinctScalarColumnWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
column string,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
prefix string,
|
|
limit int,
|
|
) ([]string, bool, error) {
|
|
prefix = strings.TrimSpace(prefix)
|
|
if limit <= 0 {
|
|
return []string{}, false, nil
|
|
}
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, false, nil
|
|
}
|
|
args = append(args, prefix+"%")
|
|
prefixIdx := len(args)
|
|
// DISTINCT in an inline subquery so the outer ORDER BY can apply
|
|
// LOWER() without violating the SELECT DISTINCT rule.
|
|
query := fmt.Sprintf(`
|
|
SELECT name FROM (
|
|
SELECT DISTINCT mi.%s AS name
|
|
FROM %s
|
|
%s
|
|
AND mi.%s IS NOT NULL
|
|
AND BTRIM(mi.%s) <> ''
|
|
AND LOWER(mi.%s) LIKE LOWER($%d)
|
|
) matches
|
|
ORDER BY LOWER(name) ASC
|
|
LIMIT %d
|
|
`, column, fromClause, browseFilterPrefix(whereClause), column, column, column, prefixIdx, limit+1)
|
|
return queryFacetSearchResults(ctx, pool, query, args, limit)
|
|
}
|
|
|
|
// searchDistinctPeopleByKindWithSource is the typeahead equivalent of
|
|
// listDistinctPeopleByKindWithSource; powers /api/v1/catalog/filters/search
|
|
// for facet=author and facet=narrator.
|
|
func searchDistinctPeopleByKindWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
kind models.PersonKind,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
prefix string,
|
|
limit int,
|
|
) ([]string, bool, error) {
|
|
prefix = strings.TrimSpace(prefix)
|
|
if limit <= 0 {
|
|
return []string{}, false, nil
|
|
}
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, false, nil
|
|
}
|
|
args = append(args, int(kind))
|
|
kindIdx := len(args)
|
|
args = append(args, prefix+"%")
|
|
prefixIdx := len(args)
|
|
// DISTINCT in an inline subquery so the outer ORDER BY can apply
|
|
// LOWER() without violating Postgres' "ORDER BY expressions must
|
|
// appear in select list" rule for SELECT DISTINCT.
|
|
query := fmt.Sprintf(`
|
|
SELECT name FROM (
|
|
SELECT DISTINCT p.name AS name
|
|
FROM %s
|
|
JOIN item_people ip ON ip.content_id = mi.content_id AND ip.kind = $%d
|
|
JOIN people p ON p.id = ip.person_id
|
|
%s
|
|
AND p.name IS NOT NULL
|
|
AND BTRIM(p.name) <> ''
|
|
AND LOWER(p.name) LIKE LOWER($%d)
|
|
) matches
|
|
ORDER BY LOWER(name) ASC
|
|
LIMIT %d
|
|
`, fromClause, kindIdx, browseFilterPrefix(whereClause), prefixIdx, limit+1)
|
|
return queryFacetSearchResults(ctx, pool, query, args, limit)
|
|
}
|
|
|
|
// searchDistinctAudiobookSeriesWithSource is the typeahead equivalent of
|
|
// listDistinctAudiobookSeriesWithSource.
|
|
func searchDistinctAudiobookSeriesWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
prefix string,
|
|
limit int,
|
|
) ([]string, bool, error) {
|
|
prefix = strings.TrimSpace(prefix)
|
|
if limit <= 0 {
|
|
return []string{}, false, nil
|
|
}
|
|
fromClause, whereClause, args, empty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if empty {
|
|
return []string{}, false, nil
|
|
}
|
|
args = append(args, prefix+"%")
|
|
prefixIdx := len(args)
|
|
query := fmt.Sprintf(`
|
|
SELECT name FROM (
|
|
SELECT DISTINCT BTRIM(s.series_name) AS name
|
|
FROM %s
|
|
JOIN %s s ON s.content_id = mi.content_id
|
|
%s
|
|
AND s.series_name IS NOT NULL
|
|
AND BTRIM(s.series_name) <> ''
|
|
AND LOWER(BTRIM(s.series_name)) LIKE LOWER($%d)
|
|
) names
|
|
ORDER BY LOWER(name) ASC
|
|
LIMIT %d
|
|
`, fromClause, bookSeriesTableForMediaScope(mediaScope), browseFilterPrefix(whereClause), prefixIdx, limit+1)
|
|
return queryFacetSearchResults(ctx, pool, query, args, limit)
|
|
}
|
|
|
|
func bookSeriesTableForMediaScope(mediaScope string) string {
|
|
if mediaScope == "ebook" {
|
|
return "ebook_series"
|
|
}
|
|
return "audiobook_series"
|
|
}
|
|
|
|
// queryFacetSearchResults executes a facet search query that was built
|
|
// with LIMIT N+1 and returns the first N rows plus a hasMore flag
|
|
// indicating whether an additional row was present (i.e. the result set
|
|
// exceeded the requested limit).
|
|
func queryFacetSearchResults(ctx context.Context, pool *pgxpool.Pool, query string, args []any, limit int) ([]string, bool, error) {
|
|
rows, err := pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer rows.Close()
|
|
values := make([]string, 0, limit)
|
|
hasMore := false
|
|
for rows.Next() {
|
|
var value string
|
|
if err := rows.Scan(&value); err != nil {
|
|
return nil, false, err
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
continue
|
|
}
|
|
if len(values) >= limit {
|
|
hasMore = true
|
|
continue
|
|
}
|
|
values = append(values, value)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, false, err
|
|
}
|
|
return values, hasMore, nil
|
|
}
|
|
|
|
func listDistinctJSONBLanguageWithSource(
|
|
ctx context.Context,
|
|
pool *pgxpool.Pool,
|
|
column string,
|
|
filters BrowseFilters,
|
|
baseRelation string,
|
|
mediaScope string,
|
|
) ([]string, error) {
|
|
fromClause, whereClause, args, earlyEmpty := filterWhereClauseForSource(filters, baseRelation, mediaScope)
|
|
if earlyEmpty {
|
|
return nil, nil
|
|
}
|
|
mediaFileJoin := catalogMediaFileJoinConditionForScope(mediaScope, "mf", "mi")
|
|
|
|
// Migration 104 added STORED text[] generated columns derived from the
|
|
// JSONB tracks. Use them when available so this listing UNNESTs an
|
|
// indexed array instead of parsing JSONB per row
|
|
// (audit 2026-05-01 §2.5b).
|
|
arrayColumn := generatedLanguageColumnFor(column)
|
|
if arrayColumn != "" {
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT lang AS value
|
|
FROM %s
|
|
JOIN media_files mf ON %s
|
|
CROSS JOIN LATERAL UNNEST(mf.%s) AS lang
|
|
%s
|
|
AND mf.missing_since IS NULL
|
|
AND lang IS NOT NULL
|
|
AND lang <> ''
|
|
ORDER BY value ASC
|
|
LIMIT %d
|
|
`, fromClause, mediaFileJoin, arrayColumn, browseFilterPrefix(whereClause), catalogFacetMaxValues)
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
SELECT DISTINCT LOWER(COALESCE(track->>'language', '')) AS value
|
|
FROM %s
|
|
JOIN media_files mf ON %s
|
|
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(mf.%s, '[]'::jsonb)) AS track
|
|
%s
|
|
AND mf.missing_since IS NULL
|
|
AND COALESCE(track->>'language', '') <> ''
|
|
ORDER BY value ASC
|
|
LIMIT %d
|
|
`, fromClause, mediaFileJoin, column, browseFilterPrefix(whereClause), catalogFacetMaxValues)
|
|
return queryDistinctStrings(ctx, pool, query, args)
|
|
}
|
|
|
|
// generatedLanguageColumnFor returns the migration-104 STORED text[] column
|
|
// that mirrors the given JSONB tracks column, or "" if none exists.
|
|
func generatedLanguageColumnFor(jsonbColumn string) string {
|
|
switch jsonbColumn {
|
|
case "audio_tracks":
|
|
return "audio_language_codes"
|
|
case "subtitle_tracks":
|
|
return "subtitle_language_codes"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func browseFilterPrefix(whereClause string) string {
|
|
if whereClause == "" {
|
|
return "WHERE TRUE"
|
|
}
|
|
return whereClause
|
|
}
|
|
|
|
func queryDistinctStrings(ctx context.Context, pool *pgxpool.Pool, query string, args []any) ([]string, error) {
|
|
rows, err := pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
values := make([]string, 0)
|
|
for rows.Next() {
|
|
var value string
|
|
if err := rows.Scan(&value); err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.TrimSpace(value) == "" {
|
|
continue
|
|
}
|
|
values = append(values, value)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return values, nil
|
|
}
|
|
|
|
// browseItemColumns returns the item columns prefixed with the given alias.
|
|
func browseItemColumns(alias string) string {
|
|
cols := []string{
|
|
"content_id", "type", "title", "sort_title", "original_title", "year", "genres",
|
|
"content_rating", "runtime", "overview", "tagline",
|
|
"rating_imdb", "rating_tmdb", "rating_rt_critic", "rating_rt_audience",
|
|
"imdb_id", "tmdb_id", "tvdb_id",
|
|
"poster_path", "poster_thumbhash", "backdrop_path", "backdrop_thumbhash", "logo_path",
|
|
"metadata_s3_path", "metadata_etag", "season_count",
|
|
"studios", "networks", "countries", "keywords", "original_language", "release_date::text", "first_air_date", "last_air_date",
|
|
"show_status",
|
|
"matched_at", "episode_metadata_incomplete", "episode_metadata_last_checked_at", "status", "created_at", "updated_at",
|
|
}
|
|
prefixed := make([]string, len(cols))
|
|
for i, col := range cols {
|
|
if col == "last_air_date" {
|
|
prefixed[i] = effectiveLastAirDateExpr(alias)
|
|
continue
|
|
}
|
|
prefixed[i] = alias + "." + col
|
|
}
|
|
return strings.Join(prefixed, ", ")
|
|
}
|
|
|
|
// browseGroupByColumns returns the columns needed for GROUP BY when joining
|
|
// with the junction table.
|
|
func browseGroupByColumns(alias string) string {
|
|
cols := []string{
|
|
"content_id", "type", "title", "sort_title", "original_title", "year", "genres",
|
|
"content_rating", "runtime", "overview", "tagline",
|
|
"rating_imdb", "rating_tmdb", "rating_rt_critic", "rating_rt_audience",
|
|
"imdb_id", "tmdb_id", "tvdb_id",
|
|
"poster_path", "poster_thumbhash", "backdrop_path", "backdrop_thumbhash", "logo_path",
|
|
"metadata_s3_path", "metadata_etag", "season_count",
|
|
"studios", "networks", "countries", "keywords", "original_language", "release_date::text", "first_air_date", "last_air_date",
|
|
"show_status",
|
|
"matched_at", "episode_metadata_incomplete", "episode_metadata_last_checked_at", "status", "created_at", "updated_at",
|
|
}
|
|
prefixed := make([]string, len(cols))
|
|
for i, col := range cols {
|
|
prefixed[i] = alias + "." + col
|
|
}
|
|
return strings.Join(prefixed, ", ")
|
|
}
|
|
|
|
// likePrefixPattern returns prefix lowercased and LIKE-escaped (%, _, \) with
|
|
// a trailing % so it forms a prefix-anchored LIKE pattern. Thin wrapper over
|
|
// escapePrefixForLike so any future special-character fix applies to both
|
|
// the browse path and the query_executor preview path.
|
|
func likePrefixPattern(prefix string) string {
|
|
return escapePrefixForLike(prefix) + "%"
|
|
}
|
|
|
|
// scanBrowseItems scans rows returned by the browse query, which include an
|
|
// extra added_at column appended after the standard item columns.
|
|
func scanBrowseItems(rows pgx.Rows) ([]*models.MediaItem, error) {
|
|
var items []*models.MediaItem
|
|
for rows.Next() {
|
|
var item models.MediaItem
|
|
err := rows.Scan(
|
|
&item.ContentID,
|
|
&item.Type,
|
|
&item.Title,
|
|
&item.SortTitle,
|
|
&item.OriginalTitle,
|
|
&item.Year,
|
|
&item.Genres,
|
|
&item.ContentRating,
|
|
&item.Runtime,
|
|
&item.Overview,
|
|
&item.Tagline,
|
|
&item.RatingIMDB,
|
|
&item.RatingTMDB,
|
|
&item.RatingRTCritic,
|
|
&item.RatingRTAudience,
|
|
&item.ImdbID,
|
|
&item.TmdbID,
|
|
&item.TvdbID,
|
|
&item.PosterPath,
|
|
&item.PosterThumbhash,
|
|
&item.BackdropPath,
|
|
&item.BackdropThumbhash,
|
|
&item.LogoPath,
|
|
&item.MetadataS3Path,
|
|
&item.MetadataEtag,
|
|
&item.SeasonCount,
|
|
&item.Studios,
|
|
&item.Networks,
|
|
&item.Countries,
|
|
&item.Keywords,
|
|
&item.OriginalLanguage,
|
|
&item.ReleaseDate,
|
|
&item.FirstAirDate,
|
|
&item.LastAirDate,
|
|
&item.ShowStatus,
|
|
&item.MatchedAt,
|
|
&item.EpisodeMetadataIncomplete,
|
|
&item.EpisodeMetadataLastCheckedAt,
|
|
&item.Status,
|
|
&item.CreatedAt,
|
|
&item.UpdatedAt,
|
|
&item.AddedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scanning browse item row: %w", err)
|
|
}
|
|
items = append(items, &item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterating browse item rows: %w", err)
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
// buildOrderBy constructs the ORDER BY clause from sort and order parameters.
|
|
func buildOrderBy(sort, order string) string {
|
|
clause, _ := buildOrderByPlan(sort, order, nil, 0, false, false)
|
|
return clause
|
|
}
|
|
|
|
func buildOrderByPlan(sort, order string, snapshot *time.Time, argIdx int, singleLibraryNoDedup bool, movieOnly bool) (string, []any) {
|
|
direction := "DESC"
|
|
if strings.EqualFold(order, "asc") {
|
|
direction = "ASC"
|
|
}
|
|
|
|
nullsClause := ""
|
|
if direction == "DESC" {
|
|
nullsClause = " NULLS LAST"
|
|
}
|
|
|
|
// Every case includes mi.content_id as a final tiebreaker so that
|
|
// OFFSET-based pagination is deterministic when many rows share the
|
|
// same sort value (e.g. same IMDB rating or NULL).
|
|
switch sort {
|
|
case "random":
|
|
if snapshot != nil && argIdx > 0 {
|
|
return fmt.Sprintf("ORDER BY md5(mi.content_id || $%d::text) ASC, mi.content_id ASC", argIdx), []any{snapshot.UTC().Format(time.RFC3339Nano)}
|
|
}
|
|
return "ORDER BY RANDOM()", nil
|
|
case "release_date":
|
|
if movieOnly {
|
|
return fmt.Sprintf("ORDER BY mi.release_date %s%s, mi.content_id ASC", direction, nullsClause), nil
|
|
}
|
|
return fmt.Sprintf(
|
|
"ORDER BY COALESCE(mi.release_date::text, NULLIF(BTRIM(mi.first_air_date), '')) %s%s, mi.content_id ASC",
|
|
direction,
|
|
nullsClause,
|
|
), nil
|
|
case "last_air_date":
|
|
return fmt.Sprintf(
|
|
"ORDER BY %s %s%s, mi.content_id ASC",
|
|
effectiveLastAirDateExpr("mi"),
|
|
direction,
|
|
nullsClause,
|
|
), nil
|
|
case "rating_imdb":
|
|
return fmt.Sprintf("ORDER BY mi.rating_imdb %s%s, mi.content_id ASC", direction, nullsClause), nil
|
|
case "rating_tmdb":
|
|
return fmt.Sprintf("ORDER BY mi.rating_tmdb %s%s, mi.content_id ASC", direction, nullsClause), nil
|
|
case "year":
|
|
return fmt.Sprintf("ORDER BY mi.year %s, mi.content_id ASC", direction), nil
|
|
case "title", "sort_title":
|
|
return fmt.Sprintf(
|
|
"ORDER BY LOWER(COALESCE(NULLIF(BTRIM(mi.sort_title), ''), mi.title)) %s, LOWER(mi.title) %s, mi.content_id ASC",
|
|
direction,
|
|
direction,
|
|
), nil
|
|
case "recently_added":
|
|
// Without GROUP BY (single-library filter), reference mil.first_seen_at
|
|
// directly so the planner can drive the order from
|
|
// idx_item_libraries_folder_seen_content instead of materializing
|
|
// every library row to aggregate.
|
|
if singleLibraryNoDedup {
|
|
return fmt.Sprintf("ORDER BY mil.first_seen_at %s, mi.content_id ASC", direction), nil
|
|
}
|
|
return fmt.Sprintf("ORDER BY MIN(mil.first_seen_at) %s, mi.content_id ASC", direction), nil
|
|
case "created_at":
|
|
return fmt.Sprintf("ORDER BY mi.created_at %s, mi.content_id ASC", direction), nil
|
|
default:
|
|
return fmt.Sprintf("ORDER BY mi.created_at %s, mi.content_id ASC", direction), nil
|
|
}
|
|
}
|
|
|
|
func browseFiltersAreMovieOnly(filters BrowseFilters) bool {
|
|
types := splitTypes(filters.Type)
|
|
return len(types) == 1 && types[0] == "movie"
|
|
}
|
|
|
|
// ParseContentRatings splits a comma-separated content rating string into
|
|
// a slice. Empty input returns nil.
|
|
func ParseContentRatings(s string) []string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(s, ",")
|
|
ratings := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
trimmed := strings.TrimSpace(p)
|
|
if trimmed != "" {
|
|
ratings = append(ratings, trimmed)
|
|
}
|
|
}
|
|
if len(ratings) == 0 {
|
|
return nil
|
|
}
|
|
return ratings
|
|
}
|
|
|
|
// ParseIntParam parses a string as int, returning 0 if empty or invalid.
|
|
func ParseIntParam(s string) int {
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
v, _ := strconv.Atoi(s)
|
|
return v
|
|
}
|
|
|
|
// ParseInt64Param parses a string to int64, returning 0 if empty or invalid.
|
|
func ParseInt64Param(s string) int64 {
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
v, _ := strconv.ParseInt(s, 10, 64)
|
|
return v
|
|
}
|
|
|
|
// splitTypes splits a comma-separated type string into trimmed, non-empty parts.
|
|
func splitTypes(s string) []string {
|
|
parts := strings.Split(s, ",")
|
|
result := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
if t := strings.TrimSpace(p); t != "" {
|
|
result = append(result, t)
|
|
}
|
|
}
|
|
return result
|
|
}
|