Skip to content

🎧 Your Personal Streaming Service

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

Deluan67f2a89d8908
diff --git a/model/criteria/fields.go b/model/criteria/fields.go
index 205861f90..89187077f 100644
--- a/model/criteria/fields.go
+++ b/model/criteria/fields.go
@@ -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)
diff --git a/model/criteria/fields_test.go b/model/criteria/fields_test.go
new file mode 100644
index 000000000..2828dbda4
--- /dev/null
+++ b/model/criteria/fields_test.go
@@ -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())
+ })
+ })
+})
diff --git a/model/criteria/operators_test.go b/model/criteria/operators_test.go
index 2b5f2203b..a5ceb914f 100644
--- a/model/criteria/operators_test.go
+++ b/model/criteria/operators_test.go
@@ -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),