Fix GetNowPlaying endpoint showing only the last play

This commit is contained in:
Deluan
2021-06-20 10:36:50 -04:00
parent f8ee6db72a
commit 97434c1789
12 changed files with 110 additions and 31 deletions
+7 -7
View File
@@ -24,7 +24,7 @@ type players struct {
ds model.DataStore
}
func (p *players) Register(ctx context.Context, id, client, typ, ip string) (*model.Player, *model.Transcoding, error) {
func (p *players) Register(ctx context.Context, id, client, userAgent, ip string) (*model.Player, *model.Transcoding, error) {
var plr *model.Player
var trc *model.Transcoding
var err error
@@ -36,22 +36,22 @@ func (p *players) Register(ctx context.Context, id, client, typ, ip string) (*mo
}
}
if err != nil || id == "" {
plr, err = p.ds.Player(ctx).FindByName(client, userName)
plr, err = p.ds.Player(ctx).FindMatch(userName, client, userAgent)
if err == nil {
log.Debug("Found player by name", "id", plr.ID, "client", client, "username", userName)
log.Debug("Found matching player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
} else {
plr = &model.Player{
ID: uuid.NewString(),
Name: fmt.Sprintf("%s (%s)", client, userName),
UserName: userName,
Client: client,
}
log.Info("Registering new player", "id", plr.ID, "client", client, "username", userName)
log.Info("Registering new player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
}
}
plr.LastSeen = time.Now()
plr.Type = typ
plr.Name = fmt.Sprintf("%s [%s]", client, userAgent)
plr.UserAgent = userAgent
plr.IPAddress = ip
plr.LastSeen = time.Now()
err = p.ds.Player(ctx).Put(plr)
if err != nil {
return nil, nil, err
+2 -2
View File
@@ -35,7 +35,7 @@ var _ = Describe("Players", func() {
Expect(p.LastSeen).To(BeTemporally(">=", beforeRegister))
Expect(p.Client).To(Equal("client"))
Expect(p.UserName).To(Equal("johndoe"))
Expect(p.Type).To(Equal("chrome"))
Expect(p.UserAgent).To(Equal("chrome"))
Expect(repo.lastSaved).To(Equal(p))
Expect(trc).To(BeNil())
})
@@ -125,7 +125,7 @@ func (m *mockPlayerRepository) Get(id string) (*model.Player, error) {
return nil, model.ErrNotFound
}
func (m *mockPlayerRepository) FindByName(client, userName string) (*model.Player, error) {
func (m *mockPlayerRepository) FindMatch(userName, client, typ string) (*model.Player, error) {
for _, p := range m.data {
if p.Client == client && p.UserName == userName {
return &p, nil
+3 -3
View File
@@ -17,12 +17,12 @@ type NowPlayingInfo struct {
TrackID string
Start time.Time
Username string
PlayerId int
PlayerId string
PlayerName string
}
type Scrobbler interface {
NowPlaying(ctx context.Context, playerId int, playerName string, trackId string) error
NowPlaying(ctx context.Context, playerId string, playerName string, trackId string) error
GetNowPlaying(ctx context.Context) ([]NowPlayingInfo, error)
Submit(ctx context.Context, playerId int, trackId string, playTime time.Time) error
}
@@ -40,7 +40,7 @@ func New(ds model.DataStore) Scrobbler {
return instance.(*scrobbler)
}
func (s *scrobbler) NowPlaying(ctx context.Context, playerId int, playerName string, trackId string) error {
func (s *scrobbler) NowPlaying(ctx context.Context, playerId string, playerName string, trackId string) error {
username, _ := request.UsernameFrom(ctx)
info := NowPlayingInfo{
TrackID: trackId,