sonda/internal/usersettings/usersettings.go
tgiachi e0592c0260 feat(settings): my own settings, starting with Discover from my listening
- GET and PATCH /me/settings: settings that belong to the signed-in user.
- First setting, discover_from_listening, is off by default. When on, Discover
  may start from the artists I listen to most, which sends their names to
  Last.fm.
- New user_settings table (migration 0011, SQLite and PostgreSQL), removed with
  the account. A user with no row has the private defaults.
- A misspelt setting is rejected instead of being ignored.
- Spec, contract test and README updated.

Refs #29
2026-11-11 19:16:26 +01:00

52 lines
1.7 KiB
Go

// Package usersettings keeps the settings that belong to one user.
//
// They are choices about privacy and taste, not about the server: a user with no
// stored settings gets the defaults, and the defaults never send anything to a
// provider.
package usersettings
import (
"context"
"errors"
)
// ErrUserNotFound is returned for an account that no longer exists.
var ErrUserNotFound = errors.New("user not found")
// Settings are the choices of one user.
type Settings struct {
// DiscoverFromListening lets Discover start from the artists the user listens
// to most. Doing so sends their names to Last.fm, so it is off until the user
// turns it on.
DiscoverFromListening bool
}
// Patch changes some settings. A nil field is left alone.
type Patch struct {
DiscoverFromListening *bool
}
// Store keeps the settings.
type Store interface {
// Get returns the settings, the defaults for a user who never set any, or
// ErrUserNotFound.
Get(ctx context.Context, userID int64) (Settings, error)
// Update applies the patch and returns the result, or ErrUserNotFound.
Update(ctx context.Context, userID int64, p Patch) (Settings, error)
}
// Service reads and changes a user's own settings.
type Service struct{ store Store }
// NewService builds a Service.
func NewService(store Store) *Service { return &Service{store: store} }
// Get returns the user's settings.
func (s *Service) Get(ctx context.Context, userID int64) (Settings, error) {
return s.store.Get(ctx, userID)
}
// Update applies the patch and returns the settings as they are now.
func (s *Service) Update(ctx context.Context, userID int64, p Patch) (Settings, error) {
return s.store.Update(ctx, userID, p)
}