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
141 lines
4 KiB
Go
141 lines
4 KiB
Go
package opensubsonic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/catalog"
|
|
"git.kmalloc.org/squid/sonda/internal/favorites"
|
|
)
|
|
|
|
// Favorites keeps the favourites of a person: *favorites.Service. In an app they are the starred things.
|
|
type Favorites interface {
|
|
Add(ctx context.Context, userID int64, kind favorites.Kind, id int64) error
|
|
Remove(ctx context.Context, userID int64, kind favorites.Kind, id int64) error
|
|
IDs(ctx context.Context, userID int64) (favorites.All, error)
|
|
Marks(ctx context.Context, userID int64, kind favorites.Kind, ids []int64) (map[int64]time.Time, error)
|
|
}
|
|
|
|
// target is something an app stars or unstars.
|
|
type target struct {
|
|
kind favorites.Kind
|
|
id int64
|
|
}
|
|
|
|
// targets reads what a call stars: song ids in id, albums in albumId, artists in artistId. Each may be given many times,
|
|
// and the prefix of an id says what it is whatever parameter it came in.
|
|
func targets(k *call) ([]target, *Error) {
|
|
var out []target
|
|
for _, name := range []string{"id", "albumId", "artistId"} {
|
|
for _, raw := range k.c.Request.Form[name] {
|
|
var (
|
|
kind favorites.Kind
|
|
prefix string
|
|
)
|
|
switch {
|
|
case strings.HasPrefix(raw, trackPrefix):
|
|
kind, prefix = favorites.Track, trackPrefix
|
|
case strings.HasPrefix(raw, albumPrefix):
|
|
kind, prefix = favorites.Album, albumPrefix
|
|
case strings.HasPrefix(raw, artistPrefix):
|
|
kind, prefix = favorites.Artist, artistPrefix
|
|
default:
|
|
return nil, notFound()
|
|
}
|
|
id, bad := idOf(raw, prefix)
|
|
if bad != nil {
|
|
return nil, bad
|
|
}
|
|
out = append(out, target{kind, id})
|
|
}
|
|
}
|
|
if len(out) == 0 {
|
|
return nil, missing("id, albumId or artistId")
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (h *handler) star(k *call) (*Response, *Error) { return h.mark(k, true) }
|
|
func (h *handler) unstar(k *call) (*Response, *Error) { return h.mark(k, false) }
|
|
|
|
// mark stars or unstars what the call names. Something that cannot be found stops the call, and what came before it
|
|
// stays starred.
|
|
func (h *handler) mark(k *call, on bool) (*Response, *Error) {
|
|
if h.Favorites == nil {
|
|
return &Response{}, nil
|
|
}
|
|
all, bad := targets(k)
|
|
if bad != nil {
|
|
return nil, bad
|
|
}
|
|
for _, t := range all {
|
|
var err error
|
|
if on {
|
|
err = h.Favorites.Add(k.ctx, k.principal.UserID, t.kind, t.id)
|
|
} else {
|
|
err = h.Favorites.Remove(k.ctx, k.principal.UserID, t.kind, t.id)
|
|
}
|
|
switch {
|
|
case err == nil:
|
|
case errors.Is(err, favorites.ErrNotFound):
|
|
return nil, notFound()
|
|
default:
|
|
return nil, &Error{Code: ErrGeneric, Message: "the favourite could not be changed"}
|
|
}
|
|
}
|
|
return &Response{}, nil
|
|
}
|
|
|
|
// starred2 is the favourites of the person: artists, albums and songs, the newest first.
|
|
func (h *handler) starred2(k *call) (*Response, *Error) {
|
|
res := &SearchResult{}
|
|
if h.Favorites == nil {
|
|
return &Response{Starred2: res}, nil
|
|
}
|
|
all, err := h.Favorites.IDs(k.ctx, k.principal.UserID)
|
|
if err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
artists, err := h.artistsByID(k, all.Artists)
|
|
if err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
if res.Artist, err = h.artistList(k, artists); err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
albums, err := h.albumsByID(k.ctx, all.Albums)
|
|
if err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
if res.Album, err = h.albums(k, albums); err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
tracks, err := h.tracksByID(k.ctx, all.Tracks)
|
|
if err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
if res.Song, err = h.songs(k, tracks); err != nil {
|
|
return nil, failure(err)
|
|
}
|
|
return &Response{Starred2: res}, nil
|
|
}
|
|
|
|
// artistsByID reads artists by their ids, in the order of the ids.
|
|
func (h *handler) artistsByID(k *call, ids []int64) ([]catalog.Artist, error) {
|
|
byID := map[int64]catalog.Artist{}
|
|
rest := ids
|
|
for len(rest) > 0 {
|
|
chunk := rest[:min(len(rest), catalog.MaxLimit)]
|
|
rest = rest[len(chunk):]
|
|
p, err := h.Catalog.Artists(k.ctx, catalog.Filter{IDs: chunk, Limit: len(chunk)})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, a := range p.Items {
|
|
byID[a.ID] = a
|
|
}
|
|
}
|
|
return inOrder(ids, byID), nil
|
|
}
|