-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathboard.go
463 lines (391 loc) · 13.5 KB
/
board.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package vkapi
import (
"crypto/rand"
"encoding/json"
"net/url"
"strconv"
"strings"
)
type Topic struct {
ID int `json:"id"`
Title string `json:"title"`
Created int64 `json:"created"`
CreatedBy int `json:"created_by"`
Updated int64 `json:"updated"`
UpdatedBy int `json:"updated_by"`
IsClosed int `json:"is_closed"`
IsFixed int `json:"is_fixed"`
Comments int `json:"comments"`
FirstComment string `json:"first_comment"`
LastComment string `json:"last_comment"`
}
type Topics struct {
Count int `json:"count"`
Topics []*Topic `json:"items"`
DefaultOrder float64 `json:"default_order"`
CanAddTopics int `json:"can_add_topics"`
Profiles []*User `json:"profiles"`
}
type TopicCommentLike struct {
Count int `json:"count"`
UserLikes int `json:"user_likes"`
CanLike int `json:"can_like"`
}
type TopicComment struct {
ID int `json:"id"`
FromID int `json:"from_id"`
Date int64 `json:"date"`
Text string `json:"text"`
Likes *TopicCommentLike `json:"likes"`
ReplyToUID int `json:"reply_to_uid"`
ReplyToCID int `json:"reply_to_cid"`
Attachments []*Attachment `json:"attachments"`
}
type Attachment struct {
Type string `json:"type"`
Photo *AttachmentPhoto `json:"photo"`
Video *AttachmentVideo `json:"video"`
Audio *AttachmentAudio `json:"audio"`
Doc *AttachmentDoc `json:"doc"`
Sticker *AttachmentSticker `json:"sticker"`
}
type AttachmentImageInfo struct {
Type string `json:"type"`
Url string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
WithPadding int `json:"with_padding"`
}
type AttachmentPhoto struct {
ID int `json:"id"`
AlbumID int `json:"album_id"`
OwnerID int `json:"owner_id"`
UserID int `json:"user_id"`
Sizes []*AttachmentImageInfo `json:"sizes"`
Text string `json:"text"`
Date int64 `json:"date"`
AccessKey string `json:"access_key"`
PostID int `json:"post_id"`
}
type AttachmentVideo struct {
AccessKey string `json:"access_key"`
CanComment int `json:"can_comment"`
CanEdit int `json:"can_edit"`
CanLike int `json:"can_like"`
CanRepost int `json:"can_repost"`
CanSubscribe int `json:"can_subscribe"`
CanAddToFaves int `json:"can_add_to_faves"`
CanAdd int `json:"can_add"`
CanAttachLink int `json:"can_attach_link"`
Comments int `json:"comments"`
Date int64 `json:"date"`
Description string `json:"description"`
Duration int `json:"duration"`
Image []*AttachmentImageInfo `json:"image"`
FirstFrame []*AttachmentImageInfo `json:"first_frame"`
Width int `json:"width"`
Height int `json:"height"`
ID int `json:"id"`
OwnerID int `json:"owner_id"`
UserID int `json:"user_id"`
Title string `json:"title"`
IsFavorite bool `json:"is_favorite"`
TrackCode string `json:"track_code"`
Type string `json:"type"`
Views int `json:"views"`
LocalViews int `json:"local_views"`
Platform string `json:"platform"`
}
type AttachmentAudio struct {
Artist string `json:"artist"`
ID int `json:"id"`
OwnerID int `json:"owner_id"`
Title string `json:"title"`
Duration int `json:"duration"`
AccessKey string `json:"access_key"`
IsLicensed bool `json:"is_licensed"`
TrackCode string `json:"track_code"`
Url string `json:"url"`
Date int64 `json:"date"`
Album *AttachmentAudioAlbum `json:"album"`
MainArtist []*AttachmentAudioArtist `json:"main_artist"`
}
type AttachmentAudioAlbum struct {
ID int `json:"id"`
Title string `json:"title"`
OwnerID int `json:"owner_id"`
AccessKey string `json:"access_key"`
Thumb *AttachmentAudioAlbumThumb `json:"thumb"`
}
type AttachmentAudioAlbumThumb struct {
Width int `json:"width"`
Height int `json:"height"`
Photo34 string `json:"photo_34"`
Photo68 string `json:"photo_68"`
Photo135 string `json:"photo_135"`
Photo270 string `json:"photo_270"`
Photo300 string `json:"photo_300"`
Photo600 string `json:"photo_600"`
}
type AttachmentAudioArtist struct {
Name string `json:"name"`
Domain string `json:"domain"`
ID string `json:"id"`
}
type AttachmentDoc struct {
ID int `json:"id"`
OwnerID int `json:"owner_id"`
Title string `json:"title"`
Size int `json:"size"`
Ext string `json:"ext"`
Url string `json:"url"`
Date int64 `json:"date"`
Type int `json:"type"`
IsLicensed int `json:"is_licensed"` // NOTE(Pedro): There is some inconsistency on VK API, this field can return "true" or zero for false
AccessKey string `json:"access_key"`
}
type AttachmentSticker struct {
ProductID int `json:"product_id"`
StickerID int `json:"sticker_id"`
Images []*AttachmentImageInfo `json:"images"`
ImagesWithBackground []*AttachmentImageInfo `json:"images_with_background"`
}
type Poll struct {
ID int `json:"id"`
OwnerID int `json:"owner_id"`
Created int64 `json:"created"`
Question string `json:"question"`
Votes int `json:"votes"`
Answers []*PollAnswer `json:"answers"`
Anonymous bool `json:"anonymous"`
Multiple bool `json:"multiple"`
AnswerIDS []int `json:"answer_ids"`
EndDate int64 `json:"end_date"`
Closed bool `json:"closed"`
IsBoard bool `json:"is_board"`
CanEdit bool `json:"can_edit"`
CanVote bool `json:"can_vote"`
CanReport bool `json:"can_report"`
CanShare bool `json:"can_share"`
}
type PollAnswer struct {
ID int `json:"id"`
Text string `json:"text"`
Votes int `json:"votes"`
Rate float64 `json:"rate"`
}
type Comments struct {
Count int `json:"count"`
Comments []*TopicComment `json:"items"`
Poll *Poll `json:"poll"`
Profiles []*User `json:"profiles"`
RealOffset int `json:"real_offset"`
}
func (client *VKClient) BoardAddTopic(groupID int, title string, text string, fromGroup bool, attachments []string) (int, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("title", title)
params.Set("text", text)
params.Set("from_group", strconv.Itoa(BoolToInt(fromGroup)))
params.Set("attachments", strings.Join(attachments, ","))
resp, err := client.MakeRequest("board.addTopic", params)
if err != nil {
return 0, err
}
var id int
if err = json.Unmarshal(resp.Response, &id); err != nil {
return 0, err
}
return id, nil
}
func (client *VKClient) BoardCloseTopic(groupID int, topicID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
resp, err := client.MakeRequest("board.closeTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardCreateComment(groupID int, topicID int, message string, attachments []string, fromGroup bool, stickerID int) (int, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("message", message)
params.Set("from_group", strconv.Itoa(BoolToInt(fromGroup)))
params.Set("sticker_id", strconv.Itoa(stickerID))
params.Set("attachments", strings.Join(attachments, ","))
guid := make([]byte, 16)
_, err := rand.Read(guid)
if err != nil {
return 0, err
}
params.Set("guid", string(guid))
resp, err := client.MakeRequest("board.createComment", params)
if err != nil {
return 0, err
}
var id int
if err = json.Unmarshal(resp.Response, &id); err != nil {
return 0, err
}
return id, nil
}
func (client *VKClient) BoardDeleteComment(groupID int, topicID int, commetID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("comment_id", strconv.Itoa(commetID))
resp, err := client.MakeRequest("board.deleteComment", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardDeleteTopic(groupID int, topicID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
resp, err := client.MakeRequest("board.deleteTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardEditComment(groupID int, topicID int, commentID int, message string, attachments []string) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("comment_id", strconv.Itoa(commentID))
params.Set("message", message)
params.Set("attachments", strings.Join(attachments, ","))
resp, err := client.MakeRequest("board.editComment", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardEditTopic(groupID int, topicID int, title string) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("title", title)
resp, err := client.MakeRequest("board.editTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardFixTopic(groupID int, topicID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
resp, err := client.MakeRequest("board.fixTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardGetComments(groupID int, topicID int, count int, params url.Values) (*Comments, error) {
if params == nil {
params = url.Values{}
}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("count", strconv.Itoa(count))
resp, err := client.MakeRequest("board.getComments", params)
if err != nil {
return nil, err
}
var comments *Comments
err = json.Unmarshal(resp.Response, &comments)
if err != nil {
return nil, err
}
return comments, nil
}
func (client *VKClient) BoardGetTopics(groupID int, count int, params url.Values) (*Topics, error) {
if params == nil {
params = url.Values{}
}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("count", strconv.Itoa(count))
resp, err := client.MakeRequest("board.getTopics", params)
if err != nil {
return nil, err
}
var topics *Topics
err = json.Unmarshal(resp.Response, &topics)
if err != nil {
return nil, err
}
return topics, nil
}
func (client *VKClient) BoardOpenTopic(groupID int, topicID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
resp, err := client.MakeRequest("board.openTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardRestoreComment(groupID int, topicID int, commentID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
params.Set("comment_id", strconv.Itoa(commentID))
resp, err := client.MakeRequest("board.restoreComment", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}
func (client *VKClient) BoardUnfixTopic(groupID int, topicID int) (bool, error) {
params := url.Values{}
params.Set("group_id", strconv.Itoa(groupID))
params.Set("topic_id", strconv.Itoa(topicID))
resp, err := client.MakeRequest("board.unfixTopic", params)
if err != nil {
return false, err
}
var ok int
if err = json.Unmarshal(resp.Response, &ok); err != nil {
return false, err
}
return IntToBool(ok), nil
}