~xenrox/10man-api

3c87ab4f0aee697a264b4e04e4b7313b522cfa25 — Thorben Günther 1 year, 5 months ago ea0b15d
createUser: Retrieve name from Steam

For that reason name was removed from UserInput.
M graph/generated/generated.go => graph/generated/generated.go +1 -10
@@ 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
			}
		}
	}


M graph/model/models_gen.go => graph/model/models_gen.go +0 -1
@@ 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 {

M graph/schema.graphqls => graph/schema.graphqls +0 -1
@@ 31,7 31,6 @@ input NewUser {
    steamID: String!
    teamspeakID: String!
    avatar: String
    name: String
}

input UserInput {

M graph/schema.resolvers.go => graph/schema.resolvers.go +8 -2
@@ 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
}


A steam/steam.go => steam/steam.go +49 -0
@@ 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
}