package jellyfin
import (
"net/http"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/jellyfin/dto"
"github.com/navidrome/navidrome/utils/req"
"github.com/navidrome/navidrome/utils/slice"
)
func (api *Router) getArtists(w http.ResponseWriter, r *http.Request) {
api.listArtistsByRole(w, r, model.RoleArtist)
}
func (api *Router) getAlbumArtists(w http.ResponseWriter, r *http.Request) {
api.listArtistsByRole(w, r, model.RoleAlbumArtist)
}
func (api *Router) listArtistsByRole(w http.ResponseWriter, r *http.Request, role model.Role) {
ctx := r.Context()
scopeIDs, _, ok := parentIDScope(ctx, r)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
genreIds, ok := decodedQueryIDs(r, "genreids")
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
q := listParams(req.Params(r))
q.scopeIDs = scopeIDs
q.genreIds = genreIds
opts := model.QueryOptions{Offset: q.offset, Max: q.limit}
applySort(&opts, "MusicArtist", q.sortBy, q.sortOrder)
if q.search != "" {
opts.Max = clampLimit(opts.Max, defaultSearchLimit, maxSearchLimit)
}
res, err := api.listArtists(ctx, opts, q, role)
if err != nil {
api.internalError(w, r, err)
return
}
api.ok(w, r, res)
}
func (api *Router) getGenres(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
p := req.Params(r)
opts := model.QueryOptions{Offset: p.IntOr("startindex", 0), Max: p.IntOr("limit", 0)}
res, err := api.listGenres(ctx, opts)
if err != nil {
api.internalError(w, r, err)
return
}
api.ok(w, r, res)
}
func (api *Router) getStudios(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
p := req.Params(r)
scope, _, ok := parentIDScope(ctx, r)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
opts := model.QueryOptions{Sort: "tag_value", Filters: libraryScopeFilter(scope)}
labels, err := api.ds.Tag().GetAll(ctx, model.TagRecordLabel, opts)
if err != nil {
api.internalError(w, r, err)
return
}
items := slice.Map(labels, dto.StudioToBaseItem)
offset, max := p.IntOr("startindex", 0), p.IntOr("limit", 0)
api.ok(w, r, result(paginate(items, offset, max), len(items), offset))
}
func (api *Router) getQueryFiltersLegacy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
scope, _, ok := parentIDScope(ctx, r)
if !ok {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
genreOpts := model.QueryOptions{Sort: "name", Filters: libraryScopeFilter(scope)}
genres, err := api.ds.Genre().GetAll(ctx, genreOpts)
if err != nil {
api.internalError(w, r, err)
return
}
years, err := api.ds.Album().GetYears(ctx, scope...)
if err != nil {
api.internalError(w, r, err)
return
}
api.ok(w, r, dto.QueryFiltersLegacy{
Genres: slice.Map(genres, func(g model.Genre) string { return g.Name }),
Tags: []string{},
OfficialRatings: []string{},
Years: years,
})
}