Files
minstrel/internal/coverart/settings.go
T
bvandeusenandClaude Opus 5.5 487e11f59f refactor(providers): cover art and tags share one registry and settings service (#8002)
coverart and tags each carried a line-for-line copy of the provider
registry and the settings service: reconcile at boot, seal and reveal
API keys, bump the sources version when the enabled set or the
registered set changes. A fix to either had to be made twice.

internal/providers now holds Registry, Service (over a small Store
interface) and EnabledAs[T]. Each family keeps its own tables, provider
set and capabilities: a 40-line Store adapter over its sqlc queries and
its capability views (EnabledAlbumProviders, EnabledTrackTagProviders).
Provider, ProviderSettings, TestableProvider, ProviderInfo and
ProviderUpdatePatch stay as the families' names for the shared types.
The service has unit tests over an in-memory store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 12:57:19 -05:00

110 lines
3.4 KiB
Go

package coverart
import (
"context"
"fmt"
"log/slog"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/providers"
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
)
// ProviderUpdatePatch is the admin PATCH body; see providers.UpdatePatch.
type ProviderUpdatePatch = providers.UpdatePatch
// ProviderInfo is a provider as GET /api/admin/cover-sources shows it.
type ProviderInfo = providers.Info
// SettingsService is the cover-art providers' settings: the shared
// providers.Service over cover_art_provider_settings and the
// cover_art_sources_meta version, plus the cover-art capability views.
type SettingsService struct {
*providers.Service
}
// NewSettingsService reconciles the registry with the settings table and
// Configures every provider.
func NewSettingsService(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) (*SettingsService, error) {
svc, err := providers.NewService(ctx, providers.Config{
Registry: registry,
Store: settingsStore{q: dbq.New(pool)},
KeyColumn: secret.CoverArtAPIKey,
Supports: supports,
Logger: logger,
})
if err != nil {
return nil, fmt.Errorf("cover art settings boot: %w", err)
}
return &SettingsService{svc}, nil
}
// EnabledAlbumProviders returns the enabled album-cover providers, in
// registration order.
func (s *SettingsService) EnabledAlbumProviders() []AlbumCoverProvider {
return providers.EnabledAs[AlbumCoverProvider](s.Service)
}
// EnabledArtistProviders returns the enabled artist-image providers, in
// registration order.
func (s *SettingsService) EnabledArtistProviders() []ArtistArtProvider {
return providers.EnabledAs[ArtistArtProvider](s.Service)
}
// supports names what p can fetch, for the admin view.
func supports(p Provider) []string {
var out []string
if _, ok := p.(AlbumCoverProvider); ok {
out = append(out, "album_cover")
}
if _, ok := p.(ArtistArtProvider); ok {
out = append(out, "artist_thumb", "artist_fanart")
}
return out
}
// settingsStore is cover art's providers.Store.
type settingsStore struct{ q *dbq.Queries }
func (st settingsStore) EnsureRow(ctx context.Context, id string, enabled bool, order int32) error {
return st.q.UpsertProviderSettings(ctx, dbq.UpsertProviderSettingsParams{
ProviderID: id, Enabled: enabled, DisplayOrder: order,
})
}
func (st settingsStore) List(ctx context.Context) ([]providers.Row, error) {
rows, err := st.q.ListProviderSettings(ctx)
if err != nil {
return nil, err
}
out := make([]providers.Row, len(rows))
for i, r := range rows {
out[i] = providers.Row{ProviderID: r.ProviderID, Enabled: r.Enabled, APIKey: r.ApiKey}
}
return out, nil
}
func (st settingsStore) Save(ctx context.Context, id string, enabled, keyChanged bool, sealedKey *string) error {
return st.q.UpdateProviderSettings(ctx, dbq.UpdateProviderSettingsParams{
ProviderID: id, Enabled: enabled, Column3: keyChanged, ApiKey: sealedKey,
})
}
func (st settingsStore) Version(ctx context.Context) (int32, error) {
return st.q.GetCurrentSourcesVersion(ctx)
}
func (st settingsStore) BumpVersion(ctx context.Context) (int32, error) {
return st.q.BumpSourcesVersion(ctx)
}
func (st settingsStore) ProvidersHash(ctx context.Context) (string, error) {
return st.q.GetCoverArtProvidersHash(ctx)
}
func (st settingsStore) BumpVersionForProviders(ctx context.Context, hash string) (int32, error) {
return st.q.BumpVersionAndSetProvidersHash(ctx, hash)
}