feat(playlists): changes a phone queues offline are safe to send twice (#8023)
release / go (push) Successful in 1m59s
release / web (push) Successful in 1m23s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m38s
release / android (push) Successful in 4m45s
release / Build signed APK (push) Successful in 5m22s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 1m59s
release / web (push) Successful in 1m23s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m38s
release / android (push) Successful in 4m45s
release / Build signed APK (push) Successful in 5m22s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped
- A new playlist can carry the client's own id; sending the create again finds the same list. - Removing a song can name the song expected at that position; if another song is there now, nothing is removed (409 list_changed). - A reorder can give the songs in their new order (track_ids); sending it again changes nothing. - The playlist service tells the owner's clients about every change itself, so a change made from a Subsonic app refreshes the web and Android apps too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+59
-18
@@ -183,7 +183,10 @@ func (h *handlers) maybeKickBackgroundBuild(ctx context.Context, userID pgtype.U
|
||||
}()
|
||||
}
|
||||
|
||||
// createPlaylistBody's ID is optional: a client that makes a playlist
|
||||
// offline picks its id, so a retry of the same create finds the same list.
|
||||
type createPlaylistBody struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IsPublic bool `json:"is_public"`
|
||||
@@ -200,12 +203,19 @@ func (h *handlers) handleCreatePlaylist(w http.ResponseWriter, r *http.Request)
|
||||
if !decodeBody(w, r, &body) {
|
||||
return
|
||||
}
|
||||
row, err := h.playlists.Create(r.Context(), caller.ID, body.Name, body.Description, body.IsPublic)
|
||||
var id pgtype.UUID
|
||||
if body.ID != "" {
|
||||
var ok bool
|
||||
if id, ok = parseUUID(body.ID); !ok {
|
||||
writeErr(w, apierror.BadRequest("bad_request", "invalid playlist id"))
|
||||
return
|
||||
}
|
||||
}
|
||||
row, err := h.playlists.CreateWithID(r.Context(), caller.ID, id, body.Name, body.Description, body.IsPublic)
|
||||
if err != nil {
|
||||
h.writePlaylistErr(w, err, "create")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.created", caller.ID, row.ID)
|
||||
writeJSON(w, http.StatusOK, playlistRowToView(row))
|
||||
}
|
||||
|
||||
@@ -261,7 +271,6 @@ func (h *handlers) handleUpdatePlaylist(w http.ResponseWriter, r *http.Request)
|
||||
h.writePlaylistErr(w, err, "update")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.updated", caller.ID, playlistID)
|
||||
writeJSON(w, http.StatusOK, playlistRowToView(row))
|
||||
}
|
||||
|
||||
@@ -280,7 +289,6 @@ func (h *handlers) handleDeletePlaylist(w http.ResponseWriter, r *http.Request)
|
||||
h.writePlaylistErr(w, err, "delete")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.deleted", caller.ID, playlistID)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -304,20 +312,14 @@ func (h *handlers) handleAppendTracks(w http.ResponseWriter, r *http.Request) {
|
||||
if !decodeBody(w, r, &body) {
|
||||
return
|
||||
}
|
||||
trackIDs := make([]pgtype.UUID, 0, len(body.TrackIDs))
|
||||
for _, s := range body.TrackIDs {
|
||||
u, ok := parseUUID(s)
|
||||
if !ok {
|
||||
writeErr(w, apierror.BadRequest("bad_request", fmt.Sprintf("invalid track id %q", s)))
|
||||
return
|
||||
}
|
||||
trackIDs = append(trackIDs, u)
|
||||
trackIDs, ok := parseTrackIDs(w, body.TrackIDs)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.playlists.AppendTracks(r.Context(), caller.ID, playlistID, trackIDs); err != nil {
|
||||
h.writePlaylistErr(w, err, "append tracks")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.tracks_changed", caller.ID, playlistID)
|
||||
detail, err := h.playlists.Get(r.Context(), caller.ID, playlistID)
|
||||
if err != nil {
|
||||
h.writePlaylistErr(w, err, "get-after-append")
|
||||
@@ -344,11 +346,19 @@ func (h *handlers) handleRemovePlaylistTrack(w http.ResponseWriter, r *http.Requ
|
||||
writeErr(w, apierror.BadRequest("bad_request", "invalid position"))
|
||||
return
|
||||
}
|
||||
if err := h.playlists.RemoveTrack(r.Context(), caller.ID, playlistID, int32(pos64)); err != nil {
|
||||
// track_id, when sent, names the song the caller expects at position;
|
||||
// if another song is there now, nothing is removed (409).
|
||||
var expect pgtype.UUID
|
||||
if raw := r.URL.Query().Get("track_id"); raw != "" {
|
||||
if expect, ok = parseUUID(raw); !ok {
|
||||
writeErr(w, apierror.BadRequest("bad_request", "invalid track id"))
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := h.playlists.RemoveTrack(r.Context(), caller.ID, playlistID, int32(pos64), expect); err != nil {
|
||||
h.writePlaylistErr(w, err, "remove track")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.tracks_changed", caller.ID, playlistID)
|
||||
detail, err := h.playlists.Get(r.Context(), caller.ID, playlistID)
|
||||
if err != nil {
|
||||
h.writePlaylistErr(w, err, "get-after-remove")
|
||||
@@ -357,8 +367,13 @@ func (h *handlers) handleRemovePlaylistTrack(w http.ResponseWriter, r *http.Requ
|
||||
writeJSON(w, http.StatusOK, playlistDetailToView(detail))
|
||||
}
|
||||
|
||||
// reorderTracksBody carries the new order one of two ways:
|
||||
// ordered_positions, a permutation of every position, or track_ids, the
|
||||
// songs in their new order (see playlists.Service.ReorderTracks), which a
|
||||
// client can safely send again.
|
||||
type reorderTracksBody struct {
|
||||
OrderedPositions []int32 `json:"ordered_positions"`
|
||||
OrderedPositions []int32 `json:"ordered_positions"`
|
||||
TrackIDs []string `json:"track_ids"`
|
||||
}
|
||||
|
||||
// handleReorderPlaylist implements PUT /api/playlists/{id}/tracks. Owner
|
||||
@@ -377,11 +392,20 @@ func (h *handlers) handleReorderPlaylist(w http.ResponseWriter, r *http.Request)
|
||||
if !decodeBody(w, r, &body) {
|
||||
return
|
||||
}
|
||||
if err := h.playlists.Reorder(r.Context(), caller.ID, playlistID, body.OrderedPositions); err != nil {
|
||||
var err error
|
||||
if body.TrackIDs != nil {
|
||||
trackIDs, ok := parseTrackIDs(w, body.TrackIDs)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
err = h.playlists.ReorderTracks(r.Context(), caller.ID, playlistID, trackIDs)
|
||||
} else {
|
||||
err = h.playlists.Reorder(r.Context(), caller.ID, playlistID, body.OrderedPositions)
|
||||
}
|
||||
if err != nil {
|
||||
h.writePlaylistErr(w, err, "reorder")
|
||||
return
|
||||
}
|
||||
h.publishPlaylistEvent("playlist.tracks_changed", caller.ID, playlistID)
|
||||
detail, err := h.playlists.Get(r.Context(), caller.ID, playlistID)
|
||||
if err != nil {
|
||||
h.writePlaylistErr(w, err, "get-after-reorder")
|
||||
@@ -390,6 +414,21 @@ func (h *handlers) handleReorderPlaylist(w http.ResponseWriter, r *http.Request)
|
||||
writeJSON(w, http.StatusOK, playlistDetailToView(detail))
|
||||
}
|
||||
|
||||
// parseTrackIDs parses a body's track ids, or answers 400 naming the first
|
||||
// one that isn't an id and returns false.
|
||||
func parseTrackIDs(w http.ResponseWriter, raw []string) ([]pgtype.UUID, bool) {
|
||||
out := make([]pgtype.UUID, 0, len(raw))
|
||||
for _, s := range raw {
|
||||
u, ok := parseUUID(s)
|
||||
if !ok {
|
||||
writeErr(w, apierror.BadRequest("bad_request", fmt.Sprintf("invalid track id %q", s)))
|
||||
return nil, false
|
||||
}
|
||||
out = append(out, u)
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
|
||||
// handleGetPlaylistCover serves the cached collage from disk. The
|
||||
// playlist visibility check (owner-or-public) runs in the service Get
|
||||
// call before we look at cover_path, so we don't leak the existence of
|
||||
@@ -435,6 +474,8 @@ func (h *handlers) writePlaylistErr(w http.ResponseWriter, err error, op string)
|
||||
writeErr(w, apierror.Forbidden("system_playlist_readonly", "system-generated playlists cannot be edited"))
|
||||
case errors.Is(err, playlists.ErrInvalidInput):
|
||||
writeErr(w, apierror.BadRequest("bad_request", err.Error()))
|
||||
case errors.Is(err, playlists.ErrListChanged):
|
||||
writeErr(w, apierror.Conflict("list_changed", "the playlist has changed; reload it and try again"))
|
||||
case errors.Is(err, playlists.ErrTrackNotFound):
|
||||
writeErr(w, apierror.BadRequest("bad_request", "one of the supplied track ids does not exist"))
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user