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.
This commit is contained in:
2026-08-04 17:35:17 +02:00
parent c55ff45692
commit 62873cab3d
52 changed files with 6110 additions and 191 deletions
+101
View File
@@ -0,0 +1,101 @@
// TRACES: UR-067, UR-068 | DR-117, DR-119 | UT-105, UT-106
import { describe, it, expect, beforeEach } from "vitest";
import { get } from "svelte/store";
import type { MediaItem } from "$lib/api/types";
import {
favoriteOverrides,
setFavorite,
clearFavorite,
clearAllFavorites,
resolveIsFavorite,
isFavoriteNow,
retainFavorites,
} from "./favorites";
function item(id: string, isFavorite?: boolean): MediaItem {
return {
id,
name: `Item ${id}`,
type: "Movie",
kind: "movie",
isFolder: false,
serverId: "s1",
userData: isFavorite === undefined ? undefined : { isFavorite },
} as unknown as MediaItem;
}
describe("favorites store", () => {
beforeEach(() => clearAllFavorites());
describe("resolveIsFavorite (UT-105)", () => {
it("falls back to the server's userData when nothing was toggled here", () => {
expect(resolveIsFavorite(item("a", true), new Map())).toBe(true);
expect(resolveIsFavorite(item("a", false), new Map())).toBe(false);
});
it("treats an item with no userData as not favourited", () => {
expect(resolveIsFavorite(item("a"), new Map())).toBe(false);
});
it("lets a session override win over userData", () => {
// The whole point: after tapping the heart on a card, the item object
// still carries the server's stale value until the next fetch.
expect(resolveIsFavorite(item("a", false), new Map([["a", true]]))).toBe(true);
expect(resolveIsFavorite(item("a", true), new Map([["a", false]]))).toBe(false);
});
it("is false for a missing item rather than throwing", () => {
expect(resolveIsFavorite(null, new Map())).toBe(false);
expect(resolveIsFavorite(undefined, new Map())).toBe(false);
});
});
describe("overrides", () => {
it("publishes a toggle to subscribers", () => {
setFavorite("a", true);
expect(get(favoriteOverrides).get("a")).toBe(true);
expect(isFavoriteNow(item("a", false))).toBe(true);
setFavorite("a", false);
expect(isFavoriteNow(item("a", true))).toBe(false);
});
it("clearing an override hands authority back to the item's userData", () => {
setFavorite("a", false);
expect(isFavoriteNow(item("a", true))).toBe(false);
clearFavorite("a");
expect(isFavoriteNow(item("a", true))).toBe(true);
});
it("replaces the map so Svelte sees a new reference", () => {
const before = get(favoriteOverrides);
setFavorite("a", true);
expect(get(favoriteOverrides)).not.toBe(before);
});
});
describe("retainFavorites (UT-106)", () => {
it("drops an item un-favourited during this session", () => {
const items = [item("a", true), item("b", true)];
const kept = retainFavorites(items, new Map([["a", false]]));
expect(kept.map((i) => i.id)).toEqual(["b"]);
});
it("keeps everything when nothing was toggled", () => {
const items = [item("a", true), item("b", true)];
expect(retainFavorites(items, new Map())).toHaveLength(2);
});
it("keeps an item favourited during this session even if the server said otherwise", () => {
const items = [item("a", false)];
expect(retainFavorites(items, new Map([["a", true]]))).toHaveLength(1);
});
it("drops items the server never marked as favourites", () => {
// A listing fetched with a stale scope should not keep non-favourites.
expect(retainFavorites([item("a")], new Map())).toHaveLength(0);
});
});
});
+89
View File
@@ -0,0 +1,89 @@
// Favourites overlay — in-session heart state shared across every surface.
//
// The durable record lives in Rust (local `user_data` + the Jellyfin server).
// This store holds only what the *current session* has changed, so a heart
// tapped on a card is reflected on the detail page and the item vanishes from
// the Favourites grid without anyone refetching. It is view state, not truth.
//
// TRACES: UR-068 | DR-119 | UT-105
import { derived, get, writable } from "svelte/store";
import type { MediaItem } from "$lib/api/types";
/** Item id → favourite state set during this session. */
const overrides = writable<Map<string, boolean>>(new Map());
export const favoriteOverrides = { subscribe: overrides.subscribe };
/**
* Record a favourite state locally so every mounted view agrees immediately.
* Called by the toggle service after the optimistic local write.
*/
export function setFavorite(itemId: string, isFavorite: boolean): void {
overrides.update((map) => {
const next = new Map(map);
next.set(itemId, isFavorite);
return next;
});
}
/**
* Forget a session override, so the item's own `userData` is authoritative
* again. Used when the backend reports the server's state changed underneath
* us (`favorites-changed`) — the fresh fetch that follows carries the truth.
*/
export function clearFavorite(itemId: string): void {
overrides.update((map) => {
if (!map.has(itemId)) return map;
const next = new Map(map);
next.delete(itemId);
return next;
});
}
export function clearAllFavorites(): void {
overrides.set(new Map());
}
/**
* Resolution order: a session override wins, then the item's own server-sent
* `userData`, then "not favourited".
*
* The override has to win, or tapping the heart on a card would flip back the
* moment the (unchanged) item object re-rendered.
*
* TRACES: UR-068 | DR-119 | UT-105
*/
export function resolveIsFavorite(
item: Pick<MediaItem, "id" | "userData"> | null | undefined,
overrideMap: Map<string, boolean>
): boolean {
if (!item) return false;
const override = overrideMap.get(item.id);
if (override !== undefined) return override;
return item.userData?.isFavorite ?? false;
}
/** Non-reactive read, for call sites outside a component. */
export function isFavoriteNow(item: Pick<MediaItem, "id" | "userData">): boolean {
return resolveIsFavorite(item, get(overrides));
}
/**
* Drop the items a listing should no longer show once un-favourited.
*
* Pure so it can be unit-tested without mounting the page: un-hearting on the
* Favourites grid must remove the card, while a *newly* favourited item is
* left alone (it belongs to whatever scope the caller fetched).
*
* TRACES: UR-067 | DR-117, DR-119 | UT-106
*/
export function retainFavorites<T extends Pick<MediaItem, "id" | "userData">>(
items: T[],
overrideMap: Map<string, boolean>
): T[] {
return items.filter((item) => resolveIsFavorite(item, overrideMap));
}
/** Count of items still favourited, for "hide the row when empty" decisions. */
export const hasOverrides = derived(overrides, ($o) => $o.size > 0);
+23 -1
View File
@@ -1,5 +1,5 @@
// Home screen data store - featured items, continue watching, recently added
// TRACES: UR-023, UR-024, UR-034, UR-059 | DR-026, DR-027, DR-038, DR-039, DR-089
// TRACES: UR-023, UR-024, UR-034, UR-059, UR-067 | DR-026, DR-027, DR-038, DR-039, DR-089, DR-118
import { writable, derived } from "svelte/store";
import type { MediaItem } from "$lib/api/types";
import { auth } from "./auth";
@@ -12,6 +12,10 @@ interface HomeState {
latestItems: MediaItem[];
recentlyPlayedAudio: MediaItem[];
resumeMovies: MediaItem[];
/** Favourites per scope. Empty rows are not rendered. TRACES: UR-067 | DR-118 */
favoriteMovies: MediaItem[];
favoriteShows: MediaItem[];
favoriteMusic: MediaItem[];
isLoading: boolean;
error: string | null;
}
@@ -24,6 +28,9 @@ function createHomeStore() {
latestItems: [],
recentlyPlayedAudio: [],
resumeMovies: [],
favoriteMovies: [],
favoriteShows: [],
favoriteMusic: [],
isLoading: false,
error: null,
};
@@ -46,6 +53,11 @@ function createHomeStore() {
repo.getLatestItems("", 16),
repo.getRecentlyPlayedAudio(12), // Backend now handles intelligent grouping
repo.getResumeMovies(12),
// Favourites, one request per row. The scope is opaque — Rust decides
// which item types it covers. TRACES: UR-067 | DR-118
repo.getFavorites("movies", { limit: 20 }),
repo.getFavorites("tv", { limit: 20 }),
repo.getFavorites("music", { limit: 20 }),
]);
const valueOr = <T>(i: number, fallback: T): T =>
@@ -60,6 +72,10 @@ function createHomeStore() {
const latest = valueOr(2, [] as typeof initialState.latestItems);
const recentAudio = valueOr(3, [] as typeof initialState.recentlyPlayedAudio);
const resumeMovies = valueOr(4, [] as typeof initialState.resumeMovies);
const emptyResult = { items: [] as MediaItem[], totalRecordCount: 0 };
const favoriteMovies = valueOr(5, emptyResult).items;
const favoriteShows = valueOr(6, emptyResult).items;
const favoriteMusic = valueOr(7, emptyResult).items;
// Use resume items or latest as hero items
const hero = resume.length >= 3 ? resume.slice(0, 5) : latest.slice(0, 5);
@@ -72,6 +88,9 @@ function createHomeStore() {
latestItems: latest,
recentlyPlayedAudio: recentAudio,
resumeMovies: resumeMovies,
favoriteMovies,
favoriteShows,
favoriteMusic,
isLoading: false,
}));
} catch (error) {
@@ -101,4 +120,7 @@ export const nextUpItems = derived(home, $home => $home.nextUpItems);
export const latestItems = derived(home, $home => $home.latestItems);
export const recentlyPlayedAudio = derived(home, $home => $home.recentlyPlayedAudio);
export const resumeMovies = derived(home, $home => $home.resumeMovies);
export const favoriteMovies = derived(home, $home => $home.favoriteMovies);
export const favoriteShows = derived(home, $home => $home.favoriteShows);
export const favoriteMusic = derived(home, $home => $home.favoriteMusic);
export const isHomeLoading = derived(home, $home => $home.isLoading);