feat(lidarr): a page of the artists I sent to Lidarr, and whether they arrived
Sonda kept no record of what an admin sent to Lidarr. Each send is now kept (migration 0033, lidarr_requests), once for each artist, with the first date. GET /lidarr/requests lists them newest first and says for each whether its music is in the library: it is noticed when the list is read, by the artist that was sent or by an artist of the same name that a scan made, and the day it first was is kept. The web app has a Requests page for admins, under Discover in the menu and on Home on a phone, with the waiting and the arrived artists apart.
This commit is contained in:
parent
d31d4da1bd
commit
af933ad9ea
13 changed files with 1050 additions and 27 deletions
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
98
internal/providers/lidarr/requests.go
Normal file
98
internal/providers/lidarr/requests.go
Normal 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())
|
||||
}
|
||||
73
internal/providers/lidarr/requests_test.go
Normal file
73
internal/providers/lidarr/requests_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
99
internal/storage/lidarr_requests.go
Normal file
99
internal/storage/lidarr_requests.go
Normal 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()
|
||||
}
|
||||
136
internal/storage/lidarr_requests_test.go
Normal file
136
internal/storage/lidarr_requests_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue