Files
jellytau/src/lib/services/offlineCatalog.ts
T
dtourolle 62873cab3d feat(search): answer search from a local index; tier downloads by lifetime
Search's instant leg read only downloaded items, so with no downloads it
returned nothing and every keystroke fell through to a full Recursive=true
server query. It now reads the whole synced catalog through the same
availability CTE get_items uses, gated on the same include_catalog_browse
flag so search and browse cannot diverge. (UR-065, DR-108)

Also fixes three defects found while confirming that:

- items_fts grew by a full duplicate index every catalog pass. INSERT OR
  REPLACE fires no AFTER DELETE trigger without recursive_triggers, so the
  old index row was orphaned, and a TEXT PRIMARY KEY meant the replacement
  took a fresh rowid and inserted a second entry. Now a real upsert, with
  migration 021 rebuilding existing indexes. (DR-110)
- DELETE FROM items existed nowhere, so server-side deletions never
  propagated. Adds a post-crawl mark-and-sweep, scoped to crawled types,
  skipping downloaded items, and refusing to run after a partial crawl
  because items.parent_id cascades. (DR-110)
- The index omitted MusicArtist, Playlist and People, which search groups
  results by. Adds them plus people_fts (migration 022). (DR-111)

Re-indexing moves from a frontend startup call to a Rust background task
with a 6h TTL, so a long session no longer searches a stale catalog and a
restart no longer forces a crawl regardless of freshness. (DR-109, IR-030)

Downloads gain a lifetime tier. Eviction selected every completed row by
age with no download_source filter, so hitting the storage limit deleted
the oldest download -- typically one saved deliberately for offline -- to
make room for a precached track. It now reclaims only 'auto' rows, and
expired ones are reclaimed first, before live cache is evicted.
(DR-126, DR-127)

Downloaded video and audio-only handoffs now play from disk instead of
streaming; the video path had never consulted downloads at all. No
transcode is involved: MPV runs video=no and ExoPlayer has no surface for
an Audio item. (DR-123 in part, DR-128)

FTS queries are built as quoted phrases so apostrophes, hyphens and
slashes are data rather than operator syntax, and the item-type filter is
bound rather than interpolated.

Specs: docs/specs/catalog-index-search.md,
docs/specs/read-through-media-cache.md

Includes concurrently-developed favourites browsing and background-audio
stream-end handling; the two workstreams share offline.rs, lib.rs and
online.rs, so no subset of files builds independently.
2026-08-04 17:35:17 +02:00

137 lines
4.9 KiB
TypeScript

// Offline catalog service - "browse & queue" for offline mode.
//
// Two responsibilities:
// 1. Full-catalog pre-sync: while online, walk every library and persist all
// items to the offline cache so the whole catalog is browsable (greyed out)
// offline. Backed by the `syncFullCatalog` Rust command.
// 2. Resume-on-reconnect: when the server becomes reachable again, resolve the
// stream URLs of downloads that were queued while offline and let the pump
// start them. Backed by `resumeQueuedDownloads`.
//
// It also owns the `showServerCatalog` UI flag (the offline banner toggle that
// reveals greyed-out, non-downloaded server media).
//
// TRACES: UR-002, UR-052 | DR-078
import { writable, type Writable } from "svelte/store";
import { commands } from "$lib/api/bindings";
import { auth } from "$lib/stores/auth";
import { isConnected } from "$lib/stores/connectivity";
/**
* When true (and offline), library grids reveal greyed-out versions of media
* that exists on the server but isn't downloaded, each with a button to queue
* a download for the next reconnect. Toggled from the offline banner.
*/
export const showServerCatalog: Writable<boolean> = writable(false);
// Keep the backend's offline library queries in sync with the UI toggle. The
// offline cache holds the whole synced catalog, so `get_items` would otherwise
// return every server item even offline with the toggle off. Include the
// non-downloaded catalog only when online (fast browsing reads the same cache)
// or when the "Show all server media" toggle is on.
let lastIncludeCatalog: boolean | null = null;
function pushCatalogVisibility(connected: boolean, showCatalog: boolean): void {
const include = connected || showCatalog;
if (include === lastIncludeCatalog) return;
lastIncludeCatalog = include;
commands.setShowServerCatalog(include).catch((err) => {
console.warn("[OfflineCatalog] Failed to set catalog visibility:", err);
});
}
let connectedNow = true;
let showCatalogNow = false;
isConnected.subscribe((v) => {
connectedNow = v;
pushCatalogVisibility(connectedNow, showCatalogNow);
});
showServerCatalog.subscribe((v) => {
showCatalogNow = v;
pushCatalogVisibility(connectedNow, showCatalogNow);
});
/** Last time a full catalog sync completed, for a UI hint. */
export const lastCatalogSync: Writable<string | null> = writable(null);
// Guard against overlapping syncs (they can be slow on large libraries).
let syncInProgress = false;
function currentHandle(): string | null {
try {
return auth.getRepository().getHandle();
} catch {
return null; // not connected yet
}
}
/**
* Force a full re-index now, ignoring freshness.
*
* Routine scheduling is the Rust indexer's job (DR-109) — this is the manual
* override, for a "re-index now" affordance. It is deliberately *not* called on
* startup or reconnect any more: doing so forced a full crawl on every launch
* regardless of how fresh the index was.
*
* The backend refuses overlapping passes, so this is safe to call at any time.
*/
export async function syncCatalog(): Promise<void> {
if (syncInProgress) return;
const handle = currentHandle();
if (!handle) return;
syncInProgress = true;
try {
const result = await commands.syncFullCatalog(handle);
console.info(
`[OfflineCatalog] Synced ${result.itemsCached} items (${result.librariesFailed} libraries failed)`
);
await refreshSyncStatus();
} catch (err) {
console.warn("[OfflineCatalog] Full catalog sync failed:", err);
} finally {
syncInProgress = false;
}
}
/**
* Resolve URLs for downloads queued while offline and pump them. Call on
* reconnect. No-ops if not connected.
*/
export async function resumeQueued(): Promise<void> {
const handle = currentHandle();
if (!handle) return;
try {
const result = await commands.resumeQueuedDownloads(handle);
if (result.resolved > 0 || result.failed > 0) {
console.info(
`[OfflineCatalog] Resumed queued downloads: ${result.resolved} resolved, ${result.failed} failed`
);
}
} catch (err) {
console.warn("[OfflineCatalog] Failed to resume queued downloads:", err);
}
}
/** Refresh the last-synced timestamp from the backend. */
export async function refreshSyncStatus(): Promise<void> {
try {
const status = await commands.catalogSyncStatus();
lastCatalogSync.set(status.lastSyncedAt ?? null);
} catch (err) {
console.debug("[OfflineCatalog] Failed to fetch sync status:", err);
}
}
/**
* Called when the server becomes reachable again: resume queued downloads first
* (fast, user-visible), then refresh the catalog in the background.
*/
export async function onReconnected(): Promise<void> {
await resumeQueued();
// Re-indexing on reconnect is the Rust indexer's job (DR-109) — it re-checks
// staleness every tick, so it picks this up without a nudge from here. Queued
// downloads still need resolving from the frontend, which is why this
// function remains.
}