forked from vartanbeno/go-reddit
/
post-and-comment.go
182 lines (139 loc) · 4.31 KB
/
post-and-comment.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
package reddit
import (
"context"
"net/http"
"net/url"
"strconv"
)
// postAndCommentService handles communication with the post and comment
// related methods of the Reddit API.
// This service holds functionality common to both posts and comments.
//
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments
type postAndCommentService struct {
client *Client
}
type vote int
// Reddit interprets -1, 0, 1 as downvote, no vote, and upvote, respectively.
const (
downvote vote = iota - 1
novote
upvote
)
// Delete a post or comment via its full ID.
func (s *postAndCommentService) Delete(ctx context.Context, id string) (*Response, error) {
path := "api/del"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Save a post or comment.
func (s *postAndCommentService) Save(ctx context.Context, id string) (*Response, error) {
path := "api/save"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Unsave a post or comment.
func (s *postAndCommentService) Unsave(ctx context.Context, id string) (*Response, error) {
path := "api/unsave"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// EnableReplies enables inbox replies for one of your posts or comments.
func (s *postAndCommentService) EnableReplies(ctx context.Context, id string) (*Response, error) {
path := "api/sendreplies"
form := url.Values{}
form.Set("id", id)
form.Set("state", "true")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// DisableReplies dsables inbox replies for one of your posts or comments.
func (s *postAndCommentService) DisableReplies(ctx context.Context, id string) (*Response, error) {
path := "api/sendreplies"
form := url.Values{}
form.Set("id", id)
form.Set("state", "false")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Lock a post or comment, preventing it from receiving new comments.
func (s *postAndCommentService) Lock(ctx context.Context, id string) (*Response, error) {
path := "api/lock"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Unlock a post or comment, allowing it to receive new comments.
func (s *postAndCommentService) Unlock(ctx context.Context, id string) (*Response, error) {
path := "api/unlock"
form := url.Values{}
form.Set("id", id)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
func (s *postAndCommentService) vote(ctx context.Context, id string, vote vote) (*Response, error) {
path := "api/vote"
form := url.Values{}
form.Set("id", id)
form.Set("dir", strconv.Itoa(int(vote)))
form.Set("rank", "10")
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// Upvote a post or a comment.
func (s *postAndCommentService) Upvote(ctx context.Context, id string) (*Response, error) {
return s.vote(ctx, id, upvote)
}
// Downvote a post or a comment.
func (s *postAndCommentService) Downvote(ctx context.Context, id string) (*Response, error) {
return s.vote(ctx, id, downvote)
}
// RemoveVote removes your vote on a post or a comment.
func (s *postAndCommentService) RemoveVote(ctx context.Context, id string) (*Response, error) {
return s.vote(ctx, id, novote)
}
// Report a post or comment.
// The reason must not be longer than 100 characters.
func (s *postAndCommentService) Report(ctx context.Context, id string, reason string) (*Response, error) {
path := "api/report"
form := url.Values{}
form.Set("api_type", "json")
form.Set("thing_id", id)
form.Set("reason", reason)
req, err := s.client.NewRequest(http.MethodPost, path, form)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}