Merge pull request 'A page of the artists I sent to Lidarr, and whether they arrived' (#192) from feat/lidarr-requests into main

This commit is contained in:
squidd 2026-11-15 12:33:31 +01:00
commit fee06e2bb9
30 changed files with 1050 additions and 27 deletions

View file

@ -30,6 +30,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr
- **Importing from Spotify.** `POST /me/import/spotify` brings your listening history and your playlists from a Spotify data export into your own account. Ask Spotify for your data (Account, Privacy, "Extended streaming history" and "Account data") and send what it gives, as a multipart form with a `files` field for each file: the zip itself, or the JSON files in it (`Streaming_History_Audio_*.json`, the older `StreamingHistory_music_*.json` and `Playlist*.json`); a request may be up to 512 MB, and the rest of the export (the account, the searches, the library) is never opened. It all happens on your server and nothing is sent anywhere; the files are read from the request and not kept, and of each play only the song, the artist, the album, the time and how it ended are read, never the IP addresses, the country or the platform in the file. Of a play of a song you do not have that counts as listened, the name of the artist and the time are kept (and nothing else), so that Sonda can learn which artists you play together. A play is matched to a song of your library by artist and title (case, accents and punctuation do not matter, a `Remastered` note on either side does not, any credited artist will do, and a play of a compilation is matched by its album when Spotify says `Various Artists`). A match is recorded as a listen at the time of the play: `complete` when it ran to its end (or nine tenths were played), `skip` when Spotify says it was skipped or it lasted less than a listen, `play` otherwise; nothing is posted to Mastodon. Importing the same files again records nothing twice, and the same play in the extended and in the older history is one. A playlist becomes a playlist of yours with the songs that were found, in order; one whose name you already have is left alone, and one with none of its songs in your library is not made. The report counts what was added, what was already there and what is not in your library (and lists those artists, most played first), so you can see what is missing. One import of yours runs at a time. The report is kept: `GET /me/import/spotify` lists your past imports, newest first, with the numbers of each (never the files). The web app has an Import page for the form and for those numbers.
- **Suggestions from your own listening.** `GET /me/suggestions` learns which artists you play together: your history is split into sittings (plays less than 30 minutes apart) and two artists that turn up in the same sitting often are linked (a cosine over the sittings of each). It is a count over your own data, built on your server in a moment and again when plays are added, with no provider and nothing sent anywhere; it needs `discover_from_listening` in `PATCH /me/settings`. The model reads your listening events and the plays of artists you have no music of that an import kept (the artist and the time only). `library` are artists you have that go with what you play and that you did not play for two months; `missing` are artists you played at least three times and have no music of, the ones you play most that also go with what you play now first. Each says which of your artists it goes with. `POST /me/suggestions/wishlist` puts an artist you played on your wishlist by name (it is made without music and without a MusicBrainz id: Sonda never looks one up by name, so it cannot be sent to Lidarr from here). The web app has a "From your listening" tab in Discover.
- **Listening history by year and month.** `GET /listens/months` says how many listens that count you made in each calendar month, newest first, in the time zone you send as `tz` (UTC without it), and `GET /listens` takes `from` and `until` to read the listens of one of them; each listen now names its artists. The web app has a History page: a button for each year, the months of the year as bars, and the listens of the month a day at a time.
- **Requests to Lidarr.** `GET /lidarr/requests` (admin) lists the artists you sent to your Lidarr with `POST /artists/{id}/lidarr`, newest first, and says for each whether its music has arrived: `waiting` while there is none in the library, `arrived` once a scan found some (by the artist you sent or by an artist of the same name; the day it was first noticed is kept). Sending an artist again keeps one request with the first date; only what you sent from Sonda is listed. The web app has a Requests page (admins only), with the waiting and the arrived artists apart.
- **Sharing on Mastodon.** `PUT /me/mastodon` connects your own Mastodon account (the address of your instance and an access token that can only write posts); `GET` shows which account is connected and `DELETE` forgets it and the token. Nothing is connected by default. Once it is, each track of this server that counts as a listen is posted to your account as an unlisted status with the title, artists, album and `#NowPlaying`, and no link: at most one post a minute, never music of a friend. The token stays on the server and is never sent back. If the instance refuses the token on a post (it was revoked or expired), `GET /me/mastodon` says `problem: token_refused`, Settings shows it, and nothing is posted until you connect again with a new token. The address of the instance must be a domain name on https, and the server never connects to a private, loopback or link-local address for it, never follows a redirect and ignores proxy settings.
- **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.

View file

@ -309,6 +309,7 @@ func run() error {
Providers: providerSettings,
Lidarr: lidarrClient,
LidarrArtists: lidarr.NewService(discoveryRepo, providerSettings, lidarrClient),
LidarrRequests: lidarr.NewRequests(storage.NewLidarrRequestRepo(db)),
Discovery: discoveries,
Tags: tags,
Taste: tasteService,

View file

@ -34,6 +34,7 @@ import (
"codeberg.org/squidd/sonda/internal/playlists"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/providers/anthropic"
"codeberg.org/squidd/sonda/internal/providers/lidarr"
"codeberg.org/squidd/sonda/internal/smart"
"codeberg.org/squidd/sonda/internal/spotifyimport"
"codeberg.org/squidd/sonda/internal/storage"
@ -96,23 +97,24 @@ 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{},
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{}),
Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()),
Mastodon: mastodon.NewService(storage.NewMastodonRepo(db), &fakeMastodon{handle: "@test@mastodon.social"}, zap.NewNop()),
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{},
LidarrRequests: lidarr.NewRequests(storage.NewLidarrRequestRepo(db)),
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{}),
Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()),
Mastodon: mastodon.NewService(storage.NewMastodonRepo(db), &fakeMastodon{handle: "@test@mastodon.social"}, zap.NewNop()),
}
libRepo := storage.NewLibraryRepo(db)
deps.PeerCatalog = catalog.NewService(storage.NewSharedCatalogRepo(db))

View file

@ -651,6 +651,13 @@ func TestResponsesMatchTheSpec(t *testing.T) {
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)
}
requested := c.expect(c.call("GET", "/lidarr/requests", token, nil), 200, "the artists sent to lidarr")
if items, _ := requested["items"].([]any); len(items) != 1 || requested["total"] != 1.0 {
t.Fatalf("requests = %v, want the one artist that was sent", requested)
}
c.expect(c.call("GET", "/lidarr/requests?status=waiting&limit=5", token, nil), 200, "the requests that wait")
c.expect(c.call("GET", "/lidarr/requests?status=lost", token, nil), 400, "a status that does not exist", "invalid_input")
c.expect(c.call("GET", "/lidarr/requests", "", nil), 401, "requests without a token", "unauthorized")
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

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"time"
"github.com/gin-gonic/gin"
@ -16,7 +17,16 @@ type lidarrArtistService interface {
Presence(ctx context.Context, artistID int64) (lidarr.Presence, error)
}
type lidarrArtistHandlers struct{ svc lidarrArtistService }
// lidarrRequestService keeps and lists the artists a person sent to Lidarr.
type lidarrRequestService interface {
Record(ctx context.Context, userID, artistID int64) error
List(ctx context.Context, userID int64, status string, limit, offset int) (lidarr.RequestPage, error)
}
type lidarrArtistHandlers struct {
svc lidarrArtistService
requests lidarrRequestService
}
// 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.
@ -28,6 +38,10 @@ func (h *lidarrArtistHandlers) send(c *gin.Context) {
out, err := h.svc.Send(c.Request.Context(), id)
switch {
case err == nil:
// Lidarr has the artist now. That it could not be written down does not undo it: say so in the log, not to the admin.
if rerr := h.requests.Record(c.Request.Context(), currentUserID(c), id); rerr != nil {
_ = c.Error(rerr)
}
c.JSON(http.StatusOK, gin.H{"status": string(out)})
case errors.Is(err, discovery.ErrNotFound):
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
@ -77,3 +91,61 @@ func (h *lidarrArtistHandlers) presence(c *gin.Context) {
c.JSON(http.StatusOK, lidarrPresenceJSON{ProviderStatus: p.Status, InLidarr: p.InLidarr, Monitored: p.Monitored})
}
}
type lidarrRequestJSON struct {
ID int64 `json:"id"`
Artist lidarrArtistRefJSON `json:"artist"`
MBID *string `json:"mbid"`
RequestedAt time.Time `json:"requested_at"`
Status string `json:"status"`
ArrivedAt *time.Time `json:"arrived_at"`
}
type lidarrArtistRefJSON struct {
ID *int64 `json:"id"`
Name string `json:"name"`
}
// requestList lists the artists the signed-in admin sent to Lidarr, newest first, saying which have music in the
// library now.
func (h *lidarrArtistHandlers) requestList(c *gin.Context) {
limit, ok := queryInt(c, "limit")
if !ok {
return
}
offset, ok := queryInt(c, "offset")
if !ok {
return
}
page, err := h.requests.List(c.Request.Context(), currentUserID(c), c.Query("status"), clampInt(limit), clampInt(offset))
switch {
case errors.Is(err, lidarr.ErrInvalidQuery):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
return
case err != nil:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
return
}
items := make([]lidarrRequestJSON, 0, len(page.Items))
for _, r := range page.Items {
j := lidarrRequestJSON{ID: r.ID, Artist: lidarrArtistRefJSON{Name: r.Name}, RequestedAt: r.RequestedAt.UTC(), Status: r.Status()}
if r.ArtistID != 0 {
id := r.ArtistID
j.Artist.ID = &id
}
if r.MBID != "" {
m := r.MBID
j.MBID = &m
}
if !r.ArrivedAt.IsZero() {
at := r.ArrivedAt.UTC()
j.ArrivedAt = &at
}
items = append(items, j)
}
c.JSON(http.StatusOK, gin.H{
"items": items, "total": page.Total, "limit": page.Limit, "offset": page.Offset,
"counts": gin.H{"waiting": page.Waiting, "arrived": page.Arrived},
})
}

View file

@ -12,6 +12,7 @@ import (
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/providers/lidarr"
"codeberg.org/squidd/sonda/internal/storage"
)
const lidarrAPIKey = "fedcba9876543210fedcba9876543210"
@ -279,3 +280,78 @@ func TestAnyUserSeesWhetherLidarrHasAnArtist(t *testing.T) {
t.Errorf("unknown: %d %v", r.Code, r.Body)
}
}
func TestTheArtistsSentToLidarrAreListedAndSayWhenTheirMusicArrives(t *testing.T) {
fake := &fakeSender{outcome: lidarr.Added}
h, db := newTestEnvDB(t, func(d *Deps, _ *bun.DB) { d.LidarrArtists = fake })
admin := registerAndLogin(t, h)
_, bob := addUser(t, h, admin, "bob", false)
var artist int64
if err := db.QueryRow("INSERT INTO artists (mbid, name, name_key, search_key, discovered) VALUES ('a74b1b7f-71a5-4011-9441-d0b5e4122711', 'Chet Baker', 'chet baker', ?, ?) RETURNING id", storage.SearchKey("Chet Baker"), true).Scan(&artist); err != nil {
t.Fatal(err)
}
send := fmt.Sprintf("/api/v1/artists/%d/lidarr", artist)
if r := do(t, h, "GET", "/api/v1/lidarr/requests", admin, nil); r.Code != http.StatusOK || r.Body["total"] != 0.0 || len(r.Body["items"].([]any)) != 0 {
t.Fatalf("before sending anything: %d %v, want an empty list", r.Code, r.Body)
}
if r := do(t, h, "POST", send, admin, nil); r.Code != http.StatusOK {
t.Fatalf("send: %d %v", r.Code, r.Body)
}
if r := do(t, h, "POST", send, admin, nil); r.Code != http.StatusOK { // again: still one request
t.Fatalf("send again: %d %v", r.Code, r.Body)
}
r := do(t, h, "GET", "/api/v1/lidarr/requests", admin, nil)
sameKeys(t, "requests", r.Body, "items", "total", "limit", "offset", "counts")
if r.Body["total"] != 1.0 || r.Body["limit"] != 50.0 {
t.Fatalf("list = %v, want one request", r.Body)
}
counts, _ := r.Body["counts"].(map[string]any)
sameKeys(t, "counts", counts, "waiting", "arrived")
item := r.Body["items"].([]any)[0].(map[string]any)
sameKeys(t, "request", item, "id", "artist", "mbid", "requested_at", "status", "arrived_at")
who, _ := item["artist"].(map[string]any)
if who["name"] != "Chet Baker" || who["id"] != float64(artist) || item["status"] != "waiting" || item["arrived_at"] != nil || item["mbid"] != "a74b1b7f-71a5-4011-9441-d0b5e4122711" || counts["waiting"] != 1.0 {
t.Errorf("request = %v, counts %v", item, counts)
}
// Lidarr finishes and the music is scanned in.
seedSong(t, db, "a.flac", "Almost Blue", "Elvis Costello", "Chet Baker")
r = do(t, h, "GET", "/api/v1/lidarr/requests", admin, nil)
item = r.Body["items"].([]any)[0].(map[string]any)
if item["status"] != "arrived" || item["arrived_at"] == nil || r.Body["counts"].(map[string]any)["arrived"] != 1.0 {
t.Errorf("after the music arrived: %v", r.Body)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests?status=waiting", admin, nil); r.Body["total"] != 0.0 {
t.Errorf("waiting = %v, want none", r.Body)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests?status=lost", admin, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("a status that does not exist: %d %v", r.Code, r.Body)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests?limit=-1", admin, nil); r.Code != http.StatusBadRequest {
t.Errorf("a negative limit: %d", r.Code)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests", bob, nil); r.Code != http.StatusForbidden || errCode(r) != "admin_required" {
t.Errorf("as a user: %d %v, want 403 admin_required", r.Code, r.Body)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests", "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("without a token: %d", r.Code)
}
}
func TestASendThatFailsIsNotWrittenDown(t *testing.T) {
fake := &fakeSender{err: lidarr.ErrDisabled}
h, db := newTestEnvDB(t, func(d *Deps, _ *bun.DB) { d.LidarrArtists = fake })
admin := registerAndLogin(t, h)
var artist int64
if err := db.QueryRow("INSERT INTO artists (name, name_key, search_key, discovered) VALUES ('Sade', 'sade', ?, ?) RETURNING id", storage.SearchKey("Sade"), true).Scan(&artist); err != nil {
t.Fatal(err)
}
if r := do(t, h, "POST", fmt.Sprintf("/api/v1/artists/%d/lidarr", artist), admin, nil); r.Code != http.StatusConflict {
t.Fatalf("send: %d %v", r.Code, r.Body)
}
if r := do(t, h, "GET", "/api/v1/lidarr/requests", admin, nil); r.Body["total"] != 0.0 {
t.Errorf("list = %v, want nothing: Lidarr was not asked", r.Body)
}
}

View file

@ -225,6 +225,7 @@ type Deps struct {
Providers providerService
Lidarr lidarrOptionsService
LidarrArtists lidarrArtistService
LidarrRequests lidarrRequestService
Discovery discoveryService
Tags tagService
Taste tasteService
@ -397,7 +398,7 @@ 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}
la := &lidarrArtistHandlers{svc: d.LidarrArtists, requests: d.LidarrRequests}
protected.GET("/artists/:id/lidarr", la.presence)
protected.GET("/artists/:id/image/credit", (&artistImageHandlers{svc: d.ArtistImages}).credit)
protected.GET("/albums", cat.listAlbums)
@ -452,6 +453,7 @@ func NewRouter(d Deps) *gin.Engine {
admin.PUT("/providers/:name", prov.update)
admin.GET("/providers/lidarr/options", prov.lidarrOptions)
admin.POST("/artists/:id/lidarr", la.send)
admin.GET("/lidarr/requests", la.requestList)
admin.POST("/providers/:name/test", prov.test)
admin.DELETE("/providers/:name/data", prov.purge)
fed := &federationHandlers{svc: d.Federation}

View file

@ -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", "LidarrArtists", "ArtistImages", "About", "Streams"} {
for _, want := range []string{"Auth", "Tokens", "Listening", "CoversJob", "ArtistImagesJob", "DescriptionsJob", "Lidarr", "LidarrArtists", "LidarrRequests", "ArtistImages", "About", "Streams"} {
found := false
for _, m := range missing {
found = found || m == want

View file

@ -2331,6 +2331,35 @@ paths:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/lidarr/requests:
get:
tags: [Discovery]
operationId: listLidarrRequests
summary: The artists you sent to Lidarr
description: |
Admin only. The artists you sent to your Lidarr with `POST /artists/{id}/lidarr`, newest first, each saying
whether its music has arrived: `waiting` while there is none in the library, `arrived` once there is (the
day it was first noticed is `arrived_at`; it is noticed when this list is read, by the artist you sent or by
an artist of the same name that a scan made). Sending an artist again keeps one request with the first date.
Only what you sent is listed, never what is in Lidarr. `counts` are of all your requests, whatever `status`.
security: [{bearerAuth: []}]
parameters:
- {$ref: '#/components/parameters/Limit'}
- {$ref: '#/components/parameters/Offset'}
- name: status
in: query
description: Only the requests that are `waiting` or `arrived`.
schema: {type: string, enum: [waiting, arrived]}
responses:
'200':
description: A page of requests.
content:
application/json:
schema: {$ref: '#/components/schemas/LidarrRequestPage'}
'400': {$ref: '#/components/responses/InvalidInput'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
/artists/{id}/lidarr:
parameters:
- {$ref: '#/components/parameters/Id'}
@ -5002,6 +5031,41 @@ components:
duration_played_ms: {type: integer, format: int64}
counted: {type: boolean, description: Whether the event counts as a listen under the current rule.}
LidarrRequestPage:
type: object
additionalProperties: false
required: [items, total, limit, offset, counts]
properties:
items:
type: array
items:
type: object
additionalProperties: false
required: [id, artist, mbid, requested_at, status, arrived_at]
properties:
id: {type: integer, format: int64}
artist:
type: object
additionalProperties: false
required: [id, name]
properties:
id: {type: integer, format: int64, nullable: true, description: 'The artist on this server, null when it is gone.'}
name: {type: string}
mbid: {type: string, nullable: true}
requested_at: {type: string, format: date-time, description: The first time it was sent.}
status: {type: string, enum: [waiting, arrived]}
arrived_at: {type: string, format: date-time, nullable: true}
total: {type: integer, description: "Size of the whole result (of the `status`), not of this page."}
limit: {type: integer}
offset: {type: integer}
counts:
type: object
additionalProperties: false
required: [waiting, arrived]
properties:
waiting: {type: integer}
arrived: {type: integer}
ListeningSuggestion:
type: object
additionalProperties: false

View file

@ -0,0 +1,98 @@
package lidarr
import (
"context"
"errors"
"fmt"
"time"
)
// Errors of Requests.
var (
// ErrInvalidQuery means a bad limit, offset or status.
ErrInvalidQuery = errors.New("invalid query")
// ErrNoSuchArtist means the artist to record does not exist.
ErrNoSuchArtist = errors.New("lidarr: no such artist")
)
// Status of a request.
const (
// Waiting means Lidarr was asked and there is no music of the artist in the library yet.
Waiting = "waiting"
// Arrived means there is music of the artist in the library.
Arrived = "arrived"
)
// Limits of a page of requests.
const (
DefaultRequestLimit = 50
MaxRequestLimit = 200
)
// Request is an artist that a person sent to Lidarr.
type Request struct {
ID int64
ArtistID int64 // 0 when the artist is no longer known to this server
Name string
MBID string
RequestedAt time.Time
ArrivedAt time.Time // zero while it is waiting
}
// Status is Arrived or Waiting.
func (r Request) Status() string {
if r.ArrivedAt.IsZero() {
return Waiting
}
return Arrived
}
// RequestPage is a page of requests, newest first, with how many there are of each status (whatever the filter).
type RequestPage struct {
Items []Request
Total int // of the filter
Waiting int
Arrived int
Limit, Offset int
}
// RequestStore keeps the requests.
type RequestStore interface {
// Record keeps that the person sent the artist. Sending it again keeps the first date.
Record(ctx context.Context, userID, artistID int64, at time.Time) error
// List notices which waiting artists have music now, then gives a page of the requests of the person, newest
// first; status is "", Waiting or Arrived.
List(ctx context.Context, userID int64, status string, limit, offset int, now time.Time) (RequestPage, error)
}
// Requests keeps and lists the artists a person sent to Lidarr.
type Requests struct {
store RequestStore
now func() time.Time
}
// NewRequests builds Requests.
func NewRequests(store RequestStore) *Requests { return &Requests{store: store, now: time.Now} }
// Record keeps that the person sent the artist to Lidarr.
func (r *Requests) Record(ctx context.Context, userID, artistID int64) error {
return r.store.Record(ctx, userID, artistID, r.now())
}
// List gives a page of the requests of a person. A limit of 0 is DefaultRequestLimit, and one above MaxRequestLimit
// is lowered to it. The status is "", Waiting or Arrived.
func (r *Requests) List(ctx context.Context, userID int64, status string, limit, offset int) (RequestPage, error) {
if limit < 0 || offset < 0 {
return RequestPage{}, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalidQuery)
}
switch status {
case "", Waiting, Arrived:
default:
return RequestPage{}, fmt.Errorf("%w: status must be waiting or arrived", ErrInvalidQuery)
}
if limit == 0 {
limit = DefaultRequestLimit
}
limit = min(limit, MaxRequestLimit)
return r.store.List(ctx, userID, status, limit, offset, r.now())
}

View file

@ -0,0 +1,73 @@
package lidarr
import (
"context"
"errors"
"testing"
"time"
)
type fakeRequestStore struct {
recorded [][2]int64
at time.Time
status string
limit, offset int
err error
}
func (f *fakeRequestStore) Record(_ context.Context, userID, artistID int64, at time.Time) error {
f.recorded = append(f.recorded, [2]int64{userID, artistID})
f.at = at
return f.err
}
func (f *fakeRequestStore) List(_ context.Context, _ int64, status string, limit, offset int, _ time.Time) (RequestPage, error) {
f.status, f.limit, f.offset = status, limit, offset
return RequestPage{Limit: limit, Offset: offset}, f.err
}
func TestRecordPassesThePersonTheArtistAndTheTime(t *testing.T) {
store := &fakeRequestStore{}
r := NewRequests(store)
moment := time.Date(2026, 11, 15, 12, 0, 0, 0, time.UTC)
r.now = func() time.Time { return moment }
if err := r.Record(context.Background(), 7, 42); err != nil {
t.Fatal(err)
}
if len(store.recorded) != 1 || store.recorded[0] != [2]int64{7, 42} || !store.at.Equal(moment) {
t.Errorf("recorded %v at %v", store.recorded, store.at)
}
store.err = errors.New("disk full")
if err := r.Record(context.Background(), 7, 42); err == nil {
t.Error("an error of the store was swallowed")
}
}
func TestAPageOfRequestsHasBoundsAndKnowsItsStatuses(t *testing.T) {
store := &fakeRequestStore{}
r := NewRequests(store)
ctx := context.Background()
if _, err := r.List(ctx, 1, "", 0, 0); err != nil || store.limit != DefaultRequestLimit {
t.Errorf("no limit: %v, %d, want the default", err, store.limit)
}
if _, err := r.List(ctx, 1, Waiting, 100000, 3); err != nil || store.limit != MaxRequestLimit || store.offset != 3 || store.status != Waiting {
t.Errorf("a huge limit: %v, %d/%d/%q", err, store.limit, store.offset, store.status)
}
for _, bad := range []struct {
status string
limit, offset int
}{{"", -1, 0}, {"", 0, -1}, {"lost", 10, 0}} {
if _, err := r.List(ctx, 1, bad.status, bad.limit, bad.offset); !errors.Is(err, ErrInvalidQuery) {
t.Errorf("%+v: %v, want ErrInvalidQuery", bad, err)
}
}
}
func TestARequestIsWaitingUntilItHasAnArrivalDay(t *testing.T) {
if got := (Request{}).Status(); got != Waiting {
t.Errorf("status = %q, want waiting", got)
}
if got := (Request{ArrivedAt: time.Now()}).Status(); got != Arrived {
t.Errorf("status = %q, want arrived", got)
}
}

View file

@ -13,6 +13,12 @@ import (
"codeberg.org/squidd/sonda/internal/spotifyimport"
)
// affinitySongBy is affinitySong for a test that does not need the track.
func affinitySongBy(t *testing.T, db *bun.DB, lib int64, rel, title, artist string) {
t.Helper()
affinitySong(t, db, lib, rel, title, artist)
}
// affinitySong puts a song with one artist in the library and gives the id of its track.
func affinitySong(t *testing.T, db *bun.DB, lib int64, rel, title, artist string) int64 {
t.Helper()

View file

@ -0,0 +1,99 @@
package storage
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/providers/lidarr"
)
// LidarrRequestRepo keeps the artists a person sent to their Lidarr. It implements lidarr.RequestStore.
type LidarrRequestRepo struct{ db *bun.DB }
// NewLidarrRequestRepo builds a LidarrRequestRepo.
func NewLidarrRequestRepo(db *bun.DB) *LidarrRequestRepo { return &LidarrRequestRepo{db: db} }
// Record keeps that the person sent the artist. Sending the same artist again (the same name, whatever its id) keeps
// the first date and fills what was missing.
func (r *LidarrRequestRepo) Record(ctx context.Context, userID, artistID int64, at time.Time) error {
var (
name string
mbid sql.NullString
)
err := r.db.QueryRowContext(ctx, "SELECT name, mbid FROM artists WHERE id = ?", artistID).Scan(&name, &mbid)
if errors.Is(err, sql.ErrNoRows) {
return lidarr.ErrNoSuchArtist
}
if err != nil {
return fmt.Errorf("read artist: %w", err)
}
_, err = r.db.ExecContext(ctx,
`INSERT INTO lidarr_requests (user_id, artist_id, artist_name, artist_key, artist_mbid, requested_at) VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (user_id, artist_key) DO UPDATE SET
artist_id = COALESCE(lidarr_requests.artist_id, excluded.artist_id),
artist_mbid = COALESCE(lidarr_requests.artist_mbid, excluded.artist_mbid)`,
userID, artistID, name, searchKey(name), mbid, at.Unix())
if err != nil {
return fmt.Errorf("keep lidarr request: %w", err)
}
return nil
}
// List first notices the waiting artists that have music now (by the artist of the request, or by an artist with the
// same name that a scan made), and keeps the moment. Then it gives a page of the requests of the person, newest first.
func (r *LidarrRequestRepo) List(ctx context.Context, userID int64, status string, limit, offset int, now time.Time) (lidarr.RequestPage, error) {
page := lidarr.RequestPage{Items: []lidarr.Request{}, Limit: limit, Offset: offset}
if _, err := r.db.ExecContext(ctx,
`UPDATE lidarr_requests SET arrived_at = ? WHERE user_id = ? AND arrived_at IS NULL AND (
(artist_id IS NOT NULL AND `+playableArtist("lidarr_requests.artist_id")+`)
OR EXISTS (SELECT 1 FROM artists a WHERE a.search_key = lidarr_requests.artist_key AND `+playableArtist("a.id")+`))`,
now.Unix(), userID); err != nil {
return page, fmt.Errorf("notice arrived artists: %w", err)
}
if err := r.db.QueryRowContext(ctx,
`SELECT COALESCE(SUM(CASE WHEN arrived_at IS NULL THEN 1 ELSE 0 END), 0), COALESCE(SUM(CASE WHEN arrived_at IS NULL THEN 0 ELSE 1 END), 0)
FROM lidarr_requests WHERE user_id = ?`, userID).Scan(&page.Waiting, &page.Arrived); err != nil {
return page, fmt.Errorf("count lidarr requests: %w", err)
}
where := "user_id = ?"
switch status {
case lidarr.Waiting:
where += " AND arrived_at IS NULL"
page.Total = page.Waiting
case lidarr.Arrived:
where += " AND arrived_at IS NOT NULL"
page.Total = page.Arrived
default:
page.Total = page.Waiting + page.Arrived
}
rows, err := r.db.QueryContext(ctx,
`SELECT id, artist_id, artist_name, artist_mbid, requested_at, arrived_at FROM lidarr_requests WHERE `+where+
` ORDER BY requested_at DESC, id DESC LIMIT ? OFFSET ?`, userID, limit, offset)
if err != nil {
return page, fmt.Errorf("list lidarr requests: %w", err)
}
defer rows.Close()
for rows.Next() {
var (
req lidarr.Request
artist sql.NullInt64
mbid sql.NullString
requested int64
arrived sql.NullInt64
)
if err := rows.Scan(&req.ID, &artist, &req.Name, &mbid, &requested, &arrived); err != nil {
return page, err
}
req.ArtistID, req.MBID, req.RequestedAt = artist.Int64, mbid.String, time.Unix(requested, 0)
if arrived.Valid {
req.ArrivedAt = time.Unix(arrived.Int64, 0)
}
page.Items = append(page.Items, req)
}
return page, rows.Err()
}

View file

@ -0,0 +1,136 @@
package storage
import (
"context"
"errors"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/providers/lidarr"
)
func TestLidarrRequestsAreKeptOnceAndSayWhenMusicArrives(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
users := NewUserRepo(db)
alice, err := users.CreateUser(ctx, "alice", "h", true)
if err != nil {
t.Fatal(err)
}
bob, err := users.CreateUser(ctx, "bob", "h", true)
if err != nil {
t.Fatal(err)
}
lib, err := NewLibraryRepo(db).CreateLibrary(ctx, "music", "/music")
if err != nil {
t.Fatal(err)
}
// Two artists named by a provider, with no music: one with an id, one without.
var chet, sade int64
if err := db.QueryRow("INSERT INTO artists (mbid, name, name_key, search_key, discovered) VALUES ('11111111-1111-1111-1111-111111111111', 'Chet Baker', 'chet baker', ?, ?) RETURNING id", SearchKey("Chet Baker"), true).Scan(&chet); err != nil {
t.Fatal(err)
}
if err := db.QueryRow("INSERT INTO artists (name, name_key, search_key, discovered) VALUES ('Sade', 'sade', ?, ?) RETURNING id", SearchKey("Sade"), true).Scan(&sade); err != nil {
t.Fatal(err)
}
repo := NewLidarrRequestRepo(db)
t0 := time.Unix(1_000_000, 0)
if err := repo.Record(ctx, alice.ID, chet, t0); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, sade, t0.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, chet, t0.Add(2*time.Hour)); err != nil { // sent again: the first date stays
t.Fatal(err)
}
if err := repo.Record(ctx, bob.ID, sade, t0); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, alice.ID, 999999, t0); !errors.Is(err, lidarr.ErrNoSuchArtist) {
t.Errorf("an artist that does not exist: %v, want ErrNoSuchArtist", err)
}
got, err := repo.List(ctx, alice.ID, "", 50, 0, t0.Add(3*time.Hour))
if err != nil {
t.Fatal(err)
}
if got.Total != 2 || got.Waiting != 2 || got.Arrived != 0 || len(got.Items) != 2 {
t.Fatalf("list = %+v, want her two, both waiting", got)
}
if got.Items[0].Name != "Sade" || got.Items[1].Name != "Chet Baker" || got.Items[1].RequestedAt.Unix() != t0.Unix() || got.Items[1].MBID == "" || got.Items[0].MBID != "" {
t.Errorf("items = %+v, want newest first, the first date kept and the id when there is one", got.Items)
}
if got.Items[0].Status() != lidarr.Waiting {
t.Errorf("status = %q, want waiting", got.Items[0].Status())
}
// Music of Chet Baker is scanned in: the artist of the request has a file now.
affinitySongBy(t, db, lib.ID, "a.flac", "Almost Blue", "Chet Baker")
arrival := t0.Add(4 * time.Hour)
got, err = repo.List(ctx, alice.ID, "", 50, 0, arrival)
if err != nil {
t.Fatal(err)
}
if got.Waiting != 1 || got.Arrived != 1 {
t.Fatalf("counts = %d waiting, %d arrived, want 1 and 1", got.Waiting, got.Arrived)
}
var chetItem lidarr.Request
for _, it := range got.Items {
if it.Name == "Chet Baker" {
chetItem = it
}
}
if chetItem.Status() != lidarr.Arrived || chetItem.ArrivedAt.Unix() != arrival.Unix() {
t.Errorf("Chet Baker = %+v, want arrived now", chetItem)
}
// The day it arrived does not move on later reads.
again, _ := repo.List(ctx, alice.ID, lidarr.Arrived, 50, 0, arrival.Add(24*time.Hour))
if again.Total != 1 || again.Items[0].ArrivedAt.Unix() != arrival.Unix() {
t.Errorf("arrived = %+v, want the day kept", again)
}
waiting, _ := repo.List(ctx, alice.ID, lidarr.Waiting, 50, 0, arrival)
if waiting.Total != 1 || waiting.Items[0].Name != "Sade" {
t.Errorf("waiting = %+v, want Sade", waiting)
}
// Music that a scan made as another artist of the same name also counts.
affinitySongBy(t, db, lib.ID, "b.flac", "Smooth Operator", "SADE")
both, _ := repo.List(ctx, alice.ID, "", 50, 0, arrival.Add(time.Hour))
if both.Waiting != 0 || both.Arrived != 2 {
t.Errorf("counts = %d waiting, %d arrived, want all arrived: %+v", both.Waiting, both.Arrived, both.Items)
}
// A page, and past the end.
page, _ := repo.List(ctx, alice.ID, "", 1, 1, arrival)
if len(page.Items) != 1 || page.Total != 2 || page.Items[0].Name != "Chet Baker" {
t.Errorf("second page = %+v", page)
}
if past, _ := repo.List(ctx, alice.ID, "", 10, 5, arrival); len(past.Items) != 0 || past.Total != 2 {
t.Errorf("past the end = %+v", past)
}
// Bob's request is his own and still waiting for himself until he looks: the list of one never shows another's.
mine, _ := repo.List(ctx, bob.ID, "", 50, 0, arrival)
if mine.Total != 1 || mine.Items[0].Name != "Sade" {
t.Errorf("bob's = %+v, want his one", mine)
}
// An artist removed from the server leaves the request, with its name.
var ghost int64
if err := db.QueryRow("INSERT INTO artists (name, name_key, search_key, discovered) VALUES ('Ghost', 'ghost', ?, ?) RETURNING id", SearchKey("Ghost"), true).Scan(&ghost); err != nil {
t.Fatal(err)
}
if err := repo.Record(ctx, bob.ID, ghost, t0); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "DELETE FROM artists WHERE id = ?", ghost); err != nil {
t.Fatal(err)
}
kept, _ := repo.List(ctx, bob.ID, lidarr.Waiting, 50, 0, arrival)
if len(kept.Items) != 1 || kept.Items[0].Name != "Ghost" || kept.Items[0].ArtistID != 0 {
t.Errorf("after the artist is removed: %+v, want the request with no artist id", kept.Items)
}
})
}

View file

@ -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 != 32 {
t.Errorf("applied migrations = %d, want 32", applied)
if applied != 33 {
t.Errorf("applied migrations = %d, want 33", applied)
}
})
}

View file

@ -0,0 +1,15 @@
-- PostgreSQL flavour of sqlite/0033_lidarr_requests.sql.
CREATE TABLE lidarr_requests (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
artist_id BIGINT REFERENCES artists(id) ON DELETE SET NULL,
artist_name TEXT NOT NULL,
artist_key TEXT NOT NULL,
artist_mbid TEXT,
requested_at BIGINT NOT NULL,
arrived_at BIGINT
);
CREATE UNIQUE INDEX idx_lidarr_requests_artist ON lidarr_requests(user_id, artist_key);
CREATE INDEX idx_lidarr_requests_time ON lidarr_requests(user_id, requested_at DESC);

View file

@ -0,0 +1,18 @@
-- The artists a person sent to their Lidarr from Sonda, so that a page can say which of them have arrived. An artist
-- has arrived when its music is in the library: Sonda notices it when the list is read and keeps the day it first did.
-- The name is kept too, because the artist of a request can be removed (an artist only a provider named) while the
-- request is still of interest.
CREATE TABLE lidarr_requests (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
artist_id INTEGER REFERENCES artists(id) ON DELETE SET NULL,
artist_name TEXT NOT NULL,
artist_key TEXT NOT NULL, -- storage.SearchKey of the name: how music that arrives is told to be this artist's
artist_mbid TEXT,
requested_at INTEGER NOT NULL, -- unix seconds, the first time it was sent
arrived_at INTEGER -- unix seconds, NULL while there is no music of it
);
CREATE UNIQUE INDEX idx_lidarr_requests_artist ON lidarr_requests(user_id, artist_key);
CREATE INDEX idx_lidarr_requests_time ON lidarr_requests(user_id, requested_at DESC);

View file

@ -0,0 +1,20 @@
import { useQuery } from '@tanstack/react-query'
import { api } from '@/api/client'
import type { components } from '@/api/schema'
export type LidarrRequestPage = components['schemas']['LidarrRequestPage']
export type LidarrRequest = LidarrRequestPage['items'][number]
export type RequestStatus = 'waiting' | 'arrived'
/**
* The artists the admin sent to Lidarr, newest first, with how many wait and how many arrived. They are few, so one
* request with the largest page the server gives is enough. Reading the list is also what notices that music arrived,
* so it is read again whenever the page is opened.
*/
export function useLidarrRequests(status?: RequestStatus) {
return useQuery({
queryKey: ['/lidarr/requests', status ?? 'all'],
queryFn: () => api.get<LidarrRequestPage>('/lidarr/requests', { status, limit: 200 }),
staleTime: 0,
})
}

View file

@ -1649,6 +1649,30 @@ export interface paths {
patch?: never;
trace?: never;
};
"/lidarr/requests": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* The artists you sent to Lidarr
* @description Admin only. The artists you sent to your Lidarr with `POST /artists/{id}/lidarr`, newest first, each saying
* whether its music has arrived: `waiting` while there is none in the library, `arrived` once there is (the
* day it was first noticed is `arrived_at`; it is noticed when this list is read, by the artist you sent or by
* an artist of the same name that a scan made). Sending an artist again keeps one request with the first date.
* Only what you sent is listed, never what is in Lidarr. `counts` are of all your requests, whatever `status`.
*/
get: operations["listLidarrRequests"];
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/artists/{id}/lidarr": {
parameters: {
query?: never;
@ -3339,6 +3363,38 @@ export interface components {
/** @description Whether the event counts as a listen under the current rule. */
counted: boolean;
};
LidarrRequestPage: {
items: {
/** Format: int64 */
id: number;
artist: {
/**
* Format: int64
* @description The artist on this server, null when it is gone.
*/
id: number | null;
name: string;
};
mbid: string | null;
/**
* Format: date-time
* @description The first time it was sent.
*/
requested_at: string;
/** @enum {string} */
status: "waiting" | "arrived";
/** Format: date-time */
arrived_at: string | null;
}[];
/** @description Size of the whole result (of the `status`), not of this page. */
total: number;
limit: number;
offset: number;
counts: {
waiting: number;
arrived: number;
};
};
ListeningSuggestion: {
name: string;
/** @description The artist on this server, null when there is none yet. */
@ -6947,6 +7003,35 @@ export interface operations {
};
};
};
listLidarrRequests: {
parameters: {
query?: {
/** @description Page size. 50 when absent or 0; values above 200 are lowered to 200. */
limit?: components["parameters"]["Limit"];
offset?: components["parameters"]["Offset"];
/** @description Only the requests that are `waiting` or `arrived`. */
status?: "waiting" | "arrived";
};
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description A page of requests. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["LidarrRequestPage"];
};
};
400: components["responses"]["InvalidInput"];
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
};
};
getArtistInLidarr: {
parameters: {
query?: never;

View file

@ -30,7 +30,7 @@ test('the tabs are Home, Search, Library and More, and only on a phone', () => {
expect(nav).toHaveClass('sm:hidden')
})
test.each(['/discover'])('Home is the current tab on %s, which it leads to', (path) => {
test.each(['/discover', '/requests'])('Home is the current tab on %s, which it leads to', (path) => {
const nav = renderTabs(path)
expect(within(nav).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page')
expect(within(nav).getByRole('link', { name: 'Library' })).not.toHaveAttribute('aria-current')

Some files were not shown because too many files have changed in this diff Show more