sonda/internal/favorites/favorites.go
tgiachi 74f683d26e feat: favourites of songs, albums and artists, in the API and in apps
A person can mark songs, albums and artists as favourites. They are theirs
alone: never shown to a friend, never sent anywhere. Only what can be played is
listed.

- migration 0035 favorites, internal/favorites, storage.FavoritesRepo
- PUT/DELETE /me/favorites/{kind}/{id}, GET /me/favorites/{kind} and
  GET /me/favorites/ids, in the OpenAPI spec
- catalog.Filter IDs now also reads artists by id
- OpenSubsonic: star, unstar, getStarred2, the starred date on songs, albums and
  artists, and getAlbumList2 of type starred
2026-11-16 10:51:07 +01:00

166 lines
4.9 KiB
Go

// Package favorites keeps the songs, albums and artists a person marked as favourites.
//
// They are the person's own: never shown to a friend and never sent anywhere. Only music of this server can be a
// favourite. A favourite whose music is gone (or cannot be played any more) is not listed, and is listed again if the
// music comes back.
package favorites
import (
"context"
"errors"
"fmt"
"time"
"git.kmalloc.org/squid/sonda/internal/catalog"
)
// Kind is what is marked.
type Kind string
// The kinds of favourite.
const (
Artist Kind = "artist"
Album Kind = "album"
Track Kind = "track"
)
// Errors returned by the service.
var (
// ErrNotFound is an item that is not in the catalogue, or a kind that does not exist.
ErrNotFound = errors.New("not found")
ErrInvalid = errors.New("invalid input")
)
// Paging limits of a list of favourites.
const (
DefaultLimit = 50
MaxLimit = 200
)
// ParseKind reads the form a kind has in an address: artists, albums or tracks.
func ParseKind(s string) (Kind, bool) {
switch s {
case "artists":
return Artist, true
case "albums":
return Album, true
case "tracks":
return Track, true
}
return "", false
}
// Store keeps the favourites. It lists only items that can be played.
type Store interface {
// Add marks an item; marking it again keeps the first date.
Add(ctx context.Context, userID int64, kind Kind, id int64, at time.Time) error
// Remove unmarks an item; one that is not marked is not an error.
Remove(ctx context.Context, userID int64, kind Kind, id int64) error
// Page returns the ids of a page of the favourites of a kind, the newest first, and how many there are.
Page(ctx context.Context, userID int64, kind Kind, limit, offset int) ([]int64, int, error)
// IDs returns the ids of every favourite of a kind, the newest first.
IDs(ctx context.Context, userID int64, kind Kind) ([]int64, error)
// Marks returns when each of the ids that are favourites was marked.
Marks(ctx context.Context, userID int64, kind Kind, ids []int64) (map[int64]time.Time, error)
}
// Catalog says what can be a favourite: *catalog.Service.
type Catalog interface {
Artist(ctx context.Context, id int64) (catalog.Artist, error)
Album(ctx context.Context, id int64) (catalog.Album, error)
Track(ctx context.Context, id int64) (catalog.Track, error)
}
// Service marks and lists favourites.
type Service struct {
store Store
catalog Catalog
now func() time.Time
}
// NewService builds a Service.
func NewService(store Store, catalog Catalog) *Service {
return &Service{store: store, catalog: catalog, now: time.Now}
}
func valid(kind Kind) bool { return kind == Artist || kind == Album || kind == Track }
// Add marks an item as a favourite of a person.
func (s *Service) Add(ctx context.Context, userID int64, kind Kind, id int64) error {
if !valid(kind) || id < 1 {
return ErrNotFound
}
var err error
switch kind {
case Artist:
var a catalog.Artist
if a, err = s.catalog.Artist(ctx, id); err == nil && a.TrackCount == 0 && a.AlbumCount == 0 {
err = catalog.ErrNotFound // an artist a provider named, with no music here
}
case Album:
_, err = s.catalog.Album(ctx, id)
case Track:
_, err = s.catalog.Track(ctx, id)
}
if errors.Is(err, catalog.ErrNotFound) {
return ErrNotFound
}
if err != nil {
return fmt.Errorf("find %s: %w", kind, err)
}
return s.store.Add(ctx, userID, kind, id, s.now())
}
// Remove unmarks an item.
func (s *Service) Remove(ctx context.Context, userID int64, kind Kind, id int64) error {
if !valid(kind) {
return ErrNotFound
}
return s.store.Remove(ctx, userID, kind, id)
}
// Page returns a page of the favourites of a kind, the newest first.
func (s *Service) Page(ctx context.Context, userID int64, kind Kind, limit, offset int) ([]int64, int, error) {
if !valid(kind) {
return nil, 0, ErrNotFound
}
if limit < 0 || offset < 0 {
return nil, 0, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalid)
}
switch {
case limit == 0:
limit = DefaultLimit
case limit > MaxLimit:
limit = MaxLimit
}
return s.store.Page(ctx, userID, kind, limit, offset)
}
// All is every favourite of a person, by kind, as ids.
type All struct {
Artists, Albums, Tracks []int64
}
// IDs returns every favourite of a person, for the hearts of a list.
func (s *Service) IDs(ctx context.Context, userID int64) (All, error) {
var out All
for kind, into := range map[Kind]*[]int64{Artist: &out.Artists, Album: &out.Albums, Track: &out.Tracks} {
ids, err := s.store.IDs(ctx, userID, kind)
if err != nil {
return All{}, err
}
if ids == nil {
ids = []int64{}
}
*into = ids
}
return out, nil
}
// Marks returns when each of the ids that are favourites of a person was marked.
func (s *Service) Marks(ctx context.Context, userID int64, kind Kind, ids []int64) (map[int64]time.Time, error) {
if !valid(kind) {
return nil, ErrNotFound
}
return s.store.Marks(ctx, userID, kind, ids)
}