feat(subsonic): add /rest/star and /rest/unstar handlers
Validate-all-first atomicity on star: a missing entity refuses the whole call with Subsonic error 70. Unstar is a best-effort delete (missing entities are no-ops, matching client expectations after library moves).
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
)
|
||||
|
||||
// handleStar implements /rest/star. Accepts any combination of id (track),
|
||||
// albumId, artistId. All entities are validated before any insert; a single
|
||||
// missing entity fails the whole call with Subsonic error 70.
|
||||
func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := UserFromContext(r.Context())
|
||||
if !ok {
|
||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
||||
return
|
||||
}
|
||||
params := r.URL.Query()
|
||||
q := dbq.New(m.pool)
|
||||
trackID, err := resolveStarID(r.Context(), q, params.Get("id"), entityKindTrack)
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrDataNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
albumID, err := resolveStarID(r.Context(), q, params.Get("albumId"), entityKindAlbum)
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrDataNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
artistID, err := resolveStarID(r.Context(), q, params.Get("artistId"), entityKindArtist)
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrDataNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
if trackID.Valid {
|
||||
if err := q.LikeTrack(r.Context(), dbq.LikeTrackParams{UserID: user.ID, TrackID: trackID}); err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Could not star track")
|
||||
return
|
||||
}
|
||||
}
|
||||
if albumID.Valid {
|
||||
if err := q.LikeAlbum(r.Context(), dbq.LikeAlbumParams{UserID: user.ID, AlbumID: albumID}); err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Could not star album")
|
||||
return
|
||||
}
|
||||
}
|
||||
if artistID.Valid {
|
||||
if err := q.LikeArtist(r.Context(), dbq.LikeArtistParams{UserID: user.ID, ArtistID: artistID}); err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Could not star artist")
|
||||
return
|
||||
}
|
||||
}
|
||||
Write(w, r, struct{ Envelope }{Envelope: NewEnvelope("ok")})
|
||||
}
|
||||
|
||||
// handleUnstar mirrors handleStar but DELETEs. Missing entities are treated
|
||||
// as a no-op rather than an error — Subsonic clients commonly call unstar
|
||||
// on entities the server has never seen (cross-server library moves).
|
||||
func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := UserFromContext(r.Context())
|
||||
if !ok {
|
||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
||||
return
|
||||
}
|
||||
params := r.URL.Query()
|
||||
q := dbq.New(m.pool)
|
||||
if t, ok := parseUUID(params.Get("id")); ok {
|
||||
_ = q.UnlikeTrack(r.Context(), dbq.UnlikeTrackParams{UserID: user.ID, TrackID: t})
|
||||
}
|
||||
if a, ok := parseUUID(params.Get("albumId")); ok {
|
||||
_ = q.UnlikeAlbum(r.Context(), dbq.UnlikeAlbumParams{UserID: user.ID, AlbumID: a})
|
||||
}
|
||||
if a, ok := parseUUID(params.Get("artistId")); ok {
|
||||
_ = q.UnlikeArtist(r.Context(), dbq.UnlikeArtistParams{UserID: user.ID, ArtistID: a})
|
||||
}
|
||||
Write(w, r, struct{ Envelope }{Envelope: NewEnvelope("ok")})
|
||||
}
|
||||
|
||||
type entityKind int
|
||||
|
||||
const (
|
||||
entityKindTrack entityKind = iota
|
||||
entityKindAlbum
|
||||
entityKindArtist
|
||||
)
|
||||
|
||||
// resolveStarID returns (uuid, nil) if the param is empty (no-op), or
|
||||
// (uuid, nil) if the entity exists, or (zero, error) if the entity is
|
||||
// missing/malformed. Non-empty malformed UUID is treated as a 'not found'
|
||||
// per Subsonic semantics.
|
||||
func resolveStarID(ctx context.Context, q *dbq.Queries, raw string, kind entityKind) (pgtype.UUID, error) {
|
||||
if raw == "" {
|
||||
return pgtype.UUID{}, nil
|
||||
}
|
||||
id, ok := parseUUID(raw)
|
||||
if !ok {
|
||||
return pgtype.UUID{}, errors.New("not found")
|
||||
}
|
||||
switch kind {
|
||||
case entityKindTrack:
|
||||
if _, err := q.GetTrackByID(ctx, id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return pgtype.UUID{}, errors.New("track not found")
|
||||
}
|
||||
return pgtype.UUID{}, err
|
||||
}
|
||||
case entityKindAlbum:
|
||||
if _, err := q.GetAlbumByID(ctx, id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return pgtype.UUID{}, errors.New("album not found")
|
||||
}
|
||||
return pgtype.UUID{}, err
|
||||
}
|
||||
case entityKindArtist:
|
||||
if _, err := q.GetArtistByID(ctx, id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return pgtype.UUID{}, errors.New("artist not found")
|
||||
}
|
||||
return pgtype.UUID{}, err
|
||||
}
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
Reference in New Issue
Block a user