release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every import and the Dockerfile's version ldflags. - buildinfo.ProjectURL, the contact outside services see in Minstrel's User-Agent: https://github.com/Fabled-Sword/minstrel. - README's compose example pulls ghcr.io/fabled-sword/minstrel:latest. - docs/security.md sends problem reports to the GitHub issues. - The demo-library stack builds Minstrel from the checkout, since :dev is not published to GHCR. - hygiene.go's same-site example uses example.com names. The workbench CI (.gitea/, ci/, export-ignored) still builds and publishes :dev to the private registry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
// 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"
|
||
|
||
"github.com/Fabled-Sword/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,
|
||
}
|
||
}
|