From fb9e92319773d35c3c9a99a834b76a59e44e0338 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 11:04:53 +0100 Subject: [PATCH] feat(lidarr): set up Lidarr as a provider and read what it offers Lidarr is a server of the user's own that looks for and downloads music. Sonda never downloads; this is the first piece of letting an admin send it an artist. - The `lidarr` provider, off by default: an address, an API key (32 hexadecimal characters, never returned) and, chosen once, a quality profile, a metadata profile, a root folder, the monitor option and whether to search for missing albums. It cannot be enabled without an address and a key. - The address is checked: http or https, no user and password, no query, no fragment, no closing slash. - A small client of its own, apart from the outbound one (which only makes plain GETs): the key travels in a header, no redirect is ever followed, answers and time are bounded, and the address and key are read for every request. Status, options, artist by MusicBrainz id and add (lookup, then the add that Lidarr's own screen sends). - `GET /providers/lidarr/options` lists the profiles and root folders; the test of the provider checks the address and the key (`no_address` when it has none). - Migration 0017 adds the address and the options to the provider settings. - OpenAPI, contract test and README say what is sent and where. --- README.md | 21 +- cmd/sondad/main.go | 15 + internal/api/http/api_test.go | 1 + internal/api/http/contract_test.go | 16 +- internal/api/http/handlers_providers.go | 117 ++++- internal/api/http/lidarr_test.go | 157 +++++++ internal/api/http/providers_test.go | 8 +- internal/api/http/router.go | 4 +- internal/api/http/router_test.go | 2 +- internal/api/openapi/openapi.yaml | 118 ++++- internal/providers/lidarr/lidarr.go | 434 +++++++++++++++++++ internal/providers/lidarr/lidarr_test.go | 375 ++++++++++++++++ internal/providers/lidarr_settings_test.go | 210 +++++++++ internal/providers/settings.go | 184 +++++++- internal/providers/settings_test.go | 6 +- internal/storage/providers.go | 23 +- internal/storage/providers_test.go | 35 ++ internal/storage/storage_test.go | 4 +- migrations/postgres/0017_lidarr_settings.sql | 9 + migrations/sqlite/0017_lidarr_settings.sql | 9 + 20 files changed, 1706 insertions(+), 42 deletions(-) create mode 100644 internal/api/http/lidarr_test.go create mode 100644 internal/providers/lidarr/lidarr.go create mode 100644 internal/providers/lidarr/lidarr_test.go create mode 100644 internal/providers/lidarr_settings_test.go create mode 100644 migrations/postgres/0017_lidarr_settings.sql create mode 100644 migrations/sqlite/0017_lidarr_settings.sql diff --git a/README.md b/README.md index 43cdaf2..79bf6a2 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ curl -s -X POST $BASE/users -H "$AUTH" -d '{"username":"bob","password":"another ## External providers -Sonda can ask Last.fm, MusicBrainz, Wikipedia, Wikimedia Commons and the Cover Art Archive about artists and albums. This is **off by default**: until an admin turns a provider on, nothing leaves your server. Today Last.fm is used for similar artists and tags, MusicBrainz for tags and for the link to Wikidata, Wikipedia (with Wikidata) for descriptions of artists, Wikimedia Commons (with Wikidata) for free photos of artists, and the Cover Art Archive for the cover of an album that has none in your files. +Sonda can ask Last.fm, MusicBrainz, Wikipedia, Wikimedia Commons and the Cover Art Archive about artists and albums, and can tell your own Lidarr which artist to add. This is **off by default**: until an admin turns a provider on, nothing leaves your server. Today Last.fm is used for similar artists and tags, MusicBrainz for tags and for the link to Wikidata, Wikipedia (with Wikidata) for descriptions of artists, Wikimedia Commons (with Wikidata) for free photos of artists, and the Cover Art Archive for the cover of an album that has none in your files. - **Last.fm** needs an API key. Create one for your own use at `last.fm/api` (it needs a Last.fm account). No user name or password is involved and the "shared secret" is not needed. Last.fm data is for non-commercial use only, and you should credit Last.fm when you show it. - **MusicBrainz** needs no key, but it asks every client to identify itself with a contact (a URL or an e-mail address) in the `User-Agent`. By default Sonda uses the address of the project, so no personal address is sent unless you choose to. @@ -203,9 +203,21 @@ curl -s -X POST $BASE/providers/lastfm/test -H "$AUTH" | jq # does the key wor curl -s -X PUT $BASE/providers/musicbrainz -H "$AUTH" -d '{"enabled":true,"contact":"https://example.org/me"}' ``` +**Lidarr** is not a source of data but a server of your own that looks for and downloads music. Sonda never downloads, searches for or chooses music: with the `lidarr` provider an admin can send an artist, by its MusicBrainz id and never by name, to *your* Lidarr, which then does what you set it up to do. Nothing is sent by itself, and only an admin sends. It needs the address of your Lidarr and its API key (Settings, General), then you pick, once, from what Lidarr offers: + +```sh +curl -s -X PUT $BASE/providers/lidarr -H "$AUTH" -d '{"url":"http://orion:8686","api_key":""}' +curl -s -X POST $BASE/providers/lidarr/test -H "$AUTH" | jq # does the key work? +curl -s $BASE/providers/lidarr/options -H "$AUTH" | jq # profiles and root folders to choose from +curl -s -X PUT $BASE/providers/lidarr -H "$AUTH" -d '{"lidarr":{"quality_profile_id":1,"metadata_profile_id":1,"root_folder_path":"/music","monitor":"all","search_missing":true}}' +curl -s -X PUT $BASE/providers/lidarr -H "$AUTH" -d '{"enabled":true}' +``` + +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. `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`. 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; 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 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. @@ -250,7 +262,8 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | admin | `GET`, `POST /libraries`; `PATCH`, `DELETE /libraries/{id}` | Manage libraries. Deleting one removes its index entries, never your files. | | admin | `POST`, `DELETE /library/covers`; `POST`, `DELETE /library/artist-images`; `POST`, `DELETE /library/descriptions` | Start, in the background, a job that builds the cover of every album, looks for the picture of every artist or for the description of every artist (`?refresh=true` for all), or stop it. One of each at a time. | | 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 Last.fm and MusicBrainz, and remove what they gave. The API key is write-only. | +| 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 | `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. @@ -299,7 +312,7 @@ internal/catalog/ browsing and searching artists, albums, tracks internal/library/ libraries, the scanner, the event hub, the tag reader internal/about/ descriptions of artists, from Wikipedia through MusicBrainz and Wikidata internal/discovery/ similar artists: what the providers said, kept and refreshed -internal/providers/ external providers: settings, the outbound HTTP client, the Last.fm and MusicBrainz clients +internal/providers/ external providers: settings, the outbound HTTP client, the clients of the providers 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 diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index d0940fb..d89a73c 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -30,6 +30,7 @@ import ( "codeberg.org/squidd/sonda/internal/providers/commons" "codeberg.org/squidd/sonda/internal/providers/coverart" "codeberg.org/squidd/sonda/internal/providers/lastfm" + "codeberg.org/squidd/sonda/internal/providers/lidarr" "codeberg.org/squidd/sonda/internal/providers/musicbrainz" "codeberg.org/squidd/sonda/internal/providers/outbound" "codeberg.org/squidd/sonda/internal/providers/wikipedia" @@ -202,6 +203,19 @@ func run() error { return coverartClient.Probe(ctx) }) pictures.SetAlbumRemote(&images.CoverArtSource{Albums: imageRepo, Settings: providerSettings, Client: coverartClient}) + // Lidarr is the admin's own server: it is read from the stored settings for every request, on or off, + // since setting it up (a test, what it offers) happens before it is switched on. + lidarrClient := lidarr.New(func(ctx context.Context) (lidarr.Config, error) { + s, err := providerSettings.Stored(ctx, providers.Lidarr) + if err != nil { + return lidarr.Config{}, err + } + if s.URL == "" || s.APIKey == "" { + return lidarr.Config{}, lidarr.ErrNotConfigured + } + return lidarr.Config{URL: s.URL, APIKey: s.APIKey}, nil + }, providerSettings.UserAgentFor(providers.Lidarr, version), nil) + providerSettings.SetChecker(providers.Lidarr, func(ctx context.Context, _ string) error { return lidarrClient.Probe(ctx) }) providerSettings.SetPurger(func(ctx context.Context, name string) (providers.Purged, error) { switch name { case providers.Commons: @@ -255,6 +269,7 @@ func run() error { Libraries: libraries, Catalog: catalog.NewService(storage.NewCatalogRepo(db)), Providers: providerSettings, + Lidarr: lidarrClient, Discovery: discoveries, Tags: tags, Taste: tasteService, diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index bbdc71f..24ef8ad 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -93,6 +93,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun 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}), diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index c7af981..df0f78a 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -426,9 +426,21 @@ func TestResponsesMatchTheSpec(t *testing.T) { // ---- providers ---- listed := c.expect(c.call("GET", "/providers", token, nil), 200, "list providers")["providers"].([]any) - if len(listed) != 5 || listed[0].(map[string]any)["enabled"] != false || listed[1].(map[string]any)["enabled"] != false || listed[2].(map[string]any)["enabled"] != false || listed[3].(map[string]any)["enabled"] != false || listed[4].(map[string]any)["enabled"] != false { - t.Fatalf("providers = %v, want five, all off", listed) + if len(listed) != 6 || listed[0].(map[string]any)["enabled"] != false || listed[1].(map[string]any)["enabled"] != false || listed[2].(map[string]any)["enabled"] != false || listed[3].(map[string]any)["enabled"] != false || listed[4].(map[string]any)["enabled"] != false || listed[5].(map[string]any)["enabled"] != false { + t.Fatalf("providers = %v, want six, all off", listed) } + // ---- lidarr ---- + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": true}), 400, "enable lidarr with nothing", "invalid_input") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"url": "ftp://orion"}), 400, "lidarr with a bad address", "invalid_input") + c.expect(c.call("PUT", "/providers/musicbrainz", token, map[string]any{"url": "http://orion:8686"}), 400, "an address for a provider that has none", "invalid_input") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"api_key": "0123456789abcdef0123456789abcdef", "url": "http://orion:8686/"}), 200, "set up lidarr") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"lidarr": map[string]any{"quality_profile_id": 2, "metadata_profile_id": 1, "root_folder_path": "/music", "monitor": "latest"}}), 200, "lidarr options") + c.expect(c.call("GET", "/providers/lidarr/options", token, nil), 200, "what lidarr offers") + c.expect(c.call("GET", "/providers/lidarr/options", "", nil), 401, "lidarr options without a token", "unauthorized") + c.expect(c.call("PUT", "/providers/lidarr", token, map[string]any{"enabled": true}), 200, "enable lidarr") + 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") + // ---- the other providers ---- c.expect(c.call("PUT", "/providers/lastfm", token, map[string]any{"enabled": true}), 400, "enable without a key", "invalid_input") c.expect(c.call("PUT", "/providers/lastfm", token, map[string]any{"api_key": "short"}), 400, "a bad key", "invalid_input") c.expect(c.call("PUT", "/providers/spotify", token, map[string]any{"enabled": true}), 404, "unknown provider", "provider_not_found") diff --git a/internal/api/http/handlers_providers.go b/internal/api/http/handlers_providers.go index 8de50ab..44fedc9 100644 --- a/internal/api/http/handlers_providers.go +++ b/internal/api/http/handlers_providers.go @@ -1,33 +1,56 @@ package httpapi import ( + "context" "errors" "net/http" "github.com/gin-gonic/gin" "codeberg.org/squidd/sonda/internal/providers" + "codeberg.org/squidd/sonda/internal/providers/lidarr" ) // providersHandlers are the admin-only routes that set up the external // providers. The API key can be written here but is never returned. -type providersHandlers struct{ svc providerService } +type providersHandlers struct { + svc providerService + lidarr lidarrOptionsService +} type providerJSON struct { Name string `json:"name"` Enabled bool `json:"enabled"` NeedsAPIKey bool `json:"needs_api_key"` + NeedsURL bool `json:"needs_url"` HasAPIKey bool `json:"has_api_key"` Contact string `json:"contact"` + URL string `json:"url"` Sends string `json:"sends"` Notes string `json:"notes"` UpdatedAt *string `json:"updated_at"` + // Lidarr is only there for the provider that has these options. + Lidarr *lidarrOptionsJSON `json:"lidarr,omitempty"` +} + +type lidarrOptionsJSON struct { + QualityProfileID int `json:"quality_profile_id"` + MetadataProfileID int `json:"metadata_profile_id"` + RootFolderPath string `json:"root_folder_path"` + Monitor string `json:"monitor"` + SearchMissing bool `json:"search_missing"` } func toProviderJSON(in providers.Info) providerJSON { out := providerJSON{ - Name: in.Name, Enabled: in.Enabled, NeedsAPIKey: in.NeedsAPIKey, HasAPIKey: in.HasAPIKey, - Contact: in.Contact, Sends: in.Sends, Notes: in.Notes, + Name: in.Name, Enabled: in.Enabled, NeedsAPIKey: in.NeedsAPIKey, NeedsURL: in.NeedsURL, HasAPIKey: in.HasAPIKey, + Contact: in.Contact, URL: in.URL, Sends: in.Sends, Notes: in.Notes, + } + if in.NeedsURL { + out.Lidarr = &lidarrOptionsJSON{ + QualityProfileID: in.Lidarr.QualityProfileID, MetadataProfileID: in.Lidarr.MetadataProfileID, + RootFolderPath: in.Lidarr.RootFolderPath, Monitor: in.Lidarr.Monitor, SearchMissing: in.Lidarr.SearchMissing, + } } if !in.UpdatedAt.IsZero() { out.UpdatedAt = rfc3339(in.UpdatedAt) @@ -66,6 +89,15 @@ type updateProviderRequest struct { Enabled *bool `json:"enabled"` // pointers: an absent field is left alone APIKey *string `json:"api_key"` Contact *string `json:"contact"` + URL *string `json:"url"` + // Lidarr replaces all the options at once; what it does not say is the default. + Lidarr *struct { + QualityProfileID int `json:"quality_profile_id"` + MetadataProfileID int `json:"metadata_profile_id"` + RootFolderPath string `json:"root_folder_path"` + Monitor string `json:"monitor"` + SearchMissing *bool `json:"search_missing"` + } `json:"lidarr"` } func (h *providersHandlers) update(c *gin.Context) { @@ -73,8 +105,21 @@ func (h *providersHandlers) update(c *gin.Context) { if !bindJSON(c, &req) { return } - info, err := h.svc.Update(c.Request.Context(), c.Param("name"), - providers.Patch{Enabled: req.Enabled, APIKey: req.APIKey, Contact: req.Contact}) + patch := providers.Patch{Enabled: req.Enabled, APIKey: req.APIKey, Contact: req.Contact, URL: req.URL} + if l := req.Lidarr; l != nil { + o := providers.LidarrOptions{ + QualityProfileID: l.QualityProfileID, MetadataProfileID: l.MetadataProfileID, + RootFolderPath: l.RootFolderPath, Monitor: l.Monitor, SearchMissing: true, + } + if o.Monitor == "" { + o.Monitor = "all" + } + if l.SearchMissing != nil { + o.SearchMissing = *l.SearchMissing + } + patch.Lidarr = &o + } + info, err := h.svc.Update(c.Request.Context(), c.Param("name"), patch) if err != nil { writeProviderError(c, err) return @@ -99,3 +144,65 @@ func (h *providersHandlers) purge(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{"similar_artists": res.SimilarArtists, "tags": res.Tags, "descriptions": res.Descriptions, "images": res.Images, "artists": res.Artists}) } + +type lidarrOptionsService interface { + Options(ctx context.Context) (lidarr.Options, error) +} + +// writeLidarrError says what went wrong asking Lidarr, which is the admin's own server: not being +// set up is a conflict with the state of the settings, anything else is a bad gateway. +func writeLidarrError(c *gin.Context, err error) { + switch { + case errors.Is(err, lidarr.ErrNotConfigured): + writeError(c, http.StatusConflict, "lidarr_not_configured", "Lidarr has no address or no API key set") + case errors.Is(err, lidarr.ErrKeyRefused): + writeError(c, http.StatusBadGateway, "lidarr_key_refused", "Lidarr refused the API key") + case errors.Is(err, context.Canceled): + writeError(c, 499, "canceled", "request canceled") + case errors.Is(err, lidarr.ErrUnreachable), errors.Is(err, lidarr.ErrBadResponse): + _ = c.Error(err) + writeError(c, http.StatusBadGateway, "lidarr_unavailable", "Lidarr could not be asked") + default: + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + } +} + +type lidarrProfileJSON struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type lidarrFolderJSON struct { + Path string `json:"path"` + Accessible bool `json:"accessible"` +} + +// lidarrOptions lists what Lidarr offers, for an admin to choose from. It works whether Lidarr is +// switched on or not, like a test of the key: the admin needs it to finish setting it up. +func (h *providersHandlers) lidarrOptions(c *gin.Context) { + o, err := h.lidarr.Options(c.Request.Context()) + if err != nil { + writeLidarrError(c, err) + return + } + out := gin.H{ + "quality_profiles": make([]lidarrProfileJSON, 0, len(o.QualityProfiles)), + "metadata_profiles": make([]lidarrProfileJSON, 0, len(o.MetadataProfiles)), + "root_folders": make([]lidarrFolderJSON, 0, len(o.RootFolders)), + } + q := out["quality_profiles"].([]lidarrProfileJSON) + for _, p := range o.QualityProfiles { + q = append(q, lidarrProfileJSON{ID: p.ID, Name: p.Name}) + } + m := out["metadata_profiles"].([]lidarrProfileJSON) + for _, p := range o.MetadataProfiles { + m = append(m, lidarrProfileJSON{ID: p.ID, Name: p.Name}) + } + f := out["root_folders"].([]lidarrFolderJSON) + for _, r := range o.RootFolders { + f = append(f, lidarrFolderJSON{Path: r.Path, Accessible: r.Accessible}) + } + out["quality_profiles"], out["metadata_profiles"], out["root_folders"] = q, m, f + c.JSON(http.StatusOK, out) +} diff --git a/internal/api/http/lidarr_test.go b/internal/api/http/lidarr_test.go new file mode 100644 index 0000000..c435384 --- /dev/null +++ b/internal/api/http/lidarr_test.go @@ -0,0 +1,157 @@ +package httpapi + +import ( + "context" + "encoding/json" + "net/http" + "strings" + "testing" + + "github.com/uptrace/bun" + + "codeberg.org/squidd/sonda/internal/providers/lidarr" +) + +const lidarrAPIKey = "fedcba9876543210fedcba9876543210" + +type fakeLidarr struct { + options lidarr.Options + err error + asked int +} + +func (f *fakeLidarr) Options(context.Context) (lidarr.Options, error) { + f.asked++ + return f.options, f.err +} + +func newLidarrAPI(t *testing.T) (http.Handler, *fakeLidarr, string, string) { + t.Helper() + fake := &fakeLidarr{options: lidarr.Options{ + QualityProfiles: []lidarr.Profile{{ID: 1, Name: "Any"}, {ID: 2, Name: "Lossless"}}, + MetadataProfiles: []lidarr.Profile{{ID: 1, Name: "Standard"}}, + RootFolders: []lidarr.RootFolder{{Path: "/music", Accessible: true}}, + }} + h, _ := newTestEnvDB(t, func(d *Deps, _ *bun.DB) { d.Lidarr = fake }) + admin := registerAndLogin(t, h) + _, bob := addUser(t, h, admin, "bob", false) + return h, fake, admin, bob +} + +func TestLidarrIsListedLastWithItsAddressAndOptions(t *testing.T) { + h, _, admin, _ := newLidarrAPI(t) + list := do(t, h, "GET", "/api/v1/providers", admin, nil).Body["providers"].([]any) + if len(list) != 6 { + t.Fatalf("providers = %v", list) + } + for _, it := range list[:5] { + p := it.(map[string]any) + if p["needs_url"] != false || p["url"] != "" || p["lidarr"] != nil { + t.Errorf("%v has an address or Lidarr options: %v", p["name"], p) + } + } + l := list[5].(map[string]any) + sameKeys(t, "lidarr", l, "name", "enabled", "needs_api_key", "needs_url", "has_api_key", "contact", "url", "lidarr", "sends", "notes", "updated_at") + opts, _ := l["lidarr"].(map[string]any) + sameKeys(t, "lidarr options", opts, "quality_profile_id", "metadata_profile_id", "root_folder_path", "monitor", "search_missing") + if l["name"] != "lidarr" || l["enabled"] != false || l["needs_api_key"] != true || l["needs_url"] != true || + opts["monitor"] != "all" || opts["search_missing"] != true || opts["quality_profile_id"] != float64(0) { + t.Errorf("lidarr = %v", l) + } +} + +func TestSettingLidarrUp(t *testing.T) { + h, _, admin, _ := newLidarrAPI(t) + put := func(body map[string]any) resp { return do(t, h, "PUT", "/api/v1/providers/lidarr", admin, body) } + + if r := put(map[string]any{"enabled": true}); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Fatalf("enabled with nothing: %d %v", r.Code, r.Body) + } + r := put(map[string]any{"api_key": lidarrAPIKey, "url": "http://orion:8686/"}) + if r.Code != http.StatusOK || r.Body["url"] != "http://orion:8686" || r.Body["has_api_key"] != true || r.Body["enabled"] != false { + t.Fatalf("key and address: %d %v", r.Code, r.Body) + } + if raw, _ := json.Marshal(r.Body); strings.Contains(string(raw), lidarrAPIKey) { + t.Fatal("the response carries the API key") + } + // What is not said in the options is the default; what is said replaces all of them. + r = put(map[string]any{"lidarr": map[string]any{"quality_profile_id": 2, "metadata_profile_id": 1, "root_folder_path": "/music"}}) + opts, _ := r.Body["lidarr"].(map[string]any) + if r.Code != http.StatusOK || opts["quality_profile_id"] != float64(2) || opts["root_folder_path"] != "/music" || + opts["monitor"] != "all" || opts["search_missing"] != true { + t.Fatalf("options: %d %v", r.Code, r.Body) + } + r = put(map[string]any{"lidarr": map[string]any{"quality_profile_id": 2, "metadata_profile_id": 1, "root_folder_path": "/music", "monitor": "latest", "search_missing": false}}) + opts, _ = r.Body["lidarr"].(map[string]any) + if opts["monitor"] != "latest" || opts["search_missing"] != false { + t.Fatalf("options: %v", r.Body) + } + if r := put(map[string]any{"enabled": true}); r.Code != http.StatusOK || r.Body["enabled"] != true { + t.Fatalf("enable: %d %v", r.Code, r.Body) + } + for name, body := range map[string]map[string]any{ + "an address that is not http": {"url": "ftp://orion"}, + "an address with a secret": {"url": "http://orion/?apikey=x"}, + "a short key": {"api_key": "abc"}, + "a monitor that is not one": {"lidarr": map[string]any{"monitor": "everything"}}, + } { + if r := put(body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { + t.Errorf("%s: %d %v", name, r.Code, r.Body) + } + } + // Clearing the address turns it off. + if r := put(map[string]any{"url": ""}); r.Code != http.StatusOK || r.Body["enabled"] != false || r.Body["url"] != "" { + t.Errorf("clear the address: %d %v", r.Code, r.Body) + } + // Other providers have neither. + if r := do(t, h, "PUT", "/api/v1/providers/musicbrainz", admin, map[string]any{"url": "http://orion"}); r.Code != http.StatusBadRequest { + t.Errorf("an address for MusicBrainz: %d %v", r.Code, r.Body) + } +} + +func TestOnlyAnAdminReadsWhatLidarrOffers(t *testing.T) { + h, fake, admin, bob := newLidarrAPI(t) + if r := do(t, h, "GET", "/api/v1/providers/lidarr/options", "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("without a token: %d", r.Code) + } + if r := do(t, h, "GET", "/api/v1/providers/lidarr/options", bob, nil); r.Code != http.StatusForbidden || errCode(r) != "admin_required" { + t.Errorf("as a user: %d %v", r.Code, r.Body) + } + if fake.asked != 0 { + t.Error("Lidarr was asked on behalf of someone who is not an admin") + } + r := do(t, h, "GET", "/api/v1/providers/lidarr/options", admin, nil) + if r.Code != http.StatusOK { + t.Fatalf("%d %v", r.Code, r.Body) + } + sameKeys(t, "options", r.Body, "quality_profiles", "metadata_profiles", "root_folders") + q := r.Body["quality_profiles"].([]any) + if len(q) != 2 || q[1].(map[string]any)["id"] != float64(2) || q[1].(map[string]any)["name"] != "Lossless" { + t.Errorf("quality profiles = %v", q) + } + sameKeys(t, "profile", q[0].(map[string]any), "id", "name") + f := r.Body["root_folders"].([]any)[0].(map[string]any) + sameKeys(t, "root folder", f, "path", "accessible") + if f["path"] != "/music" || f["accessible"] != true { + t.Errorf("root folder = %v", f) + } +} + +func TestWhatGoesWrongAskingLidarrIsSaidPlainly(t *testing.T) { + h, fake, admin, _ := newLidarrAPI(t) + for _, c := range []struct { + err error + status int + code string + }{ + {lidarr.ErrNotConfigured, http.StatusConflict, "lidarr_not_configured"}, + {lidarr.ErrKeyRefused, http.StatusBadGateway, "lidarr_key_refused"}, + {lidarr.ErrUnreachable, http.StatusBadGateway, "lidarr_unavailable"}, + {lidarr.ErrBadResponse, http.StatusBadGateway, "lidarr_unavailable"}, + } { + fake.err = c.err + if r := do(t, h, "GET", "/api/v1/providers/lidarr/options", 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) + } + } +} diff --git a/internal/api/http/providers_test.go b/internal/api/http/providers_test.go index 13ed45d..4681b8f 100644 --- a/internal/api/http/providers_test.go +++ b/internal/api/http/providers_test.go @@ -46,12 +46,16 @@ func TestProvidersStartOffAndSayWhatTheySend(t *testing.T) { t.Fatalf("list: %d %v", r.Code, r.Body) } list := r.Body["providers"].([]any) - if len(list) != 5 { + if len(list) != 6 { t.Fatalf("providers = %v", list) } for _, it := range list { p := it.(map[string]any) - sameKeys(t, "provider", p, "name", "enabled", "needs_api_key", "has_api_key", "contact", "sends", "notes", "updated_at") + keys := []string{"name", "enabled", "needs_api_key", "needs_url", "has_api_key", "contact", "url", "sends", "notes", "updated_at"} + if p["name"] == "lidarr" { + keys = append(keys, "lidarr") + } + sameKeys(t, "provider", p, keys...) if p["enabled"] != false || p["has_api_key"] != false { t.Errorf("%v: a provider must start off and without a key", p["name"]) } diff --git a/internal/api/http/router.go b/internal/api/http/router.go index 00b6e84..8efec57 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -143,6 +143,7 @@ type Deps struct { Libraries libraryService Catalog catalogService Providers providerService + Lidarr lidarrOptionsService Discovery discoveryService Tags tagService Taste tasteService @@ -272,9 +273,10 @@ func NewRouter(d Deps) *gin.Engine { admin.PATCH("/libraries/:id", libs.update) admin.DELETE("/libraries/:id", libs.remove) admin.POST("/libraries/:id/scan", lib.startLibraryScan) - prov := &providersHandlers{svc: d.Providers} + prov := &providersHandlers{svc: d.Providers, lidarr: d.Lidarr} admin.GET("/providers", prov.list) admin.PUT("/providers/:name", prov.update) + admin.GET("/providers/lidarr/options", prov.lidarrOptions) 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 74db602..cff068e 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", "ArtistImages", "About", "Streams"} { + for _, want := range []string{"Auth", "Tokens", "Listening", "CoversJob", "ArtistImagesJob", "DescriptionsJob", "Lidarr", "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 1fc67ed..7cf21a4 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -1679,7 +1679,7 @@ paths: operationId: listProviders summary: List the external providers description: | - Admin only. Every provider Sonda knows (Last.fm, MusicBrainz), with whether it is on and + Admin only. Every provider Sonda knows (Last.fm, MusicBrainz, Wikipedia, Commons, the Cover Art Archive and Lidarr), with whether it is on and what it would send. Providers are off by default: nothing leaves the server until an admin enables one. The API key is never returned; `has_api_key` says whether one is stored. security: [{bearerAuth: []}] @@ -1699,18 +1699,48 @@ paths: '401': {$ref: '#/components/responses/Unauthorized'} '403': {$ref: '#/components/responses/AdminRequired'} + /providers/lidarr/options: + get: + tags: [Providers] + operationId: getLidarrOptions + summary: What Lidarr offers to choose from + description: | + Admin only. Reads Lidarr's quality profiles, metadata profiles and root folders, so an admin can pick + the ones to send artists with (`PUT /providers/lidarr`, field `lidarr`). It works whether Lidarr is + switched on or not, since it is needed to finish setting it up, and it sends nothing but the API key, + to the address set for Lidarr, which is your own server. + security: [{bearerAuth: []}] + responses: + '200': + description: What Lidarr offers. + content: + application/json: + schema: {$ref: '#/components/schemas/LidarrOffers'} + '401': {$ref: '#/components/responses/Unauthorized'} + '403': {$ref: '#/components/responses/AdminRequired'} + '409': + description: Lidarr has no address or no API key yet (`lidarr_not_configured`). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + '502': + description: Lidarr refused the key (`lidarr_key_refused`), or could not be asked or answered something unexpected (`lidarr_unavailable`). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + /providers/{name}: parameters: - name: name in: path required: true - schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive]} + schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive, lidarr]} put: tags: [Providers] operationId: updateProvider summary: Set up a provider description: | - Admin only. Send any of `enabled`, `api_key` and `contact`; fields you leave out are kept. + Admin only. Send any of `enabled`, `api_key`, `contact`, `url` and `lidarr`; fields you leave out are kept. The key is write-only: it is never returned and never logged. Sending an empty `api_key` clears it, and turns a provider that needs a key (Last.fm) off; a provider that needs none @@ -1742,7 +1772,7 @@ paths: - name: name in: path required: true - schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive]} + schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive, lidarr]} post: tags: [Providers] operationId: testProvider @@ -1777,7 +1807,7 @@ paths: - name: name in: path required: true - schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive]} + schema: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive, lidarr]} delete: tags: [Providers] operationId: purgeProviderData @@ -2236,13 +2266,16 @@ components: Provider: type: object additionalProperties: false - required: [name, enabled, needs_api_key, has_api_key, contact, sends, notes, updated_at] + required: [name, enabled, needs_api_key, needs_url, has_api_key, contact, url, sends, notes, updated_at] properties: - name: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive]} + name: {type: string, enum: [lastfm, musicbrainz, wikipedia, commons, coverartarchive, lidarr]} enabled: {type: boolean, description: Whether Sonda may contact this provider.} needs_api_key: {type: boolean} + needs_url: {type: boolean, description: "Whether the provider is a server of your own that needs an address (only Lidarr)."} has_api_key: {type: boolean, description: Whether a key is stored. The key itself is never returned.} contact: {type: string, description: Contact in the User-Agent; empty means the project address.} + url: {type: string, description: "Where your own server is, without a closing slash. Empty for the providers that have none."} + lidarr: {$ref: '#/components/schemas/LidarrOptions'} sends: {type: string, description: "Plain statement of what is sent to the provider, and what never is."} notes: {type: string, description: Terms of use and attribution that apply to the provider's data.} updated_at: @@ -2251,18 +2284,83 @@ components: nullable: true description: When the settings last changed; null if they never did. + LidarrOptions: + type: object + additionalProperties: false + description: | + Only for `lidarr`: what is sent to Lidarr when an artist is added, chosen once by an admin from + `GET /providers/lidarr/options`. Until they are saved, `monitor` is `all`, `search_missing` is + true and the rest is empty or 0, which is not enough to add an artist. + required: [quality_profile_id, metadata_profile_id, root_folder_path, monitor, search_missing] + properties: + quality_profile_id: {type: integer, minimum: 0, description: "Lidarr's quality profile; 0 when none is chosen."} + metadata_profile_id: {type: integer, minimum: 0, description: "Lidarr's metadata profile; 0 when none is chosen."} + root_folder_path: {type: string, maxLength: 500, description: "One of Lidarr's root folders; empty when none is chosen."} + monitor: + type: string + enum: [all, future, missing, existing, latest, first, none] + description: "Which of the artist's albums Lidarr monitors when it is added. `none` also stops new albums from being monitored." + search_missing: {type: boolean, description: Whether Lidarr starts looking for the albums as soon as the artist is added.} + + LidarrOffers: + type: object + additionalProperties: false + required: [quality_profiles, metadata_profiles, root_folders] + properties: + quality_profiles: + type: array + items: {$ref: '#/components/schemas/LidarrProfile'} + metadata_profiles: + type: array + items: {$ref: '#/components/schemas/LidarrProfile'} + root_folders: + type: array + items: + type: object + additionalProperties: false + required: [path, accessible] + properties: + path: {type: string} + accessible: {type: boolean, description: Whether Lidarr can use the folder now.} + + LidarrProfile: + type: object + additionalProperties: false + required: [id, name] + properties: + id: {type: integer} + name: {type: string} + ProviderUpdate: type: object anyOf: - required: [enabled] - required: [api_key] - required: [contact] + - required: [url] + - required: [lidarr] properties: enabled: {type: boolean} api_key: type: string format: password - description: A Last.fm key is 32 hexadecimal characters. Empty clears it. + description: A Last.fm or Lidarr key is 32 hexadecimal characters. Empty clears it. + url: + type: string + maxLength: 300 + description: | + Only for Lidarr: where your own server is, `http://` or `https://`, with a port and a path if it + has them, and without user and password, query or fragment. A closing slash is dropped. Empty + clears it, and turns Lidarr off. Lidarr cannot be enabled without an address and a key. + lidarr: + description: "Only for Lidarr. Replaces all its options at once: what is left out is the default (`monitor` all, `search_missing` true, the rest empty or 0)." + type: object + properties: + quality_profile_id: {type: integer, minimum: 0} + metadata_profile_id: {type: integer, minimum: 0} + root_folder_path: {type: string, maxLength: 500} + monitor: {type: string, enum: [all, future, missing, existing, latest, first, none]} + search_missing: {type: boolean} contact: type: string maxLength: 200 @@ -2333,9 +2431,9 @@ components: ok: {type: boolean} status: type: string - enum: [ok, no_api_key, key_refused, rate_limited, unreachable, error] + enum: [ok, no_api_key, no_address, key_refused, rate_limited, unreachable, error] description: | - `no_api_key`: none is stored, so nobody was asked. `key_refused`: the provider does not accept + `no_api_key`: none is stored, so nobody was asked. `no_address`: Lidarr has no address yet, so nobody was asked. `key_refused`: the provider does not accept it. `rate_limited`: try again later. `unreachable`: the provider did not answer. `error`: anything else. ArtistTags: diff --git a/internal/providers/lidarr/lidarr.go b/internal/providers/lidarr/lidarr.go new file mode 100644 index 0000000..a0241a3 --- /dev/null +++ b/internal/providers/lidarr/lidarr.go @@ -0,0 +1,434 @@ +// Package lidarr is a small client for the Lidarr API of the user's own server. +// +// Lidarr is not a provider of data like the others: it is a program the user runs, which looks for +// and downloads music by itself. Sonda only tells it which artist to add, by MusicBrainz id and +// never by name, when an admin asks. That is why this client does not go through the outbound +// client, which is built for strangers' services (plain GETs, no authentication). This one talks +// to one address that an admin set, sends the API key in a header, and: +// +// - never follows a redirect, so the key cannot be sent to another address; +// - reads the address and the key anew for every request, so a change by an admin applies at once; +// - bounds the time of a request and the size of an answer, and does not retry (adding an artist +// twice is harmless, and a person is waiting for the answer); +// - never puts the key in an address or in an error. +// +// The shapes of the requests and answers were read in Lidarr's own sources (see +// ~/docs/plans/sonda/m5-lidarr-design.md). +package lidarr + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "regexp" + "slices" + "strings" + "time" + "unicode" + "unicode/utf8" + + "codeberg.org/squidd/sonda/internal/providers" +) + +// Errors a call can return, to be matched with errors.Is. +var ( + ErrNotConfigured = errors.New("lidarr: no address or no API key set") + ErrKeyRefused = errors.New("lidarr: the API key was refused") + ErrUnreachable = errors.New("lidarr: the server could not be reached") + ErrBadResponse = errors.New("lidarr: unexpected answer") + ErrBadRequest = errors.New("lidarr: not something that can be sent") + ErrNotFound = errors.New("lidarr: it does not know this artist") + ErrAlreadyAdded = errors.New("lidarr: the artist is already there") + ErrRejected = errors.New("lidarr: the artist was refused") +) + +const ( + timeout = 20 * time.Second + maxBody = 4 << 20 + maxReason = 200 +) + +var idRe = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) + +// Config is where Lidarr is and how to speak to it. +type Config struct { + URL string // without a closing slash, may carry a path (a URL base) + APIKey string +} + +// ConfigFunc returns the current Config, or ErrNotConfigured when there is none to use. +type ConfigFunc func(ctx context.Context) (Config, error) + +// Client talks to Lidarr. It is safe for concurrent use. +type Client struct { + cfg ConfigFunc + userAgent func() string + hc *http.Client +} + +// New builds a Client. userAgent may be nil; rt may be nil (the default transport). +func New(cfg ConfigFunc, userAgent func() string, rt http.RoundTripper) *Client { + if userAgent == nil { + userAgent = func() string { return providers.UserAgent("", "") } + } + return &Client{ + cfg: cfg, + userAgent: userAgent, + hc: &http.Client{ + Transport: rt, // no cookie jar + CheckRedirect: func(*http.Request, []*http.Request) error { + return http.ErrUseLastResponse // reported as a bad answer: the key must not follow it + }, + }, + } +} + +// do sends one request and returns the status and the body of the answer. +func (c *Client) do(ctx context.Context, method, path string, query url.Values, body []byte) (int, []byte, error) { + cfg, err := c.cfg(ctx) + if err != nil { + return 0, nil, err + } + if cfg.URL == "" || cfg.APIKey == "" { + return 0, nil, ErrNotConfigured + } + target := cfg.URL + path + if len(query) > 0 { + target += "?" + query.Encode() + } + actx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + var rd io.Reader + if body != nil { + rd = bytes.NewReader(body) + } + req, err := http.NewRequestWithContext(actx, method, target, rd) + if err != nil { + return 0, nil, ErrNotConfigured // the address stored cannot be used + } + req.Header.Set("X-Api-Key", cfg.APIKey) + req.Header.Set("User-Agent", c.userAgent()) + req.Header.Set("Accept", "application/json") + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + resp, err := c.hc.Do(req) + if err != nil { + if ctx.Err() != nil { + return 0, nil, ctx.Err() + } + var ue *url.Error + if errors.As(err, &ue) { + err = ue.Err // the address is not repeated: only what went wrong + } + return 0, nil, fmt.Errorf("%w: %v", ErrUnreachable, err) + } + defer resp.Body.Close() + if resp.StatusCode >= 300 && resp.StatusCode < 400 { + return 0, nil, fmt.Errorf("%w: a redirect, which is never followed", ErrBadResponse) + } + data, err := io.ReadAll(io.LimitReader(resp.Body, maxBody+1)) + if err != nil { + if ctx.Err() != nil { + return 0, nil, ctx.Err() + } + return 0, nil, fmt.Errorf("%w: %v", ErrUnreachable, err) + } + if len(data) > maxBody { + return 0, nil, fmt.Errorf("%w: the answer is too large", ErrBadResponse) + } + return resp.StatusCode, data, nil +} + +// get sends a GET that must be answered with a JSON document, and decodes it into out. +func (c *Client) get(ctx context.Context, path string, query url.Values, out any) error { + status, data, err := c.do(ctx, http.MethodGet, path, query, nil) + if err != nil { + return err + } + if err := statusError(status); err != nil { + return err + } + dec := json.NewDecoder(bytes.NewReader(data)) + dec.UseNumber() + if err := dec.Decode(out); err != nil { + return fmt.Errorf("%w: not the document that was expected", ErrBadResponse) + } + return nil +} + +// statusError turns a status that is not a success into one of the errors. +func statusError(status int) error { + switch { + case status >= 200 && status < 300: + return nil + case status == http.StatusUnauthorized, status == http.StatusForbidden: + return ErrKeyRefused + } + return fmt.Errorf("%w: status %d", ErrBadResponse, status) +} + +// Status is what Lidarr says about itself. +type Status struct { + AppName string + Version string +} + +// Status asks Lidarr who it is: the cheapest way to find out that the address and the key work. +func (c *Client) Status(ctx context.Context) (Status, error) { + var doc struct { + AppName string `json:"appName"` + Version string `json:"version"` + } + if err := c.get(ctx, "/api/v1/system/status", nil, &doc); err != nil { + return Status{}, err + } + return Status{AppName: doc.AppName, Version: doc.Version}, nil +} + +// Probe is the test of the provider: it asks Lidarr who it is and answers in the terms of +// providers.Checker, nil or one of providers.ErrKeyRefused and providers.ErrUnreachable. +func (c *Client) Probe(ctx context.Context) error { + _, err := c.Status(ctx) + switch { + case err == nil: + return nil + case errors.Is(err, ErrKeyRefused): + return providers.ErrKeyRefused + case errors.Is(err, ErrUnreachable), errors.Is(err, ErrBadResponse): + return providers.ErrUnreachable + } + return err +} + +// Profile is a quality profile or a metadata profile of Lidarr. +type Profile struct { + ID int + Name string +} + +// RootFolder is a folder where Lidarr keeps music. +type RootFolder struct { + Path string + Accessible bool +} + +// Options is what an admin can choose from when setting up the artists that are sent. +type Options struct { + QualityProfiles []Profile + MetadataProfiles []Profile + RootFolders []RootFolder +} + +// Options reads the profiles and the root folders. If any of the three cannot be read the call +// fails: half a list would hide a choice. +func (c *Client) Options(ctx context.Context) (Options, error) { + var ( + out = Options{QualityProfiles: []Profile{}, MetadataProfiles: []Profile{}, RootFolders: []RootFolder{}} + profiles = func(path string, to *[]Profile) error { + var rows []struct { + ID int `json:"id"` + Name string `json:"name"` + } + if err := c.get(ctx, path, nil, &rows); err != nil { + return err + } + for _, r := range rows { + *to = append(*to, Profile{ID: r.ID, Name: clean(r.Name)}) + } + return nil + } + ) + if err := profiles("/api/v1/qualityprofile", &out.QualityProfiles); err != nil { + return Options{}, err + } + if err := profiles("/api/v1/metadataprofile", &out.MetadataProfiles); err != nil { + return Options{}, err + } + var folders []struct { + Path string `json:"path"` + Accessible bool `json:"accessible"` + } + if err := c.get(ctx, "/api/v1/rootfolder", nil, &folders); err != nil { + return Options{}, err + } + for _, f := range folders { + out.RootFolders = append(out.RootFolders, RootFolder{Path: clean(f.Path), Accessible: f.Accessible}) + } + return out, nil +} + +// Artist is an artist that Lidarr has. +type Artist struct { + ID int + Name string + Monitored bool + Path string +} + +// Artist returns the artist Lidarr has with this MusicBrainz id, or nil when it has none. +func (c *Client) Artist(ctx context.Context, mbid string) (*Artist, error) { + if !idRe.MatchString(mbid) { + return nil, ErrBadRequest + } + mbid = strings.ToLower(mbid) + var rows []struct { + ID int `json:"id"` + Name string `json:"artistName"` + ForeignArtistID string `json:"foreignArtistId"` + Monitored bool `json:"monitored"` + Path string `json:"path"` + } + if err := c.get(ctx, "/api/v1/artist", url.Values{"mbId": {mbid}}, &rows); err != nil { + return nil, err + } + for _, r := range rows { + if strings.EqualFold(r.ForeignArtistID, mbid) { + return &Artist{ID: r.ID, Name: clean(r.Name), Monitored: r.Monitored, Path: clean(r.Path)}, nil + } + } + return nil, nil +} + +// AddRequest says which artist to add and with which choices. +type AddRequest struct { + MBID string + QualityProfileID int + MetadataProfileID int + RootFolderPath string + Monitor string // one of providers.LidarrMonitors + SearchMissing bool // Lidarr starts looking for the albums at once +} + +func (r AddRequest) check() error { + switch { + case !idRe.MatchString(r.MBID): + return fmt.Errorf("%w: not a MusicBrainz id", ErrBadRequest) + case r.QualityProfileID <= 0 || r.MetadataProfileID <= 0 || r.RootFolderPath == "": + return fmt.Errorf("%w: both profiles and the root folder are needed", ErrBadRequest) + case !slices.Contains(providers.LidarrMonitors, r.Monitor): + return fmt.Errorf("%w: unknown monitor option", ErrBadRequest) + } + return nil +} + +// Add adds the artist to Lidarr the way Lidarr's own screen does: it asks Lidarr to look the artist +// up by MusicBrainz id, then sends what it answered back with the choices added. It returns +// ErrNotFound when Lidarr does not know the id, ErrAlreadyAdded when it already has the artist +// (nothing changes then), and ErrRejected, with Lidarr's reason, when it refuses. +func (c *Client) Add(ctx context.Context, r AddRequest) error { + if err := r.check(); err != nil { + return err + } + mbid := strings.ToLower(r.MBID) + + var found []map[string]json.RawMessage + if err := c.get(ctx, "/api/v1/artist/lookup", url.Values{"term": {"lidarr:" + mbid}}, &found); err != nil { + return err + } + var artist map[string]json.RawMessage + for _, item := range found { + var id string + if json.Unmarshal(item["foreignArtistId"], &id) == nil && strings.EqualFold(id, mbid) { + artist = item + break + } + } + if artist == nil { + return ErrNotFound + } + var have int + if json.Unmarshal(artist["id"], &have) == nil && have > 0 { + return ErrAlreadyAdded // Lidarr shows the artists it has with their own id + } + + newItems := "all" + if r.Monitor == "none" { + newItems = "none" // an artist that is not monitored does not start monitoring what it releases + } + set := func(key string, v any) error { + raw, err := json.Marshal(v) + if err != nil { + return err + } + artist[key] = raw + return nil + } + delete(artist, "id") + for key, v := range map[string]any{ + "foreignArtistId": mbid, + "qualityProfileId": r.QualityProfileID, + "metadataProfileId": r.MetadataProfileID, + "rootFolderPath": r.RootFolderPath, + "monitored": true, + "monitorNewItems": newItems, + "addOptions": map[string]any{"monitor": r.Monitor, "searchForMissingAlbums": r.SearchMissing}, + } { + if err := set(key, v); err != nil { + return fmt.Errorf("%w: %v", ErrBadRequest, err) + } + } + body, err := json.Marshal(artist) + if err != nil { + return fmt.Errorf("%w: %v", ErrBadRequest, err) + } + + status, data, err := c.do(ctx, http.MethodPost, "/api/v1/artist", nil, body) + if err != nil { + return err + } + if status >= 200 && status < 300 { + return nil + } + if status == http.StatusBadRequest { + reason := reasons(data) + switch lower := strings.ToLower(reason); { + case strings.Contains(lower, "already been added"): + return ErrAlreadyAdded + case strings.Contains(lower, "was not found"): + return ErrNotFound + case reason == "": + return fmt.Errorf("%w: no reason given", ErrRejected) + } + return fmt.Errorf("%w: %s", ErrRejected, reason) + } + return statusError(status) +} + +// reasons reads the messages of a validation error, which Lidarr sends as a list of +// {"errorMessage": ...}, and makes them one safe line. +func reasons(data []byte) string { + var list []struct { + Message string `json:"errorMessage"` + } + if json.Unmarshal(data, &list) != nil { + return "" + } + var parts []string + for _, e := range list { + if m := clean(e.Message); m != "" { + parts = append(parts, m) + } + } + return clean(strings.Join(parts, "; ")) +} + +// clean makes text from Lidarr safe to show: one line, no control characters, not too long. +func clean(s string) string { + s = strings.Map(func(r rune) rune { + if unicode.IsControl(r) { + return ' ' + } + return r + }, s) + s = strings.Join(strings.Fields(s), " ") + if utf8.RuneCountInString(s) > maxReason { + s = string([]rune(s)[:maxReason-1]) + "…" + } + return s +} diff --git a/internal/providers/lidarr/lidarr_test.go b/internal/providers/lidarr/lidarr_test.go new file mode 100644 index 0000000..481ce81 --- /dev/null +++ b/internal/providers/lidarr/lidarr_test.go @@ -0,0 +1,375 @@ +package lidarr + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + + "codeberg.org/squidd/sonda/internal/providers" +) + +const ( + key = "0123456789abcdef0123456789abcdef" + mbid = "a74b1b7f-71a5-4011-9441-d0b5e4122711" +) + +// fakeLidarr answers like the Lidarr API that was read in its sources, and remembers what it was asked. +type fakeLidarr struct { + mu sync.Mutex + srv *httptest.Server + requests []seen + handlers map[string]http.HandlerFunc // "METHOD /path" -> answer +} + +type seen struct { + Method, Path, Query, Key, UA string + Body []byte +} + +func newFake(t *testing.T) *fakeLidarr { + t.Helper() + f := &fakeLidarr{handlers: map[string]http.HandlerFunc{}} + f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + f.mu.Lock() + f.requests = append(f.requests, seen{r.Method, r.URL.Path, r.URL.RawQuery, r.Header.Get("X-Api-Key"), r.Header.Get("User-Agent"), body}) + h := f.handlers[r.Method+" "+r.URL.Path] + f.mu.Unlock() + if r.Header.Get("X-Api-Key") != key { + http.Error(w, `{"error":"Unauthorized"}`, http.StatusUnauthorized) + return + } + if h == nil { + http.NotFound(w, r) + return + } + h(w, r) + })) + t.Cleanup(f.srv.Close) + return f +} + +func (f *fakeLidarr) on(method, path string, h http.HandlerFunc) { f.handlers[method+" "+path] = h } + +func (f *fakeLidarr) json(method, path, body string) { + f.on(method, path, func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, body) + }) +} + +func (f *fakeLidarr) last() seen { + f.mu.Lock() + defer f.mu.Unlock() + return f.requests[len(f.requests)-1] +} + +func (f *fakeLidarr) count() int { + f.mu.Lock() + defer f.mu.Unlock() + return len(f.requests) +} + +func (f *fakeLidarr) client() *Client { + return New(func(context.Context) (Config, error) { return Config{URL: f.srv.URL, APIKey: key}, nil }, + func() string { return "Sonda/test ( https://example.org )" }, nil) +} + +func TestStatusReadsTheVersionAndSendsTheKeyInTheHeader(t *testing.T) { + f := newFake(t) + f.json("GET", "/api/v1/system/status", `{"appName":"Lidarr","instanceName":"Lidarr","version":"2.9.6.4552","branch":"master"}`) + st, err := f.client().Status(context.Background()) + if err != nil || st.AppName != "Lidarr" || st.Version != "2.9.6.4552" { + t.Fatalf("status = %+v err = %v", st, err) + } + got := f.last() + if got.Key != key || got.UA != "Sonda/test ( https://example.org )" || strings.Contains(got.Query, "apikey") { + t.Errorf("request = %+v: the key goes in the header, never in the address", got) + } +} + +func TestWhatGoesWrongIsOneOfTheKnownErrors(t *testing.T) { + ctx := context.Background() + f := newFake(t) + f.json("GET", "/api/v1/system/status", `{"appName":"Lidarr"}`) + + wrong := New(func(context.Context) (Config, error) { + return Config{URL: f.srv.URL, APIKey: strings.Repeat("f", 32)}, nil + }, nil, nil) + if _, err := wrong.Status(ctx); !errors.Is(err, ErrKeyRefused) { + t.Errorf("a wrong key: %v", err) + } + + none := New(func(context.Context) (Config, error) { return Config{}, ErrNotConfigured }, nil, nil) + if _, err := none.Status(ctx); !errors.Is(err, ErrNotConfigured) { + t.Errorf("not set up: %v", err) + } + + f.srv.Close() + if _, err := f.client().Status(ctx); !errors.Is(err, ErrUnreachable) { + t.Errorf("a server that is down: %v", err) + } + + g := newFake(t) + g.json("GET", "/api/v1/system/status", `not json`) + if _, err := g.client().Status(ctx); !errors.Is(err, ErrBadResponse) { + t.Errorf("an answer that is not json: %v", err) + } + g.on("GET", "/api/v1/system/status", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(500) }) + if _, err := g.client().Status(ctx); !errors.Is(err, ErrBadResponse) { + t.Errorf("a server error: %v", err) + } +} + +func TestARedirectIsNeverFollowedAndTheKeyNeverLeavesForAnotherAddress(t *testing.T) { + var stolen string + other := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { stolen = r.Header.Get("X-Api-Key") })) + defer other.Close() + f := newFake(t) + f.on("GET", "/api/v1/system/status", func(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, other.URL+"/steal", http.StatusFound) + }) + if _, err := f.client().Status(context.Background()); !errors.Is(err, ErrBadResponse) { + t.Errorf("err = %v, want ErrBadResponse", err) + } + if stolen != "" { + t.Errorf("the key reached another address: %q", stolen) + } +} + +func TestAnAnswerThatIsTooLargeIsRefused(t *testing.T) { + f := newFake(t) + f.on("GET", "/api/v1/system/status", func(w http.ResponseWriter, _ *http.Request) { + _, _ = io.WriteString(w, `{"appName":"`+strings.Repeat("a", 5<<20)+`"}`) + }) + if _, err := f.client().Status(context.Background()); !errors.Is(err, ErrBadResponse) { + t.Errorf("err = %v", err) + } +} + +func TestTheAddressAndTheKeyAreReadForEveryRequest(t *testing.T) { + a, b := newFake(t), newFake(t) + a.json("GET", "/api/v1/system/status", `{"appName":"A"}`) + b.json("GET", "/lidarr/api/v1/system/status", `{"appName":"B"}`) + cur := a.srv.URL + c := New(func(context.Context) (Config, error) { return Config{URL: cur, APIKey: key}, nil }, nil, nil) + if st, _ := c.Status(context.Background()); st.AppName != "A" { + t.Errorf("first = %+v", st) + } + cur = b.srv.URL + "/lidarr" // an address with a URL base + if st, err := c.Status(context.Background()); err != nil || st.AppName != "B" { + t.Errorf("after the change = %+v err=%v", st, err) + } +} + +func TestOptionsListsWhatAnAdminCanChoose(t *testing.T) { + f := newFake(t) + f.json("GET", "/api/v1/qualityprofile", `[{"id":1,"name":"Any","upgradeAllowed":false},{"id":2,"name":"Lossless"}]`) + f.json("GET", "/api/v1/metadataprofile", `[{"id":1,"name":"Standard"}]`) + f.json("GET", "/api/v1/rootfolder", `[{"id":1,"name":"Music","path":"/music","accessible":true,"freeSpace":123},{"id":2,"path":"/old","accessible":false}]`) + o, err := f.client().Options(context.Background()) + if err != nil { + t.Fatal(err) + } + if len(o.QualityProfiles) != 2 || o.QualityProfiles[1] != (Profile{ID: 2, Name: "Lossless"}) || + len(o.MetadataProfiles) != 1 || o.MetadataProfiles[0] != (Profile{ID: 1, Name: "Standard"}) || + len(o.RootFolders) != 2 || o.RootFolders[0] != (RootFolder{Path: "/music", Accessible: true}) || o.RootFolders[1].Accessible { + t.Errorf("options = %+v", o) + } + // One that fails fails the whole call: half a list would hide a choice. + f.on("GET", "/api/v1/rootfolder", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(500) }) + if _, err := f.client().Options(context.Background()); !errors.Is(err, ErrBadResponse) { + t.Errorf("err = %v", err) + } +} + +func TestArtistSaysWhetherAnIdIsInLidarr(t *testing.T) { + f := newFake(t) + c := f.client() + f.json("GET", "/api/v1/artist", `[]`) + if a, err := c.Artist(context.Background(), mbid); err != nil || a != nil { + t.Fatalf("not there: %+v err=%v", a, err) + } + if got := f.last(); got.Query != "mbId="+mbid { + t.Errorf("query = %q", got.Query) + } + f.json("GET", "/api/v1/artist", `[{"id":7,"artistName":"Radiohead","foreignArtistId":"`+mbid+`","monitored":true,"path":"/music/Radiohead"}]`) + a, err := c.Artist(context.Background(), strings.ToUpper(mbid)) + if err != nil || a == nil || *a != (Artist{ID: 7, Name: "Radiohead", Monitored: true, Path: "/music/Radiohead"}) { + t.Errorf("there: %+v err=%v", a, err) + } + n := f.count() + if _, err := c.Artist(context.Background(), "Radiohead"); !errors.Is(err, ErrBadRequest) || f.count() != n { + t.Errorf("a name: err=%v, requests %d -> %d: only an id is ever sent", err, n, f.count()) + } +} + +// lookup is what Lidarr returns for an artist it does not have yet. +const lookup = `[{"id":0,"artistName":"Radiohead","foreignArtistId":"` + mbid + `","overview":"x","images":[{"coverType":"poster","url":"u"}],"genres":["rock"],"ratings":{"votes":1,"value":9.1}}]` + +func TestAddAsksLidarrForTheArtistAndSendsItBackWithTheChoices(t *testing.T) { + f := newFake(t) + f.json("GET", "/api/v1/artist/lookup", lookup) + f.on("POST", "/api/v1/artist", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusCreated) + _, _ = io.WriteString(w, `{"id":7}`) + }) + err := f.client().Add(context.Background(), AddRequest{ + MBID: strings.ToUpper(mbid), QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/music", Monitor: "latest", SearchMissing: true, + }) + if err != nil { + t.Fatal(err) + } + f.mu.Lock() + reqs := f.requests + f.mu.Unlock() + if len(reqs) != 2 || reqs[0].Path != "/api/v1/artist/lookup" || reqs[0].Query != "term=lidarr%3A"+mbid { + t.Fatalf("requests = %+v, want the lookup with the id (lower case) and then the add", reqs) + } + var body map[string]any + if err := json.Unmarshal(reqs[1].Body, &body); err != nil { + t.Fatal(err) + } + opts, _ := body["addOptions"].(map[string]any) + if body["foreignArtistId"] != mbid || body["artistName"] != "Radiohead" || body["qualityProfileId"] != float64(2) || + body["metadataProfileId"] != float64(1) || body["rootFolderPath"] != "/music" || body["monitored"] != true || + body["monitorNewItems"] != "all" || opts["monitor"] != "latest" || opts["searchForMissingAlbums"] != true { + t.Errorf("body = %s", reqs[1].Body) + } + // What Lidarr said about the artist is sent back as it came, the way its own screen does. + if body["overview"] != "x" || body["genres"] == nil || body["images"] == nil { + t.Errorf("the rest of the lookup was lost: %s", reqs[1].Body) + } + if _, has := body["id"]; has && body["id"] != float64(0) { + t.Errorf("id = %v", body["id"]) + } +} + +func TestMonitoringNothingAlsoStopsNewAlbumsFromBeingMonitored(t *testing.T) { + f := newFake(t) + f.json("GET", "/api/v1/artist/lookup", lookup) + f.json("POST", "/api/v1/artist", `{"id":7}`) + if err := f.client().Add(context.Background(), AddRequest{MBID: mbid, QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "none"}); err != nil { + t.Fatal(err) + } + var body map[string]any + _ = json.Unmarshal(f.last().Body, &body) + opts, _ := body["addOptions"].(map[string]any) + if body["monitorNewItems"] != "none" || opts["monitor"] != "none" || opts["searchForMissingAlbums"] != false { + t.Errorf("body = %s", f.last().Body) + } +} + +func TestAddSaysWhenTheArtistIsAlreadyThereOrUnknown(t *testing.T) { + ctx := context.Background() + req := AddRequest{MBID: mbid, QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"} + + f := newFake(t) + f.json("GET", "/api/v1/artist/lookup", lookup) + f.on("POST", "/api/v1/artist", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, `[{"propertyName":"ForeignArtistId","errorMessage":"This artist has already been added.","errorCode":"ArtistExistsValidator"}]`) + }) + if err := f.client().Add(ctx, req); !errors.Is(err, ErrAlreadyAdded) { + t.Errorf("already there: %v", err) + } + + // Lidarr marks an artist it already has in the lookup, with its id: nothing to add then. + g := newFake(t) + g.json("GET", "/api/v1/artist/lookup", strings.Replace(lookup, `"id":0`, `"id":7`, 1)) + if err := g.client().Add(ctx, req); !errors.Is(err, ErrAlreadyAdded) || g.count() != 1 { + t.Errorf("already there in the lookup: err=%v requests=%d", err, g.count()) + } + + h := newFake(t) + h.json("GET", "/api/v1/artist/lookup", `[]`) + if err := h.client().Add(ctx, req); !errors.Is(err, ErrNotFound) || h.count() != 1 { + t.Errorf("unknown: err=%v requests=%d", err, h.count()) + } + // A lookup that answers with another artist is not an answer about this one. + h.json("GET", "/api/v1/artist/lookup", strings.Replace(lookup, mbid, "11111111-2222-4333-8444-555555555555", 1)) + if err := h.client().Add(ctx, req); !errors.Is(err, ErrNotFound) { + t.Errorf("another artist: %v", err) + } + + r := newFake(t) + r.json("GET", "/api/v1/artist/lookup", lookup) + r.on("POST", "/api/v1/artist", func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, "[{\"errorMessage\":\"Root folder does not exist\\nat /m\"}]") + }) + err := r.client().Add(ctx, req) + if !errors.Is(err, ErrRejected) || !strings.Contains(err.Error(), "Root folder does not exist") || strings.ContainsAny(err.Error(), "\n\r") { + t.Errorf("rejected: %v", err) + } +} + +func TestAddRefusesWhatIsNotComplete(t *testing.T) { + f := newFake(t) + for name, req := range map[string]AddRequest{ + "a name": {MBID: "Radiohead", QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, + "no profile": {MBID: mbid, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, + "no folder": {MBID: mbid, QualityProfileID: 1, MetadataProfileID: 1, Monitor: "all"}, + "a monitor that Lidarr does not have": {MBID: mbid, QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "x"}, + } { + if err := f.client().Add(context.Background(), req); err == nil { + t.Errorf("%s: no error", name) + } + } + if f.count() != 0 { + t.Errorf("%d requests for things that cannot be added", f.count()) + } +} + +func TestAWrongKeyOnAddIsReported(t *testing.T) { + f := newFake(t) + c := New(func(context.Context) (Config, error) { + return Config{URL: f.srv.URL, APIKey: strings.Repeat("f", 32)}, nil + }, nil, nil) + err := c.Add(context.Background(), AddRequest{MBID: mbid, QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}) + if !errors.Is(err, ErrKeyRefused) { + t.Errorf("err = %v", err) + } +} + +func TestNoErrorEverCarriesTheKey(t *testing.T) { + f := newFake(t) + f.srv.Close() + _, err := f.client().Status(context.Background()) + if err == nil || strings.Contains(err.Error(), key) { + t.Errorf("err = %v", err) + } +} + +func TestProbeSaysWhatTheTestOfAProviderNeeds(t *testing.T) { + ctx := context.Background() + f := newFake(t) + f.json("GET", "/api/v1/system/status", `{"appName":"Lidarr"}`) + if err := f.client().Probe(ctx); err != nil { + t.Errorf("a working Lidarr: %v", err) + } + wrong := New(func(context.Context) (Config, error) { + return Config{URL: f.srv.URL, APIKey: strings.Repeat("f", 32)}, nil + }, nil, nil) + if err := wrong.Probe(ctx); !errors.Is(err, providers.ErrKeyRefused) { + t.Errorf("a wrong key: %v", err) + } + f.on("GET", "/api/v1/system/status", func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(404) }) + if err := f.client().Probe(ctx); !errors.Is(err, providers.ErrUnreachable) { + t.Errorf("an address that is not Lidarr: %v", err) + } + f.srv.Close() + if err := f.client().Probe(ctx); !errors.Is(err, providers.ErrUnreachable) { + t.Errorf("a server that is down: %v", err) + } + none := New(func(context.Context) (Config, error) { return Config{}, ErrNotConfigured }, nil, nil) + if err := none.Probe(ctx); err == nil || errors.Is(err, providers.ErrKeyRefused) || errors.Is(err, providers.ErrUnreachable) { + t.Errorf("not set up: %v", err) + } +} diff --git a/internal/providers/lidarr_settings_test.go b/internal/providers/lidarr_settings_test.go new file mode 100644 index 0000000..891017e --- /dev/null +++ b/internal/providers/lidarr_settings_test.go @@ -0,0 +1,210 @@ +package providers + +import ( + "context" + "errors" + "strings" + "testing" +) + +const lidarrKey = "0123456789abcdef0123456789abcdef" + +func lidarrInfo(t *testing.T, s *Service) Info { + t.Helper() + infos, err := s.List(context.Background()) + if err != nil { + t.Fatal(err) + } + for _, in := range infos { + if in.Name == Lidarr { + return in + } + } + t.Fatalf("no lidarr provider in %+v", infos) + return Info{} +} + +func TestLidarrIsAProviderThatNeedsAKeyAndAnAddressAndIsOffByDefault(t *testing.T) { + s, _ := newSvc() + in := lidarrInfo(t, s) + if in.Enabled || !in.NeedsAPIKey || !in.NeedsURL || in.HasAPIKey || in.URL != "" { + t.Errorf("lidarr = %+v", in) + } + for _, want := range []string{"MusicBrainz id", "your own", "Never"} { + if !strings.Contains(in.Sends, want) { + t.Errorf("sends does not say %q: %q", want, in.Sends) + } + } + if strings.TrimSpace(in.Notes) == "" { + t.Error("no notes") + } + // Until it is set, the options are the ones a new setup starts from. + if in.Lidarr.Monitor != "all" || !in.Lidarr.SearchMissing || in.Lidarr.QualityProfileID != 0 { + t.Errorf("default options = %+v", in.Lidarr) + } +} + +func TestLidarrNeedsTheKeyAndTheAddressBeforeItIsEnabled(t *testing.T) { + s, _ := newSvc() + ctx := context.Background() + on := true + if _, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("enabled with nothing: %v", err) + } + if _, err := s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)}); err != nil { + t.Fatal(err) + } + if _, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("enabled with a key and no address: %v", err) + } + if _, err := s.Update(ctx, Lidarr, Patch{URL: strp("http://192.168.1.20:8686/")}); err != nil { + t.Fatal(err) + } + if in, err := s.Update(ctx, Lidarr, Patch{Enabled: &on}); err != nil || !in.Enabled || in.URL != "http://192.168.1.20:8686" { + t.Fatalf("enable: %+v err=%v, want it on with the address without the closing slash", in, err) + } + set, active, err := s.Active(ctx, Lidarr) + if err != nil || !active || set.URL != "http://192.168.1.20:8686" || set.APIKey != lidarrKey { + t.Errorf("active = %+v %v %v", set, active, err) + } + // Taking the address away switches it off, as taking away the key does. + if in, err := s.Update(ctx, Lidarr, Patch{URL: strp("")}); err != nil || in.Enabled || in.URL != "" { + t.Errorf("after clearing the address: %+v err=%v", in, err) + } + if _, active, _ := s.Active(ctx, Lidarr); active { + t.Error("active without an address") + } +} + +func TestLidarrTakesAnAddressWithAPathAndRefusesAnythingElse(t *testing.T) { + s, _ := newSvc() + ctx := context.Background() + for in, want := range map[string]string{ + "http://orion:8686": "http://orion:8686", + " https://lidarr.example.org/ ": "https://lidarr.example.org", + "http://nas.local:8686/lidarr/": "http://nas.local:8686/lidarr", + "HTTP://Orion:8686": "http://Orion:8686", + } { + if got, err := s.Update(ctx, Lidarr, Patch{URL: strp(in)}); err != nil || got.URL != want { + t.Errorf("%q: %q err=%v, want %q", in, got.URL, err, want) + } + } + for _, bad := range []string{ + "ftp://orion", "orion:8686", "//orion", "http://", "javascript:alert(1)", "http://user:pass@orion", + "http://orion/?apikey=x", "http://orion/#a", "http://orion/a b", "http://" + strings.Repeat("a", 400), + "file:///etc/passwd", "http://orion\n.evil", + } { + if _, err := s.Update(ctx, Lidarr, Patch{URL: strp(bad)}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("%q: err = %v, want ErrInvalidSettings", bad, err) + } + } +} + +func TestOnlyLidarrHasAnAddressAndOptions(t *testing.T) { + s, _ := newSvc() + ctx := context.Background() + if _, err := s.Update(ctx, MusicBrainz, Patch{URL: strp("http://orion:8686")}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("an address for MusicBrainz: %v", err) + } + if _, err := s.Update(ctx, LastFM, Patch{Lidarr: &LidarrOptions{Monitor: "all"}}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("options for Last.fm: %v", err) + } +} + +func TestALidarrKeyIs32HexCharacters(t *testing.T) { + s, _ := newSvc() + for _, bad := range []string{"short", strings.Repeat("g", 32), lidarrKey + "0"} { + if _, err := s.Update(context.Background(), Lidarr, Patch{APIKey: strp(bad)}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("%q: %v", bad, err) + } + } + if in, err := s.Update(context.Background(), Lidarr, Patch{APIKey: strp(strings.ToUpper(lidarrKey))}); err != nil || !in.HasAPIKey { + t.Errorf("an upper case key: %+v %v", in, err) + } +} + +func TestLidarrOptionsAreCheckedAndReplacedAsAWhole(t *testing.T) { + s, st := newSvc() + ctx := context.Background() + good := LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/music", Monitor: "latest", SearchMissing: false} + in, err := s.Update(ctx, Lidarr, Patch{Lidarr: &good}) + if err != nil || in.Lidarr != good { + t.Fatalf("options = %+v err=%v", in.Lidarr, err) + } + if st.rows[Lidarr].Lidarr != good { + t.Errorf("stored = %+v", st.rows[Lidarr].Lidarr) + } + // A patch that does not mention them leaves them alone. + if in, err := s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)}); err != nil || in.Lidarr != good { + t.Errorf("after another change: %+v err=%v", in.Lidarr, err) + } + for name, bad := range map[string]LidarrOptions{ + "a monitor option Lidarr does not have": {Monitor: "everything"}, + "no monitor option": {QualityProfileID: 1}, + "a negative profile": {Monitor: "all", QualityProfileID: -1}, + "a folder with a control character": {Monitor: "all", RootFolderPath: "/mu\nsic"}, + "a folder that is too long": {Monitor: "all", RootFolderPath: "/" + strings.Repeat("a", 600)}, + } { + if _, err := s.Update(ctx, Lidarr, Patch{Lidarr: &bad}); !errors.Is(err, ErrInvalidSettings) { + t.Errorf("%s: err = %v", name, err) + } + } + for _, m := range []string{"all", "future", "missing", "existing", "latest", "first", "none"} { + o := good + o.Monitor = m + if _, err := s.Update(ctx, Lidarr, Patch{Lidarr: &o}); err != nil { + t.Errorf("monitor %q: %v", m, err) + } + } +} + +func TestLidarrIsCompleteOnlyWithProfilesAndAFolder(t *testing.T) { + cases := map[string]struct { + o LidarrOptions + want bool + }{ + "all set": {LidarrOptions{QualityProfileID: 1, MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, true}, + "no quality": {LidarrOptions{MetadataProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, false}, + "no metadata": {LidarrOptions{QualityProfileID: 1, RootFolderPath: "/m", Monitor: "all"}, false}, + "no root folder": {LidarrOptions{QualityProfileID: 1, MetadataProfileID: 1, Monitor: "all"}, false}, + } + for name, c := range cases { + if got := c.o.Complete(); got != c.want { + t.Errorf("%s: Complete = %v, want %v", name, got, c.want) + } + } +} + +func TestATestOfLidarrSaysWhatIsMissing(t *testing.T) { + s, _ := newSvc() + ctx := context.Background() + called := false + s.SetChecker(Lidarr, func(context.Context, string) error { called = true; return nil }) + if res, _ := s.Test(ctx, Lidarr); res.Status != TestNoKey || called { + t.Errorf("no key: %+v called=%v", res, called) + } + _, _ = s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey)}) + if res, _ := s.Test(ctx, Lidarr); res.Status != TestNoURL || res.OK || called { + t.Errorf("no address: %+v called=%v", res, called) + } + _, _ = s.Update(ctx, Lidarr, Patch{URL: strp("http://orion:8686")}) + if res, _ := s.Test(ctx, Lidarr); !res.OK || !called { + t.Errorf("set up: %+v called=%v", res, called) + } +} + +func TestStoredGivesTheSettingsWhetherTheProviderIsOnOrNot(t *testing.T) { + s, _ := newSvc() + ctx := context.Background() + if got, err := s.Stored(ctx, Lidarr); err != nil || got.Provider != "" || got.URL != "" { + t.Errorf("never configured: %+v %v", got, err) + } + _, _ = s.Update(ctx, Lidarr, Patch{APIKey: strp(lidarrKey), URL: strp("http://orion:8686")}) + got, err := s.Stored(ctx, Lidarr) // still off + if err != nil || got.APIKey != lidarrKey || got.URL != "http://orion:8686" || got.Enabled { + t.Errorf("stored = %+v err=%v", got, err) + } + if _, err := s.Stored(ctx, "spotify"); !errors.Is(err, ErrUnknownProvider) { + t.Errorf("unknown: %v", err) + } +} diff --git a/internal/providers/settings.go b/internal/providers/settings.go index 5b523a2..f8506d6 100644 --- a/internal/providers/settings.go +++ b/internal/providers/settings.go @@ -9,7 +9,9 @@ import ( "context" "errors" "fmt" + "net/url" "regexp" + "slices" "strings" "sync" "time" @@ -24,6 +26,7 @@ const ( Wikipedia = "wikipedia" Commons = "commons" CoverArtArchive = "coverartarchive" + Lidarr = "lidarr" ) // Errors returned by the service. @@ -41,10 +44,45 @@ var ( ErrUnreachable = errors.New("the provider could not be reached") ) -const maxContactLen = 200 +const ( + maxContactLen = 200 + maxURLLen = 300 + maxFolderLen = 500 +) var lastfmKeyRe = regexp.MustCompile(`^[0-9a-f]{32}$`) +// A Lidarr API key is 32 hexadecimal characters too. +var lidarrKeyRe = lastfmKeyRe + +// LidarrMonitors are the values of the monitor option that Lidarr accepts when an artist is added. +var LidarrMonitors = []string{"all", "future", "missing", "existing", "latest", "first", "none"} + +// LidarrOptions are the choices an admin makes once for what is sent to Lidarr when an artist +// is added. The profiles and the folder are Lidarr's own: the admin reads them from +// GET /providers/lidarr/options and picks. +type LidarrOptions struct { + QualityProfileID int + MetadataProfileID int + RootFolderPath string + Monitor string // one of LidarrMonitors + SearchMissing bool // Lidarr starts looking for the albums at once +} + +// Complete says whether an artist can be added with these options: Lidarr needs both profiles and a +// folder, and Sonda does not guess them. +func (o LidarrOptions) Complete() bool { + return o.QualityProfileID > 0 && o.MetadataProfileID > 0 && o.RootFolderPath != "" +} + +// withDefaults is what a setup that was never saved starts from. +func (o LidarrOptions) withDefaults() LidarrOptions { + if o.Monitor == "" { + o.Monitor, o.SearchMissing = "all", true + } + return o +} + // Settings are the stored settings of one provider, API key included. They are // only for code that talks to the provider; the API never returns the key. type Settings struct { @@ -52,6 +90,8 @@ type Settings struct { Enabled bool APIKey string Contact string // goes into the User-Agent; empty means the project address + URL string // where the provider is, for those that are the user's own (Lidarr) + Lidarr LidarrOptions UpdatedAt time.Time } @@ -61,19 +101,24 @@ type Info struct { Name string Enabled bool NeedsAPIKey bool + NeedsURL bool HasAPIKey bool Contact string - Sends string // what is sent to the provider, and what never is - Notes string // terms of use and attribution - UpdatedAt time.Time // zero when the provider was never configured + URL string // only for a provider that has one + Lidarr LidarrOptions // only for Lidarr; what an unsaved setup starts from until it is set + Sends string // what is sent to the provider, and what never is + Notes string // terms of use and attribution + UpdatedAt time.Time // zero when the provider was never configured } -// Patch changes some settings. A nil field is left alone. An empty APIKey or -// Contact clears it. +// Patch changes some settings. A nil field is left alone. An empty APIKey, Contact or URL +// clears it. Lidarr replaces all the options of Lidarr at once. type Patch struct { Enabled *bool APIKey *string Contact *string + URL *string + Lidarr *LidarrOptions } // Store persists provider settings. @@ -87,6 +132,7 @@ type Store interface { type meta struct { name string needsKey bool + needsURL bool sends, note string } @@ -138,6 +184,18 @@ var known = []meta{ note: "Covers are uploaded by MusicBrainz users and have no licence of their own: Sonda keeps a copy only for " + "the albums of your own library, for your use, and never shows it as free.", }, + { + name: Lidarr, + needsKey: true, + needsURL: true, + sends: "Only when an admin sends an artist to Lidarr, and for the admin's setup: the MusicBrainz id of the artist " + + "(never a name: Sonda does not search) and your API key, to the address you set, which is your own Lidarr; " + + "asking Lidarr what it offers (profiles and folders) and testing the key send nothing but the key. Lidarr " + + "then looks for the music through its own indexers and download clients, which Sonda does not control. " + + "Never your users, your listens, track titles or file paths.", + note: "Sonda does not download, search for or choose music: Lidarr does, with the quality profile, the metadata " + + "profile and the folder an admin picked, and only for the artists an admin sends. Nothing is sent by itself.", + }, } func lookup(name string) (meta, bool) { @@ -162,10 +220,15 @@ type Service struct { func NewService(store Store) *Service { return &Service{store: store, now: time.Now} } func info(m meta, s Settings) Info { - return Info{ - Name: m.name, NeedsAPIKey: m.needsKey, Sends: m.sends, Notes: m.note, + in := Info{ + Name: m.name, NeedsAPIKey: m.needsKey, NeedsURL: m.needsURL, Sends: m.sends, Notes: m.note, Enabled: s.Enabled, HasAPIKey: s.APIKey != "", Contact: s.Contact, UpdatedAt: s.UpdatedAt, } + if m.needsURL { + in.URL = s.URL + in.Lidarr = s.Lidarr.withDefaults() + } + return in } // List returns every known provider, configured or not, in a fixed order. @@ -191,7 +254,7 @@ func (s *Service) Update(ctx context.Context, name string, p Patch) (Info, error if !ok { return Info{}, ErrUnknownProvider } - if p.Enabled == nil && p.APIKey == nil && p.Contact == nil { + if p.Enabled == nil && p.APIKey == nil && p.Contact == nil && p.URL == nil && p.Lidarr == nil { return Info{}, fmt.Errorf("%w: nothing to change", ErrInvalidSettings) } @@ -214,6 +277,25 @@ func (s *Service) Update(ctx context.Context, name string, p Patch) (Info, error cur.Enabled = false // a provider that needs a key is off without one } } + if p.URL != nil { + u, err := validateURL(m, *p.URL) + if err != nil { + return Info{}, err + } + cur.URL = u + if u == "" && m.needsURL { + cur.Enabled = false // and so is one that needs an address + } + } + if p.Lidarr != nil { + if !m.needsURL { + return Info{}, fmt.Errorf("%w: %s has no Lidarr options", ErrInvalidSettings, m.name) + } + if err := validateLidarrOptions(*p.Lidarr); err != nil { + return Info{}, err + } + cur.Lidarr = *p.Lidarr + } if p.Contact != nil { contact, err := validateContact(*p.Contact) if err != nil { @@ -225,6 +307,9 @@ func (s *Service) Update(ctx context.Context, name string, p Patch) (Info, error if *p.Enabled && m.needsKey && cur.APIKey == "" { return Info{}, fmt.Errorf("%w: %s needs an API key before it can be enabled", ErrInvalidSettings, name) } + if *p.Enabled && m.needsURL && cur.URL == "" { + return Info{}, fmt.Errorf("%w: %s needs an address before it can be enabled", ErrInvalidSettings, name) + } cur.Enabled = *p.Enabled } @@ -247,12 +332,27 @@ func (s *Service) Active(ctx context.Context, name string) (Settings, bool, erro if err != nil { return Settings{}, false, fmt.Errorf("read provider settings: %w", err) } - if !found || !cur.Enabled || (m.needsKey && cur.APIKey == "") { + if !found || !cur.Enabled || (m.needsKey && cur.APIKey == "") || (m.needsURL && cur.URL == "") { return Settings{}, false, nil } return cur, true, nil } +// Stored returns the stored settings of a provider, key included, whether it is on or not; the zero +// Settings when it was never configured. It is for what an admin does to finish setting a provider up +// (testing a key, reading what Lidarr offers). Anything that talks to a provider for someone else asks +// Active instead. +func (s *Service) Stored(ctx context.Context, name string) (Settings, error) { + if _, ok := lookup(name); !ok { + return Settings{}, ErrUnknownProvider + } + cur, _, err := s.store.Get(ctx, name) + if err != nil { + return Settings{}, fmt.Errorf("read provider settings: %w", err) + } + return cur, nil +} + func validateKey(m meta, raw string) (string, error) { key := strings.ToLower(strings.TrimSpace(raw)) if key == "" { @@ -264,9 +364,69 @@ func validateKey(m meta, raw string) (string, error) { if m.name == LastFM && !lastfmKeyRe.MatchString(key) { return "", fmt.Errorf("%w: a Last.fm API key is 32 hexadecimal characters", ErrInvalidSettings) } + if m.name == Lidarr && !lidarrKeyRe.MatchString(key) { + return "", fmt.Errorf("%w: a Lidarr API key is 32 hexadecimal characters", ErrInvalidSettings) + } return key, nil } +// validateURL accepts the address of a provider that is the user's own: http or https, a host, an +// optional port and path (Lidarr can live under a URL base), and nothing that could carry a secret or +// change where a request goes: no user and password, no query, no fragment. The closing slash goes. +func validateURL(m meta, raw string) (string, error) { + if !m.needsURL { + return "", fmt.Errorf("%w: %s does not have an address", ErrInvalidSettings, m.name) + } + raw = strings.TrimSpace(raw) + if raw == "" { + return "", nil + } + bad := func(why string) (string, error) { + return "", fmt.Errorf("%w: the address %s", ErrInvalidSettings, why) + } + if utf8.RuneCountInString(raw) > maxURLLen { + return bad(fmt.Sprintf("is longer than %d characters", maxURLLen)) + } + if strings.ContainsFunc(raw, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }) { + return bad("cannot contain spaces or control characters") + } + if strings.ContainsAny(raw, "?#") { + return bad("cannot have a query or a fragment") + } + u, err := url.Parse(raw) + if err != nil { + return bad("is not a valid address") + } + if u.Scheme != "http" && u.Scheme != "https" { + return bad("must start with http:// or https://") + } + if u.Hostname() == "" { + return bad("has no host") + } + if u.User != nil { + return bad("cannot carry a user or a password: the key is set apart") + } + return u.Scheme + "://" + u.Host + strings.TrimRight(u.EscapedPath(), "/"), nil +} + +func validateLidarrOptions(o LidarrOptions) error { + if o.QualityProfileID < 0 || o.MetadataProfileID < 0 { + return fmt.Errorf("%w: a profile id cannot be negative", ErrInvalidSettings) + } + if !slices.Contains(LidarrMonitors, o.Monitor) { + return fmt.Errorf("%w: monitor must be one of %s", ErrInvalidSettings, strings.Join(LidarrMonitors, ", ")) + } + if utf8.RuneCountInString(o.RootFolderPath) > maxFolderLen { + return fmt.Errorf("%w: the root folder is longer than %d characters", ErrInvalidSettings, maxFolderLen) + } + for _, r := range o.RootFolderPath { + if unicode.IsControl(r) { + return fmt.Errorf("%w: the root folder cannot contain control characters", ErrInvalidSettings) + } + } + return nil +} + // validateContact accepts what MusicBrainz asks for in a User-Agent: a URL or an // e-mail address. Parentheses are refused because they delimit it there. func validateContact(raw string) (string, error) { @@ -327,6 +487,7 @@ func (s *Service) SetChecker(name string, c Checker) { const ( TestOK = "ok" TestNoKey = "no_api_key" + TestNoURL = "no_address" TestKeyRefused = "key_refused" TestRateLimited = "rate_limited" TestUnreachable = "unreachable" @@ -359,6 +520,9 @@ func (s *Service) Test(ctx context.Context, name string) (TestResult, error) { if m.needsKey && cur.APIKey == "" { return TestResult{Status: TestNoKey}, nil } + if m.needsURL && cur.URL == "" { + return TestResult{Status: TestNoURL}, nil + } switch err := check(ctx, cur.APIKey); { case err == nil: return TestResult{OK: true, Status: TestOK}, nil diff --git a/internal/providers/settings_test.go b/internal/providers/settings_test.go index f71cfd1..b96a7e3 100644 --- a/internal/providers/settings_test.go +++ b/internal/providers/settings_test.go @@ -53,8 +53,8 @@ func TestEveryProviderIsOffByDefault(t *testing.T) { if err != nil { t.Fatal(err) } - if len(infos) != 5 || infos[0].Name != LastFM || infos[1].Name != MusicBrainz || infos[2].Name != Wikipedia || infos[3].Name != Commons || infos[4].Name != CoverArtArchive { - t.Fatalf("providers = %+v, want lastfm, musicbrainz, wikipedia, commons, then coverartarchive", infos) + if len(infos) != 6 || infos[0].Name != LastFM || infos[1].Name != MusicBrainz || infos[2].Name != Wikipedia || infos[3].Name != Commons || infos[4].Name != CoverArtArchive || infos[5].Name != Lidarr { + t.Fatalf("providers = %+v, want lastfm, musicbrainz, wikipedia, commons, coverartarchive, then lidarr", infos) } for _, in := range infos { if in.Enabled || in.HasAPIKey { @@ -222,7 +222,7 @@ func TestListMergesStoredRowsInAFixedOrder(t *testing.T) { st.rows[MusicBrainz] = Settings{Provider: MusicBrainz, Enabled: true, Contact: "c", UpdatedAt: time.Unix(1, 0)} st.rows["stale-unknown"] = Settings{Provider: "stale-unknown", Enabled: true} // a row of a provider that no longer exists infos, err := s.List(context.Background()) - if err != nil || len(infos) != 5 { + if err != nil || len(infos) != 6 { t.Fatalf("infos = %+v err=%v, want exactly the known providers", infos, err) } if infos[0].Name != LastFM || infos[1].Name != MusicBrainz || !infos[1].Enabled || infos[1].Contact != "c" { diff --git a/internal/storage/providers.go b/internal/storage/providers.go index 27c5687..2c97eca 100644 --- a/internal/storage/providers.go +++ b/internal/storage/providers.go @@ -19,12 +19,23 @@ type providerRow struct { Enabled bool `bun:"enabled,notnull"` APIKey string `bun:"api_key,notnull"` Contact string `bun:"contact,notnull"` + URL string `bun:"url,notnull"` UpdatedAt int64 `bun:"updated_at,notnull"` + + QualityProfileID int `bun:"quality_profile_id,notnull"` + MetadataProfileID int `bun:"metadata_profile_id,notnull"` + RootFolder string `bun:"root_folder,notnull"` + Monitor string `bun:"monitor,notnull"` + SearchMissing bool `bun:"search_missing,notnull"` } func (r providerRow) toSettings() providers.Settings { return providers.Settings{ - Provider: r.Provider, Enabled: r.Enabled, APIKey: r.APIKey, Contact: r.Contact, + Provider: r.Provider, Enabled: r.Enabled, APIKey: r.APIKey, Contact: r.Contact, URL: r.URL, + Lidarr: providers.LidarrOptions{ + QualityProfileID: r.QualityProfileID, MetadataProfileID: r.MetadataProfileID, + RootFolderPath: r.RootFolder, Monitor: r.Monitor, SearchMissing: r.SearchMissing, + }, UpdatedAt: time.Unix(r.UpdatedAt, 0), } } @@ -65,13 +76,21 @@ func (r *ProviderRepo) List(ctx context.Context) ([]providers.Settings, error) { func (r *ProviderRepo) Save(ctx context.Context, s providers.Settings) error { row := &providerRow{ Provider: s.Provider, Enabled: s.Enabled, APIKey: s.APIKey, - Contact: s.Contact, UpdatedAt: s.UpdatedAt.Unix(), + Contact: s.Contact, URL: s.URL, UpdatedAt: s.UpdatedAt.Unix(), + QualityProfileID: s.Lidarr.QualityProfileID, MetadataProfileID: s.Lidarr.MetadataProfileID, + RootFolder: s.Lidarr.RootFolderPath, Monitor: s.Lidarr.Monitor, SearchMissing: s.Lidarr.SearchMissing, } _, err := r.db.NewInsert().Model(row). On("CONFLICT (provider) DO UPDATE"). Set("enabled = EXCLUDED.enabled"). Set("api_key = EXCLUDED.api_key"). Set("contact = EXCLUDED.contact"). + Set("url = EXCLUDED.url"). + Set("quality_profile_id = EXCLUDED.quality_profile_id"). + Set("metadata_profile_id = EXCLUDED.metadata_profile_id"). + Set("root_folder = EXCLUDED.root_folder"). + Set("monitor = EXCLUDED.monitor"). + Set("search_missing = EXCLUDED.search_missing"). Set("updated_at = EXCLUDED.updated_at"). Exec(ctx) if err != nil { diff --git a/internal/storage/providers_test.go b/internal/storage/providers_test.go index 87a70e1..8815410 100644 --- a/internal/storage/providers_test.go +++ b/internal/storage/providers_test.go @@ -70,3 +70,38 @@ func TestProviderTableDefaultsToOff(t *testing.T) { } }) } + +func TestProviderRepoKeepsTheAddressAndTheLidarrOptions(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + repo := NewProviderRepo(db) + at := time.Unix(1_700_000_000, 0) + want := providers.Settings{ + Provider: providers.Lidarr, Enabled: true, APIKey: "0123456789abcdef0123456789abcdef", URL: "http://orion:8686/lidarr", + Lidarr: providers.LidarrOptions{QualityProfileID: 2, MetadataProfileID: 1, RootFolderPath: "/música/New", Monitor: "latest", SearchMissing: false}, + UpdatedAt: at, + } + if err := repo.Save(ctx, want); err != nil { + t.Fatal(err) + } + got, found, err := repo.Get(ctx, providers.Lidarr) + if err != nil || !found || got.URL != want.URL || got.Lidarr != want.Lidarr { + t.Fatalf("got %+v found=%v err=%v, want %+v", got, found, err, want) + } + want.Lidarr.SearchMissing = true + want.URL = "" + if err := repo.Save(ctx, want); err != nil { + t.Fatal(err) + } + if got, _, _ = repo.Get(ctx, providers.Lidarr); got.URL != "" || !got.Lidarr.SearchMissing { + t.Errorf("after the second save: %+v", got) + } + // A provider that never had them reads them as empty. + if err := repo.Save(ctx, providers.Settings{Provider: providers.LastFM, UpdatedAt: at}); err != nil { + t.Fatal(err) + } + if got, _, _ := repo.Get(ctx, providers.LastFM); got.URL != "" || got.Lidarr != (providers.LidarrOptions{}) { + t.Errorf("last.fm = %+v", got) + } + }) +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index 9de55c6..70c3366 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -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 != 16 { - t.Errorf("applied migrations = %d, want 16", applied) + if applied != 17 { + t.Errorf("applied migrations = %d, want 17", applied) } }) } diff --git a/migrations/postgres/0017_lidarr_settings.sql b/migrations/postgres/0017_lidarr_settings.sql new file mode 100644 index 0000000..edc3911 --- /dev/null +++ b/migrations/postgres/0017_lidarr_settings.sql @@ -0,0 +1,9 @@ +-- Lidarr is the user's own server: it has an address, and the choices an admin makes once for +-- what is sent when an artist is added. The columns are empty for the other providers. + +ALTER TABLE provider_settings ADD COLUMN url TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN quality_profile_id INTEGER NOT NULL DEFAULT 0; +ALTER TABLE provider_settings ADD COLUMN metadata_profile_id INTEGER NOT NULL DEFAULT 0; +ALTER TABLE provider_settings ADD COLUMN root_folder TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN monitor TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN search_missing BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/migrations/sqlite/0017_lidarr_settings.sql b/migrations/sqlite/0017_lidarr_settings.sql new file mode 100644 index 0000000..28d3b33 --- /dev/null +++ b/migrations/sqlite/0017_lidarr_settings.sql @@ -0,0 +1,9 @@ +-- Lidarr is the user's own server: it has an address, and the choices an admin makes once for +-- what is sent when an artist is added. The columns are empty for the other providers. + +ALTER TABLE provider_settings ADD COLUMN url TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN quality_profile_id INTEGER NOT NULL DEFAULT 0; +ALTER TABLE provider_settings ADD COLUMN metadata_profile_id INTEGER NOT NULL DEFAULT 0; +ALTER TABLE provider_settings ADD COLUMN root_folder TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN monitor TEXT NOT NULL DEFAULT ''; +ALTER TABLE provider_settings ADD COLUMN search_missing INTEGER NOT NULL DEFAULT 0;