- PUT/DELETE /wishlist/{artist} and GET /wishlist: the artists I want.
- POST/DELETE /discover/{artist}/dismiss and GET /discover/dismissed: the
artists I am not interested in. An artist is wished or dismissed, never both,
and both lists are mine alone.
- Discover never suggests a dismissed artist, does not use one as a seed taken
from my listening, and marks the wished ones it shows (wishlisted).
- New artist_feedback table (migration 0012, SQLite and PostgreSQL), removed
with the account.
- An artist I marked is not cleaned up when the list that named it is replaced,
and goes when its last mark is taken off and nobody names it. Taking a
provider's data back still removes what only it named, marks included.
- Spec, contract test and README updated.
Closes #35
134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"codeberg.org/squidd/sonda/internal/discover"
|
|
"codeberg.org/squidd/sonda/internal/discovery"
|
|
)
|
|
|
|
type discoverHandlers struct{ svc discoverService }
|
|
|
|
type seedJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Weight float64 `json:"weight"`
|
|
}
|
|
|
|
type componentsJSON struct {
|
|
A float64 `json:"a"`
|
|
G *float64 `json:"g"` // null when the artist has no tags to compare
|
|
P float64 `json:"p"`
|
|
D float64 `json:"d"`
|
|
}
|
|
|
|
type reasonJSON struct {
|
|
Artist artistRefJSON `json:"artist"`
|
|
Match float64 `json:"match"`
|
|
}
|
|
|
|
type suggestionJSON struct {
|
|
Artist artistRefJSON `json:"artist"`
|
|
InLibrary bool `json:"in_library"`
|
|
Wishlisted bool `json:"wishlisted"`
|
|
Score float64 `json:"score"`
|
|
Components componentsJSON `json:"components"`
|
|
Because []reasonJSON `json:"because"`
|
|
}
|
|
|
|
type discoverJSON struct {
|
|
ProviderStatus string `json:"provider_status"`
|
|
Attribution string `json:"attribution"`
|
|
FromListening bool `json:"from_listening"`
|
|
Seeds []seedJSON `json:"seeds"`
|
|
Items []suggestionJSON `json:"items"`
|
|
}
|
|
|
|
// seedParams reads every ?seed=, each a whole number. It answers 400 itself.
|
|
func seedParams(c *gin.Context) ([]int64, bool) {
|
|
var out []int64
|
|
for _, raw := range c.QueryArray("seed") {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "seed must be an artist id")
|
|
return nil, false
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// inLibraryParam reads ?in_library=: absent means both, otherwise exactly true or false once.
|
|
func inLibraryParam(c *gin.Context) (*bool, bool) {
|
|
values := c.QueryArray("in_library")
|
|
switch {
|
|
case len(values) == 0:
|
|
return nil, true
|
|
case len(values) == 1 && (values[0] == "true" || values[0] == "false"):
|
|
v := values[0] == "true"
|
|
return &v, true
|
|
}
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "in_library must be true or false")
|
|
return nil, false
|
|
}
|
|
|
|
// discover suggests artists the signed-in user may like.
|
|
func (h *discoverHandlers) discover(c *gin.Context) {
|
|
seeds, ok := seedParams(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return
|
|
}
|
|
inLibrary, ok := inLibraryParam(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
res, err := h.svc.Discover(c.Request.Context(), currentUserID(c),
|
|
discover.Query{Seeds: seeds, Limit: clampInt(limit), InLibrary: inLibrary})
|
|
switch {
|
|
case errors.Is(err, discover.ErrSeedRequired):
|
|
writeError(c, http.StatusBadRequest, "seed_required", err.Error())
|
|
return
|
|
case errors.Is(err, discover.ErrInvalidQuery):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
return
|
|
case errors.Is(err, context.Canceled):
|
|
c.AbortWithStatus(statusClientClosed) // the client left; nobody is waiting
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, toDiscoverJSON(res))
|
|
}
|
|
|
|
func toDiscoverJSON(r discover.Result) discoverJSON {
|
|
out := discoverJSON{
|
|
ProviderStatus: r.ProviderStatus, Attribution: discovery.Attribution, FromListening: r.FromListening,
|
|
Seeds: make([]seedJSON, len(r.Seeds)), Items: make([]suggestionJSON, len(r.Items)),
|
|
}
|
|
for i, s := range r.Seeds {
|
|
out.Seeds[i] = seedJSON{ID: s.ID, Name: s.Name, Weight: s.Weight}
|
|
}
|
|
for i, it := range r.Items {
|
|
s := suggestionJSON{
|
|
Artist: artistRefJSON{ID: it.Artist.ID, Name: it.Artist.Name}, InLibrary: it.InLibrary, Wishlisted: it.Wishlisted, Score: it.Score,
|
|
Components: componentsJSON{A: it.Components.A, G: it.Components.G, P: it.Components.P, D: it.Components.D},
|
|
Because: make([]reasonJSON, len(it.Because)),
|
|
}
|
|
for j, b := range it.Because {
|
|
s.Because[j] = reasonJSON{Artist: artistRefJSON{ID: b.Artist.ID, Name: b.Artist.Name}, Match: b.Match}
|
|
}
|
|
out.Items[i] = s
|
|
}
|
|
return out
|
|
}
|