feat(podcasts): the episodes authoring door + curated chain order
"Newest episodes from my selected podcasts" is now buildable in the UI, end to end — the user could not find ANY podcast-related filter surface because none existed: the New Collection modal could only create book-scope smart collections, and the engine's episodes scope (item 3.1) had no authoring door. The scope door: - The create modal's Smart card gains a Rules-scope segmented control (📚 Books / 🎧 Podcast episodes); the copy no longer claims smart means "match books by rules". Choosing Episodes seeds an EMPTY episodes tree ({"scope":"episodes","mode":"all","children":[]}) so the collection is born episode-scoped: the editor opens with the episode vocabulary (feed/downloaded/played/published date) and membership runs the episode engine from the first rule. - ParseFilterTree now accepts an EMPTY ROOT (nested empty groups stay invalid) — the seed's legal shape. The root arrives at depth 1 by call convention, hence the depth>1 exemption. - The composers define the empty-tree semantics: zero children matches NOTHING (ComposedLiveMedia/ComposedCount guards), mirroring EvaluateSmartCollection's zero-enabled-rules convention. Without the guard the composed query degrades to the bare scope head and a fresh collection would swallow every episode in scope — pinned by TestEpisodeCollectionsEmptySeedMatchesNothing (envelope book_count 0) and TestEmptyRootTreeSeedsEpisodesScope. The curated chain order (plan §10 R2, user need: the briefing plays NEWEST-first): - view_settings.chain_order ("native"/"newest"/"oldest"/"reverse") stored on the collection — curated content, not a player-side knob. - PUT /api/collections/:id/view-settings merges the key into the stored JSONB (a blind write would clobber the playlist toggles). - buildSmartCollectionSequenceContext routes OrderEpisodes through handlers.CollectionChainOrder; the detail header's Play-as-Sequence gains the order select next to it (PlayCollectionButton), which PUTs and reloads. Pinned by chain_follows_the_curated_order (reverse flips the chain indexes and hops). Caught by the new test, a latent clobber fixed: UpdateCollection treated a NIL view_settings payload as "write NULL" — any edit that carried no settings dropped every stored key (chain_order, playlist toggles from API clients, imported provenance). Nil now means untouched, the same convention filterTree already had. Composer guards also cover ComposedCount, so the builder's live count reads the same semantics. Tests: collectionChainOrder unit matrix, empty-root parse unit pin, two integration pins (seed matches nothing; curated order drives the chain and survives whole-map edits).
This commit is contained in:
1 parent
d5dbaa48ea
commit
cc0e9d7f91
16 files changed
+946
-538
No files matched your search
@@ -86,10 +86,10 @@ func parseRequestFilterTree(payload any) ([]byte, error) {
|
||||
}
|
||||
|
||||
type UpdateCollectionRequest struct {
|
||||
Name string `form:"name" json:"name" validate:"required"`
|
||||
Name string `form:"name" json:"name" validate:"required"`
|
||||
// Smart-collection match semantics (item 2.4): "any" (OR) | "all"
|
||||
// (AND). Empty/invalid keeps the stored value.
|
||||
RuleMatchMode string `form:"rule_match_mode" json:"rule_match_mode"`
|
||||
RuleMatchMode string `form:"rule_match_mode" json:"rule_match_mode"`
|
||||
// The unified filter tree (item 3.2): when present the collection's
|
||||
// smart membership runs through the qf engine. INVALID trees are a
|
||||
// 400 (ParseFilterTree at the handler); a tree supersedes
|
||||
@@ -547,6 +547,88 @@ func (h *CollectionHandler) UpdateCollection(c *echo.Context) error {
|
||||
return c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
// ValidChainOrders — the collection chain's listening-order vocabulary
|
||||
// (plan §10 R2: a collection's playback order is CURATED on the
|
||||
// collection, not a player-side knob). Mirrors OrderEpisodes' orders;
|
||||
// "" means native (the pre-chain-order default).
|
||||
var ValidChainOrders = map[string]bool{"": true, "native": true, "newest": true, "oldest": true, "reverse": true}
|
||||
|
||||
// CollectionChainOrder resolves a collection row's curated chain order
|
||||
// from its view_settings JSONB — "" (native) when absent/unknown. The
|
||||
// episode chain producer routes OrderEpisodes through this; the
|
||||
// view-settings PUT writes the key (SetCollectionChainOrder).
|
||||
func CollectionChainOrder(viewSettings []byte) string {
|
||||
if len(viewSettings) == 0 {
|
||||
return ""
|
||||
}
|
||||
var vs map[string]interface{}
|
||||
if json.Unmarshal(viewSettings, &vs) != nil {
|
||||
return ""
|
||||
}
|
||||
if s, ok := vs["chain_order"].(string); ok && ValidChainOrders[s] {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SetCollectionChainOrder — PUT /api/collections/:id/view-settings with
|
||||
// {"chain_order": "..."}: merges the key into the collection's stored
|
||||
// view_settings (the playlist toggles share that JSONB — a blind write
|
||||
// would clobber them). The episode chain producer
|
||||
// (buildSmartCollectionSequenceContext) honors it.
|
||||
func (h *CollectionHandler) SetCollectionChainOrder(c *echo.Context) error {
|
||||
collectionID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid collection id"})
|
||||
}
|
||||
|
||||
var req struct {
|
||||
ChainOrder string `json:"chain_order"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request"})
|
||||
}
|
||||
if !ValidChainOrders[req.ChainOrder] {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||
"error": "chain_order must be one of: native, newest, oldest, reverse"})
|
||||
}
|
||||
|
||||
collection, err := h.collectionService.GetCollection(c.Request().Context(), collectionID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "collection not found"})
|
||||
}
|
||||
|
||||
settings := map[string]interface{}{}
|
||||
if len(collection.ViewSettings) > 0 {
|
||||
if uerr := json.Unmarshal(collection.ViewSettings, &settings); uerr != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "corrupt view_settings"})
|
||||
}
|
||||
}
|
||||
if req.ChainOrder == "" || req.ChainOrder == "native" {
|
||||
// Both spellings mean the default — drop the key entirely so the
|
||||
// stored doc stays clean.
|
||||
delete(settings, "chain_order")
|
||||
} else {
|
||||
settings["chain_order"] = req.ChainOrder
|
||||
}
|
||||
blob, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
if err := h.db.UpdateCollectionViewSettings(c.Request().Context(), database.UpdateCollectionViewSettingsParams{
|
||||
ID: pgtype.UUID{Bytes: collectionID, Valid: true},
|
||||
ViewSettings: blob,
|
||||
}); err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"id": uuid.UUID(collection.ID.Bytes).String(),
|
||||
"chain_order": req.ChainOrder,
|
||||
"view_settings": settings,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *CollectionHandler) DeleteCollection(c *echo.Context) error {
|
||||
collectionID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user