Skip to content

Commit

Permalink
fix: store and scrobble with real album artist info string
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Oct 25, 2023
1 parent 4657527 commit fe0567a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
60 changes: 24 additions & 36 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -293,29 +292,30 @@ type Play struct {
}

type Album struct {
ID int `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
ModifiedAt time.Time
LeftPath string `gorm:"unique_index:idx_album_abs_path"`
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
RightPathUDec string `sql:"default: null"`
Parent *Album
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
Genres []*Genre `gorm:"many2many:album_genres"`
Cover string `sql:"default: null"`
Artists []*Artist `gorm:"many2many:album_artists"`
TagTitle string `sql:"default: null"`
TagTitleUDec string `sql:"default: null"`
TagBrainzID string `sql:"default: null"`
TagYear int `sql:"default: null"`
Tracks []*Track
ChildCount int `sql:"-"`
Duration int `sql:"-"`
AlbumStar *AlbumStar
AlbumRating *AlbumRating
AverageRating float64 `sql:"default: null"`
ID int `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
ModifiedAt time.Time
LeftPath string `gorm:"unique_index:idx_album_abs_path"`
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
RightPathUDec string `sql:"default: null"`
Parent *Album
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
Genres []*Genre `gorm:"many2many:album_genres"`
Cover string `sql:"default: null"`
Artists []*Artist `gorm:"many2many:album_artists"`
TagTitle string `sql:"default: null"`
TagAlbumArtist string // display purposes only
TagTitleUDec string `sql:"default: null"`
TagBrainzID string `sql:"default: null"`
TagYear int `sql:"default: null"`
Tracks []*Track
ChildCount int `sql:"-"`
Duration int `sql:"-"`
AlbumStar *AlbumStar
AlbumRating *AlbumRating
AverageRating float64 `sql:"default: null"`
}

func (a *Album) SID() *specid.ID {
Expand All @@ -333,18 +333,6 @@ func (a *Album) IndexRightPath() string {
return a.RightPath
}

func (a *Album) ArtistsStrings() []string {
artists := append([]*Artist(nil), a.Artists...)
sort.Slice(artists, func(i, j int) bool {
return artists[i].ID < artists[j].ID
})
strs := make([]string, 0, len(artists))
for _, artist := range artists {
strs = append(strs, artist.Name)
}
return strs
}

type PlayQueue struct {
ID int `gorm:"primary_key"`
CreatedAt time.Time
Expand Down
5 changes: 5 additions & 0 deletions db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (db *DB) Migrate(ctx MigrationContext) error {
construct(ctx, "202309070009", migrateDeleteArtistCoverField),
construct(ctx, "202309131743", migrateArtistInfo),
construct(ctx, "202309161411", migratePlaylistsPaths),
construct(ctx, "202310252205", migrateAlbumTagArtistString),
}

return gormigrate.
Expand Down Expand Up @@ -729,3 +730,7 @@ func backupDBPre016(tx *gorm.DB, ctx MigrationContext) error {
}
return Dump(context.Background(), tx, fmt.Sprintf("%s.%d.bak", ctx.DBPath, time.Now().Unix()))
}

func migrateAlbumTagArtistString(tx *gorm.DB, _ MigrationContext) error {
return tx.AutoMigrate(Album{}).Error
}
2 changes: 1 addition & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime tim
albumName := tagcommon.MustAlbum(trags)
album.TagTitle = albumName
album.TagTitleUDec = decoded(albumName)
album.TagAlbumArtist = tagcommon.MustAlbumArtist(trags)
album.TagBrainzID = trags.AlbumBrainzID()
album.TagYear = trags.Year()

Expand All @@ -381,7 +382,6 @@ func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime tim
if err := tx.Save(&album).Error; err != nil {
return fmt.Errorf("saving album: %w", err)
}

return nil
}

Expand Down
21 changes: 20 additions & 1 deletion server/ctrlsubsonic/handlers_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"net/http"
"path/filepath"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (c *Controller) ServeScrobble(r *http.Request) *spec.Response {
scrobbleTrack.Track = track.TagTitle
scrobbleTrack.Artist = track.TagTrackArtist
scrobbleTrack.Album = track.Album.TagTitle
scrobbleTrack.AlbumArtist = strings.Join(track.Album.ArtistsStrings(), ", ")
scrobbleTrack.AlbumArtist = albumArtistString(track.Album)
scrobbleTrack.TrackNumber = uint(track.TagTrackNumber)
scrobbleTrack.Duration = time.Second * time.Duration(track.Length)
if _, err := uuid.Parse(track.TagBrainzID); err == nil {
Expand Down Expand Up @@ -513,3 +514,21 @@ func lowerUDecOrHash(in string) string {
}
return string(lower)
}

// not everyone's records may have the album.TagAlbumArtist column set, since we didn't always store that.
// try it first, but fallback to a list from the artists table
func albumArtistString(album *db.Album) string {
if album.TagAlbumArtist != "" {
return album.TagAlbumArtist
}

artists := append([]*db.Artist(nil), album.Artists...)
sort.Slice(artists, func(i, j int) bool {
return artists[i].ID < artists[j].ID
})
names := make([]string, 0, len(artists))
for _, artist := range artists {
names = append(names, artist.Name)
}
return strings.Join(names, ", ")
}

0 comments on commit fe0567a

Please sign in to comment.