2024-01-30 13:35:04 +05:30
|
|
|
package profiles
|
|
|
|
|
|
|
|
import (
|
2024-02-07 06:06:18 +05:30
|
|
|
"context"
|
2024-01-30 13:35:04 +05:30
|
|
|
"errors"
|
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/db"
|
|
|
|
"ely.by/chrly/internal/mojang"
|
2024-01-30 13:35:04 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type ProfilesFinder interface {
|
2024-02-13 06:38:42 +05:30
|
|
|
FindProfileByUsername(ctx context.Context, username string) (*db.Profile, error)
|
2024-01-30 13:35:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type MojangProfilesProvider interface {
|
2024-02-07 06:06:18 +05:30
|
|
|
GetForUsername(ctx context.Context, username string) (*mojang.ProfileResponse, error)
|
2024-01-30 13:35:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Provider struct {
|
|
|
|
ProfilesFinder
|
|
|
|
MojangProfilesProvider
|
|
|
|
}
|
|
|
|
|
2024-02-07 06:06:18 +05:30
|
|
|
func (p *Provider) FindProfileByUsername(ctx context.Context, username string, allowProxy bool) (*db.Profile, error) {
|
2024-02-13 06:38:42 +05:30
|
|
|
profile, err := p.ProfilesFinder.FindProfileByUsername(ctx, username)
|
2024-01-30 13:35:04 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if profile != nil && (profile.SkinUrl != "" || profile.CapeUrl != "") {
|
|
|
|
return profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowProxy {
|
2024-02-07 06:06:18 +05:30
|
|
|
mojangProfile, err := p.MojangProfilesProvider.GetForUsername(ctx, username)
|
2024-01-30 13:35:04 +05:30
|
|
|
// If we at least know something about the user,
|
|
|
|
// then we can ignore an error and return profile without textures
|
|
|
|
if err != nil && profile != nil {
|
|
|
|
return profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil || mojangProfile == nil {
|
|
|
|
if errors.Is(err, mojang.InvalidUsername) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedTextures, err := mojangProfile.DecodeTextures()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
profile = &db.Profile{
|
|
|
|
Uuid: mojangProfile.Id,
|
|
|
|
Username: mojangProfile.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
// There might be no textures property
|
|
|
|
if decodedTextures != nil {
|
|
|
|
if decodedTextures.Textures.Skin != nil {
|
|
|
|
profile.SkinUrl = decodedTextures.Textures.Skin.Url
|
|
|
|
if decodedTextures.Textures.Skin.Metadata != nil {
|
|
|
|
profile.SkinModel = decodedTextures.Textures.Skin.Metadata.Model
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if decodedTextures.Textures.Cape != nil {
|
|
|
|
profile.CapeUrl = decodedTextures.Textures.Cape.Url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var texturesProp *mojang.Property
|
|
|
|
for _, prop := range mojangProfile.Props {
|
|
|
|
if prop.Name == "textures" {
|
|
|
|
texturesProp = prop
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if texturesProp != nil {
|
|
|
|
profile.MojangTextures = texturesProp.Value
|
|
|
|
profile.MojangSignature = texturesProp.Signature
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile, nil
|
|
|
|
}
|