From c662bc1f905102ff93335fa50fa781d0bcaddbac Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sat, 28 Nov 2026 13:51:25 -0500 Subject: [PATCH] fix(subsonic): getLyrics by title alone answers nothing when two artists share the title (#8128) An app that asks for lyrics by title without an artist got the first matching song with lyrics, which could be another artist's song of the same name. It now answers empty, so the app can look elsewhere. Co-Authored-By: Claude Opus 5.5 --- internal/subsonic/info_test.go | 28 ++++++++++++++++++++++++++++ internal/subsonic/lyrics.go | 21 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/internal/subsonic/info_test.go b/internal/subsonic/info_test.go index 8d2873e5..7a24f6ae 100644 --- a/internal/subsonic/info_test.go +++ b/internal/subsonic/info_test.go @@ -139,6 +139,34 @@ func TestGetLyrics_ReadsTheSongsLrcByIdOrByName(t *testing.T) { if byName.Lyrics != (Lyrics{}) { t.Errorf("another artist's Dusk = %+v, want no lyrics", byName.Lyrics) } + + // Without an artist, a title only one artist has is that artist's song; + // a title two artists have is neither's, even when only one has lyrics. + if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 { + t.Fatalf("getLyrics: error %d", code) + } + if byName.Lyrics.Value != "First light\nGone by noon" { + t.Errorf("Dusk by title alone = %+v, want Tin Orchard's text", byName.Lyrics) + } + ctx := context.Background() + q := dbq.New(f.pool) + other, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Quiet Harbour", SortName: "Quiet Harbour"}) + if err != nil { + t.Fatal(err) + } + if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ + Title: "Dusk", AlbumID: f.albums["Paper Boats"].ID, ArtistID: other.ID, + FilePath: filepath.Join(dir, "Quiet Harbour - Dusk.flac"), DurationMs: 200_000, + }); err != nil { + t.Fatal(err) + } + byName.Lyrics = Lyrics{} + if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 { + t.Fatalf("getLyrics: error %d", code) + } + if byName.Lyrics != (Lyrics{}) { + t.Errorf("Dusk by title alone, two artists' = %+v, want no lyrics", byName.Lyrics) + } } // Artist info is the MusicBrainz id and the similar artists in the library, diff --git a/internal/subsonic/lyrics.go b/internal/subsonic/lyrics.go index 5c0392c1..7ff6fc71 100644 --- a/internal/subsonic/lyrics.go +++ b/internal/subsonic/lyrics.go @@ -189,10 +189,13 @@ func lrcMillis(min, sec, frac string) int64 { // getLyrics finds a song by artist and title, regardless of case, and // returns its lyrics as text. A song not found, or found without lyrics, is -// an empty lyrics element, as the spec has it. +// an empty lyrics element, as the spec has it. Without an artist, a title +// that more than one artist has a song by is also empty: either answer could +// be another song's words. func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() title := strings.TrimSpace(params.Get("title")) + artistName := strings.TrimSpace(params.Get("artist")) empty := LyricsResponse{Envelope: NewEnvelope("ok")} if title == "" { Write(w, r, empty) @@ -200,12 +203,16 @@ func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) { } q := dbq.New(b.pool) rows, err := q.ListTracksByArtistAndTitle(r.Context(), dbq.ListTracksByArtistAndTitleParams{ - Title: title, ArtistName: strings.TrimSpace(params.Get("artist")), + Title: title, ArtistName: artistName, }) if err != nil { WriteFail(w, r, ErrGeneric, "Failed to look up the song") return } + if artistName == "" && byMoreThanOneArtist(rows) { + Write(w, r, empty) + return + } for _, row := range rows { l, ok := readLyrics(row.Track.FilePath) if !ok { @@ -224,6 +231,16 @@ func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) { Write(w, r, empty) } +// byMoreThanOneArtist reports whether rows are songs by different artists. +func byMoreThanOneArtist(rows []dbq.ListTracksByArtistAndTitleRow) bool { + for _, row := range rows { + if row.Track.ArtistID != rows[0].Track.ArtistID { + return true + } + } + return false +} + // getLyricsBySongID serves getLyricsBySongId, OpenSubsonic's songLyrics // extension: a song's lyrics by id, timed when its .lrc file or tag is // timed. A song without lyrics has an empty list.