Files
silo-server/internal/userdb/progress.go
T
9cae868a27 feat(downloads): offline sync for mobile — downloads v2 (#258)
* feat(downloads): offline sync for mobile (downloads v2)

Replace internal/download with a unified internal/downloads package and add
fully-offline download + watch-sync support for mobile clients, across five
independently-shippable phases:

- Phase 0: reshape the downloads table and the /downloads contract to be
  device- and format-aware; add GET /downloads/capability; extend
  DownloadConfig (default-off keys); update the web download hooks/components in
  lockstep. This is the one approved pre-lock exception to the additive-only
  /api/v1 rule (the web app is the only consumer and is updated together).
- Phase 1: managed device-library entries (create/list/PATCH/delete/serve),
  keyed on the X-Silo-Device-Id header.
- Phase 2: offline playback manifest plus artwork/subtitle proxy endpoints that
  strip every presigned URL (inline thumbhashes + authenticated proxies).
- Phase 3: prepare-to-file (remux + transcode-to-single-file) as a durable,
  leased artifact queue with startup recovery, hosted on the task manager;
  playback.PrepareFile emits one +faststart MP4. Adds the admin transcode
  toggle and per-artifact LRU cleanup.
- Phase 4: offline progress reconciliation -- a clamped event_at LWW key plus a
  server-assigned synced_seq cursor on watch_progress; an optional clamped
  updated_at on POST /sync/progress and an opaque ?since= cursor on
  GET /progress (additive; existing callers unaffected).

Security & reliability invariants, each with an acceptance test:
1. Server-owned sync ordering: ?since= delta delivery is driven only by the
   server-assigned synced_seq; the client clock is bounded (event_at, clamped
   to now+skew) and used only for last-write-wins on the caller's own profile.
2. Full profile+device authorization on every managed endpoint, with a
   per-profile content/library access re-check before serving any bytes/assets.
3. Durable artifact recovery: a transactionally-claimed (FOR UPDATE SKIP
   LOCKED), lease-heartbeat, attempt-counted queue with a startup sweep, so no
   crash strands a download in preparing and concurrent workers never
   double-encode.

Migrations are timestamped Goose files: reshape downloads (device/format);
download_artifacts (durable queue); watch_progress event_at/synced_seq.

DB-backed acceptance tests skip without SILO_TEST_DATABASE_URL and run in CI;
the invariant-1 progress test also runs against the real SQLite backend locally.

Client repos (silo-android, silo-apple) consume the reshaped /downloads/*
contract and the updated_at/?since= progress fields and require coordinated
follow-up.

Implements the maintainer-approved v1 capability proposal for offline sync
(downloads v2).

AI-use disclosure: implemented by Claude (Claude Code) from the approved design
doc under docs/superpowers/specs, with human review.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(downloads): series & season downloads + client-pull monitoring

Build season downloads and a "monitor a series" capability on top of the
downloads v2 (offline sync for mobile) work.

Season downloads:
- POST /downloads accepts season_number (with series:true) to download one
  season. CreateSeries/CreateSeason share one body via a listEpisodes closure
  and register managed entries under a shared batch_id (original-only). Episode
  files are resolved in a single batched query.

Series monitoring (auto-download), client-driven:
- New device-scoped download_subscriptions table with a Sonarr-style mode
  (all | future | latest_season | specific_seasons), a client-enforced
  delete_watched flag, and a max_storage_bytes cap. The server never deletes
  on-device files; retention and the hard cap are the client's, the server
  only soft-gates registration.
- The client calls POST /downloads/subscriptions/sync on open / background
  refresh; the server registers the in-scope, not-yet-downloaded episodes
  (idempotent via the managed-entry unique index) and the device pulls them on
  its own schedule. No background worker and no dependency on the notifications
  subsystem. latest_season follows new seasons (>= subscribe-time season);
  future excludes the back catalog via air date.
- Subscription CRUD + sync are profile+device authorized (device id from the
  X-Silo-Device-Id header only) with a per-request content-access re-check. The
  capability endpoint advertises season_download / series_monitoring /
  monitoring_modes.

Also lands the downloads-v2 work already present in the tree: durable artifact
(remux/transcode) preparation and offline watch-progress reconciliation, plus
the design-spec updates.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* WIP: epitaxy pre-switch from feat/downloads-v2-offline-sync

* test(downloads): fix deterministic ID collision in reconcile test

Artifact IDs are time-sortable, so two artifacts created in the same
moment share their first 8 chars; combined with a captured timestamp the
two preparing-download IDs collided on downloads_pkey. Use the full
artifact ID, which is unique per row.

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

* fix(downloads): support sqlite userdb backend for managed downloads

With the sqlite userdb backend, profiles live only in per-user SQLite
stores and public.user_profiles stays empty, so user_devices'
profile FK made every managed create/subscription/offline-sync request
fail with an FK violation. Drop the FK (shared Postgres tables must not
FK profile tables — same rule as notifications) and replace the lost
cascade with an app-level purge on profile deletion, wired through
ProfileHandler for both backends. DB-backed regression tests cover the
no-Postgres-profile-row path and the purge cascade.

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

* fix(downloads): dispatch encode kick asynchronously

triggerDrain invoked the kick inline, and the kick (taskmanager RunTask)
executes the encode task on the caller's goroutine — so a POST
/api/v1/downloads with a bitrate quality blocked the HTTP request on the
entire queue drain, ffmpeg encodes included, delaying the 202 by minutes
on an idle queue. Dispatch the kick on a goroutine; the task manager
already serializes concurrent runs.

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

* fix(downloads): enforce per-user quota on the encode pipeline

Two gaps let a user bypass MaxConcurrentPerUser entirely for prepared
downloads: artifact-backed rows are created in 'preparing' (never
'queued'/'downloading'), which CountActiveByUser didn't count, and
createArtifactDownload enqueued the encode job before limiter.Check, so
even a 429-rejected request left a job the worker would transcode.
Count 'preparing' as active and check the limiter before Ensure; managed
replacements stay quota-exempt since they don't add a row.

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

* fix(downloads): protect ephemeral artifact links from LRU eviction

HasActiveLink only counted managed (device_id IS NOT NULL) rows, so
under a byte budget Cleanup could delete an artifact still referenced by
a ready-but-unfetched ephemeral web download — permanently 404ing a row
the API kept listing as ready (the artifact row is gone, so recovery
can't re-queue it). Any non-terminal link now protects the artifact;
only artifacts whose links are all cancelled/failed/revoked are
evictable.

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

* fix(downloads): batch manifests skip bad entries instead of failing whole batch

One deleted or access-filtered episode made GET
/downloads/batches/{id}/manifests 404 for the entire season, so a
client could no longer fetch manifests for the still-valid entries.
Report unbuildable entries in a skipped[] array (revoked | not_found |
error) alongside the delivered manifests, mirroring the create path's
skip idiom. Also cut the batch cost: the shared series detail is
resolved once per batch instead of once per episode, and buildSubtitles
reuses the already-loaded media file instead of re-querying it per
manifest.

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

* fix(migrations): wrap DO block in StatementBegin/End markers

Under NO TRANSACTION goose splits statements on semicolons, so the
dollar-quoted DO block failed every fresh install with 'unterminated
dollar-quoted string' (SQLSTATE 42601). Already-applied databases are
unaffected. Same fix is being applied to main; identical content merges
cleanly.

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

* fix(api): allow season 0 (Specials) in season downloads

season_number was a plain int dispatched with '> 0', so requesting the
Specials season was indistinguishable from omitting the field and
silently broadened to a full-series download. Dispatch on pointer
presence, treat 0 as the Specials season, and reject negatives with 400.

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

* fix(downloads): capability quality_presets is never JSON null

PresetsFor returned a nil slice when downloads are disabled or the user
lacks the permission, and Capability's []string{} initialization was
immediately overwritten by it — so GET /downloads/capability serialized
"quality_presets": null where the contract documents an array.
Normalize at the source so every caller inherits the guarantee.

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

* fix(downloads): subscription sync correctness + batched registration

Three subscription fixes:

- A paused subscription no longer syncs: PATCHing scope (or pausing and
  changing scope in one request) registered episodes for a monitor the
  user had just stopped, inconsistently with SyncSubscriptions' guard.
- SubModeFuture compares calendar days (UTC): air_date is date-only, so
  the strict instant comparison permanently excluded episodes airing the
  same day the user subscribed; episodes with no air date now fall back
  to their ingest time instead of never registering.
- Registration is one batched fetch (GetManagedEntriesByKeys) plus one
  batched INSERT ... ON CONFLICT DO NOTHING RETURNING
  (CreateManagedEntriesBatch) instead of a SELECT+INSERT per episode —
  a 300-episode series cost ~600 sequential round trips per request and
  every no-op sync re-walked the full set. RETURNING yields exactly the
  new rows, so the sync response's 'registered' count now honestly
  reports 0 in the steady state instead of the full in-scope count on
  every app open. The now-unused InsertManagedEntryIfAbsent is removed.

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

* fix(userstore): stamp triggers own the event_at LWW key

MarkProgressBatch (jellycompat series mark-played) advanced updated_at
but never event_at, and both stamp triggers only defaulted event_at when
NULL — so a queued offline event with a client time between the row's
old event_at and the mark could win SetProgressIfNewer and resurrect a
stale resume position that then re-synced to every device.

Make the triggers authoritative instead of adding a tenth hand-written
SET clause: whenever an UPDATE changes updated_at without explicitly
changing event_at, the trigger advances the LWW key; writes that do set
event_at (offline sync's clamped client event time) keep their value.
Postgres gets a CREATE OR REPLACE migration; SQLite gets a v12 userdb
migration that drops and reinstalls the trigger bodies (CREATE TRIGGER
IF NOT EXISTS never replaces). Conformance tests cover both batch paths,
the preserved-client-time invariant, and the v11→v12 upgrade.

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

* fix(downloads): lifecycle hygiene — squash migrations, dead status, stale-row sweeps

Migrations: fold the 20260621 corrective migration back into the base
Downloads V2 migrations (its columns/constraints already exist there)
and fix the reshape Down, which re-added the narrow status CHECK without
collapsing managed-lifecycle rows first — rollback aborted on any DB
with preparing/ready/revoked rows; validated against a live row. Branch
databases that applied the corrective migration need its version row
removed: DELETE FROM goose_db_version WHERE version_id = 20260621020459.

Code: drop the dead 'registered' status (nothing ever wrote it; the
lifecycle is preparing -> ready; 'revoked' stays reserved for the
planned admin revoke flow) along with unused KindDirect and
ErrInvalidFormat.

Sweeps: Cleanup now runs an age-based hygiene pass independent of the
byte budget — cold terminally-failed artifacts (with .part leftovers),
orphaned ready artifacts no download row references, and ephemeral web
rows older than their convenience-record lifetime (also unpinning their
artifacts and bounding GET /downloads growth). The byte budget remains
the disk quota per the limits & restrictions design.

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

* docs(downloads): sync API doc with v2 fixes; HEAD on file route; Android handoff

Document the contract changes from the review fixes: batch-manifest
skipped[] shape, honest subscription 'registered' semantics, season 0 =
Specials, always-array quality_presets, bytes_sent actual behavior,
ephemeral 7-day retention, header-pairing requirement, progress-delta
deletion caveat, and the ready/failed push event schema (new §9.4).
Add an Android client handoff section (§11) mirroring the Apple one,
register HEAD on /downloads/{id}/file for download stacks that probe
before ranged GETs, and add season_number to the web create-request
type. Flag the /direct-download session-token-in-URL tradeoff; a
short-lived download-scoped URL is a follow-up.

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

* refactor: consolidate download/progress helpers, prune dead code, gate sweeps

Behavior-preserving consolidation from the Downloads V2 review:

- appendVideoFilterArgs: one home for the burn-in/hwaccel -vf selection,
  shared by the HLS builder and the single-file prepare builder (the
  drift pattern that already bit tone-mapping once).
- userstore.ResolveProgressState: one home for the min-resume/watched
  threshold rule, replacing five identical copies across both store
  backends and the offline-sync ingest.
- Download file selection ranks resolutions via access.CompareQuality
  (adds 4320p, agrees with playback) instead of a private switch.
- writeSubtitle uses the shared subtitles.SubtitleContentType mapping.
- config.DefaultTranscodeDir replaces three '/tmp/silo-transcode'
  literals.
- Read-side quality/revision defaulting helpers removed: insertArgs plus
  the NOT NULL/CHECK schema already guarantee the invariant.
- Dead code removed: Repository.ListByUser, SubscriptionRepository.
  ListActiveBySeries, and the stale auto-register-worker comments (the
  design is client-pull; no worker exists).
- Redundant left-prefix indexes dropped from the base migrations (their
  unique indexes serve the same prefixes).
- recover()'s disk-presence sweep and the stale-row hygiene sweep run on
  startup then hourly instead of every 30s tick (both are O(cache
  size)).
- gofmt/prettier fixes for pre-existing drift in handlers/playback.go
  and pages/Profiles.tsx.

Deferred (noted for follow-ups): quality-ladder preset table collides
with the drafted download limits & restrictions design, which specifies
its own ladder helper; Download-literal construction consolidation and
the managed-identity value object remain open.

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

* docs(downloads): draft download limits & restrictions design

Design input for the follow-up v1 capability proposal (quality ceiling,
batch size cap, per-user quantity/bandwidth overrides). Committed with
downloads v2 because the remediation work explicitly defers the quality
ladder refactor and revocation wiring to this spec.

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

* fix(progress): reject malformed updated_at; clamp negative progress inputs

Review findings on #258:

- A malformed (non-RFC3339) updated_at in POST /sync/progress previously
  parsed to the zero time, which clampEventAt treated as "now" — letting a
  stale offline event win LWW as a fresh server-time write. The item is now
  rejected with a per-item error instead.
- ResolveProgressState now clamps negative position/duration before
  classification so no backend can persist negative progress through
  UpdateProgress/SetProgress.
- The online-write event_at invariant test is table-driven over both
  SetProgress and UpdateProgress, which share the same contract.

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

* fix(downloads): close review gaps — permission gates, file-access recheck, artifact-true manifests

Review findings on #258:

- UpdateSubscription now applies the same feature/DownloadAllowed gate as
  CreateSubscription and SyncSubscriptions; a PATCH could previously
  re-activate or widen a monitor and register managed rows after an admin
  disabled downloads or revoked the user.
- Serving download bytes (managed and ephemeral) and /direct-download now
  mirror playback's per-file authorization via catalog.FileAllowedByAccess:
  library scope and the profile's max playback quality are re-checked at
  serve time, with artifact-backed rows checked against the artifact's
  resolution (a 720p transcode of a 4K source stays servable under a 1080p
  ceiling).
- Offline manifests for remux/transcode entries now describe the prepared
  artifact (container, codecs, resolution, single selected audio track)
  instead of the catalog source file the client never receives.
- ArtifactRepository.Requeue reports ErrNotFound when the row was
  concurrently swept; ArtifactManager.Ensure recreates the job in that case
  instead of linking downloads to a dead artifact id.
- "No downloadable episodes" is a sentinel (mapped to 404
  no_downloadable_episodes) rather than a bare error that surfaced as 500.
- Subscription season_numbers are bounds-checked (0–9999) before the int32
  narrowing in the repo could silently wrap them.
- HandlePatchDownload reuses requireManaged instead of hand-rolling the
  same managed-identity checks.

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

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 22:05:36 -04:00

1012 lines
32 KiB
Go

package userdb
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/Silo-Server/silo-server/internal/userstore"
)
// WatchProgress is an alias for the canonical type in userstore.
type WatchProgress = userstore.WatchProgress
// WatchHistoryEntry is an alias for the canonical type in userstore.
type WatchHistoryEntry = userstore.WatchHistoryEntry
// UpdateProgress uses the forward-only guard - position only moves forward.
// The position is only updated if the new value is greater than the existing one.
// The completed flag is set to true when position/duration exceeds the watched threshold.
func UpdateProgress(db *sql.DB, profileID, mediaItemID string, position, duration float64, thresholds userstore.ProgressThresholds) error {
position, completed, skip := userstore.ResolveProgressState(position, duration, thresholds)
if skip {
return nil
}
now := nowUTC()
// Mirrors the Postgres pgstore UpdateProgress: `completed` is a one-way
// watched latch; position resets to 0 on completion so a rewatch
// heartbeat on a completed row re-enters Continue Watching through plain
// MAX while the watched flag survives.
query := `
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at)
SELECT ?, ?, ?, ?, ?, ` + visibleTimestampSQL + `
FROM (SELECT 1) seed
LEFT JOIN hidden_history_items hhi
ON hhi.profile_id = ?
AND hhi.media_item_id = ?
WHERE true
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
position_seconds = CASE WHEN excluded.completed = 1 THEN 0
ELSE MAX(excluded.position_seconds, watch_progress.position_seconds) END,
duration_seconds = excluded.duration_seconds,
completed = CASE WHEN excluded.completed = 1
THEN 1 ELSE watch_progress.completed END,
updated_at = excluded.updated_at,
event_at = excluded.updated_at
`
_, err := db.Exec(query, profileID, mediaItemID, position, duration, completed, now, now, profileID, mediaItemID)
if err != nil {
return fmt.Errorf("updating progress: %w", err)
}
return nil
}
// SetProgress bypasses the forward-only guard (for rewatches/explicit seek)
// after the min-resume threshold. The completed flag stays a one-way watched
// latch: only ClearProgress/ClearProgressBatch (mark unwatched) release it.
func SetProgress(db *sql.DB, profileID, mediaItemID string, position, duration float64, thresholds userstore.ProgressThresholds) error {
position, completed, skip := userstore.ResolveProgressState(position, duration, thresholds)
if skip {
return nil
}
now := nowUTC()
query := `
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at)
SELECT ?, ?, ?, ?, ?, ` + visibleTimestampSQL + `
FROM (SELECT 1) seed
LEFT JOIN hidden_history_items hhi
ON hhi.profile_id = ?
AND hhi.media_item_id = ?
WHERE true
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
position_seconds = excluded.position_seconds,
duration_seconds = excluded.duration_seconds,
completed = watch_progress.completed OR excluded.completed,
updated_at = excluded.updated_at,
event_at = excluded.updated_at
`
_, err := db.Exec(query, profileID, mediaItemID, position, duration, completed, now, now, profileID, mediaItemID)
if err != nil {
return fmt.Errorf("setting progress: %w", err)
}
return nil
}
// SetProgressAt writes progress using an explicit timestamp and completion state.
func SetProgressAt(db *sql.DB, profileID, mediaItemID string, position, duration float64, completed bool, updatedAt time.Time) error {
if position < 0 {
position = 0
}
if duration < 0 {
duration = 0
}
if completed {
position = 0
}
if updatedAt.IsZero() {
updatedAt = time.Now().UTC()
}
updatedAtText := updatedAt.UTC().Format(time.RFC3339)
suppressed, err := historyIsHidden(db, profileID, mediaItemID, updatedAtText)
if err != nil {
return err
}
if suppressed {
return nil
}
query := `
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
position_seconds = excluded.position_seconds,
duration_seconds = excluded.duration_seconds,
completed = watch_progress.completed OR excluded.completed,
updated_at = excluded.updated_at,
event_at = excluded.updated_at
`
_, err = db.Exec(query, profileID, mediaItemID, position, duration, completed, updatedAtText)
if err != nil {
return fmt.Errorf("setting progress at time: %w", err)
}
return nil
}
func SetProgressIfNewer(db *sql.DB, profileID, mediaItemID string, position, duration float64, completed bool, updatedAt time.Time) (bool, error) {
if position < 0 {
position = 0
}
if duration < 0 {
duration = 0
}
if completed {
position = 0
}
if updatedAt.IsZero() {
updatedAt = time.Now().UTC()
}
updatedAtText := updatedAt.UTC().Format(time.RFC3339)
suppressed, err := historyIsHidden(db, profileID, mediaItemID, updatedAtText)
if err != nil {
return false, err
}
if suppressed {
return false, nil
}
// event_at is the LWW comparison key (the clamped client event time); the
// synced_seq cursor is stamped server-side by the watch_progress triggers.
res, err := db.Exec(`
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at, event_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
position_seconds = excluded.position_seconds,
duration_seconds = excluded.duration_seconds,
completed = watch_progress.completed OR excluded.completed,
updated_at = excluded.updated_at,
event_at = excluded.event_at
WHERE excluded.event_at > watch_progress.event_at
`, profileID, mediaItemID, position, duration, completed, updatedAtText, updatedAtText)
if err != nil {
return false, fmt.Errorf("setting newer progress: %w", err)
}
rows, err := res.RowsAffected()
if err != nil {
return false, err
}
return rows > 0, nil
}
// MarkWatched creates or replaces progress with a completed entry.
func MarkWatched(db *sql.DB, profileID, mediaItemID string, duration float64) error {
if duration < 0 {
duration = 0
}
now := nowUTC()
query := `
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at)
SELECT ?, ?, 0, ?, 1, ` + visibleTimestampSQL + `
FROM (SELECT 1) seed
LEFT JOIN hidden_history_items hhi
ON hhi.profile_id = ?
AND hhi.media_item_id = ?
WHERE true
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
position_seconds = 0,
duration_seconds = excluded.duration_seconds,
completed = 1,
updated_at = excluded.updated_at,
event_at = excluded.updated_at
`
_, err := db.Exec(query, profileID, mediaItemID, duration, now, now, profileID, mediaItemID)
if err != nil {
return fmt.Errorf("marking watched: %w", err)
}
return nil
}
// ClearProgress removes any saved resume or watched state for an item.
func ClearProgress(db *sql.DB, profileID, mediaItemID string) error {
_, err := db.Exec(
`DELETE FROM watch_progress WHERE profile_id = ? AND media_item_id = ?`,
profileID,
mediaItemID,
)
if err != nil {
return fmt.Errorf("clearing progress: %w", err)
}
return nil
}
// MarkProgressBatch marks every (profile, media_item_id) pair as completed in a
// single SQLite statement.
func MarkProgressBatch(db *sql.DB, profileID string, mediaItemIDs []string, updatedAt time.Time) error {
mediaItemIDs = compactText(mediaItemIDs)
if len(mediaItemIDs) == 0 {
return nil
}
if updatedAt.IsZero() {
updatedAt = time.Now().UTC()
}
updatedAtText := updatedAt.UTC().Format(time.RFC3339)
targetValues := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+4)
for i, mediaItemID := range mediaItemIDs {
targetValues[i] = "(?)"
args = append(args, mediaItemID)
}
args = append(args, updatedAtText, updatedAtText, profileID, profileID)
if _, err := db.Exec(`
WITH target(media_item_id) AS (
VALUES `+strings.Join(targetValues, ",")+`
),
visible AS (
SELECT
t.media_item_id,
`+visibleTimestampSQL+` AS updated_at
FROM target t
LEFT JOIN hidden_history_items hhi
ON hhi.profile_id = ?
AND hhi.media_item_id = t.media_item_id
)
INSERT INTO watch_progress (profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at)
SELECT ?, media_item_id, 0, 0, 1, updated_at
FROM visible
WHERE true
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
completed = 1,
position_seconds = 0,
updated_at = excluded.updated_at
WHERE watch_progress.completed != 1
OR watch_progress.updated_at < excluded.updated_at
`, args...); err != nil {
return fmt.Errorf("marking progress batch: %w", err)
}
return nil
}
// ClearProgressBatch resets every (profile, media_item_id) pair to
// completed=false, position=0 in a single statement. SQLite supports IN(...)
// with placeholders so this is truly one UPDATE.
func ClearProgressBatch(db *sql.DB, profileID string, mediaItemIDs []string, updatedAt time.Time) error {
mediaItemIDs = compactText(mediaItemIDs)
if len(mediaItemIDs) == 0 {
return nil
}
if updatedAt.IsZero() {
updatedAt = time.Now().UTC()
}
updatedAtText := updatedAt.UTC().Format(time.RFC3339)
placeholders := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+2)
args = append(args, updatedAtText, profileID)
for i, mediaItemID := range mediaItemIDs {
placeholders[i] = "?"
args = append(args, mediaItemID)
}
// Clear partially-watched rows (completed = 0 with position_seconds > 0)
// in addition to fully-completed ones — the prior single-item ClearProgress
// path DELETE-d unconditionally, so any non-default state must be cleared.
// Skip rows already in the target state (completed = 0 AND
// position_seconds = 0) to avoid pointless writes.
if _, err := db.Exec(`
UPDATE watch_progress
SET completed = 0, position_seconds = 0, updated_at = ?
WHERE profile_id = ?
AND media_item_id IN (`+strings.Join(placeholders, ",")+`)
AND (completed = 1 OR position_seconds <> 0)`,
args...,
); err != nil {
return fmt.Errorf("clear progress batch: %w", err)
}
return nil
}
// UpdateProgressHints writes version hint columns for an existing progress row.
func UpdateProgressHints(db *sql.DB, profileID, mediaItemID string, hints userstore.VersionHints) error {
_, err := db.Exec(`
UPDATE watch_progress
SET last_file_id = ?, last_resolution = ?, last_hdr = ?, last_codec_video = ?, last_edition_key = ?
WHERE profile_id = ? AND media_item_id = ?`,
hints.FileID, hints.Resolution, hints.HDR, hints.CodecVideo, nilIfBlank(hints.EditionKey),
profileID, mediaItemID,
)
if err != nil {
return fmt.Errorf("updating progress hints: %w", err)
}
return nil
}
// GetProgress returns progress for a specific item, or nil if not found.
func GetProgress(db *sql.DB, profileID, mediaItemID string) (*WatchProgress, error) {
query := `
SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ? AND media_item_id = ?
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)
`
var wp WatchProgress
err := db.QueryRow(query, profileID, mediaItemID).Scan(
&wp.ProfileID, &wp.MediaItemID, &wp.PositionSeconds,
&wp.DurationSeconds, &wp.Completed, &wp.UpdatedAt,
&wp.LastFileID, &wp.LastResolution, &wp.LastHDR, &wp.LastCodecVideo, &wp.LastEditionKey,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("getting progress: %w", err)
}
return &wp, nil
}
// ListProgress returns paginated progress entries, filterable by status.
// Valid status values: "in_progress", "completed", "all" (or empty string for all).
func ListProgress(db *sql.DB, profileID string, status string, limit, offset int) ([]WatchProgress, error) {
var query string
var args []any
switch status {
case "in_progress":
// position_seconds > 0 (not completed = 0): completed rows hold
// position 0, so a rewatch of a watched item has completed = 1 with
// a live resume point and belongs in Continue Watching.
query = `
SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ? AND position_seconds > 0
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)
ORDER BY updated_at DESC
LIMIT ? OFFSET ?
`
args = []any{profileID, limit, offset}
case "completed":
query = `
SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ? AND completed = 1
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)
ORDER BY updated_at DESC
LIMIT ? OFFSET ?
`
args = []any{profileID, limit, offset}
default: // "all" or ""
query = `
SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ?
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)
ORDER BY updated_at DESC
LIMIT ? OFFSET ?
`
args = []any{profileID, limit, offset}
}
rows, err := db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("listing progress: %w", err)
}
defer rows.Close()
var results []WatchProgress
for rows.Next() {
var wp WatchProgress
if err := rows.Scan(
&wp.ProfileID, &wp.MediaItemID, &wp.PositionSeconds,
&wp.DurationSeconds, &wp.Completed, &wp.UpdatedAt,
&wp.LastFileID, &wp.LastResolution, &wp.LastHDR, &wp.LastCodecVideo, &wp.LastEditionKey,
); err != nil {
return nil, fmt.Errorf("scanning progress row: %w", err)
}
results = append(results, wp)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating progress rows: %w", err)
}
return results, nil
}
// ListProgressSince returns watch_progress rows whose server cursor (synced_seq)
// exceeds cursor, in cursor order, along with the next cursor to resume from.
// Delta delivery is driven only by synced_seq, never a client clock (invariant 1).
func ListProgressSince(db *sql.DB, profileID string, cursor int64, limit int) ([]WatchProgress, int64, error) {
if limit <= 0 || limit > 500 {
limit = 500
}
rows, err := db.Query(`
SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at, synced_seq,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ? AND synced_seq IS NOT NULL AND synced_seq > ?
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)
ORDER BY synced_seq ASC
LIMIT ?
`, profileID, cursor, limit)
if err != nil {
return nil, cursor, fmt.Errorf("listing progress since: %w", err)
}
defer rows.Close()
next := cursor
var results []WatchProgress
for rows.Next() {
var wp WatchProgress
var seq int64
if err := rows.Scan(
&wp.ProfileID, &wp.MediaItemID, &wp.PositionSeconds, &wp.DurationSeconds, &wp.Completed, &wp.UpdatedAt, &seq,
&wp.LastFileID, &wp.LastResolution, &wp.LastHDR, &wp.LastCodecVideo, &wp.LastEditionKey,
); err != nil {
return nil, cursor, fmt.Errorf("scanning progress since row: %w", err)
}
if seq > next {
next = seq
}
results = append(results, wp)
}
if err := rows.Err(); err != nil {
return nil, cursor, fmt.Errorf("iterating progress since rows: %w", err)
}
return results, next, nil
}
func ListProgressByMediaItems(db *sql.DB, profileID string, mediaItemIDs []string) (map[string]WatchProgress, error) {
result := make(map[string]WatchProgress, len(mediaItemIDs))
if len(mediaItemIDs) == 0 {
return result, nil
}
placeholders := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+1)
args = append(args, profileID)
for i, mediaItemID := range mediaItemIDs {
placeholders[i] = "?"
args = append(args, mediaItemID)
}
rows, err := db.Query(
`SELECT profile_id, media_item_id, position_seconds, duration_seconds, completed, updated_at,
last_file_id, last_resolution, last_hdr, last_codec_video, last_edition_key
FROM watch_progress
WHERE profile_id = ? AND media_item_id IN (`+strings.Join(placeholders, ",")+`)
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_progress.profile_id
AND hhi.media_item_id = watch_progress.media_item_id
AND watch_progress.updated_at <= hhi.hidden_before
)`,
args...,
)
if err != nil {
return nil, fmt.Errorf("listing progress by media items: %w", err)
}
defer rows.Close()
for rows.Next() {
var wp WatchProgress
if err := rows.Scan(
&wp.ProfileID, &wp.MediaItemID, &wp.PositionSeconds,
&wp.DurationSeconds, &wp.Completed, &wp.UpdatedAt,
&wp.LastFileID, &wp.LastResolution, &wp.LastHDR, &wp.LastCodecVideo, &wp.LastEditionKey,
); err != nil {
return nil, fmt.Errorf("scanning progress row: %w", err)
}
result[wp.MediaItemID] = wp
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating progress rows: %w", err)
}
return result, nil
}
func nilIfBlank(value string) any {
if strings.TrimSpace(value) == "" {
return nil
}
return value
}
// AddHistory adds a watch history entry. If the entry ID is empty, a UUID is generated.
// If WatchedAt is empty, it defaults to the current time.
func AddHistory(db *sql.DB, entry WatchHistoryEntry) error {
if entry.ID == "" {
entry.ID = generateUUID()
}
if entry.WatchedAt == "" {
entry.WatchedAt = nowUTC()
}
if entry.Source == "" {
entry.Source = userstore.WatchHistorySourceLegacy
}
suppressed, err := historyIsHidden(db, entry.ProfileID, entry.MediaItemID, entry.WatchedAt)
if err != nil {
return err
}
if suppressed {
return nil
}
identityJSON, err := json.Marshal(entry.Identity)
if err != nil {
return fmt.Errorf("marshaling watch identity: %w", err)
}
query := `
INSERT INTO watch_history (id, profile_id, media_item_id, watched_at, duration_seconds, completed, source, watch_identity)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`
_, err = db.Exec(query, entry.ID, entry.ProfileID, entry.MediaItemID,
entry.WatchedAt, entry.DurationSeconds, entry.Completed, entry.Source, string(identityJSON))
if err != nil {
return fmt.Errorf("adding history entry: %w", err)
}
return nil
}
func AddVisibleHistory(db *sql.DB, entry WatchHistoryEntry) (WatchHistoryEntry, error) {
if entry.ID == "" {
entry.ID = generateUUID()
}
if entry.WatchedAt == "" {
entry.WatchedAt = nowUTC()
}
if entry.Source == "" {
entry.Source = userstore.WatchHistorySourceLegacy
}
identityJSON, err := json.Marshal(entry.Identity)
if err != nil {
return entry, fmt.Errorf("marshaling watch identity: %w", err)
}
if err := db.QueryRow(`
INSERT INTO watch_history (id, profile_id, media_item_id, watched_at, duration_seconds, completed, source, watch_identity)
SELECT ?, ?, ?, `+visibleTimestampSQL+`, ?, ?, ?, ?
FROM (SELECT 1) seed
LEFT JOIN hidden_history_items hhi
ON hhi.profile_id = ?
AND hhi.media_item_id = ?
WHERE true
RETURNING watched_at
`, entry.ID, entry.ProfileID, entry.MediaItemID, entry.WatchedAt, entry.WatchedAt, entry.DurationSeconds, entry.Completed, entry.Source, string(identityJSON), entry.ProfileID, entry.MediaItemID).Scan(&entry.WatchedAt); err != nil {
return entry, fmt.Errorf("adding visible history entry: %w", err)
}
return entry, nil
}
func AddHistoryIfMissing(db *sql.DB, entry WatchHistoryEntry) (bool, error) {
if entry.WatchedAt == "" {
entry.WatchedAt = nowUTC()
}
suppressed, err := historyIsHidden(db, entry.ProfileID, entry.MediaItemID, entry.WatchedAt)
if err != nil {
return false, err
}
if suppressed {
return false, nil
}
var exists bool
if err := db.QueryRow(
`SELECT EXISTS(
SELECT 1
FROM watch_history
WHERE profile_id = ? AND media_item_id = ? AND watched_at = ?
)`,
entry.ProfileID,
entry.MediaItemID,
entry.WatchedAt,
).Scan(&exists); err != nil {
return false, fmt.Errorf("checking history row existence: %w", err)
}
if exists {
return false, nil
}
if err := AddHistory(db, entry); err != nil {
return false, err
}
return true, nil
}
// ListHistory returns paginated watch history entries ordered by most recent first.
func ListHistory(db *sql.DB, profileID string, limit, offset int) ([]WatchHistoryEntry, error) {
query := `
SELECT h.id, h.profile_id, h.media_item_id, h.watched_at, h.duration_seconds, h.completed, h.source, h.watch_identity
FROM watch_history h
WHERE h.profile_id = ?
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = h.profile_id
AND hhi.media_item_id = h.media_item_id
AND h.watched_at <= hhi.hidden_before
)
ORDER BY watched_at DESC
LIMIT ? OFFSET ?
`
rows, err := db.Query(query, profileID, limit, offset)
if err != nil {
return nil, fmt.Errorf("listing history: %w", err)
}
defer rows.Close()
var results []WatchHistoryEntry
for rows.Next() {
var entry WatchHistoryEntry
var identityJSON string
if err := rows.Scan(
&entry.ID, &entry.ProfileID, &entry.MediaItemID,
&entry.WatchedAt, &entry.DurationSeconds, &entry.Completed, &entry.Source,
&identityJSON,
); err != nil {
return nil, fmt.Errorf("scanning history row: %w", err)
}
if identityJSON != "" && identityJSON != "{}" {
_ = json.Unmarshal([]byte(identityJSON), &entry.Identity)
}
results = append(results, entry)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating history rows: %w", err)
}
return results, nil
}
func ListCompletedHistory(db *sql.DB, query userstore.CompletedHistoryQuery) ([]WatchHistoryEntry, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 500
}
filters, args := completedHistoryFilterSQL(query.ProfileID, query.MediaItemIDs, query.IncludeSources, query.ExcludeSources)
args = append(args, limit, query.Offset)
rows, err := db.Query(`
SELECT h.id, h.profile_id, h.media_item_id, h.watched_at, h.duration_seconds, h.completed, h.source, h.watch_identity
FROM watch_history h
WHERE h.profile_id = ?
AND h.completed = 1
`+filters+completedHistoryVisibleSQL+`
ORDER BY h.watched_at ASC, h.id ASC
LIMIT ? OFFSET ?
`, args...)
if err != nil {
return nil, fmt.Errorf("listing completed history: %w", err)
}
defer rows.Close()
var results []WatchHistoryEntry
for rows.Next() {
var entry WatchHistoryEntry
var identityJSON string
if err := rows.Scan(
&entry.ID, &entry.ProfileID, &entry.MediaItemID,
&entry.WatchedAt, &entry.DurationSeconds, &entry.Completed, &entry.Source,
&identityJSON,
); err != nil {
return nil, fmt.Errorf("scanning completed history row: %w", err)
}
if identityJSON != "" && identityJSON != "{}" {
_ = json.Unmarshal([]byte(identityJSON), &entry.Identity)
}
results = append(results, entry)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating completed history rows: %w", err)
}
return results, nil
}
func ListCompletedHistoryItems(db *sql.DB, query userstore.CompletedHistoryItemQuery) ([]userstore.CompletedHistoryItem, error) {
filters, args := completedHistoryFilterSQL(query.ProfileID, query.MediaItemIDs, query.IncludeSources, query.ExcludeSources)
rows, err := db.Query(`
SELECT h.media_item_id, MAX(h.watched_at)
FROM watch_history h
WHERE h.profile_id = ?
AND h.completed = 1
`+filters+completedHistoryVisibleSQL+`
GROUP BY h.media_item_id
ORDER BY h.media_item_id ASC`,
args...,
)
if err != nil {
return nil, fmt.Errorf("listing completed history items: %w", err)
}
defer rows.Close()
var results []userstore.CompletedHistoryItem
for rows.Next() {
var item userstore.CompletedHistoryItem
if err := rows.Scan(&item.MediaItemID, &item.WatchedAt); err != nil {
return nil, fmt.Errorf("scanning completed history item: %w", err)
}
results = append(results, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating completed history items: %w", err)
}
return results, nil
}
const completedHistoryVisibleSQL = `
AND NOT EXISTS (
SELECT 1
FROM hidden_history_items hhi
WHERE hhi.profile_id = h.profile_id
AND hhi.media_item_id = h.media_item_id
AND h.watched_at <= hhi.hidden_before
)`
const visibleTimestampSQL = `
CASE
WHEN hhi.hidden_before IS NOT NULL AND ? <= hhi.hidden_before
THEN strftime('%Y-%m-%dT%H:%M:%SZ', hhi.hidden_before, '+1 second')
ELSE ?
END`
func completedHistoryFilterSQL(
profileID string,
mediaItemIDs []string,
includeSources []userstore.WatchHistorySource,
excludeSources []userstore.WatchHistorySource,
) (string, []any) {
args := []any{profileID}
var filters strings.Builder
if len(includeSources) > 0 {
placeholders := make([]string, 0, len(includeSources))
for _, source := range includeSources {
placeholders = append(placeholders, "?")
args = append(args, string(source))
}
filters.WriteString(" AND h.source IN (" + strings.Join(placeholders, ",") + ")")
}
if len(excludeSources) > 0 {
placeholders := make([]string, 0, len(excludeSources))
for _, source := range excludeSources {
placeholders = append(placeholders, "?")
args = append(args, string(source))
}
filters.WriteString(" AND h.source NOT IN (" + strings.Join(placeholders, ",") + ")")
}
mediaItemIDs = compactText(mediaItemIDs)
if len(mediaItemIDs) > 0 {
placeholders := make([]string, 0, len(mediaItemIDs))
for _, mediaItemID := range mediaItemIDs {
placeholders = append(placeholders, "?")
args = append(args, mediaItemID)
}
filters.WriteString(" AND h.media_item_id IN (" + strings.Join(placeholders, ",") + ")")
}
return filters.String(), args
}
func RemoveHistoryItems(db *sql.DB, profileID string, mediaItemIDs []string, removedAt time.Time) error {
mediaItemIDs = compactText(mediaItemIDs)
if len(mediaItemIDs) == 0 {
return nil
}
if removedAt.IsZero() {
removedAt = time.Now().UTC()
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("begin remove history items: %w", err)
}
defer tx.Rollback()
removedAtText := removedAt.UTC().Format(time.RFC3339)
targetValues := make([]string, len(mediaItemIDs))
watermarkArgs := make([]any, 0, len(mediaItemIDs)+5)
for i, mediaItemID := range mediaItemIDs {
targetValues[i] = "(?)"
watermarkArgs = append(watermarkArgs, mediaItemID)
}
watermarkArgs = append(watermarkArgs, removedAtText, removedAtText, profileID, profileID, removedAtText)
if _, err := tx.Exec(`
WITH target(media_item_id) AS (
VALUES `+strings.Join(targetValues, ",")+`
),
watermark AS (
SELECT
t.media_item_id,
CASE
WHEN MAX(h.watched_at) IS NOT NULL AND MAX(h.watched_at) > ?
THEN MAX(h.watched_at)
ELSE ?
END AS hidden_before
FROM target t
LEFT JOIN watch_history h
ON h.profile_id = ?
AND h.media_item_id = t.media_item_id
GROUP BY t.media_item_id
)
INSERT INTO hidden_history_items (profile_id, media_item_id, hidden_before, updated_at)
SELECT ?, media_item_id, hidden_before, ?
FROM watermark
WHERE true
ON CONFLICT(profile_id, media_item_id) DO UPDATE SET
hidden_before = CASE
WHEN excluded.hidden_before > hidden_history_items.hidden_before
THEN excluded.hidden_before
ELSE hidden_history_items.hidden_before
END,
updated_at = excluded.updated_at
`, watermarkArgs...); err != nil {
return fmt.Errorf("upserting hidden history items: %w", err)
}
placeholders := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+1)
args = append(args, profileID)
for i, mediaItemID := range mediaItemIDs {
placeholders[i] = "?"
args = append(args, mediaItemID)
}
if _, err := tx.Exec(`
DELETE FROM watch_history
WHERE profile_id = ?
AND media_item_id IN (`+strings.Join(placeholders, ",")+`)
AND watched_at <= (
SELECT hhi.hidden_before
FROM hidden_history_items hhi
WHERE hhi.profile_id = watch_history.profile_id
AND hhi.media_item_id = watch_history.media_item_id
)
`, args...); err != nil {
return fmt.Errorf("deleting removed history rows: %w", err)
}
progressArgs := make([]any, 0, len(mediaItemIDs)+1)
progressArgs = append(progressArgs, profileID)
progressArgs = append(progressArgs, args[1:1+len(mediaItemIDs)]...)
if _, err := tx.Exec(`
DELETE FROM watch_progress
WHERE profile_id = ?
AND media_item_id IN (`+strings.Join(placeholders, ",")+`)
`, progressArgs...); err != nil {
return fmt.Errorf("deleting removed progress rows: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit remove history items: %w", err)
}
return nil
}
func DeleteHistoryBySource(db *sql.DB, profileID string, mediaItemIDs []string, source userstore.WatchHistorySource) error {
if len(mediaItemIDs) == 0 {
return nil
}
placeholders := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+2)
args = append(args, profileID, source)
for i, mediaItemID := range mediaItemIDs {
placeholders[i] = "?"
args = append(args, mediaItemID)
}
_, err := db.Exec(
`DELETE FROM watch_history
WHERE profile_id = ? AND source = ? AND media_item_id IN (`+strings.Join(placeholders, ",")+`)`,
args...,
)
if err != nil {
return fmt.Errorf("deleting history by source: %w", err)
}
return nil
}
func historyIsHidden(db *sql.DB, profileID, mediaItemID, watchedAt string) (bool, error) {
var exists bool
if err := db.QueryRow(`
SELECT EXISTS(
SELECT 1
FROM hidden_history_items
WHERE profile_id = ?
AND media_item_id = ?
AND hidden_before >= ?
)
`, profileID, mediaItemID, watchedAt).Scan(&exists); err != nil {
return false, fmt.Errorf("checking hidden history item: %w", err)
}
return exists, nil
}
func VisibleHistoryTimestamps(db *sql.DB, profileID string, mediaItemIDs []string, at time.Time) (map[string]string, error) {
mediaItemIDs = compactText(mediaItemIDs)
result := make(map[string]string, len(mediaItemIDs))
if len(mediaItemIDs) == 0 {
return result, nil
}
if at.IsZero() {
at = time.Now().UTC()
}
targetValues := make([]string, len(mediaItemIDs))
args := make([]any, 0, len(mediaItemIDs)+1)
for i, mediaItemID := range mediaItemIDs {
targetValues[i] = "(?)"
args = append(args, mediaItemID)
}
args = append(args, profileID)
rows, err := db.Query(`
WITH target(media_item_id) AS (
VALUES `+strings.Join(targetValues, ",")+`
)
SELECT t.media_item_id, hhi.hidden_before
FROM target t
LEFT JOIN hidden_history_items hhi
ON hhi.media_item_id = t.media_item_id
AND hhi.profile_id = ?
`, args...)
if err != nil {
return nil, fmt.Errorf("listing visible history timestamps: %w", err)
}
defer rows.Close()
for rows.Next() {
var mediaItemID string
var hiddenBefore sql.NullString
if err := rows.Scan(&mediaItemID, &hiddenBefore); err != nil {
return nil, fmt.Errorf("scanning visible history timestamp: %w", err)
}
result[mediaItemID] = visibleTimestampAfterHiddenString(at, hiddenBefore)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating visible history timestamps: %w", err)
}
return result, nil
}
func visibleTimestampAfterHiddenString(at time.Time, hiddenBefore sql.NullString) string {
timestamp := at.UTC().Format(time.RFC3339)
if !hiddenBefore.Valid {
return timestamp
}
hiddenAt, err := time.Parse(time.RFC3339, hiddenBefore.String)
if err != nil {
return timestamp
}
if at.UTC().After(hiddenAt) {
return timestamp
}
return hiddenAt.UTC().Add(time.Second).Format(time.RFC3339)
}
func compactText(values []string) []string {
if len(values) == 0 {
return nil
}
result := make([]string, 0, len(values))
seen := make(map[string]struct{}, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if _, ok := seen[value]; ok {
continue
}
seen[value] = struct{}{}
result = append(result, value)
}
return result
}