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