mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Implement new Artist refresh
This commit is contained in:
parent
bce7b163ba
commit
8e640bb858
9 changed files with 170 additions and 167 deletions
|
@ -1,6 +1,11 @@
|
|||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/utils/slice"
|
||||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
Annotations `structs:"-"`
|
||||
|
@ -42,13 +47,35 @@ func (a Album) CoverArtID() ArtworkID {
|
|||
return artworkIDFromAlbum(a)
|
||||
}
|
||||
|
||||
type (
|
||||
Albums []Album
|
||||
DiscID struct {
|
||||
AlbumID string `json:"albumId"`
|
||||
DiscNumber int `json:"discNumber"`
|
||||
type DiscID struct {
|
||||
AlbumID string `json:"albumId"`
|
||||
DiscNumber int `json:"discNumber"`
|
||||
}
|
||||
|
||||
type Albums []Album
|
||||
|
||||
// ToAlbumArtist creates an Artist object based on the attributes of this Albums collection.
|
||||
// It assumes all albums have the same AlbumArtist, or else results are unpredictable.
|
||||
func (als Albums) ToAlbumArtist() Artist {
|
||||
a := Artist{AlbumCount: len(als)}
|
||||
var mbzArtistIds []string
|
||||
for _, al := range als {
|
||||
a.ID = al.AlbumArtistID
|
||||
a.Name = al.AlbumArtist
|
||||
a.SortArtistName = al.SortAlbumArtistName
|
||||
a.OrderArtistName = al.OrderAlbumArtistName
|
||||
|
||||
a.SongCount += al.SongCount
|
||||
a.Size += al.Size
|
||||
a.Genres = append(a.Genres, al.Genres...)
|
||||
mbzArtistIds = append(mbzArtistIds, al.MbzAlbumArtistID)
|
||||
}
|
||||
)
|
||||
slices.SortFunc(a.Genres, func(a, b Genre) bool { return a.ID < b.ID })
|
||||
a.Genres = slices.Compact(a.Genres)
|
||||
a.MbzArtistID = slice.MostFrequent(mbzArtistIds)
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
type AlbumRepository interface {
|
||||
CountAll(...QueryOptions) (int64, error)
|
||||
|
|
88
model/album_test.go
Normal file
88
model/album_test.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package model_test
|
||||
|
||||
import (
|
||||
. "github.com/navidrome/navidrome/model"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Albums", func() {
|
||||
var albums Albums
|
||||
|
||||
Context("Simple attributes", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{
|
||||
{ID: "1", AlbumArtist: "Artist", AlbumArtistID: "11", SortAlbumArtistName: "SortAlbumArtistName", OrderAlbumArtistName: "OrderAlbumArtistName"},
|
||||
{ID: "2", AlbumArtist: "Artist", AlbumArtistID: "11", SortAlbumArtistName: "SortAlbumArtistName", OrderAlbumArtistName: "OrderAlbumArtistName"},
|
||||
}
|
||||
})
|
||||
|
||||
It("sets the single values correctly", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.ID).To(Equal("11"))
|
||||
Expect(artist.Name).To(Equal("Artist"))
|
||||
Expect(artist.SortArtistName).To(Equal("SortAlbumArtistName"))
|
||||
Expect(artist.OrderArtistName).To(Equal("OrderAlbumArtistName"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("Aggregated attributes", func() {
|
||||
When("we have multiple songs", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{
|
||||
{ID: "1", SongCount: 4, Size: 1024},
|
||||
{ID: "2", SongCount: 6, Size: 2048},
|
||||
}
|
||||
})
|
||||
It("calculates the aggregates correctly", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.AlbumCount).To(Equal(2))
|
||||
Expect(artist.SongCount).To(Equal(10))
|
||||
Expect(artist.Size).To(Equal(int64(3072)))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Context("Calculated attributes", func() {
|
||||
Context("Genres", func() {
|
||||
When("we have only one Genre", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{{Genres: Genres{{ID: "g1", Name: "Rock"}}}}
|
||||
})
|
||||
It("sets the correct Genre", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.Genres).To(ConsistOf(Genre{ID: "g1", Name: "Rock"}))
|
||||
})
|
||||
})
|
||||
When("we have multiple Genres", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{{Genres: Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g3", Name: "Alternative"}, {ID: "g2", Name: "Punk"}}}}
|
||||
})
|
||||
It("sets the correct Genres", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.Genres).To(Equal(Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g3", Name: "Alternative"}}))
|
||||
})
|
||||
})
|
||||
})
|
||||
Context("MbzArtistID", func() {
|
||||
When("we have only one MbzArtistID", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{{MbzAlbumArtistID: "id1"}}
|
||||
})
|
||||
It("sets the correct MbzArtistID", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.MbzArtistID).To(Equal("id1"))
|
||||
})
|
||||
})
|
||||
When("we have multiple MbzArtistID", func() {
|
||||
BeforeEach(func() {
|
||||
albums = Albums{{MbzAlbumArtistID: "id1"}, {MbzAlbumArtistID: "id2"}, {MbzAlbumArtistID: "id1"}}
|
||||
})
|
||||
It("sets the correct MbzArtistID", func() {
|
||||
artist := albums.ToAlbumArtist()
|
||||
Expect(artist.MbzArtistID).To(Equal("id1"))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
|
@ -49,7 +49,6 @@ type ArtistRepository interface {
|
|||
Get(id string) (*Artist, error)
|
||||
GetAll(options ...QueryOptions) (Artists, error)
|
||||
Search(q string, offset int, size int) (Artists, error)
|
||||
Refresh(ids ...string) error
|
||||
GetIndex() (ArtistIndexes, error)
|
||||
AnnotatedRepository
|
||||
}
|
||||
|
|
|
@ -83,6 +83,7 @@ func (mf MediaFile) AlbumCoverArtID() ArtworkID {
|
|||
|
||||
type MediaFiles []MediaFile
|
||||
|
||||
// Dirs returns a deduped list of all directories from the MediaFiles' paths
|
||||
func (mfs MediaFiles) Dirs() []string {
|
||||
var dirs []string
|
||||
for _, mf := range mfs {
|
||||
|
@ -93,6 +94,8 @@ func (mfs MediaFiles) Dirs() []string {
|
|||
return slices.Compact(dirs)
|
||||
}
|
||||
|
||||
// ToAlbum creates an Album object based on the attributes of this MediaFiles collection.
|
||||
// It assumes all mediafiles have the same Album, or else results are unpredictable.
|
||||
func (mfs MediaFiles) ToAlbum() Album {
|
||||
a := Album{SongCount: len(mfs)}
|
||||
var fullText []string
|
||||
|
|
|
@ -117,12 +117,12 @@ var _ = Describe("MediaFiles", func() {
|
|||
})
|
||||
When("we have multiple Genres", func() {
|
||||
BeforeEach(func() {
|
||||
mfs = MediaFiles{{Genres: Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g2", Name: "Alternative"}}}}
|
||||
mfs = MediaFiles{{Genres: Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g3", Name: "Alternative"}}}}
|
||||
})
|
||||
It("sets the correct Genre", func() {
|
||||
album := mfs.ToAlbum()
|
||||
Expect(album.Genre).To(Equal("Rock"))
|
||||
Expect(album.Genres).To(Equal(Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g2", Name: "Alternative"}}))
|
||||
Expect(album.Genres).To(Equal(Genres{{ID: "g1", Name: "Rock"}, {ID: "g2", Name: "Punk"}, {ID: "g3", Name: "Alternative"}}))
|
||||
})
|
||||
})
|
||||
When("we have one predominant Genre", func() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue