navidrome/server/jellyfin/library.go
Deluan Quintão 18205366c8
fix(jellyfin): match Jellyfin's item payloads so strict clients can sync (#6151)
* fix(jellyfin): match Jellyfin's item payloads so strict clients can sync

Manet (iOS/macOS) aborted its whole library sync on the first item that was
missing a key its decoder requires, leaving the library empty (#6147). Every
gap was a field real Jellyfin always sends:

- dates now use .NET's round-trip layout with 7 fractional digits, which Manet
  requires and plain RFC3339 failed
- playlists carry SortName/DateCreated, and every item carries MediaType,
  ImageTags, ChannelId and, when Fields asks, Genres/GenreItems/Tags
- albums carry Artists and LocationType; songs always carry HasLyrics
- the library view is a full CollectionFolder (ChildCount, DateCreated,
  SortName, Path, LocationType, UserData), read from the library rows rather
  than the user projection, which has no counts
- IncludeItemTypes matches case-insensitively and returns nothing for Jellyfin
  kinds Navidrome has none of, instead of falling back to every album

Verified against a real Jellyfin 10.10.7 server and a live Manet client.

* refactor(jellyfin): fold the repeated empty-list defaults into one helper

The three mappers each initialised Genres/GenreItems/Tags the same way, and the
e2e suite grew three near-identical specs walking every item type. Both now go
through a single helper and one table.

* refactor(jellyfin): fill the always-present item fields at the serialization edge

The defaults real Jellyfin puts on every item were spread across three mappers,
so item types nobody had tested yet (playlists, genres, the library view) still
shipped payloads a strict client rejects. stampItem now takes the request's
Fields and fills them for every item, which is provably the only path to JSON.

Also drops the hand-copied BaseItemKind list: only an absent IncludeItemTypes
defaults to albums now, so any type Navidrome does not serve returns nothing,
as it would from Jellyfin. The synthetic playlists folder matches
case-insensitively like the rest, /Items/{libraryId} reads the full library row
instead of the count-less user projection, and PremiereDate and LastPlayedDate
go through jellyfinDate rather than spelling the layout out again.
2026-09-16 07:28:32 -04:00

75 lines
2.8 KiB
Go

package jellyfin
import (
"context"
"net/http"
"strconv"
"github.com/Masterminds/squirrel"
"github.com/go-chi/chi/v5"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/server/jellyfin/dto"
"github.com/navidrome/navidrome/utils/req"
)
// accessibleLibraryIDs returns the ids of the libraries the current user can access. An empty
// slice (non-admin with no libraries) is treated as a no-op/unrestricted by the library filters.
func accessibleLibraryIDs(ctx context.Context) []int {
u, _ := request.UserFrom(ctx)
return u.Libraries.IDs()
}
// resolveLibraryScope handles ParentId's ambiguity: a library id (browsing a UserView) or an
// entity id (artist/album). It's treated as a library only when the user has access; otherwise
// isLibraryParent is false and callers fall through to entity-id handling.
func resolveLibraryScope(ctx context.Context, parentId string) (scopeIDs []int, isLibraryParent bool) {
if parentId != "" {
if id, err := strconv.Atoi(parentId); err == nil {
if u, _ := request.UserFrom(ctx); u.HasLibraryAccess(id) {
return []int{id}, true
}
}
}
return accessibleLibraryIDs(ctx), false
}
// decodeFilterParam separates an absent param from an undecodable one: dropping the filter for a
// stale id would silently widen the query to the whole library. It is the only place that opts
// into treating "absent" as fine — every other caller of dto.DecodeID must 404 on ok=false.
func decodeFilterParam(raw string) (id string, ok bool) {
if raw == "" {
return "", true
}
return dto.DecodeID(raw)
}
// itemIDParam decodes a chi URL id param, writing 404 and reporting false when it isn't a
// well-formed GUID. Handlers must return immediately when ok is false.
func itemIDParam(w http.ResponseWriter, r *http.Request, key string) (string, bool) {
id, ok := dto.DecodeID(chi.URLParam(r, key))
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return "", false
}
return id, true
}
// parentIDScope resolves the request's ParentId param to a library scope (see resolveLibraryScope).
// ok is false when a non-empty ParentId fails to decode (see decodeFilterParam).
func parentIDScope(ctx context.Context, r *http.Request) (scopeIDs []int, isLibraryParent bool, ok bool) {
parentId, ok := decodeFilterParam(req.Params(r).StringOr("parentid", ""))
if !ok {
return nil, false, false
}
scopeIDs, isLibraryParent = resolveLibraryScope(ctx, parentId)
return scopeIDs, isLibraryParent, true
}
// libraryScopeFilter restricts a tag query to the given library scope. Empty scope means
// unrestricted (see accessibleLibraryIDs), so it returns nil rather than an impossible IN ().
func libraryScopeFilter(scope []int) squirrel.Sqlizer {
if len(scope) == 0 {
return nil
}
return squirrel.Eq{"library_tag.library_id": scope}
}