2024-01-10 06:12:10 +05:30
|
|
|
package mojang
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
import "context"
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
type MojangUuidsStorage interface {
|
|
|
|
// The second argument must be returned as a incoming username in case,
|
|
|
|
// when cached result indicates that there is no Mojang user with provided username
|
2024-02-13 06:38:42 +05:30
|
|
|
GetUuidForMojangUsername(ctx context.Context, username string) (foundUuid string, foundUsername string, err error)
|
2024-01-10 06:12:10 +05:30
|
|
|
// An empty uuid value can be passed if the corresponding account has not been found
|
2024-02-13 06:38:42 +05:30
|
|
|
StoreMojangUuid(ctx context.Context, username string, uuid string) error
|
2024-01-10 06:12:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type UuidsProviderWithCache struct {
|
|
|
|
Provider UuidsProvider
|
|
|
|
Storage MojangUuidsStorage
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *UuidsProviderWithCache) GetUuid(ctx context.Context, username string) (*ProfileInfo, error) {
|
2024-02-13 06:38:42 +05:30
|
|
|
uuid, foundUsername, err := p.Storage.GetUuidForMojangUsername(ctx, username)
|
2024-01-10 06:12:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundUsername != "" {
|
|
|
|
if uuid != "" {
|
|
|
|
return &ProfileInfo{Id: uuid, Name: foundUsername}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
profile, err := p.Provider.GetUuid(ctx, username)
|
2024-01-10 06:12:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
freshUuid := ""
|
|
|
|
wellCasedUsername := username
|
|
|
|
if profile != nil {
|
|
|
|
freshUuid = profile.Id
|
|
|
|
wellCasedUsername = profile.Name
|
|
|
|
}
|
|
|
|
|
2024-02-13 06:38:42 +05:30
|
|
|
_ = p.Storage.StoreMojangUuid(ctx, wellCasedUsername, freshUuid)
|
2024-01-10 06:12:10 +05:30
|
|
|
|
|
|
|
return profile, nil
|
|
|
|
}
|