From 0030f32a5d09dda593d9ce41e3706bf5a4e2ac65 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 03:31:34 +0200 Subject: [PATCH 1/7] feat(plex): lazy-enrich the full cast on info-page view (S1a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cheap section listing that drives the main sync only stores the top ~3 cast, so any cast member below that was unfilterable (clicking them AND-emptied the grid). Now the movie/show info fetch (/item, /show) — which already pulls the full rich cast — persists it onto cast_names + people_text (so every displayed cast member becomes filterable) and caches each headshot in the new plex_people table (migration 0056). Idempotent (only grows a thinner stored set); best-effort (never 500s a read). Verified: 10 Cloverfield Lane 3->8 cast after viewing; plex_people populated with photos. (Plex cast-explore S1a) --- backend/alembic/versions/0056_plex_people.py | 34 ++++++++++++ backend/app/models.py | 17 ++++++ backend/app/routes/plex.py | 55 ++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 backend/alembic/versions/0056_plex_people.py diff --git a/backend/alembic/versions/0056_plex_people.py b/backend/alembic/versions/0056_plex_people.py new file mode 100644 index 00000000..7c7f6927 --- /dev/null +++ b/backend/alembic/versions/0056_plex_people.py @@ -0,0 +1,34 @@ +"""plex_people: a small cast/crew headshot cache for person filter-chips + +Revision ID: 0056_plex_people +Revises: 0055_channel_keywords +Create Date: 2026-07-19 +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + +revision: str = "0056_plex_people" +down_revision: Union[str, None] = "0055_channel_keywords" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + op.create_table( + "plex_people", + sa.Column("id", sa.Integer(), primary_key=True), + sa.Column("name", sa.String(length=255), nullable=False), + # A representative proxied headshot URL (or NULL when none is available). Captured + # opportunistically from an item's rich metadata (info-page view + the rich re-sync). + sa.Column("photo_thumb", sa.Text(), nullable=True), + sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), + sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), + ) + op.create_index("ix_plex_people_name", "plex_people", ["name"], unique=True) + + +def downgrade() -> None: + op.drop_index("ix_plex_people_name", table_name="plex_people") + op.drop_table("plex_people") diff --git a/backend/app/models.py b/backend/app/models.py index d31a9f85..bbe598bf 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1129,6 +1129,23 @@ class PlexItem(Base, TimestampMixin, UpdatedAtMixin): ) +class PlexPerson(Base, TimestampMixin, UpdatedAtMixin): + """A small cast/crew headshot cache, keyed by name, so person filter-chips (the cards for the + actors currently in the filter) can show a photo + count without a live per-card metadata fetch. + Populated opportunistically when an item's RICH metadata is fetched — the info-page view and the + periodic rich re-sync — since the cheap section listing that drives the main sync only carries the + top few cast. Rows whose name no longer appears in ANY item's cast_names/directors (a library + removal orphaned them) are pruned by the maintenance sweep, mirroring how Plex drops orphaned + metadata. The filterable cast lives in plex_items/plex_shows.cast_names (self-cleaning with the + row); this table is ONLY the headshot cache.""" + + __tablename__ = "plex_people" + + id: Mapped[int] = mapped_column(primary_key=True) + name: Mapped[str] = mapped_column(String(255), unique=True, index=True) + photo_thumb: Mapped[str | None] = mapped_column(Text) # proxied /person-image URL, or None + + class PlexCollection(Base, TimestampMixin, UpdatedAtMixin): """A mirrored Plex collection (a grouping of movies or shows — franchises like Avatar, curated sets like MCU, or auto/community lists like IMDb Top 250). Mirrored in full by the background diff --git a/backend/app/routes/plex.py b/backend/app/routes/plex.py index 61cdd5af..8854ac64 100644 --- a/backend/app/routes/plex.py +++ b/backend/app/routes/plex.py @@ -34,6 +34,7 @@ from app.models import ( PlexItem, PlexLibrary, PlexLink, + PlexPerson, PlexPlaylist, PlexPlaylistItem, PlexSeason, @@ -1221,6 +1222,14 @@ def show_detail( # failure (not configured, HTTP/JSON shape, _rich_meta parse error) must degrade, not 500. pass + # Lazy enrichment: persist the show's full cast so its cast members become filterable (same as the + # movie info page — the sync only stored the listing's top ~3). Best-effort. + if _enrich_people_from_rich(db, sh, rich): + try: + db.commit() + except Exception: + db.rollback() + return { "show": { "id": sh.rating_key, @@ -1627,6 +1636,43 @@ def _rich_meta(meta: dict) -> dict: } +def _enrich_people_from_rich(db: Session, row, rich: dict) -> bool: + """Persist the FULLER cast (from an item's rich metadata) onto the row's `cast_names` + the + `people_text` search blob, and cache each cast member's headshot in plex_people. The cheap section + listing that drives the main sync only carries the top ~3 cast, so this — run when a rich fetch + happens (info-page view + the rich re-sync) — is what makes the full displayed cast filterable. + Returns True if anything was written (the caller commits). Best-effort: never the reason a read + 500s (the caller wraps the commit).""" + cast = rich.get("cast") or [] + names = list(dict.fromkeys(c["name"] for c in cast if c.get("name")))[:24] + if not names: + return False + dirty = False + # Grow/refresh cast_names only when the rich set adds names the stored (listing top-3) set lacks — + # avoid churn and never downgrade a fuller set back to the thin listing. + if set(names) - set(row.cast_names or []): + row.cast_names = names + row.people_text = " ".join(dict.fromkeys(list(row.directors or []) + names)) or None + dirty = True + # Headshot cache: upsert name -> proxied thumb (a later fetch fills a prior NULL; names without a + # whitelisted photo still get a row so a card can show a placeholder). + existing = {p.name: p for p in db.query(PlexPerson).filter(PlexPerson.name.in_(names))} + for c in cast: + nm, th = c.get("name"), c.get("thumb") + if not nm: + continue + p = existing.get(nm) + if p is None: + p = PlexPerson(name=nm, photo_thumb=th) + db.add(p) + existing[nm] = p + dirty = True + elif th and not p.photo_thumb: + p.photo_thumb = th + dirty = True + return dirty + + @router.get("/person-image") def person_image(u: str, request: Request) -> Response: """Proxy a cast/crew photo from Plex's public metadata CDN (host-whitelisted; no token needed). @@ -1779,6 +1825,15 @@ def item_detail( except Exception: pass # metadata extras are best-effort; any live-Plex failure must degrade, not 500 — playback works without them + # Lazy enrichment: the rich fetch above carries the FULL cast (the sync only stored the listing's + # top ~3), so persist it onto this movie so every cast member shown here becomes filterable. A + # movie only — episodes aren't in the grid; their show's cast is enriched from /show. Best-effort. + if it.kind == "movie" and _enrich_people_from_rich(db, it, rich): + try: + db.commit() + except Exception: + db.rollback() + prev_id, next_id = _episode_nav(db, it) show = db.get(PlexShow, it.show_id) if it.kind == "episode" and it.show_id else None return { From 6f28ce36a73e2deef1d6926fca0d53cf0ab76d68 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 03:37:57 +0200 Subject: [PATCH 2/7] feat(plex): actor filter AND/OR toggle (default OR) (S2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Multiple selected actors now combine as OR (any) by default — click several cast and see films featuring ANY of them — instead of the old hardcoded AND that emptied the grid. An AND (all) toggle in the ACTIVE filter section (shown for 2+ actors, mirroring the genre match toggle) narrows to films with ALL of them. Backend actor_mode param mirrors genre_mode. Verified: OR John Goodman|Douglas M. Griffin = 11 films; AND = 1 (10 Cloverfield Lane). (Plex cast-explore S2) --- backend/app/routes/plex.py | 10 +++++++--- frontend/src/components/PlexSidebar.tsx | 17 +++++++++++++++++ frontend/src/lib/api.ts | 4 +++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/backend/app/routes/plex.py b/backend/app/routes/plex.py index 8854ac64..8bf63246 100644 --- a/backend/app/routes/plex.py +++ b/backend/app/routes/plex.py @@ -373,8 +373,10 @@ def _meta_filter_conds(model, p: dict) -> list: conds.append(model.added_at >= cutoff) for d in _csv(p.get("directors")): # AND conds.append(model.directors.contains([d])) - for a in _csv(p.get("actors")): # AND - conds.append(model.cast_names.contains([a])) + alist = _csv(p.get("actors")) + if alist: # AND (all) or OR (any) — default OR, mirroring genre_mode + ac = [model.cast_names.contains([a]) for a in alist] + conds.append(and_(*ac) if p.get("actor_mode") == "all" else or_(*ac)) stds = _csv(p.get("studios")) if stds: # OR conds.append(model.studio.in_(stds)) @@ -405,6 +407,7 @@ class LibraryFilters: added_within: str | None = None directors: str | None = None actors: str | None = None + actor_mode: str = "any" # how multiple actors combine: any (OR, default) | all (AND) studios: str | None = None collection: str | None = None @@ -415,7 +418,8 @@ class LibraryFilters: "year_max": self.year_max, "rating_min": self.rating_min, "duration_min": self.duration_min, "duration_max": self.duration_max, "added_within": self.added_within, "directors": self.directors, - "actors": self.actors, "studios": self.studios, "collection": self.collection, + "actors": self.actors, "actor_mode": self.actor_mode, + "studios": self.studios, "collection": self.collection, } diff --git a/frontend/src/components/PlexSidebar.tsx b/frontend/src/components/PlexSidebar.tsx index d4639a69..744f5b42 100644 --- a/frontend/src/components/PlexSidebar.tsx +++ b/frontend/src/components/PlexSidebar.tsx @@ -211,6 +211,23 @@ export default function PlexSidebar({ onRemove={() => patch({ directors: filters.directors.filter((x) => x !== d) })} /> ))} + {filters.actors.length > 1 && ( +
+ {(["any", "all"] as const).map((m) => ( + + ))} +
+ )} {filters.actors.map((a) => ( Date: Sun, 19 Jul 2026 03:46:31 +0200 Subject: [PATCH 3/7] feat(plex): person cards in the grid for the filtered actors (S3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The actors currently in the filter now render as cards above the poster grid — headshot + name + in-scope title count + a remove x — so you SEE who you're exploring, not just chips in the rail (generalises the old search-only person cards to the applied filter). New GET /plex/people/cards (photo from the plex_people cache, count live against cast_names); PlexBrowse fetches + renders them; i18n en/hu. Verified: John Goodman (11) / Douglas M. Griffin (1) / Bradley Cooper (14) with photos + counts. (Plex cast-explore S3) --- backend/app/routes/plex.py | 45 +++++++++++++++++++++++ frontend/src/components/PlexBrowse.tsx | 49 ++++++++++++++++++++++++++ frontend/src/i18n/locales/en/plex.json | 7 +++- frontend/src/i18n/locales/hu/plex.json | 7 +++- frontend/src/lib/api.ts | 8 +++++ 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/backend/app/routes/plex.py b/backend/app/routes/plex.py index 8bf63246..48c4112d 100644 --- a/backend/app/routes/plex.py +++ b/backend/app/routes/plex.py @@ -1677,6 +1677,51 @@ def _enrich_people_from_rich(db: Session, row, rich: dict) -> bool: return dirty +@router.get("/people/cards") +def people_cards( + names: str, + scope: str = "both", + user: User = Depends(current_user), + db: Session = Depends(get_db), +) -> dict: + """Cards for the actor filter-chips currently applied: each name + its cached headshot + how many + movies/shows (in the given scope) feature them. Photos come from the plex_people cache (populated + as items are rich-fetched — info-page view + the rich re-sync); counts are live against cast_names. + A name with no cached photo yet still returns a card (the UI shows a placeholder).""" + wanted = _csv(names)[:16] + if not wanted: + return {"people": []} + libs = db.query(PlexLibrary).filter_by(enabled=True).all() + movie_lib_ids = [lb.id for lb in libs if lb.kind == "movie"] + show_lib_ids = [lb.id for lb in libs if lb.kind == "show"] + want_movies = scope in ("movie", "both") and bool(movie_lib_ids) + want_shows = scope in ("show", "both") and bool(show_lib_ids) + photos = {p.name: p.photo_thumb for p in db.query(PlexPerson).filter(PlexPerson.name.in_(wanted))} + out = [] + for nm in wanted: + count = 0 + if want_movies: + count += ( + db.query(func.count(PlexItem.id)) + .filter( + PlexItem.kind == "movie", + PlexItem.library_id.in_(movie_lib_ids), + PlexItem.cast_names.contains([nm]), + ) + .scalar() + or 0 + ) + if want_shows: + count += ( + db.query(func.count(PlexShow.id)) + .filter(PlexShow.library_id.in_(show_lib_ids), PlexShow.cast_names.contains([nm])) + .scalar() + or 0 + ) + out.append({"name": nm, "photo": photos.get(nm), "count": count}) + return {"people": out} + + @router.get("/person-image") def person_image(u: str, request: Request) -> Response: """Proxy a cast/crew photo from Plex's public metadata CDN (host-whitelisted; no token needed). diff --git a/frontend/src/components/PlexBrowse.tsx b/frontend/src/components/PlexBrowse.tsx index 03782890..86156c30 100644 --- a/frontend/src/components/PlexBrowse.tsx +++ b/frontend/src/components/PlexBrowse.tsx @@ -15,6 +15,8 @@ import { RotateCcw, Star, Tv2, + User, + X, type LucideIcon, } from "lucide-react"; import { @@ -136,6 +138,17 @@ export default function PlexBrowse() { const items = browseQ.data?.pages.flatMap((p) => p.items) ?? []; const total = browseQ.data?.pages[0]?.total ?? 0; + // Cards for the actors currently in the filter (name + headshot + in-scope count) — shown above the + // grid so you SEE who you're exploring, not just chips in the rail. + const peopleCardsQ = useQuery({ + queryKey: ["plex-people-cards", scope, filters.actors], + queryFn: () => api.plexPeopleCards(filters.actors, scope), + enabled: sub.view.kind === "grid" && filters.actors.length > 0, + staleTime: 60_000, + }); + const personCards = peopleCardsQ.data?.people ?? []; + const removeActor = (name: string) => + setFilters({ ...filters, actors: filters.actors.filter((x) => x !== name) }); // Episode matches (grouped "Episodes" section) — search-only; same set on every page, take page 0. const episodes = browseQ.data?.pages[0]?.episodes ?? []; @@ -280,6 +293,42 @@ export default function PlexBrowse() { + {personCards.length > 0 && ( +
+ {personCards.map((p) => ( +
+ {p.photo ? ( + + ) : ( +
+ +
+ )} +
+
{p.name}
+
{t("plex.people.count", { count: p.count })}
+
+ +
+ ))} +
+ )} + {browseQ.isLoading ? (

{t("plex.loading")}

) : items.length === 0 && episodes.length === 0 ? ( diff --git a/frontend/src/i18n/locales/en/plex.json b/frontend/src/i18n/locales/en/plex.json index 9b78e0a0..9f016131 100644 --- a/frontend/src/i18n/locales/en/plex.json +++ b/frontend/src/i18n/locales/en/plex.json @@ -225,5 +225,10 @@ "addShow": "Add whole show to a playlist", "addSeason": "Add season to a playlist", "addEpisode": "Add episode to a playlist" + }, + "people": { + "count_one": "{{count}} title", + "count_other": "{{count}} titles", + "remove": "Remove from filter" } -} +} \ No newline at end of file diff --git a/frontend/src/i18n/locales/hu/plex.json b/frontend/src/i18n/locales/hu/plex.json index e9220996..c91be3a0 100644 --- a/frontend/src/i18n/locales/hu/plex.json +++ b/frontend/src/i18n/locales/hu/plex.json @@ -225,5 +225,10 @@ "addShow": "Egész sorozat listához adása", "addSeason": "Évad listához adása", "addEpisode": "Epizód listához adása" + }, + "people": { + "count_one": "{{count}} cím", + "count_other": "{{count}} cím", + "remove": "Eltávolítás a szűrőből" } -} +} \ No newline at end of file diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index d3259058..aae94e6a 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -828,6 +828,11 @@ export interface PlexCastMember { role?: string | null; thumb?: string | null; // proxied person-image url, or null when Plex has no photo } +export interface PlexPersonCard { + name: string; + photo?: string | null; // proxied person-image url, or null (placeholder) + count: number; // in-scope movies/shows featuring them +} // The expanded Plex browse filters (movie libraries). Persisted per-account as JSON. export interface PlexFilters { genres: string[]; @@ -1351,6 +1356,9 @@ export const api = { req(`/api/plex/season/${encodeURIComponent(rk)}/state`, { method: "POST", body: JSON.stringify({ watched }) }), plexItem: (id: string): Promise => req(`/api/plex/item/${encodeURIComponent(id)}`), + // Cards for the actor filter-chips currently applied: name + cached headshot + in-scope film/show count. + plexPeopleCards: (names: string[], scope: string): Promise<{ people: PlexPersonCard[] }> => + req(`/api/plex/people/cards?scope=${encodeURIComponent(scope)}&names=${encodeURIComponent(names.join(","))}`), plexSession: (id: string, start = 0, audio?: number | null, aoff = 0, multi = false): Promise => { const p = new URLSearchParams({ start: String(Math.max(0, Math.floor(start))) }); if (audio != null) p.set("audio", String(audio)); From 33c60965d9297625f8b21fded8957fbfa9a069c0 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 03:50:00 +0200 Subject: [PATCH 4/7] feat(plex): prune orphaned people after each sync (S4) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The plex_people headshot cache can outlive its subjects — when a title leaves the Plex library, cast/crew who appear in nothing else are orphaned. prune_orphan_people deletes rows whose name no longer appears in ANY movie/show cast_names/directors, run at the end of every plex_sync (best-effort; a jsonb_typeof='array' guard skips scalar/null JSONB values). The filterable cast_names self-clean with their row; this cleans the only per-person cache. Verified: an injected orphan is dropped, real people kept. (Plex cast-explore S4) --- backend/app/plex/sync.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/backend/app/plex/sync.py b/backend/app/plex/sync.py index cd4201f3..d5655844 100644 --- a/backend/app/plex/sync.py +++ b/backend/app/plex/sync.py @@ -12,6 +12,7 @@ Cast + intro/credit markers are fetched lazily on the item-detail/player path (P import logging from datetime import datetime, timezone +from sqlalchemy import text from sqlalchemy.orm import Session from app import sysconfig @@ -135,10 +136,36 @@ def sync(db: Session) -> dict: db.rollback() log.warning("Plex sync failed: %s", e) return {"error": str(e)} + # Prune the headshot cache: a reconcile may have removed titles, orphaning people who no longer + # appear in ANY item's cast/directors — drop those rows, mirroring how Plex drops orphaned metadata. + try: + stats["people_pruned"] = prune_orphan_people(db) + except Exception as e: # best-effort housekeeping — never fail the sync over it + log.warning("Plex people prune failed: %s", e) log.info("Plex sync done: %s", stats) return stats +# The person names still referenced by ANY movie/show (cast OR directors). plex_people rows outside +# this set are orphans left by a library removal. +_REFERENCED_PEOPLE_SQL = """ + SELECT jsonb_array_elements_text(cast_names) FROM plex_items WHERE jsonb_typeof(cast_names) = 'array' + UNION SELECT jsonb_array_elements_text(directors) FROM plex_items WHERE jsonb_typeof(directors) = 'array' + UNION SELECT jsonb_array_elements_text(cast_names) FROM plex_shows WHERE jsonb_typeof(cast_names) = 'array' + UNION SELECT jsonb_array_elements_text(directors) FROM plex_shows WHERE jsonb_typeof(directors) = 'array' +""" + + +def prune_orphan_people(db: Session) -> int: + """Delete plex_people (headshot cache) rows whose name no longer appears in any movie/show cast or + directors — orphaned by a library removal. Returns the number pruned. Commits its own work.""" + res = db.execute( + text(f"DELETE FROM plex_people WHERE name NOT IN ({_REFERENCED_PEOPLE_SQL})") + ) + db.commit() + return res.rowcount or 0 + + def _upsert_library(db: Session, key: str, title: str, kind: str) -> PlexLibrary: lib = db.query(PlexLibrary).filter_by(plex_key=key).first() if lib is None: From d91f27a22e587f7969b8790de5f3de0c9fa293a4 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 03:53:03 +0200 Subject: [PATCH 5/7] fix(plex): NULL-safe orphan-people prune (review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guard the prune's NOT IN against the classic NULL trap: a JSON-null cast array element would surface as SQL NULL in the referenced set, making name NOT IN (…, NULL) evaluate NULL for every row → silently prune nothing. Filter NULLs out of the subquery. (Plex cast-explore S4 review) --- backend/app/plex/sync.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/app/plex/sync.py b/backend/app/plex/sync.py index d5655844..0eca551a 100644 --- a/backend/app/plex/sync.py +++ b/backend/app/plex/sync.py @@ -159,8 +159,14 @@ _REFERENCED_PEOPLE_SQL = """ def prune_orphan_people(db: Session) -> int: """Delete plex_people (headshot cache) rows whose name no longer appears in any movie/show cast or directors — orphaned by a library removal. Returns the number pruned. Commits its own work.""" + # `WHERE ref IS NOT NULL` matters: a JSON-null array element would surface as a SQL NULL in the + # referenced set, and `name NOT IN (…, NULL)` is NULL (never TRUE) for every row — the classic + # NOT IN trap that would silently prune nothing. Filtering NULLs out keeps the delete correct. res = db.execute( - text(f"DELETE FROM plex_people WHERE name NOT IN ({_REFERENCED_PEOPLE_SQL})") + text( + "DELETE FROM plex_people WHERE name NOT IN " + f"(SELECT ref FROM ({_REFERENCED_PEOPLE_SQL}) AS refs(ref) WHERE ref IS NOT NULL)" + ) ) db.commit() return res.rowcount or 0 From 12d17ec7b0377c9ab44cee8d1768874b208e4a02 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 04:31:02 +0200 Subject: [PATCH 6/7] polish(plex): match the feed rail's segmented-pill look for the any/all toggle MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The genre + actor MATCH toggles were a boxed button row (rounded-lg border, no track); restyle them as the feed rail's tag-match toggle — a rounded-full track (border + bg-card + p-0.5) with an inset rounded-full active pill, plus a MATCH label. Extracted a shared MatchToggle so both read identically. i18n plex.filter.matchLabel (en/hu). (Plex cast-explore polish) --- frontend/src/components/PlexSidebar.tsx | 75 +++++++++++++++---------- frontend/src/i18n/locales/en/plex.json | 3 +- frontend/src/i18n/locales/hu/plex.json | 3 +- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/frontend/src/components/PlexSidebar.tsx b/frontend/src/components/PlexSidebar.tsx index 744f5b42..824bfa61 100644 --- a/frontend/src/components/PlexSidebar.tsx +++ b/frontend/src/components/PlexSidebar.tsx @@ -33,6 +33,43 @@ const DURATION_BUCKETS: { key: string; min: number | null; max: number | null }[ { key: "long", min: 120 * 60, max: null }, ]; +// The any/all combine toggle for a multi-value filter (genres, actors). Same segmented-pill look as +// the feed rail's tag MATCH toggle (rounded-full track + inset active pill), not a boxed button row. +function MatchToggle({ + mode, + onChange, +}: { + mode: "any" | "all"; + onChange: (m: "any" | "all") => void; +}) { + const { t } = useTranslation(); + return ( +
+ + {t("plex.filter.matchLabel")} + +
+ {(["any", "all"] as const).map((m) => ( + + ))} +
+
+ ); +} + export default function PlexSidebar({ layout, setLayout, @@ -212,20 +249,11 @@ export default function PlexSidebar({ /> ))} {filters.actors.length > 1 && ( -
- {(["any", "all"] as const).map((m) => ( - - ))} +
+ patch({ actorMode: m })} + />
)} {filters.actors.map((a) => ( @@ -312,21 +340,10 @@ export default function PlexSidebar({ body: ( <> {filters.genres.length > 1 && ( -
- {(["any", "all"] as const).map((m) => ( - - ))} -
+ patch({ genreMode: m })} + /> )} {facets.genres.map((g) => ( diff --git a/frontend/src/i18n/locales/en/plex.json b/frontend/src/i18n/locales/en/plex.json index 9f016131..6c06b02a 100644 --- a/frontend/src/i18n/locales/en/plex.json +++ b/frontend/src/i18n/locales/en/plex.json @@ -57,7 +57,8 @@ }, "collection": "Collection", "collectionSearch": "Search collections…", - "collectionEmpty": "No collections" + "collectionEmpty": "No collections", + "matchLabel": "Match" }, "count": "{{count}} titles", "searchCount": "{{count}} matches", diff --git a/frontend/src/i18n/locales/hu/plex.json b/frontend/src/i18n/locales/hu/plex.json index c91be3a0..92809e5e 100644 --- a/frontend/src/i18n/locales/hu/plex.json +++ b/frontend/src/i18n/locales/hu/plex.json @@ -57,7 +57,8 @@ }, "collection": "Kollekció", "collectionSearch": "Kollekciók keresése…", - "collectionEmpty": "Nincs kollekció" + "collectionEmpty": "Nincs kollekció", + "matchLabel": "Egyezés" }, "count": "{{count}} cím", "searchCount": "{{count}} találat", From af6faf476f1f5cc3672b54f3a93c1254c4f2bd05 Mon Sep 17 00:00:00 2001 From: npeter83 Date: Sun, 19 Jul 2026 04:50:45 +0200 Subject: [PATCH 7/7] feat(plex): full-library cast backfill via a batched rich re-sync (S1b) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lazy enrichment only covers viewed titles, so an actor's count/results were partial. Add a background backfill: each plex_sync rich-fetches a bounded batch (60) of not-yet-enriched movies/shows and persists their FULL cast, stamping people_enriched_at so each is fetched once — the whole library is covered over successive runs (migration 0057 adds the marker). Extracted the enrichment into a shared app/plex/people.py (cast_from_meta / enrich_row_people / prune_orphan_people + the person-image host/proxy) so the info-page lazy path and the batch share ONE implementation — no duplication; the info page also stamps the marker so the batch skips it. Verified: an 8-item batch grows those movies' cast_names 3->24 and fills plex_people (32->224); the info-page cast is intact after the _rich_meta refactor. (Plex cast-explore S1b) --- .../versions/0057_plex_ppl_enriched.py | 25 +++++ backend/app/models.py | 4 + backend/app/plex/people.py | 97 +++++++++++++++++++ backend/app/plex/sync.py | 66 ++++++++----- backend/app/routes/plex.py | 79 +++------------ 5 files changed, 184 insertions(+), 87 deletions(-) create mode 100644 backend/alembic/versions/0057_plex_ppl_enriched.py create mode 100644 backend/app/plex/people.py diff --git a/backend/alembic/versions/0057_plex_ppl_enriched.py b/backend/alembic/versions/0057_plex_ppl_enriched.py new file mode 100644 index 00000000..528d8554 --- /dev/null +++ b/backend/alembic/versions/0057_plex_ppl_enriched.py @@ -0,0 +1,25 @@ +"""plex_items/plex_shows.people_enriched_at: full-cast backfill marker + +Revision ID: 0057_plex_ppl_enriched +Revises: 0056_plex_people +Create Date: 2026-07-19 +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + +revision: str = "0057_plex_ppl_enriched" +down_revision: Union[str, None] = "0056_plex_people" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + for tbl in ("plex_items", "plex_shows"): + op.add_column(tbl, sa.Column("people_enriched_at", sa.DateTime(timezone=True), nullable=True)) + + +def downgrade() -> None: + for tbl in ("plex_items", "plex_shows"): + op.drop_column(tbl, "people_enriched_at") diff --git a/backend/app/models.py b/backend/app/models.py index bbe598bf..3e67cfd9 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1027,6 +1027,8 @@ class PlexShow(Base, TimestampMixin, UpdatedAtMixin): directors: Mapped[list | None] = mapped_column(JSONB) cast_names: Mapped[list | None] = mapped_column(JSONB) people_text: Mapped[str | None] = mapped_column(Text) # cast+directors, folded into search_vector + # Set once the show's FULL cast has been rich-fetched by the backfill; NULL = still the listing top-3. + people_enriched_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) search_vector: Mapped[object | None] = mapped_column( TSVECTOR, Computed( @@ -1118,6 +1120,8 @@ class PlexItem(Base, TimestampMixin, UpdatedAtMixin): # Cast + director names, space-joined — folded into search_vector (weight B) so the search box # finds titles by a person's name (and ranks them above summary-only mentions). people_text: Mapped[str | None] = mapped_column(Text) + # Set once the movie's FULL cast has been rich-fetched by the backfill; NULL = still the listing top-3. + people_enriched_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) search_vector: Mapped[object | None] = mapped_column( TSVECTOR, Computed( diff --git a/backend/app/plex/people.py b/backend/app/plex/people.py new file mode 100644 index 00000000..7bcb57c2 --- /dev/null +++ b/backend/app/plex/people.py @@ -0,0 +1,97 @@ +"""Shared Plex people helpers: parse cast/crew from rich metadata, persist the full cast onto a movie +or show (cast_names + the people_text search blob + the plex_people headshot cache), and prune +orphaned people. Used by BOTH the info-page lazy enrichment (routes.plex) and the background rich +re-sync (plex.sync), so the two paths never diverge.""" + +from urllib.parse import quote + +from sqlalchemy import text +from sqlalchemy.orm import Session + +from app.models import PlexPerson + +# Cast/crew photos come from Plex's PUBLIC metadata CDN (no token). We proxy them (host-whitelisted) +# so the user's browser makes no third-party request (privacy + self-host CSP friendliness). +PERSON_IMG_HOST = "https://metadata-static.plex.tv/" + + +def person_thumb_url(thumb: str | None) -> str | None: + """Proxied /person-image URL for a Plex CDN headshot, or None when it's missing / off-CDN.""" + t = str(thumb or "") + return f"/api/plex/person-image?u={quote(t, safe='')}" if t.startswith(PERSON_IMG_HOST) else None + + +def cast_from_meta(meta: dict, limit: int = 24) -> list[dict]: + """[{name, role, thumb}] for the cast (Role tags) in a full Plex metadata dict — thumb is the + proxied person-image URL or None. Shared by the info-page response, the lazy enrichment, and the + re-sync so the parsing lives in one place.""" + out: list[dict] = [] + for role in (meta.get("Role") or [])[:limit]: + name = role.get("tag") + if not name: + continue + out.append( + {"name": name, "role": role.get("role"), "thumb": person_thumb_url(role.get("thumb"))} + ) + return out + + +def enrich_row_people(db: Session, row, cast: list[dict]) -> bool: + """Persist the FULLER cast onto the row's `cast_names` + `people_text`, and cache each cast + member's headshot in plex_people. The cheap section listing that drives the main sync only carries + the top ~3 cast, so this — run whenever a rich fetch happens (info-page view + the rich re-sync) — + is what makes the full displayed cast filterable. Returns True if anything was written (the caller + commits). Best-effort by contract: never the reason a read 500s.""" + names = list(dict.fromkeys(c["name"] for c in cast if c.get("name")))[:24] + if not names: + return False + dirty = False + # Grow/refresh cast_names only when the rich set adds names the stored (listing top-3) set lacks — + # avoid churn and never downgrade a fuller set back to the thin listing. + if set(names) - set(row.cast_names or []): + row.cast_names = names + row.people_text = " ".join(dict.fromkeys(list(row.directors or []) + names)) or None + dirty = True + # Headshot cache: upsert name -> proxied thumb (a later fetch fills a prior NULL; names without a + # whitelisted photo still get a row so a card can show a placeholder). + existing = {p.name: p for p in db.query(PlexPerson).filter(PlexPerson.name.in_(names))} + for c in cast: + nm, th = c.get("name"), c.get("thumb") + if not nm: + continue + p = existing.get(nm) + if p is None: + p = PlexPerson(name=nm, photo_thumb=th) + db.add(p) + existing[nm] = p + dirty = True + elif th and not p.photo_thumb: + p.photo_thumb = th + dirty = True + return dirty + + +# The person names still referenced by ANY movie/show (cast OR directors). plex_people rows outside +# this set are orphans left by a library removal. +_REFERENCED_PEOPLE_SQL = """ + SELECT jsonb_array_elements_text(cast_names) FROM plex_items WHERE jsonb_typeof(cast_names) = 'array' + UNION SELECT jsonb_array_elements_text(directors) FROM plex_items WHERE jsonb_typeof(directors) = 'array' + UNION SELECT jsonb_array_elements_text(cast_names) FROM plex_shows WHERE jsonb_typeof(cast_names) = 'array' + UNION SELECT jsonb_array_elements_text(directors) FROM plex_shows WHERE jsonb_typeof(directors) = 'array' +""" + + +def prune_orphan_people(db: Session) -> int: + """Delete plex_people (headshot cache) rows whose name no longer appears in any movie/show cast or + directors — orphaned by a library removal. Returns the number pruned. Commits its own work.""" + # `WHERE ref IS NOT NULL` matters: a JSON-null array element surfaces as a SQL NULL in the + # referenced set, and `name NOT IN (…, NULL)` is NULL (never TRUE) for every row — the classic + # NOT IN trap that would silently prune nothing. Filtering NULLs out keeps the delete correct. + res = db.execute( + text( + "DELETE FROM plex_people WHERE name NOT IN " + f"(SELECT ref FROM ({_REFERENCED_PEOPLE_SQL}) AS refs(ref) WHERE ref IS NOT NULL)" + ) + ) + db.commit() + return res.rowcount or 0 diff --git a/backend/app/plex/sync.py b/backend/app/plex/sync.py index 0eca551a..058349d3 100644 --- a/backend/app/plex/sync.py +++ b/backend/app/plex/sync.py @@ -12,11 +12,11 @@ Cast + intro/credit markers are fetched lazily on the item-detail/player path (P import logging from datetime import datetime, timezone -from sqlalchemy import text from sqlalchemy.orm import Session from app import sysconfig from app.models import PlexCollection, PlexItem, PlexLibrary, PlexSeason, PlexShow +from app.plex import people from app.plex.client import PlexClient, PlexError, PlexNotConfigured log = logging.getLogger("siftlode.plex") @@ -130,6 +130,12 @@ def sync(db: Session) -> dict: _sync_shows(db, plex, lib, stats) _sync_collections(db, plex, lib, stats) db.commit() + # Rich-fetch a bounded batch of not-yet-enriched titles' FULL cast, so the whole library + # becomes filterable over successive runs (the cheap listing above only stored the top ~3). + try: + stats["people_enriched"] = _enrich_people_batch(db, plex) + except Exception as e: # best-effort — never fail the sync over the backfill + log.warning("Plex people enrich batch failed: %s", e) except PlexNotConfigured as e: return {"skipped": str(e)} except PlexError as e: @@ -139,37 +145,53 @@ def sync(db: Session) -> dict: # Prune the headshot cache: a reconcile may have removed titles, orphaning people who no longer # appear in ANY item's cast/directors — drop those rows, mirroring how Plex drops orphaned metadata. try: - stats["people_pruned"] = prune_orphan_people(db) + stats["people_pruned"] = people.prune_orphan_people(db) except Exception as e: # best-effort housekeeping — never fail the sync over it log.warning("Plex people prune failed: %s", e) log.info("Plex sync done: %s", stats) return stats -# The person names still referenced by ANY movie/show (cast OR directors). plex_people rows outside -# this set are orphans left by a library removal. -_REFERENCED_PEOPLE_SQL = """ - SELECT jsonb_array_elements_text(cast_names) FROM plex_items WHERE jsonb_typeof(cast_names) = 'array' - UNION SELECT jsonb_array_elements_text(directors) FROM plex_items WHERE jsonb_typeof(directors) = 'array' - UNION SELECT jsonb_array_elements_text(cast_names) FROM plex_shows WHERE jsonb_typeof(cast_names) = 'array' - UNION SELECT jsonb_array_elements_text(directors) FROM plex_shows WHERE jsonb_typeof(directors) = 'array' -""" +# How many titles to rich-fetch per sync run for the cast backfill — bounds the extra Plex load; the +# whole library is covered over successive runs (each fetched item is stamped people_enriched_at once). +_ENRICH_BATCH = 60 -def prune_orphan_people(db: Session) -> int: - """Delete plex_people (headshot cache) rows whose name no longer appears in any movie/show cast or - directors — orphaned by a library removal. Returns the number pruned. Commits its own work.""" - # `WHERE ref IS NOT NULL` matters: a JSON-null array element would surface as a SQL NULL in the - # referenced set, and `name NOT IN (…, NULL)` is NULL (never TRUE) for every row — the classic - # NOT IN trap that would silently prune nothing. Filtering NULLs out keeps the delete correct. - res = db.execute( - text( - "DELETE FROM plex_people WHERE name NOT IN " - f"(SELECT ref FROM ({_REFERENCED_PEOPLE_SQL}) AS refs(ref) WHERE ref IS NOT NULL)" - ) +def _enrich_people_batch(db: Session, plex: PlexClient, limit: int = _ENRICH_BATCH) -> int: + """Rich-fetch the FULL cast of up to `limit` not-yet-enriched movies/shows and persist it (via the + shared enrich_row_people). Oldest-id first, movies before shows. Stamps people_enriched_at only on + a SUCCESSFUL fetch, so a transient per-item failure retries next run rather than being lost. + Returns how many items were enriched this run.""" + movies = ( + db.query(PlexItem) + .filter(PlexItem.kind == "movie", PlexItem.people_enriched_at.is_(None)) + .order_by(PlexItem.id) + .limit(limit) + .all() ) + remaining = limit - len(movies) + shows = ( + db.query(PlexShow) + .filter(PlexShow.people_enriched_at.is_(None)) + .order_by(PlexShow.id) + .limit(remaining) + .all() + if remaining > 0 + else [] + ) + now = datetime.now(timezone.utc) + n = 0 + for row in [*movies, *shows]: + try: + meta = plex.metadata(row.rating_key) or {} + except Exception as e: + log.debug("Plex people enrich fetch failed for %s: %s", row.rating_key, e) + continue # leave people_enriched_at NULL so it's retried on a later run + people.enrich_row_people(db, row, people.cast_from_meta(meta)) + row.people_enriched_at = now + n += 1 db.commit() - return res.rowcount or 0 + return n def _upsert_library(db: Session, key: str, title: str, kind: str) -> PlexLibrary: diff --git a/backend/app/routes/plex.py b/backend/app/routes/plex.py index 48c4112d..95c42a59 100644 --- a/backend/app/routes/plex.py +++ b/backend/app/routes/plex.py @@ -17,7 +17,6 @@ from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass from datetime import datetime, timedelta, timezone from pathlib import Path -from urllib.parse import quote import httpx from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query, Request, Response @@ -43,6 +42,7 @@ from app.models import ( User, ) from app.plex import paths as plex_paths +from app.plex import people as plex_people from app.plex import stream as plex_stream from app.plex import sync as plex_sync from app.plex import watch_sync as plex_watch @@ -1227,8 +1227,11 @@ def show_detail( pass # Lazy enrichment: persist the show's full cast so its cast members become filterable (same as the - # movie info page — the sync only stored the listing's top ~3). Best-effort. - if _enrich_people_from_rich(db, sh, rich): + # movie info page — the sync only stored the listing's top ~3), and stamp people_enriched_at so the + # backfill skips it. Best-effort. + if rich.get("cast"): + plex_people.enrich_row_people(db, sh, rich["cast"]) + sh.people_enriched_at = datetime.now(timezone.utc) try: db.commit() except Exception: @@ -1582,11 +1585,6 @@ _PROGRESS_MIN_S = 5 _FINISH_MARGIN_S = 30 -# Cast/crew photos come from Plex's PUBLIC metadata CDN (no token). We proxy them (host-whitelisted) -# so the user's browser makes no third-party request (privacy + self-host CSP friendliness). -_PERSON_IMG_HOST = "https://metadata-static.plex.tv/" - - def _rich_meta(meta: dict) -> dict: """Pull the "info page" extras out of a full Plex metadata dict: an IMDb score + link, content rating, genres, director(s), studio, tagline, and cast (name/role/photo). Best-effort — any @@ -1612,21 +1610,6 @@ def _rich_meta(meta: dict) -> dict: if gid.startswith("imdb://"): imdb_id = gid.split("imdb://", 1)[1] break - cast = [] - for role in (meta.get("Role") or [])[:24]: - name = role.get("tag") - if not name: - continue - thumb = str(role.get("thumb") or "") - cast.append( - { - "name": name, - "role": role.get("role"), - "thumb": f"/api/plex/person-image?u={quote(thumb, safe='')}" - if thumb.startswith(_PERSON_IMG_HOST) - else None, - } - ) return { "imdb_rating": imdb_rating, "imdb_id": imdb_id, @@ -1636,47 +1619,10 @@ def _rich_meta(meta: dict) -> dict: "directors": [d.get("tag") for d in (meta.get("Director") or []) if d.get("tag")][:4], "studio": meta.get("studio"), "tagline": meta.get("tagline"), - "cast": cast, + "cast": plex_people.cast_from_meta(meta), } -def _enrich_people_from_rich(db: Session, row, rich: dict) -> bool: - """Persist the FULLER cast (from an item's rich metadata) onto the row's `cast_names` + the - `people_text` search blob, and cache each cast member's headshot in plex_people. The cheap section - listing that drives the main sync only carries the top ~3 cast, so this — run when a rich fetch - happens (info-page view + the rich re-sync) — is what makes the full displayed cast filterable. - Returns True if anything was written (the caller commits). Best-effort: never the reason a read - 500s (the caller wraps the commit).""" - cast = rich.get("cast") or [] - names = list(dict.fromkeys(c["name"] for c in cast if c.get("name")))[:24] - if not names: - return False - dirty = False - # Grow/refresh cast_names only when the rich set adds names the stored (listing top-3) set lacks — - # avoid churn and never downgrade a fuller set back to the thin listing. - if set(names) - set(row.cast_names or []): - row.cast_names = names - row.people_text = " ".join(dict.fromkeys(list(row.directors or []) + names)) or None - dirty = True - # Headshot cache: upsert name -> proxied thumb (a later fetch fills a prior NULL; names without a - # whitelisted photo still get a row so a card can show a placeholder). - existing = {p.name: p for p in db.query(PlexPerson).filter(PlexPerson.name.in_(names))} - for c in cast: - nm, th = c.get("name"), c.get("thumb") - if not nm: - continue - p = existing.get(nm) - if p is None: - p = PlexPerson(name=nm, photo_thumb=th) - db.add(p) - existing[nm] = p - dirty = True - elif th and not p.photo_thumb: - p.photo_thumb = th - dirty = True - return dirty - - @router.get("/people/cards") def people_cards( names: str, @@ -1727,7 +1673,7 @@ def person_image(u: str, request: Request) -> Response: """Proxy a cast/crew photo from Plex's public metadata CDN (host-whitelisted; no token needed). Signed-session auth (no DB) so it, like /image, never holds a pool connection during the fetch.""" _require_session(request) - if not u.startswith(_PERSON_IMG_HOST): + if not u.startswith(plex_people.PERSON_IMG_HOST): raise HTTPException(status_code=400, detail="Unsupported image host") cache_key = "person_" + hashlib.sha1(u.encode()).hexdigest() hit = _img_cache_read(cache_key) @@ -1875,9 +1821,12 @@ def item_detail( pass # metadata extras are best-effort; any live-Plex failure must degrade, not 500 — playback works without them # Lazy enrichment: the rich fetch above carries the FULL cast (the sync only stored the listing's - # top ~3), so persist it onto this movie so every cast member shown here becomes filterable. A - # movie only — episodes aren't in the grid; their show's cast is enriched from /show. Best-effort. - if it.kind == "movie" and _enrich_people_from_rich(db, it, rich): + # top ~3), so persist it onto this movie so every cast member shown here becomes filterable, and + # stamp people_enriched_at so the backfill skips it. Movie only — episodes aren't in the grid; a + # show's cast is enriched from /show. Best-effort. + if it.kind == "movie" and rich.get("cast"): + plex_people.enrich_row_people(db, it, rich["cast"]) + it.people_enriched_at = datetime.now(timezone.utc) try: db.commit() except Exception: