mirror of
https://github.com/navidrome/navidrome.git
synced 2026-04-03 06:41:01 +00:00
* fix: prevent infinite loop in Type filter autocomplete Fixed an infinite loop issue in the album Type filter caused by an inline arrow function in the optionText prop. The inline function created a new reference on every render, causing React-Admin's AutocompleteInput to continuously re-fetch data from the /api/tag endpoint. The solution extracts the formatting function outside the component scope as formatReleaseType, ensuring a stable function reference across renders. This prevents unnecessary re-renders and API calls while maintaining the humanized display format for release type values. * fix: enable multi-valued releasetype in smart playlists Smart playlists can now match all values in multi-valued releasetype tags. Previously, the albumtype field was mapped to the single-valued mbz_album_type database field, which only stored the first value from tags like album; soundtrack. This prevented smart playlists from matching albums with secondary release types like soundtrack, live, or compilation when tagged by MusicBrainz Picard. The fix removes the direct database field mapping and allows both albumtype and releasetype to use the multi-valued tag system. The albumtype field is now an alias that points to the releasetype tag field, ensuring both query the same JSON path in the tags column. This maintains backward compatibility with the documented albumtype field while enabling proper multi-value tag matching. Added tests to verify both releasetype and albumtype correctly generate multi-valued tag queries. Fixes #4616 * fix: resolve albumtype alias for all operators and sorting Codex correctly identified that the initial fix only worked for Contains/StartsWith/EndsWith operators. The alias resolution was happening too late in the code path. Fixed by resolving the alias in two places: 1. tagCond.ToSql() - now uses the actual field name (releasetype) in the JSON path 2. Criteria.OrderBy() - now uses the actual field name when building sort expressions Added tests for Is/IsNot operators and sorting to ensure complete coverage.
160 lines
3.6 KiB
Go
160 lines
3.6 KiB
Go
// Package criteria implements a Criteria API based on Masterminds/squirrel
|
|
package criteria
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/log"
|
|
)
|
|
|
|
type Expression = squirrel.Sqlizer
|
|
|
|
type Criteria struct {
|
|
Expression
|
|
Sort string
|
|
Order string
|
|
Limit int
|
|
Offset int
|
|
}
|
|
|
|
func (c Criteria) OrderBy() string {
|
|
if c.Sort == "" {
|
|
c.Sort = "title"
|
|
}
|
|
|
|
order := strings.ToLower(strings.TrimSpace(c.Order))
|
|
if order != "" && order != "asc" && order != "desc" {
|
|
log.Error("Invalid value in 'order' field. Valid values: 'asc', 'desc'", "order", c.Order)
|
|
order = ""
|
|
}
|
|
|
|
parts := strings.Split(c.Sort, ",")
|
|
fields := make([]string, 0, len(parts))
|
|
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" {
|
|
continue
|
|
}
|
|
|
|
dir := "asc"
|
|
if strings.HasPrefix(p, "+") || strings.HasPrefix(p, "-") {
|
|
if strings.HasPrefix(p, "-") {
|
|
dir = "desc"
|
|
}
|
|
p = strings.TrimSpace(p[1:])
|
|
}
|
|
|
|
sortField := strings.ToLower(p)
|
|
f := fieldMap[sortField]
|
|
if f == nil {
|
|
log.Error("Invalid field in 'sort' field", "sort", sortField)
|
|
continue
|
|
}
|
|
|
|
var mapped string
|
|
|
|
if f.order != "" {
|
|
mapped = f.order
|
|
} else if f.isTag {
|
|
// Use the actual field name (handles aliases like albumtype -> releasetype)
|
|
tagName := sortField
|
|
if f.field != "" {
|
|
tagName = f.field
|
|
}
|
|
mapped = "COALESCE(json_extract(media_file.tags, '$." + tagName + "[0].value'), '')"
|
|
} else if f.isRole {
|
|
mapped = "COALESCE(json_extract(media_file.participants, '$." + sortField + "[0].name'), '')"
|
|
} else {
|
|
mapped = f.field
|
|
}
|
|
if f.numeric {
|
|
mapped = fmt.Sprintf("CAST(%s AS REAL)", mapped)
|
|
}
|
|
// If the global 'order' field is set to 'desc', reverse the default or field-specific sort direction.
|
|
// This ensures that the global order applies consistently across all fields.
|
|
if order == "desc" {
|
|
if dir == "asc" {
|
|
dir = "desc"
|
|
} else {
|
|
dir = "asc"
|
|
}
|
|
}
|
|
|
|
fields = append(fields, mapped+" "+dir)
|
|
}
|
|
|
|
return strings.Join(fields, ", ")
|
|
}
|
|
|
|
func (c Criteria) ToSql() (sql string, args []any, err error) {
|
|
return c.Expression.ToSql()
|
|
}
|
|
|
|
func (c Criteria) ChildPlaylistIds() []string {
|
|
if c.Expression == nil {
|
|
return nil
|
|
}
|
|
|
|
if parent := c.Expression.(interface{ ChildPlaylistIds() (ids []string) }); parent != nil {
|
|
return parent.ChildPlaylistIds()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c Criteria) MarshalJSON() ([]byte, error) {
|
|
aux := struct {
|
|
All []Expression `json:"all,omitempty"`
|
|
Any []Expression `json:"any,omitempty"`
|
|
Sort string `json:"sort,omitempty"`
|
|
Order string `json:"order,omitempty"`
|
|
Limit int `json:"limit,omitempty"`
|
|
Offset int `json:"offset,omitempty"`
|
|
}{
|
|
Sort: c.Sort,
|
|
Order: c.Order,
|
|
Limit: c.Limit,
|
|
Offset: c.Offset,
|
|
}
|
|
switch rules := c.Expression.(type) {
|
|
case Any:
|
|
aux.Any = rules
|
|
case All:
|
|
aux.All = rules
|
|
default:
|
|
aux.All = All{rules}
|
|
}
|
|
return json.Marshal(aux)
|
|
}
|
|
|
|
func (c *Criteria) UnmarshalJSON(data []byte) error {
|
|
var aux struct {
|
|
All unmarshalConjunctionType `json:"all"`
|
|
Any unmarshalConjunctionType `json:"any"`
|
|
Sort string `json:"sort"`
|
|
Order string `json:"order"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
return err
|
|
}
|
|
if len(aux.Any) > 0 {
|
|
c.Expression = Any(aux.Any)
|
|
} else if len(aux.All) > 0 {
|
|
c.Expression = All(aux.All)
|
|
} else {
|
|
return errors.New("invalid criteria json. missing rules (key 'all' or 'any')")
|
|
}
|
|
c.Sort = aux.Sort
|
|
c.Order = aux.Order
|
|
c.Limit = aux.Limit
|
|
c.Offset = aux.Offset
|
|
return nil
|
|
}
|