-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreplies.go
More file actions
292 lines (241 loc) · 8.86 KB
/
Copy pathreplies.go
File metadata and controls
292 lines (241 loc) · 8.86 KB
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
package threads
import (
"context"
"encoding/json"
"fmt"
"net/url"
)
// buildRepliesParams builds query parameters for replies and conversation requests
func buildRepliesParams(opts *RepliesOptions, maxLimit int, limitDescription string) (url.Values, error) {
params := url.Values{
"fields": {ReplyFields},
}
if opts != nil {
if opts.Limit > 0 {
if opts.Limit > maxLimit {
return nil, NewValidationError(400, "Limit too large", fmt.Sprintf("Maximum limit is %d %s", maxLimit, limitDescription), "limit")
}
params.Set("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Before != "" {
params.Set("before", opts.Before)
}
if opts.After != "" {
params.Set("after", opts.After)
}
if opts.Reverse != nil {
params.Set("reverse", fmt.Sprintf("%t", *opts.Reverse))
}
}
return params, nil
}
// fetchRepliesData makes the API call and handles common error cases
func (c *Client) fetchRepliesData(path string, params url.Values, postID PostID, dataType string) (*RepliesResponse, error) {
resp, err := c.httpClient.GET(path, params, c.getAccessTokenSafe())
if err != nil {
return nil, err
}
// Handle specific error cases
if resp.StatusCode == 404 {
return nil, NewValidationError(404, "Post not found", fmt.Sprintf("Post with ID %s does not exist or is not accessible", postID.String()), "post_id")
}
if resp.StatusCode == 403 {
return nil, NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot access %s for post %s - insufficient permissions", dataType, postID.String()))
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var repliesResp RepliesResponse
if err := safeJSONUnmarshal(resp.Body, &repliesResp, dataType, resp.RequestID); err != nil {
return nil, err
}
return &repliesResp, nil
}
// GetReplies retrieves replies to a specific post with pagination support
func (c *Client) GetReplies(ctx context.Context, postID PostID, opts *RepliesOptions) (*RepliesResponse, error) {
if !postID.Valid() {
return nil, NewValidationError(400, ErrEmptyPostID, "Cannot retrieve replies without post ID", "post_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Build query parameters
params, err := buildRepliesParams(opts, 100, "replies per request")
if err != nil {
return nil, err
}
// Make API call to get post replies
path := fmt.Sprintf("/%s/replies", postID.String())
return c.fetchRepliesData(path, params, postID, "post replies")
}
// GetConversation retrieves a flattened conversation thread for a specific post
func (c *Client) GetConversation(ctx context.Context, postID PostID, opts *RepliesOptions) (*RepliesResponse, error) {
if !postID.Valid() {
return nil, NewValidationError(400, ErrEmptyPostID, "Cannot retrieve conversation without post ID", "post_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Build query parameters
params, err := buildRepliesParams(opts, 100, "posts per request")
if err != nil {
return nil, err
}
// Make API call to get conversation
path := fmt.Sprintf("/%s/conversation", postID.String())
return c.fetchRepliesData(path, params, postID, "conversation")
}
// GetPendingReplies retrieves pending replies for a post with reply approvals enabled
func (c *Client) GetPendingReplies(ctx context.Context, postID PostID, opts *PendingRepliesOptions) (*RepliesResponse, error) {
if !postID.Valid() {
return nil, NewValidationError(400, ErrEmptyPostID, "Cannot retrieve pending replies without post ID", "post_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Build query parameters
params := url.Values{
"fields": {ReplyFields},
}
if opts != nil {
if opts.Limit > 0 {
if opts.Limit > 100 {
return nil, NewValidationError(400, "Limit too large", "Maximum limit is 100 pending replies per request", "limit")
}
params.Set("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Before != "" {
params.Set("before", opts.Before)
}
if opts.After != "" {
params.Set("after", opts.After)
}
if opts.Reverse != nil {
params.Set("reverse", fmt.Sprintf("%t", *opts.Reverse))
}
if opts.ApprovalStatus != "" {
if opts.ApprovalStatus != ApprovalStatusPending && opts.ApprovalStatus != ApprovalStatusIgnored {
return nil, NewValidationError(400, "Invalid approval status", "Approval status must be 'pending' or 'ignored'", "approval_status")
}
params.Set("approval_status", string(opts.ApprovalStatus))
}
}
path := fmt.Sprintf("/%s/pending_replies", postID.String())
return c.fetchRepliesData(path, params, postID, "pending replies")
}
// ApprovePendingReply approves a pending reply, making it publicly visible
func (c *Client) ApprovePendingReply(ctx context.Context, replyID PostID) error {
return c.managePendingReply(ctx, replyID, true)
}
// IgnorePendingReply ignores a pending reply (it can still be approved later)
func (c *Client) IgnorePendingReply(ctx context.Context, replyID PostID) error {
return c.managePendingReply(ctx, replyID, false)
}
// managePendingReply handles approving or ignoring a pending reply
func (c *Client) managePendingReply(ctx context.Context, replyID PostID, approve bool) error {
action := "approve"
if !approve {
action = "ignore"
}
if !replyID.Valid() {
return NewValidationError(400, "Reply ID is required", fmt.Sprintf("Cannot %s reply without ID", action), "reply_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return err
}
params := url.Values{
"approve": {fmt.Sprintf("%t", approve)},
}
path := fmt.Sprintf("/%s/manage_pending_reply", replyID.String())
resp, err := c.httpClient.POST(path, params, c.getAccessTokenSafe())
if err != nil {
return err
}
if resp.StatusCode == 404 {
return NewValidationError(404, "Reply not found", fmt.Sprintf("Reply with ID %s does not exist or is not a pending reply", replyID.String()), "reply_id")
}
if resp.StatusCode == 403 {
return NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot %s reply %s - insufficient permissions or not the post owner", action, replyID.String()))
}
if resp.StatusCode != 200 {
return c.handleAPIError(resp)
}
var manageResp struct {
Success bool `json:"success"`
}
if len(resp.Body) > 0 {
if err := json.Unmarshal(resp.Body, &manageResp); err != nil {
if c.config.Logger != nil {
c.config.Logger.Warn(fmt.Sprintf("Could not parse %s reply response, but got 200 status", action), "reply_id", replyID.String())
}
}
}
if c.config.Logger != nil {
c.config.Logger.Info(fmt.Sprintf("Successfully %sd pending reply", action), "reply_id", replyID.String())
}
return nil
}
// manageReplyVisibility handles hiding and unhiding replies
func (c *Client) manageReplyVisibility(ctx context.Context, replyID PostID, hide bool) error {
action := "hide"
if !hide {
action = "unhide"
}
if !replyID.Valid() {
return NewValidationError(400, "Reply ID is required", fmt.Sprintf("Cannot %s reply without ID", action), "reply_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return err
}
// Build request parameters
params := url.Values{
"hide": {fmt.Sprintf("%t", hide)},
}
// Make API call to manage reply visibility
path := fmt.Sprintf("/%s/manage_reply", replyID.String())
resp, err := c.httpClient.POST(path, params, c.getAccessTokenSafe())
if err != nil {
return err
}
// Handle specific error cases
if resp.StatusCode == 404 {
return NewValidationError(404, "Reply not found", fmt.Sprintf("Reply with ID %s does not exist or is not accessible", replyID.String()), "reply_id")
}
if resp.StatusCode == 403 {
return NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot %s reply %s - insufficient permissions or not the post owner", action, replyID.String()))
}
if resp.StatusCode != 200 {
return c.handleAPIError(resp)
}
// Parse response to confirm action
var manageResp struct {
Success bool `json:"success"`
}
if len(resp.Body) > 0 {
if err := json.Unmarshal(resp.Body, &manageResp); err != nil {
// If we can't parse the response but got 200, assume success
if c.config.Logger != nil {
c.config.Logger.Warn(fmt.Sprintf("Could not parse %s reply response, but got 200 status", action), "reply_id", replyID.String())
}
}
}
// Log successful action if logger is available
if c.config.Logger != nil {
c.config.Logger.Info(fmt.Sprintf("Successfully %sd reply", action), "reply_id", replyID.String())
}
return nil
}
// HideReply hides a specific reply for moderation purposes
func (c *Client) HideReply(ctx context.Context, replyID PostID) error {
return c.manageReplyVisibility(ctx, replyID, true)
}
// UnhideReply unhides a specific reply that was previously hidden
func (c *Client) UnhideReply(ctx context.Context, replyID PostID) error {
return c.manageReplyVisibility(ctx, replyID, false)
}