Repository navigation
feat(catalog): Latest Episodes sort — order series by newest episode file - #283
Conversation
…file Adds a latest_episode_added sort so users can see which shows received new episodes. Today's recently-added surfaces reflect when the SERIES was first added: linking a new episode file never bumps the series' media_item_libraries.first_seen_at (ON CONFLICT DO NOTHING), so a long-running show with a fresh episode sorts as stale (Silo-Server#202). - New denorm media_items.latest_episode_added_at (migration + backfill + partial series index), mirroring the last_air_date_at precedent. Source of truth is episode_libraries.first_seen_at; the three insert paths (UpdateEpisodeLink, BulkLinkEpisodesBySeries, scanner folder restore) bump the parent series atomically in the same statement, monotonically via GREATEST, and only for genuinely new links. - Sort registered in both frameworks: querySortDefs (sections + smart collections + /v1/catalog pick it up automatically via QuerySortFieldSet) and the browse buildOrderByPlan path. - Jellyfin compat: SortBy=DateLastContentAdded now maps to the new sort instead of silently collapsing to series creation date — Jellyfin clients already send this for the TV "Latest" shelf, so they get the correct behavior with no client changes. DatePlayed keeps its old created_at mapping instead of piggybacking. - Web sort picker gains "Latest Episode Added" (series scope). Additive-only per v1 API rules: new sort value, no field/status changes. Part of Silo-Server#202 Fixes Silo-Server#202 Co-Authored-By: Claude Fable 5 <[email protected]>
The picker-side QuerySortField gained the value but the api-layer QuerySort union did not, breaking the production tsc build. Part of Silo-Server#202 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Warning Review limit reached
Next review available in: 55 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (14)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6acf3529a2
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| bumped AS ( | ||
| UPDATE media_items mi | ||
| SET latest_episode_added_at = GREATEST(COALESCE(mi.latest_episode_added_at, sub.latest_added), sub.latest_added) |
There was a problem hiding this comment.
Recompute the denorm when episode links are removed
This only ever bumps latest_episode_added_at upward from newly inserted episode_libraries rows, but the same UpdateEpisodeLink statement can delete the previous episode membership below when a file is re-linked to another episode. In that scenario, or when the latest/last episode membership is later removed by reconciliation, the old parent series keeps a non-null/stale timestamp and will continue to sort as recently updated even when its current episode_libraries source of truth no longer has that row.
Useful? React with 👍 / 👎.
| case "datelastcontentadded": | ||
| // Jellyfin's standard "Latest" sort for TV libraries: shows ordered | ||
| // by their most recently added episode (issue #202). | ||
| return "latest_episode_added" |
There was a problem hiding this comment.
Keep DateLastContentAdded scoped to series requests
mapSortBy is used for all Jellyfin /Items browse requests, not just TV libraries, so SortBy=DateLastContentAdded with IncludeItemTypes=Movie now orders by mi.latest_episode_added_at. That column is only populated for series, so movie rows all compare as NULL and fall through to the tiebreaker instead of preserving the previous added-date ordering for movie libraries.
Useful? React with 👍 / 👎.
Code reviewFound 2 issues (both confirm the earlier Codex inline comments — I validated each against the code):
silo-server/internal/scanner/file_repo.go Lines 2940 to 2967 in 6acf352
silo-server/internal/jellycompat/query.go Lines 438 to 444 in 6acf352 I am pushing fixes for both to this branch. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
…ips are removed The denorm was only ever bumped upward (GREATEST) at insert time, but UpdateEpisodeLink also deletes the old episode's library membership on re-link, and reconciliation/path-prefix clears remove memberships too — leaving a stale timestamp that kept the series sorting as recently updated. All removal paths now run in a transaction and finish with a shared full MAX() recompute (catalog.RecomputeSeriesLatestEpisodeAdded) that also resets to NULL when no memberships remain, mirroring the last_air_date_at maintenance pattern. Sequential statements are load-bearing here: data-modifying CTEs are invisible to reads in the same statement, which also silently no-op'd the old path-prefix membership delete. Co-Authored-By: Claude Fable 5 <[email protected]>
…uests mapSortBy runs for every /Items browse, so the latest_episode_added mapping leaked into movie and untyped requests where the column is always NULL, destroying the previous created_at ordering. The sort now falls back to created_at unless IncludeItemTypes is exactly Series. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Both issues addressed:
DB-backed regression tests extended (re-link lowers/NULLs the old series, bumps the new one; reconciliation reset). Verified with |
…every call (#321) The statement ended with SELECT COUNT(*) FROM inserted, but the inserted CTE has no RETURNING clause — PostgreSQL rejects referencing such a CTE (SQLSTATE 0A000), so every UpdateEpisodeLink call has errored since #283. The sole caller logs a warning and continues, so the metadata-side single-file episode relink silently did nothing: episode_id stayed stale, episode_libraries rows were not inserted, latest_episode_added_at was not bumped. Bulk linking uses the correct pattern and masked the breakage. The INSERT is now the top-level statement; the count was unused. Verified against a migrated PostgreSQL scratch database: TestEpisodeLinkMaintainsLatestEpisodeAdded fails on main with the exact error and passes with this change. Fixes #320 Co-authored-by: Claude Fable 5 <[email protected]>
…every call (#321) The statement ended with SELECT COUNT(*) FROM inserted, but the inserted CTE has no RETURNING clause — PostgreSQL rejects referencing such a CTE (SQLSTATE 0A000), so every UpdateEpisodeLink call has errored since #283. The sole caller logs a warning and continues, so the metadata-side single-file episode relink silently did nothing: episode_id stayed stale, episode_libraries rows were not inserted, latest_episode_added_at was not bumped. Bulk linking uses the correct pattern and masked the breakage. The INSERT is now the top-level statement; the count was unused. Verified against a migrated PostgreSQL scratch database: TestEpisodeLinkMaintainsLatestEpisodeAdded fails on main with the exact error and passes with this change. Fixes #320 Co-authored-by: Claude Fable 5 <[email protected]>
…file (Silo-Server#283) * feat(catalog): Latest Episodes sort — order series by newest episode file Adds a latest_episode_added sort so users can see which shows received new episodes. Today's recently-added surfaces reflect when the SERIES was first added: linking a new episode file never bumps the series' media_item_libraries.first_seen_at (ON CONFLICT DO NOTHING), so a long-running show with a fresh episode sorts as stale (Silo-Server#202). - New denorm media_items.latest_episode_added_at (migration + backfill + partial series index), mirroring the last_air_date_at precedent. Source of truth is episode_libraries.first_seen_at; the three insert paths (UpdateEpisodeLink, BulkLinkEpisodesBySeries, scanner folder restore) bump the parent series atomically in the same statement, monotonically via GREATEST, and only for genuinely new links. - Sort registered in both frameworks: querySortDefs (sections + smart collections + /v1/catalog pick it up automatically via QuerySortFieldSet) and the browse buildOrderByPlan path. - Jellyfin compat: SortBy=DateLastContentAdded now maps to the new sort instead of silently collapsing to series creation date — Jellyfin clients already send this for the TV "Latest" shelf, so they get the correct behavior with no client changes. DatePlayed keeps its old created_at mapping instead of piggybacking. - Web sort picker gains "Latest Episode Added" (series scope). Additive-only per v1 API rules: new sort value, no field/status changes. Part of Silo-Server#202 Fixes Silo-Server#202 Co-Authored-By: Claude Fable 5 <[email protected]> * fix(web): include latest_episode_added in the api QuerySort field union The picker-side QuerySortField gained the value but the api-layer QuerySort union did not, breaking the production tsc build. Part of Silo-Server#202 Co-Authored-By: Claude Fable 5 <[email protected]> * fix(scanner): recompute latest_episode_added_at when episode memberships are removed The denorm was only ever bumped upward (GREATEST) at insert time, but UpdateEpisodeLink also deletes the old episode's library membership on re-link, and reconciliation/path-prefix clears remove memberships too — leaving a stale timestamp that kept the series sorting as recently updated. All removal paths now run in a transaction and finish with a shared full MAX() recompute (catalog.RecomputeSeriesLatestEpisodeAdded) that also resets to NULL when no memberships remain, mirroring the last_air_date_at maintenance pattern. Sequential statements are load-bearing here: data-modifying CTEs are invisible to reads in the same statement, which also silently no-op'd the old path-prefix membership delete. Co-Authored-By: Claude Fable 5 <[email protected]> * fix(jellycompat): keep DateLastContentAdded scoped to series-only requests mapSortBy runs for every /Items browse, so the latest_episode_added mapping leaked into movie and untyped requests where the column is always NULL, destroying the previous created_at ordering. The sort now falls back to created_at unless IncludeItemTypes is exactly Series. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Quick <[email protected]>
Problem
Every recently-added surface reflects when a SERIES was first added — linking a new episode file never bumps the series'
media_item_libraries.first_seen_at(ON CONFLICT DO NOTHING), so a long-running show with a fresh episode sorts as stale. Users can't see which shows received new episodes (#202).Approach
New denorm
media_items.latest_episode_added_at(migration + backfill + partial series index), mirroring thelast_air_date_atprecedent. Source of truth isepisode_libraries.first_seen_at; all three insert paths (UpdateEpisodeLink,BulkLinkEpisodesBySeries, scanner folder restore) bump the parent series in the same SQL statement — monotonic viaGREATEST, and only for genuinely new links (conflict no-ops return no rows). Likelast_air_date_at, the value is global rather than access-scoped — same accepted trade-off for an O(1) sort key.Sort registration in both frameworks: a
querySortDefsentry (sections, smart collections, and/v1/catalogaccept it automatically viaQuerySortFieldSet) and abuildOrderByPlancase for the browse path.NULLS LASTso movies and episode-less series trail.Jellyfin compat:
SortBy=DateLastContentAdded— Jellyfin's standard sort behind the TV "Latest" shelf, which clients already send — now maps to the new sort instead of silently collapsing to series creation date. Existing Jellyfin/compat clients get correct behavior with no client changes.DatePlayedkeeps its previouscreated_atmapping instead of piggybacking on the same case.Web picker: "Latest Episode Added" (series scope), sibling of "Latest Episode Air Date".
API contract
Additive-only per the v1 rules: a new accepted sort value; both frameworks already fall back gracefully on unknown values, so older servers/clients interop unchanged. The compat remap is a behavior change existing clients will feel — deliberately, since the previous mapping was wrong.
Testing
mapSortBytests pinning the new mapping and theDatePlayedsplit./Items?SortBy=DateLastContentAddedorder matched the SQL denorm order exactly, tracking an in-flight library re-ingest in real time; series that received new episodes today surfaced above newly-added series.Follow-ups (out of scope)
Fixes #202
AI-use disclosure
Implemented with Claude Code (test-first, live-validated); human-reviewed before submission.
🤖 Generated with Claude Code