mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-18 07:30:29 +00: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.
228 lines
8.3 KiB
Go
228 lines
8.3 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/server/jellyfin/dto"
|
|
"github.com/navidrome/navidrome/utils/req"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
)
|
|
|
|
// similarWait bounds how long a Similar request waits for the provider fetch. Returning the real
|
|
// result beats an instant empty list, which clients cache as "no similar items exist". A var so
|
|
// tests can shorten it.
|
|
var similarWait = 10 * time.Second
|
|
|
|
const (
|
|
defaultSimilarLimit = 20
|
|
maxSimilarLimit = 100
|
|
// A mix is a playback queue, not a "related items" list: Finamp's Radio Mix asks for 250, so the
|
|
// Similar ceiling would truncate it. Real Jellyfin builds mixes from a 200-track genre query.
|
|
maxInstantMixLimit = 500
|
|
)
|
|
|
|
// similarFetchTimeout bounds the detached background fetch so a hung provider can't hold a goroutine
|
|
// indefinitely.
|
|
const similarFetchTimeout = time.Minute
|
|
|
|
// awaitSimilar runs fetch on a detached background context (so it completes and caches even if the
|
|
// request times out or the client disconnects), waiting up to similarWait then answering empty.
|
|
// Identical concurrent requests share one fetch via singleflight; the key includes the user since
|
|
// mapped items embed that user's annotations.
|
|
func (api *Router) awaitSimilar(ctx context.Context, id string, limit int, fetch func(context.Context) dto.QueryResult) dto.QueryResult {
|
|
u, _ := request.UserFrom(ctx)
|
|
key := fmt.Sprintf("%s|%s|%d", u.ID, id, limit)
|
|
ch := api.similarFlight.DoChan(key, func() (any, error) {
|
|
bgCtx, cancel := context.WithTimeout(request.WithUser(context.Background(), u), similarFetchTimeout)
|
|
defer cancel()
|
|
return fetch(bgCtx), nil
|
|
})
|
|
select {
|
|
case res := <-ch:
|
|
return res.Val.(dto.QueryResult)
|
|
case <-time.After(similarWait):
|
|
return result(nil, 0, 0)
|
|
}
|
|
}
|
|
|
|
// getSimilarArtists answers GET /Artists/{itemId}/Similar with related artists from the same
|
|
// external.Provider that powers Subsonic's getArtistInfo2. Only artists present in the library are
|
|
// returned. Any provider error degrades to an empty result, not a 404 the client would keep retrying.
|
|
func (api *Router) getSimilarArtists(w http.ResponseWriter, r *http.Request) {
|
|
id, ok := itemIDParam(w, r, "itemId")
|
|
if !ok {
|
|
return
|
|
}
|
|
limit := clampLimit(req.Params(r).IntOr("limit", 0), defaultSimilarLimit, maxSimilarLimit)
|
|
api.ok(w, r, api.awaitSimilar(r.Context(), id, limit, func(ctx context.Context) dto.QueryResult {
|
|
return api.similarArtists(ctx, id, limit)
|
|
}))
|
|
}
|
|
|
|
// getSimilarItems answers GET /Items/{itemId}/Similar with items of the target's kind: similar
|
|
// songs for a track, albums for an album, artists for an artist. An unresolvable id yields an empty
|
|
// result (not 404) so the client stops retrying.
|
|
func (api *Router) getSimilarItems(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
id, ok := itemIDParam(w, r, "itemId")
|
|
if !ok {
|
|
return
|
|
}
|
|
limit := clampLimit(req.Params(r).IntOr("limit", 0), defaultSimilarLimit, maxSimilarLimit)
|
|
|
|
entity, err := model.GetEntityByID(ctx, api.ds, id)
|
|
if err != nil {
|
|
api.ok(w, r, result(nil, 0, 0))
|
|
return
|
|
}
|
|
api.ok(w, r, api.awaitSimilar(ctx, id, limit, func(ctx context.Context) dto.QueryResult {
|
|
switch entity.(type) {
|
|
case *model.Artist:
|
|
return api.similarArtists(ctx, id, limit)
|
|
case *model.Album:
|
|
return api.similarAlbums(ctx, id, limit)
|
|
default: // *model.MediaFile
|
|
return api.similarSongs(ctx, id, limit)
|
|
}
|
|
}))
|
|
}
|
|
|
|
// getSimilarAlbums answers GET /Albums/{itemId}/Similar, powering Finamp's albumMix radio mode.
|
|
func (api *Router) getSimilarAlbums(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
id, ok := itemIDParam(w, r, "itemId")
|
|
if !ok {
|
|
return
|
|
}
|
|
limit := clampLimit(req.Params(r).IntOr("limit", 0), defaultSimilarLimit, maxSimilarLimit)
|
|
api.ok(w, r, api.awaitSimilar(ctx, "albsim|"+id, limit, func(ctx context.Context) dto.QueryResult {
|
|
return api.similarAlbums(ctx, id, limit)
|
|
}))
|
|
}
|
|
|
|
// getInstantMix answers GET /Items/{itemId}/InstantMix. Finamp plays exactly what is returned, so
|
|
// a track seed leads its own mix; provider errors and unknown seeds degrade to seed-only/empty
|
|
// results, never a 404 the client would surface as an error.
|
|
func (api *Router) getInstantMix(w http.ResponseWriter, r *http.Request) {
|
|
id, ok := itemIDParam(w, r, "itemId")
|
|
if !ok {
|
|
return
|
|
}
|
|
api.instantMix(w, r, id)
|
|
}
|
|
|
|
// getInstantMixByQuery serves the legacy Artists/InstantMix and MusicGenres/InstantMix forms,
|
|
// which take the seed as ?id= instead of a path segment.
|
|
func (api *Router) getInstantMixByQuery(w http.ResponseWriter, r *http.Request) {
|
|
id, ok := dto.DecodeID(req.Params(r).StringOr("id", ""))
|
|
if !ok {
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
api.instantMix(w, r, id)
|
|
}
|
|
|
|
func (api *Router) instantMix(w http.ResponseWriter, r *http.Request, id string) {
|
|
ctx := r.Context()
|
|
limit := clampLimit(req.Params(r).IntOr("limit", 0), defaultSimilarLimit, maxInstantMixLimit)
|
|
|
|
// Genre ids don't resolve via GetEntityByID, so a not-found entity is fine: it is just "not a
|
|
// song" and the provider knows what to do with it. A real lookup failure still stops here.
|
|
entity, err := model.GetEntityByID(ctx, api.ds, id)
|
|
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
|
api.ok(w, r, result(nil, 0, 0))
|
|
return
|
|
}
|
|
mf, isSong := entity.(*model.MediaFile)
|
|
if isSong {
|
|
if u, _ := request.UserFrom(ctx); !u.HasLibraryAccess(mf.LibraryID) {
|
|
api.ok(w, r, result(nil, 0, 0))
|
|
return
|
|
}
|
|
}
|
|
// Prefixed key: a mix must not share the singleflight/cache slot with a Similar request.
|
|
tail := api.awaitSimilar(ctx, "mix|"+id, limit, func(ctx context.Context) dto.QueryResult {
|
|
return api.similarSongs(ctx, id, limit)
|
|
})
|
|
if !isSong {
|
|
// Container seeds: the provider's similar songs already blend the seed's own tracks.
|
|
api.ok(w, r, tail)
|
|
return
|
|
}
|
|
// The seed leads the mix and must not depend on the provider: a slow or failing provider times
|
|
// the await out with an empty tail, but the tapped track still plays.
|
|
items := []dto.BaseItemDto{dto.SongToBaseItem(*mf, nil)}
|
|
for _, it := range tail.Items {
|
|
if len(items) >= limit {
|
|
break
|
|
}
|
|
if it.Id != items[0].Id {
|
|
items = append(items, it)
|
|
}
|
|
}
|
|
api.ok(w, r, result(items, len(items), 0))
|
|
}
|
|
|
|
func (api *Router) similarArtists(ctx context.Context, id string, limit int) dto.QueryResult {
|
|
artist, err := api.provider.UpdateArtistInfo(ctx, id, limit, false)
|
|
if err != nil {
|
|
log.Debug(ctx, "Jellyfin API: no similar artists", "id", id, err)
|
|
return result(nil, 0, 0)
|
|
}
|
|
present := slice.Filter(artist.SimilarArtists, func(a model.Artist) bool { return a.ID != "" })
|
|
items := slice.Map(present, func(a model.Artist) dto.BaseItemDto { return dto.ArtistToBaseItem(a, nil) })
|
|
return result(items, len(items), 0)
|
|
}
|
|
|
|
func (api *Router) similarSongs(ctx context.Context, id string, limit int) dto.QueryResult {
|
|
songs, err := api.provider.SimilarSongs(ctx, id, limit)
|
|
if err != nil {
|
|
log.Debug(ctx, "Jellyfin API: no similar songs", "id", id, err)
|
|
return result(nil, 0, 0)
|
|
}
|
|
// Filter to the caller's libraries; the provider can return songs from any library.
|
|
u, _ := request.UserFrom(ctx)
|
|
var items []dto.BaseItemDto
|
|
for _, mf := range songs {
|
|
if u.HasLibraryAccess(mf.LibraryID) {
|
|
items = append(items, dto.SongToBaseItem(mf, nil))
|
|
}
|
|
}
|
|
return result(items, len(items), 0)
|
|
}
|
|
|
|
// similarAlbums derives similar albums from the provider's similar-songs signal (there's no direct
|
|
// "similar albums" source), keeping each album once in first-seen order and resolving it to a full
|
|
// model.Album for cover art and metadata.
|
|
func (api *Router) similarAlbums(ctx context.Context, id string, limit int) dto.QueryResult {
|
|
songs, err := api.provider.SimilarSongs(ctx, id, limit*5)
|
|
if err != nil {
|
|
log.Debug(ctx, "Jellyfin API: no similar albums", "id", id, err)
|
|
return result(nil, 0, 0)
|
|
}
|
|
u, _ := request.UserFrom(ctx)
|
|
// An album is not similar to itself, and the sampled-seed fallback returns its own tracks.
|
|
seen := map[string]bool{id: true}
|
|
var items []dto.BaseItemDto
|
|
for _, s := range songs {
|
|
if s.AlbumID == "" || seen[s.AlbumID] {
|
|
continue
|
|
}
|
|
seen[s.AlbumID] = true
|
|
if al, err := api.ds.Album().Get(ctx, s.AlbumID); err == nil && u.HasLibraryAccess(al.LibraryID) {
|
|
items = append(items, dto.AlbumToBaseItem(*al, nil))
|
|
if len(items) >= limit {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return result(items, len(items), 0)
|
|
}
|