feat(admin): switches for ListenBrainz similarity and MusicBrainz track lists
release / go (push) Successful in 1m50s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m11s
release / android (push) Successful in 4m33s
release / Build signed APK (push) Successful in 4m40s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 22s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 1m50s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m11s
release / android (push) Successful in 4m33s
release / Build signed APK (push) Successful in 4m40s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 22s
release / Verify release artifacts (tag releases only) (push) Skipped
Both lookups ran with no way to stop them. Admin › Metadata sources now has an "Other lookups" group with a switch each, both on by default (lookup_settings, migration 0111). Off, the similarity worker asks ListenBrainz nothing and still links what it already fetched; the duplicate resolver gets ErrLookupOff and decides from the track lists it already has. The setting is backed up, and docs/security.md names the switches. docs/security.md also says every account but the first needs an invite. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// Package lookups holds the admin's switches for the outside lookups that no
|
||||
// metadata source setting covers: ListenBrainz's similar songs and artists,
|
||||
// and MusicBrainz's album track lists for the duplicate resolver. Both are on
|
||||
// unless an admin turns them off under Admin › Metadata sources. Turning one
|
||||
// off stops the requests and keeps what was already fetched.
|
||||
package lookups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
)
|
||||
|
||||
// Settings mirrors the lookup_settings row.
|
||||
type Settings struct {
|
||||
// ListenBrainzSimilarity lets the similarity worker ask ListenBrainz Labs
|
||||
// which songs and artists are like the ones people play.
|
||||
ListenBrainzSimilarity bool
|
||||
// MusicBrainzTrackLists lets the duplicate resolver read an album's track
|
||||
// list from MusicBrainz when it has never been read.
|
||||
MusicBrainzTrackLists bool
|
||||
}
|
||||
|
||||
// Defaults mirrors the table's column defaults, so a database that can't be
|
||||
// read behaves as a new install does.
|
||||
var Defaults = Settings{ListenBrainzSimilarity: true, MusicBrainzTrackLists: true}
|
||||
|
||||
// Service caches the settings and saves them. One instance is shared by the
|
||||
// workers and the admin API, so a save reaches the workers at once.
|
||||
type Service struct {
|
||||
pool *pgxpool.Pool
|
||||
|
||||
mu sync.RWMutex
|
||||
cur Settings
|
||||
}
|
||||
|
||||
// New loads the settings once. It always returns a usable service, holding
|
||||
// the defaults when the load fails; the error says so.
|
||||
func New(ctx context.Context, pool *pgxpool.Pool) (*Service, error) {
|
||||
s := &Service{pool: pool, cur: Defaults}
|
||||
row, err := dbq.New(pool).GetLookupSettings(ctx)
|
||||
if err != nil {
|
||||
return s, fmt.Errorf("lookup settings: load: %w", err)
|
||||
}
|
||||
s.cur = fromRow(row)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Get returns the cached settings. A nil service answers with the defaults.
|
||||
func (s *Service) Get() Settings {
|
||||
if s == nil {
|
||||
return Defaults
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.cur
|
||||
}
|
||||
|
||||
// Set saves and re-caches.
|
||||
func (s *Service) Set(ctx context.Context, in Settings) (Settings, error) {
|
||||
if s == nil {
|
||||
return Settings{}, errors.New("lookup settings: no settings service")
|
||||
}
|
||||
row, err := dbq.New(s.pool).UpdateLookupSettings(ctx, dbq.UpdateLookupSettingsParams{
|
||||
ListenbrainzSimilarity: in.ListenBrainzSimilarity,
|
||||
MusicbrainzTrackLists: in.MusicBrainzTrackLists,
|
||||
})
|
||||
if err != nil {
|
||||
return Settings{}, fmt.Errorf("lookup settings: save: %w", err)
|
||||
}
|
||||
out := fromRow(row)
|
||||
s.mu.Lock()
|
||||
s.cur = out
|
||||
s.mu.Unlock()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func fromRow(row dbq.LookupSetting) Settings {
|
||||
return Settings{
|
||||
ListenBrainzSimilarity: row.ListenbrainzSimilarity,
|
||||
MusicBrainzTrackLists: row.MusicbrainzTrackLists,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package lookups
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
// With no settings service, both lookups are on, as on a new install.
|
||||
func TestGet_WithoutAServiceBothLookupsAreOn(t *testing.T) {
|
||||
var s *Service
|
||||
if got := s.Get(); got != Defaults || !got.ListenBrainzSimilarity || !got.MusicBrainzTrackLists {
|
||||
t.Errorf("nil service = %+v, want both on", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A new install starts with both on; a save is cached for the workers and
|
||||
// read back by a server started afterwards.
|
||||
func TestSettings_StartOnAndKeepWhatIsSaved_Integration(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping in -short mode")
|
||||
}
|
||||
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
||||
}
|
||||
if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil {
|
||||
t.Fatalf("migrate: %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("pool: %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
dbtest.ResetDB(t, pool)
|
||||
|
||||
s, err := New(ctx, pool)
|
||||
if err != nil {
|
||||
t.Fatalf("load: %v", err)
|
||||
}
|
||||
if got := s.Get(); got != Defaults {
|
||||
t.Fatalf("new install = %+v, want both on", got)
|
||||
}
|
||||
|
||||
off := Settings{ListenBrainzSimilarity: false, MusicBrainzTrackLists: true}
|
||||
if _, err := s.Set(ctx, off); err != nil {
|
||||
t.Fatalf("save: %v", err)
|
||||
}
|
||||
if got := s.Get(); got != off {
|
||||
t.Errorf("cached = %+v, want %+v", got, off)
|
||||
}
|
||||
again, err := New(ctx, pool)
|
||||
if err != nil {
|
||||
t.Fatalf("reload: %v", err)
|
||||
}
|
||||
if got := again.Get(); got != off {
|
||||
t.Errorf("after a restart = %+v, want %+v", got, off)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user