fix: address code review findings

This commit is contained in:
2026-07-19 19:45:08 +03:00
parent beef81d598
commit a7803615cd
7 changed files with 185 additions and 94 deletions

View File

@@ -8,19 +8,12 @@ import (
"naviwatcher/internal/database"
)
// excludedStatuses contains release-group statuses that should be filtered out.
var excludedStatuses = map[string]bool{
"Bootleg": true,
"Promotion": true,
"Pseudo-Release": true,
}
// includedTypes contains release-group types that should be included.
// includedTypes contains release-group primary types that should be included
// when no more specific type classification applies.
var includedTypes = map[string]bool{
"Album": true,
"Single": true,
"EP": true,
"Compilation": true,
"Album": true,
"Single": true,
"EP": true,
}
// GetArtistReleaseGroups fetches all release groups for a given artist from MusicBrainz.
@@ -77,23 +70,23 @@ type FilterOptions struct {
IgnoreCompilations bool
}
// FilterReleaseGroups applies status and type filtering to a list of release groups.
// It excludes Bootleg, Promotion, and Pseudo-Release statuses.
// It includes only Album, Single, EP, and Compilation types, unless the type
// is disabled via FilterOptions.
// FilterReleaseGroups applies type filtering to a list of release groups.
// It includes only Album/Single/EP primary types, or release groups whose
// secondary type list contains Single/EP/Compilation (e.g. an "Album" that is
// also a "Compilation"). The IgnoreSingles / IgnoreCompilations toggles drop
// release groups classified as such via either primary or secondary type.
//
// Release groups carry no status in ws/2, so there is no status filtering.
func FilterReleaseGroups(groups []ReleaseGroup, opts FilterOptions) []ReleaseGroup {
var filtered []ReleaseGroup
for _, rg := range groups {
if IsStatusExcluded(rg.Status) {
if !IsTypeIncluded(rg.Type) && !hasSecondaryType(rg, "Single", "EP", "Compilation") {
continue
}
if !IsTypeIncluded(rg.Type) {
if opts.IgnoreSingles && (rg.Type == "Single" || hasSecondaryType(rg, "Single")) {
continue
}
if opts.IgnoreSingles && rg.Type == "Single" {
continue
}
if opts.IgnoreCompilations && rg.Type == "Compilation" {
if opts.IgnoreCompilations && (rg.Type == "Compilation" || hasSecondaryType(rg, "Compilation")) {
continue
}
filtered = append(filtered, rg)
@@ -101,12 +94,21 @@ func FilterReleaseGroups(groups []ReleaseGroup, opts FilterOptions) []ReleaseGro
return filtered
}
// IsStatusExcluded returns true if the given status should be excluded.
func IsStatusExcluded(status string) bool {
return excludedStatuses[status]
// hasSecondaryType reports whether any of the release group's secondary types
// matches one of the provided values.
func hasSecondaryType(rg ReleaseGroup, wanted ...string) bool {
for _, s := range rg.SecondaryTypes {
for _, w := range wanted {
if s == w {
return true
}
}
}
return false
}
// IsTypeIncluded returns true if the given type is in the base included set.
// IsTypeIncluded returns true if the given primary type is in the base
// included set (Album/Single/EP).
func IsTypeIncluded(releaseType string) bool {
return includedTypes[releaseType]
}
@@ -118,6 +120,8 @@ func IsTypeIncluded(releaseType string) bool {
// MBID) must NOT be stored here, because artist_settings is keyed by the
// Navidrome ID and the foreign key / join would otherwise never match.
func (rg *ReleaseGroup) ToExternalRelease(artistID string) *database.ExternalRelease {
// Only the primary type is persisted; secondary types are used transiently
// for filtering above and are not stored in the external_releases schema.
return &database.ExternalRelease{
RGID: rg.ID,
ArtistID: artistID,