More conversion to mbtypes.MBID

This commit is contained in:
Philipp Wolfer 2025-04-03 15:19:26 +02:00
parent 13eb8342ab
commit 0d9bc74bc0
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
8 changed files with 82 additions and 81 deletions

View file

@ -189,16 +189,15 @@ func (f FavoriteTrack) AsLove() models.Love {
}
func (t Track) AsTrack() models.Track {
recordingMBID := mbtypes.MBID(t.RecordingMBID)
track := models.Track{
TrackName: t.Title,
ReleaseName: t.Album.Title,
ArtistNames: []string{t.Artist.Name},
TrackNumber: t.Position,
DiscNumber: t.DiscNumber,
RecordingMBID: recordingMBID,
ReleaseMBID: mbtypes.MBID(t.Album.ReleaseMBID),
ArtistMBIDs: []mbtypes.MBID{mbtypes.MBID(t.Artist.ArtistMBID)},
RecordingMBID: t.RecordingMBID,
ReleaseMBID: t.Album.ReleaseMBID,
ArtistMBIDs: []mbtypes.MBID{t.Artist.ArtistMBID},
Tags: t.Tags,
AdditionalInfo: map[string]any{
"media_player": FunkwhaleClientName,

View file

@ -23,7 +23,6 @@ import (
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/mbtypes"
"go.uploadedlobster.com/scotty/internal/backends/funkwhale"
"go.uploadedlobster.com/scotty/internal/config"
)
@ -75,9 +74,9 @@ func TestFunkwhaleListeningAsListen(t *testing.T) {
assert.Equal(fwListen.Track.DiscNumber, listen.Track.DiscNumber)
assert.Equal(fwListen.Track.Tags, listen.Track.Tags)
// assert.Equal(backends.FunkwhaleClientName, listen.AdditionalInfo["disc_number"])
assert.Equal(mbtypes.MBID(fwListen.Track.RecordingMBID), listen.RecordingMBID)
assert.Equal(mbtypes.MBID(fwListen.Track.Album.ReleaseMBID), listen.ReleaseMBID)
assert.Equal(mbtypes.MBID(fwListen.Track.Artist.ArtistMBID), listen.ArtistMBIDs[0])
assert.Equal(fwListen.Track.RecordingMBID, listen.RecordingMBID)
assert.Equal(fwListen.Track.Album.ReleaseMBID, listen.ReleaseMBID)
assert.Equal(fwListen.Track.Artist.ArtistMBID, listen.ArtistMBIDs[0])
assert.Equal(funkwhale.FunkwhaleClientName, listen.AdditionalInfo["media_player"])
}
@ -119,10 +118,10 @@ func TestFunkwhaleFavoriteTrackAsLove(t *testing.T) {
assert.Equal(favorite.Track.Position, love.Track.TrackNumber)
assert.Equal(favorite.Track.DiscNumber, love.Track.DiscNumber)
assert.Equal(favorite.Track.Tags, love.Track.Tags)
assert.Equal(mbtypes.MBID(favorite.Track.RecordingMBID), love.RecordingMBID)
assert.Equal(mbtypes.MBID(favorite.Track.RecordingMBID), love.Track.RecordingMBID)
assert.Equal(mbtypes.MBID(favorite.Track.Album.ReleaseMBID), love.ReleaseMBID)
assert.Equal(favorite.Track.RecordingMBID, love.RecordingMBID)
assert.Equal(favorite.Track.RecordingMBID, love.Track.RecordingMBID)
assert.Equal(favorite.Track.Album.ReleaseMBID, love.ReleaseMBID)
require.Len(t, love.Track.ArtistMBIDs, 1)
assert.Equal(mbtypes.MBID(favorite.Track.Artist.ArtistMBID), love.ArtistMBIDs[0])
assert.Equal(favorite.Track.Artist.ArtistMBID, love.ArtistMBIDs[0])
assert.Equal(funkwhale.FunkwhaleClientName, love.AdditionalInfo["media_player"])
}

View file

@ -21,6 +21,8 @@ THE SOFTWARE.
*/
package funkwhale
import "go.uploadedlobster.com/mbtypes"
type ListeningsResult struct {
Count int `json:"count"`
Previous string `json:"previous"`
@ -50,30 +52,30 @@ type FavoriteTrack struct {
}
type Track struct {
Id int `json:"int"`
Artist Artist `json:"artist"`
Album Album `json:"album"`
Title string `json:"title"`
Position int `json:"position"`
DiscNumber int `json:"disc_number"`
RecordingMBID string `json:"mbid"`
Tags []string `json:"tags"`
Uploads []Upload `json:"uploads"`
Id int `json:"int"`
Artist Artist `json:"artist"`
Album Album `json:"album"`
Title string `json:"title"`
Position int `json:"position"`
DiscNumber int `json:"disc_number"`
RecordingMBID mbtypes.MBID `json:"mbid"`
Tags []string `json:"tags"`
Uploads []Upload `json:"uploads"`
}
type Artist struct {
Id int `json:"int"`
Name string `json:"name"`
ArtistMBID string `json:"mbid"`
Id int `json:"int"`
Name string `json:"name"`
ArtistMBID mbtypes.MBID `json:"mbid"`
}
type Album struct {
Id int `json:"int"`
Title string `json:"title"`
AlbumArtist Artist `json:"artist"`
ReleaseDate string `json:"release_date"`
TrackCount int `json:"track_count"`
ReleaseMBID string `json:"mbid"`
Id int `json:"int"`
Title string `json:"title"`
AlbumArtist Artist `json:"artist"`
ReleaseDate string `json:"release_date"`
TrackCount int `json:"track_count"`
ReleaseMBID mbtypes.MBID `json:"mbid"`
}
type User struct {

View file

@ -29,6 +29,7 @@ import (
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/mbtypes"
"go.uploadedlobster.com/scotty/internal/backends/listenbrainz"
)
@ -114,7 +115,7 @@ func TestGetFeedback(t *testing.T) {
assert.Equal(302, result.TotalCount)
assert.Equal(3, result.Offset)
require.Len(t, result.Feedback, 2)
assert.Equal("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12", result.Feedback[0].RecordingMBID)
assert.Equal(mbtypes.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"), result.Feedback[0].RecordingMBID)
}
func TestSendFeedback(t *testing.T) {
@ -154,7 +155,7 @@ func TestLookup(t *testing.T) {
assert := assert.New(t)
assert.Equal("Say Just Words", result.RecordingName)
assert.Equal("Paradise Lost", result.ArtistCreditName)
assert.Equal("569436a1-234a-44bc-a370-8f4d252bef21", result.RecordingMBID)
assert.Equal(mbtypes.MBID("569436a1-234a-44bc-a370-8f4d252bef21"), result.RecordingMBID)
}
func setupHttpMock(t *testing.T, client *http.Client, url string, testDataPath string) {

View file

@ -33,7 +33,7 @@ type ListenBrainzApiBackend struct {
client Client
username string
checkDuplicates bool
existingMBIDs map[string]bool
existingMBIDs map[mbtypes.MBID]bool
}
func (b *ListenBrainzApiBackend) Name() string { return "listenbrainz" }
@ -239,14 +239,14 @@ func (b *ListenBrainzApiBackend) ImportLoves(export models.LovesResult, importRe
}
// TODO: Store MBIDs directly
b.existingMBIDs = make(map[string]bool, len(existingLoves.Items))
b.existingMBIDs = make(map[mbtypes.MBID]bool, len(existingLoves.Items))
for _, love := range existingLoves.Items {
b.existingMBIDs[string(love.RecordingMBID)] = true
b.existingMBIDs[love.RecordingMBID] = true
}
}
for _, love := range export.Items {
recordingMBID := string(love.RecordingMBID)
recordingMBID := love.RecordingMBID
if recordingMBID == "" {
lookup, err := b.client.Lookup(love.TrackName, love.ArtistName())
@ -324,7 +324,7 @@ func (lbListen Listen) AsListen() models.Listen {
}
func (f Feedback) AsLove() models.Love {
recordingMBID := mbtypes.MBID(f.RecordingMBID)
recordingMBID := f.RecordingMBID
track := f.TrackMetadata
if track == nil {
track = &Track{}
@ -351,16 +351,16 @@ func (t Track) AsTrack() models.Track {
Duration: t.Duration(),
TrackNumber: t.TrackNumber(),
DiscNumber: t.DiscNumber(),
RecordingMBID: mbtypes.MBID(t.RecordingMBID()),
ReleaseMBID: mbtypes.MBID(t.ReleaseMBID()),
ReleaseGroupMBID: mbtypes.MBID(t.ReleaseGroupMBID()),
RecordingMBID: t.RecordingMBID(),
ReleaseMBID: t.ReleaseMBID(),
ReleaseGroupMBID: t.ReleaseGroupMBID(),
ISRC: t.ISRC(),
AdditionalInfo: t.AdditionalInfo,
}
if t.MBIDMapping != nil && len(track.ArtistMBIDs) == 0 {
for _, artistMBID := range t.MBIDMapping.ArtistMBIDs {
track.ArtistMBIDs = append(track.ArtistMBIDs, mbtypes.MBID(artistMBID))
track.ArtistMBIDs = append(track.ArtistMBIDs, artistMBID)
}
}

View file

@ -73,9 +73,9 @@ func TestListenBrainzListenAsListen(t *testing.T) {
}
func TestListenBrainzFeedbackAsLove(t *testing.T) {
recordingMBID := "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"
releaseMBID := "d7f22677-9803-4d21-ba42-081b633a6f68"
artistMBID := "d7f22677-9803-4d21-ba42-081b633a6f68"
recordingMBID := mbtypes.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12")
releaseMBID := mbtypes.MBID("d7f22677-9803-4d21-ba42-081b633a6f68")
artistMBID := mbtypes.MBID("d7f22677-9803-4d21-ba42-081b633a6f68")
feedback := listenbrainz.Feedback{
Created: 1699859066,
RecordingMBID: recordingMBID,
@ -88,7 +88,7 @@ func TestListenBrainzFeedbackAsLove(t *testing.T) {
MBIDMapping: &listenbrainz.MBIDMapping{
RecordingMBID: recordingMBID,
ReleaseMBID: releaseMBID,
ArtistMBIDs: []string{artistMBID},
ArtistMBIDs: []mbtypes.MBID{artistMBID},
},
},
}
@ -99,15 +99,15 @@ func TestListenBrainzFeedbackAsLove(t *testing.T) {
assert.Equal(feedback.TrackMetadata.TrackName, love.TrackName)
assert.Equal(feedback.TrackMetadata.ReleaseName, love.ReleaseName)
assert.Equal([]string{feedback.TrackMetadata.ArtistName}, love.ArtistNames)
assert.Equal(mbtypes.MBID(recordingMBID), love.RecordingMBID)
assert.Equal(mbtypes.MBID(recordingMBID), love.Track.RecordingMBID)
assert.Equal(mbtypes.MBID(releaseMBID), love.Track.ReleaseMBID)
assert.Equal(recordingMBID, love.RecordingMBID)
assert.Equal(recordingMBID, love.Track.RecordingMBID)
assert.Equal(releaseMBID, love.Track.ReleaseMBID)
require.Len(t, love.Track.ArtistMBIDs, 1)
assert.Equal(mbtypes.MBID(artistMBID), love.Track.ArtistMBIDs[0])
assert.Equal(artistMBID, love.Track.ArtistMBIDs[0])
}
func TestListenBrainzPartialFeedbackAsLove(t *testing.T) {
recordingMBID := "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"
recordingMBID := mbtypes.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12")
feedback := listenbrainz.Feedback{
Created: 1699859066,
RecordingMBID: recordingMBID,
@ -116,7 +116,7 @@ func TestListenBrainzPartialFeedbackAsLove(t *testing.T) {
love := feedback.AsLove()
assert := assert.New(t)
assert.Equal(time.Unix(1699859066, 0).Unix(), love.Created.Unix())
assert.Equal(mbtypes.MBID(recordingMBID), love.RecordingMBID)
assert.Equal(mbtypes.MBID(recordingMBID), love.Track.RecordingMBID)
assert.Equal(recordingMBID, love.RecordingMBID)
assert.Equal(recordingMBID, love.Track.RecordingMBID)
assert.Empty(love.Track.TrackName)
}

View file

@ -71,11 +71,11 @@ type Track struct {
}
type MBIDMapping struct {
RecordingName string `json:"recording_name,omitempty"`
RecordingMBID string `json:"recording_mbid,omitempty"`
ReleaseMBID string `json:"release_mbid,omitempty"`
ArtistMBIDs []string `json:"artist_mbids,omitempty"`
Artists []Artist `json:"artists,omitempty"`
RecordingName string `json:"recording_name,omitempty"`
RecordingMBID mbtypes.MBID `json:"recording_mbid,omitempty"`
ReleaseMBID mbtypes.MBID `json:"release_mbid,omitempty"`
ArtistMBIDs []mbtypes.MBID `json:"artist_mbids,omitempty"`
Artists []Artist `json:"artists,omitempty"`
}
type Artist struct {
@ -92,21 +92,21 @@ type GetFeedbackResult struct {
}
type Feedback struct {
Created int64 `json:"created,omitempty"`
RecordingMBID string `json:"recording_mbid,omitempty"`
RecordingMsid string `json:"recording_msid,omitempty"`
Score int `json:"score,omitempty"`
TrackMetadata *Track `json:"track_metadata,omitempty"`
UserName string `json:"user_id,omitempty"`
Created int64 `json:"created,omitempty"`
RecordingMBID mbtypes.MBID `json:"recording_mbid,omitempty"`
RecordingMsid mbtypes.MBID `json:"recording_msid,omitempty"`
Score int `json:"score,omitempty"`
TrackMetadata *Track `json:"track_metadata,omitempty"`
UserName string `json:"user_id,omitempty"`
}
type LookupResult struct {
ArtistCreditName string `json:"artist_credit_name"`
ReleaseName string `json:"release_name"`
RecordingName string `json:"recording_name"`
RecordingMBID string `json:"recording_mbid"`
ReleaseMBID string `json:"release_mbid"`
ArtistMBIDs []string `json:"artist_mbids"`
ArtistCreditName string `json:"artist_credit_name"`
ReleaseName string `json:"release_name"`
RecordingName string `json:"recording_name"`
RecordingMBID mbtypes.MBID `json:"recording_mbid"`
ReleaseMBID mbtypes.MBID `json:"release_mbid"`
ArtistMBIDs []mbtypes.MBID `json:"artist_mbids"`
}
type StatusResult struct {
@ -163,8 +163,8 @@ func (t Track) ISRC() mbtypes.ISRC {
return mbtypes.ISRC(tryGetValueOrEmpty[string](t.AdditionalInfo, "isrc"))
}
func (t Track) RecordingMBID() string {
mbid := tryGetValueOrEmpty[string](t.AdditionalInfo, "recording_mbid")
func (t Track) RecordingMBID() mbtypes.MBID {
mbid := mbtypes.MBID(tryGetValueOrEmpty[string](t.AdditionalInfo, "recording_mbid"))
if mbid == "" && t.MBIDMapping != nil {
return t.MBIDMapping.RecordingMBID
} else {
@ -172,8 +172,8 @@ func (t Track) RecordingMBID() string {
}
}
func (t Track) ReleaseMBID() string {
mbid := tryGetValueOrEmpty[string](t.AdditionalInfo, "release_mbid")
func (t Track) ReleaseMBID() mbtypes.MBID {
mbid := mbtypes.MBID(tryGetValueOrEmpty[string](t.AdditionalInfo, "release_mbid"))
if mbid == "" && t.MBIDMapping != nil {
return t.MBIDMapping.ReleaseMBID
} else {
@ -181,8 +181,8 @@ func (t Track) ReleaseMBID() string {
}
}
func (t Track) ReleaseGroupMBID() string {
return tryGetValueOrEmpty[string](t.AdditionalInfo, "release_group_mbid")
func (t Track) ReleaseGroupMBID() mbtypes.MBID {
return mbtypes.MBID(tryGetValueOrEmpty[string](t.AdditionalInfo, "release_group_mbid"))
}
func tryGetValueOrEmpty[T any](dict map[string]any, key string) T {

View file

@ -142,30 +142,30 @@ func TestTrackIsrc(t *testing.T) {
}
func TestTrackRecordingMBID(t *testing.T) {
expected := "e02cc1c3-93fd-4e24-8b77-325060de920b"
expected := mbtypes.MBID("e02cc1c3-93fd-4e24-8b77-325060de920b")
track := listenbrainz.Track{
AdditionalInfo: map[string]any{
"recording_mbid": expected,
"recording_mbid": string(expected),
},
}
assert.Equal(t, expected, track.RecordingMBID())
}
func TestTrackReleaseMBID(t *testing.T) {
expected := "e02cc1c3-93fd-4e24-8b77-325060de920b"
expected := mbtypes.MBID("e02cc1c3-93fd-4e24-8b77-325060de920b")
track := listenbrainz.Track{
AdditionalInfo: map[string]any{
"release_mbid": expected,
"release_mbid": string(expected),
},
}
assert.Equal(t, expected, track.ReleaseMBID())
}
func TestReleaseGroupMBID(t *testing.T) {
expected := "e02cc1c3-93fd-4e24-8b77-325060de920b"
expected := mbtypes.MBID("e02cc1c3-93fd-4e24-8b77-325060de920b")
track := listenbrainz.Track{
AdditionalInfo: map[string]any{
"release_group_mbid": expected,
"release_group_mbid": string(expected),
},
}
assert.Equal(t, expected, track.ReleaseGroupMBID())