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.
102 lines
3.8 KiB
Python
102 lines
3.8 KiB
Python
"""Roll a show's episodes up into a single watch-state (new | in_progress | watched) + on-deck.
|
|
|
|
Drives the show/season grid badges and the Resume / Play-from-start / mark-all buttons.
|
|
"""
|
|
|
|
from sqlalchemy import and_, case, func, or_, select
|
|
from sqlalchemy.orm import Session, aliased
|
|
|
|
from app.models import PlexItem, PlexState
|
|
|
|
|
|
def show_status(total: int, watched: int, inprog: int) -> str:
|
|
"""Roll a show's episodes up into a single watch state: fully watched, partially started, or new."""
|
|
if total and watched >= total:
|
|
return "watched"
|
|
if (watched or 0) > 0 or (inprog or 0) > 0:
|
|
return "in_progress"
|
|
return "new"
|
|
|
|
|
|
def rollup(cards: list[dict]) -> dict:
|
|
"""From ORDERED episode cards → the aggregate watch status, the "on deck" episode to Resume (the
|
|
last in-progress one, else the first unwatched), the first episode (for Play from the start), and
|
|
the episode count. Drives the show/season Resume + Play + mark-all buttons."""
|
|
total = len(cards)
|
|
watched = sum(1 for c in cards if c.get("status") == "watched")
|
|
inprog = [
|
|
c
|
|
for c in cards
|
|
if (c.get("position_seconds") or 0) > 0 and c.get("status") != "watched"
|
|
]
|
|
resume = None
|
|
if inprog:
|
|
resume = inprog[-1] # continue the latest-started episode
|
|
else:
|
|
resume = next((c for c in cards if c.get("status") != "watched"), None)
|
|
return {
|
|
"status": show_status(total, watched, len(inprog)),
|
|
"resume": resume,
|
|
"first": cards[0] if cards else None,
|
|
"episode_count": total,
|
|
}
|
|
|
|
|
|
def agg_subquery(db: Session, user_id: int, include_resumed: bool = False):
|
|
"""Per-show episode counts for a user: total, watched, in-progress. Reused to (a) filter the show
|
|
grid by aggregate watch-state, (b) compute each card's badge, and (c) drive the Discover
|
|
continue-watching rail. `include_resumed` adds `resumed_at` = max(progress_updated_at) across the
|
|
show's episodes, so the rail can order shows by how recently they were last progressed."""
|
|
ps = aliased(PlexState)
|
|
cols = [
|
|
PlexItem.show_id.label("show_id"),
|
|
func.count(PlexItem.id).label("total"),
|
|
func.count(case((ps.status == "watched", 1))).label("watched"),
|
|
func.count(
|
|
case(
|
|
(
|
|
and_(
|
|
ps.position_seconds > 0,
|
|
or_(ps.status.is_(None), ps.status != "watched"),
|
|
),
|
|
1,
|
|
)
|
|
)
|
|
).label("inprog"),
|
|
]
|
|
if include_resumed:
|
|
cols.append(func.max(ps.progress_updated_at).label("resumed_at"))
|
|
return (
|
|
select(*cols)
|
|
.outerjoin(ps, and_(ps.item_id == PlexItem.id, ps.user_id == user_id))
|
|
.where(PlexItem.kind == "episode", PlexItem.show_id.isnot(None))
|
|
.group_by(PlexItem.show_id)
|
|
)
|
|
|
|
|
|
def state_map(db: Session, user_id: int, show_ids: list[int]) -> dict[int, str]:
|
|
if not show_ids:
|
|
return {}
|
|
rows = db.execute(
|
|
agg_subquery(db, user_id).where(PlexItem.show_id.in_(show_ids))
|
|
).all()
|
|
return {r.show_id: show_status(r.total, r.watched, r.inprog) for r in rows}
|
|
|
|
|
|
def item_state_map(
|
|
db: Session, user_id: int, item_ids: list[int]
|
|
) -> dict[int, PlexState]:
|
|
"""A user's PlexState rows for a set of leaf items (movies or episodes), keyed by item_id — the
|
|
leaf analogue of `state_map` (which rolls a show's episodes up to a status string). One batched
|
|
fetch, so the caller serializes cards without a per-card query. Empty in → empty dict (no `IN ()`)."""
|
|
if not item_ids:
|
|
return {}
|
|
return {
|
|
s.item_id: s
|
|
for s in db.execute(
|
|
select(PlexState).where(
|
|
PlexState.user_id == user_id, PlexState.item_id.in_(item_ids)
|
|
)
|
|
).scalars()
|
|
}
|