feat(feedback): a wishlist and "not interested"
- 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
This commit is contained in:
parent
154701191b
commit
a6dbf60ac8
8 changed files with 1283 additions and 32 deletions
|
|
@ -18,6 +18,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/auth"
|
||||
"codeberg.org/squidd/sonda/internal/discover"
|
||||
"codeberg.org/squidd/sonda/internal/discovery"
|
||||
"codeberg.org/squidd/sonda/internal/feedback"
|
||||
"codeberg.org/squidd/sonda/internal/library"
|
||||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
|
|
@ -93,10 +94,15 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun
|
|||
Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)),
|
||||
Streams: streaming.NewService(noFiles{}),
|
||||
}
|
||||
deps.Discover = discover.NewService(deps.Taste.(discover.Taster), deps.Discovery.(discover.Similar),
|
||||
discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()),
|
||||
storage.NewTasteRepo(db), usersettings.NewService(storage.NewUserSettingsRepo(db)), discover.DefaultWeights())
|
||||
feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db))
|
||||
deps.Feedback = feedbackSvc
|
||||
mutate(&deps, db)
|
||||
// Built from the final services, so a test that swaps one gets a Discover that uses it.
|
||||
if deps.Discover == nil {
|
||||
deps.Discover = discover.NewService(deps.Taste.(discover.Taster), deps.Discovery.(discover.Similar),
|
||||
discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()),
|
||||
storage.NewTasteRepo(db), usersettings.NewService(storage.NewUserSettingsRepo(db)), feedbackSvc, discover.DefaultWeights())
|
||||
}
|
||||
return NewRouter(deps), db
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -547,6 +547,27 @@ func TestResponsesMatchTheSpec(t *testing.T) {
|
|||
}
|
||||
c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"discover_from_listening": false}), 200, "forbid it again")
|
||||
|
||||
// ---- wishlist and dismissed ----
|
||||
gammaID := idOf(found["items"].([]any)[0].(map[string]any)["artist"].(map[string]any))
|
||||
c.expect(c.call("PUT", "/wishlist/"+gammaID, token, nil), 204, "wish for an artist")
|
||||
if wl := c.expect(c.call("GET", "/wishlist?limit=5", token, nil), 200, "wishlist"); wl["total"] != float64(1) {
|
||||
t.Fatalf("wishlist = %v", wl)
|
||||
}
|
||||
c.expect(c.call("PUT", "/wishlist/999", token, nil), 404, "wish for an unknown artist", "artist_not_found")
|
||||
c.expect(c.call("PUT", "/wishlist/abc", token, nil), 400, "wish for a bad id", "invalid_input")
|
||||
c.expect(c.call("GET", "/wishlist?limit=-1", token, nil), 400, "wishlist with a bad limit", "invalid_input")
|
||||
c.expect(c.call("GET", "/wishlist", "", nil), 401, "wishlist without a token", "unauthorized")
|
||||
c.expect(c.call("DELETE", "/wishlist/"+gammaID, token, nil), 204, "take it off the wishlist")
|
||||
c.expect(c.call("DELETE", "/wishlist/999", token, nil), 404, "take an unknown artist off", "artist_not_found")
|
||||
c.expect(c.call("POST", "/discover/"+gammaID+"/dismiss", token, nil), 204, "dismiss an artist")
|
||||
if dm := c.expect(c.call("GET", "/discover/dismissed", token, nil), 200, "dismissed"); dm["total"] != float64(1) {
|
||||
t.Fatalf("dismissed = %v", dm)
|
||||
}
|
||||
c.expect(c.call("POST", "/discover/999/dismiss", token, nil), 404, "dismiss an unknown artist", "artist_not_found")
|
||||
c.expect(c.call("GET", "/discover/dismissed?offset=-1", token, nil), 400, "dismissed with a bad offset", "invalid_input")
|
||||
c.expect(c.call("DELETE", "/discover/"+gammaID+"/dismiss", token, nil), 204, "interested again")
|
||||
c.expect(c.call("DELETE", "/discover/999/dismiss", token, nil), 404, "undismiss an unknown artist", "artist_not_found")
|
||||
|
||||
// ---- my settings ----
|
||||
if got := c.expect(c.call("GET", "/me/settings", token, nil), 200, "settings"); got["discover_from_listening"] != false {
|
||||
t.Fatalf("settings = %v, want everything off", got)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import (
|
|||
|
||||
"codeberg.org/squidd/sonda/internal/discover"
|
||||
"codeberg.org/squidd/sonda/internal/discovery"
|
||||
"codeberg.org/squidd/sonda/internal/feedback"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
"codeberg.org/squidd/sonda/internal/storage"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
|
|
@ -38,7 +39,9 @@ func newDiscoverEnv(t *testing.T) *discoverEnv {
|
|||
settings := usersettings.NewService(storage.NewUserSettingsRepo(db))
|
||||
tags := discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{providers.LastFM: e.lf, providers.MusicBrainz: e.mb}, prov, zap.NewNop())
|
||||
d.Taste, d.Settings = tasteSvc, settings
|
||||
d.Discover = discover.NewService(tasteSvc, d.Discovery.(discover.Similar), tags, storage.NewTasteRepo(db), settings, discover.DefaultWeights())
|
||||
fb := feedback.NewService(storage.NewFeedbackRepo(db))
|
||||
d.Feedback = fb
|
||||
d.Discover = discover.NewService(tasteSvc, d.Discovery.(discover.Similar), tags, storage.NewTasteRepo(db), settings, fb, discover.DefaultWeights())
|
||||
})
|
||||
return e
|
||||
}
|
||||
|
|
@ -110,7 +113,7 @@ func TestDiscoverFromANamedSeed(t *testing.T) {
|
|||
}
|
||||
|
||||
beta, gamma := itemFor(t, r, "Beta"), itemFor(t, r, "Gamma")
|
||||
sameKeys(t, "item", beta, "artist", "in_library", "score", "components", "because")
|
||||
sameKeys(t, "item", beta, "artist", "in_library", "wishlisted", "score", "components", "because")
|
||||
sameKeys(t, "components", beta["components"].(map[string]any), "a", "g", "p", "d")
|
||||
if beta["in_library"] != true || gamma["in_library"] != false {
|
||||
t.Errorf("in_library: Beta %v, Gamma %v", beta["in_library"], gamma["in_library"])
|
||||
|
|
|
|||
189
internal/api/http/feedback_test.go
Normal file
189
internal/api/http/feedback_test.go
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func id(n int64) string { return strconv.FormatInt(n, 10) }
|
||||
|
||||
// gamma runs Discover once so that Last.fm's answer is stored and Gamma (an artist
|
||||
// with no files) exists, and returns its id.
|
||||
func (e *discoverEnv) gamma(t *testing.T) int64 {
|
||||
t.Helper()
|
||||
r := e.get(t, "?"+e.seed(e.alpha))
|
||||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("discover: %d %v", r.Code, r.Body)
|
||||
}
|
||||
return int64(itemFor(t, r, "Gamma")["artist"].(map[string]any)["id"].(float64))
|
||||
}
|
||||
|
||||
func (e *discoverEnv) bob(t *testing.T) string {
|
||||
t.Helper()
|
||||
body := map[string]any{"username": "bob", "password": "longenough"}
|
||||
if r := do(t, e.h, "POST", "/api/v1/users", e.admin, body); r.Code != http.StatusCreated {
|
||||
t.Fatalf("create bob: %d %v", r.Code, r.Body)
|
||||
}
|
||||
tok, _ := do(t, e.h, "POST", "/api/v1/auth/login", "", body).Body["access_token"].(string)
|
||||
return tok
|
||||
}
|
||||
|
||||
func TestFeedbackRoutesNeedASignedInUser(t *testing.T) {
|
||||
e := newDiscoverEnv(t)
|
||||
for _, rt := range []struct{ method, path string }{
|
||||
{"GET", "/api/v1/wishlist"}, {"PUT", "/api/v1/wishlist/1"}, {"DELETE", "/api/v1/wishlist/1"},
|
||||
{"GET", "/api/v1/discover/dismissed"}, {"POST", "/api/v1/discover/1/dismiss"}, {"DELETE", "/api/v1/discover/1/dismiss"},
|
||||
} {
|
||||
if r := do(t, e.h, rt.method, rt.path, "", nil); r.Code != http.StatusUnauthorized {
|
||||
t.Errorf("%s %s without a token: %d", rt.method, rt.path, r.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWishlist(t *testing.T) {
|
||||
e := newDiscoverEnv(t)
|
||||
e.enableLastfm(t)
|
||||
gamma := e.gamma(t)
|
||||
|
||||
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Code != http.StatusOK || len(r.Body["items"].([]any)) != 0 || r.Body["total"] != float64(0) {
|
||||
t.Fatalf("empty wishlist: %d %v", r.Code, r.Body)
|
||||
}
|
||||
for i := 0; i < 2; i++ { // twice: the same answer
|
||||
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("PUT %d: %d %v", i, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(e.beta), e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("PUT beta: %d %v", r.Code, r.Body)
|
||||
}
|
||||
r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil)
|
||||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("%d %v", r.Code, r.Body)
|
||||
}
|
||||
sameKeys(t, "page", r.Body, "items", "total", "limit", "offset")
|
||||
items := r.Body["items"].([]any)
|
||||
if r.Body["total"] != float64(2) || len(items) != 2 || r.Body["limit"] != float64(50) || r.Body["offset"] != float64(0) {
|
||||
t.Fatalf("wishlist = %v", r.Body)
|
||||
}
|
||||
first := items[0].(map[string]any)
|
||||
sameKeys(t, "entry", first, "artist", "in_library", "marked_at")
|
||||
sameKeys(t, "artist", first["artist"].(map[string]any), "id", "name")
|
||||
byName := map[string]map[string]any{}
|
||||
for _, it := range items {
|
||||
m := it.(map[string]any)
|
||||
byName[m["artist"].(map[string]any)["name"].(string)] = m
|
||||
}
|
||||
if byName["Gamma"]["in_library"] != false || byName["Beta"]["in_library"] != true {
|
||||
t.Errorf("in_library: Gamma %v, Beta %v", byName["Gamma"]["in_library"], byName["Beta"]["in_library"])
|
||||
}
|
||||
|
||||
// Discover says which suggestions are wished for.
|
||||
d := e.get(t, "?"+e.seed(e.alpha))
|
||||
if itemFor(t, d, "Gamma")["wishlisted"] != true || itemFor(t, d, "Beta")["wishlisted"] != true {
|
||||
t.Errorf("wishlisted flags: %v", d.Body["items"])
|
||||
}
|
||||
|
||||
// Take it off, twice.
|
||||
for i := 0; i < 2; i++ {
|
||||
if r := do(t, e.h, "DELETE", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("DELETE %d: %d %v", i, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if r := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); r.Body["total"] != float64(1) {
|
||||
t.Errorf("after DELETE: %v", r.Body)
|
||||
}
|
||||
if d = e.get(t, "?"+e.seed(e.alpha)); itemFor(t, d, "Gamma")["wishlisted"] != false {
|
||||
t.Error("Gamma is still marked as wished")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWishlistRefusesWhatIsNotAnArtistAndPagesStrictly(t *testing.T) {
|
||||
e := newDiscoverEnv(t)
|
||||
for _, rt := range []struct {
|
||||
method, path string
|
||||
code int
|
||||
err string
|
||||
}{
|
||||
{"PUT", "/api/v1/wishlist/99999", 404, "artist_not_found"},
|
||||
{"DELETE", "/api/v1/wishlist/99999", 404, "artist_not_found"},
|
||||
{"PUT", "/api/v1/wishlist/abc", 400, "invalid_input"},
|
||||
{"PUT", "/api/v1/wishlist/0", 400, "invalid_input"},
|
||||
{"DELETE", "/api/v1/wishlist/-2", 400, "invalid_input"},
|
||||
{"GET", "/api/v1/wishlist?limit=x", 400, "invalid_input"},
|
||||
{"GET", "/api/v1/wishlist?limit=-1", 400, "invalid_input"},
|
||||
{"GET", "/api/v1/wishlist?offset=-1", 400, "invalid_input"},
|
||||
{"POST", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
|
||||
{"POST", "/api/v1/discover/abc/dismiss", 400, "invalid_input"},
|
||||
{"DELETE", "/api/v1/discover/99999/dismiss", 404, "artist_not_found"},
|
||||
{"GET", "/api/v1/discover/dismissed?limit=-1", 400, "invalid_input"},
|
||||
} {
|
||||
r := do(t, e.h, rt.method, rt.path, e.admin, nil)
|
||||
if r.Code != rt.code || errCode(r) != rt.err {
|
||||
t.Errorf("%s %s: %d %v, want %d %s", rt.method, rt.path, r.Code, r.Body, rt.code, rt.err)
|
||||
}
|
||||
}
|
||||
if r := do(t, e.h, "GET", "/api/v1/wishlist?limit=100000", e.admin, nil); r.Code != http.StatusOK || r.Body["limit"] != float64(200) {
|
||||
t.Errorf("a large limit is lowered: %d %v", r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDismissHidesAnArtistFromDiscoverAndCanBeUndone(t *testing.T) {
|
||||
e := newDiscoverEnv(t)
|
||||
e.enableLastfm(t)
|
||||
gamma := e.gamma(t)
|
||||
|
||||
if r := do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("dismiss: %d %v", r.Code, r.Body)
|
||||
}
|
||||
d := e.get(t, "?"+e.seed(e.alpha))
|
||||
for _, it := range d.Body["items"].([]any) {
|
||||
if it.(map[string]any)["artist"].(map[string]any)["name"] == "Gamma" {
|
||||
t.Fatal("a dismissed artist is still suggested")
|
||||
}
|
||||
}
|
||||
r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil)
|
||||
if r.Code != http.StatusOK || r.Body["total"] != float64(1) {
|
||||
t.Fatalf("dismissed = %d %v", r.Code, r.Body)
|
||||
}
|
||||
if r.Body["items"].([]any)[0].(map[string]any)["artist"].(map[string]any)["name"] != "Gamma" {
|
||||
t.Errorf("dismissed = %v", r.Body)
|
||||
}
|
||||
// It is not on the wishlist, and wishing for it replaces the dismissal.
|
||||
if w := do(t, e.h, "GET", "/api/v1/wishlist", e.admin, nil); w.Body["total"] != float64(0) {
|
||||
t.Errorf("wishlist = %v", w.Body)
|
||||
}
|
||||
if r := do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("wish: %d", r.Code)
|
||||
}
|
||||
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", e.admin, nil); r.Body["total"] != float64(0) {
|
||||
t.Errorf("a wish must replace the dismissal: %v", r.Body)
|
||||
}
|
||||
// Dismiss again, then undo it.
|
||||
do(t, e.h, "POST", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil)
|
||||
if r := do(t, e.h, "DELETE", "/api/v1/discover/"+id(gamma)+"/dismiss", e.admin, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("undo: %d", r.Code)
|
||||
}
|
||||
d = e.get(t, "?"+e.seed(e.alpha))
|
||||
itemFor(t, d, "Gamma") // back
|
||||
}
|
||||
|
||||
func TestFeedbackIsPrivate(t *testing.T) {
|
||||
e := newDiscoverEnv(t)
|
||||
e.enableLastfm(t)
|
||||
gamma := e.gamma(t)
|
||||
bob := e.bob(t)
|
||||
do(t, e.h, "PUT", "/api/v1/wishlist/"+id(gamma), e.admin, nil)
|
||||
do(t, e.h, "POST", "/api/v1/discover/"+id(e.beta)+"/dismiss", e.admin, nil)
|
||||
|
||||
if r := do(t, e.h, "GET", "/api/v1/wishlist", bob, nil); r.Body["total"] != float64(0) {
|
||||
t.Errorf("bob sees alice's wishlist: %v", r.Body)
|
||||
}
|
||||
if r := do(t, e.h, "GET", "/api/v1/discover/dismissed", bob, nil); r.Body["total"] != float64(0) {
|
||||
t.Errorf("bob sees alice's dismissals: %v", r.Body)
|
||||
}
|
||||
d := do(t, e.h, "GET", "/api/v1/discover?"+e.seed(e.alpha), bob, nil)
|
||||
if d.Code != http.StatusOK || itemFor(t, d, "Beta") == nil || itemFor(t, d, "Gamma")["wishlisted"] != false {
|
||||
t.Errorf("bob's Discover is affected by alice's choices: %v", d.Body)
|
||||
}
|
||||
}
|
||||
|
|
@ -35,6 +35,7 @@ type reasonJSON struct {
|
|||
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"`
|
||||
|
|
@ -120,7 +121,7 @@ func toDiscoverJSON(r discover.Result) discoverJSON {
|
|||
}
|
||||
for i, it := range r.Items {
|
||||
s := suggestionJSON{
|
||||
Artist: artistRefJSON{ID: it.Artist.ID, Name: it.Artist.Name}, InLibrary: it.InLibrary, Score: it.Score,
|
||||
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)),
|
||||
}
|
||||
|
|
|
|||
97
internal/api/http/handlers_feedback.go
Normal file
97
internal/api/http/handlers_feedback.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/feedback"
|
||||
)
|
||||
|
||||
// feedbackHandlers serve the signed-in user's wishlist and the artists they are
|
||||
// not interested in. Both are theirs alone.
|
||||
type feedbackHandlers struct{ svc feedbackService }
|
||||
|
||||
type markedJSON struct {
|
||||
Artist artistRefJSON `json:"artist"`
|
||||
InLibrary bool `json:"in_library"`
|
||||
MarkedAt string `json:"marked_at"`
|
||||
}
|
||||
|
||||
type markedPageJSON struct {
|
||||
Items []markedJSON `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
func writeFeedbackError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, feedback.ErrArtistNotFound):
|
||||
writeError(c, http.StatusNotFound, "artist_not_found", "no such artist")
|
||||
case errors.Is(err, feedback.ErrUserNotFound):
|
||||
writeError(c, http.StatusUnauthorized, "unauthorized", "account no longer exists")
|
||||
case errors.Is(err, feedback.ErrInvalidQuery):
|
||||
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
||||
default:
|
||||
_ = c.Error(err)
|
||||
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
||||
}
|
||||
}
|
||||
|
||||
// add marks the artist of the path. It answers 204, also when it was marked already.
|
||||
func (h *feedbackHandlers) add(kind feedback.Kind) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.Add(c.Request.Context(), currentUserID(c), id, kind); err != nil {
|
||||
writeFeedbackError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
// remove takes the mark off. It answers 204, also when there was none.
|
||||
func (h *feedbackHandlers) remove(kind feedback.Kind) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.Remove(c.Request.Context(), currentUserID(c), id, kind); err != nil {
|
||||
writeFeedbackError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *feedbackHandlers) list(kind feedback.Kind) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
limit, ok := queryInt(c, "limit")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
offset, ok := queryInt(c, "offset")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := h.svc.List(c.Request.Context(), currentUserID(c), kind,
|
||||
feedback.Page{Limit: clampInt(limit), Offset: clampInt(offset)})
|
||||
if err != nil {
|
||||
writeFeedbackError(c, err)
|
||||
return
|
||||
}
|
||||
out := markedPageJSON{Items: make([]markedJSON, len(page.Items)), Total: page.Total, Limit: page.Limit, Offset: page.Offset}
|
||||
for i, e := range page.Items {
|
||||
out.Items[i] = markedJSON{
|
||||
Artist: artistRefJSON{ID: e.Artist.ID, Name: e.Artist.Name}, InLibrary: e.InLibrary, MarkedAt: *rfc3339(e.At),
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, out)
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/catalog"
|
||||
"codeberg.org/squidd/sonda/internal/discover"
|
||||
"codeberg.org/squidd/sonda/internal/discovery"
|
||||
"codeberg.org/squidd/sonda/internal/feedback"
|
||||
"codeberg.org/squidd/sonda/internal/library"
|
||||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
|
|
@ -77,6 +78,12 @@ type discoverService interface {
|
|||
Discover(ctx context.Context, userID int64, q discover.Query) (discover.Result, error)
|
||||
}
|
||||
|
||||
type feedbackService interface {
|
||||
Add(ctx context.Context, userID, artistID int64, kind feedback.Kind) error
|
||||
Remove(ctx context.Context, userID, artistID int64, kind feedback.Kind) error
|
||||
List(ctx context.Context, userID int64, kind feedback.Kind, p feedback.Page) (feedback.Listing, error)
|
||||
}
|
||||
|
||||
type tasteService interface {
|
||||
Taste(ctx context.Context, userID int64, limit int) (taste.Profile, error)
|
||||
}
|
||||
|
|
@ -116,6 +123,7 @@ type Deps struct {
|
|||
Tags tagService
|
||||
Taste tasteService
|
||||
Discover discoverService
|
||||
Feedback feedbackService
|
||||
Settings userSettingsService
|
||||
Streams streamService
|
||||
}
|
||||
|
|
@ -149,6 +157,13 @@ func NewRouter(d Deps) *gin.Engine {
|
|||
protected.GET("/me/taste", ts.taste)
|
||||
dc := &discoverHandlers{svc: d.Discover}
|
||||
protected.GET("/discover", dc.discover)
|
||||
fb := &feedbackHandlers{svc: d.Feedback}
|
||||
protected.GET("/discover/dismissed", fb.list(feedback.Dismissed))
|
||||
protected.POST("/discover/:id/dismiss", fb.add(feedback.Dismissed))
|
||||
protected.DELETE("/discover/:id/dismiss", fb.remove(feedback.Dismissed))
|
||||
protected.GET("/wishlist", fb.list(feedback.Wishlist))
|
||||
protected.PUT("/wishlist/:id", fb.add(feedback.Wishlist))
|
||||
protected.DELETE("/wishlist/:id", fb.remove(feedback.Wishlist))
|
||||
us := &userSettingsHandlers{svc: d.Settings}
|
||||
protected.GET("/me/settings", us.get)
|
||||
protected.PATCH("/me/settings", us.update)
|
||||
|
|
|
|||
|
|
@ -722,6 +722,130 @@ paths:
|
|||
schema: {$ref: '#/components/schemas/Error'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/discover/dismissed:
|
||||
get:
|
||||
tags: [Discovery]
|
||||
operationId: listDismissed
|
||||
summary: Artists you are not interested in
|
||||
description: |
|
||||
The artists you told Discover you are not interested in (`POST /discover/{id}/dismiss`),
|
||||
newest first. Only yours; nobody else's choices are visible or affect your Discover.
|
||||
security: [{bearerAuth: []}]
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Limit'}
|
||||
- {$ref: '#/components/parameters/Offset'}
|
||||
responses:
|
||||
'200':
|
||||
description: A page of artists.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/MarkedPage'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/discover/{id}/dismiss:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
post:
|
||||
tags: [Discovery]
|
||||
operationId: dismissArtist
|
||||
summary: Not interested in this artist
|
||||
description: |
|
||||
Hides the artist from your Discover: it is never suggested again, and an artist taken
|
||||
from your listening that you dismissed no longer seeds your suggestions. An artist is
|
||||
dismissed or on your wishlist, never both: dismissing one that is on the wishlist takes
|
||||
it off. Doing it twice is the same as once. Nothing is sent to anyone.
|
||||
|
||||
The artist can be one of yours or one a provider named. An artist that a provider named
|
||||
and nobody else does is kept for as long as it is dismissed, so the choice survives a
|
||||
new list from the provider; when an admin takes a provider's data back (`DELETE
|
||||
/providers/{name}/data`), what only that provider named goes, your choice with it.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: "Done, also when it was already dismissed."}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404':
|
||||
description: No such artist (`artist_not_found`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
delete:
|
||||
tags: [Discovery]
|
||||
operationId: undismissArtist
|
||||
summary: Interested again
|
||||
description: Lets Discover suggest the artist again. A wish on the wishlist is not touched.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: "Done, also when it was not dismissed."}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404':
|
||||
description: No such artist (`artist_not_found`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
|
||||
/wishlist:
|
||||
get:
|
||||
tags: [Discovery]
|
||||
operationId: listWishlist
|
||||
summary: Your wishlist
|
||||
description: |
|
||||
The artists you want, newest first. It is a list for you: nothing is downloaded or
|
||||
sent anywhere. `in_library` says whether there is music of the artist to play now, so a
|
||||
client can hide the ones you already got. Only yours.
|
||||
security: [{bearerAuth: []}]
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Limit'}
|
||||
- {$ref: '#/components/parameters/Offset'}
|
||||
responses:
|
||||
'200':
|
||||
description: A page of artists.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/MarkedPage'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/wishlist/{id}:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
put:
|
||||
tags: [Discovery]
|
||||
operationId: addToWishlist
|
||||
summary: Put an artist on your wishlist
|
||||
description: |
|
||||
Adds the artist. An artist is on the wishlist or dismissed, never both: adding one you
|
||||
dismissed lifts the dismissal. Doing it twice keeps the first date. The artist can be one
|
||||
of yours or one a provider named; the latter is kept while it is on a wishlist (see
|
||||
`POST /discover/{id}/dismiss` for what a purge does). Discover marks it `wishlisted`.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: "Done, also when it was already there."}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404':
|
||||
description: No such artist (`artist_not_found`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
delete:
|
||||
tags: [Discovery]
|
||||
operationId: removeFromWishlist
|
||||
summary: Take an artist off your wishlist
|
||||
description: A dismissal of the artist is not touched.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: "Done, also when it was not there."}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404':
|
||||
description: No such artist (`artist_not_found`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
|
||||
/me/taste:
|
||||
get:
|
||||
tags: [Taste]
|
||||
|
|
@ -1742,6 +1866,27 @@ components:
|
|||
type: array
|
||||
items: {$ref: '#/components/schemas/Suggestion'}
|
||||
|
||||
MarkedPage:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [items, total, limit, offset]
|
||||
properties:
|
||||
items:
|
||||
type: array
|
||||
items: {$ref: '#/components/schemas/MarkedArtist'}
|
||||
total: {type: integer, description: "Size of the whole list, not of this page."}
|
||||
limit: {type: integer}
|
||||
offset: {type: integer}
|
||||
|
||||
MarkedArtist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [artist, in_library, marked_at]
|
||||
properties:
|
||||
artist: {$ref: '#/components/schemas/ArtistRef'}
|
||||
in_library: {type: boolean, description: Whether there is music of the artist to play.}
|
||||
marked_at: {type: string, format: date-time}
|
||||
|
||||
DiscoverSeed:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
|
|
@ -1754,10 +1899,11 @@ components:
|
|||
Suggestion:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [artist, in_library, score, components, because]
|
||||
required: [artist, in_library, wishlisted, score, components, because]
|
||||
properties:
|
||||
artist: {$ref: '#/components/schemas/ArtistRef'}
|
||||
in_library: {type: boolean}
|
||||
wishlisted: {type: boolean, description: True when the artist is on your wishlist.}
|
||||
score: {type: number, minimum: 0, maximum: 1}
|
||||
components: {$ref: '#/components/schemas/ScoreParts'}
|
||||
because:
|
||||
|
|
|
|||
Loading…
Reference in a new issue