This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
141 lines (131 loc) · 3.29 KB
/
entry.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
package cmtt
import (
"fmt"
"strconv"
"strings"
)
//GetEntry returns entry by ID
func (api *Cmtt) GetEntry(entryID int) (*Entry, error) {
res, err := api.execute("entry/{id}", map[string]string{
"id": strconv.Itoa(entryID),
}, nil, nil)
if err != nil {
return nil, err
}
result := &Entry{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
//GetEntryComments returns array of comments for the specified entry
func (api *Cmtt) GetEntryComments(entryID int, hash, sorting string) (*[]Comment, error) {
res, err := api.execute("entry/{id}/comments/{sorting}", map[string]string{
"id": strconv.Itoa(entryID),
"sorting": sorting,
}, map[string]string{
"hash": hash,
}, nil)
if err != nil {
return nil, err
}
result := &[]Comment{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
//GetFlashEntries returns urgent news. Usually contains only one element
func (api *Cmtt) GetFlashEntries() (*[]Entry, error) {
res, err := api.execute("getflashholdedentry", nil, nil, nil)
if err != nil {
return nil, err
}
result := &[]Entry{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
//GetEntryBundles returns short info about entries specified
func (api *Cmtt) GetEntryBundles(IDs []int) (*map[string]EntryBundle, error) {
res, err := api.execute("entry/bundle", nil, map[string]string{
"ids": strings.Trim(strings.Join(strings.Fields(fmt.Sprint(IDs)), ","), "[]"),
}, nil)
if err != nil {
return nil, err
}
result := &map[string]EntryBundle{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
//GetRecentNews returns list of entries shown as news on VC
func (api *Cmtt) GetRecentNews(count, offset int) (*[]Entry, error) {
res, err := api.execute("news/default/recent", nil, map[string]string{
"count": strconv.Itoa(count),
"offset": strconv.Itoa(offset),
}, nil)
if err != nil {
return nil, err
}
result := &[]Entry{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
//LikeEntry likes the specified entry
func (api *Cmtt) LikeEntry(entryID int) (*Likes, error) {
return api.rate(entryID, 1)
}
//DislikeEntry dislikes the specified entry
func (api *Cmtt) DislikeEntry(entryID int) (*Likes, error) {
return api.rate(entryID, -1)
}
//CommentEntry adds a comment to the specified entry
func (api *Cmtt) CommentEntry(entryID int, text string, replyTo int, attachments map[string]string) (*Comment, error) {
body := map[string]string{
"text": text,
"reply_to": strconv.Itoa(replyTo),
}
if attachments != nil {
for k, v := range attachments {
body[k] = v
}
}
res, err := api.execute("entry/{id}/comments", map[string]string{
"id": strconv.Itoa(entryID),
}, nil, body)
if err != nil {
return nil, err
}
result := &Comment{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}
func (api *Cmtt) rate(entryID, sign int) (*Likes, error) {
res, err := api.execute("entry/{id}/likes", map[string]string{
"id": strconv.Itoa(entryID),
}, nil, map[string]string{
"sign": strconv.Itoa(sign),
})
if err != nil {
return nil, err
}
result := &Likes{}
err = castStruct(res, result)
if err != nil {
return nil, err
}
return result, err
}