11
"xelbot.com/reprogl/services/oauth"
14
type ExternalUserDTO struct {
15
UserData *oauth.UserData `json:"userData"`
16
UserAgent string `json:"userAgent"`
17
IP string `json:"ipAddress"`
20
type CreatedUserDTO struct {
22
Username string `json:"username"`
23
Email string `json:"email"`
24
Role string `json:"role"`
25
DisplayName string `json:"displayName,omitempty"`
26
ImageHash string `json:"imageHash"`
29
type ProfileDTO struct {
31
Username string `json:"username"`
32
Email string `json:"email"`
33
DisplayName string `json:"displayName"`
34
Role string `json:"role"`
35
IsMale bool `json:"isMale"`
38
type CreateUserResponse struct {
39
Violations []FormError `json:"errors,omitempty"`
40
User *CreatedUserDTO `json:"user,omitempty"`
43
func SendUserData(userData ExternalUserDTO) (*CreateUserResponse, error) {
44
jsonBody, err := json.Marshal(userData)
49
request, err := http.NewRequest(http.MethodPost, apiURL()+"/api/users/external", bytes.NewReader(jsonBody))
54
request.Header.Set("Content-Type", "application/json")
56
response, err := send(request)
61
if !(response.StatusCode == http.StatusOK ||
62
response.StatusCode == http.StatusCreated ||
63
response.StatusCode == http.StatusUnprocessableEntity) {
64
return nil, errors.New("backend: unexpected HTTP status " + response.Status)
67
defer response.Body.Close()
68
buf, err := io.ReadAll(response.Body)
74
return nil, errors.New("invalid JSON string")
77
var result CreateUserResponse
78
err = json.Unmarshal(buf, &result)
86
func SendProfileData(profileData ProfileDTO) (*CreateUserResponse, error) {
87
var requestData = struct {
88
User ProfileDTO `json:"user"`
93
jsonBody, err := json.Marshal(requestData)
98
request, err := http.NewRequest(http.MethodPut, apiURL()+fmt.Sprintf("/api/users/%d", profileData.ID), bytes.NewReader(jsonBody))
103
request.Header.Set("Content-Type", "application/json")
105
response, err := send(request)
110
if !(response.StatusCode == http.StatusOK ||
111
response.StatusCode == http.StatusUnprocessableEntity) {
112
return nil, errors.New("backend: unexpected HTTP status " + response.Status)
115
defer response.Body.Close()
116
buf, err := io.ReadAll(response.Body)
121
if !json.Valid(buf) {
122
return nil, errors.New("invalid JSON string")
125
var result CreateUserResponse
126
err = json.Unmarshal(buf, &result)
134
func (u *CreatedUserDTO) Nickname() string {
135
if len(u.DisplayName) > 0 {