This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
202 lines (157 loc) · 4.66 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package stream_chat
import (
"errors"
"net/http"
"net/url"
"path"
"time"
)
type Mute struct {
User User
Target User
CreatedAt time.Time
UpdatedAt time.Time
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
Role string `json:"role"`
Online bool `json:"online"`
Invisible bool `json:"invisible"`
Mutes []*Mute `json:"mutes"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastActive time.Time `json:"last_active"`
ExtraData map[string]interface{} `json:"-,extra"`
}
// Create a mute
// targetID: the user getting muted
// userID: the user muting the target
func (c *Client) MuteUser(targetID string, userID string) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
}
data := map[string]interface{}{
"target_id": targetID,
"user_id": userID,
}
return c.makeRequest(http.MethodPost, "moderation/mute", nil, data, nil)
}
// Removes a mute
// targetID: the user getting un-muted
// userID: the user muting the target
func (c *Client) UnmuteUser(targetID string, userID string) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
}
data := map[string]interface{}{
"target_id": targetID,
"user_id": userID,
}
return c.makeRequest(http.MethodPost, "moderation/unmute", nil, data, nil)
}
func (c *Client) FlagUser(targetID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case len(options) == 0:
return errors.New("flag user: options must be not empty")
}
options["target_user_id"] = targetID
return c.makeRequest(http.MethodPost, "moderation/flag", nil, options, nil)
}
func (c *Client) UnFlagUser(targetID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case options == nil:
options = map[string]interface{}{}
}
options["target_user_id"] = targetID
return c.makeRequest(http.MethodPost, "moderation/unflag", nil, options, nil)
}
func (c *Client) BanUser(targetID string, userID string, options map[string]interface{}) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case userID == "":
return errors.New("user ID is empty")
case options == nil:
options = map[string]interface{}{}
}
options["target_user_id"] = targetID
options["user_id"] = userID
return c.makeRequest(http.MethodPost, "moderation/ban", nil, options, nil)
}
func (c *Client) UnBanUser(targetID string, options map[string]string) error {
switch {
case targetID == "":
return errors.New("target ID is empty")
case options == nil:
options = map[string]string{}
}
var params = map[string][]string{}
for k, v := range options {
params[k] = []string{v}
}
params["target_user_id"] = []string{targetID}
return c.makeRequest(http.MethodDelete, "moderation/ban", params, nil, nil)
}
func (c *Client) ExportUser(targetID string, options map[string][]string) (user *User, err error) {
if targetID == "" {
return user, errors.New("target ID is empty")
}
p := path.Join("users", url.PathEscape(targetID), "export")
err = c.makeRequest(http.MethodGet, p, options, nil, user)
return user, err
}
func (c *Client) DeactivateUser(targetID string, options map[string]interface{}) error {
if targetID == "" {
return errors.New("target ID is empty")
}
p := path.Join("users", url.PathEscape(targetID), "deactivate")
return c.makeRequest(http.MethodPost, p, nil, options, nil)
}
func (c *Client) DeleteUser(targetID string, options map[string][]string) error {
if targetID == "" {
return errors.New("target ID is empty")
}
p := path.Join("users", url.PathEscape(targetID))
return c.makeRequest(http.MethodDelete, p, options, nil, nil)
}
type usersResponse struct {
Users map[string]*User `json:"users"`
}
type usersRequest struct {
Users map[string]userRequest `json:"Users"`
}
type userRequest struct {
*User
// readonly fields
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
LastActive time.Time `json:"-"`
}
// UpdateUsers send update users request, returns updated user info
func (c *Client) UpdateUsers(users ...*User) (map[string]*User, error) {
if len(users) == 0 {
return nil, errors.New("users are not set")
}
req := usersRequest{Users: make(map[string]userRequest, len(users))}
for _, u := range users {
req.Users[u.ID] = userRequest{User: u}
}
var resp usersResponse
err := c.makeRequest(http.MethodPost, "users", nil, req, &resp)
if err != nil {
return nil, err
}
return resp.Users, err
}