mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-14 13:27:09 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
300 lines
10 KiB
Go
300 lines
10 KiB
Go
package external
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand/v2"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/core/agents"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
"github.com/navidrome/navidrome/utils"
|
|
"github.com/navidrome/navidrome/utils/random"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
const (
|
|
maxSeeds = 5
|
|
// Subsonic passes the client's count through unbounded, and it ends up as a SQL limit. 500 is
|
|
// what the widest caller (similarAlbums, limit*5) legitimately asks for.
|
|
maxSimilarSongs = 500
|
|
)
|
|
|
|
func (e *provider) SimilarSongs(ctx context.Context, id string, count int) (model.MediaFiles, error) {
|
|
// Subsonic passes the client's count straight through: a non-positive one has no valid
|
|
// interpretation, and an enormous one overflows the +1 in the local agent's query limit.
|
|
if count <= 0 {
|
|
return nil, nil
|
|
}
|
|
count = min(count, maxSimilarSongs)
|
|
entity, err := model.GetEntityByID(ctx, e.ds, id)
|
|
if err != nil {
|
|
// Genre ids don't resolve via GetEntityByID; look them up before giving up.
|
|
if !errors.Is(err, model.ErrNotFound) {
|
|
return nil, err
|
|
}
|
|
genre, err := e.ds.Genre().Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e.seedMix(ctx, count, func() (model.MediaFiles, error) {
|
|
return e.sampleGenreTracks(ctx, genre, maxSeeds)
|
|
})
|
|
}
|
|
|
|
// Try entity-specific similarity first, then fall back to seed-track sampling.
|
|
switch v := entity.(type) {
|
|
case *model.MediaFile:
|
|
return e.mixFromAgent(ctx, count,
|
|
func() ([]agents.Song, error) {
|
|
return e.ag.GetSimilarSongsByTrack(ctx, v.ID, v.Title, v.Artist, v.MbzRecordingID, count)
|
|
},
|
|
func() (model.MediaFiles, error) {
|
|
return e.similarSongsFallback(ctx, id, count)
|
|
})
|
|
case *model.Album:
|
|
return e.mixFromAgent(ctx, count,
|
|
func() ([]agents.Song, error) {
|
|
return e.ag.GetSimilarSongsByAlbum(ctx, v.ID, v.Name, v.AlbumArtist, v.MbzAlbumID, count)
|
|
},
|
|
func() (model.MediaFiles, error) {
|
|
return e.seedMix(ctx, count, func() (model.MediaFiles, error) {
|
|
return e.sampleAlbumTracks(ctx, v.ID, maxSeeds)
|
|
})
|
|
})
|
|
case *model.Artist:
|
|
return e.mixFromAgent(ctx, count,
|
|
func() ([]agents.Song, error) {
|
|
return e.ag.GetSimilarSongsByArtist(ctx, v.ID, v.Name, v.MbzArtistID, count)
|
|
},
|
|
func() (model.MediaFiles, error) {
|
|
return e.similarSongsFallback(ctx, id, count)
|
|
},
|
|
func() (model.MediaFiles, error) {
|
|
return e.seedMix(ctx, count, func() (model.MediaFiles, error) {
|
|
return e.sampleArtistTracks(ctx, v.ID, maxSeeds)
|
|
})
|
|
})
|
|
case *model.Playlist:
|
|
return e.seedMix(ctx, count, func() (model.MediaFiles, error) {
|
|
return e.samplePlaylistTracks(ctx, v.ID, maxSeeds)
|
|
})
|
|
default:
|
|
log.Warn(ctx, "Unknown entity type", "id", id, "type", fmt.Sprintf("%T", entity))
|
|
return nil, model.ErrNotFound
|
|
}
|
|
}
|
|
|
|
// mixFromAgent returns the agent's recommendations matched to library tracks, topped up from the
|
|
// fallbacks in order when they don't fill the mix on their own.
|
|
func (e *provider) mixFromAgent(ctx context.Context, count int, fetch func() ([]agents.Song, error), fallbacks ...func() (model.MediaFiles, error)) (model.MediaFiles, error) {
|
|
var matched model.MediaFiles
|
|
if songs, err := fetch(); err == nil {
|
|
// Match the whole response: capping at count can stop before a later unique pick.
|
|
matched, err = e.matcher.MatchSongs(ctx, songs, len(songs))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return topUp(ctx, matched, count, fallbacks...)
|
|
}
|
|
|
|
// topUp draws on each source in turn until the mix holds count distinct tracks.
|
|
func topUp(ctx context.Context, res model.MediaFiles, count int, sources ...func() (model.MediaFiles, error)) (model.MediaFiles, error) {
|
|
// The matcher can re-emit a track, so a full-looking res may hold fewer than count unique ones.
|
|
res = dedupByID(res)
|
|
var lastErr error
|
|
for _, more := range sources {
|
|
if len(res) >= count {
|
|
break
|
|
}
|
|
extra, err := more()
|
|
if err != nil {
|
|
log.Debug(ctx, "Could not top up a short mix", "have", len(res), "want", count, err)
|
|
lastErr = err
|
|
continue
|
|
}
|
|
res = dedupByID(append(res, extra...))
|
|
}
|
|
if len(res) == 0 {
|
|
return nil, lastErr
|
|
}
|
|
return res[:min(len(res), count)], nil
|
|
}
|
|
|
|
// seedMix samples seed tracks, runs each through the agent chain's per-track similarity and merges
|
|
// the results, falling back to the seeds themselves so the result is never empty.
|
|
func (e *provider) seedMix(ctx context.Context, count int, sample func() (model.MediaFiles, error)) (model.MediaFiles, error) {
|
|
seeds, err := sample()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(seeds) == 0 {
|
|
return nil, nil
|
|
}
|
|
seeds = seeds[:min(len(seeds), maxSeeds)]
|
|
|
|
// The per-seed similarity calls are independent and hit the (possibly remote) agent chain, so
|
|
// run them concurrently. Best-effort: a seed that errors just contributes nothing.
|
|
perSeed := make([][]agents.Song, len(seeds))
|
|
var g errgroup.Group
|
|
for i, seed := range seeds {
|
|
g.Go(func() error {
|
|
if s, err := e.ag.GetSimilarSongsByTrack(ctx, seed.ID, seed.Title, seed.Artist, seed.MbzRecordingID, count); err == nil {
|
|
perSeed[i] = s
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
_ = g.Wait()
|
|
|
|
var songs []agents.Song
|
|
for _, s := range perSeed {
|
|
songs = append(songs, s...)
|
|
}
|
|
// Match the whole merged set, not just count of it: the matcher re-emits a track when two
|
|
// seeds recommend it identically, so the duplicates have to be dropped before trimming. Every
|
|
// seed reaches the shuffle, so no seed can crowd out the others.
|
|
matched, err := e.matcher.MatchSongs(ctx, songs, len(songs))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
matched = dedupByID(matched)
|
|
if len(matched) == 0 {
|
|
matched = seeds
|
|
}
|
|
//nolint:gosec // shuffle order is not a security decision
|
|
rand.Shuffle(len(matched), func(i, j int) { matched[i], matched[j] = matched[j], matched[i] })
|
|
if len(matched) > count {
|
|
matched = matched[:count]
|
|
}
|
|
return matched, nil
|
|
}
|
|
|
|
func (e *provider) samplePlaylistTracks(ctx context.Context, playlistID string, n int) (model.MediaFiles, error) {
|
|
// Refresh: a smart playlist materializes no tracks until it is evaluated, so skipping it would
|
|
// mix an empty seed set. It is a no-op for regular playlists and inside the refresh delay.
|
|
repo := e.ds.Playlist().Tracks(ctx, playlistID, true)
|
|
if repo == nil {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
// A playlist can hold the same file at several positions, so over-fetch and dedup: a repeated
|
|
// seed wastes an agent call and can reach the mix twice through the seed fallback.
|
|
tracks, err := repo.GetAll(ctx, model.QueryOptions{
|
|
Sort: "random",
|
|
Max: n * 4,
|
|
Filters: squirrel.Eq{"missing": false},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mfs := dedupByID(tracks.MediaFiles())
|
|
return mfs[:min(len(mfs), n)], nil
|
|
}
|
|
|
|
func dedupByID(mfs model.MediaFiles) model.MediaFiles {
|
|
seen := make(map[string]struct{}, len(mfs))
|
|
return slice.Filter(mfs, func(mf model.MediaFile) bool {
|
|
if _, dup := seen[mf.ID]; dup {
|
|
return false
|
|
}
|
|
seen[mf.ID] = struct{}{}
|
|
return true
|
|
})
|
|
}
|
|
|
|
func (e *provider) sampleAlbumTracks(ctx context.Context, albumID string, n int) (model.MediaFiles, error) {
|
|
return e.sampleTracks(ctx, squirrel.Eq{"album_id": albumID}, n)
|
|
}
|
|
|
|
func (e *provider) sampleArtistTracks(ctx context.Context, artistID string, n int) (model.MediaFiles, error) {
|
|
// media_file.artist_id is the deprecated primary artist, so it misses an artist credited only
|
|
// on the album, as on compilations. Same filter the artist listings use.
|
|
filter := persistence.ParticipantIDFilter("media_file", artistID, model.RoleArtist, model.RoleAlbumArtist)
|
|
return e.sampleTracks(ctx, filter, n)
|
|
}
|
|
|
|
func (e *provider) sampleGenreTracks(ctx context.Context, genre *model.Genre, n int) (model.MediaFiles, error) {
|
|
return e.sampleTracks(ctx, persistence.SongGenres.ByID(genre.ID), n)
|
|
}
|
|
|
|
// sampleTracks returns up to n random present tracks. Seeds can end up in the mix verbatim, so
|
|
// missing files would surface as unplayable entries.
|
|
func (e *provider) sampleTracks(ctx context.Context, filter squirrel.Sqlizer, n int) (model.MediaFiles, error) {
|
|
return e.ds.MediaFile().GetRandom(ctx, model.QueryOptions{
|
|
Filters: squirrel.And{filter, squirrel.Eq{"missing": false}},
|
|
Max: n,
|
|
})
|
|
}
|
|
|
|
// similarSongsFallback uses the original similar artists + top songs algorithm. The idea is to
|
|
// get the artist of the given entity, retrieve similar artists, get their top songs, and pick
|
|
// a weighted random selection of songs to return as similar songs.
|
|
func (e *provider) similarSongsFallback(ctx context.Context, id string, count int) (model.MediaFiles, error) {
|
|
artist, err := e.getArtist(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_ = e.callGetSimilarArtists(ctx, e.ag, &artist, 15, false)
|
|
if utils.IsCtxDone(ctx) {
|
|
log.Warn(ctx, "SimilarSongs call canceled", ctx.Err())
|
|
return nil, ctx.Err()
|
|
}
|
|
|
|
weightedSongs := random.NewWeightedChooser[model.MediaFile]()
|
|
addArtist := func(a model.Artist, weightedSongs *random.WeightedChooser[model.MediaFile], count, artistWeight int) error {
|
|
if utils.IsCtxDone(ctx) {
|
|
log.Warn(ctx, "SimilarSongs call canceled", ctx.Err())
|
|
return ctx.Err()
|
|
}
|
|
|
|
topCount := max(count, 20)
|
|
topSongs, err := e.getMatchingTopSongs(ctx, e.ag, &auxArtist{Artist: a}, topCount)
|
|
if err != nil {
|
|
log.Warn(ctx, "Error getting artist's top songs", "artist", a.Name, err)
|
|
return nil
|
|
}
|
|
|
|
weight := topCount * (4 + artistWeight)
|
|
for _, mf := range topSongs {
|
|
weightedSongs.Add(mf, weight)
|
|
weight -= 4
|
|
}
|
|
return nil
|
|
}
|
|
|
|
err = addArtist(artist.Artist, weightedSongs, count, 10)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, a := range artist.SimilarArtists {
|
|
err := addArtist(a, weightedSongs, count, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Count distinct tracks, not picks: a collaboration sits in the chooser once per artist that
|
|
// lists it, and letting those repeats consume the budget strands unique candidates.
|
|
var similarSongs model.MediaFiles
|
|
picked := map[string]struct{}{}
|
|
for len(similarSongs) < count && weightedSongs.Size() > 0 {
|
|
s, err := weightedSongs.Pick()
|
|
if err != nil {
|
|
log.Warn(ctx, "Error getting weighted song", err)
|
|
continue
|
|
}
|
|
if _, dup := picked[s.ID]; dup {
|
|
continue
|
|
}
|
|
picked[s.ID] = struct{}{}
|
|
similarSongs = append(similarSongs, s)
|
|
}
|
|
|
|
return similarSongs, nil
|
|
}
|