Files
bookhoard/internal/router/podcast.go
T
John O'Keefe e3675536aa feat(podcasts): reverse order — position DESC, date-agnostic; single order helper
The 'Oldest first' order sorts by PUB DATE, which breaks on Scribl's
migrated podiobooks feeds (messy/all-equal dates). New order value
'reverse' = feed position DESC with a date-DESC tiebreak — dates are
not involved, so nothing can break.

- services.OrderEpisodes: THE single order source of truth (§0 items
  3+5a). The four surfaces (sequence JSON, reader chain context, SSR
  page list) previously carried three near-identical handwritten
  comparators — all now route through one generic helper. The SSR page
  list gains a SortDate key (lexical 2006-01-02) so the pub-date
  tiebreak is actually chronological; the old comparator compared
  'Jan 2, 2006' display strings alphabetically (latent drift bug).
- Dropdown becomes four options: Feed order / Feed order (reversed) /
  Oldest first (pub date) / Newest first (pub date).
- Serial suggestion button renamed 'Reverse feed order' (sets reverse,
  not oldest) and its response out-of-band-swaps the order select
  (PodcastOrderSelectOOB — same mechanism as Download All's policy
  select), so the dropdown mirrors the server state immediately.
- Validation: oneof=native reverse oldest newest everywhere.
- Tests: reverse binds via the form wire; messy-dates sequence
  returns 3,2,1; existing reorder-safety stays green.
2026-10-30 09:15:03 -04:00

77 lines
2.7 KiB
Go

package router
// Podcast routes (plan §8). All under /api/podcasts with the JWT
// middleware; scoping is RLS's job (D20) — handlers add no visibility
// plumbing.
import (
"bytes"
"net/http"
"bookhoard/templates"
"github.com/labstack/echo/v5"
)
func registerPodcastRoutes(cfg *Config) {
g := cfg.Echo.Group("/api/podcasts", createJWTMiddleware(cfg))
// Episode-list renderer (issue #1): htmx actions answer with the
// refreshed partial directly — no event indirection to miss. The
// view assembly lives here (templates) while the action logic lives
// in the handler; this closure is the sanctioned seam.
cfg.PodcastHandler.SetEpisodeListRenderer(func(c *echo.Context, feedID string, includePolicySelect, includeOrderSelect bool) error {
user, err := getTemplateUserWithTheme(c, cfg)
if err != nil {
return err
}
feedUUID, err := parsePodcastUUID(feedID)
if err != nil {
return err
}
data, err := buildPodcastDetailData(c, cfg, user, feedUUID)
if err != nil {
return err
}
var buf bytes.Buffer
render := templates.PodcastEpisodeList
if templates.HasActiveDownloads(data.Episodes) {
render = templates.PodcastEpisodeListPolling
}
if err := render(*data).Render(c.Request().Context(), &buf); err != nil {
return err
}
if includePolicySelect {
// Issue #3: out-of-band swap of the auto-download select —
// the dropdown reflects the flipped policy immediately.
if err := templates.PodcastAutoDownloadSelectOOB(data.Feed).Render(c.Request().Context(), &buf); err != nil {
return err
}
}
if includeOrderSelect {
// §0 item 3: out-of-band swap of the order select — the
// "Reverse feed order" button flips to reverse; the
// dropdown mirrors the server state immediately.
if err := templates.PodcastOrderSelectOOB(data.Feed.ID, data.Order).Render(c.Request().Context(), &buf); err != nil {
return err
}
}
return c.HTML(http.StatusOK, buf.String())
})
g.POST("/feeds", cfg.PodcastHandler.AddFeed)
g.GET("/feeds", cfg.PodcastHandler.ListFeeds)
g.GET("/search", cfg.PodcastHandler.Search)
// :id-scoped (RLS answers 404 for feeds outside the caller's scope).
feeds := g.Group("/feeds/:id")
feeds.GET("", cfg.PodcastHandler.GetFeed)
feeds.GET("/sequence", cfg.PodcastHandler.GetFeedSequence)
feeds.PUT("/order", cfg.PodcastHandler.SetFeedOrder)
feeds.PUT("/subscription", cfg.PodcastHandler.UpdateSubscription)
feeds.DELETE("/subscription", cfg.PodcastHandler.DeleteSubscription)
feeds.POST("/refresh", cfg.PodcastHandler.RefreshFeed)
feeds.POST("/episodes/:eid/download", cfg.PodcastHandler.DownloadEpisodes)
feeds.DELETE("", cfg.PodcastHandler.DeleteFeed) // admin-only (handler checks role)
}