Full-epic /deep-review (11 finders + 12 adversarial verifiers) surfaced no HIGH; every mechanism finding calibrated to LOW on verification. Fix all 13: Correctness / reliability: - Combobox: Enter now commits the VISUALLY highlighted row (highlightIndex ?? activeIndex), not the bare keyboard activeIndex — so arrow-then-hover-then-Enter no longer toggles a different option than the one painted, and hover-with-no-arrow is no longer a silent no-op. - parse_person: dedup the known_for pool by (id, media_type), not the bare id — TMDB movie and tv id spaces are independent, so a same-id movie+show pair is two distinct credits (matches the collision-safe key the rest of the module already uses). - person-enrich: a per-person in-flight + 60s cooldown guard (people._should_schedule_enrich) dedups concurrent opens and rate-limits re-triggering while TMDB is degraded (a transient TmdbError leaves the watermark NULL), so bursts can't pile 8s tasks onto the shared threadpool. - PlexInfo compact()/money(): format with i18n.language (keeping currency USD) instead of a hardcoded en/en-US, matching the app's Intl convention. - delete the dead `plex.filter.add` i18n key (both locales) — the +Add button that used it is gone. Reuse: - _continue_watching reuses rollup.agg_subquery(include_resumed=True) + query.apply_show_state_filter instead of a third hand-inlined copy of the per-show rollup + in_progress predicate, so the Discover rail can't drift from the grid's definition. Test coverage (the gaps a regression would ship through): - resolve_library_guids scope-exclusion (a credit in a non-scoped/disabled library is dropped) + the []-vs-None distinction; parse_person cross-media-id retention; enrich_person transient (NULL watermark) and 404 (stamp) branches; _enrich_person_bg body (fresh session close/rollback, None-guard) + the cooldown dedup; continue_watching fully-watched-show exclusion; Combobox Enter-commits-highlighted + async/controlled-query mode; PopoverItem aria-checked contract; RatingsRow missing-IMDb + imdb==audience branches. Backend lane 413→420, FE vitest 351→357; all gates green. No migration.
243 lines
10 KiB
Python
243 lines
10 KiB
Python
"""Plex people: cast/crew filter-chip cards + a single person's filmography page."""
|
|
|
|
import logging
|
|
import threading
|
|
import time
|
|
|
|
from fastapi import APIRouter, BackgroundTasks, Depends
|
|
from sqlalchemy import func, or_, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.auth import current_user
|
|
from app.db import SessionLocal, get_db
|
|
from app.models import (
|
|
PlexItem,
|
|
PlexPerson,
|
|
PlexShow,
|
|
User,
|
|
)
|
|
from app.plex import rollup
|
|
from app.plex import tmdb as plex_tmdb
|
|
from app.plex.cards import movie_card, show_card
|
|
from app.plex.query import split_csv
|
|
from app.routes.plex._shared import _content_libs
|
|
|
|
router = APIRouter(prefix="/api/plex", tags=["plex"])
|
|
log = logging.getLogger("siftlode.plex")
|
|
|
|
|
|
def _resolve_known_for(
|
|
db: Session,
|
|
known_for: list | None,
|
|
uid: int,
|
|
movie_lib_ids: list[int],
|
|
show_lib_ids: list[int],
|
|
) -> list[dict]:
|
|
"""The person's TMDB "Known for" credits (top by popularity) that are IN our library, as cards, in
|
|
popularity order. Matched by the canonical guid `tmdb:<id>`; out-of-library credits are dropped."""
|
|
if not known_for:
|
|
return []
|
|
# Match each credit to a library row by guid, bucketed by media_type (movie ids and tv ids collide
|
|
# numerically) and scoped to the enabled libraries — the shared helper owns that collision-safe
|
|
# logic (the detail-page recs use the same one).
|
|
movies, shows = plex_tmdb.resolve_library_guids(
|
|
db, known_for, movie_lib_ids=movie_lib_ids, show_lib_ids=show_lib_ids
|
|
)
|
|
m_states = rollup.item_state_map(db, uid, [m.id for m in movies.values()])
|
|
show_statuses = rollup.state_map(db, uid, [s.id for s in shows.values()])
|
|
out: list[dict] = []
|
|
for k in known_for:
|
|
g = f"tmdb:{k.get('tmdb_id')}"
|
|
# A credit resolves against its OWN media_type's bucket only.
|
|
if k.get("media_type") == "tv":
|
|
if g in shows:
|
|
out.append(show_card(shows[g], show_statuses.get(shows[g].id, "new")))
|
|
elif g in movies:
|
|
out.append(movie_card(movies[g], m_states.get(movies[g].id)))
|
|
return out
|
|
|
|
|
|
@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 = split_csv(names)[:16]
|
|
if not wanted:
|
|
return {"people": []}
|
|
movie_lib_ids, show_lib_ids = _content_libs(db)
|
|
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.execute(
|
|
select(PlexPerson).where(PlexPerson.name.in_(wanted))
|
|
).scalars()
|
|
}
|
|
out = []
|
|
for nm in wanted:
|
|
count = 0
|
|
if want_movies:
|
|
count += (
|
|
db.scalar(
|
|
select(func.count(PlexItem.id)).where(
|
|
PlexItem.kind == "movie",
|
|
PlexItem.library_id.in_(movie_lib_ids),
|
|
PlexItem.cast_names.contains([nm]),
|
|
)
|
|
)
|
|
or 0
|
|
)
|
|
if want_shows:
|
|
count += (
|
|
db.scalar(
|
|
select(func.count(PlexShow.id)).where(
|
|
PlexShow.library_id.in_(show_lib_ids),
|
|
PlexShow.cast_names.contains([nm]),
|
|
)
|
|
)
|
|
or 0
|
|
)
|
|
out.append({"name": nm, "photo": photos.get(nm), "count": count})
|
|
return {"people": out}
|
|
|
|
|
|
# Cap each filmography section — a prolific person shouldn't return an unbounded list the client then
|
|
# mounts a card per row for (the grid paginates at PAGE; this page renders all at once). Ample for a
|
|
# personal library; the most-recent titles win via the year-desc order.
|
|
_PERSON_SECTION_MAX = 200
|
|
|
|
|
|
# A person's bio is enriched at most once per this window: it dedups concurrent opens of the same person
|
|
# (each would otherwise schedule its own 8s TMDB task) AND rate-limits re-triggering when TMDB is
|
|
# down/slow (a transient failure leaves tmdb_enriched_at NULL, so without this every reload would spawn a
|
|
# fresh task and pile up on the shared threadpool). In-process + best-effort: a restart just clears it, at
|
|
# worst costing a few extra fetches. A successful enrich stamps the watermark, so the reservation only
|
|
# ever expires harmlessly.
|
|
_ENRICH_COOLDOWN_S = 60.0
|
|
_enrich_inflight: dict[int, float] = {}
|
|
_enrich_lock = threading.Lock()
|
|
|
|
|
|
def _should_schedule_enrich(person_id: int) -> bool:
|
|
"""True (and reserves a cooldown) if this person isn't already being enriched / recently attempted."""
|
|
now = time.monotonic()
|
|
with _enrich_lock:
|
|
deadline = _enrich_inflight.get(person_id)
|
|
if deadline is not None and now < deadline:
|
|
return False
|
|
_enrich_inflight[person_id] = now + _ENRICH_COOLDOWN_S
|
|
# Bound the map: drop expired reservations once it grows (a large library has many people).
|
|
if len(_enrich_inflight) > 1024:
|
|
for pid in [p for p, d in _enrich_inflight.items() if d <= now]:
|
|
del _enrich_inflight[pid]
|
|
return True
|
|
|
|
|
|
def _enrich_person_bg(person_id: int) -> None:
|
|
"""Fetch + persist one person's TMDB bio OFF the request path — a FastAPI background task that runs
|
|
AFTER the response is sent, so the outbound TMDB call never sits on the serve latency (the module's
|
|
"no live TMDB call at serve time" invariant). The request's session is already closed by then, so
|
|
open a fresh one. Best-effort: a TMDB failure just leaves `tmdb_enriched_at` NULL to retry on the
|
|
next open, and the bio lands from the row by then."""
|
|
db = SessionLocal()
|
|
try:
|
|
person = db.get(PlexPerson, person_id)
|
|
if person is not None:
|
|
plex_tmdb.enrich_person(db, person)
|
|
except Exception: # noqa: BLE001 — background best-effort; never surfaces to a user
|
|
db.rollback()
|
|
log.warning("background person bio enrich failed for id=%s", person_id, exc_info=True)
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.get("/person/{name}")
|
|
def person_detail(
|
|
name: str,
|
|
background_tasks: BackgroundTasks,
|
|
user: User = Depends(current_user),
|
|
db: Session = Depends(get_db),
|
|
) -> dict:
|
|
"""One cast/crew member's filmography: their cached headshot + the movies and shows (in the enabled
|
|
libraries) whose cast OR directors include this name — the inverse of the cast-name → library-filter
|
|
link. The name string is the identity key (Plex cast carries no stable per-person id), so distinct
|
|
people who share a name share a page. Each section is capped at _PERSON_SECTION_MAX. An unknown name
|
|
simply returns empty sections.
|
|
|
|
E5 S6: the first time a page is opened, the person's TMDB bio + "Known for" credits are lazily
|
|
fetched (from the S2-linked `tmdb_person_id`) and persisted, then served from the row on every open.
|
|
The fetch runs OFF the request path (a background task); the first open serves whatever's in the row
|
|
(bio possibly still null) and the bio lands from the row by the next open.
|
|
"""
|
|
person = db.execute(select(PlexPerson).where(PlexPerson.name == name)).scalars().first()
|
|
# Lazy one-time bio enrich, scheduled AFTER the response so the TMDB call is never on the serve path.
|
|
# Gated by a per-person cooldown so concurrent/rapid opens (or repeated opens while TMDB is degraded)
|
|
# can't pile background tasks onto the shared threadpool.
|
|
if (
|
|
person is not None
|
|
and person.tmdb_person_id
|
|
and person.tmdb_enriched_at is None
|
|
and _should_schedule_enrich(person.id)
|
|
):
|
|
background_tasks.add_task(_enrich_person_bg, person.id)
|
|
movie_lib_ids, show_lib_ids = _content_libs(db)
|
|
|
|
movies: list[dict] = []
|
|
if movie_lib_ids:
|
|
items = db.execute(
|
|
select(PlexItem)
|
|
.where(
|
|
PlexItem.kind == "movie",
|
|
PlexItem.library_id.in_(movie_lib_ids),
|
|
or_(
|
|
PlexItem.cast_names.contains([name]),
|
|
PlexItem.directors.contains([name]),
|
|
),
|
|
)
|
|
.order_by(PlexItem.year.desc().nullslast(), func.lower(PlexItem.title))
|
|
.limit(_PERSON_SECTION_MAX)
|
|
).scalars().all()
|
|
states = rollup.item_state_map(db, user.id, [it.id for it in items])
|
|
movies = [movie_card(it, states.get(it.id)) for it in items]
|
|
|
|
shows: list[dict] = []
|
|
if show_lib_ids:
|
|
show_rows = db.execute(
|
|
select(PlexShow)
|
|
.where(
|
|
PlexShow.library_id.in_(show_lib_ids),
|
|
or_(
|
|
PlexShow.cast_names.contains([name]),
|
|
PlexShow.directors.contains([name]),
|
|
),
|
|
)
|
|
.order_by(PlexShow.year.desc().nullslast(), func.lower(PlexShow.title))
|
|
.limit(_PERSON_SECTION_MAX)
|
|
).scalars().all()
|
|
statuses = rollup.state_map(db, user.id, [s.id for s in show_rows])
|
|
shows = [show_card(sh, statuses.get(sh.id, "new")) for sh in show_rows]
|
|
|
|
known_for = _resolve_known_for(
|
|
db, person.known_for if person else None, user.id, movie_lib_ids, show_lib_ids
|
|
)
|
|
return {
|
|
"name": name,
|
|
"photo": person.photo_thumb if person else None,
|
|
"biography": person.biography if person else None,
|
|
"birthday": person.birthday if person else None,
|
|
"deathday": person.deathday if person else None,
|
|
"place_of_birth": person.place_of_birth if person else None,
|
|
"known_for_department": person.known_for_department if person else None,
|
|
"also_known_as": (person.also_known_as if person else None) or [],
|
|
"known_for": known_for,
|
|
"movies": movies,
|
|
"shows": shows,
|
|
}
|