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")) }