Add option to disable external scrobbling per player

This commit is contained in:
Deluan 2021-06-23 17:50:15 -04:00
parent 5001518260
commit abe8015745
6 changed files with 47 additions and 16 deletions

View File

@ -41,9 +41,10 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
log.Debug("Found matching player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
} else {
plr = &model.Player{
ID: uuid.NewString(),
UserName: userName,
Client: client,
ID: uuid.NewString(),
UserName: userName,
Client: client,
ScrobbleEnabled: true,
}
log.Info("Registering new player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
}

View File

@ -64,7 +64,10 @@ func (p *playTracker) NowPlaying(ctx context.Context, playerId string, playerNam
PlayerName: playerName,
}
_ = p.playMap.Set(playerId, info)
p.dispatchNowPlaying(ctx, user.ID, trackId)
player, _ := request.PlayerFrom(ctx)
if player.ScrobbleEnabled {
p.dispatchNowPlaying(ctx, user.ID, trackId)
}
return nil
}
@ -109,6 +112,10 @@ func (p *playTracker) GetNowPlaying(ctx context.Context) ([]NowPlayingInfo, erro
func (p *playTracker) Submit(ctx context.Context, submissions []Submission) error {
username, _ := request.UsernameFrom(ctx)
player, _ := request.PlayerFrom(ctx)
if !player.ScrobbleEnabled {
log.Debug(ctx, "External scrobbling disabled for this player", "player", player.Name, "ip", player.IPAddress, "user", username)
}
event := &events.RefreshResource{}
success := 0
@ -125,7 +132,9 @@ func (p *playTracker) Submit(ctx context.Context, submissions []Submission) erro
success++
event.With("song", mf.ID).With("album", mf.AlbumID).With("artist", mf.AlbumArtistID)
log.Info("Scrobbled", "title", mf.Title, "artist", mf.Artist, "user", username)
_ = p.dispatchScrobble(ctx, mf, s.Timestamp)
if player.ScrobbleEnabled {
_ = p.dispatchScrobble(ctx, mf, s.Timestamp)
}
}
}

View File

@ -29,6 +29,7 @@ var _ = Describe("PlayTracker", func() {
conf.Server.DevEnableScrobble = true
ctx = context.Background()
ctx = request.WithUser(ctx, model.User{ID: "u-1"})
ctx = request.WithPlayer(ctx, model.Player{ScrobbleEnabled: true})
ds = &tests.MockDataStore{}
broker = GetPlayTracker(ds, events.GetBroker())
fake = &fakeScrobbler{Authorized: true}
@ -68,6 +69,14 @@ var _ = Describe("PlayTracker", func() {
err := broker.NowPlaying(ctx, "player-1", "player-one", "123")
Expect(err).ToNot(HaveOccurred())
Expect(fake.NowPlayingCalled).To(BeFalse())
})
It("does not send track to agent if player is not enabled to send scrobbles", func() {
ctx = request.WithPlayer(ctx, model.Player{ScrobbleEnabled: false})
err := broker.NowPlaying(ctx, "player-1", "player-one", "123")
Expect(err).ToNot(HaveOccurred())
Expect(fake.NowPlayingCalled).To(BeFalse())
})
@ -136,6 +145,15 @@ var _ = Describe("PlayTracker", func() {
Expect(fake.ScrobbleCalled).To(BeFalse())
})
It("does not send track to agent player is not enabled to send scrobbles", func() {
ctx = request.WithPlayer(ctx, model.Player{ScrobbleEnabled: false})
err := broker.Submit(ctx, []Submission{{TrackID: "123", Timestamp: time.Now()}})
Expect(err).ToNot(HaveOccurred())
Expect(fake.ScrobbleCalled).To(BeFalse())
})
It("increments play counts even if it cannot scrobble", func() {
fake.Error = errors.New("error")

View File

@ -5,16 +5,17 @@ import (
)
type Player struct {
ID string `json:"id" orm:"column(id)"`
Name string `json:"name"`
UserAgent string `json:"userAgent"`
UserName string `json:"userName"`
Client string `json:"client"`
IPAddress string `json:"ipAddress"`
LastSeen time.Time `json:"lastSeen"`
TranscodingId string `json:"transcodingId"`
MaxBitRate int `json:"maxBitRate"`
ReportRealPath bool `json:"reportRealPath"`
ID string `json:"id" orm:"column(id)"`
Name string `json:"name"`
UserAgent string `json:"userAgent"`
UserName string `json:"userName"`
Client string `json:"client"`
IPAddress string `json:"ipAddress"`
LastSeen time.Time `json:"lastSeen"`
TranscodingId string `json:"transcodingId"`
MaxBitRate int `json:"maxBitRate"`
ReportRealPath bool `json:"reportRealPath"`
ScrobbleEnabled bool `json:"scrobbleEnabled"`
}
type Players []Player

View File

@ -111,7 +111,8 @@
"client": "Client",
"userName": "Username",
"lastSeen": "Last Seen At",
"reportRealPath": "Report Real Path"
"reportRealPath": "Report Real Path",
"scrobbleEnabled": "Send Scrobbles to Last.fm?"
}
},
"transcoding": {

View File

@ -47,6 +47,7 @@ const PlayerEdit = (props) => (
]}
/>
<BooleanInput source="reportRealPath" fullWidth />
<BooleanInput source="scrobbleEnabled" fullWidth />
<TextField source="client" />
<TextField source="userName" />
</SimpleForm>