-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposts_read.go
More file actions
313 lines (261 loc) · 8.66 KB
/
posts_read.go
File metadata and controls
313 lines (261 loc) · 8.66 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package threads
import (
"context"
"fmt"
"net/url"
)
// GetPost retrieves a specific post by ID with all available fields
func (c *Client) GetPost(ctx context.Context, postID PostID) (*Post, error) {
if !postID.Valid() {
return nil, NewValidationError(400, ErrEmptyPostID, "Cannot retrieve post without ID", "post_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Build query parameters with extended fields for comprehensive data
params := url.Values{
"fields": {PostExtendedFields},
}
// Make API call to get post
path := fmt.Sprintf("/%s", postID.String())
resp, err := c.httpClient.GET(path, params, c.getAccessTokenSafe())
if err != nil {
return nil, err
}
// Handle specific error cases for non-existent posts
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 != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var post Post
if err := safeJSONUnmarshal(resp.Body, &post, "post response", resp.RequestID); err != nil {
return nil, err
}
return &post, nil
}
// GetUserPosts retrieves posts from a specific user with pagination support
func (c *Client) GetUserPosts(ctx context.Context, userID UserID, opts *PaginationOptions) (*PostsResponse, error) {
// Convert PaginationOptions to PostsOptions for backward compatibility
var postsOpts *PostsOptions
if opts != nil {
postsOpts = &PostsOptions{
Limit: opts.Limit,
Before: opts.Before,
After: opts.After,
}
}
return c.GetUserPostsWithOptions(ctx, userID, postsOpts)
}
// GetUserPostsWithOptions retrieves posts from a specific user with enhanced options
func (c *Client) GetUserPostsWithOptions(ctx context.Context, userID UserID, opts *PostsOptions) (*PostsResponse, error) {
if !userID.Valid() {
return nil, NewValidationError(400, ErrEmptyUserID, "Cannot retrieve posts without user ID", "user_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Validate options
validator := NewValidator()
if err := validator.ValidatePostsOptions(opts); err != nil {
return nil, err
}
// Build query parameters with enhanced fields from API documentation
params := url.Values{
"fields": {PostExtendedFields},
}
// Add pagination and filtering options if provided
if opts != nil {
if opts.Limit > 0 {
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.Since > 0 {
params.Set("since", fmt.Sprintf("%d", opts.Since))
}
if opts.Until > 0 {
params.Set("until", fmt.Sprintf("%d", opts.Until))
}
}
// Make API call to get user posts
path := fmt.Sprintf("/%s/threads", userID.String())
resp, err := c.httpClient.GET(path, params, c.getAccessTokenSafe())
if err != nil {
return nil, err
}
// Handle specific error cases for non-existent users
if resp.StatusCode == 404 {
return nil, NewValidationError(404, "User not found", fmt.Sprintf("User with ID %s does not exist or is not accessible", userID.String()), "user_id")
}
// Handle permission errors
if resp.StatusCode == 403 {
return nil, NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot access posts for user %s - insufficient permissions", userID.String()))
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var postsResp PostsResponse
if err := safeJSONUnmarshal(resp.Body, &postsResp, "posts response", resp.RequestID); err != nil {
return nil, err
}
return &postsResp, nil
}
// GetUserMentions retrieves posts where the user is mentioned
func (c *Client) GetUserMentions(ctx context.Context, userID UserID, opts *PostsOptions) (*PostsResponse, error) {
if !userID.Valid() {
return nil, NewValidationError(400, ErrEmptyUserID, "Cannot retrieve mentions without user ID", "user_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Validate options
validator := NewValidator()
if err := validator.ValidatePostsOptions(opts); err != nil {
return nil, err
}
// Build query parameters
params := url.Values{
"fields": {PostExtendedFields},
}
// Add pagination and filtering options if provided
if opts != nil {
if opts.Limit > 0 {
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.Since > 0 {
params.Set("since", fmt.Sprintf("%d", opts.Since))
}
if opts.Until > 0 {
params.Set("until", fmt.Sprintf("%d", opts.Until))
}
}
// Make API call to get user mentions
path := fmt.Sprintf("/%s/mentions", userID.String())
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, "User not found", fmt.Sprintf("User with ID %s does not exist or is not accessible", userID.String()), "user_id")
}
if resp.StatusCode == 403 {
return nil, NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot access mentions for user %s - insufficient permissions", userID.String()))
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var postsResp PostsResponse
if err := safeJSONUnmarshal(resp.Body, &postsResp, "mentions response", resp.RequestID); err != nil {
return nil, err
}
return &postsResp, nil
}
// GetPublishingLimits retrieves the current API quota usage for the user
func (c *Client) GetPublishingLimits(ctx context.Context) (*PublishingLimits, error) {
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Get user ID from token info
userID := c.getUserID()
if userID == "" {
return nil, NewAuthenticationError(401, "User ID not available", "Cannot determine user ID from token")
}
// Build query parameters
params := url.Values{
"fields": {PublishingLimitFields},
}
// Make API call
path := fmt.Sprintf("/%s/threads_publishing_limit", userID)
resp, err := c.httpClient.GET(path, params, c.getAccessTokenSafe())
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var limitsResp struct {
Data []PublishingLimits `json:"data"`
}
if err := safeJSONUnmarshal(resp.Body, &limitsResp, "publishing limits response", resp.RequestID); err != nil {
return nil, err
}
if len(limitsResp.Data) == 0 {
return nil, NewAPIError(resp.StatusCode, "No publishing limits data returned", "API response missing data", resp.RequestID)
}
return &limitsResp.Data[0], nil
}
// GetUserGhostPosts retrieves ghost posts from a specific user.
// Ghost posts are text-only posts that expire after 24 hours.
func (c *Client) GetUserGhostPosts(ctx context.Context, userID UserID, opts *PaginationOptions) (*PostsResponse, error) {
if !userID.Valid() {
return nil, NewValidationError(400, ErrEmptyUserID, "Cannot retrieve ghost posts without user ID", "user_id")
}
// Ensure we have a valid token
if err := c.EnsureValidToken(ctx); err != nil {
return nil, err
}
// Validate pagination options
validator := NewValidator()
if err := validator.ValidatePaginationOptions(opts); err != nil {
return nil, err
}
// Build query parameters with ghost post fields
params := url.Values{
"fields": {GhostPostFields},
}
// Add pagination options if provided
if opts != nil {
if opts.Limit > 0 {
params.Set("limit", fmt.Sprintf("%d", opts.Limit))
}
if opts.Before != "" {
params.Set("before", opts.Before)
}
if opts.After != "" {
params.Set("after", opts.After)
}
}
// Make API call to get ghost posts
path := fmt.Sprintf("/%s/ghost_posts", userID.String())
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, "User not found", fmt.Sprintf("User with ID %s does not exist or is not accessible", userID.String()), "user_id")
}
if resp.StatusCode == 403 {
return nil, NewAuthenticationError(403, "Access denied", fmt.Sprintf("Cannot access ghost posts for user %s - insufficient permissions", userID.String()))
}
if resp.StatusCode != 200 {
return nil, c.handleAPIError(resp)
}
// Parse response
var postsResp PostsResponse
if err := safeJSONUnmarshal(resp.Body, &postsResp, "ghost posts response", resp.RequestID); err != nil {
return nil, err
}
return &postsResp, nil
}