sonda/internal/opensubsonic/starred.go
tgiachi d6064763e7 fix: OpenSubsonic apps can log in: getUser and the calls that follow
Feishin asks getUser right after the login and failed on the missing user. getUser
now says who the login is, with the roles of an app (play, download, playlists,
covers; never admin), and only about the person of the login.

Also answered with nothing, because apps ask them straight away: getArtistInfo(2),
getAlbumInfo(2), getTopSongs, getSimilarSongs(2), getLyrics, getLyricsBySongId,
getInternetRadioStations, getScanStatus, startScan and the play queue calls, and
getStarred, the older way to ask for the favourites.

A call that is not known is now in the server log with its name.
2026-11-16 11:50:44 +01:00

158 lines
4.3 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, bad := h.starredOf(k)
if bad != nil {
return nil, bad
}
return &Response{Starred2: res}, nil
}
// starred is the same for the apps that ask the older way.
func (h *handler) starred(k *call) (*Response, *Error) {
res, bad := h.starredOf(k)
if bad != nil {
return nil, bad
}
return &Response{Starred: res}, nil
}
func (h *handler) starredOf(k *call) (*SearchResult, *Error) {
res := &SearchResult{}
if h.Favorites == nil {
return 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 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
}