From a6dbf60ac87c864d8c4e97a8d407dc0540fc5ede Mon Sep 17 00:00:00 2001 From: tgiachi Date: Wed, 11 Nov 2026 20:11:18 +0100 Subject: [PATCH] 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 --- README.md | 6 +- cmd/sondad/main.go | 5 +- internal/api/http/api_test.go | 12 +- internal/api/http/contract_test.go | 21 ++ internal/api/http/discover_test.go | 7 +- internal/api/http/feedback_test.go | 189 +++++++++++++++ internal/api/http/handlers_discover.go | 3 +- internal/api/http/handlers_feedback.go | 97 ++++++++ internal/api/http/router.go | 15 ++ internal/api/openapi/openapi.yaml | 148 +++++++++++- internal/discover/service.go | 36 ++- internal/discover/service_test.go | 75 +++++- internal/feedback/feedback.go | 142 ++++++++++++ internal/feedback/feedback_test.go | 134 +++++++++++ internal/storage/discovery.go | 20 +- internal/storage/feedback.go | 134 +++++++++++ internal/storage/feedback_test.go | 231 +++++++++++++++++++ internal/storage/storage_test.go | 6 +- migrations/postgres/0012_artist_feedback.sql | 17 ++ migrations/sqlite/0012_artist_feedback.sql | 17 ++ 20 files changed, 1283 insertions(+), 32 deletions(-) create mode 100644 internal/api/http/feedback_test.go create mode 100644 internal/api/http/handlers_feedback.go create mode 100644 internal/feedback/feedback.go create mode 100644 internal/feedback/feedback_test.go create mode 100644 internal/storage/feedback.go create mode 100644 internal/storage/feedback_test.go create mode 100644 migrations/postgres/0012_artist_feedback.sql create mode 100644 migrations/sqlite/0012_artist_feedback.sql diff --git a/README.md b/README.md index 66372df..c1b5f15 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Listening history.** Clients report explicit `play`, `complete` and `skip` events. An event counts as a listen when it is `complete`, or when it was played for at least 20 seconds (`listening.min_listen_seconds`); the rule is applied when the history is read, so changing it re-reads the past. You can read your own history, with a `counted` flag on every event. It stays in your database and is never sent anywhere. - **Your taste.** `GET /me/taste` shows what Sonda has learned from your own listening: the artists you like and the kinds of music (tags) they have in common. A complete listen counts for more than a short one, a skip counts against the artist, and older listens weigh less (they halve every 90 days, `taste.half_life_days`; only the last ten half-lives are read). It is worked out on every request from data already in your database; nothing is sent to any provider. - **Discover.** `GET /discover` suggests artists you may like, in your library or not, each with a score from 0 to 1 and the parts it is made of: how close it is to the artists you like (affinity), how close its tags are to yours (genre), what your listening says about it (preference) and a bonus for what you have not played (discovery). The weights are in `[scoring]` in the config. A part with no data is left out and the rest rescaled, and every answer says which seeds led to each suggestion. It starts from the artists you name with `?seed=`, or, only if you turned on `discover_from_listening`, from the artists you play most. It needs Last.fm to be switched on to ask for anything new: when Last.fm is off and a seed's similar artists are not stored yet, the answer says `disabled` (what was stored earlier is still used). See [External providers](#external-providers). +- **Wishlist and "not interested".** `PUT /wishlist/{artist}` puts an artist on your wishlist (`GET /wishlist` lists it, `DELETE` takes it off), and `POST /discover/{artist}/dismiss` hides an artist from your Discover for good (`DELETE` brings it back, `GET /discover/dismissed` lists them). An artist is wished or dismissed, never both, and both lists are yours alone. Discover marks the wished artists it shows (`wishlisted`) and never suggests a dismissed one. Nothing is downloaded or sent anywhere: the wishlist is a list for you, and for a download client you may set up later. An artist that only Last.fm named is kept while you have it marked, and goes with the rest of Last.fm's data if an admin removes it. - **Your own settings.** `GET` and `PATCH /me/settings` hold choices that belong to you, not to the server. Everyone starts private: `discover_from_listening` is off, so this setting never sends your listening to a provider. When you turn it on, Discover may start from the artists you listen to most, and their names are sent to Last.fm (only if an admin switched it on). What Last.fm answers is kept in the shared database like any other lookup, so another local user can see, from `retrieved_at` of `GET /artists/{id}/similar`, that someone looked up an artist, but not who. - **Tags.** `GET /artists/{id}/tags` shows what Last.fm and MusicBrainz say kind of music an artist plays, each with its own weights, and the genres of the artist's own files, read offline. See [External providers](#external-providers). - **Similar artists.** With Last.fm switched on, `GET /artists/{id}/similar` lists artists similar to an artist, and says which of them you already have. See [External providers](#external-providers). Without it, Sonda makes no outbound network requests at all. @@ -209,7 +210,7 @@ The key is stored in the database, in clear text, and is never returned by the A **MusicBrainz ids come from your files, never from a guess.** If your files carry MusicBrainz artist ids in their tags (taggers like Picard write them), the scan reads them, offline, and keeps them on your artists, but only when each id can be paired with its artist for certain. MusicBrainz is asked only about artists whose id is known; for the others its entry says `no_id`. Sonda never searches MusicBrainz by name, because a search returns look-alikes and picking one would be a guess. A normal scan reads a file's tags when the file is new or changed. To pick up ids and genres for music that is already indexed, scan with `full=true` (`POST /library/scan?full=true`, or `POST /libraries/{id}/scan?full=true`): every file is read again, keeps its identity, and your listening history is untouched. -**Taking it back.** `DELETE /api/v1/providers/{name}/data` (admin) removes everything a provider gave: its similar artists, its tags and the records of when it was asked, and then the artists that only that data named and that you have no files for. Last.fm's terms ask for this when an agreement ends. Your own artists, music and listens stay, and the provider is not switched off. An artist that no list names any more is also removed when the list that named it is replaced, so they do not pile up. +**Taking it back.** `DELETE /api/v1/providers/{name}/data` (admin) removes everything a provider gave: its similar artists, its tags and the records of when it was asked, and then the artists that only that data named and that you have no files for. Last.fm's terms ask for this when an agreement ends. Your own artists, music and listens stay, and the provider is not switched off. An artist that only that provider named goes too, even if someone had it on their wishlist or dismissed. An artist that no list names any more is also removed when the list that named it is replaced, so they do not pile up. The artist is sent to Last.fm by its MusicBrainz id when your files gave one (if Last.fm does not know the id, by name), otherwise by its name exactly as it is written in your library, and Sonda never lets Last.fm correct it into another artist. Names and ids from Last.fm are never written onto your own artists. @@ -232,6 +233,8 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | signed in | `POST /listens` | Record a `play`, `complete` or `skip`; the answer says whether it counts as a listen. | | signed in | `GET /listens` | Your own history, newest first, with `counted` per event. Query: `limit`, `offset`, `counted=true`. | | signed in | `GET /discover` | Artists you may like, with a score and its parts. Query: `seed` (an artist id, up to 5 times), `limit` (default 20, max 30), `in_library=true\|false`. | +| signed in | `GET /wishlist`, `PUT /wishlist/{id}`, `DELETE /wishlist/{id}` | Your wishlist: list it (query `limit`, `offset`), add an artist, take one off. | +| signed in | `GET /discover/dismissed`, `POST /discover/{id}/dismiss`, `DELETE /discover/{id}/dismiss` | The artists you are not interested in: list them, dismiss one, bring it back. | | signed in | `GET /me/taste` | The artists and tags Sonda learned you like, from your own listening. Query: `limit` (default 20, max 50). | | signed in | `GET /me/settings`, `PATCH /me/settings` | Your own settings (today: `discover_from_listening`, off by default). | | signed in | `GET /library/scan` | Scan status and the last result. | @@ -289,6 +292,7 @@ internal/providers/ external providers: settings, the outbound HTTP client, t internal/listening/ listen events internal/taste/ what the listener likes, learned from the listening history internal/discover/ the score of an artist you may like, and the suggestions +internal/feedback/ the wishlist and the artists a user is not interested in internal/usersettings/ the settings that belong to one user internal/storage/ SQLite and PostgreSQL through bun, the repositories internal/streaming/ opening a track's file safely diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index 637925d..e9bc48f 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -20,6 +20,7 @@ import ( "codeberg.org/squidd/sonda/internal/config" "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" @@ -143,6 +144,7 @@ func run() error { tasteRepo := storage.NewTasteRepo(db) tasteService := taste.NewService(tasteRepo, taste.Options{MinListen: cfg.Listening.MinListen(), HalfLife: cfg.Taste.HalfLife()}) userSettings := usersettings.NewService(storage.NewUserSettingsRepo(db)) + feedbacks := feedback.NewService(storage.NewFeedbackRepo(db)) weights := discover.Weights{ Affinity: cfg.Scoring.WeightAffinity, Genre: cfg.Scoring.WeightGenre, Preference: cfg.Scoring.WeightPreference, Discovery: cfg.Scoring.WeightDiscovery, @@ -160,7 +162,8 @@ func run() error { Discovery: discoveries, Tags: tags, Taste: tasteService, - Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, weights), + Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights), + Feedback: feedbacks, Settings: userSettings, Streams: streaming.NewService(libraryRepo), }) diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 4238c25..1cafac1 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -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 } diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index 2c65401..316ad3f 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -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) diff --git a/internal/api/http/discover_test.go b/internal/api/http/discover_test.go index 512a98f..39f7602 100644 --- a/internal/api/http/discover_test.go +++ b/internal/api/http/discover_test.go @@ -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"]) diff --git a/internal/api/http/feedback_test.go b/internal/api/http/feedback_test.go new file mode 100644 index 0000000..f3f7322 --- /dev/null +++ b/internal/api/http/feedback_test.go @@ -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) + } +} diff --git a/internal/api/http/handlers_discover.go b/internal/api/http/handlers_discover.go index 3d8a941..a8aace2 100644 --- a/internal/api/http/handlers_discover.go +++ b/internal/api/http/handlers_discover.go @@ -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)), } diff --git a/internal/api/http/handlers_feedback.go b/internal/api/http/handlers_feedback.go new file mode 100644 index 0000000..1b3eeda --- /dev/null +++ b/internal/api/http/handlers_feedback.go @@ -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) + } +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go index feadb6b..6ddf5ae 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -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) diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 15cce76..dad54d0 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -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: diff --git a/internal/discover/service.go b/internal/discover/service.go index 324786e..2efedeb 100644 --- a/internal/discover/service.go +++ b/internal/discover/service.go @@ -8,6 +8,7 @@ import ( "sync" "codeberg.org/squidd/sonda/internal/discovery" + "codeberg.org/squidd/sonda/internal/feedback" "codeberg.org/squidd/sonda/internal/providers" "codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/usersettings" @@ -70,6 +71,11 @@ type Settings interface { Get(ctx context.Context, userID int64) (usersettings.Settings, error) } +// Marks reads what the listener decided about artists. +type Marks interface { + Marks(ctx context.Context, userID int64) (map[int64]feedback.Kind, error) +} + // Query is a request for suggestions. type Query struct { Seeds []int64 // artists the listener names; none means "from my listening" @@ -94,6 +100,7 @@ type Reason struct { type Item struct { Artist discovery.ArtistRef InLibrary bool + Wishlisted bool // the listener put it on their wishlist Score float64 Components Components Because []Reason @@ -116,12 +123,13 @@ type Service struct { tags TagFetcher store Store settings Settings + marks Marks weights Weights } // NewService builds a Service. -func NewService(t Taster, sim Similar, tags TagFetcher, store Store, settings Settings, w Weights) *Service { - return &Service{taste: t, similar: sim, tags: tags, store: store, settings: settings, weights: w} +func NewService(t Taster, sim Similar, tags TagFetcher, store Store, settings Settings, marks Marks, w Weights) *Service { + return &Service{taste: t, similar: sim, tags: tags, store: store, settings: settings, marks: marks, weights: w} } // Discover suggests artists. Seeds are the artists the listener names, or, when none @@ -153,15 +161,22 @@ func (s *Service) Discover(ctx context.Context, userID int64, q Query) (Result, res.FromListening = true } + marks, err := s.marks.Marks(ctx, userID) + if err != nil { + return Result{}, fmt.Errorf("read marks: %w", err) + } learned, err := s.taste.Learn(ctx, userID) if err != nil { return Result{}, err } if res.FromListening { - for i, p := range learned.Liked { - if i == MaxListeningSeeds { + for _, p := range learned.Liked { + if len(seeds) == MaxListeningSeeds { break } + if marks[p.ArtistID] == feedback.Dismissed { + continue // the listener said they are not interested + } seeds = append(seeds, Seed{ID: p.ArtistID, Weight: p.Preference}) } if err := s.nameSeeds(ctx, seeds); err != nil { @@ -180,7 +195,7 @@ func (s *Service) Discover(ctx context.Context, userID int64, q Query) (Result, seeds, lists = withoutUnread(seeds, lists) res.Seeds = append(res.Seeds, seeds...) - cands := gather(seeds, lists, q.InLibrary) + cands := gather(seeds, lists, q.InLibrary, marks) pool := rankByAffinity(cands, max(limit, minPool)) tags, err := s.candidateTags(ctx, pool, len(learned.Tags) > 0) if err != nil { @@ -203,7 +218,8 @@ func (s *Service) Discover(ctx context.Context, userID int64, q Query) (Result, } res.Items = append(res.Items, Item{ Artist: discovery.ArtistRef{ID: c.id, Name: c.name}, InLibrary: c.inLibrary, - Score: Combine(comp, s.weights), Components: comp, Because: because(c.links, names), + Wishlisted: marks[c.id] == feedback.Wishlist, + Score: Combine(comp, s.weights), Components: comp, Because: because(c.links, names), }) } sort.SliceStable(res.Items, func(i, j int) bool { @@ -362,9 +378,9 @@ type candidate struct { links []Link } -// gather turns the similar lists into candidates. A seed is never suggested, and -// the library filter is applied here. -func gather(seeds []Seed, lists []*discovery.Result, inLibrary *bool) []*candidate { +// gather turns the similar lists into candidates. A seed is never suggested, nor +// an artist the listener dismissed, and the library filter is applied here. +func gather(seeds []Seed, lists []*discovery.Result, inLibrary *bool, marks map[int64]feedback.Kind) []*candidate { isSeed := map[int64]bool{} for _, sd := range seeds { isSeed[sd.ID] = true @@ -373,7 +389,7 @@ func gather(seeds []Seed, lists []*discovery.Result, inLibrary *bool) []*candida var order []*candidate for i, sd := range seeds { for _, it := range lists[i].Items { - if isSeed[it.Artist.ID] || (inLibrary != nil && it.InLibrary != *inLibrary) { + if isSeed[it.Artist.ID] || marks[it.Artist.ID] == feedback.Dismissed || (inLibrary != nil && it.InLibrary != *inLibrary) { continue } c := byID[it.Artist.ID] diff --git a/internal/discover/service_test.go b/internal/discover/service_test.go index c59a8cf..f36324f 100644 --- a/internal/discover/service_test.go +++ b/internal/discover/service_test.go @@ -9,6 +9,7 @@ import ( "time" "codeberg.org/squidd/sonda/internal/discovery" + "codeberg.org/squidd/sonda/internal/feedback" "codeberg.org/squidd/sonda/internal/providers" "codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/usersettings" @@ -146,7 +147,19 @@ func (f fakeSettings) Get(context.Context, int64) (usersettings.Settings, error) return usersettings.Settings{DiscoverFromListening: f.on}, f.err } +type fakeMarks struct { + marks map[int64]feedback.Kind + err error + user int64 +} + +func (f *fakeMarks) Marks(_ context.Context, userID int64) (map[int64]feedback.Kind, error) { + f.user = userID + return f.marks, f.err +} + type rig struct { + marks *fakeMarks svc *Service taste *fakeTaste similar *fakeSimilar @@ -176,10 +189,11 @@ func newRig() *rig { names: map[int64]string{1: "Seed One", 2: "Seed Two", 10: "Ten", 11: "Eleven", 12: "Twelve", 13: "Thirteen"}, tags: map[int64]map[string]float64{10: {"rock": 1}, 11: {"jazz": 1}}, }, - set: &fakeSettings{}, + set: &fakeSettings{}, + marks: &fakeMarks{marks: map[int64]feedback.Kind{}}, } r.fetch = &fakeFetcher{store: r.store, gives: map[int64]map[string]float64{13: {"rock": 0.5, "pop": 1}}} - r.svc = NewService(r.taste, r.similar, r.fetch, r.store, r.set, DefaultWeights()) + r.svc = NewService(r.taste, r.similar, r.fetch, r.store, r.set, r.marks, DefaultWeights()) return r } @@ -655,7 +669,7 @@ func TestTagsAreFetchedFourAtATimeAndStopWhenTheListenerLeaves(t *testing.T) { } r.similar.lists[1] = discovery.Result{Items: items} slow := &slowFetcher{delay: 20 * time.Millisecond} - r.svc = NewService(r.taste, r.similar, slow, r.store, r.set, DefaultWeights()) + r.svc = NewService(r.taste, r.similar, slow, r.store, r.set, r.marks, DefaultWeights()) if _, err := r.svc.Discover(context.Background(), 7, Query{Seeds: []int64{1}}); err != nil { t.Fatal(err) } @@ -664,7 +678,7 @@ func TestTagsAreFetchedFourAtATimeAndStopWhenTheListenerLeaves(t *testing.T) { } slow = &slowFetcher{delay: time.Second} - r.svc = NewService(r.taste, r.similar, slow, r.store, r.set, DefaultWeights()) + r.svc = NewService(r.taste, r.similar, slow, r.store, r.set, r.marks, DefaultWeights()) ctx, cancel := context.WithTimeout(context.Background(), 40*time.Millisecond) defer cancel() start := time.Now() @@ -695,3 +709,56 @@ func (f *slowFetcher) ProviderTags(ctx context.Context, id int64, provider strin } return discovery.TagSourceResult{Provider: provider, Status: discovery.StatusOK}, nil } + +func TestADismissedArtistIsNeverSuggestedAndAWishedOneIsMarked(t *testing.T) { + r := newRig() + r.marks.marks = map[int64]feedback.Kind{10: feedback.Dismissed, 11: feedback.Wishlist} + res, err := r.svc.Discover(context.Background(), 7, Query{Seeds: []int64{1}}) + if err != nil { + t.Fatal(err) + } + if r.marks.user != 7 { + t.Errorf("marks of user %d were read", r.marks.user) + } + for _, it := range res.Items { + if it.Artist.ID == 10 { + t.Error("a dismissed artist was suggested") + } + } + if !find(t, res, 11).Wishlisted || find(t, res, 12).Wishlisted { + t.Error("only the artist on the wishlist must be marked as wished") + } + // A dismissed artist does not use up a place: the others still fill the answer. + if len(res.Items) != 3 { + t.Errorf("items = %v, want 11, 12 and 2", ids(res.Items)) + } +} + +func TestADismissedArtistIsNotASeedFromTheListeningButANamedOneIs(t *testing.T) { + r := newRig() + r.set.on = true + r.marks.marks = map[int64]feedback.Kind{1: feedback.Dismissed} + res, err := r.svc.Discover(context.Background(), 7, Query{}) + if err != nil { + t.Fatal(err) + } + for _, sd := range res.Seeds { + if sd.ID == 1 { + t.Error("an artist the listener is not interested in seeded the suggestions") + } + } + // Named by the listener, it is a seed: they asked for it. + res, err = r.svc.Discover(context.Background(), 7, Query{Seeds: []int64{1}}) + if err != nil || len(res.Seeds) != 1 || res.Seeds[0].ID != 1 { + t.Errorf("named seed: %+v err=%v", res.Seeds, err) + } +} + +func TestMarksErrorsPass(t *testing.T) { + r := newRig() + boom := errors.New("boom") + r.marks.err = boom + if _, err := r.svc.Discover(context.Background(), 7, Query{Seeds: []int64{1}}); !errors.Is(err, boom) { + t.Errorf("err = %v", err) + } +} diff --git a/internal/feedback/feedback.go b/internal/feedback/feedback.go new file mode 100644 index 0000000..d328bce --- /dev/null +++ b/internal/feedback/feedback.go @@ -0,0 +1,142 @@ +// Package feedback keeps what a user decided about an artist: they want it (the +// wishlist) or they are not interested (dismissed). Both are theirs alone. +// +// An artist is on the wishlist or dismissed, never both: marking it again with the +// other kind replaces the first. Nothing here contacts anyone and nothing is +// downloaded; the wishlist is only a list for the user, and for a download client +// they may set up later. +package feedback + +import ( + "context" + "errors" + "fmt" + "time" +) + +// Kind is what the user decided. +type Kind string + +// The two decisions. +const ( + Wishlist Kind = "wishlist" + Dismissed Kind = "dismissed" +) + +// Paging of the lists. +const ( + DefaultLimit = 50 + MaxLimit = 200 +) + +// Errors returned by the service and the store. +var ( + ErrArtistNotFound = errors.New("artist not found") + ErrUserNotFound = errors.New("user not found") // the account was deleted + ErrInvalidKind = errors.New("invalid kind") + ErrInvalidQuery = errors.New("invalid query") +) + +// ArtistRef is the short form of an artist. +type ArtistRef struct { + ID int64 + Name string +} + +// Entry is one marked artist. InLibrary says whether there is music of it to play. +type Entry struct { + Artist ArtistRef + InLibrary bool + At time.Time // when it was marked +} + +// Page is one page of a list and the size of the whole list. +type Page struct { + Limit int + Offset int +} + +// Listing is a page of entries. +type Listing struct { + Items []Entry + Total int + Limit int + Offset int +} + +// Store keeps the decisions. +type Store interface { + // Mark records the decision, replacing the other kind, and keeps the first date + // when the same kind is marked again. It returns ErrArtistNotFound for an artist + // that cannot be seen (no music to play and not named by a provider) and + // ErrUserNotFound for an account that is gone. + Mark(ctx context.Context, userID, artistID int64, kind Kind, at time.Time) error + // Unmark removes the decision of that kind; there is nothing to do when there is + // none. It returns ErrArtistNotFound for an artist that does not exist. + Unmark(ctx context.Context, userID, artistID int64, kind Kind) error + // List returns the user's entries of one kind, newest first, and how many there are. + List(ctx context.Context, userID int64, kind Kind, limit, offset int) ([]Entry, int, error) + // Marks returns every decision of the user, by artist. + Marks(ctx context.Context, userID int64) (map[int64]Kind, error) +} + +// Service validates and records decisions. +type Service struct{ store Store } + +// NewService builds a Service. +func NewService(store Store) *Service { return &Service{store: store} } + +func validKind(k Kind) error { + if k != Wishlist && k != Dismissed { + return fmt.Errorf("%w: %q", ErrInvalidKind, string(k)) + } + return nil +} + +// Add marks an artist. +func (s *Service) Add(ctx context.Context, userID, artistID int64, kind Kind) error { + if err := validKind(kind); err != nil { + return err + } + if artistID <= 0 { + return ErrArtistNotFound + } + return s.store.Mark(ctx, userID, artistID, kind, time.Now()) +} + +// Remove takes the mark of that kind off an artist. +func (s *Service) Remove(ctx context.Context, userID, artistID int64, kind Kind) error { + if err := validKind(kind); err != nil { + return err + } + if artistID <= 0 { + return ErrArtistNotFound + } + return s.store.Unmark(ctx, userID, artistID, kind) +} + +// List returns a page of the user's artists of one kind. +func (s *Service) List(ctx context.Context, userID int64, kind Kind, p Page) (Listing, error) { + if err := validKind(kind); err != nil { + return Listing{}, err + } + if p.Limit < 0 || p.Offset < 0 { + return Listing{}, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalidQuery) + } + switch { + case p.Limit == 0: + p.Limit = DefaultLimit + case p.Limit > MaxLimit: + p.Limit = MaxLimit + } + items, total, err := s.store.List(ctx, userID, kind, p.Limit, p.Offset) + if err != nil { + return Listing{}, err + } + return Listing{Items: items, Total: total, Limit: p.Limit, Offset: p.Offset}, nil +} + +// Marks returns every decision of the user, by artist. +func (s *Service) Marks(ctx context.Context, userID int64) (map[int64]Kind, error) { + return s.store.Marks(ctx, userID) +} diff --git a/internal/feedback/feedback_test.go b/internal/feedback/feedback_test.go new file mode 100644 index 0000000..030351c --- /dev/null +++ b/internal/feedback/feedback_test.go @@ -0,0 +1,134 @@ +package feedback + +import ( + "context" + "errors" + "testing" + "time" +) + +type fakeStore struct { + marks map[[2]int64]Kind + known map[int64]bool + user map[int64]bool + listErr error + last struct{ limit, offset int } +} + +func newFake() *fakeStore { + return &fakeStore{marks: map[[2]int64]Kind{}, known: map[int64]bool{10: true, 11: true}, user: map[int64]bool{1: true, 2: true}} +} + +func (f *fakeStore) Mark(_ context.Context, u, a int64, k Kind, _ time.Time) error { + if !f.user[u] { + return ErrUserNotFound + } + if !f.known[a] { + return ErrArtistNotFound + } + f.marks[[2]int64{u, a}] = k + return nil +} + +func (f *fakeStore) Unmark(_ context.Context, u, a int64, k Kind) error { + if !f.known[a] { + return ErrArtistNotFound + } + if f.marks[[2]int64{u, a}] == k { + delete(f.marks, [2]int64{u, a}) + } + return nil +} + +func (f *fakeStore) List(_ context.Context, _ int64, _ Kind, limit, offset int) ([]Entry, int, error) { + f.last.limit, f.last.offset = limit, offset + return []Entry{}, 0, f.listErr +} + +func (f *fakeStore) Marks(context.Context, int64) (map[int64]Kind, error) { return nil, nil } + +func TestAddAndRemoveNeedARealArtist(t *testing.T) { + s := NewService(newFake()) + ctx := context.Background() + if err := s.Add(ctx, 1, 10, Wishlist); err != nil { + t.Fatalf("Add: %v", err) + } + if err := s.Add(ctx, 1, 99, Wishlist); !errors.Is(err, ErrArtistNotFound) { + t.Errorf("Add unknown: %v", err) + } + for _, id := range []int64{0, -3} { + if err := s.Add(ctx, 1, id, Wishlist); !errors.Is(err, ErrArtistNotFound) { + t.Errorf("Add %d: %v", id, err) + } + if err := s.Remove(ctx, 1, id, Wishlist); !errors.Is(err, ErrArtistNotFound) { + t.Errorf("Remove %d: %v", id, err) + } + } + if err := s.Remove(ctx, 1, 10, Wishlist); err != nil { + t.Errorf("Remove: %v", err) + } + if err := s.Remove(ctx, 1, 10, Wishlist); err != nil { + t.Errorf("Remove again must be fine: %v", err) + } + if err := s.Remove(ctx, 1, 99, Wishlist); !errors.Is(err, ErrArtistNotFound) { + t.Errorf("Remove unknown: %v", err) + } +} + +func TestOnlyTheTwoKindsExist(t *testing.T) { + s := NewService(newFake()) + if err := s.Add(context.Background(), 1, 10, Kind("favourite")); !errors.Is(err, ErrInvalidKind) { + t.Errorf("Add: %v", err) + } + if err := s.Remove(context.Background(), 1, 10, Kind("")); !errors.Is(err, ErrInvalidKind) { + t.Errorf("Remove: %v", err) + } + if _, err := s.List(context.Background(), 1, Kind("x"), Page{}); !errors.Is(err, ErrInvalidKind) { + t.Errorf("List: %v", err) + } +} + +func TestAWishlistIsPerUserAndAnAddReplacesTheOtherKind(t *testing.T) { + f := newFake() + s := NewService(f) + ctx := context.Background() + _ = s.Add(ctx, 1, 10, Dismissed) + _ = s.Add(ctx, 1, 10, Wishlist) + if f.marks[[2]int64{1, 10}] != Wishlist { + t.Error("the new kind did not replace the old one") + } + if _, ok := f.marks[[2]int64{2, 10}]; ok { + t.Error("another user got the mark") + } + if err := s.Add(ctx, 7, 10, Wishlist); !errors.Is(err, ErrUserNotFound) { + t.Errorf("a deleted account: %v", err) + } +} + +func TestListPaging(t *testing.T) { + f := newFake() + s := NewService(f) + ctx := context.Background() + for name, tt := range map[string]struct { + in Page + limit, off int + bad bool + }{ + "defaults": {Page{}, DefaultLimit, 0, false}, + "explicit": {Page{Limit: 5, Offset: 10}, 5, 10, false}, + "too big": {Page{Limit: 100000}, MaxLimit, 0, false}, + "neg limit": {Page{Limit: -1}, 0, 0, true}, + "neg offset": {Page{Offset: -1}, 0, 0, true}, + } { + page, err := s.List(ctx, 1, Wishlist, tt.in) + if tt.bad { + if !errors.Is(err, ErrInvalidQuery) { + t.Errorf("%s: err = %v", name, err) + } + continue + } + if err != nil || f.last.limit != tt.limit || f.last.offset != tt.off || page.Limit != tt.limit || page.Offset != tt.off { + t.Errorf("%s: asked %+v, page %+v, err %v", name, f.last, page, err) + } + } +} diff --git a/internal/storage/discovery.go b/internal/storage/discovery.go index a14ea01..5dd150a 100644 --- a/internal/storage/discovery.go +++ b/internal/storage/discovery.go @@ -45,9 +45,10 @@ func playableArtist(expr string) string { } // discoveredArtist is the condition "a provider named this artist as similar to -// another". Such an artist can have no files at all. +// another, or a user marked it". Such an artist can have no files at all. func discoveredArtist(expr string) string { - return "EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = " + expr + ")" + return "(EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = " + expr + ")" + + " OR EXISTS (SELECT 1 FROM artist_feedback f WHERE f.artist_id = " + expr + "))" } // Artist returns an artist that can be looked up: one with something playable, @@ -161,7 +162,7 @@ func (r *DiscoveryRepo) SaveSimilar(ctx context.Context, id int64, provider stri return fmt.Errorf("record fetch: %w", err) } if len(before) > 0 { - if _, err := removeUnnamedArtists(ctx, tx, "id IN (?) AND ", bun.In(before)); err != nil { + if _, err := removeUnnamedArtists(ctx, tx, true, "id IN (?) AND ", bun.In(before)); err != nil { return err } } @@ -174,18 +175,25 @@ func (r *DiscoveryRepo) SaveSimilar(ctx context.Context, id int64, provider stri // more goes, among all of them: removing an artist removes the list it was the // source of, which can leave the artists only that list named without anyone // naming them. Real artists (any that a file ever matched) are never touched. +// With keepMarked, neither are the artists some user marked (wishlist or not +// interested): they would come back as another row, with another id, the next +// time a provider names them. Taking a provider's data back does not keep them. // Tags and records of a removed artist go with it. The caller holds the artist // lock, so nothing is adopted or named while this runs. -func removeUnnamedArtists(ctx context.Context, tx bun.Tx, extra string, args ...any) (int, error) { +func removeUnnamedArtists(ctx context.Context, tx bun.Tx, keepMarked bool, extra string, args ...any) (int, error) { total := 0 for pass := 0; pass < maxCleanupPasses; pass++ { cond, condArgs := "", []any(nil) if pass == 0 { cond, condArgs = extra, args } + keep := "" + if keepMarked { + keep = " AND NOT EXISTS (SELECT 1 FROM artist_feedback f WHERE f.artist_id = artists.id)" + } res, err := tx.ExecContext(ctx, "DELETE FROM artists WHERE "+cond+`discovered = ? - AND NOT EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = artists.id)`, + AND NOT EXISTS (SELECT 1 FROM artist_similar s WHERE s.similar_id = artists.id)`+keep, append(condArgs, true)...) if err != nil { return total, fmt.Errorf("remove artists nobody names: %w", err) @@ -473,7 +481,7 @@ func (r *DiscoveryRepo) Purge(ctx context.Context, provider string) (providers.P n, _ := res.RowsAffected() *step.into = int(n) } - n, err := removeUnnamedArtists(ctx, tx, "") + n, err := removeUnnamedArtists(ctx, tx, false, "") out.Artists = n return err }) diff --git a/internal/storage/feedback.go b/internal/storage/feedback.go new file mode 100644 index 0000000..b9ed110 --- /dev/null +++ b/internal/storage/feedback.go @@ -0,0 +1,134 @@ +package storage + +import ( + "context" + "database/sql" + "errors" + "fmt" + "time" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/internal/feedback" +) + +// FeedbackRepo stores what users decided about artists. It implements feedback.Store. +type FeedbackRepo struct{ db *bun.DB } + +// NewFeedbackRepo builds a FeedbackRepo. +func NewFeedbackRepo(db *bun.DB) *FeedbackRepo { return &FeedbackRepo{db: db} } + +// Mark records the decision, replacing the other kind. The first date is kept when +// the same kind is marked again. +func (r *FeedbackRepo) Mark(ctx context.Context, userID, artistID int64, kind feedback.Kind, at time.Time) error { + return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + // Artists are removed by the cleanup of replaced lists: take its turn, or the + // artist could go, and the mark with it, a moment after it was seen here. + if err := lockArtistWrites(ctx, tx); err != nil { + return err + } + var one int + if err := tx.QueryRowContext(ctx, "SELECT 1 FROM users WHERE id = ?", userID).Scan(&one); err != nil { + return mapNoRows(err, feedback.ErrUserNotFound, "check user") + } + if err := tx.QueryRowContext(ctx, + "SELECT 1 FROM artists a WHERE a.id = ? AND ("+playableArtist("a.id")+" OR "+discoveredArtist("a.id")+")", + artistID).Scan(&one); err != nil { + return mapNoRows(err, feedback.ErrArtistNotFound, "check artist") + } + if _, err := tx.ExecContext(ctx, + `INSERT INTO artist_feedback (user_id, artist_id, kind, created_at) VALUES (?, ?, ?, ?) + ON CONFLICT (user_id, artist_id) DO UPDATE SET + created_at = CASE WHEN artist_feedback.kind = excluded.kind THEN artist_feedback.created_at ELSE excluded.created_at END, + kind = excluded.kind`, + userID, artistID, string(kind), at.Unix()); err != nil { + return fmt.Errorf("write feedback: %w", err) + } + return nil + }) +} + +// Unmark removes the decision of that kind. When it was the last mark of an artist +// that only a provider named and nobody names any more, the artist goes too: the +// marks were all that kept it. +func (r *FeedbackRepo) Unmark(ctx context.Context, userID, artistID int64, kind feedback.Kind) error { + return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { + if err := lockArtistWrites(ctx, tx); err != nil { + return err + } + var one int + if err := tx.QueryRowContext(ctx, "SELECT 1 FROM artists WHERE id = ?", artistID).Scan(&one); err != nil { + return mapNoRows(err, feedback.ErrArtistNotFound, "check artist") + } + if _, err := tx.ExecContext(ctx, + "DELETE FROM artist_feedback WHERE user_id = ? AND artist_id = ? AND kind = ?", userID, artistID, string(kind)); err != nil { + return fmt.Errorf("remove feedback: %w", err) + } + _, err := removeUnnamedArtists(ctx, tx, true, "id = ? AND ", artistID) + return err + }) +} + +// List returns the user's artists of one kind, newest first. +func (r *FeedbackRepo) List(ctx context.Context, userID int64, kind feedback.Kind, limit, offset int) ([]feedback.Entry, int, error) { + var total int + if err := r.db.QueryRowContext(ctx, + "SELECT COUNT(*) FROM artist_feedback WHERE user_id = ? AND kind = ?", userID, string(kind)).Scan(&total); err != nil { + return nil, 0, fmt.Errorf("count feedback: %w", err) + } + out := []feedback.Entry{} + if offset >= total { + return out, total, nil + } + rows, err := r.db.QueryContext(ctx, + `SELECT a.id, a.name, `+playableArtist("a.id")+`, f.created_at + FROM artist_feedback f JOIN artists a ON a.id = f.artist_id + WHERE f.user_id = ? AND f.kind = ? + ORDER BY f.created_at DESC, a.id LIMIT ? OFFSET ?`, + userID, string(kind), limit, offset) + if err != nil { + return nil, 0, fmt.Errorf("list feedback: %w", err) + } + defer rows.Close() + for rows.Next() { + var ( + e feedback.Entry + at int64 + ) + if err := rows.Scan(&e.Artist.ID, &e.Artist.Name, &e.InLibrary, &at); err != nil { + return nil, 0, err + } + e.At = time.Unix(at, 0) + out = append(out, e) + } + return out, total, rows.Err() +} + +// Marks returns every decision of the user, by artist. +func (r *FeedbackRepo) Marks(ctx context.Context, userID int64) (map[int64]feedback.Kind, error) { + rows, err := r.db.QueryContext(ctx, "SELECT artist_id, kind FROM artist_feedback WHERE user_id = ?", userID) + if err != nil { + return nil, fmt.Errorf("read feedback: %w", err) + } + defer rows.Close() + out := map[int64]feedback.Kind{} + for rows.Next() { + var ( + id int64 + kind string + ) + if err := rows.Scan(&id, &kind); err != nil { + return nil, err + } + out[id] = feedback.Kind(kind) + } + return out, rows.Err() +} + +// mapNoRows turns "no row" into the given error and wraps any other failure. +func mapNoRows(err, notFound error, what string) error { + if errors.Is(err, sql.ErrNoRows) { + return notFound + } + return fmt.Errorf("%s: %w", what, err) +} diff --git a/internal/storage/feedback_test.go b/internal/storage/feedback_test.go new file mode 100644 index 0000000..bbe48cb --- /dev/null +++ b/internal/storage/feedback_test.go @@ -0,0 +1,231 @@ +package storage + +import ( + "context" + "errors" + "strconv" + "testing" + "time" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/internal/feedback" + "codeberg.org/squidd/sonda/internal/providers/lastfm" +) + +type fbEnv struct { + db *bun.DB + repo *FeedbackRepo + alice int64 + bob int64 + played int64 // Radiohead, has files + found int64 // Staind, named by a provider, no files +} + +func newFbEnv(t *testing.T, db *bun.DB) *fbEnv { + t.Helper() + seedCatalog(t, db) + ctx := context.Background() + users := NewUserRepo(db) + a, _ := users.CreateUser(ctx, "alice", "h", false) + b, _ := users.CreateUser(ctx, "bob", "h", false) + e := &fbEnv{db: db, repo: NewFeedbackRepo(db), alice: a.ID, bob: b.ID, played: artistID(t, db, "Radiohead")} + if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, []lastfm.Similar{{Name: "Staind", Match: 1}, {Name: "Seether", Match: 0.5}}); err != nil { + t.Fatal(err) + } + e.found = artistID(t, db, "Staind") + return e +} + +func TestFeedbackMarkListAndUnmark(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + t0 := time.Unix(1_700_000_000, 0) + if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0); err != nil { + t.Fatal(err) + } + if err := e.repo.Mark(ctx, e.alice, e.played, feedback.Wishlist, t0.Add(time.Hour)); err != nil { + t.Fatal(err) + } + entries, total, err := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0) + if err != nil || total != 2 || len(entries) != 2 { + t.Fatalf("list = %+v total %d err %v", entries, total, err) + } + // Newest first, with the name and whether there is music. + if entries[0].Artist.ID != e.played || entries[0].Artist.Name != "Radiohead" || !entries[0].InLibrary || !entries[0].At.Equal(t0.Add(time.Hour)) { + t.Errorf("first = %+v", entries[0]) + } + if entries[1].Artist.ID != e.found || entries[1].InLibrary { + t.Errorf("second = %+v, want Staind without music", entries[1]) + } + // Paging. + if page, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 1, 1); len(page) != 1 || total != 2 || page[0].Artist.ID != e.found { + t.Errorf("page 2 = %+v total %d", page, total) + } + if page, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 5); len(page) != 0 || total != 2 { + t.Errorf("past the end = %+v total %d", page, total) + } + // Mine only. + if got, total, _ := e.repo.List(ctx, e.bob, feedback.Wishlist, 10, 0); len(got) != 0 || total != 0 || got == nil { + t.Errorf("bob's list = %+v (%d), want empty and not nil", got, total) + } + // Unmark removes only that kind. + if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Dismissed); err != nil { + t.Fatal(err) + } + if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 2 { + t.Errorf("removing a dismissal removed a wish: total %d", total) + } + if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { + t.Fatal(err) + } + if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { + t.Errorf("unmark again: %v", err) + } + if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 1 { + t.Errorf("total after unmark = %d", total) + } + }) +} + +func TestFeedbackAnArtistIsWishedOrDismissedNeverBoth(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + t0 := time.Unix(1_700_000_000, 0) + _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Dismissed, t0) + _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0.Add(time.Hour)) + marks, _ := e.repo.Marks(ctx, e.alice) + if len(marks) != 1 || marks[e.found] != feedback.Wishlist { + t.Errorf("marks = %v, want only a wish", marks) + } + // Marking again with the same kind keeps the first date. + _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0.Add(48*time.Hour)) + entries, _, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0) + if len(entries) != 1 || !entries[0].At.Equal(t0.Add(time.Hour)) { + t.Errorf("entries = %+v, want the date of the first wish", entries) + } + if got, _ := e.repo.Marks(ctx, e.bob); len(got) != 0 { + t.Errorf("bob's marks = %v", got) + } + }) +} + +func TestFeedbackNeedsAnArtistThatCanBeSeen(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + t0 := time.Unix(1_700_000_000, 0) + for name, id := range map[string]int64{"unknown": 99999, "files are gone": artistID(t, db, "Only Missing")} { + if err := e.repo.Mark(ctx, e.alice, id, feedback.Wishlist, t0); !errors.Is(err, feedback.ErrArtistNotFound) { + t.Errorf("%s: err = %v", name, err) + } + } + if err := e.repo.Unmark(ctx, e.alice, 99999, feedback.Wishlist); !errors.Is(err, feedback.ErrArtistNotFound) { + t.Errorf("unmark unknown: err = %v", err) + } + if err := e.repo.Mark(ctx, 99999, e.played, feedback.Wishlist, t0); !errors.Is(err, feedback.ErrUserNotFound) { + t.Errorf("unknown user: err = %v", err) + } + }) +} + +func TestFeedbackGoesWithTheAccountAndTheArtist(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + t0 := time.Unix(1_700_000_000, 0) + _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, t0) + _ = e.repo.Mark(ctx, e.bob, e.found, feedback.Dismissed, t0) + if err := NewUserRepo(db).DeleteUser(ctx, e.bob); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback WHERE user_id = "+itoa(e.bob)); n != 0 { + t.Errorf("rows left for a deleted user: %d", n) + } + if _, err := db.Exec("DELETE FROM artists WHERE id = ?", e.found); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback"); n != 0 { + t.Errorf("rows left for a deleted artist: %d", n) + } + }) +} + +// An artist I marked must not disappear when the list that named it is replaced: +// it would be a different row, with another id, if a provider names it again. +func TestFeedbackKeepsAnArtistWhenItsListIsReplaced(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + seether := artistID(t, db, "Seether") + if err := e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at); err != nil { + t.Fatal(err) + } + // The new list names nobody: Staind (marked) stays, Seether (not) goes. + if err := NewDiscoveryRepo(db).SaveSimilar(ctx, e.played, "lastfm", at, true, nil); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { + t.Errorf("the artist I wished for was removed") + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(seether)); n != 0 { + t.Errorf("an artist nobody marked or names stayed") + } + // It can still be seen and listed. + if _, err := NewCatalogRepo(db).GetArtist(ctx, e.found); err != nil { + t.Errorf("GetArtist: %v", err) + } + if _, total, _ := e.repo.List(ctx, e.alice, feedback.Wishlist, 10, 0); total != 1 { + t.Errorf("the wishlist has %d artists, want 1", total) + } + // Another user marking it as well keeps it when the first one lets go. + if err := e.repo.Mark(ctx, e.bob, e.found, feedback.Dismissed, at); err != nil { + t.Fatal(err) + } + if err := e.repo.Unmark(ctx, e.alice, e.found, feedback.Wishlist); err != nil { + t.Errorf("Unmark: %v", err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 1 { + t.Error("the artist went although another user still marks it") + } + // The last mark gone and nobody naming it: nothing keeps the artist. + if err := e.repo.Unmark(ctx, e.bob, e.found, feedback.Dismissed); err != nil { + t.Errorf("Unmark: %v", err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 0 { + t.Error("an artist nobody names or marks stayed") + } + // A real artist is never removed that way. + if err := e.repo.Mark(ctx, e.alice, e.played, feedback.Wishlist, at); err != nil { + t.Fatal(err) + } + if err := e.repo.Unmark(ctx, e.alice, e.played, feedback.Wishlist); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.played)); n != 1 { + t.Error("an artist with music was removed") + } + }) +} + +// Taking a provider's data back removes what only it named, wishes included. +func TestFeedbackDoesNotSurviveAPurgeOfWhatOnlyTheProviderNamed(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + e := newFbEnv(t, db) + ctx := context.Background() + _ = e.repo.Mark(ctx, e.alice, e.found, feedback.Wishlist, at) + if _, err := NewDiscoveryRepo(db).Purge(ctx, "lastfm"); err != nil { + t.Fatal(err) + } + if n := count(t, db, "SELECT COUNT(*) FROM artists WHERE id = "+itoa(e.found)); n != 0 { + t.Error("the purge left an artist only the provider named") + } + if n := count(t, db, "SELECT COUNT(*) FROM artist_feedback"); n != 0 { + t.Errorf("marks left for a purged artist: %d", n) + } + }) +} + +func itoa(n int64) string { return strconv.FormatInt(n, 10) } diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index a2e0bc5..9be6913 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -147,7 +147,7 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := Migrate(context.Background(), db, fsys); err != nil { t.Fatalf("second Migrate: %v", err) } - for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings"} { + for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback"} { if !tableExists(t, db, driver, table) { t.Errorf("table %s missing", table) } @@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil { t.Fatal(err) } - if applied != 11 { - t.Errorf("applied migrations = %d, want 11", applied) + if applied != 12 { + t.Errorf("applied migrations = %d, want 12", applied) } }) } diff --git a/migrations/postgres/0012_artist_feedback.sql b/migrations/postgres/0012_artist_feedback.sql new file mode 100644 index 0000000..d0fca60 --- /dev/null +++ b/migrations/postgres/0012_artist_feedback.sql @@ -0,0 +1,17 @@ +-- What a user decided about an artist: wanted it ('wishlist') or is not interested +-- ('dismissed'). One row per user and artist, so it is one or the other. +-- +-- The artist can be one without files that a provider named. The row keeps such an +-- artist from being cleaned up when the list that named it is replaced (see the +-- storage code); only taking a provider's data back removes it, and the row with it. + +CREATE TABLE artist_feedback ( + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + artist_id BIGINT NOT NULL REFERENCES artists(id) ON DELETE CASCADE, + kind TEXT NOT NULL CHECK (kind IN ('wishlist', 'dismissed')), + created_at BIGINT NOT NULL, -- unix seconds + PRIMARY KEY (user_id, artist_id) +); + +CREATE INDEX idx_artist_feedback_artist ON artist_feedback(artist_id); +CREATE INDEX idx_artist_feedback_list ON artist_feedback(user_id, kind, created_at); diff --git a/migrations/sqlite/0012_artist_feedback.sql b/migrations/sqlite/0012_artist_feedback.sql new file mode 100644 index 0000000..9d2d888 --- /dev/null +++ b/migrations/sqlite/0012_artist_feedback.sql @@ -0,0 +1,17 @@ +-- What a user decided about an artist: wanted it ('wishlist') or is not interested +-- ('dismissed'). One row per user and artist, so it is one or the other. +-- +-- The artist can be one without files that a provider named. The row keeps such an +-- artist from being cleaned up when the list that named it is replaced (see the +-- storage code); only taking a provider's data back removes it, and the row with it. + +CREATE TABLE artist_feedback ( + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + artist_id INTEGER NOT NULL REFERENCES artists(id) ON DELETE CASCADE, + kind TEXT NOT NULL CHECK (kind IN ('wishlist', 'dismissed')), + created_at INTEGER NOT NULL, -- unix seconds + PRIMARY KEY (user_id, artist_id) +); + +CREATE INDEX idx_artist_feedback_artist ON artist_feedback(artist_id); +CREATE INDEX idx_artist_feedback_list ON artist_feedback(user_id, kind, created_at);