release / go (push) Successful in 2m0s
release / web (push) Successful in 1m23s
release / govulncheck (push) Successful in 20s
release / integration (push) Failing after 4m23s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m8s
release / android (push) Canceled after 3m38s
Subsonic apps now get what Minstrel picks, not only what it holds: - getPlaylists lists the listener's daily mixes first, then their own playlists, then other people's public ones; getPlaylist returns the playable songs in order, as <entry> elements in XML. - getRandomSongs answers with the weighted shuffle (likes up, skips down, the last hour's plays out), narrowed by genre and release year. - getSimilarSongs and getSimilarSongs2 answer with Minstrel's radio, seeded from a song, or from a random song of an album or artist. Radio's selection moves out of the /api/radio handler into recommendation.Picker, built by recsettings.Service.Picker, so the web and Android apps and the Subsonic apps pick through one piece of code and one tuning. SongRef loses its XMLName so the field holding it names the element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/recommendation"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/recsettings"
|
|
)
|
|
|
|
// RadioResponse is the body of GET /api/radio.
|
|
type RadioResponse struct {
|
|
Tracks []TrackRef `json:"tracks"`
|
|
}
|
|
|
|
// handleRadio implements GET /api/radio?seed_track=<uuid>&limit=<int>.
|
|
//
|
|
// Returns the seed at index 0, followed by up to limit-1 picks from
|
|
// recommendation.Picker.Radio.
|
|
func (h *handlers) handleRadio(w http.ResponseWriter, r *http.Request) {
|
|
user, ok := requireUser(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
raw := strings.TrimSpace(r.URL.Query().Get("seed_track"))
|
|
if raw == "" {
|
|
writeErr(w, apierror.BadRequest("bad_request", "seed_track is required"))
|
|
return
|
|
}
|
|
seedID, ok := parseUUID(raw)
|
|
if !ok {
|
|
writeErr(w, apierror.BadRequest("bad_request", "invalid seed_track id"))
|
|
return
|
|
}
|
|
limit := h.recCfg.RadioSize
|
|
if v := r.URL.Query().Get("limit"); v != "" {
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil || n < 1 {
|
|
writeErr(w, apierror.BadRequest("bad_request", "invalid limit"))
|
|
return
|
|
}
|
|
limit = n
|
|
}
|
|
if limit > h.recCfg.RadioSizeMax {
|
|
limit = h.recCfg.RadioSizeMax
|
|
}
|
|
q := dbq.New(h.pool)
|
|
track, err := q.GetTrackByID(r.Context(), seedID)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
writeErr(w, apierror.NotFound("seed_track"))
|
|
return
|
|
}
|
|
h.logger.Error("api: get radio seed track failed", "err", err)
|
|
writeErr(w, apierror.InternalMsg("lookup failed", err))
|
|
return
|
|
}
|
|
album, err := q.GetAlbumByID(r.Context(), track.AlbumID)
|
|
if err != nil {
|
|
h.logger.Error("api: get radio seed album failed", "err", err)
|
|
writeErr(w, apierror.InternalMsg("lookup failed", err))
|
|
return
|
|
}
|
|
artist, err := q.GetArtistByID(r.Context(), track.ArtistID)
|
|
if err != nil {
|
|
h.logger.Error("api: get radio seed artist failed", "err", err)
|
|
writeErr(w, apierror.InternalMsg("lookup failed", err))
|
|
return
|
|
}
|
|
|
|
exclude := parseExcludeParam(r.URL.Query().Get("exclude"))
|
|
// limit-1 because the seed takes the first slot and is prepended below.
|
|
picks, err := h.picker().Radio(r.Context(), q, user.ID, seedID, limit-1, exclude)
|
|
if err != nil {
|
|
h.logger.Error("api: radio: load candidates failed", "err", err)
|
|
writeErr(w, apierror.InternalMsg("candidate load failed", err))
|
|
return
|
|
}
|
|
|
|
out := make([]TrackRef, 0, len(picks)+1)
|
|
out = append(out, trackRefFrom(track, album.Title, artist.Name))
|
|
for _, p := range picks {
|
|
al, err := q.GetAlbumByID(r.Context(), p.Track.AlbumID)
|
|
if err != nil {
|
|
h.logger.Error("api: radio: resolve album", "err", err)
|
|
writeErr(w, apierror.InternalMsg("resolve failed", err))
|
|
return
|
|
}
|
|
ar, err := q.GetArtistByID(r.Context(), p.Track.ArtistID)
|
|
if err != nil {
|
|
h.logger.Error("api: radio: resolve artist", "err", err)
|
|
writeErr(w, apierror.InternalMsg("resolve failed", err))
|
|
return
|
|
}
|
|
out = append(out, trackRefFrom(p.Track, al.Title, ar.Name))
|
|
}
|
|
writeJSON(w, http.StatusOK, RadioResponse{Tracks: out})
|
|
}
|
|
|
|
// picker is the shared chooser behind radio, with this server's tuning
|
|
// (recsettings.Service.Picker).
|
|
func (h *handlers) picker() *recommendation.Picker {
|
|
return h.recSettings.Picker(recsettings.PickerConfig{
|
|
RecentlyPlayedHours: h.recCfg.RecentlyPlayedHours,
|
|
MaxLength: h.recCfg.RadioSizeMax,
|
|
Rand: h.rng,
|
|
LibrarySize: h.librarySize,
|
|
Logger: h.logger,
|
|
})
|
|
}
|
|
|
|
// parseExcludeParam parses a comma-separated list of UUIDs from the
|
|
// `exclude` query string, silently dropping malformed entries. Returns
|
|
// nil for empty or all-malformed input.
|
|
func parseExcludeParam(raw string) []pgtype.UUID {
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(raw, ",")
|
|
out := make([]pgtype.UUID, 0, len(parts))
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" {
|
|
continue
|
|
}
|
|
id, ok := parseUUID(p)
|
|
if !ok {
|
|
continue
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
return out
|
|
}
|