Skip to content

🎧 Your Personal Streaming Service

fix(scanner): keep album tag order from the files instead of alphabetical (#5872)

Deluan Quintãocb0a6cedd6c4
diff --git a/model/mediafile_test.go b/model/mediafile_test.go
index 097e3ca54..9ca3489bb 100644
--- a/model/mediafile_test.go
+++ b/model/mediafile_test.go
@@ -218,11 +218,11 @@ var _ = Describe("MediaFiles", func() {
{Tags: Tags{"genre": []string{"Alternative", "Rock"}}},
}
})
- It("sets the correct Genre, sorted by frequency, then alphabetically", func() {
+ It("sets the correct Genre, sorted by frequency, then by order of appearance", func() {
album := mfs.ToAlbum()
Expect(album.Tags).To(HaveLen(2))
- Expect(album.Tags).To(HaveKeyWithValue(TagGenre, []string{"Rock", "Alternative", "Punk"}))
- Expect(album.Tags).To(HaveKeyWithValue(TagMood, []string{"Chill", "Happy"}))
+ Expect(album.Tags).To(HaveKeyWithValue(TagGenre, []string{"Rock", "Punk", "Alternative"}))
+ Expect(album.Tags).To(HaveKeyWithValue(TagMood, []string{"Happy", "Chill"}))
})
})
When("we have tags with mismatching case", func() {
diff --git a/model/tag.go b/model/tag.go
index bb4fce181..234cfb359 100644
--- a/model/tag.go
+++ b/model/tag.go
@@ -24,13 +24,17 @@ type TagList []Tag
func (l TagList) GroupByFrequency() Tags {
grouped := map[string]map[string]int{}
values := map[string]string{}
- for _, t := range l {
+ firstSeen := map[string]int{}
+ for i, t := range l {
if m, ok := grouped[string(t.TagName)]; !ok {
grouped[string(t.TagName)] = map[string]int{t.ID: 1}
} else {
m[t.ID]++
}
values[t.ID] = t.TagValue
+ if _, ok := firstSeen[t.ID]; !ok {
+ firstSeen[t.ID] = i
+ }
}
tags := Tags{}
@@ -42,7 +46,7 @@ func (l TagList) GroupByFrequency() Tags {
slices.SortFunc(idList, func(a, b string) int {
return cmp.Or(
cmp.Compare(counts[b], counts[a]),
- cmp.Compare(values[a], values[b]),
+ cmp.Compare(firstSeen[a], firstSeen[b]),
)
})
tags[TagName(name)] = slice.Map(idList, func(id string) string { return values[id] })
diff --git a/model/tag_test.go b/model/tag_test.go
index c01aa0b4c..4dc99019b 100644
--- a/model/tag_test.go
+++ b/model/tag_test.go
@@ -93,7 +93,7 @@ var _ = Describe("Tag", func() {
Expect(groupedTags).To(HaveKeyWithValue(TagName("artist"), []string{"The Beatles", "The Rolling Stones"}))
})
- It("should sort tags by name when frequency is the same", func() {
+ It("should keep the order the values appeared in when frequency is the same", func() {
tagList := TagList{
NewTag("genre", "Jazz"),
NewTag("genre", "Rock"),
@@ -103,7 +103,7 @@ var _ = Describe("Tag", func() {
groupedTags := tagList.GroupByFrequency()
- Expect(groupedTags).To(HaveKeyWithValue(TagName("genre"), []string{"Alternative", "Jazz", "Pop", "Rock"}))
+ Expect(groupedTags).To(HaveKeyWithValue(TagName("genre"), []string{"Jazz", "Rock", "Alternative", "Pop"}))
})
It("should normalize casing", func() {
tagList := TagList{