Files
minstrel/internal/subsonic/playqueue.go
T
bvandeusenandClaude Opus 5.5 b801f81058
release / go (push) Successful in 1m50s
release / web (push) Successful in 1m14s
release / govulncheck (push) Successful in 44s
release / integration (push) Successful in 4m11s
release / android (push) Successful in 6m15s
release / Build signed APK (push) Successful in 7m51s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(subsonic): an artist's top songs, and the play queue carried between devices
getTopSongs ranks the named artist's songs by how often they were played
through on this server, then by likes, then in album order, so an artist
nobody has played yet still has songs to offer. Missing files and the
listener's quarantined songs are left out.

savePlayQueue keeps one queue per listener: the songs in order, the one
playing, how far into it, and the app that saved it. getPlayQueue gives it
back to that listener only; a song no longer in the library drops out.
Saving no songs clears it. The queue lives in a new play_queues table,
which backups carry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 17:03:29 -05:00

135 lines
4.2 KiB
Go

package subsonic
import (
"errors"
"net/http"
"slices"
"strconv"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// A Subsonic app saves its play queue (savePlayQueue) so the same app on
// another device, or another app, can carry on from it (getPlayQueue). The
// server keeps one queue per user and does nothing else with it: Minstrel's
// own apps keep their queue on the device.
// maxPlayQueue is the most songs one saved queue holds. A queue longer than
// any app sends is refused rather than stored, so one request cannot grow a
// row without bound.
const maxPlayQueue = 10000
// PlayQueue is the body of getPlayQueue. Position is milliseconds into the
// current song.
type PlayQueue struct {
Current string `json:"current,omitempty" xml:"current,attr,omitempty"`
Position int64 `json:"position,omitempty" xml:"position,attr,omitempty"`
Username string `json:"username" xml:"username,attr"`
Changed string `json:"changed" xml:"changed,attr"`
ChangedBy string `json:"changedBy" xml:"changedBy,attr"`
Entries []SongRef `json:"entry" xml:"entry"`
}
// PlayQueueResponse carries no playQueue when nothing has been saved, which
// is how the Subsonic server answers too.
type PlayQueueResponse struct {
Envelope
PlayQueue *PlayQueue `json:"playQueue,omitempty" xml:"playQueue,omitempty"`
}
// getPlayQueue returns the queue the listener last saved. A song no longer
// in the library drops out of it; if that was the current song, the queue
// has no current song.
func (b *browseHandlers) getPlayQueue(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
q := dbq.New(b.pool)
saved, err := q.GetPlayQueue(r.Context(), user.ID)
if errors.Is(err, pgx.ErrNoRows) {
Write(w, r, PlayQueueResponse{Envelope: NewEnvelope("ok")})
return
}
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load the play queue")
return
}
songs, err := songsInOrder(r.Context(), q, saved.TrackIds)
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load the play queue")
return
}
queue := &PlayQueue{
Username: user.Username,
Changed: timestamptzToRFC3339(saved.ChangedAt),
ChangedBy: saved.ChangedBy,
Entries: songs,
}
current := uuidToID(saved.CurrentTrackID)
if current != "" && slices.ContainsFunc(songs, func(s SongRef) bool { return s.ID == current }) {
queue.Current = current
queue.Position = saved.PositionMs
}
Write(w, r, PlayQueueResponse{Envelope: NewEnvelope("ok"), PlayQueue: queue})
}
// savePlayQueue replaces the listener's saved queue with the songs given as
// repeated id parameters, in order, with current the one playing and
// position the milliseconds into it. With no id the saved queue is cleared,
// as OpenSubsonic specifies.
func (b *browseHandlers) savePlayQueue(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
params := r.URL.Query()
q := dbq.New(b.pool)
raw := params["id"]
if len(raw) == 0 {
if err := q.DeletePlayQueue(r.Context(), user.ID); err != nil {
WriteFail(w, r, ErrGeneric, "Failed to clear the play queue")
return
}
Write(w, r, NewEnvelope("ok"))
return
}
if len(raw) > maxPlayQueue {
WriteFail(w, r, ErrGeneric, "A play queue holds at most "+strconv.Itoa(maxPlayQueue)+" songs")
return
}
ids := make([]pgtype.UUID, 0, len(raw))
for _, s := range raw {
id, ok := parseUUID(s)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Song not found: "+s)
return
}
ids = append(ids, id)
}
var current pgtype.UUID
if s := params.Get("current"); s != "" {
if current, ok = parseUUID(s); !ok {
WriteFail(w, r, ErrDataNotFound, "Song not found: "+s)
return
}
}
position, err := strconv.ParseInt(params.Get("position"), 10, 64)
if err != nil || position < 0 {
position = 0
}
if err := q.SavePlayQueue(r.Context(), dbq.SavePlayQueueParams{
UserID: user.ID, TrackIds: ids, CurrentTrackID: current,
PositionMs: position, ChangedBy: params.Get("c"),
}); err != nil {
WriteFail(w, r, ErrGeneric, "Failed to save the play queue")
return
}
Write(w, r, NewEnvelope("ok"))
}