fix: add ApplyArtistLibraryFilter to filter artists by associated music folders

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-06-27 12:45:41 -04:00
commit 3f44e86299
2 changed files with 28 additions and 1 deletions

View file

@ -129,7 +129,7 @@ func (api *Router) getStarredItems(r *http.Request) (model.Artists, model.Albums
err := run.Parallel(
// Query starred artists
func() error {
artistOpts := filter.ApplyLibraryFilter(filter.ArtistsByStarred(), musicFolderIds)
artistOpts := filter.ApplyArtistLibraryFilter(filter.ArtistsByStarred(), musicFolderIds)
var err error
artists, err = api.ds.Artist(ctx).GetAll(artistOpts)
if err != nil {

View file

@ -138,6 +138,33 @@ func ApplyLibraryFilter(opts Options, musicFolderIds []int) Options {
return opts
}
// ApplyArtistLibraryFilter applies a filter to the given Options to ensure that only artists
// that are associated with the specified music folders are included in the results.
//
// This creates: EXISTS (SELECT 1 FROM library_artist la WHERE la.artist_id = artist.id AND la.library_id IN (...))
// Unfortunately for artists we don't have a direct `library_id` field in the artist table.
// We then need to use a subquery to filter through the library_artist junction table.
// I know, this should be the repository's responsibility, hopefully we can refactor this later.
func ApplyArtistLibraryFilter(opts Options, musicFolderIds []int) Options {
if len(musicFolderIds) == 0 {
return opts
}
subquery := Select("1").From("library_artist la").Where(And{
Expr("la.artist_id = artist.id"),
Eq{"la.library_id": musicFolderIds},
})
artistLibraryFilter := Expr("EXISTS (?)", subquery)
if opts.Filters == nil {
opts.Filters = artistLibraryFilter
} else {
opts.Filters = And{opts.Filters, artistLibraryFilter}
}
return opts
}
func ByGenre(genre string) Options {
return addDefaultFilters(Options{
Sort: "name",