From 3c87ab4f0aee697a264b4e04e4b7313b522cfa25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 4 Nov 2022 16:56:44 +0100 Subject: [PATCH] createUser: Retrieve name from Steam For that reason name was removed from UserInput. --- graph/generated/generated.go | 11 +------- graph/model/models_gen.go | 1 - graph/schema.graphqls | 1 - graph/schema.resolvers.go | 10 ++++++-- steam/steam.go | 49 ++++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 steam/steam.go diff --git a/graph/generated/generated.go b/graph/generated/generated.go index 3f96fe0..e04f4bc 100644 --- a/graph/generated/generated.go +++ b/graph/generated/generated.go @@ -445,7 +445,6 @@ input NewUser { steamID: String! teamspeakID: String! avatar: String - name: String } input UserInput { @@ -3918,7 +3917,7 @@ func (ec *executionContext) unmarshalInputNewUser(ctx context.Context, obj inter asMap[k] = v } - fieldsInOrder := [...]string{"steamID", "teamspeakID", "avatar", "name"} + fieldsInOrder := [...]string{"steamID", "teamspeakID", "avatar"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -3949,14 +3948,6 @@ func (ec *executionContext) unmarshalInputNewUser(ctx context.Context, obj inter if err != nil { return it, err } - case "name": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) - it.Name, err = ec.unmarshalOString2áš–string(ctx, v) - if err != nil { - return it, err - } } } diff --git a/graph/model/models_gen.go b/graph/model/models_gen.go index 6d65b92..bfecbd6 100644 --- a/graph/model/models_gen.go +++ b/graph/model/models_gen.go @@ -11,7 +11,6 @@ type NewUser struct { SteamID string `json:"steamID"` TeamspeakID string `json:"teamspeakID"` Avatar *string `json:"avatar"` - Name *string `json:"name"` } type Teams struct { diff --git a/graph/schema.graphqls b/graph/schema.graphqls index 9cba1ce..9f3a60a 100644 --- a/graph/schema.graphqls +++ b/graph/schema.graphqls @@ -31,7 +31,6 @@ input NewUser { steamID: String! teamspeakID: String! avatar: String - name: String } input UserInput { diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index f3c836d..307df77 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -19,6 +19,7 @@ import ( "git.xenrox.net/~xenrox/10man-api/graph/generated" "git.xenrox.net/~xenrox/10man-api/graph/model" "git.xenrox.net/~xenrox/10man-api/logic" + "git.xenrox.net/~xenrox/10man-api/steam" ) // CreateUser is the resolver for the createUser field. @@ -28,6 +29,11 @@ func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) } var elo Elo + name, err := steam.Name(input.SteamID) + if err != nil { + return "", fmt.Errorf("steam: %w", err) + } + url := "https://faceit.xenrox.net/api/elo/" + input.SteamID resp, err := http.Get(url) if err != nil { @@ -62,12 +68,12 @@ func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) INSERT INTO "User" (steam_id, teamspeak_id, elo, admin, avatar, name) VALUES ($1, $2, $3, $4, $5, $6)` _, err = r.DB.DB.Exec(query, input.SteamID, input.TeamspeakID, elo.Elo, - isAdmin, input.Avatar, input.Name) + isAdmin, input.Avatar, name) if err != nil { return "", database.CheckErrorCode(err) } - r.Logger.Infof("Created user %q (Elo: %d).", input.SteamID, elo.Elo) + r.Logger.Infof("Created user %q (SteamID %s, Elo %d).", name, input.SteamID, elo.Elo) return "Created", nil } diff --git a/steam/steam.go b/steam/steam.go new file mode 100644 index 0000000..cf83910 --- /dev/null +++ b/steam/steam.go @@ -0,0 +1,49 @@ +package steam + +import ( + "encoding/xml" + "fmt" + "io" + "net/http" + "time" +) + +type profile struct { + SteamID string `xml:"steamID"` +} + +func Name(steamID64 string) (string, error) { + url := fmt.Sprintf("https://steamcommunity.com/profiles/%s/?xml=1", steamID64) + + client := &http.Client{Timeout: time.Second * 3} + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return "", err + } + + resp, err := client.Do(req) + if err != nil { + return "", err + } + + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("received status code %d", resp.StatusCode) + } + + b, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var profile profile + if err := xml.Unmarshal(b, &profile); err != nil { + return "", err + } + + if profile.SteamID == "" { + return "", fmt.Errorf("no such profile found with ID64 %q", steamID64) + } + + return profile.SteamID, nil +} -- 2.44.0