-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.go
118 lines (102 loc) · 2.58 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
package vkutil
import (
"context"
"encoding/json"
"log"
"net/url"
"github.com/zhuharev/vk"
)
const (
MethodBoardGetTopics = "board.getTopics"
)
// Topic board item
type Topic struct {
ID int `json:"id"`
Title string `json:"title"`
Created EpochTime `json:"created"`
CreatedBy int `json:"created_by"`
Updated EpochTime `json:"updated"`
UpdatedBy int `json:"updated_by"`
IsClosed int `json:"is_closed"`
Comments int `json:"comments"`
}
type RespTopic struct {
Count int `json:"count"`
Items []Topic `json:"items"`
}
// ResponseTopics represent topic items
type ResponseTopics struct {
Resp RespTopic `json:"response"`
}
// BoardGetComments returns board items
func (api *Api) BoardGetComments(ctx context.Context, groupID, topicID int, args ...url.Values) ([]Comment, error) {
params := setToUrlValues("group_id", groupID, args...)
params = setToUrlValues("topic_id", topicID, params)
//params = setToUrlValues("sort", "desc", params)
bts, err := api.VkApi.Request(vk.METHOD_BOARD_GET_COMMENTS, params)
if err != nil {
return nil, err
}
log.Println(string(bts))
var r ResponseComments
err = json.Unmarshal(bts, &r)
if err != nil {
return nil, err
}
return r.Response.Items, nil
}
func (api *Api) BoardGetAllComments(ctx context.Context, groupID int, topicID int) ([]Comment, error) {
var (
count = 100
offset = 0
result []Comment
)
for {
params := setToUrlValues("count", count)
params = setToUrlValues("offset", offset, params)
comments, err := api.BoardGetComments(ctx, groupID, topicID, params)
if err != nil {
return nil, err
}
offset += len(comments)
result = append(result, comments...)
if len(comments) < count {
break
}
}
return result, nil
}
func (api *Api) BoardGetAllTopics(ctx context.Context, groupID int) ([]Topic, error) {
var (
count = 100
offset = 0
result []Topic
)
for {
topics, err := api.BoardGetTopics(ctx, groupID, count, offset)
if err != nil {
return nil, err
}
offset += len(topics)
result = append(result, topics...)
if len(topics) < count {
break
}
}
return result, nil
}
func (api *Api) BoardGetTopics(ctx context.Context, groupID int, count int, offset int) ([]Topic, error) {
params := setToUrlValues("group_id", groupID)
params = setToUrlValues("count", count, params)
params = setToUrlValues("offset", offset, params)
bts, err := api.VkApi.Request(MethodBoardGetTopics, params)
if err != nil {
return nil, err
}
var r ResponseTopics
err = json.Unmarshal(bts, &r)
if err != nil {
return nil, err
}
return r.Resp.Items, nil
}