feat(discover): time-boxed suggestion snooze, server side — #2374
Migration 0049 adds suggestion_snoozes(user_id, candidate_mbid, candidate_name, snoozed_until), and SuggestArtistsForUser excludes rows whose snooze hasn't expired. This is NOT a dislike. Rule #101 forbids a "Not for me" / thumbs-down UI; a snooze is the approved shape instead because it records no verdict on the music, expires on its own (~90d), and never reaches the taste profile. It's acquisition triage — "not right now" — so the filter sits at the candidate stage rather than in the score, where it would become a ranking signal by the back door. Per-user throughout (rule #47): one household member parking a candidate leaves everyone else's deck untouched. candidate_name is denormalized because suggestions are out-of-library by definition — there is no artists row to resolve a display name from, and the un-snooze list has to show something. That list is why GET /discover/snoozes exists at all: a parked candidate is by definition absent from the deck, so without it the DELETE would be unreachable. Also fixes a hole in the codegen check from #2380: `git diff` ignores untracked paths, so a brand-new generated file would have passed it silently. `git add -N` first. This commit is the first to add one. Endpoints: POST /api/discover/suggestions/{mbid}/snooze (body: name, days) DELETE /api/discover/suggestions/{mbid}/snooze GET /api/discover/snoozes UI lands in slice 4 (#2375) before any of this merges — rule #27. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,19 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/recommendation"
|
||||
)
|
||||
@@ -92,6 +98,150 @@ func (h *handlers) handleListSuggestions(w http.ResponseWriter, r *http.Request)
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// Snooze duration bounds. 90 days is long enough that a parked suggestion
|
||||
// stops feeling like it's nagging, short enough that a taste shift brings it
|
||||
// back on its own — the whole point of a snooze over a dismissal (#2374).
|
||||
const (
|
||||
defaultSnoozeDays = 90.0
|
||||
maxSnoozeDays = 365.0
|
||||
)
|
||||
|
||||
// snoozeRequest is the POST body. Both fields are optional in the JSON sense
|
||||
// (an absent body snoozes for the default), but Name is required in practice:
|
||||
// candidates are out-of-library, so the server has no artists row to resolve a
|
||||
// display name from and the un-snooze list would have nothing to show. The
|
||||
// client always has it — it just rendered the card.
|
||||
type snoozeRequest struct {
|
||||
Name string `json:"name"`
|
||||
Days float64 `json:"days"`
|
||||
}
|
||||
|
||||
// snoozeView is one row of GET /api/discover/snoozes.
|
||||
type snoozeView struct {
|
||||
MBID string `json:"mbid"`
|
||||
Name string `json:"name"`
|
||||
SnoozedUntil pgtype.Timestamptz `json:"snoozed_until"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
// handleSnoozeSuggestion implements
|
||||
// POST /api/discover/suggestions/{mbid}/snooze.
|
||||
//
|
||||
// Parks a candidate for `days` (default 90, capped at 365). Idempotent:
|
||||
// snoozing an already-snoozed candidate extends it rather than conflicting.
|
||||
//
|
||||
// This is NOT negative feedback. It records no verdict on the artist and is
|
||||
// never read by internal/taste — see 0049_suggestion_snoozes.up.sql for the
|
||||
// rule #101 reasoning. Returns 204.
|
||||
func (h *handlers) handleSnoozeSuggestion(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
mbid := strings.TrimSpace(chi.URLParam(r, "mbid"))
|
||||
if mbid == "" {
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "missing mbid"))
|
||||
return
|
||||
}
|
||||
|
||||
// An empty body is a valid "snooze this for the default period", so EOF
|
||||
// is not an error here — decodeBody would reject it as a malformed body.
|
||||
var body snoozeRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil && !errors.Is(err, io.EOF) {
|
||||
writeErr(w, apierror.BadRequest("invalid_body", ""))
|
||||
return
|
||||
}
|
||||
name := strings.TrimSpace(body.Name)
|
||||
if name == "" {
|
||||
writeErr(w, apierror.BadRequest("invalid_body", "name is required"))
|
||||
return
|
||||
}
|
||||
days := body.Days
|
||||
if days <= 0 {
|
||||
days = defaultSnoozeDays
|
||||
}
|
||||
if days > maxSnoozeDays {
|
||||
// Clamp rather than reject: a client asking for longer than we allow
|
||||
// still means "park this", and failing the write would leave the card
|
||||
// sitting there as if the tap did nothing.
|
||||
days = maxSnoozeDays
|
||||
}
|
||||
|
||||
q := dbq.New(h.pool)
|
||||
if err := q.SnoozeSuggestion(r.Context(), dbq.SnoozeSuggestionParams{
|
||||
UserID: user.ID,
|
||||
CandidateMbid: mbid,
|
||||
CandidateName: name,
|
||||
Column4: days,
|
||||
}); err != nil {
|
||||
h.logger.Error("api: snooze suggestion", "err", err)
|
||||
writeErr(w, apierror.InternalMsg("failed to snooze suggestion", err))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// handleUnsnoozeSuggestion implements
|
||||
// DELETE /api/discover/suggestions/{mbid}/snooze.
|
||||
//
|
||||
// Brings a parked candidate back immediately. 404s an MBID this user never
|
||||
// snoozed, so the client can tell "undone" from "there was nothing there".
|
||||
func (h *handlers) handleUnsnoozeSuggestion(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
mbid := strings.TrimSpace(chi.URLParam(r, "mbid"))
|
||||
if mbid == "" {
|
||||
writeErr(w, apierror.BadRequest("invalid_id", "missing mbid"))
|
||||
return
|
||||
}
|
||||
q := dbq.New(h.pool)
|
||||
rows, err := q.UnsnoozeSuggestion(r.Context(), dbq.UnsnoozeSuggestionParams{
|
||||
UserID: user.ID,
|
||||
CandidateMbid: mbid,
|
||||
})
|
||||
if err != nil {
|
||||
h.logger.Error("api: unsnooze suggestion", "err", err)
|
||||
writeErr(w, apierror.InternalMsg("failed to unsnooze suggestion", err))
|
||||
return
|
||||
}
|
||||
if rows == 0 {
|
||||
writeErr(w, apierror.NotFound("snooze"))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// handleListSuggestionSnoozes implements GET /api/discover/snoozes.
|
||||
//
|
||||
// The un-snooze surface needs this: a parked candidate is by definition
|
||||
// absent from the suggestion deck, so without a list there is no way to
|
||||
// reach the DELETE above. Scoped to the caller (rule #47). Expired rows are
|
||||
// already filtered by the query — the hourly gc sweep only reclaims space.
|
||||
func (h *handlers) handleListSuggestionSnoozes(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := requireUser(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
rows, err := dbq.New(h.pool).ListActiveSuggestionSnoozes(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
h.logger.Error("api: list suggestion snoozes", "err", err)
|
||||
writeErr(w, apierror.InternalMsg("failed to load snoozes", err))
|
||||
return
|
||||
}
|
||||
out := make([]snoozeView, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, snoozeView{
|
||||
MBID: row.CandidateMbid,
|
||||
Name: row.CandidateName,
|
||||
SnoozedUntil: row.SnoozedUntil,
|
||||
CreatedAt: row.CreatedAt,
|
||||
})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// resolveSuggestionArt fills ImageURL on-demand from Lidarr's artist
|
||||
// lookup, matched by MBID (foreignArtistId). Best-effort and cache-free:
|
||||
// Lidarr is the only source — when it's disabled, unreachable, or has
|
||||
|
||||
Reference in New Issue
Block a user