-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathuser.go
142 lines (135 loc) · 5.24 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package godisco
import (
"encoding/json"
"fmt"
)
// UserResponse define json response
type UserResponse struct {
UserBadges []struct {
ID int `json:"id"`
Granted string `json:"granted_at"`
BadgeID int `json:"badge_id"`
UserID int `json:"user_id"`
GrantedBy int `json:"granted_by_id"`
} `json:"user_badges,omitempty"`
Badges []struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
GrantCount int `json:"grant_count"`
AllowTitle bool `json:"allow_title"`
MultiGrant bool `json:"multiple_grant"`
Icon string `json:"icon"`
Image string `json:"image"`
Listable bool `json:"listable"`
Enabled bool `json:"enabled"`
BadgeGroupID int `json:"badge_grouping_id"`
System bool `json:"system"`
Slug string `json:"slug"`
BadgeTypeID int `json:"badge_type_id"`
} `json:"badges,omitempty"`
BadgeTypes []struct {
ID int `json:"id"`
Name string `json:"name"`
Order int `json:"sort_order"`
} `json:"badge_types,omitempty"`
Users []struct {
ID int `json:"id"`
Username string `json:"username"`
Avatar string `json:"avatar_template"`
Name string `json:"name"`
Moderator bool `json:"moderator"`
Admin bool `json:"admin"`
} `json:"users,omitempty"`
User userInfo `json:"user"`
Errors []string `json:"errors,omitempty"`
ErrorType string `json:"error_type,omitempty"`
}
type userInfo struct {
ID int `json:"id"`
Username string `json:"username"`
Avatar string `json:"avatar_template"`
Name string `json:"name"`
Posted string `json:"last_posted_at"`
Seen string `json:"last_seen_at"`
Created string `json:"created_at"`
Website string `json:"website_name"`
Edit bool `json:"can_edit"`
EditUser bool `json:"can_edit_username"`
EditEmail bool `json:"can_edit_email"`
EditName bool `json:"can_edit_name"`
PM bool `json:"can_send_private_messages"`
PMUser bool `json:"can_send_private_message_to_user"`
TrustLevel int `json:"trust_level"`
Moderator bool `json:"moderator"` // true,
Admin bool `json:"admin"` // true,
Title string `json:"title"` // null,
AvatarID int `json:"uploaded_avatar_id"` // 15,
BadgeCount int `json:"badge_count"` // 0,
CustomFields struct{} `json:"custom_fields"` // {},
Pending int `json:"pending_count"` // 0,
View int `json:"profile_view_count"` // 2,
InvitedBy string `json:"invited_by"` // null,
Groups []struct {
ID int `json:"id"` // 10,
Auto bool `json:"automatic"` // true,
Name string `json:"name"` // "trust_level_0",
Count int `json:"user_count"` // 2,
Alias int `json:"alias_level"` // 0,
Visible bool `json:"visible"` // true,
Domains string `json:"automatic_membership_email_domains"` // null,
Retro bool `json:"automatic_membership_retroactive"` // false,
Primary bool `json:"primary_group"` // false,
Title string `json:"title"` // null,
Trust string `json:"grant_trust_level"` // null,
Messages bool `json:"has_messages"` // false,
Mentionable bool `json:"mentionable"` // false
} `json:"groups"` //
Featured []string `json:"featured_user_badge_ids"` // [],
Card string `json:"card_badge"` // null
}
type user struct {
Name string `json:"name"`
Username string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
Active bool `json:"active"`
}
// CreateResp typical response after user update
type CreateResp struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Active bool `json:"active,omitempty"`
UserID int `json:"user_id,omitempty"`
Errors json.RawMessage `json:"errors,omitempty"`
Values json.RawMessage `json:"values,omitempty"`
Developer bool `json:"is_developer"`
}
// GetUser sends a request for information about the user
func GetUser(req Requester, user string) (userInfo *UserResponse, err error) {
endpoint := fmt.Sprintf("/users/%s.json", user)
body, _, err := req.Get(endpoint)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &userInfo)
return userInfo, err
}
// CreateUser creates a new user based on details provided
func CreateUser(req Requester, name string, username string, email string, password string, active bool) (response *CreateResp, err error) {
update := &user{
Name: name,
Username: username,
Email: email,
Password: password,
Active: active,
}
data, err := json.Marshal(update)
endpoint := "/users"
body, _, err := req.Post(endpoint, data)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &response)
return response, err
}