Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
138 lines
4.1 KiB
Go
138 lines
4.1 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/discover"
|
|
"git.kmalloc.org/squid/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, discover.ErrBusy):
|
|
c.Header("Retry-After", "2")
|
|
writeError(c, http.StatusTooManyRequests, "discover_busy", 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
|
|
}
|