feat: per-user volume leveling preference, synced across devices (M464 #4998)
release / govulncheck (push) Successful in 22s
release / go (push) Successful in 1m17s
release / web (push) Failing after 1m5s
release / android (push) Failing after 1m51s
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 / integration (push) Canceled after 4m12s
release / Build signed APK (releases and dev) (push) Canceled after 3m21s
release / govulncheck (push) Successful in 22s
release / go (push) Successful in 1m17s
release / web (push) Failing after 1m5s
release / android (push) Failing after 1m51s
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 / integration (push) Canceled after 4m12s
release / Build signed APK (releases and dev) (push) Canceled after 3m21s
Mode (off, auto, track, album), target (-18, -16, -14 LUFS) and boost (within headroom, or fully with a limiter), stored per user on the server so the web player, the Android app and casts apply the same one. - Server: user_normalization_prefs (migration 0067), GET/PUT /api/me/normalization; a whole-body PUT, validated, last write wins. - Web: Settings > Playback > Volume leveling. Saves at once, restores the old choice if the save fails, and caches the value for the player. - Android: Settings card. The device keeps a copy for offline playback (Room v9 with an explicit migration, so the upgrade wipes nothing). Writes are offline-first: shown at once, PUT best effort, queued on failure (NORMALIZATION_SET, collapsed to the newest). A refresh never overwrites a change still queued. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -109,6 +109,8 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev
|
||||
authed.Put("/me/password", h.handleChangePassword)
|
||||
authed.Put("/me/profile", h.handleUpdateMyProfile)
|
||||
authed.Put("/me/timezone", h.handlePutTimezone)
|
||||
authed.Get("/me/normalization", h.handleGetMyNormalization)
|
||||
authed.Put("/me/normalization", h.handlePutMyNormalization)
|
||||
authed.Post("/me/api-token", h.handleRegenerateMyAPIToken)
|
||||
authed.Get("/me/subsonic-password", h.handleGetMySubsonicPassword)
|
||||
authed.Post("/me/subsonic-password", h.handleGenerateMySubsonicPassword)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
)
|
||||
|
||||
// GET /api/me/normalization returns the caller's loudness-normalization
|
||||
// preference, or the defaults when they have never set one (M464 #4998).
|
||||
func (h *handlers) handleGetMyNormalization(w http.ResponseWriter, r *http.Request) {
|
||||
caller, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
p, err := library.LoadNormalizationPrefs(r.Context(), dbq.New(h.pool), caller.ID)
|
||||
if err != nil {
|
||||
writeErrWithLog(w, h.logger, "normalization: load", apierror.InternalMsg("lookup failed", err))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, p)
|
||||
}
|
||||
|
||||
// PUT /api/me/normalization replaces the caller's preference with the whole
|
||||
// body. Every field is required, so a client replaying a queued change
|
||||
// offline sends the full state it showed the user, and the last write wins.
|
||||
func (h *handlers) handlePutMyNormalization(w http.ResponseWriter, r *http.Request) {
|
||||
caller, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body library.NormalizationPrefs
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeErr(w, apierror.BadRequest("bad_body", "invalid JSON body"))
|
||||
return
|
||||
}
|
||||
p, err := library.SaveNormalizationPrefs(r.Context(), dbq.New(h.pool), caller.ID, body)
|
||||
if errors.Is(err, library.ErrNormalizationPrefsInvalid) {
|
||||
writeErr(w, apierror.BadRequest("invalid_normalization", err.Error()))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeErrWithLog(w, h.logger, "normalization: save", apierror.InternalMsg("save failed", err))
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, p)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
)
|
||||
|
||||
func TestMyNormalization_GetPut(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
user := seedUser(t, pool, "norm1", "pw", false)
|
||||
r := chi.NewRouter()
|
||||
r.Get("/api/me/normalization", h.handleGetMyNormalization)
|
||||
r.Put("/api/me/normalization", h.handlePutMyNormalization)
|
||||
do := func(method, body string) (int, library.NormalizationPrefs) {
|
||||
t.Helper()
|
||||
req := withUser(httptest.NewRequest(method, "/api/me/normalization", bytes.NewBufferString(body)), user)
|
||||
rec := httptest.NewRecorder()
|
||||
r.ServeHTTP(rec, req)
|
||||
var p library.NormalizationPrefs
|
||||
if rec.Code == http.StatusOK {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &p); err != nil {
|
||||
t.Fatalf("decode %s: %v", rec.Body.String(), err)
|
||||
}
|
||||
}
|
||||
return rec.Code, p
|
||||
}
|
||||
|
||||
if code, p := do(http.MethodGet, ""); code != http.StatusOK || p != library.DefaultNormalizationPrefs {
|
||||
t.Fatalf("GET before any save = %d %+v, want 200 with the defaults", code, p)
|
||||
}
|
||||
|
||||
want := library.NormalizationPrefs{Mode: "album", TargetLUFS: -16, Boost: "limiter"}
|
||||
if code, p := do(http.MethodPut, `{"mode":"album","target_lufs":-16,"boost":"limiter"}`); code != http.StatusOK || p != want {
|
||||
t.Fatalf("PUT = %d %+v, want 200 %+v", code, p, want)
|
||||
}
|
||||
if code, p := do(http.MethodGet, ""); code != http.StatusOK || p != want {
|
||||
t.Errorf("GET after PUT = %d %+v, want %+v", code, p, want)
|
||||
}
|
||||
|
||||
// Bad values and partial bodies are refused, and the stored prefs stand.
|
||||
for _, body := range []string{
|
||||
`{"mode":"loud","target_lufs":-16,"boost":"limiter"}`,
|
||||
`{"mode":"album","target_lufs":-23,"boost":"limiter"}`,
|
||||
`{"mode":"album","target_lufs":-16}`,
|
||||
`not json`,
|
||||
} {
|
||||
if code, _ := do(http.MethodPut, body); code != http.StatusBadRequest {
|
||||
t.Errorf("PUT %s = %d, want 400", body, code)
|
||||
}
|
||||
}
|
||||
if _, p := do(http.MethodGet, ""); p != want {
|
||||
t.Errorf("a refused PUT changed the prefs to %+v", p)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user