~thrrgilag/woodstock

woodstock/users.go -rw-r--r-- 13.4 KiB
62b10b5cMorgan McMillian Update for new project location and module name 3 months ago
                                                                                
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package woodstock

import (
	"io"
	"net/url"
	"strconv"
	"strings"
)

// UserResult user response object
type UserResult struct {
	*CommonResponse
	Data User `json:"data"`
}

// UsersResult response object
type UsersResult struct {
	*CommonResponse
	Data []User `json:"data"`
}

// StringResult resposne object
type StringResult struct {
	Data string `json:"data"`
}

// PresenceResult response object
type PresenceResult struct {
	*CommonResponse
	Data Presence `json:"data"`
}

// NumberResult response object
type NumberResult struct {
	*CommonResponse
	Data int `json:"data"`
}

// MessagesResult response object
type MessagesResult struct {
	*CommonResponse
	Data []Message `json:"data"`
}

// ClientInfosResult response object
type ClientInfosResult struct {
	*CommonResponse
	Data []ClientInfo `json:"data"`
}

// GetUser retrieves a user
// https://pnut.io/docs/resources/users/lookup#get-users-id
func (c *Client) GetUser(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetUsers retrieves multiple users
// https://pnut.io/docs/resources/users/lookup#get-users
func (c *Client) GetUsers(ids []string) (result UsersResult, err error) {
	v := url.Values{}
	v.Set("ids", strings.Join(ids, ","))

	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "?" + v.Encode(), data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// ReplaceProfile replaces a user profile
// this func will be updated
// https://pnut.io/docs/resources/users/profile#put-users-me
func (c *Client) ReplaceProfile(json string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI, data: &result, method: "PUT", responseCh: responseCh, json: json}
	return result, (<-responseCh).err
}

// UpdateProfile updates a user profile
// this func will be updated
// https://pnut.io/docs/resources/users/profile#patch-users-me
func (c *Client) UpdateProfile(json string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI, data: &result, method: "PATCH", responseCh: responseCh, json: json}
	return result, (<-responseCh).err
}

// GetAvatarURL retrieves a users avatar URL
// https://pnut.io/docs/resources/users/profile#get-users-id-avatar
func (c *Client) GetAvatarURL(id string, v ...url.Values) (url string, err error) {
	param := ""
	if len(v) > 0 {
		param = v[0].Encode()
	}
	if param != "" {
		param = "?" + param
	}
	result := &StringResult{}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/avatar" + param, data: &result, method: "GET", responseCh: responseCh, redirect: true}
	return result.Data, (<-responseCh).err
}

// UploadAvatar updates a user avatar
// https://pnut.io/docs/resources/users/profile#post-users-me-avatar
func (c *Client) UploadAvatar(params map[string]string, reader io.Reader) (result UserResult, err error) {
	params["field"] = "avatar"
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/avatar", params: params, reader: reader, data: &result, method: "POST", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetCoverURL retireves a cover URL
// https://pnut.io/docs/resources/users/profile#get-users-id-cover
func (c *Client) GetCoverURL(id string, v ...url.Values) (url string, err error) {
	param := ""
	if len(v) > 0 {
		param = v[0].Encode()
	}
	if param != "" {
		param = "?" + param
	}
	result := &StringResult{}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/cover" + param, data: &result, method: "GET", responseCh: responseCh, redirect: true}
	return result.Data, (<-responseCh).err
}

// UploadCover updates a cover image
// https://pnut.io/docs/resources/users/profile#post-users-me-cover
func (c *Client) UploadCover(params map[string]string, reader io.Reader) (result UserResult, err error) {
	params["field"] = "cover"
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/cover", params: params, reader: reader, data: &result, method: "POST", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetFollowing retrieve following list
// https://pnut.io/docs/resources/users/following#get-users-id-following
func (c *Client) GetFollowing(id string, count ...int) (result UsersResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/following" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetFollowers retrieve followers list
// https://pnut.io/docs/resources/users/followers#get-users-id-followers
func (c *Client) GetFollowers(id string, count ...int) (result UsersResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/followers" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Follow a user
// https://pnut.io/docs/resources/users/following#put-users-id-follow
func (c *Client) Follow(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/follow", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnFollow a user
//https://pnut.io/docs/resources/users/following#delete-users-id-follow
func (c *Client) UnFollow(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/follow", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMuted retrieve muted users
// https://pnut.io/docs/resources/users/muting#get-users-id-muted
func (c *Client) GetMuted() (result UsersResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/muted", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Mute a user
// https://pnut.io/docs/resources/users/muteing#put-users-id-mute
func (c *Client) Mute(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/mute", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnMute a user
// https://pnut.io/docs/resources/users/muting#delete-users-id-mute
func (c *Client) UnMute(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/mute", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetBlocked retrieve blocked users
// https://pnut.io/docs/resources/users/muting#get-users-id-blocked
func (c *Client) GetBlocked() (result UsersResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/blocked", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// Block a user
// https://pnut.io/docs/resources/users/blocking#put-users-id-block
func (c *Client) Block(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/block", data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// UnBlock a user
// https://pnut.io/docs/resources/posts/bookmarks#delete-posts-id-bookmark
func (c *Client) UnBlock(id string) (result UserResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/block", data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetPresence retrieve presence user
// https://pnut.io/docs/resources/users/presence#get-users-id-presence
func (c *Client) GetPresence(id string) (result PresenceResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/presence", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// SetPresence for user
// https://pnut.io/docs/resources/users/presence#put-users-id-presence
func (c *Client) SetPresence(presence string) (result PresenceResult, err error) {
	v := url.Values{}
	v.Set("presence", presence)
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/me/presence", form: v, data: &result, method: "PUT", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMentions retrieves mentions
// https://pnut.io/docs/resources/posts/streams#get-users-id-mentions
func (c *Client) GetMentions(id string, count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/mentions" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetPostsFromUser retrieve posts from a user
// https://pnut.io/docs/resources/posts/streams#get-users-id-posts
func (c *Client) GetPostsFromUser(id string, count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/posts" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetBookmarks retrieve a users bookmarks
// https://pnut.io/docs/resources/posts/bookmarks#get-users-id-bookmarks
func (c *Client) GetBookmarks(id string, count ...int) (result PostsResult, err error) {
	v := url.Values{}
	if len(count) > 0 {
		v.Set("count", strconv.Itoa(count[0]))
	}
	param := v.Encode()
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/bookmarks" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetActionsForMe retireve a users actions
// https://pnut.io/docs/resources/users/interactions#get-users-me-interactions
func (c *Client) GetActionsForMe(v ...url.Values) (result ActionsResult, err error) {
	param := ""
	if len(v) > 0 {
		param = v[0].Encode()
	}
	if param != "" {
		param = "?" + param
	}
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/interactions" + param, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetChannelsByMe retireves channels created by user
// https://pnut.io/docs/resources/channels/lookup#get-users-me-channels
func (c *Client) GetChannelsByMe() (result ChannelsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserMeAPI + "/channels", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetNumberOfUnreadPM retireves the number of unread private messages
// https://pnut.io/docs/resources/channels/lookup#get-users-me-channels-num_unread-pm
func (c *Client) GetNumberOfUnreadPM() (result NumberResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UnreadPMNumberAPI, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// MarkReadPM marks private messages as read
// https://pnut.io/docs/resources/channels/lookup#delete-users-me-channels-num_unread-pm
func (c *Client) MarkReadPM() (result NumberResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UnreadPMNumberAPI, data: &result, method: "DELETE", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetSubscribedChannels retrieves list of subscribed channels
// https://pnut.io/docs/resources/channels/subscribing#get-users-me-channels-subscribed
func (c *Client) GetSubscribedChannels() (result ChannelsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: SubscribedChannelsAPI, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMutedChannels retrieves list of muted channels
// https://pnut.io/docs/resources/channels/muting#get-users-me-channels-muted
func (c *Client) GetMutedChannels() (result ChannelsResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: MutedChannelsAPI, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetMessagesByMe retireves messages created by user
// https://pnut.io/docs/resources/messages/lookup#get-users-me-messages
func (c *Client) GetMessagesByMe() (result MessagesResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: MeMessagesAPI, data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}

// GetClients retrieves client information
// https://pnut.io/docs/resources/clients#get-users-id-clients
func (c *Client) GetClients(id string) (result ClientInfosResult, err error) {
	responseCh := make(chan response)
	c.queryQueue <- query{url: UserAPI + "/" + id + "/clients", data: &result, method: "GET", responseCh: responseCh}
	return result, (<-responseCh).err
}