From 8160c339606615226ec53213dbb0f54096215b7d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 11:28:15 +0100 Subject: [PATCH 1/2] feat(lidarr): send an artist to Lidarr and see whether it has it - POST /artists/{id}/lidarr (admin) adds the artist to the admin's own Lidarr by its MusicBrainz id, never by name, with the profiles, the folder and the monitor option the admin chose. It works for any artist that has an id, also one that Sonda only learned of from a list of similar artists. It can be repeated: an artist that Lidarr already has changes nothing. Two sends at once go one after the other. - GET /artists/{id}/lidarr (signed in) says whether Lidarr has the artist and monitors it; what stops the answer is a status (disabled, no_id, not_configured, error), as for the descriptions. - It needs the provider switched on and the profiles and folder chosen; a timeout says the artist may have been added anyway. - Spec, contract test and README, which also say that Sonda downloads nothing. --- README.md | 15 +- cmd/sondad/main.go | 1 + internal/api/http/api_test.go | 29 +-- internal/api/http/contract_test.go | 48 +++++ internal/api/http/handlers_lidarr.go | 77 ++++++++ internal/api/http/lidarr_test.go | 116 ++++++++++++ internal/api/http/router.go | 4 + internal/api/http/router_test.go | 2 +- internal/api/openapi/openapi.yaml | 94 ++++++++++ internal/providers/lidarr/service.go | 141 +++++++++++++++ internal/providers/lidarr/service_test.go | 210 ++++++++++++++++++++++ 11 files changed, 720 insertions(+), 17 deletions(-) create mode 100644 internal/api/http/handlers_lidarr.go create mode 100644 internal/providers/lidarr/service.go create mode 100644 internal/providers/lidarr/service_test.go diff --git a/README.md b/README.md index eea1290..9d4dc4c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Artist descriptions.** `GET /artists/{id}/description` shows what Wikipedia says about an artist: a short text, with the page it comes from, a link to it and the licence (CC BY-SA 4.0), which you must show next to it. With the `wikipedia` provider and the `musicbrainz` one switched on (the link to Wikidata comes from MusicBrainz), Sonda follows ids and never a name: the artist's MusicBrainz id, then the Wikidata item that MusicBrainz links to it, then the Wikipedia page that Wikidata names in the first of your languages (`[wikipedia] languages`, the code of that Wikipedia, for example `["it", "en"]`) that has one. An artist without a MusicBrainz id has no description. It is kept for 30 days and removed with the provider's data. To read the descriptions of the whole library in one go, an admin starts a background job like a scan: `POST /library/descriptions` (follow it with `GET /library/descriptions`, stop it with `DELETE /library/descriptions`; `?refresh=true` reads every artist again). It only goes through artists that have a MusicBrainz id, never replaces a description by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one switched on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin switched on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you switched on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). -- **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 (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch it. +- **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. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist you wished for to their own Lidarr (see [External providers](#external-providers)). An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch 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. @@ -213,11 +213,20 @@ curl -s -X PUT $BASE/providers/lidarr -H "$AUTH" -d '{"lidarr":{"quality_profile curl -s -X PUT $BASE/providers/lidarr -H "$AUTH" -d '{"enabled":true}' ``` +Then an admin sends an artist with one request, for any artist that has a MusicBrainz id, **including one that Sonda only learned of from Discover and that is not in your library**, which is the point: asking for music you do not have. Anyone signed in can see whether Lidarr has it: + +```sh +curl -s -X POST $BASE/artists//lidarr -H "$AUTH" | jq # {"status":"added"} or {"status":"already_added"} +curl -s $BASE/artists//lidarr -H "$AUTH" | jq # {"provider_status":"ok","in_lidarr":true,"monitored":true} +``` + +Sending is explicit, for one artist, never automatic, and can be repeated: an artist that Lidarr already has changes nothing. If Lidarr does not answer in time the artist may have been added all the same, so read it before sending again. + The address can have a path if Lidarr runs under a URL base, and it cannot carry a user, a password or a query: the key is sent in a header, never in the address, and Sonda never follows a redirect, so it cannot go anywhere else (if your `http://` address redirects to `https://`, set the `https://` one) and never goes through a proxy named by the environment, since Lidarr is on your own network. `lidarr` replaces all its options at once; what it leaves out is `monitor` `all` and `search_missing` `true`. The folder of an artist that Lidarr downloads shows up in Lidarr's root folder only with the first import: add that folder as a library of Sonda yourself, and a scan finds the new files. The key is stored in the database, in clear text, and is never returned by the API or written to the log: the list only says `has_api_key`. Because the database holds it, treat database backups like secrets. Removing the key turns the provider off, and each provider describes in the list exactly what it sends. -**What leaves your server, when a provider is on:** to Last.fm, the artist name (or MusicBrainz id) being looked up, your API key, your IP address and the `User-Agent`; to MusicBrainz, only the MusicBrainz id of the artist, your IP address and a `User-Agent` that carries your contact. Never your users, your listens, track titles or file paths. Requests only happen when someone asks for them, never as a bulk pass over your library (the exceptions are the three jobs that an admin starts on purpose, each of them one request that goes through the whole library, one item after the other, spaced out, and each stops when its providers keep failing: `POST /library/covers`, with the Cover Art Archive on, sends the release group id of every album that has no picture in its files; `POST /library/artist-images`, with Commons and MusicBrainz on, sends the MusicBrainz id of every artist that has one, to MusicBrainz, Wikidata and Commons; `POST /library/descriptions`, with Wikipedia and MusicBrainz on, sends it to MusicBrainz, Wikidata and Wikipedia), and they are spaced out, retried with backoff, and stopped by a circuit breaker if a provider is down. **Descriptions** (the Wikipedia provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata and the title of the page to Wikipedia, with your IP address and a `User-Agent` that carries a contact (the one set for each provider; Wikimedia refuses requests without one), and never a name. **Photos** (the Commons provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata, the name of the picture to Commons and the address of a copy to Wikimedia, with your IP address and a `User-Agent` with a contact, and never a name. **Covers** (the Cover Art Archive provider) send only the MusicBrainz release group id of an album, read from the tags of its own files and never a title, and only when the files have no picture; the redirects of the archive are followed only to archive.org, over https. Its covers have no licence of their own: Sonda keeps a copy for your library and never treats it as free. **Discover** asks Last.fm for the similar artists of each seed (the seed's name or MusicBrainz id) and, for the best suggestions that have no tags yet, for the tags of that suggestion (its name); never MusicBrainz. Seeds you name are sent when you ask. Seeds taken from your listening, which are the names of the artists you play most, are sent only if you turned on `discover_from_listening` yourself; otherwise Discover refuses to run without a `seed`. **Lidarr** (your own server, at the address an admin set) gets the MusicBrainz id of an artist an admin sends and the API key, nothing else; asking it what it offers and testing the key send only the key. Never a name, your users, your listens or your files. What Lidarr then does is up to its own indexers and download clients. Besides the three jobs, the other exception to "only when someone asks" is `POST /providers/{name}/test`, which an admin sends to check a key (or, for Lidarr, the address and the key); it sends nothing but the key. +**What leaves your server, when a provider is on:** to Last.fm, the artist name (or MusicBrainz id) being looked up, your API key, your IP address and the `User-Agent`; to MusicBrainz, only the MusicBrainz id of the artist, your IP address and a `User-Agent` that carries your contact. Never your users, your listens, track titles or file paths. Requests only happen when someone asks for them, never as a bulk pass over your library (the exceptions are the three jobs that an admin starts on purpose, each of them one request that goes through the whole library, one item after the other, spaced out, and each stops when its providers keep failing: `POST /library/covers`, with the Cover Art Archive on, sends the release group id of every album that has no picture in its files; `POST /library/artist-images`, with Commons and MusicBrainz on, sends the MusicBrainz id of every artist that has one, to MusicBrainz, Wikidata and Commons; `POST /library/descriptions`, with Wikipedia and MusicBrainz on, sends it to MusicBrainz, Wikidata and Wikipedia), and they are spaced out, retried with backoff, and stopped by a circuit breaker if a provider is down. **Descriptions** (the Wikipedia provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata and the title of the page to Wikipedia, with your IP address and a `User-Agent` that carries a contact (the one set for each provider; Wikimedia refuses requests without one), and never a name. **Photos** (the Commons provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata, the name of the picture to Commons and the address of a copy to Wikimedia, with your IP address and a `User-Agent` with a contact, and never a name. **Covers** (the Cover Art Archive provider) send only the MusicBrainz release group id of an album, read from the tags of its own files and never a title, and only when the files have no picture; the redirects of the archive are followed only to archive.org, over https. Its covers have no licence of their own: Sonda keeps a copy for your library and never treats it as free. **Discover** asks Last.fm for the similar artists of each seed (the seed's name or MusicBrainz id) and, for the best suggestions that have no tags yet, for the tags of that suggestion (its name); never MusicBrainz. Seeds you name are sent when you ask. Seeds taken from your listening, which are the names of the artists you play most, are sent only if you turned on `discover_from_listening` yourself; otherwise Discover refuses to run without a `seed`. **Lidarr** (your own server, at the address an admin set) gets the MusicBrainz id of an artist an admin sends, or of an artist someone opens `GET /artists/{id}/lidarr` for, and the API key, nothing else; asking it what it offers and testing the key send only the key. Never a name, your users, your listens or your files. What Lidarr then does is up to its own indexers and download clients. Besides the three jobs, the other exception to "only when someone asks" is `POST /providers/{name}/test`, which an admin sends to check a key (or, for Lidarr, the address and the key); it sends nothing but the key. **Similar artists.** `GET /api/v1/artists/{id}/similar` asks Last.fm for the artists similar to an artist (one of yours, or one Last.fm named earlier) and keeps the answer in your database: 7 days, or 1 day when Last.fm does not know the artist. Until then a second look costs nothing and sends nothing, and if Last.fm is down you still get the last answer, marked `stale`. If the provider is off the answer says `disabled` instead of failing. Each similar artist says whether you have it (`in_library`); the ones you do not have are stored as artists without files. They can be opened with `GET /artists/{id}` but are never part of your artist lists, and if you later add their music, the scan finds them there. Last.fm's `score` is relative to each answer (the best one is usually 1), so compare artists only inside one answer. @@ -264,6 +273,8 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | admin | `POST /library/scan`, `POST /libraries/{id}/scan` | Scan everything, or one library. `?full=true` reads every file again. | | admin | `GET /providers`, `PUT /providers/{name}`, `POST /providers/{name}/test`, `DELETE /providers/{name}/data` | See, set up and check the providers (Last.fm, MusicBrainz, Wikipedia, Commons, the Cover Art Archive, Lidarr), and remove what they gave. The API key is write-only. | | admin | `GET /providers/lidarr/options` | The profiles and root folders that Lidarr offers, to choose the ones artists are sent with. | +| admin | `POST /artists/{id}/lidarr` | Send an artist to your own Lidarr, by its MusicBrainz id, which then looks for the music. Never by name, never by itself. | +| signed in | `GET /artists/{id}/lidarr` | Whether Lidarr has the artist and monitors it. | | admin | `GET`, `POST /users`; `PATCH`, `DELETE /users/{id}`; `PUT /users/{id}/password` | Manage users and the admin role. The last admin cannot be demoted or deleted. | A library path must be an absolute, existing folder, never the filesystem root, and never inside or around another library. Search is a case- and accent-insensitive substring match: `bjork`, `BJORK` and `BJÖRK` all find `Björk`, and `strasse` finds `Straße`. Accents still tell artists apart: `Beyoncé` and `Beyonce` stay two artists, and a search for either finds both. diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index d89a73c..a705fce 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -270,6 +270,7 @@ func run() error { Catalog: catalog.NewService(storage.NewCatalogRepo(db)), Providers: providerSettings, Lidarr: lidarrClient, + LidarrArtists: lidarr.NewService(discoveryRepo, providerSettings, lidarrClient), Discovery: discoveries, Tags: tags, Taste: tasteService, diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 24ef8ad..0bd2bf4 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -85,20 +85,21 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun tokens := auth.NewTokenIssuer(secret, time.Hour) prov := newProviders(db) deps := Deps{ - Logger: zap.NewNop(), - Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), - Tokens: tokens, - Listening: listening.NewService(storage.NewListeningRepo(db), 20*time.Second), - Scan: &fakeScan{}, - Libraries: library.NewLibraries(storage.NewLibraryRepo(db)), - Catalog: catalog.NewService(storage.NewCatalogRepo(db)), - Providers: prov, - Lidarr: &fakeLidarr{}, - Discovery: discovery.NewService(storage.NewDiscoveryRepo(db), &fakeLastfm{}, prov, zap.NewNop()), - Tags: discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()), - Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}), - Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)), - Streams: streaming.NewService(noFiles{}), + Logger: zap.NewNop(), + Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour), + Tokens: tokens, + Listening: listening.NewService(storage.NewListeningRepo(db), 20*time.Second), + Scan: &fakeScan{}, + Libraries: library.NewLibraries(storage.NewLibraryRepo(db)), + Catalog: catalog.NewService(storage.NewCatalogRepo(db)), + Providers: prov, + Lidarr: &fakeLidarr{}, + LidarrArtists: &fakeSender{}, + Discovery: discovery.NewService(storage.NewDiscoveryRepo(db), &fakeLastfm{}, prov, zap.NewNop()), + Tags: discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()), + Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}), + Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)), + Streams: streaming.NewService(noFiles{}), } feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db)) deps.Feedback = feedbackSvc diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index df0f78a..95008e8 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -30,6 +30,7 @@ import ( "codeberg.org/squidd/sonda/internal/library" "codeberg.org/squidd/sonda/internal/providers" "codeberg.org/squidd/sonda/internal/providers/lastfm" + "codeberg.org/squidd/sonda/internal/providers/lidarr" "codeberg.org/squidd/sonda/internal/storage" "codeberg.org/squidd/sonda/internal/streaming" ) @@ -101,6 +102,14 @@ func scanOptions(full bool) []library.ScanOption { return nil } +// fakeLidarrRemote is a Lidarr that accepts every artist and has them all. +type fakeLidarrRemote struct{} + +func (fakeLidarrRemote) Add(context.Context, lidarr.AddRequest) error { return nil } +func (fakeLidarrRemote) Artist(context.Context, string) (*lidarr.Artist, error) { + return &lidarr.Artist{ID: 7, Name: "Someone", Monitored: true}, nil +} + type contract struct { lastfm *fakeLastfm t *testing.T @@ -139,6 +148,7 @@ func newContract(t *testing.T) *contract { pictures.SetArtistSource(&fakeArtistPictures{found: images.Found{Data: testJPEG(t, 800, 600, color.White), Credit: images.Credit{Author: "Someone", License: "CC BY 4.0", LicenseURL: "https://creativecommons.org/licenses/by/4.0", PageURL: "https://commons.wikimedia.org/wiki/File:X.jpg"}}}) d.Images, d.ArtistImages = pictures, pictures + d.LidarrArtists = lidarr.NewService(storage.NewDiscoveryRepo(db), prov, fakeLidarrRemote{}) }) router, err := legacy.NewRouter(loadSpec(t)) @@ -555,6 +565,44 @@ func TestResponsesMatchTheSpec(t *testing.T) { c.expect(c.call("GET", "/me/taste?limit=-1", token, nil), 400, "taste with a bad limit", "invalid_input") c.expect(c.call("GET", "/me/taste?limit=abc", token, nil), 400, "taste with a limit that is not a number", "invalid_input") + // ---- sending an artist to Lidarr ---- + if _, err := c.db.Exec("UPDATE artists SET mbid = NULL WHERE id = ?", artistID); err != nil { + t.Fatal(err) + } + c.expect(c.call("GET", "/artists/"+artistID+"/lidarr", "", nil), 401, "lidarr presence without a token", "unauthorized") + c.expect(c.call("POST", "/artists/"+artistID+"/lidarr", "", nil), 401, "send an artist without a token", "unauthorized") + c.expect(c.call("GET", "/artists/abc/lidarr", token, nil), 400, "lidarr presence of a bad id", "invalid_input") + c.expect(c.call("POST", "/artists/abc/lidarr", token, nil), 400, "send a bad id", "invalid_input") + c.expect(c.call("GET", "/artists/999999/lidarr", token, nil), 404, "lidarr presence of an unknown artist", "artist_not_found") + c.expect(c.call("POST", "/artists/999999/lidarr", token, nil), 404, "send an unknown artist", "artist_not_found") + c.expect(c.call("POST", "/artists/"+artistID+"/lidarr", token, nil), 422, "send an artist with no MusicBrainz id", "no_musicbrainz_id") + if got := c.expect(c.call("GET", "/artists/"+artistID+"/lidarr", token, nil), 200, "lidarr presence with no id"); got["provider_status"] != "no_id" { + t.Fatalf("presence = %v", got) + } + if _, err := c.db.Exec("UPDATE artists SET mbid = ? WHERE id = ?", "a74b1b7f-71a5-4011-9441-d0b5e4122711", artistID); err != nil { + t.Fatal(err) + } + if got := c.expect(c.call("GET", "/artists/"+artistID+"/lidarr", token, nil), 200, "lidarr presence with the provider off"); got["provider_status"] != "disabled" || got["in_lidarr"] != false { + t.Fatalf("presence = %v", got) + } + c.expect(c.call("POST", "/artists/"+artistID+"/lidarr", token, nil), 409, "send an artist with lidarr off", "lidarr_disabled") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"api_key": "0123456789abcdef0123456789abcdef", "url": "http://orion:8686", "enabled": false}), 200, "set lidarr up again") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"lidarr": map[string]any{}}), 200, "forget the choices of lidarr") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": true}), 200, "switch lidarr on") + c.expect(c.call("POST", "/artists/"+artistID+"/lidarr", token, nil), 409, "send an artist before the profiles are chosen", "lidarr_not_configured") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"lidarr": map[string]any{"quality_profile_id": 1, "metadata_profile_id": 1, "root_folder_path": "/music"}}), 200, "choose the profiles") + if got := c.expect(c.call("POST", "/artists/"+artistID+"/lidarr", token, nil), 200, "send an artist"); got["status"] != "added" { + t.Fatalf("sent = %v", got) + } + if got := c.expect(c.call("GET", "/artists/"+artistID+"/lidarr", token, nil), 200, "lidarr presence"); got["provider_status"] != "ok" || got["in_lidarr"] != true || got["monitored"] != true { + t.Fatalf("presence = %v", got) + } + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": false}), 200, "switch lidarr off again") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"api_key": "", "url": ""}), 200, "take lidarr's key and address away again") + if _, err := c.db.Exec("UPDATE artists SET mbid = NULL WHERE id = ?", artistID); err != nil { // as it was + t.Fatal(err) + } + // ---- description ---- desc := c.expect(c.call("GET", "/artists/"+artistID+"/description", token, nil), 200, "description with the provider off") if desc["provider_status"] != "disabled" || desc["description"] != nil { diff --git a/internal/api/http/handlers_lidarr.go b/internal/api/http/handlers_lidarr.go new file mode 100644 index 0000000..c6cb32f --- /dev/null +++ b/internal/api/http/handlers_lidarr.go @@ -0,0 +1,77 @@ +package httpapi + +import ( + "context" + "errors" + "net/http" + + "github.com/gin-gonic/gin" + + "codeberg.org/squidd/sonda/internal/discovery" + "codeberg.org/squidd/sonda/internal/providers/lidarr" +) + +type lidarrArtistService interface { + Send(ctx context.Context, artistID int64) (lidarr.Outcome, error) + Presence(ctx context.Context, artistID int64) (lidarr.Presence, error) +} + +type lidarrArtistHandlers struct{ svc lidarrArtistService } + +// send adds an artist to the admin's own Lidarr, which then looks for the music. It is an explicit +// action of an admin on one artist, never done by itself. +func (h *lidarrArtistHandlers) send(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + out, err := h.svc.Send(c.Request.Context(), id) + switch { + case err == nil: + c.JSON(http.StatusOK, gin.H{"status": string(out)}) + case errors.Is(err, discovery.ErrNotFound): + writeError(c, http.StatusNotFound, "artist_not_found", "not found") + case errors.Is(err, lidarr.ErrNoID): + writeError(c, http.StatusUnprocessableEntity, "no_musicbrainz_id", "the artist has no MusicBrainz id, and Sonda does not look one up by name") + case errors.Is(err, lidarr.ErrDisabled): + writeError(c, http.StatusConflict, "lidarr_disabled", "Lidarr is switched off") + case errors.Is(err, lidarr.ErrNotSetUp): + writeError(c, http.StatusConflict, "lidarr_not_configured", "the quality profile, the metadata profile and the root folder of Lidarr have not been chosen yet") + case errors.Is(err, lidarr.ErrNotFound): + writeError(c, http.StatusUnprocessableEntity, "lidarr_unknown_artist", "Lidarr does not know this artist") + case errors.Is(err, lidarr.ErrRejected): + writeError(c, http.StatusUnprocessableEntity, "lidarr_rejected", err.Error()) + case errors.Is(err, lidarr.ErrUnreachable), errors.Is(err, context.DeadlineExceeded): + // The request may have got there before the answer was lost: say so. + _ = c.Error(err) + writeError(c, http.StatusBadGateway, "lidarr_unavailable", "Lidarr could not be asked, or did not answer in time: the artist may have been added, check with GET /artists/{id}/lidarr") + default: + writeLidarrError(c, err) + } +} + +type lidarrPresenceJSON struct { + ProviderStatus string `json:"provider_status"` + InLidarr bool `json:"in_lidarr"` + Monitored bool `json:"monitored"` +} + +// presence says whether Lidarr has the artist. +func (h *lidarrArtistHandlers) presence(c *gin.Context) { + id, ok := pathID(c) + if !ok { + return + } + p, err := h.svc.Presence(c.Request.Context(), id) + switch { + case errors.Is(err, discovery.ErrNotFound): + writeError(c, http.StatusNotFound, "artist_not_found", "not found") + case errors.Is(err, context.Canceled): + c.AbortWithStatus(statusClientClosed) + case err != nil: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + default: + c.JSON(http.StatusOK, lidarrPresenceJSON{ProviderStatus: p.Status, InLidarr: p.InLidarr, Monitored: p.Monitored}) + } +} diff --git a/internal/api/http/lidarr_test.go b/internal/api/http/lidarr_test.go index 7c2d9f9..5e4bf0e 100644 --- a/internal/api/http/lidarr_test.go +++ b/internal/api/http/lidarr_test.go @@ -3,12 +3,14 @@ package httpapi import ( "context" "encoding/json" + "fmt" "net/http" "strings" "testing" "github.com/uptrace/bun" + "codeberg.org/squidd/sonda/internal/discovery" "codeberg.org/squidd/sonda/internal/providers/lidarr" ) @@ -156,3 +158,117 @@ func TestWhatGoesWrongAskingLidarrIsSaidPlainly(t *testing.T) { } } } + +// ---- sending an artist ---- + +type fakeSender struct { + outcome lidarr.Outcome + err error + sent []int64 + presence lidarr.Presence + perr error +} + +func (f *fakeSender) Send(_ context.Context, id int64) (lidarr.Outcome, error) { + if f.err != nil { + return "", f.err + } + f.sent = append(f.sent, id) + return f.outcome, nil +} + +func (f *fakeSender) Presence(context.Context, int64) (lidarr.Presence, error) { + return f.presence, f.perr +} + +func newSenderAPI(t *testing.T) (http.Handler, *fakeSender, string, string) { + t.Helper() + fake := &fakeSender{outcome: lidarr.Added} + h, _ := newTestEnvDB(t, func(d *Deps, _ *bun.DB) { d.LidarrArtists = fake }) + admin := registerAndLogin(t, h) + _, bob := addUser(t, h, admin, "bob", false) + return h, fake, admin, bob +} + +func TestOnlyAnAdminSendsAnArtistToLidarr(t *testing.T) { + h, fake, admin, bob := newSenderAPI(t) + if r := do(t, h, "POST", "/api/v1/artists/7/lidarr", "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("without a token: %d", r.Code) + } + if r := do(t, h, "POST", "/api/v1/artists/7/lidarr", bob, nil); r.Code != http.StatusForbidden || errCode(r) != "admin_required" { + t.Errorf("as a user: %d %v", r.Code, r.Body) + } + if len(fake.sent) != 0 { + t.Fatal("an artist was sent on behalf of someone who is not an admin") + } + r := do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil) + if r.Code != http.StatusOK || r.Body["status"] != "added" || len(fake.sent) != 1 || fake.sent[0] != 7 { + t.Fatalf("%d %v sent %v", r.Code, r.Body, fake.sent) + } + sameKeys(t, "sent", r.Body, "status") + fake.outcome = lidarr.AlreadyAdded + if r := do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil); r.Code != http.StatusOK || r.Body["status"] != "already_added" { + t.Errorf("again: %d %v", r.Code, r.Body) + } + if r := do(t, h, "POST", "/api/v1/artists/abc/lidarr", admin, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("a bad id: %d %v", r.Code, r.Body) + } +} + +func TestWhatStopsAnArtistFromBeingSentIsSaid(t *testing.T) { + h, fake, admin, _ := newSenderAPI(t) + for _, c := range []struct { + err error + status int + code string + }{ + {discovery.ErrNotFound, http.StatusNotFound, "artist_not_found"}, + {lidarr.ErrNoID, http.StatusUnprocessableEntity, "no_musicbrainz_id"}, + {lidarr.ErrDisabled, http.StatusConflict, "lidarr_disabled"}, + {lidarr.ErrNotSetUp, http.StatusConflict, "lidarr_not_configured"}, + {lidarr.ErrNotConfigured, http.StatusConflict, "lidarr_not_configured"}, + {lidarr.ErrNotFound, http.StatusUnprocessableEntity, "lidarr_unknown_artist"}, + {lidarr.ErrKeyRefused, http.StatusBadGateway, "lidarr_key_refused"}, + {lidarr.ErrUnreachable, http.StatusBadGateway, "lidarr_unavailable"}, + {context.DeadlineExceeded, http.StatusBadGateway, "lidarr_unavailable"}, + } { + fake.err = c.err + if r := do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil); r.Code != c.status || errCode(r) != c.code { + t.Errorf("%v: %d %v, want %d %s", c.err, r.Code, r.Body, c.status, c.code) + } + } + // A refusal carries Lidarr's reason, which the admin needs; a timeout says the artist may be there. + fake.err = fmt.Errorf("%w: Root folder does not exist", lidarr.ErrRejected) + r := do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil) + msg, _ := r.Body["error"].(map[string]any)["message"].(string) + if r.Code != http.StatusUnprocessableEntity || errCode(r) != "lidarr_rejected" || !strings.Contains(msg, "Root folder does not exist") { + t.Errorf("rejected: %d %v", r.Code, r.Body) + } + fake.err = lidarr.ErrUnreachable + r = do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil) + msg, _ = r.Body["error"].(map[string]any)["message"].(string) + if !strings.Contains(msg, "may have been added") { + t.Errorf("unreachable: %q, want it to say the artist may have been added", msg) + } +} + +func TestAnyUserSeesWhetherLidarrHasAnArtist(t *testing.T) { + h, fake, _, bob := newSenderAPI(t) + if r := do(t, h, "GET", "/api/v1/artists/7/lidarr", "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("without a token: %d", r.Code) + } + fake.presence = lidarr.Presence{Status: "ok", InLidarr: true, Monitored: true} + r := do(t, h, "GET", "/api/v1/artists/7/lidarr", bob, nil) + sameKeys(t, "presence", r.Body, "provider_status", "in_lidarr", "monitored") + if r.Code != http.StatusOK || r.Body["provider_status"] != "ok" || r.Body["in_lidarr"] != true || r.Body["monitored"] != true { + t.Errorf("%d %v", r.Code, r.Body) + } + fake.presence = lidarr.Presence{Status: "disabled"} + if r := do(t, h, "GET", "/api/v1/artists/7/lidarr", bob, nil); r.Body["provider_status"] != "disabled" || r.Body["in_lidarr"] != false { + t.Errorf("off: %v", r.Body) + } + fake.perr = discovery.ErrNotFound + if r := do(t, h, "GET", "/api/v1/artists/7/lidarr", bob, nil); r.Code != http.StatusNotFound || errCode(r) != "artist_not_found" { + t.Errorf("unknown: %d %v", r.Code, r.Body) + } +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go index 8efec57..c9d2228 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -144,6 +144,7 @@ type Deps struct { Catalog catalogService Providers providerService Lidarr lidarrOptionsService + LidarrArtists lidarrArtistService Discovery discoveryService Tags tagService Taste tasteService @@ -238,6 +239,8 @@ func NewRouter(d Deps) *gin.Engine { protected.GET("/artists/:id/tags", tg.tags) ab := &aboutHandlers{svc: d.About} protected.GET("/artists/:id/description", ab.description) + la := &lidarrArtistHandlers{svc: d.LidarrArtists} + protected.GET("/artists/:id/lidarr", la.presence) protected.GET("/artists/:id/image/credit", (&artistImageHandlers{svc: d.ArtistImages}).credit) protected.GET("/albums", cat.listAlbums) protected.GET("/albums/:id", cat.getAlbum) @@ -277,6 +280,7 @@ func NewRouter(d Deps) *gin.Engine { admin.GET("/providers", prov.list) admin.PUT("/providers/:name", prov.update) admin.GET("/providers/lidarr/options", prov.lidarrOptions) + admin.POST("/artists/:id/lidarr", la.send) admin.POST("/providers/:name/test", prov.test) admin.DELETE("/providers/:name/data", prov.purge) u := &usersHandlers{svc: d.Auth} diff --git a/internal/api/http/router_test.go b/internal/api/http/router_test.go index cff068e..b0d1e76 100644 --- a/internal/api/http/router_test.go +++ b/internal/api/http/router_test.go @@ -23,7 +23,7 @@ func TestNewRouterRefusesAMissingDependency(t *testing.T) { func TestNewRouterNamesEveryMissingDependency(t *testing.T) { missing := missingDeps(Deps{Logger: zap.NewNop()}) - for _, want := range []string{"Auth", "Tokens", "Listening", "CoversJob", "ArtistImagesJob", "DescriptionsJob", "Lidarr", "ArtistImages", "About", "Streams"} { + for _, want := range []string{"Auth", "Tokens", "Listening", "CoversJob", "ArtistImagesJob", "DescriptionsJob", "Lidarr", "LidarrArtists", "ArtistImages", "About", "Streams"} { found := false for _, m := range missing { found = found || m == want diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 7e6f5eb..88737c5 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -1165,6 +1165,88 @@ paths: '400': {$ref: '#/components/responses/InvalidInput'} '401': {$ref: '#/components/responses/Unauthorized'} + /artists/{id}/lidarr: + parameters: + - {$ref: '#/components/parameters/Id'} + get: + tags: [Discovery] + operationId: getArtistInLidarr + summary: Whether Lidarr has an artist + description: | + Asks your own Lidarr, by the artist's MusicBrainz id and never by name, whether it has the artist and + whether it monitors it. Like the descriptions, whatever stops the answer is a status and not an error: + `disabled` when the `lidarr` provider is off (nobody is asked), `no_id` when the artist has no MusicBrainz id, + `not_configured` when Lidarr has no address or key, `error` when it could not be asked now. Only the id and + the API key go out, to the address an admin set. + security: [{bearerAuth: []}] + responses: + '200': + description: Whether Lidarr has the artist. + content: + application/json: + schema: {$ref: '#/components/schemas/ArtistInLidarr'} + '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'} + post: + tags: [Discovery] + operationId: sendArtistToLidarr + summary: Send an artist to Lidarr + description: | + Admin only. Adds the artist to your own Lidarr, which then looks for and downloads the music by itself: + Sonda downloads nothing, and does this only when an admin asks, for one artist, never by itself. It works + for any artist that has a MusicBrainz id, also one that Sonda only learned of from a list of similar + artists, which is the point: asking for music you do not have. It never looks an id up by name. + + It uses what an admin set in `PUT /providers/lidarr`: the quality profile, the metadata profile, the root + folder, which of the albums to monitor and whether Lidarr starts searching at once. It needs the provider + switched on and those chosen. An artist that Lidarr already has is not an error (`already_added`, nothing + changes), so the call can be repeated. If Lidarr does not answer in time, the artist may have been added + all the same: read it with `GET /artists/{id}/lidarr`. Only the MusicBrainz id and the API key go out, + to the address an admin set. + security: [{bearerAuth: []}] + responses: + '200': + description: The artist is in Lidarr. + content: + application/json: + schema: + type: object + additionalProperties: false + required: [status] + properties: + status: + type: string + enum: [added, already_added] + description: "`added` now, or `already_added` when Lidarr had it." + '400': {$ref: '#/components/responses/InvalidInput'} + '401': {$ref: '#/components/responses/Unauthorized'} + '403': {$ref: '#/components/responses/AdminRequired'} + '404': + description: No such artist (`artist_not_found`). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + '409': + description: The provider is switched off (`lidarr_disabled`), or has no address or key, or the profiles and the folder were not chosen (`lidarr_not_configured`). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + '422': + description: The artist has no MusicBrainz id (`no_musicbrainz_id`), Lidarr does not know the id (`lidarr_unknown_artist`), or it refused the artist (`lidarr_rejected`, with its reason). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + '502': + description: Lidarr refused the key (`lidarr_key_refused`), or could not be asked or did not answer in time (`lidarr_unavailable`; the artist may have been added). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + /artists/{id}/description: parameters: - {$ref: '#/components/parameters/Id'} @@ -2323,6 +2405,18 @@ components: path: {type: string} accessible: {type: boolean, description: Whether Lidarr can use the folder now.} + ArtistInLidarr: + type: object + additionalProperties: false + required: [provider_status, in_lidarr, monitored] + properties: + provider_status: + type: string + enum: [ok, disabled, no_id, not_configured, error] + description: "`ok` when Lidarr was asked (and `in_lidarr` is its answer); the others say why it was not." + in_lidarr: {type: boolean} + monitored: {type: boolean, description: Whether Lidarr monitors the artist; false when it does not have it.} + LidarrProfile: type: object additionalProperties: false diff --git a/internal/providers/lidarr/service.go b/internal/providers/lidarr/service.go new file mode 100644 index 0000000..e2f4692 --- /dev/null +++ b/internal/providers/lidarr/service.go @@ -0,0 +1,141 @@ +package lidarr + +import ( + "context" + "errors" + "sync" + + "codeberg.org/squidd/sonda/internal/discovery" + "codeberg.org/squidd/sonda/internal/providers" +) + +// Errors of the service, besides the ones of the client and discovery.ErrNotFound for an artist +// that does not exist. +var ( + ErrDisabled = errors.New("lidarr: the provider is switched off") + ErrNoID = errors.New("lidarr: the artist has no MusicBrainz id") + ErrNotSetUp = errors.New("lidarr: the profiles and the root folder have not been chosen yet") +) + +// StatusNotConfigured is the status of Presence when Lidarr has no address or no key. +const StatusNotConfigured = "not_configured" + +// Outcome says what Send did. +type Outcome string + +// The outcomes of Send. +const ( + Added Outcome = "added" + AlreadyAdded Outcome = "already_added" +) + +// Artists finds an artist by id. +type Artists interface { + Artist(ctx context.Context, id int64) (discovery.Artist, error) +} + +// Switches says whether a provider may be used right now, and with which settings. +type Switches interface { + Active(ctx context.Context, name string) (providers.Settings, bool, error) +} + +// Remote is what the service needs of Lidarr: *Client. +type Remote interface { + Add(ctx context.Context, r AddRequest) error + Artist(ctx context.Context, mbid string) (*Artist, error) +} + +// Service sends the artists of Sonda to the admin's own Lidarr. It never searches for an artist by +// name: it has the MusicBrainz id or it does nothing. +type Service struct { + artists Artists + settings Switches + remote Remote + + mu sync.Mutex // one add at a time: two clicks on the same artist must not both reach Lidarr +} + +// NewService builds a Service. +func NewService(a Artists, s Switches, r Remote) *Service { + return &Service{artists: a, settings: s, remote: r} +} + +// Send adds the artist to Lidarr, with the profiles and the folder the admin chose, and tells +// Lidarr to start looking for the music if the admin asked for that. It does nothing and asks no +// one when the artist has no MusicBrainz id, when the provider is off or when it is not set up. +// An artist that Lidarr already has is not an error, and nothing changes. A request that ends +// without an answer (a timeout) may still have added the artist: sending again says so. +func (s *Service) Send(ctx context.Context, artistID int64) (Outcome, error) { + artist, err := s.artists.Artist(ctx, artistID) + if err != nil { + return "", err + } + if artist.MBID == "" { // Sonda never looks an id up by name + return "", ErrNoID + } + set, on, err := s.settings.Active(ctx, providers.Lidarr) + if err != nil { + return "", err + } + if !on { + return "", ErrDisabled + } + if !set.Lidarr.Complete() { + return "", ErrNotSetUp + } + + s.mu.Lock() + defer s.mu.Unlock() + err = s.remote.Add(ctx, AddRequest{ + MBID: artist.MBID, QualityProfileID: set.Lidarr.QualityProfileID, MetadataProfileID: set.Lidarr.MetadataProfileID, + RootFolderPath: set.Lidarr.RootFolderPath, Monitor: set.Lidarr.Monitor, SearchMissing: set.Lidarr.SearchMissing, + }) + switch { + case err == nil: + return Added, nil + case errors.Is(err, ErrAlreadyAdded): + return AlreadyAdded, nil + } + return "", err +} + +// Presence is whether Lidarr has an artist. Status is one of discovery.StatusOK (Lidarr was asked), +// StatusDisabled, StatusNoID, StatusError (it could not be asked now) and StatusNotConfigured. +type Presence struct { + Status string + InLidarr bool + Monitored bool +} + +// Presence asks Lidarr whether it has the artist. Like the descriptions, whatever stops the answer is +// a status and not an error; only a request that was canceled, or an artist that does not exist, is. +func (s *Service) Presence(ctx context.Context, artistID int64) (Presence, error) { + artist, err := s.artists.Artist(ctx, artistID) + if err != nil { + return Presence{}, err + } + if artist.MBID == "" { + return Presence{Status: discovery.StatusNoID}, nil + } + _, on, err := s.settings.Active(ctx, providers.Lidarr) + if err != nil { + return Presence{}, err + } + if !on { + return Presence{Status: discovery.StatusDisabled}, nil + } + have, err := s.remote.Artist(ctx, artist.MBID) + switch { + case err == nil: + case errors.Is(err, context.Canceled): + return Presence{}, err + case errors.Is(err, ErrNotConfigured): + return Presence{Status: StatusNotConfigured}, nil + default: + return Presence{Status: discovery.StatusError}, nil + } + if have == nil { + return Presence{Status: discovery.StatusOK}, nil + } + return Presence{Status: discovery.StatusOK, InLidarr: true, Monitored: have.Monitored}, nil +} diff --git a/internal/providers/lidarr/service_test.go b/internal/providers/lidarr/service_test.go new file mode 100644 index 0000000..5a3cc67 --- /dev/null +++ b/internal/providers/lidarr/service_test.go @@ -0,0 +1,210 @@ +package lidarr + +import ( + "context" + "errors" + "sync" + "sync/atomic" + "testing" + "time" + + "codeberg.org/squidd/sonda/internal/discovery" + "codeberg.org/squidd/sonda/internal/providers" +) + +type fakeArtists struct{ m map[int64]discovery.Artist } + +func (f fakeArtists) Artist(_ context.Context, id int64) (discovery.Artist, error) { + a, ok := f.m[id] + if !ok { + return discovery.Artist{}, discovery.ErrNotFound + } + return a, nil +} + +type fakeSwitch struct { + set providers.Settings + on bool + err error +} + +func (f *fakeSwitch) Active(_ context.Context, name string) (providers.Settings, bool, error) { + if name != providers.Lidarr { + return providers.Settings{}, false, errors.New("asked about another provider") + } + return f.set, f.on, f.err +} + +type fakeRemote struct { + mu sync.Mutex + added []AddRequest + addErr error + artist *Artist + artistEr error + asked int32 + delay time.Duration + live int32 + maxLive int32 +} + +func (f *fakeRemote) Add(_ context.Context, r AddRequest) error { + n := atomic.AddInt32(&f.live, 1) + defer atomic.AddInt32(&f.live, -1) + for { + m := atomic.LoadInt32(&f.maxLive) + if n <= m || atomic.CompareAndSwapInt32(&f.maxLive, m, n) { + break + } + } + time.Sleep(f.delay) + f.mu.Lock() + defer f.mu.Unlock() + if f.addErr != nil { + return f.addErr + } + for _, a := range f.added { + if a.MBID == r.MBID { + return ErrAlreadyAdded + } + } + f.added = append(f.added, r) + return nil +} + +func (f *fakeRemote) Artist(context.Context, string) (*Artist, error) { + atomic.AddInt32(&f.asked, 1) + return f.artist, f.artistEr +} + +func newService() (*Service, *fakeSwitch, *fakeRemote) { + sw := &fakeSwitch{on: true, set: providers.Settings{ + Provider: providers.Lidarr, Enabled: true, APIKey: key, URL: "http://orion:8686", + Lidarr: providers.LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/music", Monitor: "latest", SearchMissing: true}, + }} + rem := &fakeRemote{} + artists := fakeArtists{m: map[int64]discovery.Artist{ + 1: {ID: 1, Name: "Radiohead", MBID: mbid}, + 2: {ID: 2, Name: "No Id"}, + }} + return NewService(artists, sw, rem), sw, rem +} + +func TestSendAddsTheArtistWithTheChoicesOfTheAdmin(t *testing.T) { + s, _, rem := newService() + got, err := s.Send(context.Background(), 1) + if err != nil || got != Added { + t.Fatalf("got %q err=%v", got, err) + } + want := AddRequest{MBID: mbid, QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/music", Monitor: "latest", SearchMissing: true} + if len(rem.added) != 1 || rem.added[0] != want { + t.Errorf("added = %+v, want %+v", rem.added, want) + } + // Again: Lidarr has it, and nothing changes. + if got, err := s.Send(context.Background(), 1); err != nil || got != AlreadyAdded || len(rem.added) != 1 { + t.Errorf("second time: %q %v, added %d", got, err, len(rem.added)) + } +} + +func TestSendNeverLooksAnArtistUpByNameAndNeverAsksWhenItCannot(t *testing.T) { + ctx := context.Background() + s, sw, rem := newService() + if _, err := s.Send(ctx, 2); !errors.Is(err, ErrNoID) { + t.Errorf("no id: %v", err) + } + if _, err := s.Send(ctx, 99); !errors.Is(err, discovery.ErrNotFound) { + t.Errorf("unknown artist: %v", err) + } + sw.on = false + if _, err := s.Send(ctx, 1); !errors.Is(err, ErrDisabled) { + t.Errorf("switched off: %v", err) + } + sw.on = true + sw.set.Lidarr.RootFolderPath = "" + if _, err := s.Send(ctx, 1); !errors.Is(err, ErrNotSetUp) { + t.Errorf("no root folder chosen: %v", err) + } + sw.err = errors.New("database is locked") + if _, err := s.Send(ctx, 1); err == nil || errors.Is(err, ErrDisabled) { + t.Errorf("a settings error must pass through as it is: %v", err) + } + if len(rem.added) != 0 { + t.Errorf("Lidarr was sent %d artists in cases where it must not be asked", len(rem.added)) + } +} + +func TestWhatLidarrSaysComesBackAsItIs(t *testing.T) { + for _, cause := range []error{ErrNotFound, ErrRejected, ErrKeyRefused, ErrUnreachable, ErrBadResponse} { + s, _, rem := newService() + rem.addErr = cause + if _, err := s.Send(context.Background(), 1); !errors.Is(err, cause) { + t.Errorf("%v: err = %v", cause, err) + } + } +} + +func TestTwoSendsOfTheSameArtistAtOnceAreOneAfterTheOther(t *testing.T) { + s, _, rem := newService() + rem.delay = 30 * time.Millisecond + var wg sync.WaitGroup + results := make(chan Outcome, 2) + for i := 0; i < 2; i++ { + wg.Add(1) + go func() { + defer wg.Done() + got, err := s.Send(context.Background(), 1) + if err != nil { + t.Error(err) + } + results <- got + }() + } + wg.Wait() + close(results) + added, already := 0, 0 + for r := range results { + switch r { + case Added: + added++ + case AlreadyAdded: + already++ + } + } + if added != 1 || already != 1 || atomic.LoadInt32(&rem.maxLive) != 1 { + t.Errorf("added %d, already %d, at most %d at the same time, want 1, 1 and 1", added, already, rem.maxLive) + } +} + +func TestPresenceSaysWhetherLidarrHasTheArtist(t *testing.T) { + ctx := context.Background() + s, sw, rem := newService() + rem.artist = &Artist{ID: 7, Name: "Radiohead", Monitored: true} + if p, err := s.Presence(ctx, 1); err != nil || p != (Presence{Status: discovery.StatusOK, InLidarr: true, Monitored: true}) { + t.Errorf("there: %+v %v", p, err) + } + rem.artist = nil + if p, err := s.Presence(ctx, 1); err != nil || p != (Presence{Status: discovery.StatusOK}) { + t.Errorf("not there: %+v %v", p, err) + } + if p, _ := s.Presence(ctx, 2); p.Status != discovery.StatusNoID { + t.Errorf("no id: %+v", p) + } + before := atomic.LoadInt32(&rem.asked) + sw.on = false + if p, _ := s.Presence(ctx, 1); p.Status != discovery.StatusDisabled || atomic.LoadInt32(&rem.asked) != before { + t.Errorf("off: %+v, Lidarr was asked: %v", p, atomic.LoadInt32(&rem.asked) != before) + } + sw.on = true + for cause, want := range map[error]string{ErrNotConfigured: StatusNotConfigured, ErrKeyRefused: discovery.StatusError, ErrUnreachable: discovery.StatusError, ErrBadResponse: discovery.StatusError} { + rem.artistEr = cause + if p, err := s.Presence(ctx, 1); err != nil || p.Status != want || p.InLidarr { + t.Errorf("%v: %+v %v, want status %q", cause, p, err, want) + } + } + if _, err := s.Presence(ctx, 99); !errors.Is(err, discovery.ErrNotFound) { + t.Errorf("unknown artist: %v", err) + } + rem.artistEr = context.Canceled + if _, err := s.Presence(ctx, 1); !errors.Is(err, context.Canceled) { + t.Errorf("a canceled request is not a status: %v", err) + } +} From 22cd0aecd4c0343d5818dba811e0bfbcb3f8e4c5 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 11:35:28 +0100 Subject: [PATCH 2/2] fix(lidarr): protect Lidarr from many asks, refuse bad ids, give up when the request does - Asking whether Lidarr has an artist keeps the answer for a minute, asks once however many ask at once, and has at most four questions on their way; what cannot be answered is not kept, and a send forgets what was kept. - An id that is not a MusicBrainz id (one that a provider named and that was only cleaned) is no id: it is refused or shown as no_id instead of becoming a 500. - A send that waits for its turn gives up when its request does. - A bad answer after the add also says the artist may have been added; a canceled send ends like the other canceled requests. - The README says which artists can be sent. --- README.md | 2 +- internal/api/http/handlers_lidarr.go | 4 +- internal/api/http/lidarr_test.go | 17 ++- internal/providers/lidarr/service.go | 128 ++++++++++++++++-- internal/providers/lidarr/service_test.go | 152 ++++++++++++++++++++++ 5 files changed, 284 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 9d4dc4c..44e2696 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Artist descriptions.** `GET /artists/{id}/description` shows what Wikipedia says about an artist: a short text, with the page it comes from, a link to it and the licence (CC BY-SA 4.0), which you must show next to it. With the `wikipedia` provider and the `musicbrainz` one switched on (the link to Wikidata comes from MusicBrainz), Sonda follows ids and never a name: the artist's MusicBrainz id, then the Wikidata item that MusicBrainz links to it, then the Wikipedia page that Wikidata names in the first of your languages (`[wikipedia] languages`, the code of that Wikipedia, for example `["it", "en"]`) that has one. An artist without a MusicBrainz id has no description. It is kept for 30 days and removed with the provider's data. To read the descriptions of the whole library in one go, an admin starts a background job like a scan: `POST /library/descriptions` (follow it with `GET /library/descriptions`, stop it with `DELETE /library/descriptions`; `?refresh=true` reads every artist again). It only goes through artists that have a MusicBrainz id, never replaces a description by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one switched on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin switched on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you switched on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). -- **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. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist you wished for to their own Lidarr (see [External providers](#external-providers)). An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch it. +- **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. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist that Discover showed to their own Lidarr (see [External providers](#external-providers)), which works for an artist that has a MusicBrainz id and that Sonda knows of from a list or from your files. An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch 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. diff --git a/internal/api/http/handlers_lidarr.go b/internal/api/http/handlers_lidarr.go index c6cb32f..70c3971 100644 --- a/internal/api/http/handlers_lidarr.go +++ b/internal/api/http/handlers_lidarr.go @@ -41,7 +41,9 @@ func (h *lidarrArtistHandlers) send(c *gin.Context) { writeError(c, http.StatusUnprocessableEntity, "lidarr_unknown_artist", "Lidarr does not know this artist") case errors.Is(err, lidarr.ErrRejected): writeError(c, http.StatusUnprocessableEntity, "lidarr_rejected", err.Error()) - case errors.Is(err, lidarr.ErrUnreachable), errors.Is(err, context.DeadlineExceeded): + case errors.Is(err, context.Canceled): + c.AbortWithStatus(statusClientClosed) // the one who clicked left + case errors.Is(err, lidarr.ErrUnreachable), errors.Is(err, lidarr.ErrBadResponse), errors.Is(err, context.DeadlineExceeded): // The request may have got there before the answer was lost: say so. _ = c.Error(err) writeError(c, http.StatusBadGateway, "lidarr_unavailable", "Lidarr could not be asked, or did not answer in time: the artist may have been added, check with GET /artists/{id}/lidarr") diff --git a/internal/api/http/lidarr_test.go b/internal/api/http/lidarr_test.go index 5e4bf0e..e6ac6ed 100644 --- a/internal/api/http/lidarr_test.go +++ b/internal/api/http/lidarr_test.go @@ -230,6 +230,7 @@ func TestWhatStopsAnArtistFromBeingSentIsSaid(t *testing.T) { {lidarr.ErrNotFound, http.StatusUnprocessableEntity, "lidarr_unknown_artist"}, {lidarr.ErrKeyRefused, http.StatusBadGateway, "lidarr_key_refused"}, {lidarr.ErrUnreachable, http.StatusBadGateway, "lidarr_unavailable"}, + {lidarr.ErrBadResponse, http.StatusBadGateway, "lidarr_unavailable"}, {context.DeadlineExceeded, http.StatusBadGateway, "lidarr_unavailable"}, } { fake.err = c.err @@ -244,11 +245,17 @@ func TestWhatStopsAnArtistFromBeingSentIsSaid(t *testing.T) { if r.Code != http.StatusUnprocessableEntity || errCode(r) != "lidarr_rejected" || !strings.Contains(msg, "Root folder does not exist") { t.Errorf("rejected: %d %v", r.Code, r.Body) } - fake.err = lidarr.ErrUnreachable - r = do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil) - msg, _ = r.Body["error"].(map[string]any)["message"].(string) - if !strings.Contains(msg, "may have been added") { - t.Errorf("unreachable: %q, want it to say the artist may have been added", msg) + for _, cause := range []error{lidarr.ErrUnreachable, lidarr.ErrBadResponse, context.DeadlineExceeded} { + fake.err = cause + r = do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil) + msg, _ = r.Body["error"].(map[string]any)["message"].(string) + if !strings.Contains(msg, "may have been added") { + t.Errorf("%v: %q, want it to say the artist may have been added", cause, msg) + } + } + fake.err = context.Canceled + if r := do(t, h, "POST", "/api/v1/artists/7/lidarr", admin, nil); r.Code != 499 { + t.Errorf("a request that left: %d", r.Code) } } diff --git a/internal/providers/lidarr/service.go b/internal/providers/lidarr/service.go index e2f4692..30880b6 100644 --- a/internal/providers/lidarr/service.go +++ b/internal/providers/lidarr/service.go @@ -4,6 +4,9 @@ import ( "context" "errors" "sync" + "time" + + "golang.org/x/sync/singleflight" "codeberg.org/squidd/sonda/internal/discovery" "codeberg.org/squidd/sonda/internal/providers" @@ -29,6 +32,21 @@ const ( AlreadyAdded Outcome = "already_added" ) +const ( + // PresenceTTL is how long an answer about an artist is kept: a person looking at an artist + // page does not make Sonda ask the admin's Lidarr again at every look. + PresenceTTL = time.Minute + + // MaxAsking is how many artists Lidarr is asked about at the same time. + MaxAsking = 4 + + // maxSeen is how many answers are kept before the old ones are dropped. + maxSeen = 1000 + + // askTimeout bounds one question to Lidarr, however many people wait for the answer. + askTimeout = 25 * time.Second +) + // Artists finds an artist by id. type Artists interface { Artist(ctx context.Context, id int64) (discovery.Artist, error) @@ -52,12 +70,46 @@ type Service struct { settings Switches remote Remote - mu sync.Mutex // one add at a time: two clicks on the same artist must not both reach Lidarr + now func() time.Time + turn chan struct{} // one add at a time: two clicks on the same artist must not both reach Lidarr + asking chan struct{} // at most MaxAsking questions at the same time + flight singleflight.Group + + mu sync.Mutex // guards seen + seen map[presenceKey]seenEntry +} + +type presenceKey struct { + artist int64 + url string // another address is another Lidarr +} + +type seenEntry struct { + p Presence + at time.Time } // NewService builds a Service. func NewService(a Artists, s Switches, r Remote) *Service { - return &Service{artists: a, settings: s, remote: r} + return &Service{ + artists: a, settings: s, remote: r, now: time.Now, + turn: make(chan struct{}, 1), asking: make(chan struct{}, MaxAsking), seen: map[presenceKey]seenEntry{}, + } +} + +// validID says whether an id is a MusicBrainz id. Ids of files are checked when they are read, but +// the ones that a provider named are only cleaned, and an id that cannot be one is no id. +func validID(id string) bool { return idRe.MatchString(id) } + +// forget drops what was kept about an artist: it is about to change, or just did. +func (s *Service) forget(artistID int64) { + s.mu.Lock() + defer s.mu.Unlock() + for k := range s.seen { + if k.artist == artistID { + delete(s.seen, k) + } + } } // Send adds the artist to Lidarr, with the profiles and the folder the admin chose, and tells @@ -70,7 +122,7 @@ func (s *Service) Send(ctx context.Context, artistID int64) (Outcome, error) { if err != nil { return "", err } - if artist.MBID == "" { // Sonda never looks an id up by name + if !validID(artist.MBID) { // Sonda never looks an id up by name return "", ErrNoID } set, on, err := s.settings.Active(ctx, providers.Lidarr) @@ -84,8 +136,13 @@ func (s *Service) Send(ctx context.Context, artistID int64) (Outcome, error) { return "", ErrNotSetUp } - s.mu.Lock() - defer s.mu.Unlock() + select { // a request that leaves while it waits for its turn does not take it + case s.turn <- struct{}{}: + defer func() { <-s.turn }() + case <-ctx.Done(): + return "", ctx.Err() + } + defer s.forget(artistID) // what Lidarr has is not what was kept err = s.remote.Add(ctx, AddRequest{ MBID: artist.MBID, QualityProfileID: set.Lidarr.QualityProfileID, MetadataProfileID: set.Lidarr.MetadataProfileID, RootFolderPath: set.Lidarr.RootFolderPath, Monitor: set.Lidarr.Monitor, SearchMissing: set.Lidarr.SearchMissing, @@ -109,22 +166,57 @@ type Presence struct { // Presence asks Lidarr whether it has the artist. Like the descriptions, whatever stops the answer is // a status and not an error; only a request that was canceled, or an artist that does not exist, is. +// Anyone signed in can ask, so Lidarr is protected: an answer is kept for PresenceTTL, however many +// ask at once it is asked once, and only MaxAsking questions are on their way at the same time. func (s *Service) Presence(ctx context.Context, artistID int64) (Presence, error) { artist, err := s.artists.Artist(ctx, artistID) if err != nil { return Presence{}, err } - if artist.MBID == "" { + if !validID(artist.MBID) { return Presence{Status: discovery.StatusNoID}, nil } - _, on, err := s.settings.Active(ctx, providers.Lidarr) + set, on, err := s.settings.Active(ctx, providers.Lidarr) if err != nil { return Presence{}, err } if !on { return Presence{Status: discovery.StatusDisabled}, nil } - have, err := s.remote.Artist(ctx, artist.MBID) + key := presenceKey{artist: artistID, url: set.URL} + s.mu.Lock() + kept, ok := s.seen[key] + s.mu.Unlock() + if ok && s.now().Sub(kept.at) < PresenceTTL { + return kept.p, nil + } + + done := s.flight.DoChan(key.url+"\x00"+artist.MBID, func() (any, error) { + return s.ask(key, artist.MBID) + }) + select { + case <-ctx.Done(): + return Presence{}, ctx.Err() + case r := <-done: + if r.Err != nil { + return Presence{}, r.Err + } + return r.Val.(Presence), nil + } +} + +// ask puts the question to Lidarr on a context of its own, so that the one who first asked leaving +// does not cancel it for the others. +func (s *Service) ask(key presenceKey, mbid string) (Presence, error) { + ctx, cancel := context.WithTimeout(context.Background(), askTimeout) + defer cancel() + select { + case s.asking <- struct{}{}: + defer func() { <-s.asking }() + case <-ctx.Done(): + return Presence{Status: discovery.StatusError}, nil + } + have, err := s.remote.Artist(ctx, mbid) switch { case err == nil: case errors.Is(err, context.Canceled): @@ -132,10 +224,22 @@ func (s *Service) Presence(ctx context.Context, artistID int64) (Presence, error case errors.Is(err, ErrNotConfigured): return Presence{Status: StatusNotConfigured}, nil default: - return Presence{Status: discovery.StatusError}, nil + return Presence{Status: discovery.StatusError}, nil // not kept: the next ask tries again } - if have == nil { - return Presence{Status: discovery.StatusOK}, nil + p := Presence{Status: discovery.StatusOK} + if have != nil { + p.InLidarr, p.Monitored = true, have.Monitored } - return Presence{Status: discovery.StatusOK, InLidarr: true, Monitored: have.Monitored}, nil + s.mu.Lock() + now := s.now() + if len(s.seen) >= maxSeen { // what is old goes, so the memory does not follow the size of the library + for k, e := range s.seen { + if now.Sub(e.at) >= PresenceTTL { + delete(s.seen, k) + } + } + } + s.seen[key] = seenEntry{p: p, at: now} + s.mu.Unlock() + return p, nil } diff --git a/internal/providers/lidarr/service_test.go b/internal/providers/lidarr/service_test.go index 5a3cc67..59cfffd 100644 --- a/internal/providers/lidarr/service_test.go +++ b/internal/providers/lidarr/service_test.go @@ -3,6 +3,7 @@ package lidarr import ( "context" "errors" + "fmt" "sync" "sync/atomic" "testing" @@ -45,6 +46,10 @@ type fakeRemote struct { delay time.Duration live int32 maxLive int32 + + artistDelay time.Duration + artistLive int32 + artistMax int32 } func (f *fakeRemote) Add(_ context.Context, r AddRequest) error { @@ -73,6 +78,17 @@ func (f *fakeRemote) Add(_ context.Context, r AddRequest) error { func (f *fakeRemote) Artist(context.Context, string) (*Artist, error) { atomic.AddInt32(&f.asked, 1) + n := atomic.AddInt32(&f.artistLive, 1) + defer atomic.AddInt32(&f.artistLive, -1) + for { + m := atomic.LoadInt32(&f.artistMax) + if n <= m || atomic.CompareAndSwapInt32(&f.artistMax, m, n) { + break + } + } + time.Sleep(f.artistDelay) + f.mu.Lock() + defer f.mu.Unlock() return f.artist, f.artistEr } @@ -85,7 +101,11 @@ func newService() (*Service, *fakeSwitch, *fakeRemote) { artists := fakeArtists{m: map[int64]discovery.Artist{ 1: {ID: 1, Name: "Radiohead", MBID: mbid}, 2: {ID: 2, Name: "No Id"}, + 3: {ID: 3, Name: "Bad Id", MBID: "abc"}, }} + for i := int64(10); i < 30; i++ { // more artists, with ids of their own + artists.m[i] = discovery.Artist{ID: i, Name: "Many", MBID: fmt.Sprintf("00000000-0000-4000-8000-%012d", i)} + } return NewService(artists, sw, rem), sw, rem } @@ -177,11 +197,15 @@ func TestTwoSendsOfTheSameArtistAtOnceAreOneAfterTheOther(t *testing.T) { func TestPresenceSaysWhetherLidarrHasTheArtist(t *testing.T) { ctx := context.Background() s, sw, rem := newService() + now := time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC) + s.now = func() time.Time { return now } + later := func() { now = now.Add(PresenceTTL + time.Second) } // an answer kept is not the point here rem.artist = &Artist{ID: 7, Name: "Radiohead", Monitored: true} if p, err := s.Presence(ctx, 1); err != nil || p != (Presence{Status: discovery.StatusOK, InLidarr: true, Monitored: true}) { t.Errorf("there: %+v %v", p, err) } rem.artist = nil + later() if p, err := s.Presence(ctx, 1); err != nil || p != (Presence{Status: discovery.StatusOK}) { t.Errorf("not there: %+v %v", p, err) } @@ -194,6 +218,7 @@ func TestPresenceSaysWhetherLidarrHasTheArtist(t *testing.T) { t.Errorf("off: %+v, Lidarr was asked: %v", p, atomic.LoadInt32(&rem.asked) != before) } sw.on = true + later() for cause, want := range map[error]string{ErrNotConfigured: StatusNotConfigured, ErrKeyRefused: discovery.StatusError, ErrUnreachable: discovery.StatusError, ErrBadResponse: discovery.StatusError} { rem.artistEr = cause if p, err := s.Presence(ctx, 1); err != nil || p.Status != want || p.InLidarr { @@ -208,3 +233,130 @@ func TestPresenceSaysWhetherLidarrHasTheArtist(t *testing.T) { t.Errorf("a canceled request is not a status: %v", err) } } + +func TestAnIdThatIsNotAMusicBrainzIdIsNoIdAtAll(t *testing.T) { + s, _, rem := newService() + if _, err := s.Send(context.Background(), 3); !errors.Is(err, ErrNoID) { + t.Errorf("Send: %v", err) + } + if p, err := s.Presence(context.Background(), 3); err != nil || p.Status != discovery.StatusNoID { + t.Errorf("Presence: %+v %v", p, err) + } + if len(rem.added) != 0 || atomic.LoadInt32(&rem.asked) != 0 { + t.Error("Lidarr was asked about an id that cannot be one") + } +} + +func TestASendThatIsGivenUpWhileItWaitsForItsTurnDoesNotGoOn(t *testing.T) { + s, _, rem := newService() + rem.delay = 150 * time.Millisecond + first := make(chan struct{}) + go func() { _, _ = s.Send(context.Background(), 1); close(first) }() + time.Sleep(30 * time.Millisecond) // the first has its turn + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) + defer cancel() + start := time.Now() + if _, err := s.Send(ctx, 10); !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("err = %v, want the deadline of the request", err) + } + if time.Since(start) > 100*time.Millisecond { + t.Errorf("it waited %v for a turn it could not use", time.Since(start)) + } + <-first + if len(rem.added) != 1 { + t.Errorf("added %d, want only the first", len(rem.added)) + } +} + +func TestPresenceIsKeptForAWhileAndAskedOnceWhateverTheNumberOfAskers(t *testing.T) { + ctx := context.Background() + s, sw, rem := newService() + now := time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC) + s.now = func() time.Time { return now } + rem.artist = &Artist{ID: 7, Monitored: true} + rem.artistDelay = 40 * time.Millisecond + + var wg sync.WaitGroup + for i := 0; i < 20; i++ { + wg.Add(1) + go func() { defer wg.Done(); _, _ = s.Presence(ctx, 1) }() + } + wg.Wait() + if n := atomic.LoadInt32(&rem.asked); n != 1 { + t.Errorf("Lidarr was asked %d times by 20 at once, want 1", n) + } + rem.artistDelay = 0 + _, _ = s.Presence(ctx, 1) + if n := atomic.LoadInt32(&rem.asked); n != 1 { + t.Errorf("a second ask within the time asked Lidarr again (%d)", n) + } + // After a send the answer is not the one that was kept. + if _, err := s.Send(ctx, 1); err != nil { + t.Fatal(err) + } + _, _ = s.Presence(ctx, 1) + if n := atomic.LoadInt32(&rem.asked); n != 2 { + t.Errorf("after a send Lidarr was asked %d times in all, want 2", n) + } + now = now.Add(PresenceTTL + time.Second) + _, _ = s.Presence(ctx, 1) + if n := atomic.LoadInt32(&rem.asked); n != 3 { + t.Errorf("after the time Lidarr was asked %d times in all, want 3", n) + } + // Another address is another Lidarr. + sw.set.URL = "http://elsewhere:8686" + _, _ = s.Presence(ctx, 1) + if n := atomic.LoadInt32(&rem.asked); n != 4 { + t.Errorf("with another address Lidarr was asked %d times in all, want 4", n) + } + // What cannot be answered is not kept: the next ask tries again. + rem.artistEr = ErrUnreachable + now = now.Add(PresenceTTL + time.Second) + _, _ = s.Presence(ctx, 1) + _, _ = s.Presence(ctx, 1) + if n := atomic.LoadInt32(&rem.asked); n != 6 { + t.Errorf("a failure was kept: Lidarr was asked %d times in all, want 6", n) + } + // And a provider that is off is not asked, kept or not. + rem.artistEr = nil + sw.on = false + before := atomic.LoadInt32(&rem.asked) + if p, _ := s.Presence(ctx, 1); p.Status != discovery.StatusDisabled || atomic.LoadInt32(&rem.asked) != before { + t.Errorf("off: %+v", p) + } +} + +func TestOnlyAFewAskLidarrAtTheSameTime(t *testing.T) { + s, _, rem := newService() + rem.artistDelay = 30 * time.Millisecond + var wg sync.WaitGroup + for id := int64(10); id < 30; id++ { + wg.Add(1) + go func() { defer wg.Done(); _, _ = s.Presence(context.Background(), id) }() + } + wg.Wait() + if m := atomic.LoadInt32(&rem.artistMax); m > MaxAsking || m < 2 { + t.Errorf("%d asks at the same time, want at most %d (and more than one)", m, MaxAsking) + } + if n := atomic.LoadInt32(&rem.asked); n != 20 { + t.Errorf("asked %d times, want once for each of the 20 artists", n) + } +} + +func TestPresenceGivesUpWhenTheRequestDoes(t *testing.T) { + s, _, rem := newService() + rem.artistDelay = 200 * time.Millisecond + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) + defer cancel() + if _, err := s.Presence(ctx, 1); !errors.Is(err, context.DeadlineExceeded) { + t.Errorf("err = %v", err) + } +} + +func TestPresenceAndSendPassOnAFailureToReadTheSettings(t *testing.T) { + s, sw, _ := newService() + sw.err = errors.New("database is locked") + if _, err := s.Presence(context.Background(), 1); err == nil { + t.Error("Presence hid a failure to read the settings") + } +}