-
Notifications
You must be signed in to change notification settings - Fork 0
/
drisqus_threads.go
302 lines (263 loc) · 8.86 KB
/
drisqus_threads.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
// Copyright Piero de Salvia.
// All Rights Reserved
package drisqus
import (
"context"
"net/url"
"sort"
"strconv"
"time"
"github.com/pierods/gisqus"
)
/*
ThreadTrending wraps https://disqus.com/api/docs/trends/listThreads/ (https://disqus.com/api/3.0/trends/listThreads.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
*/
func (d *Drisqus) ThreadTrending(ctx context.Context) ([]*gisqus.Trend, error) {
values := url.Values{}
values.Set("limit", "10")
trendsResponse, err := d.gisqus.ThreadTrending(ctx, values)
if err != nil {
return nil, err
}
return trendsResponse.Response, nil
}
/*
ThreadPostsQuick wraps ThreadPosts. It includes frequently used parameters, and sets the rest to their zero values
When pages is -1, all pages are retrieved.
*/
func (d *Drisqus) ThreadPostsQuick(ctx context.Context, threadID string, pages int) ([]*gisqus.Post, error) {
return d.ThreadPosts(ctx, threadID, pages, []gisqus.Filter{}, []gisqus.Include{}, "", time.Time{}, "")
}
/*
ThreadPosts wraps https://disqus.com/api/docs/threads/listPosts/ (https://disqus.com/api/3.0/threads/listPosts.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
When pages is -1, all pages are retrieved.
*/
func (d *Drisqus) ThreadPosts(ctx context.Context, threadID string, pages int, filters []gisqus.Filter, includes []gisqus.Include, forumID string, since time.Time, order gisqus.Order) ([]*gisqus.Post, error) {
values := url.Values{}
for _, filter := range filters {
values.Add("filter", string(filter))
}
for _, include := range includes {
values.Add("include", string(include))
}
if forumID != "" {
values.Set("forum", forumID)
}
if since != (time.Time{}) {
values.Set("since", gisqus.ToDisqusTime(since))
}
if order != "" {
values.Set("order", string(order))
}
values.Set("limit", "100")
var posts []*gisqus.Post
postResponse, err := d.gisqus.ThreadPosts(ctx, threadID, values)
if err != nil {
return nil, err
}
for page := 0; page < pages || pages == -1; page++ {
posts = append(posts, postResponse.Response...)
if !postResponse.Cursor.HasNext {
break
}
values.Set("cursor", postResponse.Cursor.Next)
postResponse, err = d.gisqus.ThreadPosts(ctx, threadID, values)
if err != nil {
return nil, err
}
}
return posts, nil
}
/*
ThreadListQuick wraps ThreadList. It includes frequently used parameters, and sets the rest to their zero values
When pages is -1, all pages are retrieved.
*/
func (d *Drisqus) ThreadListQuick(ctx context.Context, forumID string, pages int) ([]*gisqus.Thread, error) {
return d.ThreadList(ctx, []string{}, []string{forumID}, []string{}, []string{}, []gisqus.Include{}, pages, time.Time{}, "")
}
/*
ThreadList wraps https://disqus.com/api/docs/threads/list/ (https://disqus.com/api/3.0/threads/list.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
When pages is -1, all pages are retrieved.
*/
func (d *Drisqus) ThreadList(ctx context.Context, categoryIDs, forumIDs, threadIDs, authorIDs []string, includes []gisqus.Include, pages int, since time.Time, order gisqus.Order) ([]*gisqus.Thread, error) {
values := url.Values{}
for _, categoryID := range categoryIDs {
values.Add("category", categoryID)
}
for _, forumID := range categoryIDs {
values.Add("forum", forumID)
}
for _, threadID := range threadIDs {
values.Add("thread", threadID)
}
for _, authorID := range authorIDs {
values.Add("author", authorID)
}
for _, include := range includes {
values.Add("include", string(include))
}
if since != (time.Time{}) {
values.Set("since", gisqus.ToDisqusTime(since))
}
if order != "" {
values.Set("order", string(order))
}
values.Set("limit", "100")
for _, forumID := range forumIDs {
values.Add("forum", forumID)
}
threadResponse, err := d.gisqus.ThreadList(ctx, values)
if err != nil {
return nil, err
}
var threads []*gisqus.Thread
for page := 0; page < pages || pages == -1; page++ {
threads = append(threads, threadResponse.Response...)
if !threadResponse.Cursor.HasNext {
break
}
values.Set("cursor", threadResponse.Cursor.Next)
threadResponse, err = d.gisqus.ThreadList(ctx, values)
if err != nil {
return nil, err
}
}
return threads, nil
}
/*
ThreadHotQuick wraps ThreadHot. It includes frequently used parameters, and sets the rest to their zero values
*/
func (d *Drisqus) ThreadHotQuick(ctx context.Context) ([]*gisqus.Thread, error) {
return d.ThreadHot(ctx, []string{}, []string{}, []string{}, []gisqus.Include{})
}
/*
ThreadHot wraps https://disqus.com/api/docs/threads/listHot/ (https://disqus.com/api/3.0/threads/listHot.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
*/
func (d *Drisqus) ThreadHot(ctx context.Context, categoryIDs, forumIDs, authorIDs []string, includes []gisqus.Include) ([]*gisqus.Thread, error) {
values := url.Values{}
values.Set("limit", "100")
for _, forum := range forumIDs {
values.Add("forum", forum)
}
for _, author := range authorIDs {
values.Add("author", author)
}
for _, include := range includes {
values.Add("include", string(include))
}
threadResponse, err := d.gisqus.ThreadHot(ctx, values)
if err != nil {
return nil, err
}
var threads []*gisqus.Thread
for _, thread := range threadResponse.Response {
threads = append(threads, thread)
}
return threads, nil
}
/*
ThreadPopularQuick wraps ThreadPopular. It includes frequently used parameters, and sets the rest to their zero values
*/
func (d *Drisqus) ThreadPopularQuick(ctx context.Context) ([]*gisqus.Thread, error) {
return d.ThreadPopular(ctx, "", "", "", false)
}
/*
ThreadPopular wraps https://disqus.com/api/docs/threads/listPopular/ (https://disqus.com/api/3.0/threads/listPopular.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
*/
func (d *Drisqus) ThreadPopular(ctx context.Context, categoryID, forumID string, interval gisqus.Interval, withTopPost bool) ([]*gisqus.Thread, error) {
values := url.Values{}
values.Set("limit", "100")
if forumID != "" {
values.Set("forum", forumID)
}
if categoryID != "" {
values.Add("category", categoryID)
}
if interval != "" {
values.Set("interval", string(interval))
}
if withTopPost {
values.Set("with_top_post", strconv.FormatBool(withTopPost))
}
threadResponse, err := d.gisqus.ThreadPopular(ctx, values)
if err != nil {
return nil, err
}
var threads []*gisqus.Thread
for _, thread := range threadResponse.Response {
threads = append(threads, thread)
}
return threads, nil
}
/*
ThreadSet wraps https://disqus.com/api/docs/threads/set/ (https://disqus.com/api/3.0/threads/set.json)
*/
func (d *Drisqus) ThreadSet(ctx context.Context, threadIDs []string) ([]*gisqus.Thread, error) {
values := url.Values{}
gisqusResponse, err := d.gisqus.ThreadSet(ctx, threadIDs, values)
if err != nil {
return nil, err
}
return gisqusResponse.Response, nil
}
/*
ThreadDetails wraps https://disqus.com/api/docs/threads/details/ (https://disqus.com/api/3.0/threads/details.json)
It does not support the "related" argument (related fields can be gotten with calls to their respective APIS)
*/
func (d *Drisqus) ThreadDetails(ctx context.Context, threadID string) (*gisqus.ThreadDetail, error) {
values := url.Values{}
gisqusResponse, err := d.gisqus.ThreadDetails(ctx, threadID, values)
if err != nil {
return nil, err
}
return gisqusResponse.Response, nil
}
/*
ThreadUsersVoted wraps https://disqus.com/api/docs/threads/listUsersVotedThread/ (https://disqus.com/api/3.0/threads/listUsersVotedThread.json)
Complete users are not returned by Disqus on this call
*/
func (d *Drisqus) ThreadUsersVoted(ctx context.Context, threadID string) ([]*gisqus.User, error) {
values := url.Values{}
values.Set("limit", "100")
gisqusResponse, err := d.gisqus.ThreadUsersVoted(ctx, threadID, values)
if err != nil {
return nil, err
}
return gisqusResponse.Response, nil
}
// ThreadsByPostCount is used by threadSorter to sort Threads
func ThreadsByPostCount(p1, p2 *gisqus.Thread) bool {
return p1.Posts > p2.Posts
}
// ThreadsByLikes is used by threadSorter to sort Threads
func ThreadsByLikes(p1, p2 *gisqus.Thread) bool {
return p1.Likes > p2.Likes
}
// ThreadBy is an utility type used to sort Threads
type ThreadBy func(t1, t2 *gisqus.Thread) bool
type threadSorter struct {
threads []*gisqus.Thread
by func(t1, t2 *gisqus.Thread) bool
}
// Sort is the sorting method of ThreadBy
func (by ThreadBy) Sort(threads []*gisqus.Thread) {
ts := &threadSorter{
threads: threads,
by: by,
}
sort.Sort(ts)
}
func (s *threadSorter) Len() int {
return len(s.threads)
}
func (s *threadSorter) Swap(i, j int) {
s.threads[i], s.threads[j] = s.threads[j], s.threads[i]
}
func (s *threadSorter) Less(i, j int) bool {
return s.by(s.threads[i], s.threads[j])
}