Use userId in player, other fixes (#3182)
* [bugfix] player: use userId, other fixes This PR primarily resolves #1928 by switching the foreign key of `player` from `user.user_name` to `user.id`. There are also a few other fixes/changes: - For some bizarre reason, `ip_address` is never returned from `read`/`get`. Change the field to `ip`, which works. Somehow - Update `players_test.go` mock to also check for user agent, replicating the actual code - Update `player_repository.go` `isPermitted` to check user id. I don't know how this worked before... - tests! - a few places referred to `typ`, when it is really `userAgent`. Change the field names * baseRequest -> selectPlayer * remove comment * update migration, make all of persistence foreign key enabled * maybe don't forget to save the file first
This commit is contained in:
+5
-5
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
type Players interface {
|
||||
Get(ctx context.Context, playerId string) (*model.Player, error)
|
||||
Register(ctx context.Context, id, client, typ, ip string) (*model.Player, *model.Transcoding, error)
|
||||
Register(ctx context.Context, id, client, userAgent, ip string) (*model.Player, *model.Transcoding, error)
|
||||
}
|
||||
|
||||
func NewPlayers(ds model.DataStore) Players {
|
||||
@@ -28,7 +28,7 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
|
||||
var plr *model.Player
|
||||
var trc *model.Transcoding
|
||||
var err error
|
||||
userName, _ := request.UsernameFrom(ctx)
|
||||
user, _ := request.UserFrom(ctx)
|
||||
if id != "" {
|
||||
plr, err = p.ds.Player(ctx).Get(id)
|
||||
if err == nil && plr.Client != client {
|
||||
@@ -36,13 +36,13 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
|
||||
}
|
||||
}
|
||||
if err != nil || id == "" {
|
||||
plr, err = p.ds.Player(ctx).FindMatch(userName, client, userAgent)
|
||||
plr, err = p.ds.Player(ctx).FindMatch(user.ID, client, userAgent)
|
||||
if err == nil {
|
||||
log.Debug(ctx, "Found matching player", "id", plr.ID, "client", client, "username", userName, "type", userAgent)
|
||||
} else {
|
||||
plr = &model.Player{
|
||||
ID: uuid.NewString(),
|
||||
UserName: userName,
|
||||
UserId: user.ID,
|
||||
Client: client,
|
||||
ScrobbleEnabled: true,
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func (p *players) Register(ctx context.Context, id, client, userAgent, ip string
|
||||
}
|
||||
plr.Name = fmt.Sprintf("%s [%s]", client, userAgent)
|
||||
plr.UserAgent = userAgent
|
||||
plr.IPAddress = ip
|
||||
plr.IP = ip
|
||||
plr.LastSeen = time.Now()
|
||||
err = p.ds.Player(ctx).Put(plr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user