Fix tracks never "loved" to be selected in Smart Playlists. Refer to https://github.com/navidrome/navidrome/issues/1417#issuecomment-1163423575

This commit is contained in:
Deluan 2022-07-27 20:43:24 -04:00
parent bf1f93ef1a
commit 67f2a89d89
3 changed files with 22 additions and 6 deletions

View File

@ -38,12 +38,12 @@ var fieldMap = map[string]*mappedField{
"bpm": {field: "media_file.bpm"},
"channels": {field: "media_file.channels"},
"genre": {field: "genre.name"},
"loved": {field: "annotation.starred"},
"loved": {field: "COALESCE(annotation.starred, false)"},
"dateloved": {field: "annotation.starred_at"},
"lastplayed": {field: "annotation.play_date"},
"playcount": {field: "COALESCE(annotation.play_count, 0)", order: "annotation.play_count"},
"rating": {field: "COALESCE(annotation.rating, 0)", order: "annotation.rating"},
"random": {field: "-", order: "random()"},
"playcount": {field: "COALESCE(annotation.play_count, 0)"},
"rating": {field: "COALESCE(annotation.rating, 0)"},
"random": {field: "", order: "random()"},
}
type mappedField struct {
@ -54,7 +54,7 @@ type mappedField struct {
func mapFields(expr map[string]interface{}) map[string]interface{} {
m := make(map[string]interface{})
for f, v := range expr {
if dbf := fieldMap[strings.ToLower(f)]; dbf != nil {
if dbf := fieldMap[strings.ToLower(f)]; dbf != nil && dbf.field != "" {
m[dbf.field] = v
} else {
log.Error("Invalid field in criteria", "field", f)

View File

@ -0,0 +1,16 @@
package criteria
import (
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
var _ = Describe("fields", func() {
Describe("mapFields", func() {
It("ignores random fields", func() {
m := map[string]interface{}{"random": "123"}
m = mapFields(m)
gomega.Expect(m).To(gomega.BeEmpty())
})
})
})

View File

@ -20,7 +20,7 @@ var _ = Describe("Operators", func() {
gomega.Expect(args).To(gomega.ConsistOf(expectedArgs))
},
Entry("is [string]", Is{"title": "Low Rider"}, "media_file.title = ?", "Low Rider"),
Entry("is [bool]", Is{"loved": true}, "annotation.starred = ?", true),
Entry("is [bool]", Is{"loved": true}, "COALESCE(annotation.starred, false) = ?", true),
Entry("isNot", IsNot{"title": "Low Rider"}, "media_file.title <> ?", "Low Rider"),
Entry("gt", Gt{"playCount": 10}, "COALESCE(annotation.play_count, 0) > ?", 10),
Entry("lt", Lt{"playCount": 10}, "COALESCE(annotation.play_count, 0) < ?", 10),