Files
silo-server/internal/catalog/query_builder.go
T
8b70357703 feat(ebooks): first-class ebook libraries, scanner, and reader (#124)
* 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>
2026-06-10 08:18:35 -04:00

1761 lines
52 KiB
Go

package catalog
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/Silo-Server/silo-server/internal/access"
"github.com/Silo-Server/silo-server/internal/models"
)
// EbookFinishedProgressThresholdSQL is models.EbookFinishedProgressThreshold
// rendered as a SQL numeric literal. Queries that classify ebook reader
// progress as finished must interpolate this value instead of hardcoding the
// threshold so the definition of "finished" cannot drift between subsystems.
var EbookFinishedProgressThresholdSQL = strconv.FormatFloat(models.EbookFinishedProgressThreshold, 'f', -1, 64)
type QueryBuilder struct {
alias string
argIdx int
args []any
userID int
profileID string
libraryIDs []int
mediaScope string
// requireUserHistoryCTE is set when an emitted clause references the
// per-user history aggregate (audit 2026-05-01 §3.1 Pattern B). The
// executor reads this flag to inject the user_last_watched CTE and
// the LEFT JOIN aliased as uhist before running the query.
requireUserHistoryCTE bool
}
type QuerySortPlan struct {
Joins []string
OrderBy string
Args []any
}
func NewQueryBuilder(alias string) *QueryBuilder {
return &QueryBuilder{alias: alias, argIdx: 1}
}
func (qb *QueryBuilder) WithUserScope(userID int, profileID string) *QueryBuilder {
qb.userID = userID
qb.profileID = profileID
return qb
}
func (qb *QueryBuilder) WithLibraryScope(libraryIDs []int) *QueryBuilder {
qb.libraryIDs = append([]int(nil), libraryIDs...)
return qb
}
func (qb *QueryBuilder) WithMediaScope(scope string) *QueryBuilder {
qb.mediaScope = strings.TrimSpace(scope)
return qb
}
func (qb *QueryBuilder) WithArgIdx(argIdx int) *QueryBuilder {
if argIdx > 0 {
qb.argIdx = argIdx
}
return qb
}
func (qb *QueryBuilder) Build(input any) (string, []any, error) {
normalized, err := normalizeBuilderInput(input)
if err != nil {
return "", nil, err
}
if err := normalized.Validate(); err != nil {
return "", nil, err
}
qb.argIdx = 1
qb.args = nil
if len(normalized.Groups) == 0 {
return "", nil, nil
}
topJoiner := " AND "
if normalized.Match == "any" {
topJoiner = " OR "
}
var groupClauses []string
for _, group := range normalized.Groups {
clause, err := qb.buildGroup(group)
if err != nil {
return "", nil, err
}
if clause != "" {
groupClauses = append(groupClauses, clause)
}
}
if len(groupClauses) == 0 {
return "", nil, nil
}
if len(groupClauses) == 1 {
return groupClauses[0], qb.args, nil
}
wrapped := make([]string, len(groupClauses))
for i, clause := range groupClauses {
wrapped[i] = "(" + clause + ")"
}
return strings.Join(wrapped, topJoiner), qb.args, nil
}
func normalizeBuilderInput(input any) (QueryDefinition, error) {
switch def := input.(type) {
case QueryDefinition:
return def.Normalize(), nil
case *QueryDefinition:
if def == nil {
return QueryDefinition{}.Normalize(), nil
}
return def.Normalize(), nil
}
var legacy struct {
Match string `json:"match"`
Groups []QueryGroup `json:"groups"`
}
raw, err := json.Marshal(input)
if err != nil {
return QueryDefinition{}, err
}
if err := json.Unmarshal(raw, &legacy); err != nil {
return QueryDefinition{}, err
}
return QueryDefinition{
Match: legacy.Match,
Groups: legacy.Groups,
}.Normalize(), nil
}
func (qb *QueryBuilder) ArgIdx() int {
return qb.argIdx
}
// RequiresUserHistoryCTE reports whether the most recently built clauses
// referenced the user_last_watched derived-table aggregate. The executor
// uses this signal to inject the CTE definition and the LEFT JOIN that
// surfaces uhist.last_watched (audit 2026-05-01 §3.1 Pattern B).
func (qb *QueryBuilder) RequiresUserHistoryCTE() bool {
return qb.requireUserHistoryCTE
}
// UserHistoryCTEArgs returns the [userID, profileID] pair the executor must
// prepend to the bound argument list when injecting the user_last_watched
// CTE. Returns nil when the CTE is not required.
func (qb *QueryBuilder) UserHistoryCTEArgs() []any {
if !qb.requireUserHistoryCTE {
return nil
}
return []any{qb.userID, qb.profileID}
}
// UserHistoryCTESQL returns the user_last_watched CTE definition. The two
// placeholder positions (user_id, profile_id) are bound to the args returned
// by UserHistoryCTEArgs and should be the first two entries in the final
// statement's arg list. The CTE aggregates user_watch_history watched_at,
// user_watch_progress.completed=TRUE updated_at, and completed ebook reader
// progress updated_at into one row per media_item_id, filtering out videos the
// user has hidden via
// user_history_hidden_items. The single GROUP BY replaces the two
// correlated MAX subqueries the previous lastWatchedExpr emitted (audit
// 2026-05-01 §3.1 Pattern B).
//
// NULL handling: each UNION ALL branch sets exactly one of (uwh_at, uwp_at);
// the other column is NULL. Postgres GREATEST and LEAST IGNORE NULL inputs
// (unlike MySQL/Oracle, which propagate NULL) — "NULL values in the list
// are ignored. The result will be NULL only if all the expressions evaluate
// to NULL." So GREATEST(uwh_at, NULL) = uwh_at, and the per-row aggregate
// reduces correctly to the timestamp from whichever source produced the row.
// MAX over those values then yields the per-media_item maximum across both
// sources. Do NOT wrap in COALESCE — that would mask future schema changes
// that could legitimately produce two non-NULL values per row.
// See https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST
func UserHistoryCTESQL(argIdx int) string {
return fmt.Sprintf(`user_last_watched AS (
SELECT src.media_item_id, MAX(GREATEST(src.uwh_at, src.uwp_at)) AS last_watched
FROM (
SELECT uwh.media_item_id, uwh.watched_at AS uwh_at, NULL::timestamptz AS uwp_at
FROM user_watch_history uwh
WHERE uwh.user_id = $%d AND uwh.profile_id = $%d
UNION ALL
SELECT uwp.media_item_id, NULL::timestamptz, uwp.updated_at
FROM user_watch_progress uwp
WHERE uwp.user_id = $%d AND uwp.profile_id = $%d AND uwp.completed = TRUE
UNION ALL
SELECT erp.content_id AS media_item_id, NULL::timestamptz, erp.updated_at
FROM ebook_reader_progress erp
WHERE erp.user_id = $%d AND erp.profile_id = $%d AND erp.progress >= %s
) src
WHERE NOT EXISTS (
SELECT 1 FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d AND hhi.profile_id = $%d
AND hhi.media_item_id = src.media_item_id
AND COALESCE(src.uwh_at, src.uwp_at) <= hhi.hidden_before
)
GROUP BY src.media_item_id
)`, argIdx, argIdx+1, argIdx, argIdx+1, argIdx, argIdx+1, EbookFinishedProgressThresholdSQL, argIdx, argIdx+1)
}
func (qb *QueryBuilder) BuildSortClause(sortConfig QuerySort) (string, []any, error) {
plan, err := qb.BuildSortPlan(sortConfig)
if err != nil {
return "", nil, err
}
return plan.OrderBy, plan.Args, nil
}
func (qb *QueryBuilder) BuildSortPlan(sortConfig QuerySort) (QuerySortPlan, error) {
sortConfig = NormalizeQuerySort(sortConfig)
sortDef, ok := querySortDefs[sortConfig.Field]
if !ok {
sortDef = querySortDefs[defaultSortField]
}
dir := "DESC"
if sortConfig.Order == "asc" {
dir = "ASC"
}
if sortDef.personalized {
if err := qb.ensureUserScope(sortConfig.Field); err != nil {
return QuerySortPlan{}, err
}
}
titleExpr := qb.normalizedTitleExpr()
plan := QuerySortPlan{}
switch sortConfig.Field {
case "title":
plan.OrderBy = fmt.Sprintf("ORDER BY %s %s, %s.content_id ASC", titleExpr, dir, qb.alias)
return plan, nil
case "release_date":
plan.OrderBy = qb.orderByExpr(qb.releaseDateSortExpr(), dir, true, titleExpr)
return plan, nil
case "added_at":
expr, joins, args, nullsLast := qb.addedAtSortPlan()
plan.Joins = joins
plan.Args = args
plan.OrderBy = qb.orderByExpr(expr, dir, nullsLast, titleExpr)
return plan, nil
case "content_rating":
rankExpr := qb.contentRatingRankExpr()
labelExpr := qb.contentRatingLabelExpr()
plan.OrderBy = fmt.Sprintf(
"ORDER BY %s %s, %s %s, %s ASC, %s.content_id ASC",
rankExpr,
dir,
labelExpr,
dir,
titleExpr,
qb.alias,
)
return plan, nil
case "resolution":
joinSQL, args := qb.mediaFileSortJoin("max_resolution_rank", qb.resolutionRankExpr())
plan.Joins = []string{joinSQL}
plan.Args = args
plan.OrderBy = qb.orderByExpr("sort_files.max_resolution_rank", dir, true, titleExpr)
return plan, nil
case "bitrate":
joinSQL, args := qb.mediaFileSortJoin("max_bitrate", "MAX(mf.bitrate)")
plan.Joins = []string{joinSQL}
plan.Args = args
plan.OrderBy = qb.orderByExpr("sort_files.max_bitrate", dir, true, titleExpr)
return plan, nil
case "progress":
expr, joins, args := qb.progressSortPlan()
plan.Joins = joins
plan.Args = args
plan.OrderBy = qb.orderByExpr(expr, dir, true, titleExpr)
return plan, nil
case "date_viewed":
expr, joins, args := qb.dateViewedSortPlan()
plan.Joins = joins
plan.Args = args
plan.OrderBy = qb.orderByExpr(expr, dir, true, titleExpr)
return plan, nil
case "plays":
expr, joins, args := qb.playsSortPlan()
plan.Joins = joins
plan.Args = args
plan.OrderBy = qb.orderByExpr(expr, dir, true, titleExpr)
return plan, nil
case "last_air_date":
expr, joins := qb.lastAirDateSortPlan()
plan.Joins = joins
plan.OrderBy = qb.orderByExpr(expr, dir, true, titleExpr)
return plan, nil
case "author":
plan.Joins = []string{qb.personKindSortJoin(models.PersonKindAuthor, "sort_author")}
plan.OrderBy = qb.orderByExpr("sort_author.name", dir, true, titleExpr)
return plan, nil
case "narrator":
plan.Joins = []string{qb.personKindSortJoin(models.PersonKindNarrator, "sort_narrator")}
plan.OrderBy = qb.orderByExpr("sort_narrator.name", dir, true, titleExpr)
return plan, nil
case "series":
plan.Joins = []string{fmt.Sprintf(
"LEFT JOIN %s sort_series ON sort_series.content_id = %s.content_id",
qb.bookSeriesTable(),
qb.alias,
)}
// Sort by series name primarily, then by series_index so books
// within the same series come back in narrative order. Title
// breaks ties for books that don't have a series_index.
clause := fmt.Sprintf(
"ORDER BY sort_series.series_name %s NULLS LAST, sort_series.series_index ASC NULLS LAST, %s ASC, %s.content_id ASC",
dir,
titleExpr,
qb.alias,
)
plan.OrderBy = clause
return plan, nil
}
plan.OrderBy = qb.orderByExpr(queryColumnSQL(qb.alias, sortDef.columnSQL), dir, sortDef.nullsLast, titleExpr)
if sortDef.titleSortOnly {
plan.OrderBy = fmt.Sprintf("ORDER BY %s %s, %s.content_id ASC", queryColumnSQL(qb.alias, sortDef.columnSQL), dir, qb.alias)
}
return plan, nil
}
func (qb *QueryBuilder) buildGroup(group QueryGroup) (string, error) {
if len(group.Rules) == 0 {
return "", nil
}
joiner := " AND "
if group.Match == "any" {
joiner = " OR "
}
combinedTechnicalClause, consumedRules, err := qb.buildSameFileTechnicalClause(group)
if err != nil {
return "", err
}
var ruleClauses []string
if combinedTechnicalClause != "" {
ruleClauses = append(ruleClauses, combinedTechnicalClause)
}
for index, rule := range group.Rules {
if consumedRules[index] {
continue
}
clause, err := qb.buildRule(rule)
if err != nil {
return "", err
}
if clause != "" {
ruleClauses = append(ruleClauses, clause)
}
}
if len(ruleClauses) == 0 {
return "", nil
}
return strings.Join(ruleClauses, joiner), nil
}
func (qb *QueryBuilder) buildRule(rule QueryRule) (string, error) {
def, ok := queryFieldDefs[rule.Field]
if !ok {
return "", fmt.Errorf("unknown filter field: %q", rule.Field)
}
if !def.validOps[rule.Op] {
return "", fmt.Errorf("invalid operator %q for field %q", rule.Op, rule.Field)
}
if !def.executable {
return "", fmt.Errorf("field %q is not supported for SQL execution yet", rule.Field)
}
switch rule.Field {
case "actor":
return qb.buildPersonClause(models.PersonKindActor, rule)
case "director":
return qb.buildPersonClause(models.PersonKindDirector, rule)
case "writer":
return qb.buildPersonClause(models.PersonKindWriter, rule)
case "producer":
return qb.buildPersonClause(models.PersonKindProducer, rule)
case "author":
return qb.buildPersonClause(models.PersonKindAuthor, rule)
case "narrator":
return qb.buildPersonClause(models.PersonKindNarrator, rule)
case "series":
return qb.buildBookSeriesClause(rule)
case "watched":
return qb.buildWatchedClause(rule)
case "favorited":
return qb.buildFavoriteClause(rule)
case "in_watchlist":
return qb.buildWatchlistClause(rule)
case "in_progress":
return qb.buildInProgressClause(rule)
case "last_watched":
return qb.buildLastWatchedClause(rule)
case "added_at":
return qb.buildAddedAtClause(rule)
case "release_date":
return qb.buildReleaseDateClause(rule)
case "resolution":
return qb.buildResolutionClause(rule)
case "hdr":
return qb.buildMediaFileEqualityClause("mf.hdr", rule)
case "dolby_vision":
return qb.buildDolbyVisionClause(rule)
case "bitrate":
return qb.buildMediaFileComparisonClause("mf.bitrate", rule)
case "audio_language":
return qb.buildAudioLanguageClause(rule)
case "subtitle_language":
return qb.buildSubtitleLanguageClause(rule)
}
column := queryColumnSQL(qb.alias, def.columnSQL)
switch rule.Op {
case "is":
return qb.buildEqualityClause(column, def.isArray, rule.Value, false), nil
case "is_not":
return qb.buildEqualityClause(column, def.isArray, rule.Value, true), nil
case "contains":
if !def.isArray {
return "", fmt.Errorf("contains operator only valid for array fields")
}
qb.args = append(qb.args, rule.Value)
clause := fmt.Sprintf("%s @> ARRAY[$%d]::text[]", column, qb.argIdx)
qb.argIdx++
return clause, nil
case "gt":
return qb.buildComparisonClause(column, ">", rule.Value), nil
case "gte":
return qb.buildComparisonClause(column, ">=", rule.Value), nil
case "lt":
return qb.buildComparisonClause(column, "<", rule.Value), nil
case "lte":
return qb.buildComparisonClause(column, "<=", rule.Value), nil
case "between":
values, err := toBetweenValues(rule.Value)
if err != nil {
return "", fmt.Errorf("between requires [min, max] array: %w", err)
}
qb.args = append(qb.args, values[0], values[1])
clause := fmt.Sprintf("%s >= $%d AND %s <= $%d", column, qb.argIdx, column, qb.argIdx+1)
qb.argIdx += 2
return clause, nil
case "in_last":
duration, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("in_last requires a duration string like '30d'")
}
interval, err := parseDuration(duration)
if err != nil {
return "", err
}
return fmt.Sprintf("%s >= NOW() - INTERVAL '%s'", column, interval), nil
default:
return "", fmt.Errorf("unhandled operator: %q", rule.Op)
}
}
func (qb *QueryBuilder) buildPersonClause(kind models.PersonKind, rule QueryRule) (string, error) {
qb.args = append(qb.args, rule.Value)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM item_people ip
WHERE ip.content_id = %s.content_id
AND ip.kind = %d
AND ip.person_id IN (
SELECT p.id
FROM people p
WHERE LOWER(p.name) = LOWER($%d)
)
)`, qb.alias, kind, qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
// personKindSortJoin emits a LEFT JOIN LATERAL that resolves the
// alphabetically-first person name of the given kind per item, exposed
// as <alias>.name. Used to power Author / Narrator sort plans without
// a heavyweight aggregate over the whole join.
func (qb *QueryBuilder) personKindSortJoin(kind models.PersonKind, alias string) string {
return fmt.Sprintf(
`LEFT JOIN LATERAL (
SELECT p.name
FROM item_people ip
JOIN people p ON p.id = ip.person_id
WHERE ip.content_id = %s.content_id
AND ip.kind = %d
AND p.name IS NOT NULL
AND BTRIM(p.name) <> ''
ORDER BY p.name ASC
LIMIT 1
) %s ON TRUE`,
qb.alias, kind, alias,
)
}
func (qb *QueryBuilder) bookSeriesTable() string {
if qb.mediaScope == "ebook" {
return "ebook_series"
}
return "audiobook_series"
}
func (qb *QueryBuilder) buildBookSeriesClause(rule QueryRule) (string, error) {
qb.args = append(qb.args, rule.Value)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM %s s
WHERE s.content_id = %s.content_id
AND LOWER(BTRIM(s.series_name)) = LOWER(BTRIM($%d))
)`, qb.bookSeriesTable(), qb.alias, qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildResolutionClause(rule QueryRule) (string, error) {
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("resolution requires a string value")
}
scopeClause := qb.mediaFileLibraryScopeClause("mf")
qb.args = append(qb.args, normalizeResolutionValue(value))
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND %s = $%d
)`, qb.mediaFileJoinCondition("mf"), scopeClause, normalizedResolutionExpr("mf.resolution"), qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildMediaFileEqualityClause(column string, rule QueryRule) (string, error) {
scopeClause := qb.mediaFileLibraryScopeClause("mf")
qb.args = append(qb.args, rule.Value)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND %s = $%d
)`, qb.mediaFileJoinCondition("mf"), scopeClause, column, qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildMediaFileComparisonClause(column string, rule QueryRule) (string, error) {
scopeClause := qb.mediaFileLibraryScopeClause("mf")
switch rule.Op {
case "gt", "gte", "lt", "lte":
operator := map[string]string{"gt": ">", "gte": ">=", "lt": "<", "lte": "<="}[rule.Op]
qb.args = append(qb.args, rule.Value)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND %s %s $%d
)`, qb.mediaFileJoinCondition("mf"), scopeClause, column, operator, qb.argIdx)
qb.argIdx++
return clause, nil
case "between":
values, err := toBetweenValues(rule.Value)
if err != nil {
return "", fmt.Errorf("between requires [min, max] array: %w", err)
}
qb.args = append(qb.args, values[0], values[1])
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND %s >= $%d
AND %s <= $%d
)`, qb.mediaFileJoinCondition("mf"), scopeClause, column, qb.argIdx, column, qb.argIdx+1)
qb.argIdx += 2
return clause, nil
default:
return "", fmt.Errorf("unsupported operator %q for media file comparison", rule.Op)
}
}
func (qb *QueryBuilder) buildDolbyVisionClause(rule QueryRule) (string, error) {
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("dolby_vision requires a boolean value")
}
scopeClause := qb.mediaFileLibraryScopeClause("mf")
inner := `EXISTS (
SELECT 1
FROM jsonb_array_elements(COALESCE(mf.video_tracks, '[]'::jsonb)) AS vt
WHERE NULLIF(BTRIM(vt->>'dolby_vision'), '') IS NOT NULL
)`
if !value {
inner = `NOT ` + inner
}
return fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND %s
)`, qb.mediaFileJoinCondition("mf"), scopeClause, inner), nil
}
func (qb *QueryBuilder) buildAudioLanguageClause(rule QueryRule) (string, error) {
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("audio_language requires a string value")
}
scopeClause := qb.mediaFileLibraryScopeClause("mf")
// audio_language_codes is the STORED generated text[] of LOWER(language)
// codes from audio_tracks JSONB (migration 104). Filtering via @>
// ARRAY[...] uses the GIN index and avoids per-row JSONB unnest
// (audit 2026-05-01 §2.5b).
qb.args = append(qb.args, strings.ToLower(value))
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND mf.audio_language_codes @> ARRAY[$%d]::text[]
)`, qb.mediaFileJoinCondition("mf"), scopeClause, qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildSubtitleLanguageClause(rule QueryRule) (string, error) {
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("subtitle_language requires a string value")
}
scopeClause := qb.mediaFileLibraryScopeClause("mf")
// subtitle_language_codes (migration 104) covers embedded subtitle_tracks
// only. external_subtitles is still JSONB unnest as a fallback so the
// behavior of the OR-check is preserved (audit 2026-05-01 §2.5b).
qb.args = append(qb.args, strings.ToLower(value))
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
AND mf.missing_since IS NULL
AND %s
AND (
mf.subtitle_language_codes @> ARRAY[$%d]::text[]
OR EXISTS (
SELECT 1
FROM jsonb_array_elements(COALESCE(mf.external_subtitles, '[]'::jsonb)) AS track
WHERE LOWER(COALESCE(track->>'language', '')) = $%d
)
)
)`, qb.mediaFileJoinCondition("mf"), scopeClause, qb.argIdx, qb.argIdx)
qb.argIdx++
if rule.Op == "is_not" {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildSameFileTechnicalClause(group QueryGroup) (string, map[int]bool, error) {
if group.Match == "any" {
return "", nil, nil
}
startArgIdx := qb.argIdx
startArgsLen := len(qb.args)
consumed := make(map[int]bool)
predicates := make([]string, 0, len(group.Rules))
for index, rule := range group.Rules {
if !isSameFileTechnicalRule(rule) {
continue
}
predicate, err := qb.buildTechnicalMediaFilePredicate(rule, "mf")
if err != nil {
return "", nil, err
}
if predicate == "" {
continue
}
consumed[index] = true
predicates = append(predicates, predicate)
}
if len(predicates) < 2 {
qb.argIdx = startArgIdx
qb.args = qb.args[:startArgsLen]
return "", nil, nil
}
whereParts := []string{
qb.mediaFileJoinCondition("mf"),
"mf.missing_since IS NULL",
}
if len(qb.libraryIDs) > 0 {
placeholders, args := qb.consumeIntArgs(qb.libraryIDs)
qb.args = append(qb.args, args...)
whereParts = append(whereParts, fmt.Sprintf("mf.media_folder_id IN (%s)", strings.Join(placeholders, ", ")))
}
whereParts = append(whereParts, predicates...)
return fmt.Sprintf(`EXISTS (
SELECT 1
FROM media_files mf
WHERE %s
)`, strings.Join(whereParts, "\n\t\t AND ")), consumed, nil
}
func (qb *QueryBuilder) buildTechnicalMediaFilePredicate(rule QueryRule, alias string) (string, error) {
switch rule.Field {
case "resolution":
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("resolution requires a string value")
}
qb.args = append(qb.args, normalizeResolutionValue(value))
predicate := fmt.Sprintf("%s = $%d", normalizedResolutionExpr(alias+".resolution"), qb.argIdx)
qb.argIdx++
return predicate, nil
case "hdr":
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("hdr requires a boolean value")
}
qb.args = append(qb.args, value)
predicate := fmt.Sprintf("%s.hdr = $%d", alias, qb.argIdx)
qb.argIdx++
return predicate, nil
case "dolby_vision":
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("dolby_vision requires a boolean value")
}
predicate := fmt.Sprintf(`EXISTS (
SELECT 1
FROM jsonb_array_elements(COALESCE(%s.video_tracks, '[]'::jsonb)) AS vt
WHERE NULLIF(BTRIM(vt->>'dolby_vision'), '') IS NOT NULL
)`, alias)
if !value {
return "NOT (" + predicate + ")", nil
}
return predicate, nil
case "bitrate":
return qb.buildMediaFileInlineComparisonPredicate(alias+".bitrate", rule)
case "audio_language":
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("audio_language requires a string value")
}
// audio_language_codes is the STORED generated text[] derived from
// audio_tracks JSONB (migration 104). GIN containment beats per-row
// jsonb_array_elements unnest (audit 2026-05-01 §2.5b).
qb.args = append(qb.args, strings.ToLower(value))
predicate := fmt.Sprintf("%s.audio_language_codes @> ARRAY[$%d]::text[]", alias, qb.argIdx)
qb.argIdx++
return predicate, nil
case "subtitle_language":
value, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("subtitle_language requires a string value")
}
// subtitle_language_codes (migration 104) covers embedded
// subtitle_tracks only. external_subtitles remains a JSONB unnest
// fallback to preserve OR-check behavior (audit 2026-05-01 §2.5b).
qb.args = append(qb.args, strings.ToLower(value))
predicate := fmt.Sprintf(`(
%s.subtitle_language_codes @> ARRAY[$%d]::text[]
OR EXISTS (
SELECT 1
FROM jsonb_array_elements(COALESCE(%s.external_subtitles, '[]'::jsonb)) AS track
WHERE LOWER(COALESCE(track->>'language', '')) = $%d
)
)`, alias, qb.argIdx, alias, qb.argIdx)
qb.argIdx++
return predicate, nil
default:
return "", fmt.Errorf("field %q does not support same-file filtering", rule.Field)
}
}
func (qb *QueryBuilder) buildMediaFileInlineComparisonPredicate(column string, rule QueryRule) (string, error) {
switch rule.Op {
case "gt", "gte", "lt", "lte":
operator := map[string]string{"gt": ">", "gte": ">=", "lt": "<", "lte": "<="}[rule.Op]
qb.args = append(qb.args, rule.Value)
predicate := fmt.Sprintf("%s %s $%d", column, operator, qb.argIdx)
qb.argIdx++
return predicate, nil
case "between":
values, err := toBetweenValues(rule.Value)
if err != nil {
return "", fmt.Errorf("between requires [min, max] array: %w", err)
}
qb.args = append(qb.args, values[0], values[1])
predicate := fmt.Sprintf("%s >= $%d AND %s <= $%d", column, qb.argIdx, column, qb.argIdx+1)
qb.argIdx += 2
return predicate, nil
default:
return "", fmt.Errorf("unsupported operator %q for media file comparison", rule.Op)
}
}
func isSameFileTechnicalRule(rule QueryRule) bool {
if rule.Op == "is_not" {
return false
}
switch rule.Field {
case "resolution", "hdr", "dolby_vision", "bitrate", "audio_language", "subtitle_language":
return true
default:
return false
}
}
func (qb *QueryBuilder) mediaFileLibraryScopeClause(alias string) string {
if len(qb.libraryIDs) == 0 {
return "TRUE"
}
placeholders, args := qb.consumeIntArgs(qb.libraryIDs)
qb.args = append(qb.args, args...)
return fmt.Sprintf("%s.media_folder_id IN (%s)", alias, strings.Join(placeholders, ", "))
}
func (qb *QueryBuilder) mediaFileJoinCondition(mediaFileAlias string) string {
return catalogMediaFileJoinConditionForScope(qb.mediaScope, mediaFileAlias, qb.alias)
}
func (qb *QueryBuilder) mediaFileGroupExpr(mediaFileAlias string) string {
return catalogMediaFileGroupExprForScope(qb.mediaScope, mediaFileAlias)
}
func (qb *QueryBuilder) libraryContentExpr() string {
return catalogLibraryContentExprForScope(qb.mediaScope, qb.alias)
}
func normalizedResolutionExpr(column string) string {
return fmt.Sprintf(`CASE LOWER(NULLIF(BTRIM(%s), ''))
WHEN '4k' THEN '2160p'
WHEN 'uhd' THEN '2160p'
ELSE LOWER(NULLIF(BTRIM(%s), ''))
END`, column, column)
}
func normalizeResolutionValue(value string) string {
switch strings.ToLower(strings.TrimSpace(value)) {
case "4k", "uhd":
return "2160p"
default:
return strings.ToLower(strings.TrimSpace(value))
}
}
func (qb *QueryBuilder) ensureUserScope(field string) error {
if qb.userID == 0 || strings.TrimSpace(qb.profileID) == "" {
return fmt.Errorf("field %q requires user scope", field)
}
return nil
}
func (qb *QueryBuilder) buildWatchedClause(rule QueryRule) (string, error) {
if err := qb.ensureUserScope(rule.Field); err != nil {
return "", err
}
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("watched requires a boolean value")
}
clause := qb.userStateCompletionClause()
if !value {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildFavoriteClause(rule QueryRule) (string, error) {
if err := qb.ensureUserScope(rule.Field); err != nil {
return "", err
}
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("favorited requires a boolean value")
}
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM user_favorites uf
WHERE uf.user_id = $%d
AND uf.profile_id = $%d
AND uf.media_item_id = %s.content_id
)`, qb.argIdx, qb.argIdx+1, qb.alias)
qb.argIdx += 2
if !value {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildWatchlistClause(rule QueryRule) (string, error) {
if err := qb.ensureUserScope(rule.Field); err != nil {
return "", err
}
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("in_watchlist requires a boolean value")
}
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM user_watchlist uw
WHERE uw.user_id = $%d
AND uw.profile_id = $%d
AND uw.media_item_id = %s.content_id
)`, qb.argIdx, qb.argIdx+1, qb.alias)
qb.argIdx += 2
if !value {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildInProgressClause(rule QueryRule) (string, error) {
if err := qb.ensureUserScope(rule.Field); err != nil {
return "", err
}
value, ok := rule.Value.(bool)
if !ok {
return "", fmt.Errorf("in_progress requires a boolean value")
}
if qb.mediaScope == "ebook" {
return qb.buildEbookInProgressClause(value), nil
}
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM user_watch_progress uwp
WHERE uwp.user_id = $%d
AND uwp.profile_id = $%d
AND uwp.media_item_id = %s.content_id
AND uwp.position_seconds > 0
AND NOT EXISTS (
SELECT 1
FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwp.media_item_id
AND uwp.updated_at <= hhi.hidden_before
)
)`, qb.argIdx, qb.argIdx+1, qb.alias, qb.argIdx, qb.argIdx+1)
qb.argIdx += 2
if !value {
return "NOT (" + clause + ")", nil
}
return clause, nil
}
func (qb *QueryBuilder) buildEbookInProgressClause(value bool) string {
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM ebook_reader_progress erp
WHERE erp.user_id = $%d
AND erp.profile_id = $%d
AND erp.content_id = %s.content_id
AND erp.progress > 0
AND erp.progress < %s
AND NOT EXISTS (
SELECT 1
FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = erp.content_id
AND erp.updated_at <= hhi.hidden_before
)
)`, qb.argIdx, qb.argIdx+1, qb.alias, EbookFinishedProgressThresholdSQL, qb.argIdx, qb.argIdx+1)
qb.argIdx += 2
if !value {
return "NOT (" + clause + ")"
}
return clause
}
func (qb *QueryBuilder) buildLastWatchedClause(rule QueryRule) (string, error) {
if err := qb.ensureUserScope(rule.Field); err != nil {
return "", err
}
expr := qb.lastWatchedExpr(rule.Field)
switch rule.Op {
case "gt":
return qb.buildTimestampComparisonClause(expr, ">", rule.Value), nil
case "gte":
return qb.buildTimestampComparisonClause(expr, ">=", rule.Value), nil
case "lt":
return qb.buildTimestampComparisonClause(expr, "<", rule.Value), nil
case "lte":
return qb.buildTimestampComparisonClause(expr, "<=", rule.Value), nil
case "between":
values, err := toBetweenValues(rule.Value)
if err != nil {
return "", fmt.Errorf("between requires [min, max] array: %w", err)
}
qb.args = append(qb.args, values[0], values[1])
clause := fmt.Sprintf("%s >= $%d::timestamptz AND %s <= $%d::timestamptz", expr, qb.argIdx, expr, qb.argIdx+1)
qb.argIdx += 2
return clause, nil
case "in_last":
duration, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("in_last requires a duration string like '30d'")
}
interval, err := parseDuration(duration)
if err != nil {
return "", err
}
return fmt.Sprintf("%s >= NOW() - INTERVAL '%s'", expr, interval), nil
default:
return "", fmt.Errorf("unsupported operator %q for last_watched", rule.Op)
}
}
func (qb *QueryBuilder) buildAddedAtClause(rule QueryRule) (string, error) {
column := qb.addedAtFilterExpr()
switch rule.Op {
case "gt":
return qb.buildTypedComparisonClause(column, ">", rule.Value, "timestamptz"), nil
case "gte":
return qb.buildTypedComparisonClause(column, ">=", rule.Value, "timestamptz"), nil
case "lt":
return qb.buildTypedComparisonClause(column, "<", rule.Value, "timestamptz"), nil
case "lte":
return qb.buildTypedComparisonClause(column, "<=", rule.Value, "timestamptz"), nil
case "between":
return qb.buildTypedBetweenClause(column, rule.Value, "timestamptz")
case "in_last":
duration, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("in_last requires a duration string like '30d'")
}
interval, err := parseDuration(duration)
if err != nil {
return "", err
}
return fmt.Sprintf("%s >= NOW() - INTERVAL '%s'", column, interval), nil
default:
return "", fmt.Errorf("unsupported operator %q for added_at", rule.Op)
}
}
func (qb *QueryBuilder) buildReleaseDateClause(rule QueryRule) (string, error) {
column := qb.releaseDateFilterExpr()
switch rule.Op {
case "gt":
return qb.buildTypedComparisonClause(column, ">", rule.Value, "date"), nil
case "gte":
return qb.buildTypedComparisonClause(column, ">=", rule.Value, "date"), nil
case "lt":
return qb.buildTypedComparisonClause(column, "<", rule.Value, "date"), nil
case "lte":
return qb.buildTypedComparisonClause(column, "<=", rule.Value, "date"), nil
case "between":
return qb.buildTypedBetweenClause(column, rule.Value, "date")
case "in_last":
duration, ok := rule.Value.(string)
if !ok {
return "", fmt.Errorf("in_last requires a duration string like '30d'")
}
interval, err := parseDuration(duration)
if err != nil {
return "", err
}
return fmt.Sprintf("%s >= (CURRENT_DATE - INTERVAL '%s')::date", column, interval), nil
default:
return "", fmt.Errorf("unsupported operator %q for release_date", rule.Op)
}
}
func (qb *QueryBuilder) userStateCompletionClause() string {
if qb.mediaScope == "ebook" {
return qb.ebookUserStateCompletionClause()
}
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`(
EXISTS (
SELECT 1
FROM user_watch_progress uwp
WHERE uwp.user_id = $%d
AND uwp.profile_id = $%d
AND uwp.media_item_id = %s.content_id
AND uwp.completed = TRUE
AND NOT EXISTS (
SELECT 1
FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwp.media_item_id
AND uwp.updated_at <= hhi.hidden_before
)
)
OR EXISTS (
SELECT 1
FROM user_watch_history uwh
WHERE uwh.user_id = $%d
AND uwh.profile_id = $%d
AND uwh.media_item_id = %s.content_id
AND uwh.completed = TRUE
AND NOT EXISTS (
SELECT 1
FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwh.media_item_id
AND uwh.watched_at <= hhi.hidden_before
)
)
)`,
qb.argIdx,
qb.argIdx+1,
qb.alias,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
qb.argIdx,
qb.argIdx+1,
)
qb.argIdx += 2
return clause
}
func (qb *QueryBuilder) ebookUserStateCompletionClause() string {
qb.args = append(qb.args, qb.userID, qb.profileID)
clause := fmt.Sprintf(`EXISTS (
SELECT 1
FROM ebook_reader_progress erp
WHERE erp.user_id = $%d
AND erp.profile_id = $%d
AND erp.content_id = %s.content_id
AND erp.progress >= %s
AND NOT EXISTS (
SELECT 1
FROM user_history_hidden_items hhi
WHERE hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = erp.content_id
AND erp.updated_at <= hhi.hidden_before
)
)`, qb.argIdx, qb.argIdx+1, qb.alias, EbookFinishedProgressThresholdSQL, qb.argIdx, qb.argIdx+1)
qb.argIdx += 2
return clause
}
// lastWatchedExpr returns the SQL expression the executor binds to the
// per-user history aggregate. It signals the executor (via
// requireUserHistoryCTE) that the user_last_watched CTE and a LEFT JOIN
// aliased uhist on uhist.media_item_id = mi.content_id must be injected.
// The previous implementation emitted two correlated MAX subqueries with
// nested NOT EXISTS guards which fired ~2N times per filter row (audit
// 2026-05-01 §3.1 Pattern B). The CTE aggregates user_watch_history and
// user_watch_progress (completed) once per request and the JOIN reuses the
// result for every candidate media item. We wrap the join column in
// COALESCE(..., '-infinity'::timestamptz) so never-watched items behave the
// same way under the comparison operators (lt/lte/between/in_last) as the
// previous GREATEST(COALESCE(...), COALESCE(...)) form.
func (qb *QueryBuilder) lastWatchedExpr(_ string) string {
qb.requireUserHistoryCTE = true
return "COALESCE(uhist.last_watched, '-infinity'::timestamptz)"
}
func (qb *QueryBuilder) buildTimestampComparisonClause(column, op string, value any) string {
qb.args = append(qb.args, value)
clause := fmt.Sprintf("%s %s $%d::timestamptz", column, op, qb.argIdx)
qb.argIdx++
return clause
}
func (qb *QueryBuilder) buildTypedComparisonClause(column, op string, value any, cast string) string {
qb.args = append(qb.args, value)
placeholder := fmt.Sprintf("$%d", qb.argIdx)
if cast != "" {
placeholder += "::" + cast
}
clause := fmt.Sprintf("%s %s %s", column, op, placeholder)
qb.argIdx++
return clause
}
func (qb *QueryBuilder) buildTypedBetweenClause(column string, value any, cast string) (string, error) {
values, err := toBetweenValues(value)
if err != nil {
return "", fmt.Errorf("between requires [min, max] array: %w", err)
}
qb.args = append(qb.args, values[0], values[1])
left := fmt.Sprintf("$%d", qb.argIdx)
right := fmt.Sprintf("$%d", qb.argIdx+1)
if cast != "" {
left += "::" + cast
right += "::" + cast
}
clause := fmt.Sprintf("%s >= %s AND %s <= %s", column, left, column, right)
qb.argIdx += 2
return clause, nil
}
func (qb *QueryBuilder) normalizedTitleExpr() string {
if isEpisodeCatalogScope(qb.mediaScope) {
return fmt.Sprintf("%s.sort_key", qb.alias)
}
return fmt.Sprintf(
"LOWER(COALESCE(NULLIF(BTRIM(%s.sort_title), ''), %s.title))",
qb.alias,
qb.alias,
)
}
func (qb *QueryBuilder) releaseDateSortExpr() string {
if isEpisodeCatalogScope(qb.mediaScope) {
return fmt.Sprintf("%s.episode_air_date", qb.alias)
}
return queryColumnSQL(qb.alias, querySortDefs["release_date"].columnSQL)
}
func (qb *QueryBuilder) releaseDateFilterExpr() string {
if isEpisodeCatalogScope(qb.mediaScope) {
return fmt.Sprintf("%s.episode_air_date", qb.alias)
}
firstAirDate := fmt.Sprintf("NULLIF(BTRIM(%s.first_air_date), '')", qb.alias)
return fmt.Sprintf(
`COALESCE(%s.release_date, CASE WHEN %s ~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' THEN %s::date ELSE NULL END)`,
qb.alias,
firstAirDate,
firstAirDate,
)
}
func (qb *QueryBuilder) addedAtFilterExpr() string {
if isEpisodeCatalogScope(qb.mediaScope) {
return fmt.Sprintf("%s.episode_created_at", qb.alias)
}
return fmt.Sprintf("%s.created_at", qb.alias)
}
func (qb *QueryBuilder) orderByExpr(expr, dir string, nullsLast bool, titleExpr string) string {
clause := fmt.Sprintf("ORDER BY %s %s", expr, dir)
if nullsLast {
clause += " NULLS LAST"
}
clause += fmt.Sprintf(", %s ASC, %s.content_id ASC", titleExpr, qb.alias)
return clause
}
func (qb *QueryBuilder) addedAtSortPlan() (string, []string, []any, bool) {
if len(qb.libraryIDs) == 0 {
return fmt.Sprintf("%s.created_at", qb.alias), nil, nil, false
}
placeholders, args := qb.consumeIntArgs(qb.libraryIDs)
if isEpisodeCatalogScope(qb.mediaScope) {
if len(qb.libraryIDs) == 1 {
joinSQL := fmt.Sprintf(
`JOIN episode_libraries sort_added ON sort_added.episode_id = %s.content_id AND sort_added.media_folder_id = %s`,
qb.alias,
placeholders[0],
)
return "sort_added.first_seen_at", []string{joinSQL}, args, false
}
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT el.episode_id AS content_id, MIN(el.first_seen_at) AS added_at
FROM episode_libraries el
WHERE el.media_folder_id IN (%s)
GROUP BY el.episode_id
) sort_added ON sort_added.content_id = %s.content_id`,
strings.Join(placeholders, ", "),
qb.alias,
)
return "sort_added.added_at", []string{joinSQL}, args, true
}
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT mil.content_id, MIN(mil.first_seen_at) AS added_at
FROM media_item_libraries mil
WHERE mil.media_folder_id IN (%s)
GROUP BY mil.content_id
) sort_added ON sort_added.content_id = %s`,
strings.Join(placeholders, ", "),
qb.libraryContentExpr(),
)
return "sort_added.added_at", []string{joinSQL}, args, true
}
func (qb *QueryBuilder) contentRatingRankExpr() string {
cases := make([]string, 0, len(access.RatingRankEntries()))
for _, entry := range access.RatingRankEntries() {
cases = append(
cases,
fmt.Sprintf(
"WHEN UPPER(NULLIF(BTRIM(%s.content_rating), '')) = '%s' THEN %d",
qb.alias,
entry.Rating,
entry.Rank,
),
)
}
return fmt.Sprintf(
"CASE %s ELSE 2147483647 END",
strings.Join(cases, " "),
)
}
func (qb *QueryBuilder) contentRatingLabelExpr() string {
return fmt.Sprintf(
"LOWER(COALESCE(NULLIF(BTRIM(%s.content_rating), ''), '~~~~'))",
qb.alias,
)
}
func (qb *QueryBuilder) resolutionRankExpr() string {
return `MAX(
CASE UPPER(CASE LOWER(NULLIF(BTRIM(mf.resolution), ''))
WHEN '4k' THEN '2160p'
WHEN 'uhd' THEN '2160p'
ELSE LOWER(NULLIF(BTRIM(mf.resolution), ''))
END)
WHEN '480P' THEN 1
WHEN '720P' THEN 2
WHEN '1080P' THEN 3
WHEN '2160P' THEN 4
WHEN '4320P' THEN 5
ELSE NULL
END
)`
}
func (qb *QueryBuilder) mediaFileSortJoin(columnAlias, aggregateExpr string) (string, []any) {
whereParts := []string{"mf.missing_since IS NULL"}
var args []any
if len(qb.libraryIDs) > 0 {
placeholders, scopeArgs := qb.consumeIntArgs(qb.libraryIDs)
whereParts = append(whereParts, fmt.Sprintf("mf.media_folder_id IN (%s)", strings.Join(placeholders, ", ")))
args = append(args, scopeArgs...)
}
groupExpr := qb.mediaFileGroupExpr("mf")
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT %s AS content_id, %s AS %s
FROM media_files mf
WHERE %s
GROUP BY %s
) sort_files ON sort_files.content_id = %s.content_id`,
groupExpr,
aggregateExpr,
columnAlias,
strings.Join(whereParts, " AND "),
groupExpr,
qb.alias,
)
return joinSQL, args
}
func (qb *QueryBuilder) consumeIntArgs(values []int) ([]string, []any) {
placeholders := make([]string, len(values))
args := make([]any, 0, len(values))
for i, value := range values {
placeholders[i] = "$" + strconv.Itoa(qb.argIdx+i)
args = append(args, value)
}
qb.argIdx += len(values)
return placeholders, args
}
func (qb *QueryBuilder) progressSortPlan() (string, []string, []any) {
if qb.mediaScope == "ebook" {
return qb.ebookProgressSortPlan()
}
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT uwp.media_item_id AS content_id,
CASE
WHEN uwp.position_seconds > 0
AND COALESCE(uwp.duration_seconds, 0) > 0
AND (hhi.media_item_id IS NULL OR uwp.updated_at > hhi.hidden_before)
THEN uwp.position_seconds::double precision / NULLIF(uwp.duration_seconds, 0)
ELSE NULL
END AS progress_ratio
FROM user_watch_progress uwp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwp.media_item_id
WHERE uwp.user_id = $%d
AND uwp.profile_id = $%d
) sort_progress ON sort_progress.content_id = %s.content_id`,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
return "sort_progress.progress_ratio", []string{joinSQL}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) ebookProgressSortPlan() (string, []string, []any) {
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT erp.content_id,
CASE
WHEN erp.progress > 0
AND erp.progress < %s
AND (hhi.media_item_id IS NULL OR erp.updated_at > hhi.hidden_before)
THEN erp.progress::double precision
ELSE NULL
END AS progress_ratio
FROM ebook_reader_progress erp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = erp.content_id
WHERE erp.user_id = $%d
AND erp.profile_id = $%d
) sort_progress ON sort_progress.content_id = %s.content_id`,
EbookFinishedProgressThresholdSQL,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
return "sort_progress.progress_ratio", []string{joinSQL}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) dateViewedSortPlan() (string, []string, []any) {
if qb.mediaScope == "ebook" {
return qb.ebookDateViewedSortPlan()
}
historyJoin := fmt.Sprintf(
`LEFT JOIN (
SELECT uwh.media_item_id AS content_id, MAX(uwh.watched_at) AS viewed_at
FROM user_watch_history uwh
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwh.media_item_id
WHERE uwh.user_id = $%d
AND uwh.profile_id = $%d
AND uwh.completed = TRUE
AND (hhi.media_item_id IS NULL OR uwh.watched_at > hhi.hidden_before)
GROUP BY uwh.media_item_id
) sort_history_viewed ON sort_history_viewed.content_id = %s.content_id`,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
progressJoin := fmt.Sprintf(
`LEFT JOIN (
SELECT uwp.media_item_id AS content_id,
CASE
WHEN uwp.completed = TRUE
AND (hhi.media_item_id IS NULL OR uwp.updated_at > hhi.hidden_before)
THEN uwp.updated_at
ELSE NULL
END AS viewed_at
FROM user_watch_progress uwp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwp.media_item_id
WHERE uwp.user_id = $%d
AND uwp.profile_id = $%d
) sort_progress_viewed ON sort_progress_viewed.content_id = %s.content_id`,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
expr := `GREATEST(
COALESCE(sort_history_viewed.viewed_at, '-infinity'::timestamptz),
COALESCE(sort_progress_viewed.viewed_at, '-infinity'::timestamptz)
)`
return expr, []string{historyJoin, progressJoin}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) ebookDateViewedSortPlan() (string, []string, []any) {
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT erp.content_id,
CASE
WHEN erp.progress >= %s
AND (hhi.media_item_id IS NULL OR erp.updated_at > hhi.hidden_before)
THEN erp.updated_at
ELSE NULL
END AS viewed_at
FROM ebook_reader_progress erp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = erp.content_id
WHERE erp.user_id = $%d
AND erp.profile_id = $%d
) sort_ebook_viewed ON sort_ebook_viewed.content_id = %s.content_id`,
EbookFinishedProgressThresholdSQL,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
return "sort_ebook_viewed.viewed_at", []string{joinSQL}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) playsSortPlan() (string, []string, []any) {
if qb.mediaScope == "ebook" {
return qb.ebookPlaysSortPlan()
}
historyJoin := fmt.Sprintf(
`LEFT JOIN (
SELECT uwh.media_item_id AS content_id, COUNT(*) AS play_count
FROM user_watch_history uwh
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwh.media_item_id
WHERE uwh.user_id = $%d
AND uwh.profile_id = $%d
AND uwh.completed = TRUE
AND (hhi.media_item_id IS NULL OR uwh.watched_at > hhi.hidden_before)
GROUP BY uwh.media_item_id
) sort_history_plays ON sort_history_plays.content_id = %s.content_id`,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
progressJoin := fmt.Sprintf(
`LEFT JOIN (
SELECT uwp.media_item_id AS content_id,
CASE
WHEN uwp.completed = TRUE
AND (hhi.media_item_id IS NULL OR uwp.updated_at > hhi.hidden_before)
THEN 1
ELSE 0
END AS completed_play_count
FROM user_watch_progress uwp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = uwp.media_item_id
WHERE uwp.user_id = $%d
AND uwp.profile_id = $%d
) sort_progress_plays ON sort_progress_plays.content_id = %s.content_id`,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
expr := `NULLIF(GREATEST(
COALESCE(sort_history_plays.play_count, 0),
COALESCE(sort_progress_plays.completed_play_count, 0)
), 0)`
return expr, []string{historyJoin, progressJoin}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) ebookPlaysSortPlan() (string, []string, []any) {
joinSQL := fmt.Sprintf(
`LEFT JOIN (
SELECT erp.content_id,
CASE
WHEN erp.progress >= %s
AND (hhi.media_item_id IS NULL OR erp.updated_at > hhi.hidden_before)
THEN 1
ELSE 0
END AS completed_play_count
FROM ebook_reader_progress erp
LEFT JOIN user_history_hidden_items hhi
ON hhi.user_id = $%d
AND hhi.profile_id = $%d
AND hhi.media_item_id = erp.content_id
WHERE erp.user_id = $%d
AND erp.profile_id = $%d
) sort_ebook_plays ON sort_ebook_plays.content_id = %s.content_id`,
EbookFinishedProgressThresholdSQL,
qb.argIdx,
qb.argIdx+1,
qb.argIdx,
qb.argIdx+1,
qb.alias,
)
return "NULLIF(sort_ebook_plays.completed_play_count, 0)", []string{joinSQL}, []any{qb.userID, qb.profileID}
}
func (qb *QueryBuilder) lastAirDateSortPlan() (string, []string) {
expr := fmt.Sprintf("%s.last_air_date_at", qb.alias)
return expr, nil // denormalized column — no JOIN required
}
func (qb *QueryBuilder) buildEqualityClause(column string, isArray bool, value any, negate bool) string {
qb.args = append(qb.args, value)
if isArray {
clause := fmt.Sprintf("%s @> ARRAY[$%d]::text[]", column, qb.argIdx)
qb.argIdx++
if negate {
return "NOT (" + clause + ")"
}
return clause
}
op := "="
if negate {
op = "!="
}
clause := fmt.Sprintf("%s %s $%d", column, op, qb.argIdx)
qb.argIdx++
return clause
}
func (qb *QueryBuilder) buildComparisonClause(column, op string, value any) string {
qb.args = append(qb.args, value)
clause := fmt.Sprintf("%s %s $%d", column, op, qb.argIdx)
qb.argIdx++
return clause
}
func toBetweenValues(v any) ([2]any, error) {
switch val := v.(type) {
case []any:
if len(val) != 2 {
return [2]any{}, fmt.Errorf("expected 2 elements, got %d", len(val))
}
return [2]any{val[0], val[1]}, nil
case json.RawMessage:
var arr []any
if err := json.Unmarshal(val, &arr); err != nil {
return [2]any{}, err
}
if len(arr) != 2 {
return [2]any{}, fmt.Errorf("expected 2 elements, got %d", len(arr))
}
return [2]any{arr[0], arr[1]}, nil
default:
return [2]any{}, fmt.Errorf("unsupported between value type: %T", v)
}
}
type relativeDuration struct {
amount int
unit byte
}
func parseDuration(s string) (string, error) {
spec, err := parseDurationSpec(s)
if err != nil {
return "", err
}
return spec.sqlInterval(), nil
}
func parseDurationSpec(s string) (relativeDuration, error) {
normalized := strings.ToLower(strings.TrimSpace(s))
if len(normalized) < 2 {
return relativeDuration{}, fmt.Errorf("invalid duration: %q", s)
}
value := strings.TrimSpace(normalized[:len(normalized)-1])
unit := normalized[len(normalized)-1]
amount, err := strconv.Atoi(value)
if err != nil || amount <= 0 {
return relativeDuration{}, fmt.Errorf("invalid duration: %q", s)
}
switch unit {
case 'h', 'd', 'w', 'm', 'y':
return relativeDuration{amount: amount, unit: unit}, nil
default:
return relativeDuration{}, fmt.Errorf("unsupported duration unit %q", unit)
}
}
func (d relativeDuration) sqlInterval() string {
switch d.unit {
case 'h':
return fmt.Sprintf("%d hours", d.amount)
case 'd':
return fmt.Sprintf("%d days", d.amount)
case 'w':
return fmt.Sprintf("%d weeks", d.amount)
case 'm':
return fmt.Sprintf("%d months", d.amount)
case 'y':
return fmt.Sprintf("%d years", d.amount)
default:
return ""
}
}
func (d relativeDuration) cutoffTime(now time.Time) time.Time {
switch d.unit {
case 'h':
return now.Add(-time.Duration(d.amount) * time.Hour)
case 'd':
return now.AddDate(0, 0, -d.amount)
case 'w':
return now.AddDate(0, 0, -7*d.amount)
case 'm':
return now.AddDate(0, -d.amount, 0)
case 'y':
return now.AddDate(-d.amount, 0, 0)
default:
return now
}
}