sonda/internal/feedback/feedback.go
tgiachi a6dbf60ac8 feat(feedback): a wishlist and "not interested"
- PUT/DELETE /wishlist/{artist} and GET /wishlist: the artists I want.
- POST/DELETE /discover/{artist}/dismiss and GET /discover/dismissed: the
  artists I am not interested in. An artist is wished or dismissed, never both,
  and both lists are mine alone.
- Discover never suggests a dismissed artist, does not use one as a seed taken
  from my listening, and marks the wished ones it shows (wishlisted).
- New artist_feedback table (migration 0012, SQLite and PostgreSQL), removed
  with the account.
- An artist I marked is not cleaned up when the list that named it is replaced,
  and goes when its last mark is taken off and nobody names it. Taking a
  provider's data back still removes what only it named, marks included.
- Spec, contract test and README updated.

Closes #35
2026-11-11 20:11:18 +01:00

142 lines
4.1 KiB
Go

// Package feedback keeps what a user decided about an artist: they want it (the
// wishlist) or they are not interested (dismissed). Both are theirs alone.
//
// An artist is on the wishlist or dismissed, never both: marking it again with the
// other kind replaces the first. Nothing here contacts anyone and nothing is
// downloaded; the wishlist is only a list for the user, and for a download client
// they may set up later.
package feedback
import (
"context"
"errors"
"fmt"
"time"
)
// Kind is what the user decided.
type Kind string
// The two decisions.
const (
Wishlist Kind = "wishlist"
Dismissed Kind = "dismissed"
)
// Paging of the lists.
const (
DefaultLimit = 50
MaxLimit = 200
)
// Errors returned by the service and the store.
var (
ErrArtistNotFound = errors.New("artist not found")
ErrUserNotFound = errors.New("user not found") // the account was deleted
ErrInvalidKind = errors.New("invalid kind")
ErrInvalidQuery = errors.New("invalid query")
)
// ArtistRef is the short form of an artist.
type ArtistRef struct {
ID int64
Name string
}
// Entry is one marked artist. InLibrary says whether there is music of it to play.
type Entry struct {
Artist ArtistRef
InLibrary bool
At time.Time // when it was marked
}
// Page is one page of a list and the size of the whole list.
type Page struct {
Limit int
Offset int
}
// Listing is a page of entries.
type Listing struct {
Items []Entry
Total int
Limit int
Offset int
}
// Store keeps the decisions.
type Store interface {
// Mark records the decision, replacing the other kind, and keeps the first date
// when the same kind is marked again. It returns ErrArtistNotFound for an artist
// that cannot be seen (no music to play and not named by a provider) and
// ErrUserNotFound for an account that is gone.
Mark(ctx context.Context, userID, artistID int64, kind Kind, at time.Time) error
// Unmark removes the decision of that kind; there is nothing to do when there is
// none. It returns ErrArtistNotFound for an artist that does not exist.
Unmark(ctx context.Context, userID, artistID int64, kind Kind) error
// List returns the user's entries of one kind, newest first, and how many there are.
List(ctx context.Context, userID int64, kind Kind, limit, offset int) ([]Entry, int, error)
// Marks returns every decision of the user, by artist.
Marks(ctx context.Context, userID int64) (map[int64]Kind, error)
}
// Service validates and records decisions.
type Service struct{ store Store }
// NewService builds a Service.
func NewService(store Store) *Service { return &Service{store: store} }
func validKind(k Kind) error {
if k != Wishlist && k != Dismissed {
return fmt.Errorf("%w: %q", ErrInvalidKind, string(k))
}
return nil
}
// Add marks an artist.
func (s *Service) Add(ctx context.Context, userID, artistID int64, kind Kind) error {
if err := validKind(kind); err != nil {
return err
}
if artistID <= 0 {
return ErrArtistNotFound
}
return s.store.Mark(ctx, userID, artistID, kind, time.Now())
}
// Remove takes the mark of that kind off an artist.
func (s *Service) Remove(ctx context.Context, userID, artistID int64, kind Kind) error {
if err := validKind(kind); err != nil {
return err
}
if artistID <= 0 {
return ErrArtistNotFound
}
return s.store.Unmark(ctx, userID, artistID, kind)
}
// List returns a page of the user's artists of one kind.
func (s *Service) List(ctx context.Context, userID int64, kind Kind, p Page) (Listing, error) {
if err := validKind(kind); err != nil {
return Listing{}, err
}
if p.Limit < 0 || p.Offset < 0 {
return Listing{}, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalidQuery)
}
switch {
case p.Limit == 0:
p.Limit = DefaultLimit
case p.Limit > MaxLimit:
p.Limit = MaxLimit
}
items, total, err := s.store.List(ctx, userID, kind, p.Limit, p.Offset)
if err != nil {
return Listing{}, err
}
return Listing{Items: items, Total: total, Limit: p.Limit, Offset: p.Offset}, nil
}
// Marks returns every decision of the user, by artist.
func (s *Service) Marks(ctx context.Context, userID int64) (map[int64]Kind, error) {
return s.store.Marks(ctx, userID)
}